Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/transformers/models/gemma4/convert_gemma4_weights.py
Original file line number Diff line number Diff line change
Expand Up @@ -706,13 +706,15 @@ def convert_transformer_weights(

converted_paths: list[str] = []
converted_weights: list[Any] = []
first_kv_shared_layer_idx = config.num_hidden_layers - getattr(config, "num_kv_shared_layers", 0)

# Handle new checkpoint format: transformer/layer_N/...
# TODO(philculliton):Direct handling for unstacked checkpoint type, needs to be merged to allow for unified tensor handling
if path.startswith(f"{_TRANSFORMER_PARAMETER}/layer_"):
# Extract layer number from path like "transformer/layer_0/attn/q_einsum"
layer_str = path.split("/")[1] # "layer_0"
layer_idx = int(layer_str.replace("layer_", "")) # 0
is_kv_shared_layer = layer_idx >= first_kv_shared_layer_idx > 0
base_path = f"layers.{layer_idx}"

# Determine head_dim from actual checkpoint weight dimensions
Expand All @@ -738,7 +740,7 @@ def convert_transformer_weights(
converted_weights.append(
matrix.transpose(2, 0, 1).reshape(config.hidden_size, config.num_attention_heads * head_dim)
)
elif path.endswith("attn/kv_einsum"):
elif path.endswith("attn/kv_einsum") and not is_kv_shared_layer:
converted_paths.extend(
[
f"{base_path}.self_attn.k_proj.weight",
Expand All @@ -753,7 +755,7 @@ def convert_transformer_weights(
v_proj_weights.reshape(kv_proj_shape).transpose(),
]
)
elif path.endswith("attn/k_einsum"):
elif path.endswith("attn/k_einsum") and not is_kv_shared_layer:
converted_paths.append(f"{base_path}.self_attn.k_proj.weight")
converted_weights.append(
matrix.transpose(1, 0, 2)
Expand All @@ -770,7 +772,7 @@ def convert_transformer_weights(
elif path.endswith("attn/query_norm"):
converted_paths.append(f"{base_path}.self_attn.q_norm.weight")
converted_weights.append(matrix)
elif path.endswith("attn/key_norm"):
elif path.endswith("attn/key_norm") and not is_kv_shared_layer:
converted_paths.append(f"{base_path}.self_attn.k_norm.weight")
converted_weights.append(matrix)
elif path.endswith("mlp/gating_einsum"):
Expand Down Expand Up @@ -816,6 +818,7 @@ def convert_transformer_weights(

for i, matrix in enumerate(weights):
layer_idx = _SLIDING_WINDOW_PATTERN * i + attention_type_index
is_kv_shared_layer = layer_idx >= first_kv_shared_layer_idx > 0
base_path = f"layers.{layer_idx}"
head_dim = (
config.global_head_dim
Expand All @@ -831,7 +834,7 @@ def convert_transformer_weights(
converted_weights.append(
matrix.transpose(2, 0, 1).reshape(config.hidden_size, config.num_attention_heads * head_dim)
)
elif path.endswith("attn/kv_einsum"):
elif path.endswith("attn/kv_einsum") and not is_kv_shared_layer:
converted_paths.extend(
[
f"{base_path}.self_attn.k_proj.weight",
Expand All @@ -846,7 +849,7 @@ def convert_transformer_weights(
v_proj_weights.reshape(kv_proj_shape).transpose(),
]
)
elif path.endswith("attn/k_einsum"):
elif path.endswith("attn/k_einsum") and not is_kv_shared_layer:
converted_paths.append(f"{base_path}.self_attn.k_proj.weight")
converted_weights.append(
matrix.transpose(1, 0, 2)
Expand All @@ -863,7 +866,7 @@ def convert_transformer_weights(
elif path.endswith("attn/query_norm"):
converted_paths.append(f"{base_path}.self_attn.q_norm.weight")
converted_weights.append(matrix)
elif path.endswith("attn/key_norm"):
elif path.endswith("attn/key_norm") and not is_kv_shared_layer:
converted_paths.append(f"{base_path}.self_attn.k_norm.weight")
converted_weights.append(matrix)
elif path.endswith("mlp/gating_einsum"):
Expand Down
Loading