YAML Metadata Warning:empty or missing yaml metadata in repo card
Check out the documentation for more information.
snapkitty-mlc
SnapKitty Machine Learning in C โ ๆฉๅจๅญธ็ฟ C ่ช่จๆ ธๅฟๅบซ (ู ูุชุจุฉ ุงูุชุนูู ุงูุขูู ุงูุฃุณุงุณูุฉ ุจูุบุฉ C)
Minimal autograd library with arena allocator, PCG32 PRNG, and MNIST example. Extracted from 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
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
# 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
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)
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)
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)
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)
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_gradfunctions 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 |
| GNU Affero General Public License v3 | AGPL-3.0 | LICENSE-AGPL |
| Mozilla Public License 2.0 | MPL-2.0 | LICENSE-MPL |
Unauthorized cloud SaaS redistribution without source disclosure is prohibited under all three licenses.
SnapKitty West / SNAPKITTYWEST โ Evidence or Silence โ 2026