Spaces:
Running
Running
zhan1206 commited on
Commit ·
5e7eab2
1
Parent(s): 4bf76ff
fix: audit v2 P0/P1 fixes (D1/D2/D3/D7/D9/D11/D14)
Browse files- D1: inference/dyquant.py load_model() 优先 FusionMini/FusionModel,回退 AutoModelForCausalLM 时警告
- D2: train/train_grpo.py fallback 到 AutoModelForCausalLM 时显式警告 Thinking Dial 不可用
- D3: requirements.txt 添加 gradio>=4.0.0(inference/dashboard.py 依赖)
- D7: 创建 data_pipeline/__init__.py(setup.py 声明为包)
- D9: evaluation/model_interpretability.py hash() 替换为 hashlib.md5()(确定性跨会话)
- D11: train/lora_finetune.py main() 添加 CUDA 可用性检查
- D14: evaluation/visualization.py save_visualization_report() stdout 劫持改用 try/finally 保护
25/25 测试通过
- data_pipeline/__init__.py +2 -0
- evaluation/model_interpretability.py +3 -2
- evaluation/visualization.py +5 -4
- inference/dyquant.py +29 -7
- requirements.txt +1 -0
- train/lora_finetune.py +9 -1
- train/train_grpo.py +8 -0
data_pipeline/__init__.py
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# data_pipeline package
|
| 2 |
+
# 数据管道模块:T-KD 蒸馏训练、双语过滤等
|
evaluation/model_interpretability.py
CHANGED
|
@@ -9,6 +9,7 @@ SHAP: SHapley Additive exPlanations
|
|
| 9 |
"""
|
| 10 |
import sys
|
| 11 |
import math
|
|
|
|
| 12 |
import torch
|
| 13 |
import numpy as np
|
| 14 |
from pathlib import Path
|
|
@@ -86,7 +87,7 @@ class SimplifiedLIME:
|
|
| 86 |
input_ids = []
|
| 87 |
for t in tokens:
|
| 88 |
# 简单 hash 到 vocab 范围
|
| 89 |
-
token_id =
|
| 90 |
input_ids.append(token_id)
|
| 91 |
|
| 92 |
input_ids = torch.tensor([input_ids])
|
|
@@ -188,7 +189,7 @@ class SimplifiedSHAP:
|
|
| 188 |
|
| 189 |
input_ids = []
|
| 190 |
for t in tokens:
|
| 191 |
-
token_id =
|
| 192 |
input_ids.append(token_id)
|
| 193 |
|
| 194 |
input_ids = torch.tensor([input_ids])
|
|
|
|
| 9 |
"""
|
| 10 |
import sys
|
| 11 |
import math
|
| 12 |
+
import hashlib
|
| 13 |
import torch
|
| 14 |
import numpy as np
|
| 15 |
from pathlib import Path
|
|
|
|
| 87 |
input_ids = []
|
| 88 |
for t in tokens:
|
| 89 |
# 简单 hash 到 vocab 范围
|
| 90 |
+
token_id = int(hashlib.md5(t.encode()).hexdigest(), 16) % vocab_size
|
| 91 |
input_ids.append(token_id)
|
| 92 |
|
| 93 |
input_ids = torch.tensor([input_ids])
|
|
|
|
| 189 |
|
| 190 |
input_ids = []
|
| 191 |
for t in tokens:
|
| 192 |
+
token_id = int(hashlib.md5(t.encode()).hexdigest(), 16) % vocab_size
|
| 193 |
input_ids.append(token_id)
|
| 194 |
|
| 195 |
input_ids = torch.tensor([input_ids])
|
evaluation/visualization.py
CHANGED
|
@@ -162,8 +162,8 @@ def save_visualization_report(model, attention_weights, losses, output_path):
|
|
| 162 |
import sys
|
| 163 |
original_stdout = sys.stdout
|
| 164 |
sys.stdout = f
|
| 165 |
-
|
| 166 |
-
|
| 167 |
print("Fusion-LLM 可视化报告")
|
| 168 |
print("=" * 60)
|
| 169 |
print()
|
|
@@ -184,8 +184,9 @@ def save_visualization_report(model, attention_weights, losses, output_path):
|
|
| 184 |
print("报告结束")
|
| 185 |
print("=" * 60)
|
| 186 |
|
| 187 |
-
|
| 188 |
-
|
|
|
|
| 189 |
|
| 190 |
print(f" 报告已保存到: {output_path}")
|
| 191 |
print()
|
|
|
|
| 162 |
import sys
|
| 163 |
original_stdout = sys.stdout
|
| 164 |
sys.stdout = f
|
| 165 |
+
try:
|
| 166 |
+
print("=" * 60)
|
| 167 |
print("Fusion-LLM 可视化报告")
|
| 168 |
print("=" * 60)
|
| 169 |
print()
|
|
|
|
| 184 |
print("报告结束")
|
| 185 |
print("=" * 60)
|
| 186 |
|
| 187 |
+
finally:
|
| 188 |
+
# 恢复 stdout
|
| 189 |
+
sys.stdout = original_stdout
|
| 190 |
|
| 191 |
print(f" 报告已保存到: {output_path}")
|
| 192 |
print()
|
inference/dyquant.py
CHANGED
|
@@ -93,24 +93,46 @@ class DyQuantConverter:
|
|
| 93 |
print(f" 按头量化:{config.per_head}")
|
| 94 |
|
| 95 |
def load_model(self):
|
| 96 |
-
"""加载模型"""
|
| 97 |
print(f"\n[DyQuant] 加载模型:{self.config.model_path}")
|
| 98 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
try:
|
| 100 |
from transformers import AutoModelForCausalLM
|
| 101 |
-
|
| 102 |
-
# 加载真实模型
|
| 103 |
self.model = AutoModelForCausalLM.from_pretrained(
|
| 104 |
self.config.model_path,
|
| 105 |
torch_dtype=torch.bfloat16,
|
| 106 |
-
device_map="cpu",
|
| 107 |
trust_remote_code=True,
|
| 108 |
)
|
| 109 |
self.model.eval()
|
| 110 |
-
print(f"[DyQuant]
|
|
|
|
| 111 |
return self.model
|
| 112 |
-
except Exception as
|
| 113 |
-
print(f"[DyQuant] 模型加载失败:{
|
| 114 |
import traceback; traceback.print_exc()
|
| 115 |
self.model = None
|
| 116 |
return None
|
|
|
|
| 93 |
print(f" 按头量化:{config.per_head}")
|
| 94 |
|
| 95 |
def load_model(self):
|
| 96 |
+
"""加载模型(优先 FusionModel/FusionMini,回退 AutoModelForCausalLM)"""
|
| 97 |
print(f"\n[DyQuant] 加载模型:{self.config.model_path}")
|
| 98 |
|
| 99 |
+
self.model = None
|
| 100 |
+
|
| 101 |
+
# Try FusionMini first
|
| 102 |
+
try:
|
| 103 |
+
from models.fusion_mini import FusionMini
|
| 104 |
+
self.model = FusionMini._load_from_safetensors(self.config.model_path)
|
| 105 |
+
self.model.eval()
|
| 106 |
+
print(f"[DyQuant] FusionMini 加载成功")
|
| 107 |
+
return self.model
|
| 108 |
+
except Exception as e1:
|
| 109 |
+
pass # fallback
|
| 110 |
+
|
| 111 |
+
# Try FusionModel
|
| 112 |
+
try:
|
| 113 |
+
from models.fusion_model import FusionModel
|
| 114 |
+
self.model = FusionModel.from_pretrained(self.config.model_path)
|
| 115 |
+
self.model.eval()
|
| 116 |
+
print(f"[DyQuant] FusionModel 加载成功")
|
| 117 |
+
return self.model
|
| 118 |
+
except Exception as e2:
|
| 119 |
+
pass # fallback
|
| 120 |
+
|
| 121 |
+
# Fallback to AutoModelForCausalLM
|
| 122 |
try:
|
| 123 |
from transformers import AutoModelForCausalLM
|
|
|
|
|
|
|
| 124 |
self.model = AutoModelForCausalLM.from_pretrained(
|
| 125 |
self.config.model_path,
|
| 126 |
torch_dtype=torch.bfloat16,
|
| 127 |
+
device_map="cpu",
|
| 128 |
trust_remote_code=True,
|
| 129 |
)
|
| 130 |
self.model.eval()
|
| 131 |
+
print(f"[DyQuant] AutoModelForCausalLM 加载成功(回退模式)")
|
| 132 |
+
print(f"[DyQuant] 警告:非 Fusion 模型,SBLA/ThinkingDial 量化路径未验证")
|
| 133 |
return self.model
|
| 134 |
+
except Exception as e3:
|
| 135 |
+
print(f"[DyQuant] 模型加载失败:{e3}")
|
| 136 |
import traceback; traceback.print_exc()
|
| 137 |
self.model = None
|
| 138 |
return None
|
requirements.txt
CHANGED
|
@@ -40,6 +40,7 @@ rouge-score>=0.1.2
|
|
| 40 |
# 可视化(可选)
|
| 41 |
tensorboard>=2.15.0
|
| 42 |
wandb>=0.16.0
|
|
|
|
| 43 |
|
| 44 |
|
| 45 |
# ONNX 导出
|
|
|
|
| 40 |
# 可视化(可选)
|
| 41 |
tensorboard>=2.15.0
|
| 42 |
wandb>=0.16.0
|
| 43 |
+
gradio>=4.0.0 # inference/dashboard.py 依赖
|
| 44 |
|
| 45 |
|
| 46 |
# ONNX 导出
|
train/lora_finetune.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
|
| 2 |
Fusion 模型 LoRA/QLoRA 微调脚本
|
| 3 |
|
| 4 |
支持:
|
|
@@ -367,6 +367,14 @@ def train(args):
|
|
| 367 |
|
| 368 |
|
| 369 |
def main():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 370 |
parser = argparse.ArgumentParser(description="Fusion 模型 LoRA/QLoRA 微调")
|
| 371 |
|
| 372 |
# 模型参数
|
|
|
|
| 1 |
+
"""
|
| 2 |
Fusion 模型 LoRA/QLoRA 微调脚本
|
| 3 |
|
| 4 |
支持:
|
|
|
|
| 367 |
|
| 368 |
|
| 369 |
def main():
|
| 370 |
+
"""Main entry point with CUDA availability check."""
|
| 371 |
+
# Check CUDA availability
|
| 372 |
+
if not torch.cuda.is_available():
|
| 373 |
+
print("[ERROR] CUDA is not available. This training script requires a GPU.")
|
| 374 |
+
print("[INFO] For CPU training, use train_mini.py (FusionMini).")
|
| 375 |
+
sys.exit(1)
|
| 376 |
+
print(f"[INFO] CUDA available: {torch.cuda.get_device_name(0)}")
|
| 377 |
+
|
| 378 |
parser = argparse.ArgumentParser(description="Fusion 模型 LoRA/QLoRA 微调")
|
| 379 |
|
| 380 |
# 模型参数
|
train/train_grpo.py
CHANGED
|
@@ -85,6 +85,14 @@ def load_fusion_model(model_path: str, device: str = "auto"):
|
|
| 85 |
|
| 86 |
# Fallback to HF AutoModel
|
| 87 |
if model is None:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
from transformers import AutoModelForCausalLM
|
| 89 |
model = AutoModelForCausalLM.from_pretrained(model_path, trust_remote_code=True)
|
| 90 |
config = model.config
|
|
|
|
| 85 |
|
| 86 |
# Fallback to HF AutoModel
|
| 87 |
if model is None:
|
| 88 |
+
import warnings
|
| 89 |
+
warnings.warn(
|
| 90 |
+
"无法加载 FusionModel/FusionMini,回退到 AutoModelForCausalLM。\n"
|
| 91 |
+
"Thinking Dial(推理深度控制)在此模式下不可用。\n"
|
| 92 |
+
"建议确认模型路径指向有效的 Fusion 模型。",
|
| 93 |
+
UserWarning
|
| 94 |
+
)
|
| 95 |
+
logger.warning("[WARNING] 回退到 AutoModelForCausalLM,Thinking Dial 将不可用!")
|
| 96 |
from transformers import AutoModelForCausalLM
|
| 97 |
model = AutoModelForCausalLM.from_pretrained(model_path, trust_remote_code=True)
|
| 98 |
config = model.config
|