| |
| import argparse |
| import json |
| import os |
| from pathlib import Path |
|
|
| import numpy as np |
| import torch |
| from transformers import AutoModelForCausalLM, AutoTokenizer |
|
|
| from collect_experts import _collect_topk_indices |
| from train_inverter_mlp import ExpertMLP |
| from v2.train_inverter_v2 import EncoderOnlyModel as EncoderOnlyModelV2 |
| from v4.train_inverter_v4 import EncoderOnlyModel as EncoderOnlyModelV4 |
|
|
|
|
| def _load_state_dict(path: str): |
| ckpt = torch.load(path, map_location="cpu") |
| state = ckpt.get("model", ckpt) |
| if any(k.startswith("_orig_mod.") for k in state.keys()): |
| state = {k.replace("_orig_mod.", ""): v for k, v in state.items()} |
| return state |
|
|
|
|
| def _chunk_tokens(tokens, seq_len): |
| for i in range(0, len(tokens), seq_len): |
| yield tokens[i:i + seq_len] |
|
|
|
|
| def main(): |
| parser = argparse.ArgumentParser( |
| description="Generate text with GPT-OSS and evaluate inverter accuracy." |
| ) |
| parser.add_argument("--model", default="openai/gpt-oss-20b") |
| parser.add_argument("--attn-impl", default="flash_attention_2") |
| parser.add_argument("--prompt", default="Write a technical paragraph about neural networks.") |
| parser.add_argument("--text-file", default=None) |
| parser.add_argument("--max-new-tokens", type=int, default=5000) |
| parser.add_argument("--seq-len", type=int, default=32) |
| parser.add_argument("--batch-size", type=int, default=8) |
| parser.add_argument("--layers", type=int, default=10) |
| parser.add_argument("--mlp-layers", type=int, default=None) |
| parser.add_argument("--tx-layers", type=int, default=None) |
| parser.add_argument("--topk", type=int, default=4) |
| parser.add_argument("--eval-topk", default="1,5,10") |
| parser.add_argument("--mlp-ckpt", default=None) |
| parser.add_argument("--mlp-d-model", type=int, default=256) |
| parser.add_argument("--mlp-hidden-dim", type=int, default=512) |
| parser.add_argument("--tx-ckpt", default=None) |
| parser.add_argument("--tx-backend", choices=["v2", "v4"], default="v2") |
| parser.add_argument("--tx-input-mode", choices=["set", "multihot"], default="set") |
| parser.add_argument("--tx-d-model", type=int, default=768) |
| parser.add_argument("--tx-n-head", type=int, default=12) |
| parser.add_argument("--tx-d-ff", type=int, default=2048) |
| parser.add_argument("--tx-n-layer", type=int, default=6) |
| parser.add_argument("--logit-softcap", type=float, default=30.0) |
| parser.add_argument("--layer-gating", action="store_true") |
| parser.add_argument("--eval-generated-only", action="store_true") |
| parser.add_argument("--out", default="gen_eval.json") |
| args = parser.parse_args() |
|
|
| device = torch.device("cuda" if torch.cuda.is_available() else "cpu") |
| mlp_layers = args.mlp_layers or args.layers |
| tx_layers = args.tx_layers or args.layers |
| eval_topk = [int(k.strip()) for k in args.eval_topk.split(",") if k.strip()] |
| eval_topk = sorted({k for k in eval_topk if k > 0}) |
| tokenizer = AutoTokenizer.from_pretrained(args.model) |
| if tokenizer.pad_token_id is None: |
| tokenizer.pad_token_id = tokenizer.eos_token_id |
|
|
| model = AutoModelForCausalLM.from_pretrained( |
| args.model, |
| torch_dtype=torch.bfloat16 if device.type == "cuda" else torch.float32, |
| device_map="auto" if device.type == "cuda" else None, |
| trust_remote_code=True, |
| attn_implementation=args.attn_impl, |
| ) |
| model.config.output_router_logits = False |
| if hasattr(model.config, "router_aux_loss_coef"): |
| model.config.router_aux_loss_coef = 0.0 |
| model.eval() |
|
|
| if args.text_file: |
| text_path = Path(args.text_file) |
| prompt_text = text_path.read_text(encoding="utf-8") |
| else: |
| prompt_text = args.prompt |
|
|
| inputs = tokenizer(prompt_text, return_tensors="pt").to(model.device) |
| input_len = inputs["input_ids"].shape[1] |
|
|
| if args.max_new_tokens == 0: |
| full_ids = inputs["input_ids"][0].tolist() |
| else: |
| with torch.no_grad(): |
| outputs = model.generate( |
| **inputs, |
| max_new_tokens=args.max_new_tokens, |
| do_sample=False, |
| pad_token_id=tokenizer.eos_token_id, |
| ) |
| full_ids = outputs[0].tolist() |
|
|
| gen_ids = full_ids[input_len:] |
| eval_ids = gen_ids if args.eval_generated_only else full_ids |
|
|
| |
| mlp = None |
| if args.mlp_ckpt: |
| mlp = ExpertMLP( |
| num_experts=32, |
| num_layers=mlp_layers, |
| topk=args.topk, |
| d_model=args.mlp_d_model, |
| hidden_dim=args.mlp_hidden_dim, |
| vocab_size=len(tokenizer), |
| dropout=0.1, |
| ) |
| mlp.load_state_dict(_load_state_dict(args.mlp_ckpt), strict=True) |
| mlp.eval().to(device) |
|
|
| tx = None |
| if args.tx_ckpt: |
| if args.tx_backend == "v4": |
| tx = EncoderOnlyModelV4( |
| vocab_size=len(tokenizer), |
| num_experts=32, |
| num_layers=tx_layers, |
| topk=args.topk, |
| d_model=args.tx_d_model, |
| n_head=args.tx_n_head, |
| d_ff=args.tx_d_ff, |
| n_layer=args.tx_n_layer, |
| dropout=0.1, |
| max_len=args.seq_len, |
| layer_gating=args.layer_gating, |
| logit_softcap=args.logit_softcap, |
| input_mode=args.tx_input_mode, |
| ) |
| else: |
| tx = EncoderOnlyModelV2( |
| vocab_size=len(tokenizer), |
| num_experts=32, |
| num_layers=tx_layers, |
| topk=args.topk, |
| d_model=args.tx_d_model, |
| n_head=args.tx_n_head, |
| d_ff=args.tx_d_ff, |
| n_layer=args.tx_n_layer, |
| dropout=0.1, |
| max_len=args.seq_len, |
| layer_gating=args.layer_gating, |
| logit_softcap=args.logit_softcap, |
| ) |
| tx.load_state_dict(_load_state_dict(args.tx_ckpt), strict=True) |
| tx.eval().to(device, dtype=torch.bfloat16 if device.type == "cuda" else torch.float32) |
|
|
| if not mlp and not tx: |
| raise ValueError("Provide at least one of --mlp-ckpt or --tx-ckpt.") |
|
|
| correct_mlp = {k: 0 for k in eval_topk} |
| correct_tx = {k: 0 for k in eval_topk} |
| total = 0 |
|
|
| for chunk in _chunk_tokens(eval_ids, args.seq_len): |
| if len(chunk) == 0: |
| continue |
| pad_len = args.seq_len - len(chunk) |
| if pad_len: |
| chunk = chunk + [tokenizer.pad_token_id] * pad_len |
| input_ids = torch.tensor([chunk], device=model.device) |
| with torch.no_grad(): |
| idx, vals = _collect_topk_indices(model, input_ids, topk=args.topk) |
| idx = idx.to(device) |
| vals = vals.to(device) |
|
|
| targets = torch.tensor(chunk, device=device) |
| valid_len = args.seq_len - pad_len |
|
|
| if mlp: |
| mlp_logits = mlp( |
| idx[:, :, :mlp_layers].view(-1, mlp_layers, args.topk), |
| vals[:, :, :mlp_layers].view(-1, mlp_layers, args.topk), |
| ) |
| for k in eval_topk: |
| topk_pred = torch.topk(mlp_logits, k=k, dim=-1).indices |
| match = (topk_pred[:valid_len] == targets[:valid_len].unsqueeze(-1)).any(dim=-1) |
| correct_mlp[k] += int(match.sum().item()) |
|
|
| if tx: |
| attention_mask = torch.ones((1, args.seq_len), device=device, dtype=torch.bool) |
| with torch.autocast(device_type=device.type, dtype=torch.bfloat16, enabled=device.type == "cuda"): |
| if args.tx_backend == "v4": |
| tx_logits = tx(idx[:, :, :tx_layers], attention_mask) |
| else: |
| tx_logits = tx(idx[:, :, :tx_layers], vals[:, :, :tx_layers], attention_mask) |
| logits = tx_logits[0] |
| for k in eval_topk: |
| topk_pred = torch.topk(logits, k=k, dim=-1).indices |
| match = (topk_pred[:valid_len] == targets[:valid_len].unsqueeze(-1)).any(dim=-1) |
| correct_tx[k] += int(match.sum().item()) |
|
|
| total += valid_len |
|
|
| result = { |
| "prompt": args.prompt, |
| "input_tokens": input_len, |
| "eval_tokens": total, |
| "eval_generated_only": args.eval_generated_only, |
| "mlp_topk": {str(k): (correct_mlp[k] / total) for k in eval_topk} if mlp else None, |
| "tx_topk": {str(k): (correct_tx[k] / total) for k in eval_topk} if tx else None, |
| } |
|
|
| out_path = Path(args.out) |
| out_path.write_text(json.dumps(result, indent=2), encoding="utf-8") |
| print(json.dumps(result, indent=2)) |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|