| import time |
| import torch |
| import gc |
| from transformers import AutoModelForCausalLM, AutoTokenizer, set_seed |
|
|
| |
| set_seed(3407) |
|
|
| |
| MODEL_A_PATH = "./Qwen3-4B-Thinking-2507" |
| MODEL_B_PATH = ( |
| "./QiMing-Polaris-Qwen3-4B-Final-Accelerated" |
| ) |
|
|
| test_prompt = "What is the 'Burden-based Training' method?" |
|
|
| |
| gen_kwargs = { |
| "max_new_tokens": 256, |
| "temperature": 0.7, |
| "top_p": 0.8, |
| "do_sample": True, |
| } |
|
|
|
|
| def test_model(model_path, model_name): |
| print(f"\n==========================================================") |
| print(f"🚀 正在测试模型: [{model_name}]") |
| print(f"📁 模型路径 : {model_path}") |
| print(f"==========================================================") |
|
|
| |
| tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True) |
| model = AutoModelForCausalLM.from_pretrained( |
| model_path, dtype=torch.bfloat16, device_map="cuda", trust_remote_code=True |
| ) |
|
|
| |
| messages = [{"role": "user", "content": test_prompt}] |
| formatted_input = tokenizer.apply_chat_template( |
| messages, tokenize=False, add_generation_prompt=True |
| ) |
| inputs = tokenizer(formatted_input, return_tensors="pt").to("cuda") |
|
|
| |
| print("🔥 正在预热 GPU...") |
| with torch.no_grad(): |
| _ = model.generate(**inputs, max_new_tokens=10) |
|
|
| |
| print("⚡ 开始生成...") |
| torch.cuda.synchronize() |
| start_time = time.time() |
|
|
| with torch.no_grad(): |
| outputs = model.generate( |
| **inputs, pad_token_id=tokenizer.eos_token_id, **gen_kwargs |
| ) |
|
|
| torch.cuda.synchronize() |
| latency = time.time() - start_time |
|
|
| |
| generated_tokens = outputs[0][inputs.input_ids.shape[1] :] |
| response_text = tokenizer.decode(generated_tokens, skip_special_tokens=True) |
| num_tokens = len(generated_tokens) |
| tps = num_tokens / latency if latency > 0 else 0 |
|
|
| print(f"\n💬 [{model_name}] 回答:\n{response_text}") |
| print("-" * 58) |
| print( |
| f"⏱️ 耗时: {latency:.4f} 秒 | 生成 Token 数: {num_tokens} | 吞吐率: {tps:.2f} Tokens/s" |
| ) |
|
|
| |
| del model, tokenizer |
| gc.collect() |
| torch.cuda.empty_cache() |
|
|
| return { |
| "name": model_name, |
| "latency": latency, |
| "num_tokens": num_tokens, |
| "tps": tps, |
| } |
|
|
|
|
| print("==========================================================") |
| print("⚔️ 启动终极对决: 原始 36 层模型 VS 终极 32 层加速模型") |
| print("==========================================================") |
|
|
| |
| res_A = test_model(MODEL_A_PATH, "原始 36 层模型 (Base)") |
| res_B = test_model(MODEL_B_PATH, "终极 32 层加速模型 (Final-Accelerated)") |
|
|
| |
| print("\n" + "=" * 60) |
| print("🏆 最终成品模型对决结算表 (Final Models Duel)") |
| print("=" * 60) |
| print( |
| f"1. {res_A['name']:<32} : 耗时 {res_A['latency']:.4f}s | 生成 {res_A['num_tokens']} Tokens | 吞吐率 {res_A['tps']:.2f} Tokens/s" |
| ) |
| print( |
| f"2. {res_B['name']:<32} : 耗时 {res_B['latency']:.4f}s | 生成 {res_B['num_tokens']} Tokens | 吞吐率 {res_B['tps']:.2f} Tokens/s" |
| ) |
|
|
| tps_diff = ((res_B["tps"] - res_A["tps"]) / res_A["tps"]) * 100 |
|
|
| print("-" * 60) |
| print(f"🚀 每秒生成吞吐率 (TPS) 提升幅度 : {tps_diff:+.2f}%") |
| print("=" * 60) |
|
|