sovereign-inference 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- sovereign_inference-0.1.0/.gitignore +170 -0
- sovereign_inference-0.1.0/BUILDING.md +213 -0
- sovereign_inference-0.1.0/CMakeLists.txt +184 -0
- sovereign_inference-0.1.0/PKG-INFO +565 -0
- sovereign_inference-0.1.0/README.md +552 -0
- sovereign_inference-0.1.0/bindings/python/sovereign_py.cpp +466 -0
- sovereign_inference-0.1.0/examples/basic_generate.py +108 -0
- sovereign_inference-0.1.0/examples/test_pipeline.py +351 -0
- sovereign_inference-0.1.0/include/sovereign/engine.hpp +341 -0
- sovereign_inference-0.1.0/include/sovereign/format.hpp +390 -0
- sovereign_inference-0.1.0/include/sovereign/kv_cache.hpp +290 -0
- sovereign_inference-0.1.0/include/sovereign/memory_manager.hpp +391 -0
- sovereign_inference-0.1.0/include/sovereign/quantizer.hpp +251 -0
- sovereign_inference-0.1.0/include/sovereign/vulkan_context.hpp +384 -0
- sovereign_inference-0.1.0/package-lock.json +18 -0
- sovereign_inference-0.1.0/package.json +5 -0
- sovereign_inference-0.1.0/pyproject.toml +27 -0
- sovereign_inference-0.1.0/scripts/build.sh +161 -0
- sovereign_inference-0.1.0/scripts/compile_shaders.js +45 -0
- sovereign_inference-0.1.0/setup.py +28 -0
- sovereign_inference-0.1.0/shaders/attention_gqa.comp +230 -0
- sovereign_inference-0.1.0/shaders/compiled/attention_gqa.spv +0 -0
- sovereign_inference-0.1.0/shaders/compiled/matmul_int4.spv +0 -0
- sovereign_inference-0.1.0/shaders/compiled/rmsnorm.spv +0 -0
- sovereign_inference-0.1.0/shaders/compiled/sampler.spv +0 -0
- sovereign_inference-0.1.0/shaders/compiled/silu_gate.spv +0 -0
- sovereign_inference-0.1.0/shaders/matmul_int4.comp +187 -0
- sovereign_inference-0.1.0/shaders/rmsnorm.comp +122 -0
- sovereign_inference-0.1.0/shaders/sampler.comp +164 -0
- sovereign_inference-0.1.0/shaders/silu_gate.comp +79 -0
- sovereign_inference-0.1.0/src/compute/kv_cache.cpp +447 -0
- sovereign_inference-0.1.0/src/format/format.cpp +190 -0
- sovereign_inference-0.1.0/src/inference/engine.cpp +777 -0
- sovereign_inference-0.1.0/src/memory/memory_manager.cpp +594 -0
- sovereign_inference-0.1.0/src/quantizer/quantizer.cpp +612 -0
- sovereign_inference-0.1.0/src/vulkan/vulkan_context.cpp +1020 -0
- sovereign_inference-0.1.0/tests/CMakeLists.txt +34 -0
- sovereign_inference-0.1.0/tests/test_engine.cpp +147 -0
- sovereign_inference-0.1.0/tests/test_format.cpp +195 -0
- sovereign_inference-0.1.0/tests/test_kv_cache.cpp +325 -0
- sovereign_inference-0.1.0/tests/test_quantizer.cpp +239 -0
- sovereign_inference-0.1.0/third_party/.gitkeep +9 -0
- sovereign_inference-0.1.0/third_party/volk/volk.c +4248 -0
- sovereign_inference-0.1.0/third_party/volk/volk.h +3465 -0
- sovereign_inference-0.1.0/tools/converter/main.cpp +726 -0
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
# ─── Build outputs ────────────────────────────────────────────────────────────
|
|
2
|
+
build/
|
|
3
|
+
build-*/
|
|
4
|
+
cmake-build-*/
|
|
5
|
+
out/
|
|
6
|
+
dist/
|
|
7
|
+
install/
|
|
8
|
+
_install/
|
|
9
|
+
|
|
10
|
+
# ─── CMake generated ──────────────────────────────────────────────────────────
|
|
11
|
+
CMakeCache.txt
|
|
12
|
+
CMakeFiles/
|
|
13
|
+
cmake_install.cmake
|
|
14
|
+
CTestTestfile.cmake
|
|
15
|
+
CPackConfig.cmake
|
|
16
|
+
CPackSourceConfig.cmake
|
|
17
|
+
install_manifest.txt
|
|
18
|
+
compile_commands.json
|
|
19
|
+
*.cmake
|
|
20
|
+
!CMakeLists.txt
|
|
21
|
+
!cmake/*.cmake
|
|
22
|
+
|
|
23
|
+
# ─── SPIR-V compiled shaders ──────────────────────────────────────────────────
|
|
24
|
+
shaders/*.spv
|
|
25
|
+
*.spv
|
|
26
|
+
!shaders/compiled/*.spv
|
|
27
|
+
|
|
28
|
+
# ─── Python ───────────────────────────────────────────────────────────────────
|
|
29
|
+
__pycache__/
|
|
30
|
+
*.py[cod]
|
|
31
|
+
*$py.class
|
|
32
|
+
*.egg
|
|
33
|
+
*.egg-info/
|
|
34
|
+
dist/
|
|
35
|
+
.eggs/
|
|
36
|
+
.Python
|
|
37
|
+
pip-log.txt
|
|
38
|
+
pip-delete-this-directory.txt
|
|
39
|
+
.tox/
|
|
40
|
+
.nox/
|
|
41
|
+
.coverage
|
|
42
|
+
htmlcov/
|
|
43
|
+
.pytest_cache/
|
|
44
|
+
.mypy_cache/
|
|
45
|
+
*.pyd
|
|
46
|
+
*.so
|
|
47
|
+
*.dylib
|
|
48
|
+
venv/
|
|
49
|
+
.venv/
|
|
50
|
+
env/
|
|
51
|
+
.env/
|
|
52
|
+
ENV/
|
|
53
|
+
|
|
54
|
+
# ─── Compiled C/C++ objects & binaries ────────────────────────────────────────
|
|
55
|
+
*.o
|
|
56
|
+
*.a
|
|
57
|
+
*.la
|
|
58
|
+
*.lo
|
|
59
|
+
*.ko
|
|
60
|
+
*.obj
|
|
61
|
+
*.elf
|
|
62
|
+
*.exe
|
|
63
|
+
*.dll
|
|
64
|
+
*.lib
|
|
65
|
+
*.pdb
|
|
66
|
+
*.ilk
|
|
67
|
+
*.exp
|
|
68
|
+
|
|
69
|
+
# ─── Model files (often large, not for VCS) ───────────────────────────────────
|
|
70
|
+
*.sovereign
|
|
71
|
+
*.gguf
|
|
72
|
+
*.ggml
|
|
73
|
+
*.safetensors
|
|
74
|
+
*.bin
|
|
75
|
+
*.pt
|
|
76
|
+
*.pth
|
|
77
|
+
*.ckpt
|
|
78
|
+
|
|
79
|
+
# ─── Third-party fetched headers ──────────────────────────────────────────────
|
|
80
|
+
# vk_mem_alloc.h is fetched by scripts/build.sh
|
|
81
|
+
third_party/vk_mem_alloc.h
|
|
82
|
+
|
|
83
|
+
# Keep the directory itself
|
|
84
|
+
!third_party/.gitkeep
|
|
85
|
+
|
|
86
|
+
# ─── Vulkan pipeline cache ────────────────────────────────────────────────────
|
|
87
|
+
*.sovereign_pipeline_cache.bin
|
|
88
|
+
.sovereign_pipeline_cache.bin
|
|
89
|
+
|
|
90
|
+
# ─── IDE and editor files ─────────────────────────────────────────────────────
|
|
91
|
+
# CLion / JetBrains
|
|
92
|
+
.idea/
|
|
93
|
+
*.iml
|
|
94
|
+
|
|
95
|
+
# VS Code
|
|
96
|
+
.vscode/
|
|
97
|
+
!.vscode/extensions.json
|
|
98
|
+
!.vscode/settings.json.example
|
|
99
|
+
|
|
100
|
+
# Visual Studio
|
|
101
|
+
*.sln
|
|
102
|
+
*.vcxproj
|
|
103
|
+
*.vcxproj.filters
|
|
104
|
+
*.user
|
|
105
|
+
*.suo
|
|
106
|
+
*.ncb
|
|
107
|
+
*.opensdf
|
|
108
|
+
*.sdf
|
|
109
|
+
x64/
|
|
110
|
+
x86/
|
|
111
|
+
Debug/
|
|
112
|
+
Release/
|
|
113
|
+
|
|
114
|
+
# Vim
|
|
115
|
+
*.swp
|
|
116
|
+
*.swo
|
|
117
|
+
*~
|
|
118
|
+
tags
|
|
119
|
+
.tags
|
|
120
|
+
|
|
121
|
+
# Emacs
|
|
122
|
+
*#
|
|
123
|
+
.#*
|
|
124
|
+
\#*#
|
|
125
|
+
|
|
126
|
+
# macOS
|
|
127
|
+
.DS_Store
|
|
128
|
+
.AppleDouble
|
|
129
|
+
.LSOverride
|
|
130
|
+
._*
|
|
131
|
+
|
|
132
|
+
# Linux
|
|
133
|
+
*~
|
|
134
|
+
|
|
135
|
+
# Windows
|
|
136
|
+
Thumbs.db
|
|
137
|
+
Desktop.ini
|
|
138
|
+
|
|
139
|
+
# ─── Logs and test artefacts ──────────────────────────────────────────────────
|
|
140
|
+
*.log
|
|
141
|
+
*.tmp
|
|
142
|
+
/Testing/
|
|
143
|
+
LastTest.log
|
|
144
|
+
|
|
145
|
+
# ─── Profiling ────────────────────────────────────────────────────────────────
|
|
146
|
+
gmon.out
|
|
147
|
+
*.prof
|
|
148
|
+
callgrind.*
|
|
149
|
+
massif.*
|
|
150
|
+
cachegrind.*
|
|
151
|
+
perf.data
|
|
152
|
+
perf.data.old
|
|
153
|
+
|
|
154
|
+
# ─── ASAN / TSAN / UBSAN reports ─────────────────────────────────────────────
|
|
155
|
+
*.asan.log
|
|
156
|
+
*.tsan.log
|
|
157
|
+
|
|
158
|
+
# ─── Calibration data (typically private) ────────────────────────────────────
|
|
159
|
+
calibration/
|
|
160
|
+
*.calib
|
|
161
|
+
|
|
162
|
+
# ─── Documentation generated ─────────────────────────────────────────────────
|
|
163
|
+
docs/html/
|
|
164
|
+
docs/latex/
|
|
165
|
+
docs/xml/
|
|
166
|
+
Doxyfile.bak
|
|
167
|
+
|
|
168
|
+
# ─── JavaScript/Shader Compiler Tooling ───────────────────────────────────────
|
|
169
|
+
node_modules/
|
|
170
|
+
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
# Building & Testing Sovereign Engine
|
|
2
|
+
|
|
3
|
+
Ce guide couvre le cycle complet :
|
|
4
|
+
1. Compiler la librairie C++ + le module Python
|
|
5
|
+
2. Installer le module via `pip`
|
|
6
|
+
3. Convertir un modèle `.safetensors` en `.sovereign`
|
|
7
|
+
4. Tester le tout avec le script Python fourni
|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
## Prérequis & Système de Build Sans Dépendance (SDK-Free)
|
|
12
|
+
|
|
13
|
+
Grâce à notre intégration du méta-chargeur **Volk** pour le chargement dynamique de Vulkan à l'exécution et au téléchargement automatique des en-têtes via CMake (`FetchContent`), **le Vulkan SDK est totalement facultatif** pour compiler le projet.
|
|
14
|
+
|
|
15
|
+
| Outil | Version | Obligatoire ? | Rôle |
|
|
16
|
+
|---|---|---|---|
|
|
17
|
+
| **CMake** | ≥ 3.25 | **Oui** | Orchestrateur de build principal |
|
|
18
|
+
| **Compilateur C++20** | MSVC 2022 / GCC 12 / Clang 15 | **Oui** | Support C++20 complet exigé |
|
|
19
|
+
| **Vulkan SDK** | ≥ 1.3 (inclut `glslc`) | **Non** | Utilisé uniquement si présent pour recompiler les shaders |
|
|
20
|
+
| **Python** | ≥ 3.9 | **Optionnel** | Requis uniquement pour les liaisons Python (pybind11) |
|
|
21
|
+
| **pip** | ≥ 21 | **Optionnel** | Installation des dépendances et du module local |
|
|
22
|
+
|
|
23
|
+
### Gestion des Shaders (GLSL -> SPIR-V)
|
|
24
|
+
Le projet contient un système de compilation double-mode intelligent :
|
|
25
|
+
1. **Mode Standard (glslc présent)** : Si le compilateur de shaders `glslc` (fourni avec le SDK Vulkan) est détecté sur votre système, CMake recompile automatiquement tous les fichiers `.comp` du répertoire `shaders/` en fichiers `.spv`.
|
|
26
|
+
2. **Mode Fallback (Pas de glslc)** : Si aucun compilateur n'est présent, le système utilise automatiquement et de manière totalement transparente les binaires SPIR-V précompilés inclus dans `shaders/compiled/`.
|
|
27
|
+
|
|
28
|
+
### Outils de Compilation Shader JavaScript alternatifs (Optionnel)
|
|
29
|
+
Si vous modifiez des shaders mais n'avez pas installé le SDK Vulkan, vous pouvez utiliser notre outil basé sur Node.js et WebGPU (`@webgpu/glslang`) :
|
|
30
|
+
```bash
|
|
31
|
+
# Installer les dépendances JS locales
|
|
32
|
+
npm install
|
|
33
|
+
|
|
34
|
+
# Lancer la compilation des shaders compute
|
|
35
|
+
node scripts/compile_shaders.js
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
---
|
|
39
|
+
|
|
40
|
+
## Option A — Build manuel (CMake)
|
|
41
|
+
|
|
42
|
+
```powershell
|
|
43
|
+
# 1. Configurer
|
|
44
|
+
cmake -B build -S . `
|
|
45
|
+
-DSOVEREIGN_BUILD_PYTHON=ON `
|
|
46
|
+
-DSOVEREIGN_BUILD_TESTS=ON `
|
|
47
|
+
-DCMAKE_BUILD_TYPE=Release
|
|
48
|
+
|
|
49
|
+
# 2. Compiler (tout : lib + shaders + bindings + tests + converter)
|
|
50
|
+
cmake --build build --parallel
|
|
51
|
+
|
|
52
|
+
# 3. Installer le module Python en mode éditable
|
|
53
|
+
pip install -e build/
|
|
54
|
+
|
|
55
|
+
# ─── Optionnel : compiler en Debug avec AddressSanitizer ───────────────
|
|
56
|
+
cmake -B build-debug -S . `
|
|
57
|
+
-DSOVEREIGN_BUILD_TESTS=ON `
|
|
58
|
+
-DSOVEREIGN_ENABLE_ASAN=ON `
|
|
59
|
+
-DCMAKE_BUILD_TYPE=Debug
|
|
60
|
+
cmake --build build-debug --parallel
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
---
|
|
64
|
+
|
|
65
|
+
## Option B — Script helper (Linux / WSL / Git Bash)
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
# Release + tests + Python bindings
|
|
69
|
+
bash scripts/build.sh
|
|
70
|
+
|
|
71
|
+
# Debug avec ASAN
|
|
72
|
+
bash scripts/build.sh --debug
|
|
73
|
+
|
|
74
|
+
# Sans bindings Python
|
|
75
|
+
bash scripts/build.sh --no-python
|
|
76
|
+
|
|
77
|
+
# Clean build
|
|
78
|
+
bash scripts/build.sh --clean
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
---
|
|
82
|
+
|
|
83
|
+
## Option C — pip install (via scikit-build-core)
|
|
84
|
+
|
|
85
|
+
```powershell
|
|
86
|
+
# Installe scikit-build-core d'abord
|
|
87
|
+
pip install scikit-build-core pybind11
|
|
88
|
+
|
|
89
|
+
# Installer sovereign-inference depuis le repo local
|
|
90
|
+
pip install .
|
|
91
|
+
|
|
92
|
+
# Ou en mode éditable (rebuild automatique à chaque changement)
|
|
93
|
+
pip install --no-build-isolation -e .
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
---
|
|
97
|
+
|
|
98
|
+
## Lancer les tests unitaires C++
|
|
99
|
+
|
|
100
|
+
```powershell
|
|
101
|
+
cd build
|
|
102
|
+
ctest --output-on-failure --parallel 4
|
|
103
|
+
|
|
104
|
+
# Ou directement :
|
|
105
|
+
.\test_format.exe --success
|
|
106
|
+
.\test_quantizer.exe --success
|
|
107
|
+
.\test_kv_cache.exe --success
|
|
108
|
+
.\test_engine.exe --success
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
---
|
|
112
|
+
|
|
113
|
+
## Convertir un modèle SafeTensors → .sovereign
|
|
114
|
+
|
|
115
|
+
```powershell
|
|
116
|
+
# Depuis le dossier build/
|
|
117
|
+
.\sovereign-convert.exe `
|
|
118
|
+
--input C:\models\gemma-4b\ `
|
|
119
|
+
--output gemma-4b.sovereign `
|
|
120
|
+
--arch gemma `
|
|
121
|
+
--quant mixed `
|
|
122
|
+
--bpw 4.5 `
|
|
123
|
+
--verbose
|
|
124
|
+
|
|
125
|
+
# Avec corpus de calibration (meilleure qualité) :
|
|
126
|
+
.\sovereign-convert.exe `
|
|
127
|
+
--input C:\models\gemma-4b\ `
|
|
128
|
+
--output gemma-4b-calibrated.sovereign `
|
|
129
|
+
--quant mixed `
|
|
130
|
+
--bpw 4.5 `
|
|
131
|
+
--calib calibration_corpus.txt
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
**Modes de quantisation disponibles :**
|
|
135
|
+
|
|
136
|
+
| Mode | ~bpw | Description |
|
|
137
|
+
|---|---|---|
|
|
138
|
+
| `fp16` | 16 | Aucune quantisation |
|
|
139
|
+
| `int8` | 8 | INT8 uniforme |
|
|
140
|
+
| `q4k` | 4.5 | Q4_K blocks |
|
|
141
|
+
| `q3k` | 3.5 | Q3_K blocks |
|
|
142
|
+
| `q2k` | 2.6 | Q2_K agressif |
|
|
143
|
+
| `mixed` | cible | Adaptatif par tenseur (recommandé) |
|
|
144
|
+
|
|
145
|
+
---
|
|
146
|
+
|
|
147
|
+
## Test pipeline complet en Python
|
|
148
|
+
|
|
149
|
+
```powershell
|
|
150
|
+
# ── Test API uniquement (sans GPU, sans modèle) ──────────────────────
|
|
151
|
+
python examples/test_pipeline.py --self-test
|
|
152
|
+
|
|
153
|
+
# ── Pipeline complet : convert + validate + inférence ────────────────
|
|
154
|
+
python examples/test_pipeline.py `
|
|
155
|
+
--model-dir C:\models\gemma-4b\ `
|
|
156
|
+
--output gemma-4b.sovereign `
|
|
157
|
+
--arch gemma `
|
|
158
|
+
--bpw 4.5 `
|
|
159
|
+
--prompt "Explique brièvement l'intrication quantique :"
|
|
160
|
+
|
|
161
|
+
# ── Charger un .sovereign existant directement ───────────────────────
|
|
162
|
+
python examples/test_pipeline.py `
|
|
163
|
+
--sovereign gemma-4b.sovereign `
|
|
164
|
+
--prompt "Once upon a time" `
|
|
165
|
+
--max-tokens 256 `
|
|
166
|
+
--temperature 0.8
|
|
167
|
+
|
|
168
|
+
# ── Générer en Python pur ────────────────────────────────────────────
|
|
169
|
+
python examples/basic_generate.py `
|
|
170
|
+
--model gemma-4b.sovereign `
|
|
171
|
+
--prompt "Explain transformers in ML:" `
|
|
172
|
+
--temperature 0.7 `
|
|
173
|
+
--max-tokens 512
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
---
|
|
177
|
+
|
|
178
|
+
## Workflow de développement (push + versioning)
|
|
179
|
+
|
|
180
|
+
```powershell
|
|
181
|
+
# Après chaque modification :
|
|
182
|
+
git add -A
|
|
183
|
+
git commit -m "feat: description courte"
|
|
184
|
+
|
|
185
|
+
# Nouvelle version mineure :
|
|
186
|
+
git tag v0.2.0 -m "v0.2.0 - description"
|
|
187
|
+
git push origin main --tags
|
|
188
|
+
|
|
189
|
+
# Rebuildez le wheel Python après les tags :
|
|
190
|
+
pip install .
|
|
191
|
+
python -c "import sovereign_inference; print(sovereign_inference.version())"
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
---
|
|
195
|
+
|
|
196
|
+
## Structure des sorties du build
|
|
197
|
+
|
|
198
|
+
```
|
|
199
|
+
build/
|
|
200
|
+
├── sovereign-convert(.exe) # CLI de conversion
|
|
201
|
+
├── sovereign_inference(.pyd/.so) # Module Python pybind11
|
|
202
|
+
├── sovereign_core.lib/.a # Librairie statique C++
|
|
203
|
+
├── shaders/ # SPIR-V compilés
|
|
204
|
+
│ ├── rmsnorm.spv
|
|
205
|
+
│ ├── matmul_int4.spv
|
|
206
|
+
│ ├── attention_gqa.spv
|
|
207
|
+
│ ├── silu_gate.spv
|
|
208
|
+
│ └── sampler.spv
|
|
209
|
+
├── test_format(.exe) # Binaires de test
|
|
210
|
+
├── test_quantizer(.exe)
|
|
211
|
+
├── test_kv_cache(.exe)
|
|
212
|
+
└── test_engine(.exe)
|
|
213
|
+
```
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
cmake_minimum_required(VERSION 3.25)
|
|
2
|
+
project(sovereign-engine LANGUAGES CXX)
|
|
3
|
+
|
|
4
|
+
set(CMAKE_CXX_STANDARD 20)
|
|
5
|
+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
|
6
|
+
|
|
7
|
+
# ─── Options ──────────────────────────────────────────────────────────────────
|
|
8
|
+
option(SOVEREIGN_BUILD_TESTS "Build tests" ON)
|
|
9
|
+
option(SOVEREIGN_BUILD_PYTHON "Build Python bindings" ON)
|
|
10
|
+
option(SOVEREIGN_ENABLE_AVX512 "Enable AVX-512 fallback kernels" ON)
|
|
11
|
+
option(SOVEREIGN_ENABLE_ASAN "Enable AddressSanitizer" OFF)
|
|
12
|
+
|
|
13
|
+
# ─── Compiler Flags & AddressSanitizer ────────────────────────────────────────
|
|
14
|
+
if(MSVC)
|
|
15
|
+
add_compile_options(/W4)
|
|
16
|
+
if(SOVEREIGN_ENABLE_ASAN)
|
|
17
|
+
add_compile_options(/fsanitize=address)
|
|
18
|
+
add_link_options(/fsanitize=address)
|
|
19
|
+
endif()
|
|
20
|
+
else()
|
|
21
|
+
add_compile_options(-Wall -Wextra -Wno-unused-parameter)
|
|
22
|
+
if(SOVEREIGN_ENABLE_ASAN)
|
|
23
|
+
add_compile_options(-fsanitize=address -fno-omit-frame-pointer)
|
|
24
|
+
add_link_options(-fsanitize=address)
|
|
25
|
+
endif()
|
|
26
|
+
endif()
|
|
27
|
+
|
|
28
|
+
# ─── Global Compile Definitions ───────────────────────────────────────────────
|
|
29
|
+
add_compile_definitions(
|
|
30
|
+
SOVEREIGN_VERSION_MAJOR=0
|
|
31
|
+
SOVEREIGN_VERSION_MINOR=1
|
|
32
|
+
SOVEREIGN_VERSION_PATCH=0
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
if(WIN32)
|
|
36
|
+
add_compile_definitions(WIN32_LEAN_AND_MEAN NOMINMAX)
|
|
37
|
+
endif()
|
|
38
|
+
|
|
39
|
+
if(SOVEREIGN_ENABLE_AVX512)
|
|
40
|
+
add_compile_definitions(SOVEREIGN_ENABLE_AVX512=1)
|
|
41
|
+
endif()
|
|
42
|
+
|
|
43
|
+
# ─── Dependencies ─────────────────────────────────────────────────────────────
|
|
44
|
+
find_package(Vulkan)
|
|
45
|
+
|
|
46
|
+
include(FetchContent)
|
|
47
|
+
|
|
48
|
+
if(NOT Vulkan_FOUND)
|
|
49
|
+
message(STATUS "Vulkan SDK not found. Setting up SDK-free build with Vulkan-Headers...")
|
|
50
|
+
FetchContent_Declare(
|
|
51
|
+
vulkan_headers
|
|
52
|
+
GIT_REPOSITORY https://github.com/KhronosGroup/Vulkan-Headers.git
|
|
53
|
+
GIT_TAG v1.3.283
|
|
54
|
+
GIT_SHALLOW ON
|
|
55
|
+
)
|
|
56
|
+
FetchContent_MakeAvailable(vulkan_headers)
|
|
57
|
+
|
|
58
|
+
add_library(Vulkan::Vulkan INTERFACE IMPORTED)
|
|
59
|
+
target_link_libraries(Vulkan::Vulkan INTERFACE Vulkan::Headers)
|
|
60
|
+
if(UNIX AND NOT APPLE)
|
|
61
|
+
target_link_libraries(Vulkan::Vulkan INTERFACE ${CMAKE_DL_LIBS})
|
|
62
|
+
endif()
|
|
63
|
+
endif()
|
|
64
|
+
|
|
65
|
+
if(NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/third_party/vk_mem_alloc.h")
|
|
66
|
+
message(STATUS "vk_mem_alloc.h not found. Downloading from GPUOpen GitHub...")
|
|
67
|
+
file(DOWNLOAD
|
|
68
|
+
"https://raw.githubusercontent.com/GPUOpen-LibrariesAndSDKs/VulkanMemoryAllocator/master/include/vk_mem_alloc.h"
|
|
69
|
+
"${CMAKE_CURRENT_SOURCE_DIR}/third_party/vk_mem_alloc.h"
|
|
70
|
+
SHOW_PROGRESS
|
|
71
|
+
STATUS download_status
|
|
72
|
+
)
|
|
73
|
+
list(GET download_status 0 status_code)
|
|
74
|
+
if(NOT status_code EQUAL 0)
|
|
75
|
+
message(FATAL_ERROR "Failed to download vk_mem_alloc.h: ${download_status}")
|
|
76
|
+
endif()
|
|
77
|
+
endif()
|
|
78
|
+
|
|
79
|
+
# ─── Shader Compilation (GLSL -> SPIR-V) ──────────────────────────────────────
|
|
80
|
+
find_program(GLSLC_EXECUTABLE glslc)
|
|
81
|
+
|
|
82
|
+
set(SHADER_SPV_DIR "${CMAKE_BINARY_DIR}/shaders")
|
|
83
|
+
file(MAKE_DIRECTORY "${SHADER_SPV_DIR}")
|
|
84
|
+
|
|
85
|
+
set(SHADER_SOURCES
|
|
86
|
+
shaders/rmsnorm.comp
|
|
87
|
+
shaders/matmul_int4.comp
|
|
88
|
+
shaders/attention_gqa.comp
|
|
89
|
+
shaders/silu_gate.comp
|
|
90
|
+
shaders/sampler.comp
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
if(GLSLC_EXECUTABLE)
|
|
94
|
+
message(STATUS "glslc compiler found at: ${GLSLC_EXECUTABLE}. Recompiling GLSL shaders...")
|
|
95
|
+
foreach(SHADER_SRC ${SHADER_SOURCES})
|
|
96
|
+
get_filename_component(SHADER_NAME ${SHADER_SRC} NAME_WE)
|
|
97
|
+
set(SPV_FILE "${SHADER_SPV_DIR}/${SHADER_NAME}.spv")
|
|
98
|
+
add_custom_command(
|
|
99
|
+
OUTPUT ${SPV_FILE}
|
|
100
|
+
COMMAND ${GLSLC_EXECUTABLE} -O ${CMAKE_CURRENT_SOURCE_DIR}/${SHADER_SRC} -o ${SPV_FILE}
|
|
101
|
+
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${SHADER_SRC}
|
|
102
|
+
COMMENT "Compiling shader ${SHADER_SRC} to SPIR-V"
|
|
103
|
+
)
|
|
104
|
+
list(APPEND SHADER_SPV_FILES ${SPV_FILE})
|
|
105
|
+
endforeach()
|
|
106
|
+
else()
|
|
107
|
+
message(STATUS "glslc compiler not found. Using precompiled SPIR-V shaders from shaders/compiled/ ...")
|
|
108
|
+
foreach(SHADER_SRC ${SHADER_SOURCES})
|
|
109
|
+
get_filename_component(SHADER_NAME ${SHADER_SRC} NAME_WE)
|
|
110
|
+
set(SPV_FILE "${SHADER_SPV_DIR}/${SHADER_NAME}.spv")
|
|
111
|
+
add_custom_command(
|
|
112
|
+
OUTPUT ${SPV_FILE}
|
|
113
|
+
COMMAND ${CMAKE_COMMAND} -E copy
|
|
114
|
+
${CMAKE_CURRENT_SOURCE_DIR}/shaders/compiled/${SHADER_NAME}.spv
|
|
115
|
+
${SPV_FILE}
|
|
116
|
+
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/shaders/compiled/${SHADER_NAME}.spv
|
|
117
|
+
COMMENT "Copying precompiled shader ${SHADER_NAME}.spv"
|
|
118
|
+
)
|
|
119
|
+
list(APPEND SHADER_SPV_FILES ${SPV_FILE})
|
|
120
|
+
endforeach()
|
|
121
|
+
endif()
|
|
122
|
+
|
|
123
|
+
add_custom_target(compile_shaders DEPENDS ${SHADER_SPV_FILES})
|
|
124
|
+
|
|
125
|
+
# ─── Sovereign Core Library ───────────────────────────────────────────────────
|
|
126
|
+
add_library(sovereign_core STATIC
|
|
127
|
+
src/vulkan/vulkan_context.cpp
|
|
128
|
+
src/format/format.cpp
|
|
129
|
+
src/compute/kv_cache.cpp
|
|
130
|
+
src/inference/engine.cpp
|
|
131
|
+
src/quantizer/quantizer.cpp
|
|
132
|
+
src/memory/memory_manager.cpp
|
|
133
|
+
third_party/volk/volk.c
|
|
134
|
+
)
|
|
135
|
+
|
|
136
|
+
target_include_directories(sovereign_core PUBLIC
|
|
137
|
+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
|
|
138
|
+
$<INSTALL_INTERFACE:include>
|
|
139
|
+
${CMAKE_CURRENT_SOURCE_DIR}/third_party
|
|
140
|
+
)
|
|
141
|
+
|
|
142
|
+
target_link_libraries(sovereign_core PUBLIC Vulkan::Vulkan)
|
|
143
|
+
|
|
144
|
+
# Add dependencies for shader outputs
|
|
145
|
+
add_dependencies(sovereign_core compile_shaders)
|
|
146
|
+
|
|
147
|
+
# Define shader directory path macro (normalised for Windows/C++ path string compatibility)
|
|
148
|
+
string(REPLACE "\\" "/" SHADER_SPV_DIR_NORM "${SHADER_SPV_DIR}")
|
|
149
|
+
target_compile_definitions(sovereign_core PRIVATE SOVEREIGN_SHADER_DIR="${SHADER_SPV_DIR_NORM}")
|
|
150
|
+
|
|
151
|
+
# ─── Converter CLI Tool ───────────────────────────────────────────────────────
|
|
152
|
+
add_executable(sovereign-convert
|
|
153
|
+
tools/converter/main.cpp
|
|
154
|
+
)
|
|
155
|
+
target_link_libraries(sovereign-convert PRIVATE sovereign_core)
|
|
156
|
+
|
|
157
|
+
# ─── Python Bindings ──────────────────────────────────────────────────────────
|
|
158
|
+
if(SOVEREIGN_BUILD_PYTHON)
|
|
159
|
+
FetchContent_Declare(
|
|
160
|
+
pybind11
|
|
161
|
+
GIT_REPOSITORY https://github.com/pybind/pybind11.git
|
|
162
|
+
GIT_TAG v2.11.1
|
|
163
|
+
GIT_SHALLOW ON
|
|
164
|
+
)
|
|
165
|
+
FetchContent_MakeAvailable(pybind11)
|
|
166
|
+
|
|
167
|
+
pybind11_add_module(sovereign_inference bindings/python/sovereign_py.cpp)
|
|
168
|
+
target_link_libraries(sovereign_inference PRIVATE sovereign_core)
|
|
169
|
+
install(TARGETS sovereign_inference DESTINATION .)
|
|
170
|
+
endif()
|
|
171
|
+
|
|
172
|
+
# ─── Unit Tests ───────────────────────────────────────────────────────────────
|
|
173
|
+
if(SOVEREIGN_BUILD_TESTS)
|
|
174
|
+
FetchContent_Declare(
|
|
175
|
+
doctest
|
|
176
|
+
GIT_REPOSITORY https://github.com/doctest/doctest.git
|
|
177
|
+
GIT_TAG v2.4.11
|
|
178
|
+
GIT_SHALLOW ON
|
|
179
|
+
)
|
|
180
|
+
FetchContent_MakeAvailable(doctest)
|
|
181
|
+
|
|
182
|
+
enable_testing()
|
|
183
|
+
add_subdirectory(tests)
|
|
184
|
+
endif()
|