-
Notifications
You must be signed in to change notification settings - Fork 582
add glm5.2 indexshare #4832
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add glm5.2 indexshare #4832
Changes from all commits
fd0e960
97318ff
874baa2
c984596
4c2cf74
9fc3767
71a1f3f
5bdce25
3bc1393
325e42f
5d47740
edd4688
a5b64e6
00eb672
ecc8152
64bc8e5
171a6ba
aae73ee
e55e0a7
6a07b6e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -463,7 +463,24 @@ def _build_single_axis_stacked_tensor( | |||||||||||||||||||||||||||||||||||||||||||||||||||
| if isinstance(hf_key_single, (list, tuple)): | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| hf_tensor_numpy = tuple(tensor_getter_fn(k) for k in hf_key_single) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| hf_tensor_numpy = tensor_getter_fn(hf_key_single) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| hf_tensor_numpy = tensor_getter_fn(hf_key_single) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| except (ValueError, KeyError) as e: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if "indexer" in str(hf_key_single) and str(hf_key_single).startswith("model.layers."): | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| import re | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| m = re.match(r"model\.layers\.(\d+)\.(.+)", str(hf_key_single)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if m: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| rest = m.group(2) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| donor_key = f"model.layers.0.{rest}" | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| hf_tensor_numpy = tensor_getter_fn(donor_key) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| except Exception: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| hf_tensor_numpy = np.zeros(mt_slice_shape, dtype=np.float32) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| raise e | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| raise e | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| processed_hf_tensor = apply_hook_fns(hf_tensor_numpy, mt_slice_shape, hook_fns) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| tensors_to_stack.append(processed_hf_tensor) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -999,6 +1016,24 @@ def main( | |||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| def _eager_getter(key): | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if key not in hf_state_dict_numpy: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if getattr(config, "use_index_share", False) and "indexer" in key and key.startswith("model.layers."): | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| import re | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| m = re.match(r"model\.layers\.(\d+)\.(.+)", key) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if m: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| layer_idx = int(m.group(1)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| rest = m.group(2) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| matching_layers = [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| int(k.split(".")[2]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| for k in hf_state_dict_numpy | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if k.startswith("model.layers.") and k.endswith(f".{rest}") | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+1026
to
+1030
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| if matching_layers: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| preceding = [l for l in matching_layers if l <= layer_idx] | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| donor_idx = max(preceding) if preceding else min(matching_layers) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| donor_key = f"model.layers.{donor_idx}.{rest}" | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if donor_key in hf_state_dict_numpy: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return _eager_getter(donor_key) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| raise ValueError(f"HuggingFace key {key} not found in state_dict.") | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| v = hf_state_dict_numpy[key] | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| # target dtype is "float32" | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -1017,6 +1052,28 @@ def _eager_getter(key): | |||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| tensor_getter = _eager_getter | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| if getattr(config, "use_index_share", False): | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| orig_tensor_getter = tensor_getter | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| def _index_share_tensor_getter(key): | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return orig_tensor_getter(key) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| except (ValueError, KeyError) as e: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if "indexer" in key and key.startswith("model.layers."): | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| import re | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| m = re.match(r"model\.layers\.(\d+)\.(.+)", key) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if m: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| rest = m.group(2) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| donor_key = f"model.layers.0.{rest}" | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return orig_tensor_getter(donor_key) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| except Exception: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| pass | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| raise e | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+1062
to
+1073
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to the issue in
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| tensor_getter = _index_share_tensor_getter | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| if is_merge_mode: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| tensor_getter = _setup_merge_mode_getter(tensor_getter, config, hf_lora_adapter_path, revision) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| # Copyright 2026 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # https://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| # model config for GLM-5.2 - 744B (Mixture of Experts with Cross-Layer IndexShare) | ||
|
|
||
| base_emb_dim: 6144 | ||
| base_num_query_heads: 64 | ||
| base_num_kv_heads: 64 | ||
| base_mlp_dim: 12288 | ||
| base_moe_mlp_dim: 2048 | ||
|
|
||
| base_num_decoder_layers: 78 | ||
|
|
||
| first_num_dense_layers: 3 | ||
| mlp_activations: ["silu","linear"] | ||
| vocab_size: 154880 | ||
| enable_dropout: false | ||
| logits_via_embedding: false | ||
| normalization_layer_epsilon: 1.0e-5 | ||
| num_experts: 256 | ||
| num_experts_per_tok: 8 | ||
| shared_experts: 1 | ||
| routed_scaling_factor: 2.5 | ||
| routed_score_func: "sigmoid" | ||
| routed_bias: true | ||
| norm_topk_prob: true | ||
| decoder_block: "glm5" | ||
| dtype: "bfloat16" | ||
| weight_dtype: "bfloat16" | ||
|
|
||
| # Multi-head Latent Attention (MLA) | ||
| attention_type: "mla" | ||
| attention: "dot_product" | ||
| q_lora_rank: 2048 | ||
| kv_lora_rank: 512 | ||
| qk_nope_head_dim: 192 | ||
| qk_rope_head_dim: 64 | ||
| v_head_dim: 256 | ||
|
|
||
| # RoPE | ||
| mscale: 1.0 | ||
| rope_type: "default" | ||
| rope_max_timescale: 1000000 # "rope_theta": 1000000 | ||
| max_position_embeddings: 202752 | ||
| rope_interleave: true | ||
|
|
||
| # Indexer for Dynamic Sparse Attention (DSA) | ||
| use_indexer: true | ||
| indexer_n_heads: 32 | ||
| indexer_head_dim: 128 | ||
| indexer_topk: 2048 | ||
|
|
||
| # GLM-5.2 Cross-Layer IndexCache / IndexShare | ||
| use_index_share: true | ||
| index_share_pattern: "FSSS" | ||
| prune_shared_indexers: true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current implementation hardcodes
model.layers.0as the donor layer for all shared layers. In GLM-5.2, there are multiple Full (F) layers (e.g., 0, 4, 8, 12, ...) acting as donors. Hardcoding layer 0 means higher shared layers (like 5, 6, 7) will incorrectly reuse layer 0's indexer weights instead of their actual donor layer (layer 4), leading to incorrect attention routing and degraded model quality.We can resolve this by dynamically searching backwards for the closest preceding layer that contains the indexer weights. This is extremely robust and doesn't require access to the config object.