ziyan98's picture
Add files using upload-large-folder tool
bbd7f1f verified
Raw History Blame Contribute Delete
1.64 kB
import json, os, sys
from argparse import Namespace
import torch
import torch.distributed.checkpoint as dcp
from torch.distributed.checkpoint import FileSystemReader
from safetensors import safe_open
CKPT="/projects/u6uv/ziyan/models/megatron/Qwen3.8-27B_torch_dist/release"
HF="/projects/u6uv/ziyan/models/Qwen/Qwen3.8-27B"
sys.path.insert(0,"/opt/slime")
from slime.backends.megatron_utils.megatron_to_hf import convert_to_hf
md=FileSystemReader(CKPT).read_metadata(); SDM=md.state_dict_metadata
idx=json.load(open(os.path.join(HF,"model.safetensors.index.json")))["weight_map"]
args=Namespace(kv_channels=256,hidden_size=5120,num_attention_heads=24,num_query_groups=4,
vocab_size=248320,q_lora_rank=None,num_experts=None)
for mn,expect in [("embedding.word_embeddings.weight","model.language_model.embed_tokens.weight"),
("output_layer.weight","lm_head.weight")]:
m=SDM[mn]; t=torch.empty(tuple(m.size),dtype=m.properties.dtype)
dcp.load({mn:t},storage_reader=FileSystemReader(CKPT),no_dist=True)
out=convert_to_hf(args,"qwen3_5config","module.module."+mn,t,None)
for hn,ht in out:
f=idx[hn]
with safe_open(os.path.join(HF,f),framework="pt",device="cpu") as g:
ref=g.get_tensor(hn)
nb=0
for i in range(0,ht.shape[0],16384):
nb+=int((ref[i:i+16384].reshape(-1).view(torch.int16)!=ht[i:i+16384].contiguous().reshape(-1).view(torch.int16)).sum())
print(f"{mn:40s} -> {hn:44s} megatron{tuple(t.shape)} conv{tuple(ht.shape)} hf{tuple(ref.shape)} nbitdiff={nb} name_ok={hn==expect and hn in idx}")
del ref
del t,out