FlameF0X commited on
Commit
b9968c9
·
verified ·
1 Parent(s): f662a13

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +151 -42
README.md CHANGED
@@ -2,82 +2,191 @@
2
  language: en
3
  license: apache-2.0
4
  library_name: transformers
 
5
  tags:
6
  - fwkv
7
  - rosa
 
8
  - linear-transformer
9
  - recurrent-neural-network
10
  - chat
11
  - ultrachat
12
- - pytorch
13
  ---
14
 
15
  # FWKV-ROSA
16
 
17
- A **56M‑parameter**, from‑scratch recurrent language model combining a **per‑channel leaky integrator (FWKV)** with the **RWKV‑8 ROSA copy‑signal** mechanism.
18
- Chattuned on `HuggingFaceH4/ultrachat_200k` with a simple tworole template.
 
 
19
 
20
- ## Model description
 
21
 
22
- - **Architecture:** 14‑layer FWKV blocks with a factorised tied embedding/head (512‑dim model, 128‑dim embedding) and an exact vectorised parallel scan for the constant‑decay recurrence.
23
- - **ROSA (Rapid Online Suffix Automaton):** A parameter‑free, causal predictor that injects the token that historically followed the longest matching suffix of the current context. Implemented as an embedding added to the early hidden state.
24
- - **Training data:** [UltraChat 200k](https://huggingface.co/datasets/HuggingFaceH4/ultrachat_200k) (`train_sft` / `test_sft` splits), 20k training examples, 1k validation examples.
25
- - **Context length:** 1024 tokens (no positional embeddings – context is free parameter‑wise).
26
- - **Tokenizer:** GPT‑2 tokenizer extended with `<|user|>` and `<|assistant|>` special tokens.
27
- - **Training precision:** bfloat16 mixed precision on a single NVIDIA T4 (batch size 8, gradient accumulation 4, effective batch 32). Training took ~2 hours.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
- ## Performance
 
 
 
 
 
30
 
31
- | Metric | Value |
32
- |--------|-------|
33
- | **Validation perplexity** | 67.78 |
34
- | **Validation loss** | 4.216 |
35
 
36
- *Note:* With only 56M parameters and a fairly simple linear recurrence, the model is intended as a research experiment. It can generate coherent multi‑turn conversations but may occasionally produce repetitive or factual errors.
 
 
 
 
 
37
 
38
- ## How to use
39
 
40
- ### Load directly with Transformers
 
41
 
42
  ```python
43
- import torch
44
  from transformers import AutoTokenizer, AutoModelForCausalLM
45
 
46
- model = AutoModelForCausalLM.from_pretrained("FlameF0X/FWKV-ROSA", trust_remote_code=True)
 
 
 
47
  tokenizer = AutoTokenizer.from_pretrained("FlameF0X/FWKV-ROSA")
48
  ```
49
 
50
- **Chat template:**
51
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  ```
53
- <|user|> Your prompt here
54
- <|assistant|> The model reply <eos>
55
- ```
56
 
57
- Make sure to use the special tokens as shown above. The model was trained to generate after the `<|assistant|>` tag, and the loss is only computed on assistant tokens.
 
 
 
 
58
 
59
- ## Training details
 
 
 
 
60
 
61
  | Hyperparameter | Value |
62
  |----------------|-------|
63
- | Layers | 14 |
64
- | Model dim | 512 |
65
- | Embedding dim | 128 |
66
  | FFN multiplier | 4 |
67
- | WKV decay floor | 0.1 |
68
- | Batch size (per step) | 8 |
69
  | Gradient accumulation | 4 |
70
- | Effective batch size | 32 |
71
- | Learning rate | 3e‑4 (cosine schedule) |
72
- | Weight decay | 0.1 |
73
  | Gradient clipping | 1.0 |
74
- | Epochs | 2 |
75
- | Optimizer | AdamW (fused) |
76
-
77
- Training was done on a single T4 GPU (Kaggle, 2x T4 available but only one used). Gradient checkpointing and bfloat16 autocast were essential to fit the 1024‑token sequences within 15 GB of VRAM. The parallel scan over time is exact (not an approximation) and fully vectorised, yielding a training speed of ~80K tokens/second.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
 
79
- ## Limitations & Bias
80
 
81
- - This is a small model trained on a curated synthetic dialogue dataset. It may not generalise well to factual knowledge or long‑form reasoning.
82
- - The ROSA copy mechanism helps with repeating patterns but can sometimes cause the model to inappropriately copy parts of the user’s message.
83
- - No alignment or safety filtering was performed beyond what’s implicit in the dataset.
 
 
 
2
  language: en
3
  license: apache-2.0
4
  library_name: transformers
5
+ pipeline_tag: text-generation
6
  tags:
7
  - fwkv
8
  - rosa
9
+ - rwkv
10
  - linear-transformer
11
  - recurrent-neural-network
12
  - chat
13
  - ultrachat
14
+ - custom-architecture
15
  ---
16
 
17
  # FWKV-ROSA
18
 
19
+ A **56M‑parameter**, from‑scratch recurrent language model that combines a
20
+ **perchannel leaky integrator (FWKV)** with the **RWKV8 ROSA copy‑signal**
21
+ mechanism. It is a research experiment designed to explore how far purely
22
+ recurrent architectures can go on small‑scale, curated conversational data.
23
 
24
+ The model was chat‑tuned on `HuggingFaceH4/ultrachat_200k` and uses a simple
25
+ two‑role template:
26
 
27
+ ```
28
+ <|user|> Your message
29
+ <|assistant|> Model reply<|endoftext|>
30
+ ```
31
+
32
+ ## Model Description
33
+
34
+ - **Architecture:** 14 stacked FWKV blocks. Each block replaces the standard attention with a fixed, data‑independent decayed accumulator:
35
+ `stateₜ = W·stateₜ₋₁ + kₜ·vₜ`, where `W = clamp(sigmoid(w), min=0.1)`.
36
+ The recurrence is computed exactly via a vectorised parallel scan (no
37
+ approximations).
38
+ - **ROSA (Rapid Online Suffix Automaton):** A parameter‑free, causal
39
+ predictor that injects the token that historically followed the longest
40
+ matching suffix of the current context. The ROSA signal is embedded and
41
+ added to the input representation of each token.
42
+ - **Factorised embedding/head:** 128‑dimensional embedding space, projected
43
+ to a 512‑dimensional model space, with tied weights.
44
+ - **Context length:** 1024 tokens. No positional embeddings are used, making
45
+ the context window “free” in terms of parameters.
46
+ - **Tokenizer:** GPT‑2 tokenizer extended with the special tokens `<|user|>`
47
+ and `<|assistant|>`.
48
+
49
+ ## Uses
50
+
51
+ ### Direct Use
52
+
53
+ FWKV-ROSA is intended for **research on efficient language models** and for
54
+ **educational demonstrations** of recurrent architectures. You can chat with
55
+ it in a multi‑turn setting using the template above.
56
+
57
+ ### Out‑of‑Scope Use
58
 
59
+ - This model is **not** suitable for any production or safety‑critical
60
+ application.
61
+ - It has not been aligned with RLHF or other safety methods and may generate
62
+ inappropriate or harmful content.
63
+ - The limited size and training data mean it cannot be relied upon for factual
64
+ knowledge or reasoning.
65
 
66
+ ### Bias, Risks, and Limitations
 
 
 
67
 
68
+ - Trained on a relatively small synthetic dataset, the model can produce
69
+ repetitive or nonsensical output.
70
+ - The ROSA copy mechanism may occasionally copy large chunks of the user’s
71
+ prompt verbatim.
72
+ - Biases present in the original UltraChat data are likely reflected in the
73
+ model’s responses.
74
 
75
+ ## How to Get Started
76
 
77
+ The model relies on a custom architecture. To load it, you must provide the
78
+ `modeling_fwkv.py` file (found in the repository) and trust the remote code:
79
 
80
  ```python
 
81
  from transformers import AutoTokenizer, AutoModelForCausalLM
82
 
83
+ model = AutoModelForCausalLM.from_pretrained(
84
+ "FlameF0X/FWKV-ROSA",
85
+ trust_remote_code=True
86
+ )
87
  tokenizer = AutoTokenizer.from_pretrained("FlameF0X/FWKV-ROSA")
88
  ```
89
 
90
+ Then format your prompts exactly with the chat tokens:
91
 
92
+ ```python
93
+ device = "cuda" if torch.cuda.is_available() else "cpu"
94
+ model.to(device).eval()
95
+
96
+ prompt = "<|user|> What is the capital of France?\n<|assistant|>"
97
+ input_ids = tokenizer.encode(prompt)
98
+ # ROSA IDs must be computed – you can import `rosa` from modeling_fwkv
99
+ from modeling_fwkv import rosa
100
+ rosa_ids = torch.tensor([rosa(input_ids)], device=device)
101
+ out = model(input_ids=torch.tensor([input_ids], device=device),
102
+ rosa_ids=rosa_ids, use_cache=True)
103
+ # Continue autoregressive sampling…
104
  ```
 
 
 
105
 
106
+ For a fully working chat demo, see the Gradio app provided in the repository.
107
+
108
+ ## Training Details
109
+
110
+ ### Dataset
111
 
112
+ - **Name:** [UltraChat 200k](https://huggingface.co/datasets/HuggingFaceH4/ultrachat_200k)
113
+ - **Splits:** `train_sft` (20,000 examples), `test_sft` (1,000 examples)
114
+ - **Format:** multi‑turn conversations; only assistant tokens contribute to the loss.
115
+
116
+ ### Training Procedure
117
 
118
  | Hyperparameter | Value |
119
  |----------------|-------|
120
+ | Architecture | 14 FWKV blocks, d_model=512, d_emb=128 |
 
 
121
  | FFN multiplier | 4 |
122
+ | WKV decay floor| 0.1 |
123
+ | Batch size (per GPU) | 8 |
124
  | Gradient accumulation | 4 |
125
+ | Effective batch size | 32 |
126
+ | Learning rate | 3×10⁻⁴ (cosine schedule) |
127
+ | Weight decay | 0.1 |
128
  | Gradient clipping | 1.0 |
129
+ | Optimizer | AdamW (fused) |
130
+ | Precision | bfloat16 mixed |
131
+ | Epochs | 2 |
132
+ | Hardware | NVIDIA T4 (15 GB) |
133
+ | Training time | ~2 hours |
134
+ | Speed | ~80,000 tokens/second |
135
+
136
+ Gradient checkpointing was enabled to fit the 1024‑token sequences in memory.
137
+
138
+ ## Evaluation
139
+
140
+ | Metric | Value |
141
+ |-----------------------|---------|
142
+ | Validation loss | 4.216 |
143
+ | Validation perplexity | 67.78 |
144
+
145
+ The perplexity is relatively high due to the small model size and limited
146
+ training data. It is comparable to other similarly‑sized recurrent LMs on
147
+ UltraChat.
148
+
149
+ ## Environmental Impact
150
+
151
+ The training ran for about **2 hours** on a single **NVIDIA T4** GPU
152
+ (maximum power draw ~70 W), resulting in an estimated **0.14 kWh** of
153
+ electricity consumption and approximately **0.06 kg CO₂eq** (assuming a
154
+ grid carbon intensity of 0.4 kg/kWh). This is a negligible footprint.
155
+
156
+ ## Technical Specifications
157
+
158
+ - **Model type:** Recurrent neural network (linear RNN)
159
+ - **Parameters:** 56.2 million
160
+ - Backbone (FWKV blocks + embeddings): ~50M
161
+ - ROSA embedding: ~6.2M
162
+ - **Checkpoint format:** PyTorch `safetensors`
163
+ - **Required files in the repo:**
164
+ - `config.json`
165
+ - `model.safetensors` (or `pytorch_model.bin`)
166
+ - `modeling_fwkv.py`
167
+ - `tokenizer.json` / `vocab.json` / `merges.txt`
168
+ - **Auto‑mapping:** The `config.json` includes
169
+ `"auto_map": { "AutoModelForCausalLM": "modeling_fwkv.FWKVLanguageModel" }`,
170
+ so loading with `trust_remote_code=True` will automatically locate the
171
+ correct class.
172
+
173
+ ## Citation
174
+
175
+ If you use FWKV-ROSA in your research, please cite it as:
176
+
177
+ ```bibtex
178
+ @misc{fwkv-rosa,
179
+ author = {FlameF0X},
180
+ title = {FWKV-ROSA: A 56M Recurrent Chat LM with RWKV-style Decay and ROSA Copy Signal},
181
+ year = {2025},
182
+ howpublished = {\url{https://huggingface.co/FlameF0X/FWKV-ROSA}},
183
+ }
184
+ ```
185
 
186
+ ## Additional Information
187
 
188
+ This model was built as an experiment to test the combination of a simple
189
+ leaky integrator with the ROSA copy signal on a small, clean conversational
190
+ dataset. It demonstrates that a pure linear RNN can learn to produce
191
+ coherent multi‑turn dialogue without any attention mechanisms. Feedback and
192
+ contributions are welcome!