Skip to content
stiffisPublic

About

Small compiler that turns fixed-size neural network graphs into C99 and plans their workspace memory with liveness analysis and first-fit buffer reuse

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Telar

A small compiler that turns fixed-size neural network graphs into C99 and plans their temporary memory.

Telar reads a graph of operations, computes how long each intermediate tensor stays alive and generates C code that runs the network inside a single preallocated workspace. It compares two memory plans under the same model, kernels and execution order:

  • independent: one block per tensor, no reuse.
  • first-fit: blocks are reused as soon as their tensor is no longer needed.

The name means loom in Spanish: the compiler weaves operations, dependencies and memory blocks together.

Scope

  • One JSON file per model with a single input vector and a single output.
  • Acyclic graphs with static shapes and the operations linear, relu and add.
  • Constant weights, float32 inference and batch size one.
  • Generated C99 with scalar kernels, no dynamic allocation and no in-place operations.

Buffer reuse based on liveness analysis is a well-known technique. Telar is an educational and experimental prototype, not a new contribution to the state of the art.

Usage

Requirements: Python 3.11 or later, NumPy and a C compiler (GCC or Clang). The package does not need to be installed.

make test
export PYTHONPATH=src
python3 -m telar validate examples/bifurcacion.json
python3 -m telar plan examples/bifurcacion.json
python3 -m telar compile examples/cadena.json -o build/cadena --strategy first-fit
python3 -m telar compare examples/doble_uso.json --sanitize --bench --report report.json
make experimento
make demo

The generated code exposes

void telar_run(const float *input, float *output, float *workspace);

and defines TELAR_INPUT_SIZE, TELAR_OUTPUT_SIZE and TELAR_WORKSPACE_SIZE in telar_model.h.

How the planner works

  1. Validate the graph, infer shapes and compute a stable topological order.
  2. Count the remaining uses of every tensor.
  3. For each node, reserve its output in the first free block that fits, splitting or extending the workspace when needed.
  4. Release inputs whose use count reaches zero and merge adjacent free blocks.

The output is reserved before its inputs are released, so an operation never overwrites data it is still reading. The model output stays alive until it is copied to the caller's buffer. An independent checker verifies that no two simultaneously live tensors overlap.

Verification

  • 122 automated tests cover validation, planning, generated C, buffers, reporting, caching and provenance.
  • Generated code is compared against an independent NumPy reference with atol=1e-5 and rtol=1e-4. Non-finite values never count as a match.
  • The workspace is filled with NaN before running, so any read-before-write shows up as a mismatch.
  • Every plan is also run under AddressSanitizer and UndefinedBehaviorSanitizer.

Results

Synthetic suite

make experimento compiles 18 models of different shapes. All of them match the reference (maximum absolute error 3.1e-6) and pass the sanitizers. First-fit saves between 0 % (a single layer, nothing to reuse) and 90.4 % (a six-block residual network) of the workspace. In some branching graphs first-fit reserves more than the peak live size because of fragmentation; those cases are kept in benchmarks/results/summary.md.

Handwritten digits

applications/mnist trains a 784-128-64-10 MLP (109,386 parameters) with PyTorch, exports it to Telar and runs it from the generated C libraries.

Measure Result
Test accuracy (10,000 MNIST images) 97.66 %, 95 % Wilson interval [97.34 %, 97.94 %]
Agreement between PyTorch, NumPy and both C plans 100 % of labels
Outputs of independent and first-fit bitwise identical on all 10,000 images
Workspace 1,576 B with independent, 1,024 B with first-fit (35.03 % less)
Weights and biases 437,544 B, identical in both plans
New drawings by one person 45 of 50 (90 %), reported separately from MNIST

The 35.03 % applies to the workspace only. Over weights, input, output and workspace together, the saving is about 0.125 %. All memory figures are computed statically from the plans; they are not measurements of total process memory.

Timings are measured in interleaved rounds, because timing one strategy after the other produced false differences of up to 2x on the test laptop. Both plans run the same kernels, so the remaining differences are measurement noise and are not reported as a speedup.

make mnist-demo opens a Tkinter application to draw a digit, see the processed 28x28 input, get the prediction from both C libraries and step through the memory plan. See applications/mnist/README.md for the full pipeline.

Repository layout

src/telar/            compiler: model, validation, liveness, memory, codegen, toolchain, CLI
tests/                unit and integration tests
examples/             small hand-written models
benchmarks/           synthetic model generator and experiment results
demo/                 generated HTML and SVG visualizations
applications/mnist/   digit recognition application, artifacts and results
docs/feria-2026/      project report for the 2026 project fair

Command-line messages, the MNIST application and the fair report are written in Spanish.

Limitations

  • Only linear, relu and add, with static shapes and float32 values.
  • Scalar kernels; no quantization, SIMD or operator fusion.
  • Shared library integration is tested on Linux only.
  • No deployment on a microcontroller yet, and no comparison against other embedded inference tools.

About

Small compiler that turns fixed-size neural network graphs into C99 and plans their workspace memory with liveness analysis and first-fit buffer reuse

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages