okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
b2de2641f
main
1from __future__import annotations 2 3import re 4 5from typing import Iterable ,TYPE_CHECKING 6 7if TYPE_CHECKING : 8from torch import Tensor 9 10from .base import ModelBase ,TextModel ,gguf 11 12 13@ ModelBase . register ( "HrmTextForCausalLM" ) 14@ ModelBase . example ( "danish-foundation-models/DFM-Mimir" ) 15class HrmTextModel (TextModel ): 16model_arch = gguf .MODEL_ARCH .HRM_TEXT 17 18def __init__ (self ,* args ,** kwargs ): 19super ().__init__ (* args ,** kwargs ) 20 21# training-style configs store the per-stack count in num_hidden_layers, 22# transformers-style configs keep it in num_layers_per_stack 23self .layers_per_stack = self .hparams .get ("num_layers_per_stack" )or self .hparams ["num_hidden_layers" ] 24self .h_cycles = self .hparams ["H_cycles" ] 25self .l_cycles = self .hparams ["L_cycles" ] 26 27# block_count is the expanded cache-slot count; the file only holds 28# 2 * layers_per_stack physical blocks 29self .block_count = self .layers_per_stack * self .h_cycles * (self .l_cycles + 1 ) 30self .tensor_map = gguf .get_tensor_name_map (self .model_arch ,2 * self .layers_per_stack ) 31 32def set_vocab (self ): 33self ._set_vocab_gpt2 () 34 35def set_gguf_parameters (self ): 36super ().set_gguf_parameters () 37 38head_dim = self .hparams .get ("head_dim" )or self .hparams ["hidden_size" ]// self .hparams ["num_attention_heads" ] 39self .gguf_writer .add_rope_dimension_count (head_dim ) 40self .gguf_writer .add_embedding_scale (self .hparams ["embedding_scale" ]) 41self .gguf_writer .add_hrm_layers_per_stack (self .layers_per_stack ) 42self .gguf_writer .add_hrm_h_cycles (self .h_cycles ) 43self .gguf_writer .add_hrm_l_cycles (self .l_cycles ) 44self .gguf_writer .add_hrm_prefix_lm (bool (self .hparams .get ("prefix_lm" ,False ))) 45 46def modify_tensors (self ,data_torch :Tensor ,name :str ,bid :int | None )-> Iterable [tuple [str ,Tensor ]]: 47if name == "model.embed_tokens.weight" : 48yield self .format_tensor_name (gguf .MODEL_TENSOR .TOKEN_EMBD ),data_torch 49return 50if name == "lm_head.weight" : 51yield self .format_tensor_name (gguf .MODEL_TENSOR .OUTPUT ),data_torch 52return 53if name == "model.z_L_init" : 54yield self .format_tensor_name (gguf .MODEL_TENSOR .HRM_Z_L_INIT ,suffix = "" ),data_torch 55return 56 57match = re .fullmatch (r"model\.([LH])_module\.layers\.(\d+)\.(.+)" ,name ) 58if match is None : 59raise ValueError (f"can not map tensor: { name } " ) 60 61stack ,layer_s ,tensor_name = match .groups () 62# the L stack occupies blocks [0, layers_per_stack), the H stack follows it 63layer_idx = int (layer_s )+ (self .layers_per_stack if stack == "H" else 0 ) 64 65if tensor_name == "attn.gqkv_proj.weight" : 66gate ,q ,k ,v = data_torch .chunk (4 ,dim = 0 ) 67yield self .format_tensor_name (gguf .MODEL_TENSOR .ATTN_GATE ,layer_idx ),gate .contiguous () 68yield self .format_tensor_name (gguf .MODEL_TENSOR .ATTN_Q ,layer_idx ),q .contiguous () 69yield self .format_tensor_name (gguf .MODEL_TENSOR .ATTN_K ,layer_idx ),k .contiguous () 70yield self .format_tensor_name (gguf .MODEL_TENSOR .ATTN_V ,layer_idx ),v .contiguous () 71elif tensor_name == "mlp.gate_up_proj.weight" : 72gate ,up = data_torch .chunk (2 ,dim = 0 ) 73yield self .format_tensor_name (gguf .MODEL_TENSOR .FFN_GATE ,layer_idx ),gate .contiguous () 74yield self .format_tensor_name (gguf .MODEL_TENSOR .FFN_UP ,layer_idx ),up .contiguous () 75else : 76if tensor_name .startswith ("attn." ): 77tensor_name = "self_attn." + tensor_name [len ("attn." ):] 78tensor_name = "model.layers.{bid}." + tensor_name 79yield from super ().modify_tensors (data_torch ,tensor_name .format (bid = layer_idx ),layer_idx )