YAML Metadata Warning:empty or missing yaml metadata in repo card

Check out the documentation for more information.

License: BSL-1.1 License: AGPL-3.0 License: MPL-2.0 C11 CMake FlashAttention Evidence or Silence

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_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
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

Downloads last month

-

Downloads are not tracked for this model. How to track
Inference Providers NEW
This model isn't deployed by any Inference Provider. ๐Ÿ™‹ Ask for provider support

Collection including Snapkitty/snapkitty-mlc

Paper for Snapkitty/snapkitty-mlc