ToastyPigeon commited on
Commit
3361c35
·
verified ·
1 Parent(s): ddd3ad6

Upload folder using huggingface_hub

Browse files
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ tokenizer.json filter=lfs diff=lfs merge=lfs -text
config.json ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "Gemma3ForCausalLM"
4
+ ],
5
+ "auto_map": {
6
+ "AutoConfig": "configuration_gemmagain.GemmagainConfig",
7
+ "AutoModelForCausalLM": "modeling_gemmagain.GemmagainForCausalLM"
8
+ },
9
+ "attention_bias": false,
10
+ "attention_dropout": 0.0,
11
+ "attn_logit_softcapping": null,
12
+ "cache_implementation": "hybrid",
13
+ "final_logit_softcapping": null,
14
+ "head_dim": 256,
15
+ "hidden_activation": "gelu_pytorch_tanh",
16
+ "hidden_size": 2560,
17
+ "initializer_range": 0.02,
18
+ "intermediate_size": 10240,
19
+ "max_position_embeddings": 131072,
20
+ "model_type": "gemma3",
21
+ "num_attention_heads": 8,
22
+ "num_hidden_layers": 34,
23
+ "num_key_value_heads": 4,
24
+ "query_pre_attn_scalar": 256,
25
+ "rms_norm_eps": 1e-06,
26
+ "rope_local_base_freq": 10000.0,
27
+ "rope_scaling": {
28
+ "factor": 8.0,
29
+ "rope_type": "linear"
30
+ },
31
+ "rope_theta": 1000000.0,
32
+ "sliding_window": 1024,
33
+ "sliding_window_pattern": 6,
34
+ "layer_sequence": [[0, 34, 1]],
35
+ "torch_dtype": "bfloat16",
36
+ "use_cache": true,
37
+ "vocab_size": 262208,
38
+ "transformers_version": "4.51.0"
39
+ }
configuration_gemmagain.py ADDED
@@ -0,0 +1,186 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2025 Google Inc. HuggingFace Inc. team. All rights reserved.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ """Gemmagain model configuration - Gemma3 with layer looping support"""
16
+
17
+ from transformers.configuration_utils import PretrainedConfig, layer_type_validation
18
+ from transformers.modeling_rope_utils import rope_config_validation
19
+ from transformers.utils import logging
20
+
21
+
22
+ logger = logging.get_logger(__name__)
23
+
24
+
25
+ class GemmagainConfig(PretrainedConfig):
26
+ r"""
27
+ Configuration class for Gemmagain - a Gemma3 text model with layer looping support.
28
+
29
+ This extends Gemma3TextConfig to add the `layer_sequence` parameter which controls
30
+ how layers are executed, allowing layers to be repeated multiple times.
31
+
32
+ Args:
33
+ vocab_size (`int`, *optional*, defaults to 262208):
34
+ Vocabulary size of the model.
35
+ hidden_size (`int`, *optional*, defaults to 2560):
36
+ Dimension of the hidden representations.
37
+ intermediate_size (`int`, *optional*, defaults to 10240):
38
+ Dimension of the MLP representations.
39
+ num_hidden_layers (`int`, *optional*, defaults to 34):
40
+ Number of hidden layers in the Transformer decoder.
41
+ num_attention_heads (`int`, *optional*, defaults to 8):
42
+ Number of attention heads for each attention layer.
43
+ num_key_value_heads (`int`, *optional*, defaults to 4):
44
+ Number of key_value heads for GQA.
45
+ head_dim (`int`, *optional*, defaults to 256):
46
+ The attention head dimension.
47
+ hidden_activation (`str`, *optional*, defaults to `"gelu_pytorch_tanh"`):
48
+ The activation function.
49
+ max_position_embeddings (`int`, *optional*, defaults to 131072):
50
+ Maximum sequence length.
51
+ layer_sequence (`list`, *optional*):
52
+ Order to execute layers. Defaults to all layers once.
53
+ Flexible format - each item can be:
54
+ - An integer: single layer index (e.g., 5 means layer 5)
55
+ - A 2-element list [start, end]: range of layers (e.g., [4, 20] means layers 4-19)
56
+ - A 3-element list [start, end, repeats]: range repeated N times
57
+ Examples:
58
+ - [[0, 34, 1]]: all 34 layers once
59
+ - [[0, 10], [10, 28, 2], [28, 34]]: layers 0-9, then 10-27 twice, then 28-33
60
+ layer_types (`list`, *optional*):
61
+ Attention pattern for each layer ("sliding_attention" or "full_attention").
62
+ sliding_window (`int`, *optional*, defaults to 1024):
63
+ Size of the sliding window for sliding attention layers.
64
+ rope_theta (`float`, *optional*, defaults to 1000000.0):
65
+ Base period for RoPE embeddings (global attention).
66
+ rope_local_base_freq (`float`, *optional*, defaults to 10000.0):
67
+ Base period for RoPE embeddings (local/sliding attention).
68
+ query_pre_attn_scalar (`float`, *optional*, defaults to 256):
69
+ Scaling factor for attention scores.
70
+ rms_norm_eps (`float`, *optional*, defaults to 1e-6):
71
+ Epsilon for RMS normalization.
72
+ attention_bias (`bool`, *optional*, defaults to False):
73
+ Whether to use bias in attention projections.
74
+ attention_dropout (`float`, *optional*, defaults to 0.0):
75
+ Dropout ratio for attention.
76
+ final_logit_softcapping (`float`, *optional*):
77
+ Softcapping for final logits.
78
+ attn_logit_softcapping (`float`, *optional*):
79
+ Softcapping for attention logits.
80
+ rope_scaling (`dict`, *optional*):
81
+ RoPE scaling configuration.
82
+ use_bidirectional_attention (`bool`, *optional*, defaults to False):
83
+ If True, use bidirectional attention instead of causal.
84
+ """
85
+
86
+ model_type = "gemma3"
87
+ keys_to_ignore_at_inference = ["past_key_values"]
88
+ base_model_tp_plan = {
89
+ "layers.*.self_attn.q_proj": "colwise",
90
+ "layers.*.self_attn.k_proj": "colwise",
91
+ "layers.*.self_attn.v_proj": "colwise",
92
+ "layers.*.self_attn.o_proj": "rowwise",
93
+ "layers.*.mlp.gate_proj": "colwise",
94
+ "layers.*.mlp.up_proj": "colwise",
95
+ "layers.*.mlp.down_proj": "rowwise",
96
+ }
97
+ base_model_pp_plan = {
98
+ "embed_tokens": (["input_ids"], ["inputs_embeds"]),
99
+ "layers": (["hidden_states", "attention_mask"], ["hidden_states"]),
100
+ "norm": (["hidden_states"], ["hidden_states"]),
101
+ }
102
+
103
+ def __init__(
104
+ self,
105
+ vocab_size=262_208,
106
+ hidden_size=2560,
107
+ intermediate_size=10240,
108
+ num_hidden_layers=34,
109
+ num_attention_heads=8,
110
+ num_key_value_heads=4,
111
+ head_dim=256,
112
+ hidden_activation="gelu_pytorch_tanh",
113
+ max_position_embeddings=131_072,
114
+ initializer_range=0.02,
115
+ rms_norm_eps=1e-6,
116
+ use_cache=True,
117
+ pad_token_id=0,
118
+ eos_token_id=1,
119
+ bos_token_id=2,
120
+ tie_word_embeddings=True,
121
+ rope_theta=1_000_000.0,
122
+ attention_bias=False,
123
+ attention_dropout=0.0,
124
+ query_pre_attn_scalar=256,
125
+ sliding_window=1024,
126
+ layer_types=None,
127
+ layer_sequence=None,
128
+ final_logit_softcapping=None,
129
+ attn_logit_softcapping=None,
130
+ rope_scaling=None,
131
+ rope_local_base_freq=10_000.0,
132
+ use_bidirectional_attention=False,
133
+ **kwargs,
134
+ ):
135
+ super().__init__(
136
+ pad_token_id=pad_token_id,
137
+ bos_token_id=bos_token_id,
138
+ eos_token_id=eos_token_id,
139
+ tie_word_embeddings=tie_word_embeddings,
140
+ **kwargs,
141
+ )
142
+ self.vocab_size = vocab_size
143
+ self.max_position_embeddings = max_position_embeddings
144
+ self.hidden_size = hidden_size
145
+ self.intermediate_size = intermediate_size
146
+ self.num_hidden_layers = num_hidden_layers
147
+ self.num_attention_heads = num_attention_heads
148
+ self.head_dim = head_dim
149
+ self.num_key_value_heads = num_key_value_heads
150
+ self.initializer_range = initializer_range
151
+ self.rms_norm_eps = rms_norm_eps
152
+ self.use_cache = use_cache
153
+ self.rope_theta = rope_theta
154
+ self.attention_bias = attention_bias
155
+ self.attention_dropout = attention_dropout
156
+ self.hidden_activation = hidden_activation
157
+ self.query_pre_attn_scalar = query_pre_attn_scalar
158
+ self.sliding_window = sliding_window
159
+ self.final_logit_softcapping = final_logit_softcapping
160
+ self.attn_logit_softcapping = attn_logit_softcapping
161
+ self.use_bidirectional_attention = use_bidirectional_attention
162
+
163
+ if use_bidirectional_attention:
164
+ self.sliding_window = (self.sliding_window // 2) + 1
165
+
166
+ self.rope_local_base_freq = rope_local_base_freq
167
+ self.rope_scaling = rope_scaling
168
+ rope_config_validation(self)
169
+
170
+ # Layer sequence for looping - defaults to all layers once
171
+ if layer_sequence is None:
172
+ layer_sequence = [[0, num_hidden_layers, 1]]
173
+ self.layer_sequence = layer_sequence
174
+
175
+ # Layer types (sliding vs full attention)
176
+ self._sliding_window_pattern = kwargs.get("sliding_window_pattern", 6)
177
+ self.layer_types = layer_types
178
+ if self.layer_types is None:
179
+ self.layer_types = [
180
+ "sliding_attention" if bool((i + 1) % self._sliding_window_pattern) else "full_attention"
181
+ for i in range(self.num_hidden_layers)
182
+ ]
183
+ layer_type_validation(self.layer_types, self.num_hidden_layers)
184
+
185
+
186
+ __all__ = ["GemmagainConfig"]
generation_config.json ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "bos_token_id": 2,
3
+ "cache_implementation": "hybrid",
4
+ "do_sample": true,
5
+ "eos_token_id": [
6
+ 1,
7
+ 106
8
+ ],
9
+ "pad_token_id": 0,
10
+ "top_k": 64,
11
+ "top_p": 0.95,
12
+ "transformers_version": "4.51.0"
13
+ }
model-00001-of-00002.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:c3161a965bea27504aeca1bab9330709f275d1f105b4c40cd3c7c7531f78f1b7
3
+ size 3639022304
model-00002-of-00002.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:29d9dde61d2a3dae399d1011ad1b19cfd4881ec1cc72ce7bc23f6b63173a1756
3
+ size 4121555624
model.safetensors.index.json ADDED
@@ -0,0 +1,451 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "metadata": {
3
+ "total_size": 7760526336
4
+ },
5
+ "weight_map": {
6
+ "model.layers.14.input_layernorm.weight": "model-00001-of-00002.safetensors",
7
+ "model.layers.14.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
8
+ "model.layers.14.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
9
+ "model.layers.14.post_feedforward_layernorm.weight": "model-00001-of-00002.safetensors",
10
+ "model.layers.14.pre_feedforward_layernorm.weight": "model-00001-of-00002.safetensors",
11
+ "model.layers.15.input_layernorm.weight": "model-00001-of-00002.safetensors",
12
+ "model.layers.15.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
13
+ "model.layers.15.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
14
+ "model.layers.15.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
15
+ "model.layers.15.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
16
+ "model.layers.15.post_feedforward_layernorm.weight": "model-00001-of-00002.safetensors",
17
+ "model.layers.15.pre_feedforward_layernorm.weight": "model-00001-of-00002.safetensors",
18
+ "model.layers.15.self_attn.k_norm.weight": "model-00001-of-00002.safetensors",
19
+ "model.layers.15.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
20
+ "model.layers.15.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
21
+ "model.layers.15.self_attn.q_norm.weight": "model-00001-of-00002.safetensors",
22
+ "model.layers.15.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
23
+ "model.layers.15.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
24
+ "model.layers.16.input_layernorm.weight": "model-00001-of-00002.safetensors",
25
+ "model.layers.16.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
26
+ "model.layers.16.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
27
+ "model.layers.16.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
28
+ "model.layers.16.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
29
+ "model.layers.16.post_feedforward_layernorm.weight": "model-00001-of-00002.safetensors",
30
+ "model.layers.16.pre_feedforward_layernorm.weight": "model-00001-of-00002.safetensors",
31
+ "model.layers.16.self_attn.k_norm.weight": "model-00001-of-00002.safetensors",
32
+ "model.layers.16.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
33
+ "model.layers.16.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
34
+ "model.layers.16.self_attn.q_norm.weight": "model-00001-of-00002.safetensors",
35
+ "model.layers.16.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
36
+ "model.layers.16.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
37
+ "model.layers.17.input_layernorm.weight": "model-00001-of-00002.safetensors",
38
+ "model.layers.17.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
39
+ "model.layers.17.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
40
+ "model.layers.17.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
41
+ "model.layers.17.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
42
+ "model.layers.17.post_feedforward_layernorm.weight": "model-00001-of-00002.safetensors",
43
+ "model.layers.17.pre_feedforward_layernorm.weight": "model-00001-of-00002.safetensors",
44
+ "model.layers.17.self_attn.k_norm.weight": "model-00001-of-00002.safetensors",
45
+ "model.layers.17.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
46
+ "model.layers.17.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
47
+ "model.layers.17.self_attn.q_norm.weight": "model-00001-of-00002.safetensors",
48
+ "model.layers.17.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
49
+ "model.layers.17.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
50
+ "model.layers.18.input_layernorm.weight": "model-00001-of-00002.safetensors",
51
+ "model.layers.18.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
52
+ "model.layers.18.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
53
+ "model.layers.18.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
54
+ "model.layers.18.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
55
+ "model.layers.18.post_feedforward_layernorm.weight": "model-00001-of-00002.safetensors",
56
+ "model.layers.18.pre_feedforward_layernorm.weight": "model-00001-of-00002.safetensors",
57
+ "model.layers.18.self_attn.k_norm.weight": "model-00001-of-00002.safetensors",
58
+ "model.layers.18.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
59
+ "model.layers.18.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
60
+ "model.layers.18.self_attn.q_norm.weight": "model-00001-of-00002.safetensors",
61
+ "model.layers.18.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
62
+ "model.layers.18.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
63
+ "model.layers.19.input_layernorm.weight": "model-00001-of-00002.safetensors",
64
+ "model.layers.19.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
65
+ "model.layers.19.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
66
+ "model.layers.19.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
67
+ "model.layers.19.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
68
+ "model.layers.19.post_feedforward_layernorm.weight": "model-00001-of-00002.safetensors",
69
+ "model.layers.19.pre_feedforward_layernorm.weight": "model-00001-of-00002.safetensors",
70
+ "model.layers.19.self_attn.k_norm.weight": "model-00001-of-00002.safetensors",
71
+ "model.layers.19.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
72
+ "model.layers.19.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
73
+ "model.layers.19.self_attn.q_norm.weight": "model-00001-of-00002.safetensors",
74
+ "model.layers.19.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
75
+ "model.layers.19.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
76
+ "model.layers.20.input_layernorm.weight": "model-00001-of-00002.safetensors",
77
+ "model.layers.20.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
78
+ "model.layers.20.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
79
+ "model.layers.20.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
80
+ "model.layers.20.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
81
+ "model.layers.20.post_feedforward_layernorm.weight": "model-00001-of-00002.safetensors",
82
+ "model.layers.20.pre_feedforward_layernorm.weight": "model-00001-of-00002.safetensors",
83
+ "model.layers.20.self_attn.k_norm.weight": "model-00001-of-00002.safetensors",
84
+ "model.layers.20.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
85
+ "model.layers.20.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
86
+ "model.layers.20.self_attn.q_norm.weight": "model-00001-of-00002.safetensors",
87
+ "model.layers.20.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
88
+ "model.layers.20.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
89
+ "model.layers.21.input_layernorm.weight": "model-00001-of-00002.safetensors",
90
+ "model.layers.21.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
91
+ "model.layers.21.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
92
+ "model.layers.21.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
93
+ "model.layers.21.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
94
+ "model.layers.21.post_feedforward_layernorm.weight": "model-00001-of-00002.safetensors",
95
+ "model.layers.21.pre_feedforward_layernorm.weight": "model-00001-of-00002.safetensors",
96
+ "model.layers.21.self_attn.k_norm.weight": "model-00001-of-00002.safetensors",
97
+ "model.layers.21.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
98
+ "model.layers.21.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
99
+ "model.layers.21.self_attn.q_norm.weight": "model-00001-of-00002.safetensors",
100
+ "model.layers.21.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
101
+ "model.layers.21.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
102
+ "model.layers.22.input_layernorm.weight": "model-00001-of-00002.safetensors",
103
+ "model.layers.22.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
104
+ "model.layers.22.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
105
+ "model.layers.22.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
106
+ "model.layers.22.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
107
+ "model.layers.22.post_feedforward_layernorm.weight": "model-00001-of-00002.safetensors",
108
+ "model.layers.22.pre_feedforward_layernorm.weight": "model-00001-of-00002.safetensors",
109
+ "model.layers.22.self_attn.k_norm.weight": "model-00001-of-00002.safetensors",
110
+ "model.layers.22.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
111
+ "model.layers.22.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
112
+ "model.layers.22.self_attn.q_norm.weight": "model-00001-of-00002.safetensors",
113
+ "model.layers.22.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
114
+ "model.layers.22.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
115
+ "model.layers.23.input_layernorm.weight": "model-00001-of-00002.safetensors",
116
+ "model.layers.23.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
117
+ "model.layers.23.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
118
+ "model.layers.23.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
119
+ "model.layers.23.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
120
+ "model.layers.23.post_feedforward_layernorm.weight": "model-00001-of-00002.safetensors",
121
+ "model.layers.23.pre_feedforward_layernorm.weight": "model-00001-of-00002.safetensors",
122
+ "model.layers.23.self_attn.k_norm.weight": "model-00001-of-00002.safetensors",
123
+ "model.layers.23.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
124
+ "model.layers.23.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
125
+ "model.layers.23.self_attn.q_norm.weight": "model-00001-of-00002.safetensors",
126
+ "model.layers.23.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
127
+ "model.layers.23.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
128
+ "model.layers.24.input_layernorm.weight": "model-00001-of-00002.safetensors",
129
+ "model.layers.24.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
130
+ "model.layers.24.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
131
+ "model.layers.24.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
132
+ "model.layers.24.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
133
+ "model.layers.24.post_feedforward_layernorm.weight": "model-00001-of-00002.safetensors",
134
+ "model.layers.24.pre_feedforward_layernorm.weight": "model-00001-of-00002.safetensors",
135
+ "model.layers.24.self_attn.k_norm.weight": "model-00001-of-00002.safetensors",
136
+ "model.layers.24.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
137
+ "model.layers.24.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
138
+ "model.layers.24.self_attn.q_norm.weight": "model-00001-of-00002.safetensors",
139
+ "model.layers.24.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
140
+ "model.layers.24.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
141
+ "model.layers.25.input_layernorm.weight": "model-00001-of-00002.safetensors",
142
+ "model.layers.25.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
143
+ "model.layers.25.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
144
+ "model.layers.25.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
145
+ "model.layers.25.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
146
+ "model.layers.25.post_feedforward_layernorm.weight": "model-00001-of-00002.safetensors",
147
+ "model.layers.25.pre_feedforward_layernorm.weight": "model-00001-of-00002.safetensors",
148
+ "model.layers.25.self_attn.k_norm.weight": "model-00001-of-00002.safetensors",
149
+ "model.layers.25.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
150
+ "model.layers.25.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
151
+ "model.layers.25.self_attn.q_norm.weight": "model-00001-of-00002.safetensors",
152
+ "model.layers.25.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
153
+ "model.layers.25.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
154
+ "model.layers.26.input_layernorm.weight": "model-00001-of-00002.safetensors",
155
+ "model.layers.26.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
156
+ "model.layers.26.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
157
+ "model.layers.26.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
158
+ "model.layers.26.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
159
+ "model.layers.26.post_feedforward_layernorm.weight": "model-00001-of-00002.safetensors",
160
+ "model.layers.26.pre_feedforward_layernorm.weight": "model-00001-of-00002.safetensors",
161
+ "model.layers.26.self_attn.k_norm.weight": "model-00001-of-00002.safetensors",
162
+ "model.layers.26.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
163
+ "model.layers.26.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
164
+ "model.layers.26.self_attn.q_norm.weight": "model-00001-of-00002.safetensors",
165
+ "model.layers.26.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
166
+ "model.layers.26.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
167
+ "model.layers.27.input_layernorm.weight": "model-00001-of-00002.safetensors",
168
+ "model.layers.27.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
169
+ "model.layers.27.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
170
+ "model.layers.27.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
171
+ "model.layers.27.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
172
+ "model.layers.27.post_feedforward_layernorm.weight": "model-00001-of-00002.safetensors",
173
+ "model.layers.27.pre_feedforward_layernorm.weight": "model-00001-of-00002.safetensors",
174
+ "model.layers.27.self_attn.k_norm.weight": "model-00001-of-00002.safetensors",
175
+ "model.layers.27.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
176
+ "model.layers.27.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
177
+ "model.layers.27.self_attn.q_norm.weight": "model-00001-of-00002.safetensors",
178
+ "model.layers.27.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
179
+ "model.layers.27.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
180
+ "model.layers.28.input_layernorm.weight": "model-00001-of-00002.safetensors",
181
+ "model.layers.28.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
182
+ "model.layers.28.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
183
+ "model.layers.28.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
184
+ "model.layers.28.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
185
+ "model.layers.28.post_feedforward_layernorm.weight": "model-00001-of-00002.safetensors",
186
+ "model.layers.28.pre_feedforward_layernorm.weight": "model-00001-of-00002.safetensors",
187
+ "model.layers.28.self_attn.k_norm.weight": "model-00001-of-00002.safetensors",
188
+ "model.layers.28.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
189
+ "model.layers.28.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
190
+ "model.layers.28.self_attn.q_norm.weight": "model-00001-of-00002.safetensors",
191
+ "model.layers.28.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
192
+ "model.layers.28.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
193
+ "model.layers.29.input_layernorm.weight": "model-00001-of-00002.safetensors",
194
+ "model.layers.29.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
195
+ "model.layers.29.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
196
+ "model.layers.29.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
197
+ "model.layers.29.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
198
+ "model.layers.29.post_feedforward_layernorm.weight": "model-00001-of-00002.safetensors",
199
+ "model.layers.29.pre_feedforward_layernorm.weight": "model-00001-of-00002.safetensors",
200
+ "model.layers.29.self_attn.k_norm.weight": "model-00001-of-00002.safetensors",
201
+ "model.layers.29.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
202
+ "model.layers.29.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
203
+ "model.layers.29.self_attn.q_norm.weight": "model-00001-of-00002.safetensors",
204
+ "model.layers.29.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
205
+ "model.layers.29.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
206
+ "model.layers.30.input_layernorm.weight": "model-00001-of-00002.safetensors",
207
+ "model.layers.30.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
208
+ "model.layers.30.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
209
+ "model.layers.30.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
210
+ "model.layers.30.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
211
+ "model.layers.30.post_feedforward_layernorm.weight": "model-00001-of-00002.safetensors",
212
+ "model.layers.30.pre_feedforward_layernorm.weight": "model-00001-of-00002.safetensors",
213
+ "model.layers.30.self_attn.k_norm.weight": "model-00001-of-00002.safetensors",
214
+ "model.layers.30.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
215
+ "model.layers.30.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
216
+ "model.layers.30.self_attn.q_norm.weight": "model-00001-of-00002.safetensors",
217
+ "model.layers.30.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
218
+ "model.layers.30.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
219
+ "model.layers.31.input_layernorm.weight": "model-00001-of-00002.safetensors",
220
+ "model.layers.31.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
221
+ "model.layers.31.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
222
+ "model.layers.31.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
223
+ "model.layers.31.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
224
+ "model.layers.31.post_feedforward_layernorm.weight": "model-00001-of-00002.safetensors",
225
+ "model.layers.31.pre_feedforward_layernorm.weight": "model-00001-of-00002.safetensors",
226
+ "model.layers.31.self_attn.k_norm.weight": "model-00001-of-00002.safetensors",
227
+ "model.layers.31.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
228
+ "model.layers.31.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
229
+ "model.layers.31.self_attn.q_norm.weight": "model-00001-of-00002.safetensors",
230
+ "model.layers.31.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
231
+ "model.layers.31.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
232
+ "model.layers.32.input_layernorm.weight": "model-00001-of-00002.safetensors",
233
+ "model.layers.32.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
234
+ "model.layers.32.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
235
+ "model.layers.32.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
236
+ "model.layers.32.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
237
+ "model.layers.32.post_feedforward_layernorm.weight": "model-00001-of-00002.safetensors",
238
+ "model.layers.32.pre_feedforward_layernorm.weight": "model-00001-of-00002.safetensors",
239
+ "model.layers.32.self_attn.k_norm.weight": "model-00001-of-00002.safetensors",
240
+ "model.layers.32.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
241
+ "model.layers.32.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
242
+ "model.layers.32.self_attn.q_norm.weight": "model-00001-of-00002.safetensors",
243
+ "model.layers.32.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
244
+ "model.layers.32.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
245
+ "model.layers.33.input_layernorm.weight": "model-00001-of-00002.safetensors",
246
+ "model.layers.33.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
247
+ "model.layers.33.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
248
+ "model.layers.33.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
249
+ "model.layers.33.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
250
+ "model.layers.33.post_feedforward_layernorm.weight": "model-00001-of-00002.safetensors",
251
+ "model.layers.33.pre_feedforward_layernorm.weight": "model-00001-of-00002.safetensors",
252
+ "model.layers.33.self_attn.k_norm.weight": "model-00001-of-00002.safetensors",
253
+ "model.layers.33.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
254
+ "model.layers.33.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
255
+ "model.layers.33.self_attn.q_norm.weight": "model-00001-of-00002.safetensors",
256
+ "model.layers.33.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
257
+ "model.layers.33.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
258
+ "model.norm.weight": "model-00001-of-00002.safetensors",
259
+ "model.embed_tokens.weight": "model-00002-of-00002.safetensors",
260
+ "model.layers.0.input_layernorm.weight": "model-00002-of-00002.safetensors",
261
+ "model.layers.0.mlp.down_proj.weight": "model-00002-of-00002.safetensors",
262
+ "model.layers.0.mlp.gate_proj.weight": "model-00002-of-00002.safetensors",
263
+ "model.layers.0.mlp.up_proj.weight": "model-00002-of-00002.safetensors",
264
+ "model.layers.0.post_attention_layernorm.weight": "model-00002-of-00002.safetensors",
265
+ "model.layers.0.post_feedforward_layernorm.weight": "model-00002-of-00002.safetensors",
266
+ "model.layers.0.pre_feedforward_layernorm.weight": "model-00002-of-00002.safetensors",
267
+ "model.layers.0.self_attn.k_norm.weight": "model-00002-of-00002.safetensors",
268
+ "model.layers.0.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
269
+ "model.layers.0.self_attn.o_proj.weight": "model-00002-of-00002.safetensors",
270
+ "model.layers.0.self_attn.q_norm.weight": "model-00002-of-00002.safetensors",
271
+ "model.layers.0.self_attn.q_proj.weight": "model-00002-of-00002.safetensors",
272
+ "model.layers.0.self_attn.v_proj.weight": "model-00002-of-00002.safetensors",
273
+ "model.layers.1.input_layernorm.weight": "model-00002-of-00002.safetensors",
274
+ "model.layers.1.mlp.down_proj.weight": "model-00002-of-00002.safetensors",
275
+ "model.layers.1.mlp.gate_proj.weight": "model-00002-of-00002.safetensors",
276
+ "model.layers.1.mlp.up_proj.weight": "model-00002-of-00002.safetensors",
277
+ "model.layers.1.post_attention_layernorm.weight": "model-00002-of-00002.safetensors",
278
+ "model.layers.1.post_feedforward_layernorm.weight": "model-00002-of-00002.safetensors",
279
+ "model.layers.1.pre_feedforward_layernorm.weight": "model-00002-of-00002.safetensors",
280
+ "model.layers.1.self_attn.k_norm.weight": "model-00002-of-00002.safetensors",
281
+ "model.layers.1.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
282
+ "model.layers.1.self_attn.o_proj.weight": "model-00002-of-00002.safetensors",
283
+ "model.layers.1.self_attn.q_norm.weight": "model-00002-of-00002.safetensors",
284
+ "model.layers.1.self_attn.q_proj.weight": "model-00002-of-00002.safetensors",
285
+ "model.layers.1.self_attn.v_proj.weight": "model-00002-of-00002.safetensors",
286
+ "model.layers.10.input_layernorm.weight": "model-00002-of-00002.safetensors",
287
+ "model.layers.10.mlp.down_proj.weight": "model-00002-of-00002.safetensors",
288
+ "model.layers.10.mlp.gate_proj.weight": "model-00002-of-00002.safetensors",
289
+ "model.layers.10.mlp.up_proj.weight": "model-00002-of-00002.safetensors",
290
+ "model.layers.10.post_attention_layernorm.weight": "model-00002-of-00002.safetensors",
291
+ "model.layers.10.post_feedforward_layernorm.weight": "model-00002-of-00002.safetensors",
292
+ "model.layers.10.pre_feedforward_layernorm.weight": "model-00002-of-00002.safetensors",
293
+ "model.layers.10.self_attn.k_norm.weight": "model-00002-of-00002.safetensors",
294
+ "model.layers.10.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
295
+ "model.layers.10.self_attn.o_proj.weight": "model-00002-of-00002.safetensors",
296
+ "model.layers.10.self_attn.q_norm.weight": "model-00002-of-00002.safetensors",
297
+ "model.layers.10.self_attn.q_proj.weight": "model-00002-of-00002.safetensors",
298
+ "model.layers.10.self_attn.v_proj.weight": "model-00002-of-00002.safetensors",
299
+ "model.layers.11.input_layernorm.weight": "model-00002-of-00002.safetensors",
300
+ "model.layers.11.mlp.down_proj.weight": "model-00002-of-00002.safetensors",
301
+ "model.layers.11.mlp.gate_proj.weight": "model-00002-of-00002.safetensors",
302
+ "model.layers.11.mlp.up_proj.weight": "model-00002-of-00002.safetensors",
303
+ "model.layers.11.post_attention_layernorm.weight": "model-00002-of-00002.safetensors",
304
+ "model.layers.11.post_feedforward_layernorm.weight": "model-00002-of-00002.safetensors",
305
+ "model.layers.11.pre_feedforward_layernorm.weight": "model-00002-of-00002.safetensors",
306
+ "model.layers.11.self_attn.k_norm.weight": "model-00002-of-00002.safetensors",
307
+ "model.layers.11.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
308
+ "model.layers.11.self_attn.o_proj.weight": "model-00002-of-00002.safetensors",
309
+ "model.layers.11.self_attn.q_norm.weight": "model-00002-of-00002.safetensors",
310
+ "model.layers.11.self_attn.q_proj.weight": "model-00002-of-00002.safetensors",
311
+ "model.layers.11.self_attn.v_proj.weight": "model-00002-of-00002.safetensors",
312
+ "model.layers.12.input_layernorm.weight": "model-00002-of-00002.safetensors",
313
+ "model.layers.12.mlp.down_proj.weight": "model-00002-of-00002.safetensors",
314
+ "model.layers.12.mlp.gate_proj.weight": "model-00002-of-00002.safetensors",
315
+ "model.layers.12.mlp.up_proj.weight": "model-00002-of-00002.safetensors",
316
+ "model.layers.12.post_attention_layernorm.weight": "model-00002-of-00002.safetensors",
317
+ "model.layers.12.post_feedforward_layernorm.weight": "model-00002-of-00002.safetensors",
318
+ "model.layers.12.pre_feedforward_layernorm.weight": "model-00002-of-00002.safetensors",
319
+ "model.layers.12.self_attn.k_norm.weight": "model-00002-of-00002.safetensors",
320
+ "model.layers.12.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
321
+ "model.layers.12.self_attn.o_proj.weight": "model-00002-of-00002.safetensors",
322
+ "model.layers.12.self_attn.q_norm.weight": "model-00002-of-00002.safetensors",
323
+ "model.layers.12.self_attn.q_proj.weight": "model-00002-of-00002.safetensors",
324
+ "model.layers.12.self_attn.v_proj.weight": "model-00002-of-00002.safetensors",
325
+ "model.layers.13.input_layernorm.weight": "model-00002-of-00002.safetensors",
326
+ "model.layers.13.mlp.down_proj.weight": "model-00002-of-00002.safetensors",
327
+ "model.layers.13.mlp.gate_proj.weight": "model-00002-of-00002.safetensors",
328
+ "model.layers.13.mlp.up_proj.weight": "model-00002-of-00002.safetensors",
329
+ "model.layers.13.post_attention_layernorm.weight": "model-00002-of-00002.safetensors",
330
+ "model.layers.13.post_feedforward_layernorm.weight": "model-00002-of-00002.safetensors",
331
+ "model.layers.13.pre_feedforward_layernorm.weight": "model-00002-of-00002.safetensors",
332
+ "model.layers.13.self_attn.k_norm.weight": "model-00002-of-00002.safetensors",
333
+ "model.layers.13.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
334
+ "model.layers.13.self_attn.o_proj.weight": "model-00002-of-00002.safetensors",
335
+ "model.layers.13.self_attn.q_norm.weight": "model-00002-of-00002.safetensors",
336
+ "model.layers.13.self_attn.q_proj.weight": "model-00002-of-00002.safetensors",
337
+ "model.layers.13.self_attn.v_proj.weight": "model-00002-of-00002.safetensors",
338
+ "model.layers.14.mlp.gate_proj.weight": "model-00002-of-00002.safetensors",
339
+ "model.layers.14.mlp.up_proj.weight": "model-00002-of-00002.safetensors",
340
+ "model.layers.14.self_attn.k_norm.weight": "model-00002-of-00002.safetensors",
341
+ "model.layers.14.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
342
+ "model.layers.14.self_attn.o_proj.weight": "model-00002-of-00002.safetensors",
343
+ "model.layers.14.self_attn.q_norm.weight": "model-00002-of-00002.safetensors",
344
+ "model.layers.14.self_attn.q_proj.weight": "model-00002-of-00002.safetensors",
345
+ "model.layers.14.self_attn.v_proj.weight": "model-00002-of-00002.safetensors",
346
+ "model.layers.2.input_layernorm.weight": "model-00002-of-00002.safetensors",
347
+ "model.layers.2.mlp.down_proj.weight": "model-00002-of-00002.safetensors",
348
+ "model.layers.2.mlp.gate_proj.weight": "model-00002-of-00002.safetensors",
349
+ "model.layers.2.mlp.up_proj.weight": "model-00002-of-00002.safetensors",
350
+ "model.layers.2.post_attention_layernorm.weight": "model-00002-of-00002.safetensors",
351
+ "model.layers.2.post_feedforward_layernorm.weight": "model-00002-of-00002.safetensors",
352
+ "model.layers.2.pre_feedforward_layernorm.weight": "model-00002-of-00002.safetensors",
353
+ "model.layers.2.self_attn.k_norm.weight": "model-00002-of-00002.safetensors",
354
+ "model.layers.2.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
355
+ "model.layers.2.self_attn.o_proj.weight": "model-00002-of-00002.safetensors",
356
+ "model.layers.2.self_attn.q_norm.weight": "model-00002-of-00002.safetensors",
357
+ "model.layers.2.self_attn.q_proj.weight": "model-00002-of-00002.safetensors",
358
+ "model.layers.2.self_attn.v_proj.weight": "model-00002-of-00002.safetensors",
359
+ "model.layers.3.input_layernorm.weight": "model-00002-of-00002.safetensors",
360
+ "model.layers.3.mlp.down_proj.weight": "model-00002-of-00002.safetensors",
361
+ "model.layers.3.mlp.gate_proj.weight": "model-00002-of-00002.safetensors",
362
+ "model.layers.3.mlp.up_proj.weight": "model-00002-of-00002.safetensors",
363
+ "model.layers.3.post_attention_layernorm.weight": "model-00002-of-00002.safetensors",
364
+ "model.layers.3.post_feedforward_layernorm.weight": "model-00002-of-00002.safetensors",
365
+ "model.layers.3.pre_feedforward_layernorm.weight": "model-00002-of-00002.safetensors",
366
+ "model.layers.3.self_attn.k_norm.weight": "model-00002-of-00002.safetensors",
367
+ "model.layers.3.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
368
+ "model.layers.3.self_attn.o_proj.weight": "model-00002-of-00002.safetensors",
369
+ "model.layers.3.self_attn.q_norm.weight": "model-00002-of-00002.safetensors",
370
+ "model.layers.3.self_attn.q_proj.weight": "model-00002-of-00002.safetensors",
371
+ "model.layers.3.self_attn.v_proj.weight": "model-00002-of-00002.safetensors",
372
+ "model.layers.4.input_layernorm.weight": "model-00002-of-00002.safetensors",
373
+ "model.layers.4.mlp.down_proj.weight": "model-00002-of-00002.safetensors",
374
+ "model.layers.4.mlp.gate_proj.weight": "model-00002-of-00002.safetensors",
375
+ "model.layers.4.mlp.up_proj.weight": "model-00002-of-00002.safetensors",
376
+ "model.layers.4.post_attention_layernorm.weight": "model-00002-of-00002.safetensors",
377
+ "model.layers.4.post_feedforward_layernorm.weight": "model-00002-of-00002.safetensors",
378
+ "model.layers.4.pre_feedforward_layernorm.weight": "model-00002-of-00002.safetensors",
379
+ "model.layers.4.self_attn.k_norm.weight": "model-00002-of-00002.safetensors",
380
+ "model.layers.4.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
381
+ "model.layers.4.self_attn.o_proj.weight": "model-00002-of-00002.safetensors",
382
+ "model.layers.4.self_attn.q_norm.weight": "model-00002-of-00002.safetensors",
383
+ "model.layers.4.self_attn.q_proj.weight": "model-00002-of-00002.safetensors",
384
+ "model.layers.4.self_attn.v_proj.weight": "model-00002-of-00002.safetensors",
385
+ "model.layers.5.input_layernorm.weight": "model-00002-of-00002.safetensors",
386
+ "model.layers.5.mlp.down_proj.weight": "model-00002-of-00002.safetensors",
387
+ "model.layers.5.mlp.gate_proj.weight": "model-00002-of-00002.safetensors",
388
+ "model.layers.5.mlp.up_proj.weight": "model-00002-of-00002.safetensors",
389
+ "model.layers.5.post_attention_layernorm.weight": "model-00002-of-00002.safetensors",
390
+ "model.layers.5.post_feedforward_layernorm.weight": "model-00002-of-00002.safetensors",
391
+ "model.layers.5.pre_feedforward_layernorm.weight": "model-00002-of-00002.safetensors",
392
+ "model.layers.5.self_attn.k_norm.weight": "model-00002-of-00002.safetensors",
393
+ "model.layers.5.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
394
+ "model.layers.5.self_attn.o_proj.weight": "model-00002-of-00002.safetensors",
395
+ "model.layers.5.self_attn.q_norm.weight": "model-00002-of-00002.safetensors",
396
+ "model.layers.5.self_attn.q_proj.weight": "model-00002-of-00002.safetensors",
397
+ "model.layers.5.self_attn.v_proj.weight": "model-00002-of-00002.safetensors",
398
+ "model.layers.6.input_layernorm.weight": "model-00002-of-00002.safetensors",
399
+ "model.layers.6.mlp.down_proj.weight": "model-00002-of-00002.safetensors",
400
+ "model.layers.6.mlp.gate_proj.weight": "model-00002-of-00002.safetensors",
401
+ "model.layers.6.mlp.up_proj.weight": "model-00002-of-00002.safetensors",
402
+ "model.layers.6.post_attention_layernorm.weight": "model-00002-of-00002.safetensors",
403
+ "model.layers.6.post_feedforward_layernorm.weight": "model-00002-of-00002.safetensors",
404
+ "model.layers.6.pre_feedforward_layernorm.weight": "model-00002-of-00002.safetensors",
405
+ "model.layers.6.self_attn.k_norm.weight": "model-00002-of-00002.safetensors",
406
+ "model.layers.6.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
407
+ "model.layers.6.self_attn.o_proj.weight": "model-00002-of-00002.safetensors",
408
+ "model.layers.6.self_attn.q_norm.weight": "model-00002-of-00002.safetensors",
409
+ "model.layers.6.self_attn.q_proj.weight": "model-00002-of-00002.safetensors",
410
+ "model.layers.6.self_attn.v_proj.weight": "model-00002-of-00002.safetensors",
411
+ "model.layers.7.input_layernorm.weight": "model-00002-of-00002.safetensors",
412
+ "model.layers.7.mlp.down_proj.weight": "model-00002-of-00002.safetensors",
413
+ "model.layers.7.mlp.gate_proj.weight": "model-00002-of-00002.safetensors",
414
+ "model.layers.7.mlp.up_proj.weight": "model-00002-of-00002.safetensors",
415
+ "model.layers.7.post_attention_layernorm.weight": "model-00002-of-00002.safetensors",
416
+ "model.layers.7.post_feedforward_layernorm.weight": "model-00002-of-00002.safetensors",
417
+ "model.layers.7.pre_feedforward_layernorm.weight": "model-00002-of-00002.safetensors",
418
+ "model.layers.7.self_attn.k_norm.weight": "model-00002-of-00002.safetensors",
419
+ "model.layers.7.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
420
+ "model.layers.7.self_attn.o_proj.weight": "model-00002-of-00002.safetensors",
421
+ "model.layers.7.self_attn.q_norm.weight": "model-00002-of-00002.safetensors",
422
+ "model.layers.7.self_attn.q_proj.weight": "model-00002-of-00002.safetensors",
423
+ "model.layers.7.self_attn.v_proj.weight": "model-00002-of-00002.safetensors",
424
+ "model.layers.8.input_layernorm.weight": "model-00002-of-00002.safetensors",
425
+ "model.layers.8.mlp.down_proj.weight": "model-00002-of-00002.safetensors",
426
+ "model.layers.8.mlp.gate_proj.weight": "model-00002-of-00002.safetensors",
427
+ "model.layers.8.mlp.up_proj.weight": "model-00002-of-00002.safetensors",
428
+ "model.layers.8.post_attention_layernorm.weight": "model-00002-of-00002.safetensors",
429
+ "model.layers.8.post_feedforward_layernorm.weight": "model-00002-of-00002.safetensors",
430
+ "model.layers.8.pre_feedforward_layernorm.weight": "model-00002-of-00002.safetensors",
431
+ "model.layers.8.self_attn.k_norm.weight": "model-00002-of-00002.safetensors",
432
+ "model.layers.8.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
433
+ "model.layers.8.self_attn.o_proj.weight": "model-00002-of-00002.safetensors",
434
+ "model.layers.8.self_attn.q_norm.weight": "model-00002-of-00002.safetensors",
435
+ "model.layers.8.self_attn.q_proj.weight": "model-00002-of-00002.safetensors",
436
+ "model.layers.8.self_attn.v_proj.weight": "model-00002-of-00002.safetensors",
437
+ "model.layers.9.input_layernorm.weight": "model-00002-of-00002.safetensors",
438
+ "model.layers.9.mlp.down_proj.weight": "model-00002-of-00002.safetensors",
439
+ "model.layers.9.mlp.gate_proj.weight": "model-00002-of-00002.safetensors",
440
+ "model.layers.9.mlp.up_proj.weight": "model-00002-of-00002.safetensors",
441
+ "model.layers.9.post_attention_layernorm.weight": "model-00002-of-00002.safetensors",
442
+ "model.layers.9.post_feedforward_layernorm.weight": "model-00002-of-00002.safetensors",
443
+ "model.layers.9.pre_feedforward_layernorm.weight": "model-00002-of-00002.safetensors",
444
+ "model.layers.9.self_attn.k_norm.weight": "model-00002-of-00002.safetensors",
445
+ "model.layers.9.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
446
+ "model.layers.9.self_attn.o_proj.weight": "model-00002-of-00002.safetensors",
447
+ "model.layers.9.self_attn.q_norm.weight": "model-00002-of-00002.safetensors",
448
+ "model.layers.9.self_attn.q_proj.weight": "model-00002-of-00002.safetensors",
449
+ "model.layers.9.self_attn.v_proj.weight": "model-00002-of-00002.safetensors"
450
+ }
451
+ }
modeling_gemmagain.py ADDED
@@ -0,0 +1,573 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2025 Google Inc. HuggingFace Inc. team. All rights reserved.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ """
16
+ Gemmagain - Gemma3 text model with layer looping support.
17
+
18
+ This model allows running the same physical layers multiple times in sequence,
19
+ enabling parameter-efficient deep networks. Compatible with standard Gemma3 weights.
20
+ """
21
+ import copy
22
+ from typing import Callable, Optional, Union
23
+
24
+ import torch
25
+ import torch.nn as nn
26
+ from torch.nn import CrossEntropyLoss
27
+
28
+ from transformers.activations import ACT2FN
29
+ from transformers.cache_utils import Cache, DynamicCache, DynamicLayer
30
+ from transformers.generation import GenerationMixin
31
+ from transformers.masking_utils import create_causal_mask, create_sliding_window_causal_mask
32
+ from transformers.modeling_flash_attention_utils import FlashAttentionKwargs
33
+ from transformers.modeling_layers import GradientCheckpointingLayer
34
+ from transformers.modeling_outputs import BaseModelOutputWithPast, CausalLMOutputWithPast
35
+ from transformers.modeling_rope_utils import ROPE_INIT_FUNCTIONS, dynamic_rope_update
36
+ from transformers.modeling_utils import ALL_ATTENTION_FUNCTIONS, PreTrainedModel
37
+ from transformers.processing_utils import Unpack
38
+ from transformers.utils import TransformersKwargs, auto_docstring, can_return_tuple, logging
39
+ from transformers.utils.deprecation import deprecate_kwarg
40
+
41
+ from .configuration_gemmagain import GemmagainConfig
42
+
43
+
44
+ logger = logging.get_logger(__name__)
45
+
46
+
47
+ class Gemma3TextScaledWordEmbedding(nn.Embedding):
48
+ """
49
+ This module overrides nn.Embeddings' forward by multiplying with embeddings scale.
50
+ """
51
+
52
+ def __init__(self, num_embeddings: int, embedding_dim: int, padding_idx: int, embed_scale: float = 1.0):
53
+ super().__init__(num_embeddings, embedding_dim, padding_idx)
54
+ self.register_buffer("embed_scale", torch.tensor(embed_scale), persistent=False)
55
+
56
+ def forward(self, input_ids: torch.Tensor):
57
+ return super().forward(input_ids) * self.embed_scale.to(self.weight.dtype)
58
+
59
+
60
+ class Gemma3MLP(nn.Module):
61
+ def __init__(self, config: GemmagainConfig):
62
+ super().__init__()
63
+ self.config = config
64
+ self.hidden_size = config.hidden_size
65
+ self.intermediate_size = config.intermediate_size
66
+ self.gate_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=False)
67
+ self.up_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=False)
68
+ self.down_proj = nn.Linear(self.intermediate_size, self.hidden_size, bias=False)
69
+ self.act_fn = ACT2FN[config.hidden_activation]
70
+
71
+ def forward(self, x):
72
+ down_proj = self.down_proj(self.act_fn(self.gate_proj(x)) * self.up_proj(x))
73
+ return down_proj
74
+
75
+
76
+ class Gemma3RMSNorm(nn.Module):
77
+ def __init__(self, dim: int, eps: float = 1e-6):
78
+ super().__init__()
79
+ self.eps = eps
80
+ self.weight = nn.Parameter(torch.zeros(dim))
81
+
82
+ def _norm(self, x):
83
+ return x * torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + self.eps)
84
+
85
+ def forward(self, x):
86
+ output = self._norm(x.float())
87
+ # Gemma3 uses (x * w).to(dtype) instead of x.to(dtype) * w
88
+ output = output * (1.0 + self.weight.float())
89
+ return output.type_as(x)
90
+
91
+ def extra_repr(self):
92
+ return f"{tuple(self.weight.shape)}, eps={self.eps}"
93
+
94
+
95
+ class Gemma3RotaryEmbedding(nn.Module):
96
+ inv_freq: torch.Tensor
97
+
98
+ def __init__(self, config: GemmagainConfig, device=None):
99
+ super().__init__()
100
+ if hasattr(config, "rope_scaling") and isinstance(config.rope_scaling, dict):
101
+ self.rope_type = config.rope_scaling.get("rope_type", config.rope_scaling.get("type"))
102
+ else:
103
+ self.rope_type = "default"
104
+ self.max_seq_len_cached = config.max_position_embeddings
105
+ self.original_max_seq_len = config.max_position_embeddings
106
+
107
+ self.config = config
108
+ self.rope_init_fn = ROPE_INIT_FUNCTIONS[self.rope_type]
109
+
110
+ inv_freq, self.attention_scaling = self.rope_init_fn(self.config, device)
111
+ self.register_buffer("inv_freq", inv_freq, persistent=False)
112
+ self.original_inv_freq = self.inv_freq
113
+
114
+ @torch.no_grad()
115
+ @dynamic_rope_update
116
+ def forward(self, x, position_ids):
117
+ inv_freq_expanded = self.inv_freq[None, :, None].float().expand(position_ids.shape[0], -1, 1).to(x.device)
118
+ position_ids_expanded = position_ids[:, None, :].float()
119
+
120
+ device_type = x.device.type if isinstance(x.device.type, str) and x.device.type != "mps" else "cpu"
121
+ with torch.autocast(device_type=device_type, enabled=False):
122
+ freqs = (inv_freq_expanded.float() @ position_ids_expanded.float()).transpose(1, 2)
123
+ emb = torch.cat((freqs, freqs), dim=-1)
124
+ cos = emb.cos() * self.attention_scaling
125
+ sin = emb.sin() * self.attention_scaling
126
+
127
+ return cos.to(dtype=x.dtype), sin.to(dtype=x.dtype)
128
+
129
+
130
+ def rotate_half(x):
131
+ """Rotates half the hidden dims of the input."""
132
+ x1 = x[..., : x.shape[-1] // 2]
133
+ x2 = x[..., x.shape[-1] // 2 :]
134
+ return torch.cat((-x2, x1), dim=-1)
135
+
136
+
137
+ def apply_rotary_pos_emb(q, k, cos, sin, position_ids=None, unsqueeze_dim=1):
138
+ """Applies Rotary Position Embedding to the query and key tensors."""
139
+ cos = cos.unsqueeze(unsqueeze_dim)
140
+ sin = sin.unsqueeze(unsqueeze_dim)
141
+ q_embed = (q * cos) + (rotate_half(q) * sin)
142
+ k_embed = (k * cos) + (rotate_half(k) * sin)
143
+ return q_embed, k_embed
144
+
145
+
146
+ def repeat_kv(hidden_states: torch.Tensor, n_rep: int) -> torch.Tensor:
147
+ """Repeat KV heads for GQA."""
148
+ batch, num_key_value_heads, slen, head_dim = hidden_states.shape
149
+ if n_rep == 1:
150
+ return hidden_states
151
+ hidden_states = hidden_states[:, :, None, :, :].expand(batch, num_key_value_heads, n_rep, slen, head_dim)
152
+ return hidden_states.reshape(batch, num_key_value_heads * n_rep, slen, head_dim)
153
+
154
+
155
+ def eager_attention_forward(
156
+ module: nn.Module,
157
+ query: torch.Tensor,
158
+ key: torch.Tensor,
159
+ value: torch.Tensor,
160
+ attention_mask: Optional[torch.Tensor],
161
+ dropout: float = 0.0,
162
+ scaling: Optional[float] = None,
163
+ softcap: Optional[float] = None,
164
+ **kwargs,
165
+ ) -> tuple[torch.Tensor, torch.Tensor]:
166
+ if scaling is None:
167
+ scaling = module.head_dim**-0.5
168
+
169
+ key_states = repeat_kv(key, module.num_key_value_groups)
170
+ value_states = repeat_kv(value, module.num_key_value_groups)
171
+
172
+ attn_weights = torch.matmul(query, key_states.transpose(2, 3)) * scaling
173
+
174
+ if softcap is not None:
175
+ attn_weights = attn_weights / softcap
176
+ attn_weights = torch.tanh(attn_weights)
177
+ attn_weights = attn_weights * softcap
178
+ if attention_mask is not None:
179
+ causal_mask = attention_mask[:, :, :, : key_states.shape[-2]]
180
+ attn_weights = attn_weights + causal_mask
181
+
182
+ attn_weights = nn.functional.softmax(attn_weights, dim=-1, dtype=torch.float32).to(query.dtype)
183
+ attn_weights = nn.functional.dropout(attn_weights, p=dropout, training=module.training)
184
+ attn_output = torch.matmul(attn_weights, value_states)
185
+ attn_output = attn_output.transpose(1, 2).contiguous()
186
+ return attn_output, attn_weights
187
+
188
+
189
+ class Gemma3Attention(nn.Module):
190
+ """Multi-headed attention with support for looping (cache_slot_idx)."""
191
+
192
+ def __init__(self, config: GemmagainConfig, layer_idx: int):
193
+ super().__init__()
194
+ self.is_sliding = config.layer_types[layer_idx] == "sliding_attention"
195
+ self.config = config
196
+ self.layer_idx = layer_idx
197
+ self.head_dim = getattr(config, "head_dim", config.hidden_size // config.num_attention_heads)
198
+ self.num_key_value_groups = config.num_attention_heads // config.num_key_value_heads
199
+ self.scaling = config.query_pre_attn_scalar**-0.5
200
+ self.attention_dropout = self.config.attention_dropout
201
+ self.is_causal = not self.config.use_bidirectional_attention
202
+
203
+ self.q_proj = nn.Linear(
204
+ config.hidden_size, config.num_attention_heads * self.head_dim, bias=config.attention_bias
205
+ )
206
+ self.k_proj = nn.Linear(
207
+ config.hidden_size, config.num_key_value_heads * self.head_dim, bias=config.attention_bias
208
+ )
209
+ self.v_proj = nn.Linear(
210
+ config.hidden_size, config.num_key_value_heads * self.head_dim, bias=config.attention_bias
211
+ )
212
+ self.o_proj = nn.Linear(
213
+ config.num_attention_heads * self.head_dim, config.hidden_size, bias=config.attention_bias
214
+ )
215
+ self.attn_logit_softcapping = self.config.attn_logit_softcapping
216
+ self.sliding_window = config.sliding_window if self.is_sliding else None
217
+
218
+ self.q_norm = Gemma3RMSNorm(dim=config.head_dim, eps=config.rms_norm_eps)
219
+ self.k_norm = Gemma3RMSNorm(dim=config.head_dim, eps=config.rms_norm_eps)
220
+
221
+ @deprecate_kwarg("past_key_value", new_name="past_key_values", version="4.58")
222
+ def forward(
223
+ self,
224
+ hidden_states: torch.Tensor,
225
+ position_embeddings: torch.Tensor,
226
+ attention_mask: Optional[torch.Tensor],
227
+ past_key_values: Optional[Cache] = None,
228
+ cache_position: Optional[torch.LongTensor] = None,
229
+ cache_slot_idx: Optional[int] = None,
230
+ **kwargs: Unpack[FlashAttentionKwargs],
231
+ ) -> tuple[torch.Tensor, Optional[torch.Tensor]]:
232
+ input_shape = hidden_states.shape[:-1]
233
+ hidden_shape = (*input_shape, -1, self.head_dim)
234
+
235
+ query_states = self.q_proj(hidden_states).view(hidden_shape).transpose(1, 2)
236
+ key_states = self.k_proj(hidden_states).view(hidden_shape).transpose(1, 2)
237
+ value_states = self.v_proj(hidden_states).view(hidden_shape).transpose(1, 2)
238
+
239
+ query_states = self.q_norm(query_states)
240
+ key_states = self.k_norm(key_states)
241
+
242
+ cos, sin = position_embeddings
243
+ query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin)
244
+
245
+ if past_key_values is not None:
246
+ cache_kwargs = {"sin": sin, "cos": cos, "cache_position": cache_position}
247
+ # Use cache_slot_idx for looping support - each visit to a layer gets its own cache slot
248
+ slot_idx = cache_slot_idx if cache_slot_idx is not None else self.layer_idx
249
+ key_states, value_states = past_key_values.update(key_states, value_states, slot_idx, cache_kwargs)
250
+
251
+ attention_interface: Callable = eager_attention_forward
252
+ if self.config._attn_implementation != "eager":
253
+ attention_interface = ALL_ATTENTION_FUNCTIONS[self.config._attn_implementation]
254
+
255
+ attn_output, attn_weights = attention_interface(
256
+ self,
257
+ query_states,
258
+ key_states,
259
+ value_states,
260
+ attention_mask,
261
+ dropout=self.attention_dropout if self.training else 0.0,
262
+ scaling=self.scaling,
263
+ sliding_window=self.sliding_window,
264
+ **kwargs,
265
+ )
266
+
267
+ attn_output = attn_output.reshape(*input_shape, -1).contiguous()
268
+ attn_output = self.o_proj(attn_output)
269
+ return attn_output, attn_weights
270
+
271
+
272
+ class Gemma3DecoderLayer(GradientCheckpointingLayer):
273
+ def __init__(self, config: GemmagainConfig, layer_idx: int):
274
+ super().__init__()
275
+ self.config = config
276
+ self.hidden_size = config.hidden_size
277
+ self.layer_idx = layer_idx
278
+ self.attention_type = config.layer_types[layer_idx]
279
+ self.self_attn = Gemma3Attention(config=config, layer_idx=layer_idx)
280
+ self.mlp = Gemma3MLP(config)
281
+ self.input_layernorm = Gemma3RMSNorm(self.hidden_size, eps=config.rms_norm_eps)
282
+ self.post_attention_layernorm = Gemma3RMSNorm(self.hidden_size, eps=config.rms_norm_eps)
283
+ self.pre_feedforward_layernorm = Gemma3RMSNorm(self.hidden_size, eps=config.rms_norm_eps)
284
+ self.post_feedforward_layernorm = Gemma3RMSNorm(self.hidden_size, eps=config.rms_norm_eps)
285
+
286
+ @deprecate_kwarg("past_key_value", new_name="past_key_values", version="4.58")
287
+ def forward(
288
+ self,
289
+ hidden_states: torch.Tensor,
290
+ position_embeddings_global: torch.Tensor,
291
+ position_embeddings_local: torch.Tensor,
292
+ attention_mask: Optional[torch.Tensor] = None,
293
+ position_ids: Optional[torch.LongTensor] = None,
294
+ past_key_values: Optional[Cache] = None,
295
+ use_cache: Optional[bool] = False,
296
+ cache_position: Optional[torch.LongTensor] = None,
297
+ cache_slot_idx: Optional[int] = None,
298
+ **kwargs,
299
+ ) -> torch.Tensor:
300
+ residual = hidden_states
301
+
302
+ hidden_states = self.input_layernorm(hidden_states)
303
+
304
+ # Apply global RoPE to non-sliding layers, local RoPE to sliding layers
305
+ if self.self_attn.is_sliding:
306
+ position_embeddings = position_embeddings_local
307
+ else:
308
+ position_embeddings = position_embeddings_global
309
+
310
+ hidden_states, _ = self.self_attn(
311
+ hidden_states=hidden_states,
312
+ position_embeddings=position_embeddings,
313
+ attention_mask=attention_mask,
314
+ position_ids=position_ids,
315
+ past_key_values=past_key_values,
316
+ use_cache=use_cache,
317
+ cache_position=cache_position,
318
+ cache_slot_idx=cache_slot_idx,
319
+ **kwargs,
320
+ )
321
+ hidden_states = self.post_attention_layernorm(hidden_states)
322
+ hidden_states = residual + hidden_states
323
+
324
+ residual = hidden_states
325
+ hidden_states = self.pre_feedforward_layernorm(hidden_states)
326
+ hidden_states = self.mlp(hidden_states)
327
+ hidden_states = self.post_feedforward_layernorm(hidden_states)
328
+ hidden_states = residual + hidden_states
329
+
330
+ return hidden_states
331
+
332
+
333
+ @auto_docstring
334
+ class GemmagainPreTrainedModel(PreTrainedModel):
335
+ config_class = GemmagainConfig
336
+ base_model_prefix = "model"
337
+ supports_gradient_checkpointing = True
338
+ _no_split_modules = ["Gemma3DecoderLayer"]
339
+ _skip_keys_device_placement = ["past_key_values"]
340
+ _supports_flash_attn = True
341
+ _supports_sdpa = True
342
+ _supports_flex_attn = True
343
+ _can_compile_fullgraph = True
344
+ _supports_attention_backend = True
345
+ _can_record_outputs = {
346
+ "hidden_states": Gemma3DecoderLayer,
347
+ "attentions": Gemma3Attention,
348
+ }
349
+
350
+ def _init_weights(self, module):
351
+ super()._init_weights(module)
352
+ # Initialize RMSNorm weights to 0 (Gemma3 uses 1 + weight)
353
+ if "RMSNorm" in module.__class__.__name__:
354
+ module.weight.data.zero_()
355
+
356
+
357
+ def _expand_layer_sequence(layer_sequence, num_hidden_layers):
358
+ """Expand layer_sequence config into a flat list of layer indices."""
359
+ l_seq = []
360
+ for item in layer_sequence:
361
+ if isinstance(item, int):
362
+ l_seq.append(item)
363
+ elif isinstance(item, list):
364
+ if len(item) == 2:
365
+ start, end = item
366
+ l_seq += list(range(start, min(end, num_hidden_layers)))
367
+ elif len(item) == 3:
368
+ start, end, repeats = item
369
+ l_seq += list(range(start, min(end, num_hidden_layers))) * repeats
370
+ else:
371
+ raise ValueError(f"Invalid layer_sequence item: {item}")
372
+ else:
373
+ raise ValueError(f"Invalid layer_sequence item type: {type(item)}")
374
+ return l_seq
375
+
376
+
377
+ def _bidirectional_window_overlay(sliding_window: int) -> Callable[[int, int, int, int], bool]:
378
+ """Enables a bidirectional mask within the sliding window."""
379
+ def inner_mask(batch_idx: int, head_idx: int, q_idx: int, kv_idx: int) -> bool:
380
+ return abs(q_idx - kv_idx) < sliding_window
381
+ return inner_mask
382
+
383
+
384
+ @auto_docstring
385
+ class GemmagainModel(GemmagainPreTrainedModel):
386
+ def __init__(self, config: GemmagainConfig):
387
+ super().__init__(config)
388
+ self.padding_idx = config.pad_token_id
389
+ self.vocab_size = config.vocab_size
390
+
391
+ self.embed_tokens = Gemma3TextScaledWordEmbedding(
392
+ config.vocab_size, config.hidden_size, self.padding_idx, embed_scale=config.hidden_size**0.5
393
+ )
394
+ self.layers = nn.ModuleList(
395
+ [Gemma3DecoderLayer(config, layer_idx) for layer_idx in range(config.num_hidden_layers)]
396
+ )
397
+ self.norm = Gemma3RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
398
+ self.rotary_emb = Gemma3RotaryEmbedding(config=config)
399
+ self.gradient_checkpointing = False
400
+
401
+ # Create local RoPE with different theta
402
+ local_config = copy.deepcopy(config)
403
+ local_config.rope_theta = config.rope_local_base_freq
404
+ local_config.rope_scaling = {"rope_type": "default"}
405
+ self.rotary_emb_local = Gemma3RotaryEmbedding(config=local_config)
406
+
407
+ # Pre-compute expanded layer sequence for looping
408
+ self._layer_sequence = _expand_layer_sequence(config.layer_sequence, config.num_hidden_layers)
409
+ self._num_cache_slots = len(self._layer_sequence)
410
+
411
+ self.post_init()
412
+
413
+ @auto_docstring
414
+ def forward(
415
+ self,
416
+ input_ids: Optional[torch.LongTensor] = None,
417
+ attention_mask: Optional[torch.Tensor] = None,
418
+ position_ids: Optional[torch.LongTensor] = None,
419
+ past_key_values: Optional[Cache] = None,
420
+ inputs_embeds: Optional[torch.FloatTensor] = None,
421
+ use_cache: Optional[bool] = None,
422
+ cache_position: Optional[torch.LongTensor] = None,
423
+ **kwargs: Unpack[TransformersKwargs],
424
+ ) -> BaseModelOutputWithPast:
425
+ if (input_ids is None) ^ (inputs_embeds is not None):
426
+ raise ValueError("You must specify exactly one of input_ids or inputs_embeds")
427
+
428
+ if inputs_embeds is None:
429
+ inputs_embeds = self.embed_tokens(input_ids)
430
+
431
+ if use_cache:
432
+ if past_key_values is None:
433
+ # Create cache with enough slots for the full layer sequence
434
+ cache_config = copy.copy(self.config)
435
+ cache_config.num_hidden_layers = self._num_cache_slots
436
+ past_key_values = DynamicCache(config=cache_config)
437
+ elif isinstance(past_key_values, DynamicCache) and len(past_key_values.layers) < self._num_cache_slots:
438
+ # Extend cache if created externally with fewer slots
439
+ while len(past_key_values.layers) < self._num_cache_slots:
440
+ past_key_values.layers.append(DynamicLayer())
441
+
442
+ if cache_position is None:
443
+ past_seen_tokens = past_key_values.get_seq_length() if past_key_values is not None else 0
444
+ cache_position = torch.arange(
445
+ past_seen_tokens, past_seen_tokens + inputs_embeds.shape[1], device=inputs_embeds.device
446
+ )
447
+
448
+ if position_ids is None:
449
+ position_ids = cache_position.unsqueeze(0)
450
+
451
+ # Prepare attention masks
452
+ if not isinstance(causal_mask_mapping := attention_mask, dict):
453
+ mask_kwargs = {
454
+ "config": self.config,
455
+ "input_embeds": inputs_embeds,
456
+ "attention_mask": attention_mask,
457
+ "cache_position": cache_position,
458
+ "past_key_values": past_key_values,
459
+ "position_ids": position_ids,
460
+ }
461
+ sliding_mask_kwargs = mask_kwargs.copy()
462
+
463
+ if self.config.use_bidirectional_attention:
464
+ mask_kwargs["or_mask_function"] = lambda *args: torch.tensor(True, dtype=torch.bool)
465
+ sliding_mask_kwargs["or_mask_function"] = _bidirectional_window_overlay(self.config.sliding_window)
466
+
467
+ causal_mask_mapping = {
468
+ "full_attention": create_causal_mask(**mask_kwargs),
469
+ "sliding_attention": create_sliding_window_causal_mask(**sliding_mask_kwargs),
470
+ }
471
+
472
+ hidden_states = inputs_embeds
473
+ position_embeddings_global = self.rotary_emb(hidden_states, position_ids)
474
+ position_embeddings_local = self.rotary_emb_local(hidden_states, position_ids)
475
+
476
+ # Execute layers in the configured sequence with looping support
477
+ for cache_slot_idx, layer_idx in enumerate(self._layer_sequence):
478
+ decoder_layer = self.layers[layer_idx]
479
+ hidden_states = decoder_layer(
480
+ hidden_states,
481
+ position_embeddings_global=position_embeddings_global,
482
+ position_embeddings_local=position_embeddings_local,
483
+ attention_mask=causal_mask_mapping[decoder_layer.attention_type],
484
+ position_ids=position_ids,
485
+ past_key_values=past_key_values,
486
+ use_cache=use_cache,
487
+ cache_position=cache_position,
488
+ cache_slot_idx=cache_slot_idx,
489
+ **kwargs,
490
+ )
491
+
492
+ hidden_states = self.norm(hidden_states)
493
+
494
+ return BaseModelOutputWithPast(
495
+ last_hidden_state=hidden_states,
496
+ past_key_values=past_key_values if use_cache else None,
497
+ )
498
+
499
+
500
+ @auto_docstring
501
+ class GemmagainForCausalLM(GemmagainPreTrainedModel, GenerationMixin):
502
+ _tied_weights_keys = ["lm_head.weight"]
503
+ _tp_plan = {"lm_head": "colwise_rep"}
504
+ _pp_plan = {"lm_head": (["hidden_states"], ["logits"])}
505
+
506
+ def __init__(self, config: GemmagainConfig):
507
+ super().__init__(config)
508
+ self.model = GemmagainModel(config)
509
+ self.vocab_size = config.vocab_size
510
+ self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
511
+
512
+ self.post_init()
513
+
514
+ @can_return_tuple
515
+ @auto_docstring
516
+ def forward(
517
+ self,
518
+ input_ids: Optional[torch.LongTensor] = None,
519
+ attention_mask: Optional[torch.Tensor] = None,
520
+ position_ids: Optional[torch.LongTensor] = None,
521
+ past_key_values: Optional[Cache] = None,
522
+ inputs_embeds: Optional[torch.FloatTensor] = None,
523
+ labels: Optional[torch.LongTensor] = None,
524
+ use_cache: Optional[bool] = None,
525
+ cache_position: Optional[torch.LongTensor] = None,
526
+ logits_to_keep: Union[int, torch.Tensor] = 0,
527
+ **kwargs: Unpack[TransformersKwargs],
528
+ ) -> CausalLMOutputWithPast:
529
+ outputs: BaseModelOutputWithPast = self.model(
530
+ input_ids=input_ids,
531
+ attention_mask=attention_mask,
532
+ position_ids=position_ids,
533
+ past_key_values=past_key_values,
534
+ inputs_embeds=inputs_embeds,
535
+ use_cache=use_cache,
536
+ cache_position=cache_position,
537
+ **kwargs,
538
+ )
539
+
540
+ hidden_states = outputs.last_hidden_state
541
+ slice_indices = slice(-logits_to_keep, None) if isinstance(logits_to_keep, int) else logits_to_keep
542
+ logits = self.lm_head(hidden_states[:, slice_indices, :])
543
+
544
+ if self.config.final_logit_softcapping is not None:
545
+ logits = logits / self.config.final_logit_softcapping
546
+ logits = torch.tanh(logits)
547
+ logits = logits * self.config.final_logit_softcapping
548
+
549
+ loss = None
550
+ if labels is not None:
551
+ # Standard loss calculation
552
+ shift_logits = logits[..., :-1, :].contiguous()
553
+ shift_labels = labels[..., 1:].contiguous()
554
+ loss_fct = CrossEntropyLoss()
555
+ shift_logits = shift_logits.view(-1, self.config.vocab_size)
556
+ shift_labels = shift_labels.view(-1)
557
+ shift_labels = shift_labels.to(shift_logits.device)
558
+ loss = loss_fct(shift_logits, shift_labels)
559
+
560
+ return CausalLMOutputWithPast(
561
+ loss=loss,
562
+ logits=logits,
563
+ past_key_values=outputs.past_key_values,
564
+ hidden_states=outputs.hidden_states,
565
+ attentions=outputs.attentions,
566
+ )
567
+
568
+
569
+ __all__ = [
570
+ "GemmagainForCausalLM",
571
+ "GemmagainModel",
572
+ "GemmagainPreTrainedModel",
573
+ ]
special_tokens_map.json ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "boi_token": "<start_of_image>",
3
+ "bos_token": {
4
+ "content": "<bos>",
5
+ "lstrip": false,
6
+ "normalized": false,
7
+ "rstrip": false,
8
+ "single_word": false
9
+ },
10
+ "eoi_token": "<end_of_image>",
11
+ "eos_token": {
12
+ "content": "<eos>",
13
+ "lstrip": false,
14
+ "normalized": false,
15
+ "rstrip": false,
16
+ "single_word": false
17
+ },
18
+ "image_token": "<image_soft_token>",
19
+ "pad_token": {
20
+ "content": "<pad>",
21
+ "lstrip": false,
22
+ "normalized": false,
23
+ "rstrip": false,
24
+ "single_word": false
25
+ },
26
+ "unk_token": {
27
+ "content": "<unk>",
28
+ "lstrip": false,
29
+ "normalized": false,
30
+ "rstrip": false,
31
+ "single_word": false
32
+ }
33
+ }
tokenizer.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4667f2089529e8e7657cfb6d1c19910ae71ff5f28aa7ab2ff2763330affad795
3
+ size 33384568
tokenizer.model ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:1299c11d7cf632ef3b4e11937501358ada021bbdf7c47638d13c0ee982f2e79c
3
+ size 4689074
tokenizer_config.json ADDED
The diff for this file is too large to render. See raw diff