|
| 1 | +""" |
| 2 | +Each modeling file in this library is a mapping between |
| 3 | +abstract naming of intervention anchor points and actual |
| 4 | +model module defined in the huggingface library. |
| 5 | +
|
| 6 | +We also want to let the intervention library know how to |
| 7 | +config the dimensions of intervention based on model config |
| 8 | +defined in the huggingface library. |
| 9 | +""" |
| 10 | + |
| 11 | +from ..constants import * |
| 12 | + |
| 13 | + |
| 14 | +"""gpt-oss base model""" |
| 15 | +gpt_oss_type_to_module_mapping = { |
| 16 | + "block_input": ("layers[%s]", CONST_INPUT_HOOK), |
| 17 | + "block_output": ("layers[%s]", CONST_OUTPUT_HOOK), |
| 18 | + "mlp_input": ("layers[%s].mlp", CONST_INPUT_HOOK), |
| 19 | + "mlp_output": ("layers[%s].mlp", CONST_OUTPUT_HOOK), |
| 20 | + "router_input": ("layers[%s].mlp.router", CONST_INPUT_HOOK), |
| 21 | + "router_output": ("layers[%s].mlp.router", CONST_OUTPUT_HOOK), |
| 22 | + "expert_input": ("layers[%s].mlp.experts", CONST_INPUT_HOOK), |
| 23 | + "expert_output": ("layers[%s].mlp.experts", CONST_OUTPUT_HOOK), |
| 24 | + "attention_input": ("layers[%s].self_attn", CONST_INPUT_HOOK), |
| 25 | + "attention_output": ("layers[%s].self_attn", CONST_OUTPUT_HOOK), |
| 26 | + "attention_value_output": ("layers[%s].self_attn.o_proj", CONST_INPUT_HOOK), |
| 27 | + "head_attention_value_output": ( |
| 28 | + "layers[%s].self_attn.o_proj", |
| 29 | + CONST_INPUT_HOOK, |
| 30 | + (split_head_and_permute, "num_attention_heads"), |
| 31 | + ), |
| 32 | + "query_output": ("layers[%s].self_attn.q_proj", CONST_OUTPUT_HOOK), |
| 33 | + "key_output": ("layers[%s].self_attn.k_proj", CONST_OUTPUT_HOOK), |
| 34 | + "value_output": ("layers[%s].self_attn.v_proj", CONST_OUTPUT_HOOK), |
| 35 | + "head_query_output": ( |
| 36 | + "layers[%s].self_attn.q_proj", |
| 37 | + CONST_OUTPUT_HOOK, |
| 38 | + (split_head_and_permute, "num_attention_heads"), |
| 39 | + ), |
| 40 | + "head_key_output": ( |
| 41 | + "layers[%s].self_attn.k_proj", |
| 42 | + CONST_OUTPUT_HOOK, |
| 43 | + (split_head_and_permute, "num_key_value_heads"), |
| 44 | + ), |
| 45 | + "head_value_output": ( |
| 46 | + "layers[%s].self_attn.v_proj", |
| 47 | + CONST_OUTPUT_HOOK, |
| 48 | + (split_head_and_permute, "num_key_value_heads"), |
| 49 | + ), |
| 50 | +} |
| 51 | + |
| 52 | + |
| 53 | +gpt_oss_type_to_dimension_mapping = { |
| 54 | + "num_attention_heads": ("num_attention_heads",), |
| 55 | + "num_key_value_heads": ("num_key_value_heads",), |
| 56 | + "num_local_experts": ("num_local_experts",), |
| 57 | + "num_experts_per_tok": ("num_experts_per_tok",), |
| 58 | + "block_input": ("hidden_size",), |
| 59 | + "block_output": ("hidden_size",), |
| 60 | + "mlp_input": ("hidden_size",), |
| 61 | + "mlp_output": ("hidden_size",), |
| 62 | + "router_input": ("hidden_size",), |
| 63 | + "router_output": ("num_local_experts",), |
| 64 | + "expert_input": ("hidden_size",), |
| 65 | + "expert_output": ("hidden_size",), |
| 66 | + "attention_input": ("hidden_size",), |
| 67 | + "attention_output": ("hidden_size",), |
| 68 | + "attention_value_output": ("hidden_size",), |
| 69 | + "head_attention_value_output": ("hidden_size/num_attention_heads",), |
| 70 | + "query_output": ("hidden_size",), |
| 71 | + "key_output": ("hidden_size",), |
| 72 | + "value_output": ("hidden_size",), |
| 73 | + "head_query_output": ("hidden_size/num_attention_heads",), |
| 74 | + "head_key_output": ("hidden_size/num_key_value_heads",), |
| 75 | + "head_value_output": ("hidden_size/num_key_value_heads",), |
| 76 | +} |
| 77 | + |
| 78 | + |
| 79 | +"""gpt-oss model with LM head""" |
| 80 | +gpt_oss_lm_type_to_module_mapping = {} |
| 81 | +for k, v in gpt_oss_type_to_module_mapping.items(): |
| 82 | + gpt_oss_lm_type_to_module_mapping[k] = (f"model.{v[0]}",) + v[1:] |
| 83 | + |
| 84 | +gpt_oss_lm_type_to_dimension_mapping = gpt_oss_type_to_dimension_mapping |
| 85 | + |
| 86 | + |
| 87 | +def create_gpt_oss(name="openai/gpt-oss-20b", cache_dir=None, access_token=None): |
| 88 | + """Creates a GPT-OSS model, config, and tokenizer from the given name and revision""" |
| 89 | + from transformers import AutoModelForCausalLM, AutoTokenizer, AutoConfig |
| 90 | + |
| 91 | + config = AutoConfig.from_pretrained(name, cache_dir=cache_dir, token=access_token) |
| 92 | + tokenizer = AutoTokenizer.from_pretrained( |
| 93 | + name, cache_dir=cache_dir, token=access_token |
| 94 | + ) |
| 95 | + gpt_oss = AutoModelForCausalLM.from_pretrained( |
| 96 | + name, cache_dir=cache_dir, token=access_token |
| 97 | + ) |
| 98 | + print("loaded model") |
| 99 | + return config, tokenizer, gpt_oss |
0 commit comments