Different Weight Key Nesting from The Original

#1
by chibop - opened

The vision model weights in this repository are nested under an incorrect prefix than the original model from Qwen repository.

The original model uses model.visual.* as a prefix, but this repo uses model.language_model.visual.*.

Because the architecture Qwen3_5MoeForConditionalGeneration expects the vision tower to be a sibling of the language model rather than a child of it, loader scripts for conversion tools fail with a ValueError reporting hundreds of "extra" parameters.

The affected files are model.safetensors.index.json and model-00002-of-00002.safetensors. To fix this at the source, the keys inside the safetensors and the index map must be renamed. Below is a Python script that performs this correction:

import json
import os
from safetensors import safe_open
from safetensors.torch import save_file

model_path = "./llmfan46/Qwen3.6-35B-A3B-uncensored-heretic"
index_file = os.path.join(model_path, "model.safetensors.index.json")

# 1. Update Index Map
with open(index_file, "r") as f:
    index = json.load(f)

weight_map = index["weight_map"]
new_weight_map = {}
files_to_update = set()

for key, val in weight_map.items():
    if key.startswith("model.language_model.visual."):
        new_key = key.replace("model.language_model.visual.", "model.visual.")
        new_weight_map[new_key] = val
        files_to_update.add(val)
    else:
        new_weight_map[key] = val

index["weight_map"] = new_weight_map
with open(index_file, "w") as f:
    json.dump(index, f, indent=2)

# 2. Update Safetensors Files
for filename in files_to_update:
    file_path = os.path.join(model_path, filename)
    tensors = {}
    with safe_open(file_path, framework="pt", device="cpu") as f:
        for key in f.keys():
            tensor = f.get_tensor(key)
            if key.startswith("model.language_model.visual."):
                new_key = key.replace("model.language_model.visual.", "model.visual.")
                tensors[new_key] = tensor
            else:
                tensors[key] = tensor

    save_file(tensors, file_path)

I was running into this when attempting to quantize it to oQ for MLX use. Thanks for the comment.

The vision model weights in this repository are nested under an incorrect prefix than the original model from Qwen repository.

It's not "incorrect", this model was exported with transformers 5.5.4 which is current (released in April 2026), the official model was exported with transformers 4.57.1 which is at this point very old (released in October 2025), there has been a lot of changes in transformers between version 4.57.1 and version 5.5.4, heck the latest transformers version is version 5.7.0 released only 15 days after version 5.5.4 and it is incompatible with models exported with version 5.5.4.

Sign up or log in to comment