snapkitty
machine-learning
c
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)

[![License: BSL-1.1](https://img.shields.io/badge/License-BSL--1.1-blue.svg)](https://opensource.org/licenses/BSL-1.0)
[![License: AGPL-3.0](https://img.shields.io/badge/License-AGPL--3.0-purple.svg)](https://www.gnu.org/licenses/agpl-3.0)
[![License: MPL-2.0](https://img.shields.io/badge/License-MPL--2.0-orange.svg)](https://www.mozilla.org/en-US/MPL/2.0/)
[![C11](https://img.shields.io/badge/C-C11-00599C.svg)](https://en.cppreference.com/w/c/11)
[![CMake](https://img.shields.io/badge/CMake-%E2%89%A53.16-064F8C.svg)](https://cmake.org/)
[![FlashAttention](https://img.shields.io/badge/FlashAttention-Dao%202022-green.svg)](https://arxiv.org/abs/2205.14135)
[![Evidence or Silence](https://img.shields.io/badge/Protocol-Evidence%20or%20Silence-black.svg)](#)

# 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