Text Generation
File size: 5,785 Bytes
50abc74
 
8a4768f
 
d983957
8a4768f
 
 
50abc74
8a4768f
 
 
 
 
 
 
f3ac3cb
 
1449065
 
8a4768f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44c5767
8a4768f
 
 
 
 
 
 
 
 
 
 
 
 
44c5767
 
8a4768f
 
 
 
 
 
 
44c5767
 
 
 
 
 
 
 
 
8a4768f
 
 
 
44c5767
 
8a4768f
 
 
 
 
 
44c5767
 
 
 
 
 
 
 
8a4768f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b0ab32f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8a4768f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
395bf81
 
8a4768f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d983957
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
---
license: apache-2.0
datasets:
- CodonProject/Organic-Reasoning-195k
- CodonProject/Motif-Prev1
base_model:
- CodonProject/MotifA1-Base
pipeline_tag: text-generation
---

# MotifA1

> A 105M-parameter bilingual causal language model with dual-mode reasoning, built on the Codon stack.

MotifA1 is a compact, CPU-friendly causal language model trained for bilingual (Chinese / English) instruction following. It supports both an explicit *thinking* mode (chain-of-thought wrapped in `[cot_start] ... [cot_end]`) and a direct *non-thinking* mode, switchable at inference time.

Web Demo at [ModelScope](https://www.modelscope.cn/studios/CodonProject/MotifA1-SFT) [HuggingFace](https://huggingface.co/spaces/CodonProject/MotifA1-SFT)

![exp](https://huggingface.co/CodonProject/MotifA1-SFT/resolve/main/exp_chat_en.png "exp")

---

## Model Summary

| Field | Value |
| --- | --- |
| Parameters | **105.41 M** |
| Vocabulary | 8,192 (BPE, packed) |
| Architecture | Causal Transformer (decoder-only) |
| Position Encoding | RoPE, base = **500,000** |
| Training Context | 4,096 tokens |
| Languages | δΈ­ζ–‡ / English |
| Modes | Thinking / Non-thinking |
| Runtime | CUDA / CPU |
| Precision | fp32 / bf16 |
| License | See repository |

### Why RoPE base 500k

A RoPE base of 500k flattens the rotary frequency spectrum, which gives MotifA1 headroom to **extend its context window beyond the 4,096 it was trained on** via interpolation-style scaling, without retraining the position basis from scratch.

---

## Installation

```bash
pip install codon-model==0.0.6a2
```

Required artifacts:

- `motifa1_sft.safetensors` β€” model weights
- `motif.vocab` β€” packed tokenizer (vocab + chat template + config in one zip)

---

## Quickstart

### Load the model

If you have downloaded the `motifa1_sft.safetensors`, then you can load as:

```python
from codon.motif import MotifA1

model = MotifA1().load_pretrained('motifa1_sft.safetensors').to('cuda')
print(model.count_params(human_readable=True))   # -> 105.41 M
```

More simply load from network:

```python
from codon.motif import MotifA1

model = MotifA1().from_remote().to('cuda')
print(model.count_params(human_readable=True))   # -> 105.41 M
```

CPU users: replace `'cuda'` with `'cpu'`. Inference works out of the box, just slower.

### Load the tokenizer

If you have downloaded the `motif.vocab`, then you can load as:

```python
from codon.utils.tokens import PackedTokenizer

tokenizer = PackedTokenizer('motif.vocab')
```

More simply load from network:

```python
from codon.motif import MotifA1Tokenizer

tokenizer = MotifA1Tokenizer().from_remote()
```

### Streaming chat

```python
from codon.utils.generate import chat
from rich.console import Console

console = Console()

for chunk in chat(
    model, tokenizer, model.device,
    messages=[{'role': 'user', 'content': 'Your Q'}],
    stream=True,
    max_new_tokens=1024,
):
    if chunk.cot_ended:
        console.print('\n')
    if chunk.is_cot:
        console.print(chunk.content, end='', style='blue')
    else:
        console.print(chunk.content, end='')
```

The stream yields chunks tagged with:
- `chunk.is_cot` β€” whether the current span is inside a chain-of-thought block
- `chunk.cot_ended` β€” fires once when the model exits thinking mode and begins the user-facing answer
- `chunk.content` β€” the decoded text fragment

This lets you render reasoning in a separate visual channel (e.g. dim blue) and the final answer in normal style.

### Using as OpenAI-Compat Endpoint

```python
from codon.utils.service import Service, ModelCard

Service([
    ModelCard(
        model=model,
        tokenizer=tokenizer,
        model_id='Motif-A1',
        owned='CodonProject'
    )
]).run(port=11305)
```

---

## Modes

MotifA1 follows a chat template with explicit reasoning markers.

**Thinking mode** β€” the model first generates content between `[cot_start]` and `[cot_end]`, then produces the final answer. Recommended for math, multi-step reasoning, code planning.

**Non-thinking mode** β€” the model emits an empty `[cot_start][cot_end]` block and answers directly. Recommended for chit-chat, translation, short-form generation, and latency-sensitive applications.

The `chat` helper exposes mode switching; consult the Codon docs for the parameter form your version exposes.

---

## Training

- **Stage 1 β€” Pretraining** at 4,096-token context, bilingual corpus.
- **Stage 2 β€” SFT** on a curated mix of single- and multi-turn dialogues, with thinking and non-thinking samples blended.
- **Optimizer** AdamW, weight decay 0.01, gradient clip 1.0.
- **Schedule** Linear warmup β†’ cosine annealing to 10% of peak LR.
- **Precision** bf16 autocast.

The pretraining and SFT code are available at https://github.com/CodonProject/codon-model/tree/v0.0.6-alpha.2/train_exp .

---

## Known Limitations

- **Long-range attention is weak.** Even with RoPE base 500k allowing window extension, retrieval and reasoning over long spans (>2k effective tokens) degrade noticeably. Treat MotifA1 as a short- to mid-context model in practice.
- **Scale-bound knowledge.** At ~105 M parameters, factual recall is limited. Pair with retrieval for knowledge-heavy tasks.
- **Vocabulary is compact.** With 8,192 BPE tokens, rare scripts, niche jargon, and long URLs may be tokenized inefficiently.
- **Hallucination.** Like all LMs of this scale, MotifA1 can produce confident but incorrect answers. Verify safety-critical outputs.

---

## Intended Use

- Personal assistants, on-device chat, edge deployment
- Education and research on small LMs, dual-mode reasoning, and RoPE scaling
- A base model for further fine-tuning at modest compute budgets

Not intended for high-stakes decisions (medical, legal, financial) or as a sole knowledge source.