Upload code/train_sft_v5.py with huggingface_hub
Browse files167 unmodified lines16816917017117217317417517617724 unmodified lines202203204205206207208167 unmodified linescheckpoint = torch.load(ckpt_path, map_location="cpu", weights_only=False)state_dict = strip_training_prefixes(checkpoint["model"])ckpt_vocab_size = state_dict['tok_emb.weight'].shape[0]cfg = ViuAIConfig(vocab_size=ckpt_vocab_size, use_checkpoint=True)model = ViuAI(cfg).to(DEVICE)model.load_state_dict(state_dict, strict=True)model.head.weight = model.tok_emb.weightif dist.is_initialized() and world_size > 1:24 unmodified lineslogits, _ = model(x_in)# Loss calculation (ignoring -100)loss = F.cross_entropy(logits.view(-1, ckpt_vocab_size), y_target.view(-1), ignore_index=-100)loss.backward()torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0)167 unmodified lines16816917017117217317417517617717817918018118218318418518618718818919019119219324 unmodified lines218219220221222223224167 unmodified linescheckpoint = torch.load(ckpt_path, map_location="cpu", weights_only=False)state_dict = strip_training_prefixes(checkpoint["model"])cfg = ViuAIConfig(use_checkpoint=True) # Defaults to vocab_size=64009ckpt_vocab_size = state_dict['tok_emb.weight'].shape[0]model_vocab_size = cfg.vocab_size# Resize checkpoint embeddings to match SFT model size (e.g., 64000 -> 64009)if model_vocab_size > ckpt_vocab_size:log(f"?? Resizing tok_emb from {ckpt_vocab_size} to {model_vocab_size}")new_emb = torch.zeros(model_vocab_size, state_dict['tok_emb.weight'].shape[1], dtype=state_dict['tok_emb.weight'].dtype)new_emb[:ckpt_vocab_size] = state_dict['tok_emb.weight']new_emb[ckpt_vocab_size:] = state_dict['tok_emb.weight'].mean(dim=0, keepdim=True)state_dict['tok_emb.weight'] = new_embif 'head.weight' in state_dict:new_head = torch.zeros(model_vocab_size, state_dict['head.weight'].shape[1], dtype=state_dict['head.weight'].dtype)new_head[:ckpt_vocab_size] = state_dict['head.weight']new_head[ckpt_vocab_size:] = state_dict['head.weight'].mean(dim=0, keepdim=True)state_dict['head.weight'] = new_headmodel = ViuAI(cfg).to(DEVICE)model.load_state_dict(state_dict, strict=False)model.head.weight = model.tok_emb.weightif dist.is_initialized() and world_size > 1:24 unmodified lineslogits, _ = model(x_in)# Loss calculation (ignoring -100)loss = F.cross_entropy(logits.view(-1, model_vocab_size), y_target.view(-1), ignore_index=-100)loss.backward()torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0)
code/train_sft_v5.py+19-3