Skip to content

Instantly share code, notes, and snippets.

@Ademking
Created December 8, 2025 13:50
Show Gist options
  • Select an option

  • Save Ademking/f240b12a647c8365862b4741e2b9a627 to your computer and use it in GitHub Desktop.

Select an option

Save Ademking/f240b12a647c8365862b4741e2b9a627 to your computer and use it in GitHub Desktop.
import torch
from safetensors.torch import load_file
import json
import os
# path to HF checkpoint
index_file = "model.safetensors.index.json"
with open(index_file, "r") as f:
index = json.load(f)
# all shards
shards = index["weight_map"].values()
shards = sorted(list(set(shards)))
# load each shard
state_dict = {}
for shard in shards:
print(f"Loading {shard}...")
sd = load_file(shard)
state_dict.update(sd)
# extract only the vision encoder keys
visual_sd = {
k: v for k, v in state_dict.items()
if k.startswith("visual.") or k.startswith("vision_tower.")
}
print(f"Extracted {len(visual_sd)} visual weights")
torch.save(visual_sd, "qwen_visual_model_weights.pth")
print("Saved to qwen_visual_model_weights.pth")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment