KenteaKe11 commited on
Commit
ee9e9d9
·
verified ·
1 Parent(s): fb17feb

Upload 4 files

Browse files
Files changed (4) hide show
  1. README.md +2 -8
  2. config.json +1 -1
  3. infer.py +2 -2
  4. model.py +4 -3
README.md CHANGED
@@ -10,10 +10,6 @@ tags:
10
  - dct
11
  - swiglu
12
  - custom_code
13
- - base
14
- datasets:
15
- - HuggingFaceFW/fineweb-edu
16
- - wikimedia/wikipedia
17
  ---
18
 
19
  # KenT-300M-Base
@@ -29,7 +25,7 @@ H_i[t+1] = (1 - A_i) ⊙ H_i[t] + A_i ⊙ u_i # Learnable EMA Matrix, A = si
29
 
30
  S = Σ_i H_i[t+1] # Add
31
  y = S · D # "DCT-II Proj (D)" (DCT ngược)
32
- out = y + SwiGLU(RMSNorm(y))
33
  logits = LM_head(RMSNorm_f(out)) # tied với embedding
34
  ```
35
 
@@ -48,12 +44,10 @@ D là ma trận DCT-II trực chuẩn cố định (hàng = tần số, vector h
48
  EMA khởi tạo theo cấp số nhân từ 0.5 (tap đầu, feature đầu) đến 0.01 (tap cuối, feature cuối); vẫn học được.
49
  EMA chạy FP32 bằng affine-scan log-depth, kết quả bằng đúng hồi quy tuần tự.
50
 
51
- ![KenT diagram](KenT.png)
52
-
53
  ```python
54
  from transformers import AutoModelForCausalLM, AutoTokenizer
55
  tok = AutoTokenizer.from_pretrained("Vi-LAB/KenT-300M-Base", trust_remote_code=True)
56
  model = AutoModelForCausalLM.from_pretrained("Vi-LAB/KenT-300M-Base", trust_remote_code=True)
57
  ```
58
 
59
- Hoặc `python infer.py --model Vi-LAB/KenT-300M-Base --prompt "Today the weather is"`.
 
10
  - dct
11
  - swiglu
12
  - custom_code
 
 
 
 
13
  ---
14
 
15
  # KenT-300M-Base
 
25
 
26
  S = Σ_i H_i[t+1] # Add
27
  y = S · D # "DCT-II Proj (D)" (DCT ngược)
28
+ out = x[t] + y + SwiGLU(RMSNorm(y)) # 2 skip: sau phép chiếu D và từ đầu vào gốc x[t]
29
  logits = LM_head(RMSNorm_f(out)) # tied với embedding
30
  ```
31
 
 
44
  EMA khởi tạo theo cấp số nhân từ 0.5 (tap đầu, feature đầu) đến 0.01 (tap cuối, feature cuối); vẫn học được.
45
  EMA chạy FP32 bằng affine-scan log-depth, kết quả bằng đúng hồi quy tuần tự.
46
 
 
 
47
  ```python
48
  from transformers import AutoModelForCausalLM, AutoTokenizer
49
  tok = AutoTokenizer.from_pretrained("Vi-LAB/KenT-300M-Base", trust_remote_code=True)
50
  model = AutoModelForCausalLM.from_pretrained("Vi-LAB/KenT-300M-Base", trust_remote_code=True)
51
  ```
52
 
53
+ Hoặc `python infer.py --model Vi-LAB/KenT-300M-Base --prompt "Today the weather is"`.
config.json CHANGED
@@ -19,7 +19,7 @@
19
  "pad_token_id": 50256,
20
  "bos_token_id": 50256,
21
  "eos_token_id": 50256,
22
- "dtype": "float16",
23
  "ema_alpha_first": 0.5,
24
  "ema_alpha_last": 0.01,
25
  "use_gradient_checkpointing": false
 
19
  "pad_token_id": 50256,
20
  "bos_token_id": 50256,
21
  "eos_token_id": 50256,
22
+ "dtype": "bfloat16",
23
  "ema_alpha_first": 0.5,
24
  "ema_alpha_last": 0.01,
25
  "use_gradient_checkpointing": false
infer.py CHANGED
@@ -28,8 +28,8 @@ from transformers import AutoModelForCausalLM, AutoTokenizer, GenerationConfig
28
  # --------------------------------------------------------------------------- #
29
  def load(model_id: str, device: Optional[str] = None, dtype: Optional[torch.dtype] = None):
30
  device = device or ("cuda" if torch.cuda.is_available() else "cpu")
31
- if dtype is None: # FP16 trên GPU; CPU dùng FP32 vì nhiều phép FP16 chậm/không hỗ trợ
32
- dtype = torch.float16 if device.startswith("cuda") else torch.float32
33
  tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
34
  model = AutoModelForCausalLM.from_pretrained(model_id, trust_remote_code=True)
35
  model = model.to(device=device, dtype=dtype).eval()
 
28
  # --------------------------------------------------------------------------- #
29
  def load(model_id: str, device: Optional[str] = None, dtype: Optional[torch.dtype] = None):
30
  device = device or ("cuda" if torch.cuda.is_available() else "cpu")
31
+ if dtype is None: # BF16 trên GPU; CPU dùng FP32 vì nhiều phép FP16 chậm/không hỗ trợ
32
+ dtype = torch.bfloat16 if device.startswith("cuda") else torch.float32
33
  tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
34
  model = AutoModelForCausalLM.from_pretrained(model_id, trust_remote_code=True)
35
  model = model.to(device=device, dtype=dtype).eval()
model.py CHANGED
@@ -11,7 +11,7 @@ Mỗi bước nhận ĐÚNG MỘT token x[t] (vector 1 x D); lịch sử nằm h
11
 
12
  S = sum_i H_i[t+1] # Add
13
  y = S · D # "DCT-II Proj (D)": DCT ngược (D trực chuẩn, D^T D = I)
14
- out = y + SwiGLU( RMSNorm(y) ) # RMSNorm -> SwiGLU -> Add (skip lấy sau phép chiếu D)
15
  logits = LM_head( RMSNorm_f(out) ) # head buộc chung trọng số với embedding
16
 
17
  Quy ước hàng: x là vector hàng nên DCT-II là x·D^T (D[k, i] = α_k cos(π(i+½)k/D), hàng = tần số k, cột = vị trí i)
@@ -22,7 +22,7 @@ bằng DCT ngược.
22
  toàn mô hình chỉ là H shape (n+1, D): O(1) theo độ dài chuỗi.
23
 
24
  EMA là hồi quy tuyến tính theo từng kênh nên song song hóa được theo thời gian bằng affine-scan log-depth
25
- (Hillis-Steele): kết quả bằng đúng hồi quy tuần tự. EMA và RMSNorm chạy ở FP32; trọng số lưu ở FP16.
26
 
27
  RMSNorm cuối (`norm_f`) trước LM head không có trong sơ đồ; thêm vào để logits ổn định.
28
  """
@@ -417,7 +417,8 @@ class KentModel(KentPreTrainedModel):
417
  # --- Add -> DCT-II Proj (D) -> RMSNorm -> SwiGLU -> Add
418
  s = h.sum(dim=2).to(feats[0].dtype)
419
  y = idct2(s)
420
- y = y + self.out_mlp(self.out_norm(y))
 
421
  y = self.norm_f(y) # (1, K*C, D) nếu chunked
422
  if chunked:
423
  y = y.reshape(1, b * t, d)
 
11
 
12
  S = sum_i H_i[t+1] # Add
13
  y = S · D # "DCT-II Proj (D)": DCT ngược (D trực chuẩn, D^T D = I)
14
+ out = x[t] + y + SwiGLU( RMSNorm(y) ) # RMSNorm -> SwiGLU -> Add: skip lấy sau phép chiếu D VÀ skip từ đầu vào gốc x[t]
15
  logits = LM_head( RMSNorm_f(out) ) # head buộc chung trọng số với embedding
16
 
17
  Quy ước hàng: x là vector hàng nên DCT-II là x·D^T (D[k, i] = α_k cos(π(i+½)k/D), hàng = tần số k, cột = vị trí i)
 
22
  toàn mô hình chỉ là H shape (n+1, D): O(1) theo độ dài chuỗi.
23
 
24
  EMA là hồi quy tuyến tính theo từng kênh nên song song hóa được theo thời gian bằng affine-scan log-depth
25
+ (Hillis-Steele): kết quả bằng đúng hồi quy tuần tự. EMA và RMSNorm chạy ở FP32; trọng số lưu ở BF16.
26
 
27
  RMSNorm cuối (`norm_f`) trước LM head không có trong sơ đồ; thêm vào để logits ổn định.
28
  """
 
417
  # --- Add -> DCT-II Proj (D) -> RMSNorm -> SwiGLU -> Add
418
  s = h.sum(dim=2).to(feats[0].dtype)
419
  y = idct2(s)
420
+ x0 = feats[0].reshape(1, b * t, d) if chunked else feats[0] # đầu vào gốc x[t] (embedding)
421
+ y = x0 + y + self.out_mlp(self.out_norm(y)) # Add: skip sau D + skip đầu vào gốc
422
  y = self.norm_f(y) # (1, K*C, D) nếu chunked
423
  if chunked:
424
  y = y.reshape(1, b * t, d)