KenteaKe11 commited on
Commit
ba698bf
·
1 Parent(s): 9220821

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +50 -50
README.md CHANGED
@@ -22,41 +22,41 @@ tags:
22
 
23
  # KenT
24
 
25
- KenT là một mô hình ngôn ngữ hồi quy thuộc họ SSM. Thay vì chồng thêm tầng, KenT dồn dung lượng vào **chiều sâu của phép biến đổi bên trong một cell duy nhất**, và giữ phần ghi nhớ theo thời gian ở dạng đơn giản nhất có thể.
26
-
27
- > Repo này chứa mã `trust_remote_code`; trọng số được lưu ở **FP16**.
28
-
29
- ## Triết lý kiến trúc
30
-
31
- - **Một cell, không chồng tầng.** KenT là một cell hồi quy duy nhất, không nhân bản cả mô hình thành nhiều tầng.
32
- Chiều sâu không đến từ số tầng mà đến từ chuỗi khối biến đổi nối tiếp bên trong cell.
33
- - **Đầu tư vào lõi biến đổi.** Dung lượng được dồn vào phần "hidden", nơi đặc trưng thực sự được biến đổi,
34
- thay vì vào bảng từ vựng hay số lần lặp lại kiến trúc. Độ rộng và độ sâu của lõi này được tăng cùng nhau để
35
- giữ cân bằng, không đẩy riêng một chiều.
36
- - **Hai luồng, hai vai trò.** Một luồng biểu diễn mang thông tin của token hiện tại; một luồng bộ nhớ gom
37
- lịch sử. Hai luồng chỉ gặp nhau qua các phép chiếu học được, không trộn trực tiếp.
38
- - **Tần số là nền cố định, học tập ở nơi cần.** Đặc trưng được xử lý trong không gian hệ số cosine bằng một
39
- toán tử **cố định, không học**, xen kẽ với các khối phi tuyến. Phần cố định mang cấu trúc sẵn có; phần học
40
- chỉ tập trung vào việc biến đổi đặc trưng.
41
- - **Ghi nhớ bằng phép tích lũy đơn giản.** Lịch sử được tổng hợp bằng trung bình tích lũy: không có tham số
42
- riêng, không có cơ chế quên ẩn. Trạng thái giải mã có kích thước **không đổi** theo độ dài ngữ cảnh.
43
- - **Hai trục chiều sâu tách biệt.** Độ sâu biến đổi đặc trưng trong một bước và độ dài lịch sử được nhớ là hai
44
- trục độc lập: tăng cái này không làm đổi cái kia.
45
- - **Song song hóa được theo chunk.** Vì bộ nhớ là phép tích lũy kết hợp, một tài liệu dài có thể được chia thành
46
- nhiều chunk, xử lý đồng thời rồi ghép lại, cho cùng kết quả như xử lý tuần tự.
47
-
48
- Các điểm trên là **giả thuyết thiết kế** cần được kiểm chứng bằng thực nghiệm, không phải bảo đảm về hiệu năng.
49
- Chi tiết cấu hình và công thức đầy đủ không được mô tả ở đây.
50
-
51
- ## Cấu trúc repo
52
-
53
- | File | Vai trò |
54
  |---|---|
55
- | `model.py` | Cài đặt mô hình (`KentConfig`, `KentModel`, `KentForCausalLM`, `KentCache`) |
56
- | `config.json` | Cấu hình chuẩn của mô hình |
57
- | `generate_config.json` | Cấu hình sinh mặc định |
58
- | `infer.py` | Nạp prefix theo chunk song song và vòng lặp sinh |
59
- | `model.safetensors` | Trọng số FP16 |
60
 
61
  ## Quickstart
62
 
@@ -64,20 +64,20 @@ Chi tiết cấu hình và công thức đầy đủ không được mô tả
64
  pip install torch transformers
65
  ```
66
 
67
- ### Sinh văn bản chuẩn (`generate`)
68
 
69
  ```python
70
  import torch
71
  from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig
72
 
73
- repo = "Vi-LAB/KenT-100M-Base" # thay bằng repo id hoặc thư mục cục bộ
74
  tok = AutoTokenizer.from_pretrained(repo)
75
  model = AutoModelForCausalLM.from_pretrained(
76
  repo, trust_remote_code=True, torch_dtype=torch.float16
77
  ).to("cuda").eval()
78
  gen_cfg = GenerationConfig.from_pretrained(repo, config_file_name="generate_config.json")
79
 
80
- inputs = tok("Xin chào, hôm nay", return_tensors="pt").to("cuda")
81
  out = model.generate(
82
  input_ids=inputs.input_ids,
83
  attention_mask=inputs.attention_mask,
@@ -86,42 +86,42 @@ out = model.generate(
86
  print(tok.decode(out[0], skip_special_tokens=True))
87
  ```
88
 
89
- Trạng thái hồi quy được trả về dưới dạng `cache_params` (tương tự các mô hình SSM khác trong `transformers`):
90
 
91
  ```python
92
  out = model(**inputs, use_cache=True)
93
- cache = out.cache_params # trạng thái hồi quy
94
  out = model(input_ids=next_token, cache_params=cache, use_cache=True)
95
  ```
96
 
97
- ### Tài liệu dài: nạp prefix theo chunk song song
98
 
99
- Khi xử lý theo batch, mỗi hàng của batch là **một chunk của cùng một tài liệu**; các chunk được xử lý
100
- song song bằng cờ `chunked=True`, trạng thái cuối được trả về trong `cache_params`.
101
 
102
  ```python
103
  from infer import prefill, stream
104
 
105
  ids = tok(open("doc.txt", encoding="utf-8").read(), return_tensors="pt").input_ids[0]
106
 
107
- # 1) chỉ nạp prefix, lấy logits token cuối và trạng thái
108
  logits, cache = prefill(model, ids, chunk_size=1024, max_parallel_chunks=16)
109
 
110
- # 2) nạp prefix rồi sinh (streaming)
111
  for token_id in stream(model, ids, gen_cfg):
112
  print(tok.decode([token_id]), end="", flush=True)
113
  ```
114
 
115
- Dòng lệnh:
116
 
117
  ```bash
118
  python infer.py --model Vi-LAB/KenT-100M-Base --prompt-file doc.txt --chunk-size 1024
119
  ```
120
 
121
- `cache.clone()` cho phép dùng lại cùng một prefix để sinh nhiều nhánh khác nhau mà không phải nạp lại.
122
 
123
- ## Lưu ý
124
 
125
- - Bộ nhớ là trung bình tích lũy đều theo mọi timestep đã thấy; mô hình không tự học cách quên hay chọn lọc.
126
- - Tokenizer và trọng số cần khớp `vocab_size` trong `config.json`.
127
- - Đây là mô hình nghiên cứu; hãy đánh giá trước khi dùng cho tác vụ thực tế.
 
22
 
23
  # KenT
24
 
25
+ KenT is a recurrent language model from the SSM family. Rather than stacking more layers, KenT concentrates its capacity in the **depth of the transformation inside a single cell**, and keeps temporal memory as simple as possible.
26
+
27
+ > This repo contains `trust_remote_code` code; weights are stored in **FP16**.
28
+
29
+ ## Architectural Philosophy
30
+
31
+ - **One cell, no layer stacking.** KenT is a single recurrent cell; the model is not replicated into multiple layers.
32
+ Depth comes not from the number of layers but from a chain of transformation blocks applied in sequence inside the cell.
33
+ - **Invest in the transformation core.** Capacity is concentrated in the "hidden" part, where features are actually transformed,
34
+ rather than in the vocabulary table or in repeating the architecture. The width and depth of this core are scaled together to
35
+ stay balanced, rather than pushing a single dimension.
36
+ - **Two streams, two roles.** One stream is a representation carrying information about the current token; the other is a memory
37
+ stream that aggregates history. The two streams meet only through learned projections and are never mixed directly.
38
+ - **Frequency as a fixed foundation, learning where it's needed.** Features are processed in the cosine-coefficient space using a
39
+ **fixed, non-learned** operator, interleaved with nonlinear blocks. The fixed part carries built-in structure; the learned part
40
+ focuses solely on transforming features.
41
+ - **Memory via simple accumulation.** History is summarized with a cumulative average: no dedicated parameters and no implicit
42
+ forgetting mechanism. The decoding state has **constant** size regardless of context length.
43
+ - **Two separate depth axes.** The depth of feature transformation within a single step and the length of history remembered are
44
+ two independent axes: increasing one does not change the other.
45
+ - **Chunk-parallelizable.** Because memory is an associative accumulation, a long document can be split into multiple chunks,
46
+ processed concurrently, and then merged, yielding the same result as sequential processing.
47
+
48
+ The points above are **design hypotheses** that need to be verified experimentally, not performance guarantees.
49
+ Detailed configuration and full formulas are not described here.
50
+
51
+ ## Repo Structure
52
+
53
+ | File | Role |
54
  |---|---|
55
+ | `model.py` | Model implementation (`KentConfig`, `KentModel`, `KentForCausalLM`, `KentCache`) |
56
+ | `config.json` | Standard model configuration |
57
+ | `generate_config.json` | Default generation configuration |
58
+ | `infer.py` | Parallel chunked prefix loading and the generation loop |
59
+ | `model.safetensors` | FP16 weights |
60
 
61
  ## Quickstart
62
 
 
64
  pip install torch transformers
65
  ```
66
 
67
+ ### Standard text generation (`generate`)
68
 
69
  ```python
70
  import torch
71
  from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig
72
 
73
+ repo = "Vi-LAB/KenT-100M-Base" # replace with a repo id or a local directory
74
  tok = AutoTokenizer.from_pretrained(repo)
75
  model = AutoModelForCausalLM.from_pretrained(
76
  repo, trust_remote_code=True, torch_dtype=torch.float16
77
  ).to("cuda").eval()
78
  gen_cfg = GenerationConfig.from_pretrained(repo, config_file_name="generate_config.json")
79
 
80
+ inputs = tok("Hello, today", return_tensors="pt").to("cuda")
81
  out = model.generate(
82
  input_ids=inputs.input_ids,
83
  attention_mask=inputs.attention_mask,
 
86
  print(tok.decode(out[0], skip_special_tokens=True))
87
  ```
88
 
89
+ The recurrent state is returned as `cache_params` (similar to other SSM models in `transformers`):
90
 
91
  ```python
92
  out = model(**inputs, use_cache=True)
93
+ cache = out.cache_params # recurrent state
94
  out = model(input_ids=next_token, cache_params=cache, use_cache=True)
95
  ```
96
 
97
+ ### Long documents: parallel chunked prefix loading
98
 
99
+ When processing in batches, each row of the batch is **one chunk of the same document**; the chunks are processed
100
+ in parallel with the `chunked=True` flag, and the final state is returned in `cache_params`.
101
 
102
  ```python
103
  from infer import prefill, stream
104
 
105
  ids = tok(open("doc.txt", encoding="utf-8").read(), return_tensors="pt").input_ids[0]
106
 
107
+ # 1) load the prefix only, get the last token's logits and the state
108
  logits, cache = prefill(model, ids, chunk_size=1024, max_parallel_chunks=16)
109
 
110
+ # 2) load the prefix, then generate (streaming)
111
  for token_id in stream(model, ids, gen_cfg):
112
  print(tok.decode([token_id]), end="", flush=True)
113
  ```
114
 
115
+ Command line:
116
 
117
  ```bash
118
  python infer.py --model Vi-LAB/KenT-100M-Base --prompt-file doc.txt --chunk-size 1024
119
  ```
120
 
121
+ `cache.clone()` lets you reuse the same prefix to generate multiple different branches without reloading it.
122
 
123
+ ## Notes
124
 
125
+ - Memory is a uniform cumulative average over every timestep seen; the model does not learn to forget or to select on its own.
126
+ - The tokenizer and weights must match `vocab_size` in `config.json`.
127
+ - This is a research model; please evaluate it before using it for real-world tasks.