File size: 5,809 Bytes
104e933
df7b902
 
 
 
 
 
 
 
 
 
 
 
 
104e933
df7b902
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c5ccfaa
df7b902
 
 
 
 
c5ccfaa
 
 
 
 
 
df7b902
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
---
language:
- en
- ru
license: apache-2.0
library_name: pytorch
tags:
- text-generation
- conversational
- bilingual
- causal-lm
- muon
- from-scratch
- safetensors
---

# InDEXt-135M (InDEX-Transformer)

**InDEXt-135M** is a compact, bilingual (English/Russian) causal language model with **135.2M parameters**, trained completely from scratch on a custom **InDEXt-BPE-32k** tokenizer. It combines a deep-and-narrow transformer topology (24 layers) with Grouped Query Attention (GQA), Rotary Position Embeddings (RoPE), QK-Norm, SwiGLU, Z-loss regularization, and the **Muon** optimizer.

The weights are distributed in the **`safetensors`** format (`model.safetensors`) for fast, safe loading without pickle deserialization, and the model is lightweight enough to run on consumer GPUs or CPUs without external dependencies.

## Model Architecture

| Specification | Value | Details |
| :--- | :--- | :--- |
| **Model Name** | `InDEXt-135M` | InDEX-Transformer |
| **Parameters** | `~135.2M` | Tied input/output embeddings (`weight tying`) |
| **Layers (`N_LAYERS`)** | `24` | Deep-and-narrow design |
| **Hidden Size (`DIM`)** | `576` | Model embedding dimension |
| **FFN Intermediate (`HIDDEN_DIM`)** | `1536` | `SwiGLU` gated activation |
| **Attention Mechanism** | `GQA (9Q / 3KV)` | 9 query heads, 3 KV heads, `head_dim = 64` |
| **Normalization** | `RMSNorm` + `QK-Norm` | Pre-norm blocks + query/key head normalization |
| **Positional Encoding** | `RoPE` (theta = 100,000) | Supports sequences up to 2048 tokens |
| **Context Length (`SEQ_LEN`)** | `2048` | Tokens |
| **Tokenizer** | `InDEXt-BPE-32k` | Byte-level BPE, `32,768` vocabulary (`2^15`) |
| **Control Tokens** | 4 special tokens | `<|endoftext|>`, `<|pad|>`, `<|user|>`, `<|assistant|>` |

## Training Pipeline

### 1. Custom Bilingual Tokenizer (`InDEXt-BPE-32k`)
* **Algorithm:** Byte-level BPE with Unicode `NFC` normalization.
* **Training Sample:** 140,000 documents balanced 50/50 between English (`HuggingFaceTB/smollm-corpus`) and Russian (`HuggingFaceFW/fineweb-2`).
* **Vocabulary Size:** `32,768` tokens (aligned to multiples of 128 for Tensor Core efficiency).

### 2. Pre-training
* **Dataset:** `1.08B` unique tokens (`train.bin`) + `10M` validation tokens (`val.bin`), trained for `6,675` steps (`~437.5M` tokens seen).
* **Data Mix:**
  * `35%` — `HuggingFaceTB/smollm-corpus` (`fineweb-edu-dedup`, English)
  * `25%` — `HuggingFaceTB/smollm-corpus` (`cosmopedia-v2`, English)
  * `40%` — `HuggingFaceFW/fineweb-2` (`rus_Cyrl`, Russian)
* **Optimization:**
  * Effective batch size: `65,536` tokens per step (`32 × 2048`)
  * Matrix parameters: **Muon** (`lr = 0.008`, 3-step Newton-Schulz orthogonalization, `weight_decay = 0.1`)
  * Embeddings & 1D parameters: **AdamW** (`lr = 1.2e-3`, `betas = (0.9, 0.95)`, `weight_decay = 0.1`)
  * Precision: `bfloat16` mixed precision with `torch.compile` and native FlashAttention GQA
  * Hardware: NVIDIA A100-SXM4-40GB (~1 hour full pipeline including SFT)
    
### 3. Supervised Fine-Tuning (SFT)
* **Dataset:** `50M` packed tokens (`60%` English / `40%` Russian) from `HuggingFaceTB/smoltalk` and `IlyaGusev/ru_turbo_saiga`.
* **Loss Masking:** Cross-entropy loss is computed exclusively on `<|assistant|>` response tokens (`ignore_index = -100` on `<|user|>` turns).
* **Optimization:** 2 epochs with **Muon** (`lr = 3e-3`, `wd = 0.05`) and **AdamW** (`lr = 4e-4`, `wd = 0.05`).

## Chat Template

The model expects the following turn structure without system prompts:

    <|user|>User message here
    <|assistant|>Assistant response here<|endoftext|>

Token IDs:
* `<|endoftext|>`: `0`
* `<|pad|>`: `1`
* `<|user|>`: `2`
* `<|assistant|>`: `3`

## Repository Files

* `model.safetensors` — Final instruction-tuned checkpoint stored in `safetensors` format.
* `indext_tokenizer.json` — Hugging Face `tokenizers` JSON file for `InDEXt-BPE-32k`.
* `chat_indext.py` — Standalone interactive CLI inference script.

## Usage

### Requirements

    pip install torch tokenizers safetensors

### Python Inference Example

    import torch
    from safetensors.torch import load_file
    from tokenizers import Tokenizer
    from chat_indext import InDEXt135M, reply, USER_ID, ASST_ID

    device = "cuda" if torch.cuda.is_available() else "cpu"
    tokenizer = Tokenizer.from_file("indext_tokenizer.json")

    model = InDEXt135M().to(device)
    model.load_state_dict(load_file("model.safetensors", device=device))
    model.eval()

    prompt = "Explain in simple terms why the sky is blue."
    input_ids = [USER_ID] + tokenizer.encode(prompt + "\n").ids + [ASST_ID]

    output = reply(input_ids, max_new=200, temp=0.7, top_p=0.9, rep_pen=1.15)
    print(output)

### Interactive CLI

Update `CKPT_PATH = "model.safetensors"` in `chat_indext.py` (using `safetensors.torch.load_file`), place `model.safetensors`, `indext_tokenizer.json`, and `chat_indext.py` in the same directory, and run:

    python chat_indext.py

## Export to `GGUF` (`llama.cpp` / Ollama)

The architecture follows a Llama-style layout (`RMSNorm`, `GQA`, `RoPE`, `SwiGLU`, no biases) with the addition of per-head `QK-Norm` (`q_norm`, `k_norm`) in each attention block. Standard `llama.cpp` conversion requires mapping `q_norm.weight` and `k_norm.weight` to a GGUF architecture that supports QK-Norm (such as `qwen3` or `olmo2`). Recommended quantization formats: `F16` (~270 MB) or `Q8_0` (~145 MB).

## Limitations

* At **135.2M parameters**, the model is intended for lightweight conversational tasks, basic explanations, and bilingual text generation. It is not suited for complex multi-step reasoning, mathematical proofs, code generation, or factual recall of niche topics.
* Recommended decoding parameters: `temperature = 0.6–0.75`, `top_p = 0.9`, `repetition_penalty = 1.15`.