Mini Sentinel µAI-32: audited release files, source and model card
Browse files- LICENSE +202 -0
- NOTICE +14 -0
- README.md +53 -0
- THIRD_PARTY_NOTICES.md +53 -0
- dist/SHA256SUMS +5 -0
- dist/iris.model +3 -0
- dist/mnist14.model +3 -0
- dist/rings.model +3 -0
- dist/spirals.model +3 -0
- dist/uai32 +0 -0
- refcheck.py +74 -0
- uai32.c +257 -0
- uai32.js +35 -0
LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
Apache License
|
| 3 |
+
Version 2.0, January 2004
|
| 4 |
+
http://www.apache.org/licenses/
|
| 5 |
+
|
| 6 |
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
| 7 |
+
|
| 8 |
+
1. Definitions.
|
| 9 |
+
|
| 10 |
+
"License" shall mean the terms and conditions for use, reproduction,
|
| 11 |
+
and distribution as defined by Sections 1 through 9 of this document.
|
| 12 |
+
|
| 13 |
+
"Licensor" shall mean the copyright owner or entity authorized by
|
| 14 |
+
the copyright owner that is granting the License.
|
| 15 |
+
|
| 16 |
+
"Legal Entity" shall mean the union of the acting entity and all
|
| 17 |
+
other entities that control, are controlled by, or are under common
|
| 18 |
+
control with that entity. For the purposes of this definition,
|
| 19 |
+
"control" means (i) the power, direct or indirect, to cause the
|
| 20 |
+
direction or management of such entity, whether by contract or
|
| 21 |
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
| 22 |
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
| 23 |
+
|
| 24 |
+
"You" (or "Your") shall mean an individual or Legal Entity
|
| 25 |
+
exercising permissions granted by this License.
|
| 26 |
+
|
| 27 |
+
"Source" form shall mean the preferred form for making modifications,
|
| 28 |
+
including but not limited to software source code, documentation
|
| 29 |
+
source, and configuration files.
|
| 30 |
+
|
| 31 |
+
"Object" form shall mean any form resulting from mechanical
|
| 32 |
+
transformation or translation of a Source form, including but
|
| 33 |
+
not limited to compiled object code, generated documentation,
|
| 34 |
+
and conversions to other media types.
|
| 35 |
+
|
| 36 |
+
"Work" shall mean the work of authorship, whether in Source or
|
| 37 |
+
Object form, made available under the License, as indicated by a
|
| 38 |
+
copyright notice that is included in or attached to the work
|
| 39 |
+
(an example is provided in the Appendix below).
|
| 40 |
+
|
| 41 |
+
"Derivative Works" shall mean any work, whether in Source or Object
|
| 42 |
+
form, that is based on (or derived from) the Work and for which the
|
| 43 |
+
editorial revisions, annotations, elaborations, or other modifications
|
| 44 |
+
represent, as a whole, an original work of authorship. For the purposes
|
| 45 |
+
of this License, Derivative Works shall not include works that remain
|
| 46 |
+
separable from, or merely link (or bind by name) to the interfaces of,
|
| 47 |
+
the Work and Derivative Works thereof.
|
| 48 |
+
|
| 49 |
+
"Contribution" shall mean any work of authorship, including
|
| 50 |
+
the original version of the Work and any modifications or additions
|
| 51 |
+
to that Work or Derivative Works thereof, that is intentionally
|
| 52 |
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
| 53 |
+
or by an individual or Legal Entity authorized to submit on behalf of
|
| 54 |
+
the copyright owner. For the purposes of this definition, "submitted"
|
| 55 |
+
means any form of electronic, verbal, or written communication sent
|
| 56 |
+
to the Licensor or its representatives, including but not limited to
|
| 57 |
+
communication on electronic mailing lists, source code control systems,
|
| 58 |
+
and issue tracking systems that are managed by, or on behalf of, the
|
| 59 |
+
Licensor for the purpose of discussing and improving the Work, but
|
| 60 |
+
excluding communication that is conspicuously marked or otherwise
|
| 61 |
+
designated in writing by the copyright owner as "Not a Contribution."
|
| 62 |
+
|
| 63 |
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
| 64 |
+
on behalf of whom a Contribution has been received by Licensor and
|
| 65 |
+
subsequently incorporated within the Work.
|
| 66 |
+
|
| 67 |
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
| 68 |
+
this License, each Contributor hereby grants to You a perpetual,
|
| 69 |
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
| 70 |
+
copyright license to reproduce, prepare Derivative Works of,
|
| 71 |
+
publicly display, publicly perform, sublicense, and distribute the
|
| 72 |
+
Work and such Derivative Works in Source or Object form.
|
| 73 |
+
|
| 74 |
+
3. Grant of Patent License. Subject to the terms and conditions of
|
| 75 |
+
this License, each Contributor hereby grants to You a perpetual,
|
| 76 |
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
| 77 |
+
(except as stated in this section) patent license to make, have made,
|
| 78 |
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
| 79 |
+
where such license applies only to those patent claims licensable
|
| 80 |
+
by such Contributor that are necessarily infringed by their
|
| 81 |
+
Contribution(s) alone or by combination of their Contribution(s)
|
| 82 |
+
with the Work to which such Contribution(s) was submitted. If You
|
| 83 |
+
institute patent litigation against any entity (including a
|
| 84 |
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
| 85 |
+
or a Contribution incorporated within the Work constitutes direct
|
| 86 |
+
or contributory patent infringement, then any patent licenses
|
| 87 |
+
granted to You under this License for that Work shall terminate
|
| 88 |
+
as of the date such litigation is filed.
|
| 89 |
+
|
| 90 |
+
4. Redistribution. You may reproduce and distribute copies of the
|
| 91 |
+
Work or Derivative Works thereof in any medium, with or without
|
| 92 |
+
modifications, and in Source or Object form, provided that You
|
| 93 |
+
meet the following conditions:
|
| 94 |
+
|
| 95 |
+
(a) You must give any other recipients of the Work or
|
| 96 |
+
Derivative Works a copy of this License; and
|
| 97 |
+
|
| 98 |
+
(b) You must cause any modified files to carry prominent notices
|
| 99 |
+
stating that You changed the files; and
|
| 100 |
+
|
| 101 |
+
(c) You must retain, in the Source form of any Derivative Works
|
| 102 |
+
that You distribute, all copyright, patent, trademark, and
|
| 103 |
+
attribution notices from the Source form of the Work,
|
| 104 |
+
excluding those notices that do not pertain to any part of
|
| 105 |
+
the Derivative Works; and
|
| 106 |
+
|
| 107 |
+
(d) If the Work includes a "NOTICE" text file as part of its
|
| 108 |
+
distribution, then any Derivative Works that You distribute must
|
| 109 |
+
include a readable copy of the attribution notices contained
|
| 110 |
+
within such NOTICE file, excluding those notices that do not
|
| 111 |
+
pertain to any part of the Derivative Works, in at least one
|
| 112 |
+
of the following places: within a NOTICE text file distributed
|
| 113 |
+
as part of the Derivative Works; within the Source form or
|
| 114 |
+
documentation, if provided along with the Derivative Works; or,
|
| 115 |
+
within a display generated by the Derivative Works, if and
|
| 116 |
+
wherever such third-party notices normally appear. The contents
|
| 117 |
+
of the NOTICE file are for informational purposes only and
|
| 118 |
+
do not modify the License. You may add Your own attribution
|
| 119 |
+
notices within Derivative Works that You distribute, alongside
|
| 120 |
+
or as an addendum to the NOTICE text from the Work, provided
|
| 121 |
+
that such additional attribution notices cannot be construed
|
| 122 |
+
as modifying the License.
|
| 123 |
+
|
| 124 |
+
You may add Your own copyright statement to Your modifications and
|
| 125 |
+
may provide additional or different license terms and conditions
|
| 126 |
+
for use, reproduction, or distribution of Your modifications, or
|
| 127 |
+
for any such Derivative Works as a whole, provided Your use,
|
| 128 |
+
reproduction, and distribution of the Work otherwise complies with
|
| 129 |
+
the conditions stated in this License.
|
| 130 |
+
|
| 131 |
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
| 132 |
+
any Contribution intentionally submitted for inclusion in the Work
|
| 133 |
+
by You to the Licensor shall be under the terms and conditions of
|
| 134 |
+
this License, without any additional terms or conditions.
|
| 135 |
+
Notwithstanding the above, nothing herein shall supersede or modify
|
| 136 |
+
the terms of any separate license agreement you may have executed
|
| 137 |
+
with Licensor regarding such Contributions.
|
| 138 |
+
|
| 139 |
+
6. Trademarks. This License does not grant permission to use the trade
|
| 140 |
+
names, trademarks, service marks, or product names of the Licensor,
|
| 141 |
+
except as required for reasonable and customary use in describing the
|
| 142 |
+
origin of the Work and reproducing the content of the NOTICE file.
|
| 143 |
+
|
| 144 |
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
| 145 |
+
agreed to in writing, Licensor provides the Work (and each
|
| 146 |
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
| 147 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
| 148 |
+
implied, including, without limitation, any warranties or conditions
|
| 149 |
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
| 150 |
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
| 151 |
+
appropriateness of using or redistributing the Work and assume any
|
| 152 |
+
risks associated with Your exercise of permissions under this License.
|
| 153 |
+
|
| 154 |
+
8. Limitation of Liability. In no event and under no legal theory,
|
| 155 |
+
whether in tort (including negligence), contract, or otherwise,
|
| 156 |
+
unless required by applicable law (such as deliberate and grossly
|
| 157 |
+
negligent acts) or agreed to in writing, shall any Contributor be
|
| 158 |
+
liable to You for damages, including any direct, indirect, special,
|
| 159 |
+
incidental, or consequential damages of any character arising as a
|
| 160 |
+
result of this License or out of the use or inability to use the
|
| 161 |
+
Work (including but not limited to damages for loss of goodwill,
|
| 162 |
+
work stoppage, computer failure or malfunction, or any and all
|
| 163 |
+
other commercial damages or losses), even if such Contributor
|
| 164 |
+
has been advised of the possibility of such damages.
|
| 165 |
+
|
| 166 |
+
9. Accepting Warranty or Additional Liability. While redistributing
|
| 167 |
+
the Work or Derivative Works thereof, You may choose to offer,
|
| 168 |
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
| 169 |
+
or other liability obligations and/or rights consistent with this
|
| 170 |
+
License. However, in accepting such obligations, You may act only
|
| 171 |
+
on Your own behalf and on Your sole responsibility, not on behalf
|
| 172 |
+
of any other Contributor, and only if You agree to indemnify,
|
| 173 |
+
defend, and hold each Contributor harmless for any liability
|
| 174 |
+
incurred by, or claims asserted against, such Contributor by reason
|
| 175 |
+
of your accepting any such warranty or additional liability.
|
| 176 |
+
|
| 177 |
+
END OF TERMS AND CONDITIONS
|
| 178 |
+
|
| 179 |
+
APPENDIX: How to apply the Apache License to your work.
|
| 180 |
+
|
| 181 |
+
To apply the Apache License to your work, attach the following
|
| 182 |
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
| 183 |
+
replaced with your own identifying information. (Don't include
|
| 184 |
+
the brackets!) The text should be enclosed in the appropriate
|
| 185 |
+
comment syntax for the file format. We also recommend that a
|
| 186 |
+
file or class name and description of purpose be included on the
|
| 187 |
+
same "printed page" as the copyright notice for easier
|
| 188 |
+
identification within third-party archives.
|
| 189 |
+
|
| 190 |
+
Copyright [yyyy] [name of copyright owner]
|
| 191 |
+
|
| 192 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 193 |
+
you may not use this file except in compliance with the License.
|
| 194 |
+
You may obtain a copy of the License at
|
| 195 |
+
|
| 196 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 197 |
+
|
| 198 |
+
Unless required by applicable law or agreed to in writing, software
|
| 199 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 200 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 201 |
+
See the License for the specific language governing permissions and
|
| 202 |
+
limitations under the License.
|
NOTICE
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Sentovara µAI-32
|
| 2 |
+
Copyright 2026 Sentovara µAI-32 contributors
|
| 3 |
+
|
| 4 |
+
Audited implementation: 3e924e600ef6b65eff9f00668cb283b259930049
|
| 5 |
+
Project code and release documentation in uai32/ are licensed under the Apache
|
| 6 |
+
License, Version 2.0. See LICENSE. Dataset terms remain separate. Host libraries
|
| 7 |
+
and development tools are not bundled or relicensed.
|
| 8 |
+
|
| 9 |
+
Recorded engineering contributors: OpenAI GPT-5.6 Sol, Anthropic Fable 5.1 and
|
| 10 |
+
OpenAI Codex. Project direction and release stewardship: Sentovara.
|
| 11 |
+
|
| 12 |
+
This publication copy has documentation and privacy redactions listed in
|
| 13 |
+
PUBLICATION_PROVENANCE.json at the repository root. Original audit records are
|
| 14 |
+
retained privately. The executable, model bytes and computational source are unchanged.
|
README.md
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: apache-2.0
|
| 3 |
+
tags:
|
| 4 |
+
- tiny-ml
|
| 5 |
+
- mnist
|
| 6 |
+
- c
|
| 7 |
+
- image-classification
|
| 8 |
+
library_name: other
|
| 9 |
+
pipeline_tag: image-classification
|
| 10 |
+
---
|
| 11 |
+
|
| 12 |
+
# Mini Sentinel µAI-32
|
| 13 |
+
|
| 14 |
+
**A whole AI in 30 KB.** A trainable neural classifier in plain C. The audited x86-64 Linux executable is **9,188 bytes** and its MNIST model is **21,468 bytes**: **30,656 bytes combined**, under a 32,768-byte limit. That pair classifies **9,781 of the 10,000 standard MNIST test digits (97.81%)** after 2×2 pooling to 14×14.
|
| 15 |
+
|
| 16 |
+
**Try it:** [draw a digit in your browser](https://huggingface.co/spaces/Sentovara/Mini-Sentinel-uAI-32-demo).
|
| 17 |
+
|
| 18 |
+
For scale: our [Sentinel S1-32B](https://huggingface.co/Sentovara/Sentinel-S1-32B) has 32 billion parameters. µAI-32's digit model has 9,946.
|
| 19 |
+
|
| 20 |
+
## Files
|
| 21 |
+
|
| 22 |
+
| file | bytes | what it is | held-out result |
|
| 23 |
+
|---|---:|---|---|
|
| 24 |
+
| `dist/uai32` | 9,188 | the program: train, test, predict (x86-64 Linux) | — |
|
| 25 |
+
| `dist/mnist14.model` | 21,468 | digits, 196-48-10, 9,946 parameters | 9,781/10,000 = 97.81% |
|
| 26 |
+
| `dist/spirals.model` | 348 | two spirals, 2-32-2 | 298/300 = 99.3% |
|
| 27 |
+
| `dist/rings.model` | 222 | three rings, 2-16-3 | 300/300 = 100% |
|
| 28 |
+
| `dist/iris.model` | 174 | iris flowers, 4-8-3 | 29/30 = 96.7% (one test row duplicates a training row) |
|
| 29 |
+
| `dist/SHA256SUMS` | | checksums of the five files above | |
|
| 30 |
+
| `uai32.c` | | the full source (257 lines); build files and tests are on GitHub | |
|
| 31 |
+
| `uai32.js` | | the browser demo's forward pass; same answers as the C program on every test set above | |
|
| 32 |
+
| `refcheck.py` | | independent NumPy check of inference and one training step | |
|
| 33 |
+
|
| 34 |
+
## Use
|
| 35 |
+
|
| 36 |
+
```sh
|
| 37 |
+
sha256sum -c dist/SHA256SUMS
|
| 38 |
+
cc -O2 uai32.c -o uai32 -lm # or use dist/uai32 as is; the exact size-optimised build is in the GitHub Makefile
|
| 39 |
+
echo "0.5 -0.2" | ./dist/uai32 predict dist/spirals.model # class, then each class's probability
|
| 40 |
+
./uai32 test mnist14_test.txt dist/mnist14.model # MNIST as 14x14 text rows; see the GitHub repo's get_mnist.py
|
| 41 |
+
```
|
| 42 |
+
|
| 43 |
+
Model file format (little-endian): `u16` magic `0xA132`, `u16` inputs, hidden, outputs; `float32` mean and scale per input; then every weight as bfloat16, hidden layer then output layer (last column is the bias). One hidden ReLU layer, softmax output, trained by SGD.
|
| 44 |
+
|
| 45 |
+
## Evidence and limits
|
| 46 |
+
|
| 47 |
+
The original focused audit (7 Oct 2026) reports PASS on 28/28 mandatory checks, including exact rebuilds, sanitizer runs and a scrambled-label control (47% on spirals, chance 50%). Full evidence, reproduction guide and the "Disprove us" challenge: [GitHub](https://github.com/jonny5isalive5/mini-sentinel-uai32-public).
|
| 48 |
+
|
| 49 |
+
This is a one-hidden-layer classifier, not a language model. The size figure counts the executable and one model file only. It excludes the host C library, operating system, RAM, datasets and tools. MNIST images are not redistributed here.
|
| 50 |
+
|
| 51 |
+
## Credits
|
| 52 |
+
|
| 53 |
+
Project direction and release stewardship: **Sentovara**. Engineering contributors: OpenAI GPT-5.6 Sol, Anthropic Fable 5.1 and OpenAI Codex. Browser demo: Anthropic Claude. Code Apache-2.0; Iris data notice in `THIRD_PARTY_NOTICES.md`.
|
THIRD_PARTY_NOTICES.md
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Licensing and attribution
|
| 2 |
+
|
| 3 |
+
## Project scope
|
| 4 |
+
|
| 5 |
+
The owner selected **Apache-2.0** for the project code and release documentation in `uai32/` during
|
| 6 |
+
release preparation. See [LICENSE](../LICENSE) and [NOTICE](../NOTICE). The license is an added release
|
| 7 |
+
overlay: the original audited commit had no license file. It does not relicense third-party datasets,
|
| 8 |
+
host libraries or development tools.
|
| 9 |
+
The publication evidence retains source and toolchain attributions; personal paths are redacted, with original and publication hashes recorded separately.
|
| 10 |
+
|
| 11 |
+
## Iris
|
| 12 |
+
|
| 13 |
+
R. A. Fisher (1936), *Iris* [Dataset], UCI Machine Learning Repository,
|
| 14 |
+
[DOI 10.24432/C56C76](https://doi.org/10.24432/C56C76).
|
| 15 |
+
The [UCI source](https://archive.ics.uci.edu/dataset/53/iris) identifies the dataset as
|
| 16 |
+
[CC BY 4.0](https://creativecommons.org/licenses/by/4.0/).
|
| 17 |
+
`data/iris.data` is the preserved source data. `gen_data.py` shuffles and converts it into labelled
|
| 18 |
+
120-row training and 30-row test text files. One test row duplicates a training row in the source.
|
| 19 |
+
These transformations and that caveat are retained; the data is not relicensed under Apache-2.0.
|
| 20 |
+
|
| 21 |
+
## MNIST
|
| 22 |
+
|
| 23 |
+
Yann LeCun, Corinna Cortes and Christopher J. C. Burges, *The MNIST database of handwritten digits*.
|
| 24 |
+
See the [historical dataset homepage](http://yann.lecun.com/exdb/mnist/), as linked by the
|
| 25 |
+
[CVDF mirror](https://github.com/cvdfoundation/mnist) and
|
| 26 |
+
[TensorFlow Datasets catalog](https://www.tensorflow.org/datasets/catalog/mnist).
|
| 27 |
+
The homepage was unavailable during the 7 October 2026 documentation review; the mirror remains
|
| 28 |
+
available for dataset provenance and download information.
|
| 29 |
+
The preserved downloader identifies MNIST as CC BY-SA 3.0; this attribution is part of the audited source.
|
| 30 |
+
That source comment is not independent confirmation of the upstream terms.
|
| 31 |
+
|
| 32 |
+
**MNIST licensing note:** This release does not redistribute MNIST images, labels, source archives,
|
| 33 |
+
or generated dataset files. Reproduction requires users to obtain MNIST separately from the documented
|
| 34 |
+
upstream mirrors and comply with whatever terms apply to that dataset. Sentovara does not grant any
|
| 35 |
+
rights in MNIST and does not make an independent claim about its upstream licence.
|
| 36 |
+
|
| 37 |
+
Reproduction downloads the four hash-pinned archives from the documented CVDF/OSSCI mirrors and uses
|
| 38 |
+
2×2 average pooling with rounding to convert 28×28 images into 14×14 integer features.
|
| 39 |
+
Consult the dataset's applicable terms when redistributing dataset material; the project software
|
| 40 |
+
license does not grant additional rights in the data.
|
| 41 |
+
|
| 42 |
+
## Synthetic data, host libraries and verification tools
|
| 43 |
+
|
| 44 |
+
Spirals and rings are generated by this project's seeded `gen_data.py`; their original generated files
|
| 45 |
+
are preserved. The executable dynamically uses the host's C library, math library and dynamic loader.
|
| 46 |
+
Those libraries, GCC, GNU binutils, Python and NumPy are not bundled or relicensed. Their dependency
|
| 47 |
+
roles and exclusions are documented in [REPRODUCE.md](REPRODUCE.md).
|
| 48 |
+
|
| 49 |
+
## Tool-assisted development and checking
|
| 50 |
+
|
| 51 |
+
PR #2 records Claude Code assistance in development. The supplied audit evidence came from a separate
|
| 52 |
+
AI-assisted checking workflow. Separate processes, recorded probes and exact source/artifact hashes
|
| 53 |
+
are evidence of what was checked; model assistance does not constitute external certification.
|
dist/SHA256SUMS
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
996d73cba9676e8932206b9c80493becde6d04b6a57b70839999b0824c0186cb uai32
|
| 2 |
+
2a859cf4edba8af4de4fa0a598dfc26280c3ecb914d298260c8846de1671fb48 iris.model
|
| 3 |
+
24092e4006782550ac02a355d8e4d6bd24eb272a94993152fc159467cfc80c80 mnist14.model
|
| 4 |
+
7e12bcb541bc8ce39117437458bbd40306f562722a668904fc03cb903b7fc834 rings.model
|
| 5 |
+
d9a5b4228c59479837c2c77f91a19e0ce7cb9c19561e4ff8f136e69c46bfbb82 spirals.model
|
dist/iris.model
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:2a859cf4edba8af4de4fa0a598dfc26280c3ecb914d298260c8846de1671fb48
|
| 3 |
+
size 174
|
dist/mnist14.model
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:24092e4006782550ac02a355d8e4d6bd24eb272a94993152fc159467cfc80c80
|
| 3 |
+
size 21468
|
dist/rings.model
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:7e12bcb541bc8ce39117437458bbd40306f562722a668904fc03cb903b7fc834
|
| 3 |
+
size 222
|
dist/spirals.model
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:d9a5b4228c59479837c2c77f91a19e0ce7cb9c19561e4ff8f136e69c46bfbb82
|
| 3 |
+
size 348
|
dist/uai32
ADDED
|
Binary file (9.19 kB). View file
|
|
|
refcheck.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Independent check of uai32's maths, in numpy (no C code involved).
|
| 3 |
+
|
| 4 |
+
python3 refcheck.py MODEL DATA 1. re-implements inference from the documented file format and
|
| 5 |
+
prints accuracy/loss to compare with `uai32 test DATA MODEL`
|
| 6 |
+
2. compares one C SGD step against the analytic gradient of
|
| 7 |
+
softmax cross-entropy (needs ./uai32 in the same directory)
|
| 8 |
+
Exit status 0 if both agree, 1 otherwise.
|
| 9 |
+
"""
|
| 10 |
+
import os, subprocess, sys, tempfile, re, math
|
| 11 |
+
import numpy as np
|
| 12 |
+
|
| 13 |
+
def load(path):
|
| 14 |
+
b = open(path, "rb").read()
|
| 15 |
+
magic, ni, nh, no = np.frombuffer(b[:8], "<u2")
|
| 16 |
+
assert magic == 0xA132, "bad magic"
|
| 17 |
+
stats = np.frombuffer(b[8:8 + 8 * ni], "<f4").astype(np.float64) # mean, scale as float32
|
| 18 |
+
w = np.frombuffer(b[8 + 8 * ni:], "<u2").astype(np.uint32) << 16 # bfloat16 -> float32 bits
|
| 19 |
+
w = w.view("<f4").astype(np.float64)
|
| 20 |
+
assert w.size == nh * (ni + 1) + no * (nh + 1), "file size does not match header"
|
| 21 |
+
W1 = w[:nh * (ni + 1)].reshape(nh, ni + 1); W2 = w[nh * (ni + 1):].reshape(no, nh + 1)
|
| 22 |
+
return stats[:ni], stats[ni:], W1, W2
|
| 23 |
+
|
| 24 |
+
def forward(x, mean, scale, W1, W2):
|
| 25 |
+
xn = (x - mean) * scale
|
| 26 |
+
h = np.maximum(W1[:, :-1] @ xn + W1[:, -1], 0)
|
| 27 |
+
z = W2[:, :-1] @ h + W2[:, -1]
|
| 28 |
+
lse = z.max() + np.log(np.exp(z - z.max()).sum())
|
| 29 |
+
return xn, h, z, lse
|
| 30 |
+
|
| 31 |
+
def main(model, data):
|
| 32 |
+
mean, scale, W1, W2 = load(model)
|
| 33 |
+
rows = np.loadtxt(data, ndmin=2); X, y = rows[:, :-1], rows[:, -1].astype(int)
|
| 34 |
+
ok = loss = 0.0
|
| 35 |
+
for xi, yi in zip(X, y):
|
| 36 |
+
_, _, z, lse = forward(xi, mean, scale, W1, W2)
|
| 37 |
+
ok += z.argmax() == yi; loss += lse - z[yi]
|
| 38 |
+
print(f"numpy: loss {loss / len(y):.4f} accuracy {int(ok)}/{len(y)} = {100 * ok / len(y):.1f}%")
|
| 39 |
+
evaluation = subprocess.run(["./uai32", "test", data, model], capture_output=True, text=True)
|
| 40 |
+
c = evaluation.stdout.strip()
|
| 41 |
+
print("c: ", c)
|
| 42 |
+
match = re.fullmatch(r"test loss (\S+) accuracy (\d+)/(\d+) = (\S+)%", c)
|
| 43 |
+
same = evaluation.returncode == 0 and match is not None
|
| 44 |
+
if same:
|
| 45 |
+
closs, correct, count, accuracy = match.groups()
|
| 46 |
+
closs, accuracy = float(closs), float(accuracy)
|
| 47 |
+
same = (math.isfinite(closs) and math.isfinite(accuracy)
|
| 48 |
+
and int(correct) == int(ok) and int(count) == len(y)
|
| 49 |
+
and accuracy == float(f"{100 * ok / len(y):.1f}")
|
| 50 |
+
and abs(closs - loss / len(y)) <= 0.0001)
|
| 51 |
+
if not same:
|
| 52 |
+
print("DISAGREE: evaluation status, accuracy or loss"); return 1
|
| 53 |
+
# Gradient check: one SGD step on one example with a tiny rate, compared with the analytic gradient.
|
| 54 |
+
xi = X[0]; xn, h, z, lse = forward(xi, mean, scale, W1, W2)
|
| 55 |
+
lr, yi = 0.5, (int(z.argmax()) + 1) % len(z) # a wrong label and a big rate: the update must dwarf bfloat16 rounding
|
| 56 |
+
p = np.exp(z - lse); dz = p; dz[yi] -= 1
|
| 57 |
+
gW2 = np.outer(dz, np.append(h, 1)); gW1 = np.outer((W2[:, :-1].T @ dz) * (h > 0), np.append(xn, 1))
|
| 58 |
+
with tempfile.TemporaryDirectory() as d:
|
| 59 |
+
one, m2 = os.path.join(d, "one.txt"), os.path.join(d, "m.model")
|
| 60 |
+
np.savetxt(one, [np.append(xi, yi)], fmt="%.8g")
|
| 61 |
+
open(m2, "wb").write(open(model, "rb").read())
|
| 62 |
+
subprocess.run(["./uai32", "train", one, m2, "0", "1", str(lr)], capture_output=True, check=True)
|
| 63 |
+
_, _, W1b, W2b = load(m2)
|
| 64 |
+
# The C program rounds weights to bfloat16 when saving (relative error up to 2^-8), so judge only the
|
| 65 |
+
# weights whose update is at least 16x that noise; for them (step - gradient)/gradient must be small.
|
| 66 |
+
for name, before, after, g in (("W1", W1, W1b, gW1), ("W2", W2, W2b, gW2)):
|
| 67 |
+
step = (before - after) / lr; big = np.abs(g) * lr > np.abs(after) * 2 ** -4 + 1e-6
|
| 68 |
+
err = (np.abs(step[big] - g[big]) / np.abs(g[big])).max() if big.any() else 1.0
|
| 69 |
+
print(f"gradient {name}: {int(big.sum())} weights checked, max relative error {err:.3g}")
|
| 70 |
+
same &= big.any() and err < 0.1
|
| 71 |
+
print("AGREE" if same else "DISAGREE"); return 0 if same else 1
|
| 72 |
+
|
| 73 |
+
if __name__ == "__main__":
|
| 74 |
+
sys.exit(main(*sys.argv[1:3]))
|
uai32.c
ADDED
|
@@ -0,0 +1,257 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* uai32.c -- Sentovara uAI-32: the smallest genuinely trainable neural network we could make.
|
| 2 |
+
*
|
| 3 |
+
* A one-hidden-layer multilayer perceptron (MLP) classifier, trained by plain stochastic
|
| 4 |
+
* gradient descent with backpropagation (softmax output, cross-entropy loss). Everything
|
| 5 |
+
* the program learns lives in ONE array of floats (P) which is saved to and loaded from a
|
| 6 |
+
* small file. There are no rules, lookup tables or dataset-specific code anywhere.
|
| 7 |
+
*
|
| 8 |
+
* uai32 train DATA MODEL [HIDDEN] [EPOCHS] [RATE] [SEED] learn from DATA, save to MODEL
|
| 9 |
+
* uai32 test DATA MODEL accuracy + loss of MODEL on DATA
|
| 10 |
+
* uai32 predict MODEL < rows print class + probabilities per row
|
| 11 |
+
*
|
| 12 |
+
* If MODEL already exists, `train` loads it and keeps training it (HIDDEN is then ignored,
|
| 13 |
+
* pass anything; SEED still seeds the shuffle). Delete the file to start from scratch.
|
| 14 |
+
*
|
| 15 |
+
* DATA format: one example per line, whitespace-separated numbers: the features, then the
|
| 16 |
+
* integer class label (0,1,2,...). The number of features is taken from the first line.
|
| 17 |
+
* Blank lines and lines that do not start with a number are skipped. `predict` reads rows
|
| 18 |
+
* from stdin and ignores extra numeric columns after the features, so a labelled file can be piped in.
|
| 19 |
+
*
|
| 20 |
+
* MODEL format (all little-endian): u16 magic 0xA132, u16 NI, u16 NH, u16 NO, then the
|
| 21 |
+
* parameters in the order mean[NI], scale[NI], W1[NH][NI+1], W2[NO][NH+1] (last column = bias).
|
| 22 |
+
* mean and scale are stored as exact float32; every weight as bfloat16 (the top 16 bits of
|
| 23 |
+
* an IEEE-754 float, rounded to nearest even), which halves the file at no accuracy cost.
|
| 24 |
+
*/
|
| 25 |
+
#define _POSIX_C_SOURCE 200809L
|
| 26 |
+
#include <stdio.h>
|
| 27 |
+
#include <stdlib.h>
|
| 28 |
+
#include <string.h>
|
| 29 |
+
#include <math.h>
|
| 30 |
+
#include <errno.h>
|
| 31 |
+
#include <float.h>
|
| 32 |
+
#include <limits.h>
|
| 33 |
+
|
| 34 |
+
static int NI, NH, NO; /* inputs, hidden units, outputs (classes) */
|
| 35 |
+
static float *P, *mean, *scale, *W1, *W2; /* all learned parameters live in P */
|
| 36 |
+
static float *xn, *hid, *z, *out, *dh, lse; /* normalised input, hidden, logits, softmax, hidden grad, log-sum-exp */
|
| 37 |
+
static unsigned rng = 1; /* xorshift32 state; SEED sets it */
|
| 38 |
+
|
| 39 |
+
static void die(const char *m) { fprintf(stderr, "uai32: %s\n", m); exit(1); }
|
| 40 |
+
|
| 41 |
+
static unsigned integer(const char *s, unsigned max) {
|
| 42 |
+
char *e; unsigned long v; errno = 0; v = strtoul(s, &e, 10);
|
| 43 |
+
if (*s < '0' || *s > '9' || *e || errno || v > max) die("invalid integer argument");
|
| 44 |
+
return (unsigned)v;
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
static float rate(const char *s) {
|
| 48 |
+
char *e; float v; errno = 0; v = strtof(s, &e);
|
| 49 |
+
if (e == s || *e || errno || !isfinite(v) || v <= 0 || v > 1e6f) die("invalid RATE");
|
| 50 |
+
return v;
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
static float frand(void) { /* uniform in [-1, 1) */
|
| 54 |
+
rng ^= rng << 13; rng ^= rng >> 17; rng ^= rng << 5;
|
| 55 |
+
return (int)rng * (1.0f / 2147483648.0f);
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
static int nparams(void) { return 2 * NI + NH * (NI + 1) + NO * (NH + 1); }
|
| 59 |
+
|
| 60 |
+
static void valid_params(void) {
|
| 61 |
+
int i;
|
| 62 |
+
for (i = 0; i < nparams(); i++)
|
| 63 |
+
if (!isfinite(P[i]) || (i >= NI && i < 2 * NI && P[i] < 0)) die("invalid model parameter");
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
static void alloc(void) {
|
| 67 |
+
if (NI < 1 || NH < 1 || NO < 1 || (double)NH * (NI + 1) + (double)NO * (NH + 1) + 2.0 * NI > 1e8) die("bad size");
|
| 68 |
+
P = calloc(nparams(), sizeof *P);
|
| 69 |
+
xn = calloc(NI + 2 * NH + 2 * NO, sizeof *xn);
|
| 70 |
+
if (!P || !xn) die("out of memory");
|
| 71 |
+
mean = P; scale = mean + NI; W1 = scale + NI; W2 = W1 + NH * (NI + 1);
|
| 72 |
+
hid = xn + NI; dh = hid + NH; z = dh + NH; out = z + NO;
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
/* Forward pass: fills xn, hid (ReLU), z (logits), lse, out (softmax probabilities); returns the argmax class. */
|
| 76 |
+
static int forward(const float *x) {
|
| 77 |
+
int i, j, k, best = 0; float m = -1e30f, s = 0;
|
| 78 |
+
for (i = 0; i < NI; i++) { xn[i] = (x[i] - mean[i]) * scale[i]; if (!isfinite(xn[i])) die("numeric overflow"); }
|
| 79 |
+
for (j = 0; j < NH; j++) {
|
| 80 |
+
float *w = W1 + j * (NI + 1), a = w[NI];
|
| 81 |
+
for (i = 0; i < NI; i++) a += w[i] * xn[i];
|
| 82 |
+
if (!isfinite(a)) die("numeric overflow");
|
| 83 |
+
hid[j] = a > 0 ? a : 0;
|
| 84 |
+
}
|
| 85 |
+
for (k = 0; k < NO; k++) {
|
| 86 |
+
float *w = W2 + k * (NH + 1), a = w[NH];
|
| 87 |
+
for (j = 0; j < NH; j++) a += w[j] * hid[j];
|
| 88 |
+
if (!isfinite(a)) die("numeric overflow");
|
| 89 |
+
z[k] = a; if (a > m) { m = a; best = k; }
|
| 90 |
+
}
|
| 91 |
+
for (k = 0; k < NO; k++) s += expf(z[k] - m);
|
| 92 |
+
lse = m + logf(s); /* log of the softmax denominator, computed stably */
|
| 93 |
+
if (!isfinite(lse)) die("numeric overflow");
|
| 94 |
+
for (k = 0; k < NO; k++) out[k] = expf(z[k] - lse);
|
| 95 |
+
return best;
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
/* One SGD step on example (x, y): backpropagate the cross-entropy gradient, update P in place. */
|
| 99 |
+
static void learn(const float *x, int y, float lr) {
|
| 100 |
+
int i, j, k;
|
| 101 |
+
forward(x);
|
| 102 |
+
for (j = 0; j < NH; j++) dh[j] = 0;
|
| 103 |
+
for (k = 0; k < NO; k++) {
|
| 104 |
+
float g = out[k] - (k == y), *w = W2 + k * (NH + 1); /* dLoss/dlogit_k */
|
| 105 |
+
for (j = 0; j < NH; j++) { dh[j] += g * w[j]; w[j] -= lr * g * hid[j]; }
|
| 106 |
+
w[NH] -= lr * g;
|
| 107 |
+
}
|
| 108 |
+
for (j = 0; j < NH; j++) if (hid[j] > 0) { /* ReLU gate */
|
| 109 |
+
float g = dh[j], *w = W1 + j * (NI + 1);
|
| 110 |
+
for (i = 0; i < NI; i++) w[i] -= lr * g * xn[i];
|
| 111 |
+
w[NI] -= lr * g;
|
| 112 |
+
}
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
/* ---- model file ---- */
|
| 116 |
+
static void put16(unsigned v, FILE *f) { putc(v & 255, f); putc(v >> 8 & 255, f); }
|
| 117 |
+
static int get16(FILE *f) { int lo = getc(f), hi = getc(f); if (hi < 0) die("truncated model file"); return lo | hi << 8; }
|
| 118 |
+
static unsigned bits(float v) { unsigned b; memcpy(&b, &v, 4); return b; }
|
| 119 |
+
static float value(unsigned b) { float v; memcpy(&v, &b, 4); return v; }
|
| 120 |
+
|
| 121 |
+
static void save(const char *path) {
|
| 122 |
+
valid_params();
|
| 123 |
+
for (int i = 2 * NI; i < nparams(); i++)
|
| 124 |
+
if (!isfinite(value((bits(P[i]) + 0x7FFF + (bits(P[i]) >> 16 & 1)) & 0xFFFF0000u))) die("model rounding overflow");
|
| 125 |
+
FILE *f = fopen(path, "wb"); int i, n = nparams();
|
| 126 |
+
if (!f) die("cannot write model file");
|
| 127 |
+
put16(0xA132, f); put16(NI, f); put16(NH, f); put16(NO, f);
|
| 128 |
+
for (i = 0; i < 2 * NI; i++) { put16(bits(P[i]) & 0xFFFF, f); put16(bits(P[i]) >> 16, f); } /* mean, scale: float32 */
|
| 129 |
+
for (; i < n; i++) put16((bits(P[i]) + 0x7FFF + (bits(P[i]) >> 16 & 1)) >> 16, f); /* weights: bfloat16, round to nearest even */
|
| 130 |
+
if (fclose(f)) die("cannot write model file");
|
| 131 |
+
}
|
| 132 |
+
|
| 133 |
+
static int load(const char *path) { /* returns 0 if the file does not exist */
|
| 134 |
+
FILE *f = fopen(path, "rb"); int i, n;
|
| 135 |
+
if (!f) { if (errno == ENOENT) return 0; die("cannot open model file"); }
|
| 136 |
+
if (get16(f) != 0xA132) die("not a uai32 model file");
|
| 137 |
+
NI = get16(f); NH = get16(f); NO = get16(f); alloc();
|
| 138 |
+
if (fseek(f, 0, SEEK_END) || ftell(f) != 8L + 8L * NI + 2L * (nparams() - 2 * NI) || fseek(f, 8, SEEK_SET)) die("wrong model size");
|
| 139 |
+
for (i = 0, n = nparams(); i < 2 * NI; i++) { unsigned lo = get16(f); P[i] = value(lo | (unsigned)get16(f) << 16); }
|
| 140 |
+
for (; i < n; i++) P[i] = value((unsigned)get16(f) << 16);
|
| 141 |
+
fclose(f); valid_params(); return 1;
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
+
/* ---- data ---- */
|
| 145 |
+
static int parse(char *s, float *v, int max, double *label) { /* numbers in s -> v; label kept as double before integer validation */
|
| 146 |
+
int n = 0; char *e;
|
| 147 |
+
for (;;) {
|
| 148 |
+
while (*s == ' ' || *s == '\t' || *s == '\r' || *s == '\n') s++;
|
| 149 |
+
if (!*s) break;
|
| 150 |
+
errno = 0; double x = strtod(s, &e);
|
| 151 |
+
if (e == s) { if (n) die("invalid data token"); break; }
|
| 152 |
+
if (errno || !isfinite(x) || x > FLT_MAX || x < -FLT_MAX || (*e && *e != ' ' && *e != '\t' && *e != '\r' && *e != '\n')) die("invalid data number");
|
| 153 |
+
if (n < max) v[n] = (float)x;
|
| 154 |
+
if (label && n == max - 1) *label = x;
|
| 155 |
+
if (n == INT_MAX) die("too many columns");
|
| 156 |
+
n++; s = e;
|
| 157 |
+
}
|
| 158 |
+
return n;
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
+
static float *X; static int *Y, N; /* the loaded dataset */
|
| 162 |
+
static void read_data(const char *path, int need_labels) { /* fixes NI if it is still 0 */
|
| 163 |
+
FILE *f = path ? fopen(path, "r") : stdin; char *line = 0; size_t cap = 0; float *row = 0; int room = 0, n;
|
| 164 |
+
if (!f) die("cannot open data file");
|
| 165 |
+
while (getline(&line, &cap, f) > 0) {
|
| 166 |
+
if (!(n = parse(line, 0, 0, 0))) continue; /* skip blank / non-numeric lines */
|
| 167 |
+
if (!NI) { if (n < 2) die("need at least one feature and a label"); NI = n - 1; }
|
| 168 |
+
if (need_labels ? n != NI + 1 : n < NI) die("wrong number of columns"); /* predict ignores extra columns */
|
| 169 |
+
if (!row && !(row = calloc(NI + 1, sizeof *row))) die("out of memory");
|
| 170 |
+
if (N == room) {
|
| 171 |
+
if (room > INT_MAX / 2) die("too many rows");
|
| 172 |
+
room = room ? 2 * room : 256;
|
| 173 |
+
X = realloc(X, (size_t)room * NI * sizeof *X); Y = realloc(Y, room * sizeof *Y);
|
| 174 |
+
if (!X || !Y) die("out of memory");
|
| 175 |
+
}
|
| 176 |
+
double label = 0; parse(line, row, NI + 1, &label);
|
| 177 |
+
for (n = 0; n < NI; n++) if (!(row[n] - row[n] == 0)) die("feature is not a finite number"); /* NaN or inf */
|
| 178 |
+
memcpy(X + (size_t)N * NI, row, NI * sizeof *X);
|
| 179 |
+
if (need_labels) {
|
| 180 |
+
if (label < 0 || label > 65534 || label != (int)label) die("labels must be integers in 0..65534");
|
| 181 |
+
Y[N] = (int)label;
|
| 182 |
+
}
|
| 183 |
+
N++;
|
| 184 |
+
}
|
| 185 |
+
if (path) fclose(f); /* memory is reclaimed at exit; no free() needed */
|
| 186 |
+
if (!N) die("no examples");
|
| 187 |
+
}
|
| 188 |
+
|
| 189 |
+
/* Accuracy and mean cross-entropy of the current model over the loaded data (no learning). */
|
| 190 |
+
static void report(const char *tag) {
|
| 191 |
+
int i, ok = 0; double loss = 0;
|
| 192 |
+
for (i = 0; i < N; i++) { ok += forward(X + (size_t)i * NI) == Y[i]; loss += lse - z[Y[i]]; } /* -log p_y */
|
| 193 |
+
if (!isfinite(loss)) die("numeric overflow");
|
| 194 |
+
printf("%s loss %.4f accuracy %d/%d = %.1f%%\n", tag, loss / N, ok, N, 100.0 * ok / N);
|
| 195 |
+
}
|
| 196 |
+
|
| 197 |
+
static void usage(void) {
|
| 198 |
+
die("usage:\n uai32 train DATA MODEL [HIDDEN=16] [EPOCHS=50] [RATE=0.05] [SEED=1 (integer)]\n"
|
| 199 |
+
" uai32 test DATA MODEL\n uai32 predict MODEL < rows");
|
| 200 |
+
}
|
| 201 |
+
|
| 202 |
+
int main(int argc, char **argv) {
|
| 203 |
+
int i, j, e;
|
| 204 |
+
if (argc < 3) usage();
|
| 205 |
+
if (!strcmp(argv[1], "predict")) { /* ---- inference from stdin ---- */
|
| 206 |
+
if (!load(argv[2])) die("cannot open model file");
|
| 207 |
+
read_data(0, 0);
|
| 208 |
+
for (i = 0; i < N; i++) {
|
| 209 |
+
printf("%d", forward(X + (size_t)i * NI));
|
| 210 |
+
for (j = 0; j < NO; j++) printf(" %.3f", out[j]);
|
| 211 |
+
printf("\n");
|
| 212 |
+
}
|
| 213 |
+
return 0;
|
| 214 |
+
}
|
| 215 |
+
if (argc < 4) usage();
|
| 216 |
+
if (!strcmp(argv[1], "test")) { /* ---- evaluate a saved model ---- */
|
| 217 |
+
if (!load(argv[3])) die("cannot open model file");
|
| 218 |
+
read_data(argv[2], 1);
|
| 219 |
+
for (i = 0; i < N; i++) if (Y[i] >= NO) die("label outside the model's classes");
|
| 220 |
+
report("test");
|
| 221 |
+
return 0;
|
| 222 |
+
}
|
| 223 |
+
if (strcmp(argv[1], "train")) usage(); /* ---- train (fresh or continued) ---- */
|
| 224 |
+
int hidden = argc > 4 ? integer(argv[4], 65535) : 16, epochs = argc > 5 ? integer(argv[5], INT_MAX - 1u) : 50;
|
| 225 |
+
float lr = argc > 6 ? rate(argv[6]) : 0.05f;
|
| 226 |
+
rng = argc > 7 ? integer(argv[7], 4294967295u) : 1; if (!rng) rng = 1;
|
| 227 |
+
int resumed = load(argv[3]);
|
| 228 |
+
read_data(argv[2], 1);
|
| 229 |
+
if (!resumed) {
|
| 230 |
+
NH = hidden; NO = 0;
|
| 231 |
+
for (i = 0; i < N; i++) if (Y[i] >= NO) NO = Y[i] + 1;
|
| 232 |
+
if (NI > 65535 || NH > 65535 || NO > 65535) die("bad size");
|
| 233 |
+
alloc();
|
| 234 |
+
for (j = 0; j < NI; j++) { /* feature normalisation: shift by the mean, scale by the range */
|
| 235 |
+
float lo = X[j], hi = X[j]; double s = 0;
|
| 236 |
+
for (i = 0; i < N; i++) { float v = X[(size_t)i * NI + j]; s += v; if (v < lo) lo = v; if (v > hi) hi = v; }
|
| 237 |
+
float range = hi - lo; /* finite inputs can still overflow here (3e38 - -3e38) */
|
| 238 |
+
if (!(range <= FLT_MAX)) die("feature range overflows float");
|
| 239 |
+
mean[j] = s / N; scale[j] = range > 0 ? 1 / range : 0;
|
| 240 |
+
}
|
| 241 |
+
for (i = 0; i < NH * (NI + 1); i++) W1[i] = (i + 1) % (NI + 1) ? frand() * sqrtf(6.0f / NI) : 0; /* He-uniform, zero bias */
|
| 242 |
+
for (i = 0; i < NO * (NH + 1); i++) W2[i] = (i + 1) % (NH + 1) ? frand() * sqrtf(6.0f / NH) : 0;
|
| 243 |
+
printf("new model: %d inputs, %d hidden, %d classes, %d parameters\n", NI, NH, NO, nparams());
|
| 244 |
+
} else {
|
| 245 |
+
for (i = 0; i < N; i++) if (Y[i] >= NO) die("label outside the model's classes");
|
| 246 |
+
printf("continuing: %d inputs, %d hidden, %d classes\n", NI, NH, NO);
|
| 247 |
+
}
|
| 248 |
+
int *idx = calloc(N, sizeof *idx); if (!idx) die("out of memory");
|
| 249 |
+
for (i = 0; i < N; i++) idx[i] = i;
|
| 250 |
+
for (e = 1; e <= epochs; e++) {
|
| 251 |
+
for (i = N - 1; i > 0; i--) { frand(); j = rng % (i + 1); int t = idx[i]; idx[i] = idx[j]; idx[j] = t; } /* shuffle */
|
| 252 |
+
for (i = 0; i < N; i++) learn(X + (size_t)idx[i] * NI, Y[idx[i]], lr);
|
| 253 |
+
printf("epoch %d/%d ", e, epochs); report("train");
|
| 254 |
+
}
|
| 255 |
+
save(argv[3]);
|
| 256 |
+
return 0;
|
| 257 |
+
}
|
uai32.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// µAI-32 inference in JavaScript: the forward pass of uai32.c, reading the audited model file byte-for-byte.
|
| 2 |
+
// File format (little-endian): u16 magic 0xA132, u16 NI, u16 NH, u16 NO, float32 mean[NI], float32 scale[NI],
|
| 3 |
+
// then bfloat16 W1[NH][NI+1] and W2[NO][NH+1] (last column = bias). Float32 arithmetic, as in the C program.
|
| 4 |
+
function uaiLoad(bytes) {
|
| 5 |
+
const dv = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
| 6 |
+
if (dv.getUint16(0, true) !== 0xA132) throw new Error("bad magic");
|
| 7 |
+
const NI = dv.getUint16(2, true), NH = dv.getUint16(4, true), NO = dv.getUint16(6, true);
|
| 8 |
+
const nw = NH * (NI + 1) + NO * (NH + 1);
|
| 9 |
+
if (bytes.byteLength !== 8 + 8 * NI + 2 * nw) throw new Error("file size does not match header");
|
| 10 |
+
const mean = new Float32Array(NI), scale = new Float32Array(NI), w = new Float32Array(nw), u = new Uint32Array(1), f = new Float32Array(u.buffer);
|
| 11 |
+
for (let i = 0; i < NI; i++) { mean[i] = dv.getFloat32(8 + 4 * i, true); scale[i] = dv.getFloat32(8 + 4 * NI + 4 * i, true); }
|
| 12 |
+
for (let i = 0; i < nw; i++) { u[0] = dv.getUint16(8 + 8 * NI + 2 * i, true) << 16; w[i] = f[0]; }
|
| 13 |
+
return { NI, NH, NO, mean, scale, W1: w.subarray(0, NH * (NI + 1)), W2: w.subarray(NH * (NI + 1)), params: nw, bytes: bytes.byteLength };
|
| 14 |
+
}
|
| 15 |
+
function uaiPredict(m, x) {
|
| 16 |
+
const f = Math.fround, xn = new Float32Array(m.NI), h = new Float32Array(m.NH), z = new Float64Array(m.NO);
|
| 17 |
+
for (let i = 0; i < m.NI; i++) xn[i] = f(f(x[i] - m.mean[i]) * m.scale[i]);
|
| 18 |
+
for (let j = 0; j < m.NH; j++) {
|
| 19 |
+
const r = j * (m.NI + 1); let s = m.W1[r + m.NI];
|
| 20 |
+
for (let i = 0; i < m.NI; i++) s = f(s + f(m.W1[r + i] * xn[i]));
|
| 21 |
+
h[j] = s > 0 ? s : 0;
|
| 22 |
+
}
|
| 23 |
+
let mx = -Infinity;
|
| 24 |
+
for (let k = 0; k < m.NO; k++) {
|
| 25 |
+
const r = k * (m.NH + 1); let s = m.W2[r + m.NH];
|
| 26 |
+
for (let j = 0; j < m.NH; j++) s = f(s + f(m.W2[r + j] * h[j]));
|
| 27 |
+
z[k] = s; if (s > mx) mx = s;
|
| 28 |
+
}
|
| 29 |
+
let sum = 0; const p = new Array(m.NO);
|
| 30 |
+
for (let k = 0; k < m.NO; k++) { p[k] = Math.exp(z[k] - mx); sum += p[k]; }
|
| 31 |
+
let best = 0;
|
| 32 |
+
for (let k = 0; k < m.NO; k++) { p[k] /= sum; if (p[k] > p[best]) best = k; }
|
| 33 |
+
return { cls: best, p };
|
| 34 |
+
}
|
| 35 |
+
if (typeof module !== "undefined") module.exports = { uaiLoad, uaiPredict };
|