|
| 1 | +from typing_extensions import override |
| 2 | +import torch |
| 3 | +from ..modules import RMSNorm, Embedding, TransformerBlock, Attention, MLP, Linear |
| 4 | +from ..model.config import Config, no_default |
| 5 | +from ..model.model import Model |
| 6 | +from ..util.rope import RopeStyle |
| 7 | +from ..modules.attn import prepare_for_attn |
| 8 | + |
| 9 | +class ArceeConfig(Config): |
| 10 | + arch_string = "ArceeForCausalLM" |
| 11 | + |
| 12 | + def __init__( |
| 13 | + self, |
| 14 | + directory: str, |
| 15 | + derived_model: dict | None = None, |
| 16 | + **kwargs, |
| 17 | + ): |
| 18 | + super().__init__( |
| 19 | + directory, |
| 20 | + derived_model if derived_model else {"text": ArceeModel}, |
| 21 | + **kwargs |
| 22 | + ) |
| 23 | + |
| 24 | + # Attention params |
| 25 | + self.head_dim = self.read_cfg(int, "head_dim", None) |
| 26 | + self.hidden_size = self.read_cfg(int, "hidden_size", no_default) |
| 27 | + self.num_q_heads = self.read_cfg(int, "num_attention_heads", no_default) |
| 28 | + self.num_kv_heads = self.read_cfg(int, "num_key_value_heads", self.num_q_heads) |
| 29 | + |
| 30 | + if not self.head_dim: |
| 31 | + self.head_dim = self.hidden_size // self.num_q_heads |
| 32 | + |
| 33 | + # MLP params |
| 34 | + self.assert_cfg(str, "hidden_act", "relu2", True) |
| 35 | + self.intermediate_size = self.read_cfg(int, "intermediate_size", no_default) |
| 36 | + |
| 37 | + # Norms |
| 38 | + self.rms_norm_eps = self.read_cfg(float, "rms_norm_eps", no_default) |
| 39 | + |
| 40 | + # Layers |
| 41 | + self.num_hidden_layers = self.read_cfg(int, "num_hidden_layers", no_default) |
| 42 | + self.tie_word_embeddings = self.read_cfg(bool, "tie_word_embeddings", False) |
| 43 | + |
| 44 | + # RoPE |
| 45 | + self.rope_settings = self.read_rope_settings_default(RopeStyle.NEOX) |
| 46 | + |
| 47 | + |
| 48 | +class ArceeModel(Model): |
| 49 | + config_class = ArceeConfig |
| 50 | + |
| 51 | + def __init__( |
| 52 | + self, |
| 53 | + config: ArceeConfig, |
| 54 | + **kwargs |
| 55 | + ): |
| 56 | + super().__init__(config, **kwargs) |
| 57 | + |
| 58 | + self.modules += [ |
| 59 | + Embedding( |
| 60 | + config = config, |
| 61 | + key = "model.embed_tokens", |
| 62 | + vocab_size = config.vocab_size, |
| 63 | + hidden_size = config.hidden_size, |
| 64 | + ) |
| 65 | + ] |
| 66 | + |
| 67 | + self.first_block_idx = len(self.modules) |
| 68 | + |
| 69 | + self.modules += [ |
| 70 | + TransformerBlock( |
| 71 | + config = config, |
| 72 | + key = f"model.layers.{idx}", |
| 73 | + attn_norm = RMSNorm( |
| 74 | + config = config, |
| 75 | + key = f"model.layers.{idx}.input_layernorm", |
| 76 | + rms_norm_eps = config.rms_norm_eps, |
| 77 | + ), |
| 78 | + attn = Attention( |
| 79 | + config = config, |
| 80 | + key = f"model.layers.{idx}.self_attn", |
| 81 | + layer_idx = idx, |
| 82 | + hidden_size = config.hidden_size, |
| 83 | + head_dim = config.head_dim, |
| 84 | + num_q_heads = config.num_q_heads, |
| 85 | + num_kv_heads = config.num_kv_heads, |
| 86 | + rope_settings = config.rope_settings, |
| 87 | + sm_scale = None, |
| 88 | + key_q = "q_proj", |
| 89 | + key_k = "k_proj", |
| 90 | + key_v = "v_proj", |
| 91 | + key_o = "o_proj", |
| 92 | + qmap = "block.attn", |
| 93 | + ), |
| 94 | + mlp_norm = RMSNorm( |
| 95 | + config = config, |
| 96 | + key = f"model.layers.{idx}.post_attention_layernorm", |
| 97 | + rms_norm_eps = config.rms_norm_eps, |
| 98 | + ), |
| 99 | + mlp = MLP( |
| 100 | + config = config, |
| 101 | + key = f"model.layers.{idx}.mlp", |
| 102 | + hidden_size = config.hidden_size, |
| 103 | + intermediate_size = config.intermediate_size, |
| 104 | + key_up = "up_proj", |
| 105 | + key_down = "down_proj", |
| 106 | + qmap = "block.mlp", |
| 107 | + activation_fn = "relu2", |
| 108 | + out_dtype = torch.float, |
| 109 | + ), |
| 110 | + ) |
| 111 | + for idx in range(config.num_hidden_layers) |
| 112 | + ] |
| 113 | + |
| 114 | + self.last_kv_module_idx = len(self.modules) - 1 |
| 115 | + |
| 116 | + head_alt_key = None |
| 117 | + if config.tie_word_embeddings and not self.config.stc.has_tensor("lm_head"): |
| 118 | + head_alt_key = "model.embed_tokens" |
| 119 | + |
| 120 | + self.modules += [ |
| 121 | + RMSNorm( |
| 122 | + config = config, |
| 123 | + key = "model.norm", |
| 124 | + rms_norm_eps = config.rms_norm_eps, |
| 125 | + out_dtype = torch.half, |
| 126 | + ), |
| 127 | + Linear( |
| 128 | + config = config, |
| 129 | + key = "lm_head", |
| 130 | + qbits_key = "head_bits", |
| 131 | + alt_key = head_alt_key, |
| 132 | + in_features = config.hidden_size, |
| 133 | + out_features = config.vocab_size, |
| 134 | + qmap = "block", |
| 135 | + caps = {"logits_output": True} |
| 136 | + ) |
| 137 | + ] |
| 138 | + |
| 139 | + self.logit_layer_idx = len(self.modules) - 1 |
| 140 | + |
| 141 | + @override |
| 142 | + def prepare_inputs(self, input_ids: torch.Tensor, params: dict) -> torch.Tensor: |
| 143 | + params["input_ids"] = input_ids |
| 144 | + input_ids = prepare_for_attn(input_ids, params) |
| 145 | + return input_ids |
| 146 | + |
| 147 | + @override |
| 148 | + def default_chat_prompt(self, prompt: str, system_prompt: str | None = None) -> str: |
| 149 | + p = "<|begin_of_text|>" |
| 150 | + if system_prompt: |
| 151 | + p += f"<|im_start|>system\n{system_prompt}<|im_end|>\n" |
| 152 | + p += f"<|im_start|>user\n{prompt}<|im_end|>\n<|im_start|>assistant\n" |
| 153 | + return p |
0 commit comments