MCPcopy Create free account
hub / github.com/MuLabPKU/TransArch / main

Function main

GQLA_preprint/src/convert.py:101–251  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

99
100
101def main():
102 args = parse_args()
103 torch.manual_seed(args.seed)
104 dtype = _DTYPES[args.dtype]
105
106 print(f"Loading {args.model_path} (dtype={args.dtype}, device_map={args.device_map}) ...")
107 model = AutoModelForCausalLM.from_pretrained(
108 args.model_path, dtype=dtype, device_map=args.device_map, trust_remote_code=False,
109 )
110 model.eval()
111 config = model.config
112 layout = GqlaLayout.from_config(config, args.num_kv_heads)
113 print(
114 f"H={layout.num_heads} heads -> G={layout.num_kv_heads} KV groups "
115 f"(group_size={layout.group_size}); KV cache reduction = {layout.group_size}x"
116 )
117
118 tokenizer = AutoTokenizer.from_pretrained(args.model_path, trust_remote_code=False)
119
120 ppl_loader = ppl_orig = ppl_gqla = None
121 if args.eval_ppl:
122 print(f"Building PPL loader ({args.eval_ppl_dataset}, seqlen={args.eval_ppl_seqlen}) ...")
123 ppl_loader = prepare_ppl_dataloader(
124 tokenizer, args.eval_ppl_dataset, args.eval_ppl_seqlen,
125 args.eval_ppl_batch_size, max_chunks=args.eval_ppl_max_chunks,
126 )
127 print("Eval PPL on original MLA model ...")
128 ppl_orig = evaluate_ppl(model, ppl_loader)
129 print(f" Original MLA PPL = {ppl_orig:.4f}")
130
131 print(
132 f"\nBuilding calibration ({args.cal_dataset}, n={args.cal_nsamples}, "
133 f"len={args.cal_seqlen}) ..."
134 )
135 batches = prepare_calibration_inputs(
136 tokenizer, args.cal_dataset, args.cal_nsamples, args.cal_seqlen, seed=args.seed,
137 )
138
139 # First-rank device for input_ids; HF device_map auto-routes through the rest.
140 input_device = next(model.parameters()).device
141 print(f"Gathering per-layer hidden states ({config.num_hidden_layers} layers) ...")
142 if args.hessian_pca:
143 calib, final_hiddens = gather_calibration_hidden_states(
144 model, batches, input_device, capture_final=True,
145 )
146 else:
147 calib = gather_calibration_hidden_states(model, batches, input_device)
148 final_hiddens = None
149
150 # Hessian-aware token weights (NLL of next token from the teacher's lm_head).
151 # Computed once on the cached final hidden states; threaded into every layer's
152 # cov accumulation. When --hessian_pca is OFF, token_weights stays None and the
153 # cov path is byte-identical to the legacy code.
154 token_weights = None
155 if args.hessian_pca:
156 print(f"[hessian_pca] computing per-token NLL weights ({args.hessian_mode}) ...")
157 token_weights = compute_token_weights_nll(model, final_hiddens, batches)
158 stats = diagnose_weights(token_weights)

Callers 1

convert.pyFile · 0.70

Calls 15

parse_argsFunction · 0.85
prepare_ppl_dataloaderFunction · 0.85
diagnose_weightsFunction · 0.85
collect_full_kv_gramsFunction · 0.85
compute_head_similarityFunction · 0.85
greedy_balanced_groupingFunction · 0.85
fit_per_group_pcaFunction · 0.85

Tested by

no test coverage detected