Views
No views yet
Qwen/Qwen2.5-7B-Instructq_proj, v_projrichardr1126/spider-schema.0.2434 at step 350 / 1000QuantizedLinear before copying the adapter weights:1import torch
2from transformers import AutoModelForCausalLM
3
4# 1. Load base model
5base_model = AutoModelForCausalLM.from_pretrained(
6 "Qwen/Qwen2.5-7B-Instruct",
7 torch_dtype=torch.float16,
8 device_map="auto"
9)
10
11# 2. Re-create the NF4 codebook
12cb = make_nf4_codebook()
13
14# 3. Swap linear layers to custom QuantizedLinear in-place
15for layer in base_model.model.layers:
16 layer.self_attn.q_proj = QuantizedLinear.from_linear(layer.self_attn.q_proj, cb)
17 layer.self_attn.v_proj = QuantizedLinear.from_linear(layer.self_attn.v_proj, cb)
18
19# 4. Inject LoRA adapters
20inject_lora(base_model, r=8, alpha=16)
21
22# 5. Load adapter weights from checkpoint
23checkpoint = torch.load("best_qlora_checkpoint.pt")
24lora_state = checkpoint["lora_state_dict"]
25
26for name, param in base_model.named_parameters():
27 if name in lora_state:
28 param.data.copy_(lora_state[name].to(param.device))
29
30print("Model successfully loaded with custom quantized weights!")