Repository navigation
Tensor IR (Future)
This document captures a future milestone for a tensor-first intermediate representation (IR) and optional accelerator backends.
- Represent tensor operations as first-class IR nodes.
- Enable optional lowering to NPU/GPU backends.
- Preserve verification-first semantics (no proof, no run).
In-scope for the first Tensor-IR milestone:
- A small, explicitly-typed, explicitly-shaped tensor IR.
- Deterministic semantics (same input -> same output) for all supported ops.
- A printer (text form) suitable for golden tests.
- A minimal lowering path from Curlee AST/bytecode compiler into Tensor IR (even if behind a flag).
Out-of-scope initially (must fail fast with a clear diagnostic if encountered):
- Automatic shape inference beyond trivial consistency checks.
- Data-dependent shapes / dynamic rank.
- Non-deterministic parallel scheduling.
- Approximate math kernels (e.g., “fast math”, non-deterministic reductions).
- Any backend execution that cannot be proven deterministic and resource-bounded.
Tensor IR is a pure, effect-free graph/program representation where each op has:
- An op name + fixed semantics.
- Typed operands and a typed result.
- An explicit output shape.
- Explicit preconditions (e.g., shape compatibility).
The IR should be serializable/printable in a stable format.
Minimal model (MVP):
- DType:
i32,i64,f32(optional at first;i32alone is acceptable for MVP). - Shape:
rankplus a vector of dimensions.- Dimensions are compile-time known integers in the first milestone.
- “Unknown” dimensions and symbolic constraints are out-of-scope.
Shape inference policy:
- Out-of-scope to infer shapes from dataflow.
- In-scope to check that declared shapes are consistent with op rules (e.g., matmul inner dims match).
Start small and decidable. A plausible first op set:
-
const: create constant tensor (shape + dtype + literal payload) -
zeros: allocate zero tensor (shape + dtype) -
add,sub,mul: elementwise (same shape; broadcasting out-of-scope) -
matmul: 2D matrix multiply ([m,k] x [k,n] -> [m,n]) -
reshape: view/reshape with same element count -
transpose: permute axes (explicit permutation) -
slice: take a rectangular slice (static indices) -
reduce_sum: reduce along an axis (defined evaluation order; see determinism)
Every op must declare:
- Shape rule (precondition + result shape)
- DType rule
- Fuel cost model (see resource bounds)
Curlee’s VM must be deterministic; tensor execution inherits this requirement.
Rules:
- The IR semantics define a single evaluation order (no implicit parallelism).
- Reductions (like
reduce_sum) must define a strict fold order. - Floating-point behavior must be pinned:
- Either restrict MVP to integer-only tensors, OR
- Specify IEEE-754
f32semantics on the CPU reference backend and require all backends to match bit-for-bit.
Backends are only permitted if they can demonstrate conformance to the deterministic semantics.
Potential lowering pipeline:
- Curlee AST/type-checker identifies tensor-typed values and emits Tensor IR instead of (or alongside) scalar bytecode.
- Tensor IR is printed for debugging/golden tests.
- Tensor IR is lowered to:
- CPU reference executor (interpreter) for correctness and determinism.
- Optional backend interface for accelerator execution (future).
If any proof obligation for tensor execution cannot be discharged, the compiler must refuse to run.
Implement a CPU reference executor that:
- Interprets Tensor IR ops in program order.
- Uses a deterministic memory layout (e.g., row-major) and a stable allocator.
- Implements all ops in straightforward scalar loops (correctness > speed).
- Accounts resource usage with fuel:
- Charge per op + per element (e.g.,
matmulchargesm*k*n). - Hard-stop once fuel is exhausted, with an error that points to the op span.
- Charge per op + per element (e.g.,
This executor is the “source of truth” for backend conformance.
- Golden tests for the Tensor IR printer (stable text output).
- Unit tests for shape-checking rules per op (good diagnostics on mismatch).
- Determinism tests: same program executed twice yields identical output bytes.
- Backend conformance tests (future): run a fixed corpus through CPU reference vs backend, byte-for-byte compare.
- What tensor shapes and dtypes are in the MVP scope?
- How are effects and capabilities modeled for accelerator execution?
- How does the IR preserve contracts and refinement info?
- What is the minimal backend interface for deterministic execution?
- How do we enforce resource bounds (fuel) on accelerator runs?