Lgr54HFi commited on
Commit
a4a6375
·
verified ·
1 Parent(s): 74be8b4

Upload README.md

Browse files
Files changed (1) hide show
  1. README.md +98 -72
README.md CHANGED
@@ -1,117 +1,122 @@
1
- # Chimera GGUF Import
2
 
3
- **Universal weight importer**: convert ANY GGUF model (any quantization, any architecture) into Chimera 5.1 ternary format.
4
 
5
- ## What it does
6
 
7
  ```
8
- GGUF (Q4_0/Q5_1/Q8_0/F16/F32/BF16...)
9
- Dequantize to FP32 (lossless for the given quant)
10
- Smart noise reduction (outlier-aware, median-robust)
11
- Ternary conversion {-1, 0, +1} with per-row AbsMean
12
- → 2-bit pack (4 weights/byte = 16× memory reduction)
13
- → Chimera 5.1 checkpoint (.pt)
14
  ```
15
 
16
- ## Install
17
 
18
  ```bash
19
  pip install gguf torch numpy
20
  ```
21
 
22
- ## Usage
23
 
24
  ```bash
25
  python gguf_import.py \
26
- --gguf /path/to/any-model.gguf \
27
- --config /path/to/chimera/config.json \
28
  --scale tiny \
29
  --output ./imported_chimera.pt
30
  ```
31
 
32
- ### With noise reduction tuning
 
 
 
 
 
 
33
 
34
  ```bash
 
35
  python gguf_import.py \
36
  --gguf model.gguf \
37
  --config config.json \
38
- --scale small \
39
- --noise-method outlier_clip \
40
- --noise-sigma 2.5 \
41
- --output ./model_chimera.pt
42
  ```
43
 
44
- ### Supported GGUF quantizations
45
-
46
- | Type | Status |
47
- |---|---|
48
- | F32 | ✅ Direct |
49
- | F16 | ✅ Cast |
50
- | BF16 | ✅ Cast |
51
- | Q8_0 / Q8_1 | ✅ Dequantize |
52
- | Q5_0 / Q5_1 | ✅ Dequantize |
53
- | Q4_0 / Q4_1 | ✅ Dequantize |
54
- | Q2_K / Q3_K / Q4_K / Q5_K / Q6_K | ✅ Via `gguf.dequantize` |
55
-
56
- ### Supported source architectures
57
 
58
- Any GGUF model: LLaMA, Qwen, Mistral, Phi, Gemma, DeepSeek, etc.
 
 
 
 
 
59
 
60
- ## How the conversion works
 
 
 
 
 
 
 
61
 
62
- ### 1. Noise reduction
63
 
64
- Before ternary conversion, weights are pre-processed to remove outliers that would distort the AbsMean scale:
 
 
 
 
65
 
66
- - **`outlier_clip`** (default): clip values beyond `mean ± 3σ`. Keeps 99.7% of mass, removes extreme outliers.
67
- - **`median_center`**: center by median, scale by MAD. Robust to heavy-tailed distributions.
68
- - **`none`**: passthrough (useful if source is already clean).
69
 
70
- ### 2. Ternary conversion
71
 
72
- Per-row AbsMean scaling:
73
- ```
74
- α_m = mean(|W_m,:|)
75
- W̃ = W / α
76
- W_q = round_STE(W̃) ∈ {-1, 0, +1}
77
- ```
78
 
79
- Straight-Through Estimator (STE) allows the ternary grid to learn from the original weight distribution.
80
 
81
- ### 3. 2-bit packing
82
-
83
- ```
84
- -1 bits 10 (2)
85
- 0 bits 00 (0)
86
- +1 bits 01 (1)
87
- ```
88
-
89
- 4 weights packed into 1 uint8 byte = **16× memory reduction** vs FP32.
90
 
91
- ### 4. Shape adaptation
92
 
93
- When source and Chimera dimensions differ (e.g., 4096 → 256 hidden size), weights are resized via bilinear interpolation, preserving the spatial structure of the weight distribution.
94
 
95
- ## Output format
96
 
97
- ```python
98
- ckpt = torch.load("imported_chimera.pt")
99
- ckpt["model"] # state_dict with FP32 latent weights for BitLinear + norms/embeds
100
- ckpt["config"] # Chimera config used
101
- ckpt["source"] # GGUF path, scale, noise params
102
- ```
 
 
 
103
 
104
- ## Architecture note
105
 
106
- Chimera 5.1 uses hybrid recurrent layers (GatedDeltaNet, xLSTM mLSTM, Titans MAC, TSP Span Knot) — NOT standard transformer attention. Weight mapping from standard transformer QKV → Chimera attention is inherently lossy. The importer maps:
 
 
 
107
 
108
- - **Embeddings** (`token_embd` `embed`)
109
- - **Output** (`output` → `lm_head`)
110
- - **Norms** (`attn_norm`, `ffn_norm` → `attn_norm`, `mlp_norm`)
111
- - **MLP** (`ffn_gate`, `ffn_up`, `ffn_down` → `mlp.gate_proj`, `mlp.up_proj`, `mlp.down_proj`)
112
- - **Attention projections** (`attn_q`, `attn_k`, `attn_v`, `attn_output` → `attn.q_proj`, `k_proj`, `v_proj`, `o_proj`)
113
 
114
- After import, **fine-tuning is strongly recommended** to adapt the transplanted weights to Chimera's recurrence dynamics. Use the patched `train.py` with MeZO:
115
 
116
  ```bash
117
  OMP_NUM_THREADS=20 python train.py \
@@ -120,3 +125,24 @@ OMP_NUM_THREADS=20 python train.py \
120
  --max_tokens 50000000 --compile --no-bf16 --num_workers 0 \
121
  --output_dir ./finetune_imported
122
  ```
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Chimera GGUF Import — v2.0 Optimized
2
 
3
+ Importer universel : convertit **n'importe quel modèle GGUF** (n'importe quelle quantisation, n'importe quelle architecture) en checkpoint compatible Chimera 5.1.
4
 
5
+ ## Ce que fait le script
6
 
7
  ```
8
+ GGUF (Q4_0, Q5_1, Q8_0, F16, F32, BF16...)
9
+ Déquantification FP32 via gguf.dequantize()
10
+ Noise reduction (outlier-aware, par ligne ou global)
11
+ Conversion ternaire {-1, 0, +1} avec AbsMean par ligne
12
+ → 2-bit packing (4 poids/byte = 16× réduction mémoire)
13
+ Checkpoint Chimera 5.1 (.pt)
14
  ```
15
 
16
+ ## Installation
17
 
18
  ```bash
19
  pip install gguf torch numpy
20
  ```
21
 
22
+ ## Usage rapide
23
 
24
  ```bash
25
  python gguf_import.py \
26
+ --gguf /chemin/vers/nimportequel-modele.gguf \
27
+ --config /chemin/vers/chimera/config.json \
28
  --scale tiny \
29
  --output ./imported_chimera.pt
30
  ```
31
 
32
+ ## Modes de stockage
33
+
34
+ | Mode | Description | Quand l'utiliser |
35
+ |---|---|---|
36
+ | `fp32` (défaut) | Sauvegarde weight latent FP32 natif Chimera. Compatible avec `Chimera51ForCausalLM.load_state_dict()`. | **Recommandé** — le plus simple. |
37
+ | `packed` | Sauvegarde uniquement `packed_weight` + `alpha` pour les couches linéaires. **Nécessite un loader custom** dans Chimera. | Expérimental — checkpoint ultra-compact. |
38
+ | `both` | Sauvegarde les deux : weight FP32 + packed + alpha. | Pour migration progressive vers packed. |
39
 
40
  ```bash
41
+ # Mode packed (expérimental)
42
  python gguf_import.py \
43
  --gguf model.gguf \
44
  --config config.json \
45
+ --scale tiny \
46
+ --storage packed \
47
+ --output ./chimera_packed.pt
 
48
  ```
49
 
50
+ ## Réduction de bruit configurable
 
 
 
 
 
 
 
 
 
 
 
 
51
 
52
+ | Méthode | Description | Par défaut |
53
+ |---|---|---|
54
+ | `row_outlier_clip` | Clip par ligne `mean ± 3σ` — préserve la structure locale des poids. | **✅ défaut** |
55
+ | `global_clip` | Clip global mean ± σ — plus agressif, moins de granularité. | |
56
+ | `median_center` | Center par médiane, scale par MAD — robuste aux distributions lourdes. | |
57
+ | `none` | Passthrough — si la source est déjà propre. | |
58
 
59
+ ```bash
60
+ python gguf_import.py \
61
+ --gguf model.gguf \
62
+ --config config.json \
63
+ --noise-method row_outlier_clip \
64
+ --noise-sigma 2.5 \
65
+ --output ./model_chimera.pt
66
+ ```
67
 
68
+ ## Stratégies de resize
69
 
70
+ | Stratégie | Description | Par défaut |
71
+ |---|---|---|
72
+ | `crop_pad` | Copie les zones communes, init le reste avec des gaussiennes de même std. | **✅ défaut** |
73
+ | `interpolate` | Interpolation bilinéaire (préserve la structure spatiale). | |
74
+ | `strict` | Échoue si les shapes ne matchent exactement. | |
75
 
76
+ ## Auto-transpose
 
 
77
 
78
+ Détecte automatiquement si les dimensions source et cible sont inversées (`[out, in]` vs `[in, out]`) et transpose silencieusement.
79
 
80
+ Désactiver avec `--no-auto-transpose`.
 
 
 
 
 
81
 
82
+ ## Quantisations GGUF supportées
83
 
84
+ | Type | Statut |
85
+ |---|---|
86
+ | F32 | ✅ Direct |
87
+ | F16 | Cast |
88
+ | BF16 | Cast |
89
+ | Q8_0 / Q8_1 | ✅ Déquantification |
90
+ | Q5_0 / Q5_1 | ✅ Déquantification |
91
+ | Q4_0 / Q4_1 | ✅ Déquantification |
92
+ | Q2_K / Q3_K / Q4_K / Q5_K / Q6_K | Via `gguf.dequantize` |
93
 
94
+ ## Architectures source supportées
95
 
96
+ Tout modèle GGUF : LLaMA, Qwen, Mistral, Phi, Gemma, DeepSeek, etc.
97
 
98
+ ## Mapping GGUF → Chimera
99
 
100
+ | GGUF source | Chimera cible |
101
+ |---|---|
102
+ | `token_embd` | `embed.weight` |
103
+ | `output` | `lm_head.weight` |
104
+ | `output_norm` | `norm.weight` |
105
+ | `blk.N.attn_q/k/v/output` | `layers.N.attn.q/k/v/o_proj` |
106
+ | `blk.N.ffn_gate/up/down` | `layers.N.mlp.gate/up/down_proj` |
107
+ | `blk.N.attn_norm` | `layers.N.attn_norm` |
108
+ | `blk.N.ffn_norm` | `layers.N.mlp_norm` |
109
 
110
+ ## Clés manquantes
111
 
112
+ Par défaut (`--no-init-missing` désactivé), toutes les couches Chimera absentes du GGUF source sont initialisées automatiquement :
113
+ - Norms : `torch.ones(...)`
114
+ - Embeddings/Head : `normal_(0, 0.02)`
115
+ - Linéaires BitLinear : `normal_(0, sqrt(2/fan_in))` + ternarisation
116
 
117
+ ## Après import : fine-tuning obligatoire
 
 
 
 
118
 
119
+ Chimera utilise des couches récurrentes hybrides (GatedDeltaNet, xLSTM, Titans, TSP Span Knot) — **pas** du transformer standard. Le mapping QKV↔recurrence est intrinsèquement lossy. Fine-tunez avec MeZO sur CPU :
120
 
121
  ```bash
122
  OMP_NUM_THREADS=20 python train.py \
 
125
  --max_tokens 50000000 --compile --no-bf16 --num_workers 0 \
126
  --output_dir ./finetune_imported
127
  ```
128
+
129
+ ## Exemple complet
130
+
131
+ ```bash
132
+ python gguf_import.py \
133
+ --gguf ./models/mistral-7b-q4_0.gguf \
134
+ --config ./chimera/config.json \
135
+ --scale tiny \
136
+ --storage fp32 \
137
+ --param-dtype fp32 \
138
+ --noise-method row_outlier_clip \
139
+ --noise-sigma 3.0 \
140
+ --ternary-threshold 0.5 \
141
+ --resize-strategy crop_pad \
142
+ --output ./mistral_chimera_tiny.pt
143
+ ```
144
+
145
+ ## Dépôt
146
+
147
+ - Repo HuggingFace : [`Lgr54HFi/chimera-gguf-import`](https://huggingface.co/Lgr54HFi/chimera-gguf-import)
148
+ - Version : 2.0-optimized