zhan1206 commited on
Commit
336bb73
·
1 Parent(s): 36e31c4

fix(v9): resolve 3 fatal + 1 serious + 1 moderate defects from external audit

Browse files

FATAL:
- F1: lora_finetune create_local_model signature missing vocab_size_override
(was referenced in body but not declared, caused TypeError at runtime)
- F2: full_finetune uses Optional[int] but 'from typing import Optional' was missing
- F3: both JSON configs had sbla_mode='mixed' (removed in v6), fixed to 'hybrid'

SERIOUS:
- S1: run_tests.py four broken calls:
* SlidingBlockLatentAttention -> SBLAttention (class renamed)
* d_model/n_heads -> hidden_size/num_heads (param names changed)
* processor.parse_thinking_depth -> parse_think_token (function moved)
* processor.inject_thinking_token -> apply_thinking_control

MODERATE:
- M1: get_effective_vocab_size hardcoded 50257+5, now loads actual tokenizer

MINOR:
- N1: fusion-mini-config hidden_act 'gelu' -> 'silu' (code uses silu)

Ref: 36e31c4 audit report

configs/fusion-8b-config.json CHANGED
@@ -19,7 +19,7 @@
19
  "block_size": 512,
20
  "latent_dim": 64,
21
  "window_size": 2048,
22
- "sbla_mode": "mixed",
23
 
24
  "rms_norm_eps": 1e-6,
25
  "rope_theta": 10000.0,
 
19
  "block_size": 512,
20
  "latent_dim": 64,
21
  "window_size": 2048,
22
+ "sbla_mode": "hybrid",
23
 
24
  "rms_norm_eps": 1e-6,
25
  "rope_theta": 10000.0,
configs/fusion-mini-config.json CHANGED
@@ -9,7 +9,7 @@
9
  "num_attention_heads": 4,
10
  "num_key_value_heads": 4,
11
  "intermediate_size": 512,
12
- "hidden_act": "gelu",
13
  "hidden_dropout_prob": 0.1,
14
  "attention_probs_dropout_prob": 0.1,
15
  "max_position_embeddings": 256,
@@ -19,7 +19,7 @@
19
  "block_size": 32,
20
  "latent_dim": 16,
21
  "window_size": 128,
22
- "sbla_mode": "mixed",
23
 
24
  "rms_norm_eps": 1e-5,
25
  "rope_theta": 10000.0,
 
9
  "num_attention_heads": 4,
10
  "num_key_value_heads": 4,
11
  "intermediate_size": 512,
12
+ "hidden_act": "silu",
13
  "hidden_dropout_prob": 0.1,
14
  "attention_probs_dropout_prob": 0.1,
15
  "max_position_embeddings": 256,
 
19
  "block_size": 32,
20
  "latent_dim": 16,
21
  "window_size": 128,
22
+ "sbla_mode": "hybrid",
23
 
24
  "rms_norm_eps": 1e-5,
25
  "rope_theta": 10000.0,
models/tokenizer.py CHANGED
@@ -125,11 +125,12 @@ def get_effective_vocab_size(tokenizer_type: str = "gpt2", requested_vocab: int
125
  Return the effective vocab size that should be used in model config.
126
  This ensures model embedding size matches the actual tokenizer.
127
  """
128
- if tokenizer_type == "gpt2":
129
- return 50257 + len(FUSION_SPECIAL_TOKENS["think_tokens"]) + 1 # ~50262
130
- if tokenizer_type == "fusion":
 
 
131
  return requested_vocab
132
- return requested_vocab
133
 
134
 
135
  if __name__ == "__main__":
 
125
  Return the effective vocab size that should be used in model config.
126
  This ensures model embedding size matches the actual tokenizer.
127
  """
128
+ try:
129
+ tok = get_tokenizer(tokenizer_type)
130
+ return len(tok)
131
+ except Exception:
132
+ # Fallback to requested vocab on error
133
  return requested_vocab
 
134
 
135
 
136
  if __name__ == "__main__":
tests/run_tests.py CHANGED
@@ -33,39 +33,46 @@ class TestSBLAAttention(unittest.TestCase):
33
 
34
  def test_forward_pass(self):
35
  """测试前向传播"""
36
- from models.sbla_attention import SlidingBlockLatentAttention
37
 
38
  batch_size = 2
39
  seq_len = 1024
40
- d_model = 512
41
- n_heads = 8
42
 
43
- attn = SlidingBlockLatentAttention(
44
- d_model=d_model,
45
- n_heads=n_heads,
46
  block_size=512,
47
  latent_dim=64,
 
 
48
  )
49
 
50
- x = torch.randn(batch_size, seq_len, d_model)
51
- output = attn(x)
 
52
 
53
- self.assertEqual(output.shape, (batch_size, seq_len, d_model))
54
  print("✅ SBLA 前向传播测试通过")
55
 
56
  def test_long_sequence(self):
57
  """测试长序列处理"""
58
- from models.sbla_attention import SlidingBlockLatentAttention
59
 
60
- attn = SlidingBlockLatentAttention(
61
- d_model=256,
62
- n_heads=4,
63
  block_size=256,
 
 
 
64
  )
65
 
66
  # 测试 8K 序列
67
  x = torch.randn(1, 8192, 256)
68
- output = attn(x)
 
69
 
70
  self.assertEqual(output.shape, (1, 8192, 256))
71
  print("✅ SBLA 长序列测试通过")
@@ -76,17 +83,10 @@ class TestThinkingDial(unittest.TestCase):
76
 
77
  def test_parse_depth(self):
78
  """测试解析推理深度"""
79
- from models.thinking_dial import ThinkingDialProcessor
80
-
81
- # 模拟 tokenizer
82
- class MockTokenizer:
83
- def add_special_tokens(self, tokens):
84
- pass
85
-
86
- processor = ThinkingDialProcessor(MockTokenizer())
87
 
88
  # 测试解析
89
- depth, clean = processor.parse_thinking_depth(
90
  "<|think_depth_2|> 证明勾股定理"
91
  )
92
 
@@ -96,15 +96,9 @@ class TestThinkingDial(unittest.TestCase):
96
 
97
  def test_inject_token(self):
98
  """测试注入控制 token"""
99
- from models.thinking_dial import ThinkingDialProcessor
100
-
101
- class MockTokenizer:
102
- def add_special_tokens(self, tokens):
103
- pass
104
-
105
- processor = ThinkingDialProcessor(MockTokenizer())
106
 
107
- result = processor.inject_thinking_token(
108
  "解释量子纠缠",
109
  depth=1,
110
  )
 
33
 
34
  def test_forward_pass(self):
35
  """测试前向传播"""
36
+ from models.sbla_attention import SBLAttention
37
 
38
  batch_size = 2
39
  seq_len = 1024
40
+ hidden_size = 512
41
+ num_heads = 8
42
 
43
+ attn = SBLAttention(
44
+ hidden_size=hidden_size,
45
+ num_heads=num_heads,
46
  block_size=512,
47
  latent_dim=64,
48
+ window_size=1024,
49
+ mode="pure_sbla",
50
  )
51
 
52
+ x = torch.randn(batch_size, seq_len, hidden_size)
53
+ attention_mask = torch.ones(batch_size, seq_len)
54
+ output, _ = attn(hidden_states=x, attention_mask=attention_mask)
55
 
56
+ self.assertEqual(output.shape, (batch_size, seq_len, hidden_size))
57
  print("✅ SBLA 前向传播测试通过")
58
 
59
  def test_long_sequence(self):
60
  """测试长序列处理"""
61
+ from models.sbla_attention import SBLAttention
62
 
63
+ attn = SBLAttention(
64
+ hidden_size=256,
65
+ num_heads=4,
66
  block_size=256,
67
+ latent_dim=32,
68
+ window_size=512,
69
+ mode="pure_sbla",
70
  )
71
 
72
  # 测试 8K 序列
73
  x = torch.randn(1, 8192, 256)
74
+ attention_mask = torch.ones(1, 8192)
75
+ output, _ = attn(hidden_states=x, attention_mask=attention_mask)
76
 
77
  self.assertEqual(output.shape, (1, 8192, 256))
78
  print("✅ SBLA 长序列测试通过")
 
83
 
84
  def test_parse_depth(self):
85
  """测试解析推理深度"""
86
+ from models.thinking_dial import parse_think_token
 
 
 
 
 
 
 
87
 
88
  # 测试解析
89
+ depth, clean = parse_think_token(
90
  "<|think_depth_2|> 证明勾股定理"
91
  )
92
 
 
96
 
97
  def test_inject_token(self):
98
  """测试注入控制 token"""
99
+ from models.thinking_dial import apply_thinking_control
 
 
 
 
 
 
100
 
101
+ result = apply_thinking_control(
102
  "解释量子纠缠",
103
  depth=1,
104
  )
train/full_finetune.py CHANGED
@@ -22,6 +22,7 @@ Fusion 模型全参数微调脚本
22
 
23
  import argparse
24
  import torch
 
25
  import torch.nn as nn
26
  import deepspeed
27
  from transformers import (
 
22
 
23
  import argparse
24
  import torch
25
+ from typing import Optional
26
  import torch.nn as nn
27
  import deepspeed
28
  from transformers import (
train/lora_finetune.py CHANGED
@@ -130,8 +130,8 @@ def create_local_model(
130
  quantize: bool = False,
131
  load_in_4bit: bool = False,
132
  load_in_8bit: bool = False,
 
133
  ):
134
- """
135
  """
136
  创建本地 FusionModel(无需预训练权重)
137
 
 
130
  quantize: bool = False,
131
  load_in_4bit: bool = False,
132
  load_in_8bit: bool = False,
133
+ vocab_size_override: int | None = None,
134
  ):
 
135
  """
136
  创建本地 FusionModel(无需预训练权重)
137