File size: 7,018 Bytes
c2f486e d778433 c2f486e 7d8730f 8b41169 7d8730f 8b41169 7d8730f 8b41169 7d8730f 8b41169 7d8730f 8b41169 7d8730f 8b41169 7d8730f 8b41169 7d8730f 8b41169 7d8730f 8b41169 7d8730f 8b41169 7d8730f 8b41169 7d8730f 8b41169 7d8730f 8b41169 7d8730f 8b41169 7d8730f 8b41169 c2f486e | 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 | ---
license: agpl-3.0
tags:
- snapkitty
- machine-learning
- c
---
> Source: [github.com/SNAPKITTYWEST/snapkitty-mlc](https://github.com/SNAPKITTYWEST/snapkitty-mlc)
[](https://opensource.org/licenses/BSL-1.0)
[](https://www.gnu.org/licenses/agpl-3.0)
[](https://www.mozilla.org/en-US/MPL/2.0/)
[](https://en.cppreference.com/w/c/11)
[](https://cmake.org/)
[](https://arxiv.org/abs/2205.14135)
[](#)
# snapkitty-mlc
**SnapKitty Machine Learning in C** โ ๆฉๅจๅญธ็ฟ C ่ช่จๆ ธๅฟๅบซ (ู
ูุชุจุฉ ุงูุชุนูู
ุงูุขูู ุงูุฃุณุงุณูุฉ ุจูุบุฉ C)
Minimal autograd library with arena allocator, PCG32 PRNG, and MNIST example. Extracted from [SNAPKITTYAGENT9NOVA/MLC](https://github.com/SNAPKITTYAGENT9NOVA/MLC) and extended with FlashAttention (Dao et al. 2022).
Authors: Ahmad Ali Parr, Jessica L. Williams (SNAPKITTYWEST)
---
## Modules
| Module | Description | ๆ่ฟฐ (ุงููุตู) |
|--------|-------------|--------------|
| `sk_arena` | Virtual-memory arena allocator โ Win32 VirtualAlloc + Linux mmap; 2 thread-local scratch pools | ่ๆฌ่จๆถ้ซๅ้
ๅจ (ู
ุฎุตุต ุงูุฐุงูุฑุฉ ุงูุงูุชุฑุงุถูุฉ) |
| `sk_random` | PCG32 (O'Neill 2014) โ reentrant `_r` variants, better statistical properties than `rand()` | ๅฝ้จๆฉๆธ็ๆๅจ (ู
ููุฏ ุงูุฃุฑูุงู
ุงูุนุดูุงุฆูุฉ ุงูุฒุงุฆูุฉ) |
| `sk_matrix` | Row-major f32 matrix โ all 4 matmul transpose variants, ReLU/Softmax/CrossEntropy + gradient accumulation | ็ฉ้ฃ้็ฎ + ๆขฏๅบฆ็ดฏ็ฉ (ุนู
ููุงุช ุงูู
ุตูููุงุช + ุชุฑุงูู
ุงูุชุฏุฑุฌ) |
| `sk_model` | Computation graph โ iterative DFS topological sort, reverse-mode autograd, mini-batch SGD with Fisher-Yates shuffle | ่จ็ฎๅ + ่ชๅๅพฎๅ (ุงูุฑุณู
ุงูุจูุงูู ุงูุญุณุงุจู + ุงูุชูุงุถู ุงูุชููุงุฆู) |
---
## Repository Structure
```
snapkitty-mlc/
โโโ include/
โ โโโ sk_defs.h Type aliases (i8/u8/f32โฆ), KiB/MiB/GiB, MIN/MAX
โ โโโ sk_arena.h Virtual-memory arena allocator (Win32 + POSIX)
โ โโโ sk_random.h PCG32 PRNG (O'Neill 2014) โ reentrant + global
โ โโโ sk_matrix.h Row-major f32 matrix: create/fill/matmul/relu/softmax/xent
โ โโโ sk_model.h Computation graph, autograd, mini-batch SGD
โโโ src/
โ โโโ sk_arena.c Arena implementation (VirtualAlloc / mmap)
โ โโโ sk_random.c PCG32 implementation
โ โโโ sk_matrix.c All matrix ops + gradient accumulation
โ โโโ sk_model.c Graph build, topological sort, forward/backward, train loop
โโโ examples/
โ โโโ mnist.c 784โ16 ReLU โ 16+skip ReLU โ 10 Softmax
โ โโโ flash_attention_golden.py FlashAttention Alg 1 (Dao et al. 2022)
โ โโโ data_convert.py MNIST โ binary .mat
โโโ CMakeLists.txt
```
---
## Build
```bash
cmake -S . -B build
cmake --build build
# โ build/libsk_mlc.a build/mnist
```
**Requirements:** C11 compiler (GCC / Clang / MSVC), CMake โฅ 3.16. No external dependencies.
---
## MNIST Example
```bash
# 1. Generate data (once)
pip install tensorflow-datasets numpy
python examples/data_convert.py
# 2. Train
./build/mnist
```
Architecture: `784 โ 16 (ReLU) โ 16+skip (ReLU) โ 10 (Softmax+CrossEntropy)`
Expected: ~97% test accuracy after 10 epochs (SGD lr=0.01, batch=50).
---
## FlashAttention Verification
```bash
python3 examples/flash_attention_golden.py
# ALL TESTS PASSED โ max error 2ร10โปยนยฒ
```
All 4 test configurations pass against Algorithm 1 from Dao et al. 2022.
---
## API Reference
### Arena Allocator (`sk_arena.h`)
```c
sk_arena* sk_arena_create(u64 reserve_size, u64 commit_size);
void sk_arena_destroy(sk_arena* arena);
void* sk_arena_push(sk_arena* arena, u64 size, b32 non_zero);
void sk_arena_clear(sk_arena* arena);
SK_PUSH_STRUCT(arena, T) // allocate one T, zeroed
SK_PUSH_ARRAY(arena, T, n) // allocate n ร T, zeroed
```
### PRNG (`sk_random.h`)
```c
void sk_prng_seed(u64 initstate, u64 initseq);
u32 sk_prng_rand(void); // [0, 2^32)
f32 sk_prng_randf(void); // [0, 1)
// Reentrant:
void sk_prng_seed_r(sk_prng* rng, u64 s, u64 seq);
u32 sk_prng_rand_r(sk_prng* rng);
f32 sk_prng_randf_r(sk_prng* rng);
```
### Matrix (`sk_matrix.h`)
```c
sk_matrix* sk_mat_create(sk_arena*, u32 rows, u32 cols);
b32 sk_mat_mul(out, a, b, zero_out, transpose_a, transpose_b);
b32 sk_mat_relu(out, in);
b32 sk_mat_softmax(out, in);
b32 sk_mat_cross_entropy(out, p, q);
```
### Model / Autograd (`sk_model.h`)
```c
sk_model* sk_model_create(sk_arena* arena);
sk_model_var* sk_mv_matmul(arena, model, a, b, flags);
sk_model_var* sk_mv_relu(arena, model, input, flags);
sk_model_var* sk_mv_softmax(arena, model, input, flags);
sk_model_var* sk_mv_cross_entropy(arena, model, p, q, flags);
void sk_model_compile(arena, model); // topological sort
void sk_model_train(model, &desc); // SGD training loop
```
---
## Design Principles
- **No malloc** in the hot path โ all allocation through the arena
- **No external dependencies** โ libc only (`stdio`, `string`, `math`)
- **PCG32** โ better statistical properties than `rand()`, reproducible with seed
- **Iterative DFS** topological sort โ forward/backward are simple array loops
- **Gradient accumulation** โ `_add_grad` functions accumulate; caller clears per batch
---
## License
This project is released under a **trilicense** model. You may choose any one of the following:
| License | SPDX | Link |
|---------|------|------|
| Boost Software License 1.0 | BSL-1.1 | [LICENSE-BSL](https://opensource.org/licenses/BSL-1.0) |
| GNU Affero General Public License v3 | AGPL-3.0 | [LICENSE-AGPL](https://www.gnu.org/licenses/agpl-3.0) |
| Mozilla Public License 2.0 | MPL-2.0 | [LICENSE-MPL](https://www.mozilla.org/en-US/MPL/2.0/) |
Unauthorized cloud SaaS redistribution without source disclosure is prohibited under all three licenses.
---
SnapKitty West / SNAPKITTYWEST โ Evidence or Silence โ 2026
### ๐ผ Commercial License
Snapkitty code is free and open under **AGPL-3.0** for open-source use. Building a commercial product or service? A **proprietary commercial license** from Snapkitty Collective LLC lets you ship this code without the AGPL's source-sharing and network-use obligations.
**[โ Get a commercial license](mailto:A.parr@belespritdaccord.uk?subject=Commercial%20license:%20snapkitty-mlc)** ยท A.parr@belespritdaccord.uk
|