File size: 9,999 Bytes
d093b93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
---
library_name: transformers
pipeline_tag: text-generation
language:
  - en
license: mit
datasets:
  - roneneldan/TinyStories
tags:
  - deepseek-v2
  - multi-head-latent-attention
  - mla
  - mixture-of-experts
  - moe
  - tinystories
  - tiny-model
  - text-generation
  - validation
  - debug-model
---

# Tiny DeepSeek V2 3M

This repository contains a tiny `DeepseekV2ForCausalLM` Mixture-of-Experts
language model trained from scratch on TinyStories.

The model has **2,928,392 parameters**. It combines DeepSeek V2-style
Multi-head Latent Attention (MLA), compressed key/value states, decoupled
RoPE, routed experts, and a shared expert in a compact checkpoint intended for
implementation testing and architecture experiments.

This is a synthetic tiny checkpoint. It is not an official DeepSeek model,
does not contain weights from an original DeepSeek checkpoint, and should not
be expected to match the quality or capabilities of production DeepSeek
models.

## Repository contents

- `hf/`: the final Hugging Face checkpoint and tokenizer
- `example_generate.py`: a minimal generation example
- `eval_text_generation.json`: generations from the final training evaluation
- `artifact_metadata.json`: training arguments, metrics, router usage, and the
  expanded configuration
- `deepseek_v2_config_dump.json`: a standalone configuration dump

Optimizer checkpoints and the full training history are intentionally omitted
from this distribution package.

## Architecture identity

This checkpoint uses:

```text
DeepseekV2Config
DeepseekV2ForCausalLM
model_type: deepseek_v2
```

It is specifically a DeepSeek V2 architecture, not DeepSeek V3 and not a
generic dense decoder renamed as DeepSeek. The checkpoint exercises the
DeepSeek V2 MLA and MoE implementations provided by Hugging Face Transformers.

## Model architecture

```yaml
architecture: DeepseekV2ForCausalLM
model_type: deepseek_v2
parameter_count: 2,928,392

model_vocab_size: 1,024
tokenizer_size: 1,003
hidden_size: 216
intermediate_size: 432
num_hidden_layers: 5

num_attention_heads: 8
num_key_value_heads: 8
qk_nope_head_dim: 16
qk_rope_head_dim: 16
v_head_dim: 32
q_lora_rank: null
kv_lora_rank: 64

first_k_dense_replace: 1
n_routed_experts: 4
n_shared_experts: 1
num_experts_per_tok: 1
moe_intermediate_size: 128
topk_method: greedy
norm_topk_prob: false
routed_scaling_factor: 4.0
aux_loss_alpha: 0.01
seq_aux: true

tie_word_embeddings: true
rms_norm_eps: 1.0e-6
max_position_embeddings: 2,048
rope_theta: 10,000
```

## Multi-head Latent Attention

Each attention head separates its query/key dimensions into:

```text
16 non-positional dimensions + 16 rotary dimensions
```

Values use 32 dimensions per head. The key/value path is compressed through a
64-dimensional latent projection (`kv_lora_rank: 64`) before being expanded
for attention. Query LoRA is disabled in this tiny configuration, while the
compressed KV path and decoupled rotary/non-rotary query-key components remain
active.

This keeps the checkpoint small while exercising the defining DeepSeek V2 MLA
code paths.

## Dense and MoE layers

The first decoder layer uses a dense MLP. The remaining four layers use
DeepSeek V2 MoE blocks:

```text
layer 0: dense MLP
layer 1: 4 routed experts, top-1 + 1 shared expert
layer 2: 4 routed experts, top-1 + 1 shared expert
layer 3: 4 routed experts, top-1 + 1 shared expert
layer 4: 4 routed experts, top-1 + 1 shared expert
```

Every token is sent to one routed expert, while the shared expert is evaluated
for all tokens. Sequence-level router auxiliary loss was enabled during
training.

All routed experts received traffic. Final aggregate routing fractions were
close to 25% for every expert:

| MoE layer | Expert 0 | Expert 1 | Expert 2 | Expert 3 |
| ---: | ---: | ---: | ---: | ---: |
| 1 | 0.2502 | 0.2487 | 0.2522 | 0.2488 |
| 2 | 0.2538 | 0.2481 | 0.2487 | 0.2495 |
| 3 | 0.2512 | 0.2454 | 0.2519 | 0.2514 |
| 4 | 0.2465 | 0.2535 | 0.2542 | 0.2458 |

## Training data

The model was trained on the full TinyStories training corpus using an
independent 1% validation split:

```yaml
selected_stories: 2,119,489
training_stories: 2,098,294
validation_stories: 21,195
validation_fraction: 0.01

training_blocks: 2,441,089
validation_blocks: 24,608
block_size: 256
```

Stories were joined into a continuous packed stream:

```text
BOS + story 1 + EOS + BOS + story 2 + EOS + ...
```

The stream was split into fixed 256-token blocks without padding. Only the
final incomplete block was discarded.

## Tokenizer

The checkpoint uses a small custom byte-level BPE tokenizer. The base BPE
vocabulary was trained with:

```text
BPE()
ByteLevel(add_prefix_space=False)
base_vocab_size: 1,000
min_frequency: 2
normalizer: None
```

Special tokens were then added at fixed IDs:

```text
<s>          -> 1000
</s>         -> 1001
<|im_start|> -> 1002
```

The tokenizer uses `<s>` as both BOS and padding, and `</s>` as EOS. The model
configuration reserves 1,024 embedding rows while the tokenizer exposes
1,003 tokens.

## Training setup

The checkpoint was trained from scratch in float32 on an NVIDIA GeForce GTX
1650:

```yaml
dtype: float32
batch_size: 16
block_size: 256
training_steps: 152,568
epochs: 1.0
tokens_processed: 624,918,528

optimizer: AdamW
learning_rate: 3.0e-4
warmup_steps: 2,000
scheduler: warmup + cosine decay
minimum_learning_rate: 3.0e-5
weight_decay: 0.0
grad_clip: 1.0
```

## Evaluation

The final checkpoint produced:

```yaml
final_train_loss: 1.4096
validation_loss: 1.4226
validation_perplexity: 4.1478
```

Validation loss was computed over 32 batches, or 131,072 tokens, from the
independent packed validation split. These values are compact checkpoint
diagnostics, not general language-model benchmark results.

## Example generation

One final evaluation sample for prompt `There was a little` was:

```text
There was a little girl named Lily who loved to explore. One day, she saw a
big bush with lots of yummy peaches on it. She wanted to eat one, but her mom
said no.

Lily didn't listen and kept trying to eat her peach.
```

Sampling is stochastic. The model usually produces recognizable
TinyStories-style English, but semantic contradictions, unfinished sentences,
invented words, and repetition remain possible at this size.

## Usage

Install the requirements:

```bash
pip install -r requirements.txt
```

Run the included local example from the repository root:

```bash
python example_generate.py
```

To load the package from Hugging Face Hub, resolve the `hf` directory to a
local path first. This avoids a Transformers 5.14.1 local-subfolder issue in
which generation configuration lookup may incorrectly fall back to the
repository root:

```python
from pathlib import Path

import torch
from huggingface_hub import snapshot_download
from transformers import AutoModelForCausalLM, AutoTokenizer

repo = "shibatch/tinydeepseekv2-3m"
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model_dir = Path(snapshot_download(
    repo_id=repo,
    allow_patterns=["hf/*"],
)) / "hf"

tokenizer = AutoTokenizer.from_pretrained(model_dir)
model = AutoModelForCausalLM.from_pretrained(
    model_dir,
    dtype=torch.float32,
).to(device)
model.eval()

prompt = "There was a little"
input_ids = torch.tensor(
    [[tokenizer.bos_token_id] + tokenizer.encode(
        prompt,
        add_special_tokens=False,
    )],
    dtype=torch.long,
    device=device,
)

torch.manual_seed(0)
if device.type == "cuda":
    torch.cuda.manual_seed_all(0)

with torch.no_grad():
    output = model.generate(
        input_ids=input_ids,
        max_new_tokens=100,
        do_sample=True,
        temperature=0.8,
        top_p=0.95,
        top_k=40,
        repetition_penalty=1.1,
        pad_token_id=tokenizer.pad_token_id,
        eos_token_id=tokenizer.eos_token_id,
    )

print(tokenizer.decode(output[0].tolist(), skip_special_tokens=True))
```

## Loading requirements

The checkpoint requires a Transformers release containing
`DeepseekV2ForCausalLM`. It was trained and tested with:

```text
transformers 5.14.1
torch 2.14.0.dev20260720+cu126
```

## Intended uses

This model is intended for:

- testing `DeepseekV2Config` and `DeepseekV2ForCausalLM`
- testing Multi-head Latent Attention and compressed KV states
- exercising decoupled rotary and non-rotary query/key dimensions
- testing dense-to-MoE layer transitions
- testing top-1 routed experts and shared experts
- checking sequence-level router load-balancing loss
- exercising custom tokenizer loading
- testing `generate()`, `save_pretrained()`, and `from_pretrained()`
- compact inference-engine and architecture experiments

It is not intended for:

- instruction following or chat
- factual question answering
- high-quality long-form generation
- production deployment
- safety-critical use
- benchmark comparison with production DeepSeek models

## Limitations

Known limitations include:

- only 2.93 million parameters
- small 1,003-token tokenizer
- English TinyStories-only pretraining
- weak factual knowledge and reasoning
- no instruction tuning or chat template
- occasional grammatical and semantic errors
- invented words and truncated sentences
- repetition and template-like stories
- no quality-equivalence claim with official DeepSeek models
- no current llama.cpp or GGUF inference support assumed for this exact
  DeepSeek V2 MLA/MoE configuration

## Notes on GGUF

The checkpoint is distributed as a normal float32 Hugging Face Safetensors
model. A useful GGUF build requires converter and runtime support for this
DeepSeek V2 MLA and MoE graph, including compressed KV projections, decoupled
RoPE, routed experts, and the shared expert. Merely placing tensors in a GGUF
container is not sufficient for compatible inference.

## Citation

This is a synthetic tiny DeepSeek V2-compatible MoE checkpoint trained from
scratch on TinyStories. It is intended for implementation validation,
debugging, education, and small-scale architecture experiments.