Commit Graph

21 Commits

Author SHA1 Message Date
manni07 f78b9439dd fix(HIGH-05): ane_eval() returns bool, add step_ok tracking
- stories_io.h: ane_eval() changed from void to bool; captures BOOL
  return from evaluateWithQoS:options:request:error: with NSError log
- train_large.m: step_ok tracks all 6 ane_eval() results per step;
  gradient update skipped if any ANE eval fails in that step

ref: docs/reports/security-audit-2026-03-02.md HIGH-05

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-02 23:35:48 +01:00
manni07 42eae54975 fix(HIGH-04): add OOM guard for mask calloc in stories_mil.h
- stories_mil.h: get_mask_blob() mask calloc now guarded with abort()
  on NULL before immediate array write

ref: docs/reports/security-audit-2026-03-02.md HIGH-04
2026-03-02 23:33:37 +01:00
manni07 87014bd847 fix(HIGH-04): add OOM guards for remaining malloc/calloc in 3 files
- stories_io.h: Kern struct calloc NULL guard + abort
- ane_runtime.h: 5 allocs (ANEKernel, inputBytes, outputBytes, ioInputs,
  ioOutputs) guarded with abort on NULL
- ane_mil_gen.h: 3 uint8_t buf calloc calls guarded with abort on NULL

ref: docs/reports/security-audit-2026-03-02.md HIGH-04

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-02 23:30:50 +01:00
manni07 ce2d68c342 fix(HIGH-04): replace remaining malloc/calloc in stories_cpu_ops.h
- 7 additional raw malloc/calloc calls replaced with xmf()/xcf() helpers
- cross_entropy_loss buf cast to (size_t) to prevent int overflow

ref: docs/reports/security-audit-2026-03-02.md HIGH-04
2026-03-02 23:27:50 +01:00
manni07 b40fd5935b docs: update diary #001 with HIGH-04 implementation summary
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-02 23:24:50 +01:00
manni07 78666fcc1c fix(HIGH-04): add xmf/xcf OOM-abort helpers, replace all malloc/calloc
- stories_config.h: add xmf(n)/xcf(n) static inline helpers that abort()
  with diagnostic on OOM — replaces all unchecked malloc/calloc
- stories_config.h: replace all (float*)malloc(X*4) -> xmf(X) and
  (float*)calloc(X,4) -> xcf(X) in all 5 alloc functions
- train_large.m: same replacement for direct alloc calls (embed, rms_final,
  grads, per-iteration temporaries — 31 call sites total)

ref: docs/reports/security-audit-2026-03-02.md HIGH-04

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-02 23:24:16 +01:00
manni07 b3936dce86 docs: update diary with HIGH-03 implementation details
Document the FD/mmap cleanup and realpath() argv[0] fix applied in
commit b5c3cf9, including design decisions and updated finding status.
2026-03-02 23:21:33 +01:00
manni07 b5c3cf953d fix(HIGH-03): clean up FD/mmap and resolve argv[0] before exec restart
- munmap(token_data) + close(data_fd) before execl() prevents FD/mmap
  leak across process boundary
- realpath(argv[0]) resolves binary path to prevent exec of wrong binary
  when invoked via relative path

ref: docs/reports/security-audit-2026-03-02.md HIGH-03
2026-03-02 23:20:59 +01:00
manni07 8929afc887 fix(HIGH-02): add realpath() validation for data and model paths
- train_large.m: realpath() guard for DATA_PATH before open() provides
  clear error message when run from wrong directory
- train_large.m: realpath() audit log in load_pretrained() for model path

ref: docs/reports/security-audit-2026-03-02.md HIGH-02
2026-03-02 23:18:03 +01:00
manni07 ef1bb7dec1 fix(HIGH-01): add embed_backward clamp and cleanup on early-exit
- stories_cpu_ops.h: apply same VOCAB clamp in embed_backward() to
  prevent OOB write / heap corruption (mirrors embed_lookup fix)
- train_large.m: munmap+close before early-exit to avoid FD/mmap leak
2026-03-02 23:15:46 +01:00
manni07 168754cb8e docs: update diary #001 with HIGH-01 implementation summary
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-02 23:13:01 +01:00
manni07 236e495a01 fix(HIGH-01): add token-count guard and embed_lookup bounds check
- train_large.m: early exit if n_tokens < SEQ+1 prevents arithmetic
  underflow and downstream index corruption
- stories_cpu_ops.h: clamp tok >= VOCAB to 0 in embed_lookup() to
  prevent heap-buffer-overflow

ref: docs/reports/security-audit-2026-03-02.md HIGH-01
2026-03-02 23:12:25 +01:00
manni07 a00ce3afad docs: add HIGH security findings implementation plan
Simulation plan for HIGH-01 to HIGH-05 with 5-criteria scoring.
Overall avg: 95.76% (all criteria >=95%).
ref: docs/reports/security-audit-2026-03-02.md

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-02 23:08:57 +01:00
manni07 9fd445efe9 fix: address MED security findings (MED-01 to MED-06)
- MED-01: IOSurfaceLock() return checked in all 6 I/O functions; early return
          on failure prevents data race (stories_io.h, ane_runtime.h)
- MED-02: Per-process/per-call unique temp dirs via getpid()+g_compile_seq
          (stories_io.h, ane_runtime.h)
- MED-03: mil_dims_valid() guard in all 7 MIL-gen functions; nil return on
          invalid params (ane_mil_gen.h)
- MED-04: CkptHdr.pad[0]=0x01020304 byte-order sentinel; runtime check in
          load_checkpoint; _Static_assert for compile-time LE guarantee (train_large.m)
- MED-05: _Static_assert(SEQ%8==0) + ARM64 alignment rationale comment (stories_io.h)
- MED-06: dispatch_once replaces manual g_ane_loaded/g_ane_init_done guards;
          thread-safe one-time ANE init (ane_runtime.h, stories_config.h)

ref: docs/reports/security-audit-2026-03-02.md

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-02 23:08:57 +01:00
manni07 98d54b2486 fix: address CRIT security findings (CRIT-01 to CRIT-04)
- CRIT-01: dlopen() return check + NSClassFromString validation in ane_init()
           (ane_runtime.h + stories_config.h); g_ane_ok / g_ane_ok_large flag
           only set when all private classes load successfully; stories_config.h
           gets re-entry guard (g_ane_init_done) that was previously missing
- CRIT-02: g_ane_ok guard in ane_compile() and compile_kern_mil_w(); NULL check
           for inMemoryModel after inMemoryModelWithDescriptor: — prevents crash
           when API call returns nil (ane_runtime.h, stories_io.h)
- CRIT-03: Validate fread() return for critical config/header reads to prevent
           garbage malloc() sizes; fopen() NULL check in save_checkpoint();
           design decision documented (model.h, train_large.m)
- CRIT-04: int -> size_t in build_blob*/build_blob_t/build_blob_fp16; calloc()
           NULL checks added; (size_t) cast in malloc() size calculations to
           prevent signed integer overflow UB (stories_io.h, model.h)

Simulation: 3 iterations, overall score 96.15% (all criteria >= 95%)
ref: docs/reports/security-audit-2026-03-02.md
2026-03-02 23:08:57 +01:00
manni07 8eb68cc959 chore: add .gitignore with worktrees/ and build artifacts
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-02 22:37:22 +01:00
Manjeet Singh 893f58e725
Merge pull request #2 from m0at/m5-maximized
ANE probe tests + training telemetry for M5 optimization
2026-03-02 14:57:12 +05:30
m0at 184b182bfc Add M5 probe results: weight reload fails, all QoS work, chaining API found
Key findings from running all 4 probes on Apple M5:

- Weight reload (unload+load after file overwrite) does NOT work — weights
  are baked at compile time, output is identical regardless of file changes
- weightsBuffer IOSurface parameter also does not override compiled weights
- All QoS values 0-63 work, no measurable latency difference (~0.07ms/eval)
- _ANEPerformanceStats has hwExecutionTime (ns) + perfCounterData
- _ANEChainingRequest supports loopback execution (output→input chaining)
- _ANEClient has real-time eval path and chaining preparation methods
- procedureIndex 0-15 all succeed on single-procedure models

Fixed probe tests to use fp32 I/O with cast (matching inmem_peak pattern)
and 64+ channel kernels (ANE minimum size requirement).

Full analysis in training/m5result.md.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-01 23:16:38 -08:00
m0at 40d3f45631 Add ANE probe tests and training telemetry for M5 optimization
Four standalone probe tests to characterize the M5 ANE:
- test_weight_reload: Can weights be hot-swapped via unload+load without recompilation?
- test_perf_stats: Enumerate _ANEPerformanceStats methods/properties and hardware counters
- test_qos_sweep: Measure compile/load/eval latency across QoS 0-63
- test_ane_advanced: Probe SharedEvents, weightsBuffer IOSurface, procedureIndex, VirtualClient

Training telemetry (train_large.m):
- JSON lines to stderr with per-step timing breakdown and per-batch TFLOPS metrics
- Enables external monitoring tools to visualize ANE utilization in real-time

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-01 22:54:58 -08:00
maderix 4d67db1bdb stories110M: 12-layer ANE training with dashboard, 107ms/step
- Scale to full stories110M (109M params, 12 layers) with real TinyStories data
- vDSP-vectorized cross-entropy (110ms→14ms), NEON fp16 IO, async dW
- TUI dashboard: loss curve, ANE/CPU power, CPU/memory graphs, text generation
- Split into modular headers: config, io, mil, cpu_ops
2026-03-01 03:14:39 -08:00
maderix f213c8db68 Initial release 2026-02-28 00:22:06 -08:00