|
| 1 | +# NineToothed Multi-Backend IR Design |
| 2 | + |
| 3 | +## Objectives |
| 4 | + |
| 5 | +NineToothed currently exposes a tensor-oriented metaprogramming DSL and lowers it to Triton. The target backend programming languages are now: |
| 6 | + |
| 7 | +- Triton: keep the current production path and use it as the correctness baseline. |
| 8 | +- CUDA: add native CUDA source generation while preserving the current AOT CUDA launcher path. |
| 9 | +- TileLang: add a TileLang path for tile-level scheduling experiments. |
| 10 | +- TVM: add a TensorIR/TVMScript path for a widely used tensor compiler stack. |
| 11 | + |
| 12 | +SGLang is intentionally excluded from this backend list because it is an inference engine, not a kernel programming language target. |
| 13 | + |
| 14 | +## IR Stack |
| 15 | + |
| 16 | +The design should remain multi-level instead of forcing all targets through one flat IR: |
| 17 | + |
| 18 | +- TOM Graph IR: tensor arrangement history, shapes, strides, jagged metadata, dtypes, constexpr values, and symbolic constraints. |
| 19 | +- Program IR: loads, stores, pointer/index expressions, masks, arithmetic, dot, reductions, math intrinsics, helper calls, and function boundaries. |
| 20 | +- Schedule IR: grid/block decomposition, thread/warp mapping, memory scopes, vectorization, unrolling, pipelining, and target capabilities. |
| 21 | +- Target IR: backend-specific syntax, ABI metadata, runtime dispatch metadata, and compiler flags. |
| 22 | + |
| 23 | +## First Implementation Slice |
| 24 | + |
| 25 | +The current implementation slice adds a compact `KernelIR` bridge: |
| 26 | + |
| 27 | +- `TensorTypeIR` records public tensor parameter metadata. |
| 28 | +- `LaunchIR` records launch function name, launch args, and grid text. |
| 29 | +- `KernelIR` records canonical source, entrypoint, launch metadata, compiler options, and extensible metadata. |
| 30 | +- Backend lowerers consume `KernelIR` and return `BackendArtifact`. |
| 31 | + |
| 32 | +This bridge does not replace the Triton generator yet. It creates a stable backend contract while the production Triton path remains untouched. |
| 33 | + |
| 34 | +## Backend Status |
| 35 | + |
| 36 | +| Backend | Current Status | Execution Status | |
| 37 | +| --- | --- | --- | |
| 38 | +| Triton | Existing production path plus artifact lowerer | Executable | |
| 39 | +| CUDA | Emits `.cu` manifest and ABI shell; existing `caller="cuda"` AOT remains the only executable CUDA path | Native source shell is not executable | |
| 40 | +| TileLang | Emits TileLang Python module shell and metadata | Not executable | |
| 41 | +| TVM | Emits TVMScript module shell and metadata | Not executable | |
| 42 | + |
| 43 | +## API Direction |
| 44 | + |
| 45 | +```python |
| 46 | +artifact = ninetoothed.lower(arrangement, application, tensors, backend="triton") |
| 47 | +cuda_artifact = ninetoothed.lower(arrangement, application, tensors, backend="cuda") |
| 48 | +tilelang_artifact = ninetoothed.lower(arrangement, application, tensors, backend="tilelang") |
| 49 | +tvm_artifact = ninetoothed.lower(arrangement, application, tensors, backend="tvm") |
| 50 | +``` |
| 51 | + |
| 52 | +Existing `make`, `jit`, and `aot` calls should keep working. New backend selection should remain explicit until native CUDA, TileLang, and TVM lowering are feature complete enough to pass the full correctness gates. |
| 53 | + |
| 54 | +## Lowering Roadmap |
| 55 | + |
| 56 | +1. Keep the bridge IR and backend registry in place. |
| 57 | +2. Extract TOM Graph IR from `Tensor` arrangement history instead of reflecting only generated source. |
| 58 | +3. Split semantic lowering from Triton rendering. |
| 59 | +4. Add Program IR statement and expression nodes for loads, stores, masks, arithmetic, dot, reductions, and intrinsics. |
| 60 | +5. Re-render Triton from Program IR and prove parity with the existing generator. |
| 61 | +6. Implement native CUDA rendering for elementwise, reductions, and matmul-style tiled dot. |
| 62 | +7. Implement TileLang rendering for elementwise, reductions, and matmul-style tiled dot. |
| 63 | +8. Implement TVM TensorIR rendering for elementwise, reductions, and matmul-style tiled dot. |
| 64 | +9. Add target capability descriptions for AscendC, BangC, and similar vendor languages. |
| 65 | + |
| 66 | +## Validation Plan |
| 67 | + |
| 68 | +The acceptance gate is intentionally strict: |
| 69 | + |
| 70 | +- All existing NineToothed tests pass. |
| 71 | +- All `ntops` tests pass. |
| 72 | +- All runnable `ntops.lab` operators pass. |
| 73 | +- Backend artifact audit passes for Triton, CUDA, TileLang, and TVM. |
| 74 | +- Executable backend artifacts compile and run. |
| 75 | +- Outputs match existing PyTorch/reference checks. |
| 76 | + |
| 77 | +Until CUDA, TileLang, and TVM emit real executable target code, backend compatibility must be reported as incomplete. |
0 commit comments