Metadata-Version: 2.4
Name: intentdsl
Version: 2.1.0
Summary: Multi-target native JIT compiler for typed Python tensor kernels and programs
License-Expression: Apache-2.0
License-File: LICENSE
Project-URL: Repository, https://github.com/Tianchen-ai/Intent
Project-URL: Documentation, https://tianchen-ai.github.io/Intent/
Project-URL: Issues, https://github.com/Tianchen-ai/Intent/issues
Requires-Python: >=3.10
Requires-Dist: compilerinfra==0.6.0
Requires-Dist: numpy>=1.24
Provides-Extra: cuda
Requires-Dist: torch; extra == "cuda"
Requires-Dist: triton>=3.0; extra == "cuda"
Provides-Extra: cutile
Requires-Dist: cuda-tile; extra == "cutile"
Provides-Extra: cpu
Requires-Dist: weft-core<0.3,>=0.2.0; extra == "cpu"
Requires-Dist: triton-weft<0.3,>=0.2.0; extra == "cpu"
Provides-Extra: docs
Requires-Dist: mkdocs-material<10,>=9.6; extra == "docs"
Requires-Dist: mkdocs-static-i18n<2,>=1.3; extra == "docs"
Requires-Dist: tomli<3,>=2; python_version < "3.11" and extra == "docs"
Description-Content-Type: text/markdown

# IntentDSL

**Write an operator algorithm once. Compile it for different execution models.**

[English](README.md) · [简体中文](README.zh-CN.md)

[Documentation Site](https://tianchen-ai.github.io/Intent/en/) · [First Route](https://tianchen-ai.github.io/Intent/en/first-route/) · [Handbook](https://tianchen-ai.github.io/Intent/en/manual/) · [Architecture](https://tianchen-ai.github.io/Intent/en/architecture/) · [Scoreboard](https://tianchen-ai.github.io/Intent/en/scoreboard/) · [V1 Archive](https://tianchen-ai.github.io/Intent/en/v1/)

IntentDSL is a Python kernel language and an MLIR compiler built on [CompilerInfra](https://github.com/Tianchen-ai/compilerinfra) for GPU, CPU, and accelerator programs. Authors describe tensor computations, logical domains, accesses, state, and explicit multi-kernel composition with `@I.kernel` and `@I.program`. Compiler passes form the physical program and lower it through five native exits: **Triton**, **cuTile**, **Mojo**, **Weft**, and **BANG C**.

IntentDSL is a component of the Dongshan Community's [TianChen AI Software Stack](https://gitee.com/dongshan-community/TianChen_Software_Stack). This repository provides the language frontend, MLIR compiler, runtime bindings, examples, and bilingual handbook ([online](https://tianchen-ai.github.io/Intent/en/) and offline in [`doc/`](doc/index.en.md)). Development is maintained on [GitHub](https://github.com/Tianchen-ai/Intent), with a [Gitee mirror](https://gitee.com/dongshan-community/Intent).

![IntentDSL paper overview: algorithm reuse and compiler workflow](doc/images/paper-fig1-overview.png)

*Original paper overview ([PDF](doc/images/paper-fig1-overview.pdf)). Target labels reflect the paper prototype; current native exits are Triton, cuTile, Mojo, Weft, and BANG C.*

- **Programmable algorithms:** compose reductions, contractions, indexed access, ordered recurrences, and multi-stage `@I.program` workflows without branching on hardware backends.
- **Reusable optimization:** shared semantic analyses in `KIR` and execution-family passes in `Block`, `CPU`, and `DSA` organize ownership, reuse, and storage; downstream compilers perform final layout and instruction lowering.
- **Ordinary Python calls:** compile once with `I.jit`, pass PyTorch tensors or native buffers, reuse warm calls across shapes within artifact guards, and inspect IR, receipts, and decisions.

## Quick Start & Showcase Examples

Prebuilt **`manylinux_2_28_x86_64` wheels are upcoming on PyPI**—bundling all five exits' compilation units (`libintentdsl.so` with static MLIR) so deployment environments need no LLVM/MLIR SDK (`[cuda]` pulls `torch` and `triton`, `[cutile]` pulls `cuda-tile`, `[cpu]` pulls `weft-core` and `triton-weft`; MLU uses the same wheel with host Neuware/`torch_mlu`, and Mojo is probed by `intentdsl doctor`). Until the PyPI release goes live, build from source on **Linux** (**Python ≥3.10**, CMake ≥3.22, Ninja, C++17, **LLVM/MLIR 20.1.8**; see [First Route](https://tianchen-ai.github.io/Intent/en/first-route/) and [Handbook](https://tianchen-ai.github.io/Intent/en/manual/)).

```sh
# Upcoming on PyPI (switch to direct pip install once published):
# python -m pip install "intentdsl[cuda,cpu]"
python -m pip install torch triton
intentdsl setup --backend triton
intentdsl doctor --backend triton
python examples/demo/rmsnorm_demo.py
python examples/first_kernel.py
python examples/formulas.py
```

- **[`examples/demo/rmsnorm_demo.py`](examples/demo/rmsnorm_demo.py):** compiles one RMSNorm kernel across available exits (Triton, Mojo, Weft), verifies FP64 accuracy, and benchmarks against PyTorch eager, `F.rms_norm`, and `torch.compile` (pass `--show-triton --show-ir` to inspect generated code and `KIR` IR).
- **[`examples/first_kernel.py`](examples/first_kernel.py):** minimal starter `@I.kernel` compiling row-wise Softmax to Triton and verifying against PyTorch:

```python
import torch
import intentdsl as I


@I.kernel
def softmax(x: I.In[I.f16, ("M", "N")], out: I.Out[I.f16, ("M", "N")]):
    for row in I.parallel(x.dim(0)):
        v = I.cast(x[row, :], I.f32)
        peak = I.reduce(v, axis=0, combine=I.max_num, init=I.neg_inf(I.f32))
        w = I.exp(v - peak)
        out[row, :] = I.cast(w / I.sum(w, axis=0, acc=I.f32), I.f16)


if __name__ == "__main__":
    x = torch.randn(64, 1024, dtype=torch.float16, device="cuda")
    y = I.jit(softmax, backend="triton").run(x)
    torch.testing.assert_close(y, torch.softmax(x.float(), dim=-1).half())
    print("softmax passed")
```

- **[`examples/formulas.py`](examples/formulas.py) & [`examples/inspect_candidate.py`](examples/inspect_candidate.py):** verifies nine core operators (ReLU, Softmax, RMSNorm, LayerNorm, Transpose, Prefix Sum, Embedding, GEMM, Q6_K GEMV) and demonstrates candidate derivation via `compilerinfra-dev`. Over 30 complete `@I.kernel` and `@I.program` operators cover attention, FlashAttention backward, paged decode / MLA, ALiBi, state-space scans, normalizations, quantized contractions, sparse access, and mutable updates.

## How the Compiler is Organized

Compiler passes consume typed semantics, def-use, coordinate relations, side effects, and target capabilities. Execution decisions are formed and verified in MLIR IR before each exit printer emits target code, allowing GPU, CPU, and DSA targets to share algorithm semantics while keeping distinct execution and storage models.

![Paper figure: one logical computation realized as a GPU program, CPU task hierarchy, and DSA local-storage program](doc/images/paper-fig5-execution-models.png)

*Original execution-model figure ([PDF](doc/images/paper-fig5-execution-models.pdf)): each execution model assigns ownership and carries state across source regions; CPU implementation requirements shape local blocks, while DSA dependencies govern input-buffer reuse.*

![IntentDSL compilation tree from Python kernel through KIR, Block, CPU tasks, and DSA to five native exits](doc/images/v2-compilation-tree.png)

*Compilation tree ([PDF](doc/images/v2-compilation-tree.pdf)): `KIR` lowers to `Block` (feeding `Triton`, `cuTile`, and `DSA` → `BANG C`) and `CPU tasks` (feeding `Mojo` and `Weft`).*

See the [Product Architecture](https://tianchen-ai.github.io/Intent/en/architecture/) ([local](doc/architecture.en.md)) and [KIR Numerical Contracts](https://tianchen-ai.github.io/Intent/en/design/kir/) ([local](doc/design/kir.en.md)) for node boundaries, exit contracts, and numerical permissions.

## Performance Across Execution Models

### Paper evaluation across GPU, CPU, and MLU

The following original paper figures compare complete operators against target-written implementations. Speedup is **reference latency / generated latency** (higher is better). Median operator latency includes preparation and required layout conversion, and excludes compilation, JIT, and tuning. See [paper evaluation notes](doc/paper-results.en.md) for measurement scope.

![Paper GPU performance: generated Triton and cuTile against Triton references on RTX 5090D and H100](doc/images/paper-eval-gpu-triton-reference.png)

*GPU ([PDF](doc/images/paper-eval-gpu-triton-reference.pdf)): eight representative workloads and the geometric mean across all 44 common workloads on RTX 5090D (1.18× Triton, 1.20× cuTile) and H100 (1.27× Triton, 1.24× cuTile) against the paper's Triton references.*

| x86 CPU · 7 native comparisons | MLU370 · 6 library comparisons |
|---|---|
| <img src="doc/images/paper-eval-cpu-native.png" alt="Paper x86 Mojo performance against five MAX-based and two independent Mojo implementations" width="360"> | <img src="doc/images/paper-eval-mlu-libraries.png" alt="Paper MLU BANG C performance against CNNL, CNNL Extra, and TMO implementations" width="360"> |
| [Original PDF](doc/images/paper-eval-cpu-native.pdf) | [Original PDF](doc/images/paper-eval-mlu-libraries.pdf) |

*CPU: seven generated x86 Mojo programs on eight physical cores within one NUMA node compared against five Modular/MAX and two independent Mojo references. MLU: six generated BANG C programs on MLU370 compared against CNNL, CNNL Extra, and TMO using CNCC/CNRT.*

### Current five-exit same-semantics scoreboard

<!-- SCOREBOARD-SUMMARY:START -->
Across RTX 5090 D, Xeon 8470Q, and MLU370, the [same-semantics scoreboard](doc/scoreboard.en.md) ([per-row CSV](https://github.com/Tianchen-ai/Intent-Bench/blob/main/results/v0.6/v0.6-semantics.csv)) evaluates **367 complete calls**, of which **348** have a same-device reference under matching numerical semantics: **200 faster, 126 tied, and 22 slower** (classified from the interquartile spread across three interleaved rounds; the remaining **19** rows on MLU370 have no same-semantics device reference because CNNL/`torch_mlu` lacks BF16, FP8, INT32-output, or device linear-algebra primitives). Each row's primary baseline is the fastest mature implementation available on the same device—including Inductor (`torch.compile`), library kernels, and eager execution—with every candidate required to pass the numerical criteria; see the [scoreboard details](doc/scoreboard.en.md).

| Exit | Device | Faster | Tied | Slower | Unpaired |
|---|---|---:|---:|---:|---:|
| Triton | RTX 5090 D | 26 | 39 | 7 | 0 |
| cuTile | RTX 5090 D | 29 | 37 | 6 | 0 |
| BANG C | MLU370 | 39 | 15 | 6 | 19 |
| Mojo | Xeon 8470Q | 55 | 16 | 1 | 0 |
| Weft | Xeon 8470Q | 51 | 19 | 2 | 0 |
<!-- SCOREBOARD-SUMMARY:END -->

Representative complete-call timings from the scoreboard:

- **FlashAttention backward:** on Xeon 8470Q (0–7 cores, fused SDPA contract), Mojo runs in **111.14 ms** vs same-round fused SDPA **134.00 ms**, and Weft runs in **107.70 ms** vs **133.73 ms**; on RTX 5090 D, Triton (**612.35 µs**) and cuTile (**660.99 µs**) both beat the Triton reference (**765.54 / 765.31 µs**); on MLU370, BANG C runs in **64.45 ms** vs ATen **72.78 ms**.
- **CPU ALiBi attention** (Xeon 8470Q, 0–7 cores, SDPA contract): Mojo **31.88 ms** vs ATen **60.21 ms**; Weft **32.70 ms** vs ATen **60.58 ms**.
- **`rms_norm_backward`** (4096×4096 BF16): RTX 5090 D Triton **102.40 µs** and cuTile **103.97 µs** tie Inductor (**102.37 µs**); Xeon (cores 0–7) Weft **3.73 ms** and Mojo **4.37 ms** both beat Inductor (**8.47 ms**).

Independent baselines, numerical verdicts, and raw CSVs are maintained in [Intent-Bench](https://github.com/Tianchen-ai/Intent-Bench).

## Documentation, Agents & Contributing

- **Online Documentation (`https://tianchen-ai.github.io/Intent/`):** [English Handbook](https://tianchen-ai.github.io/Intent/en/manual/) · [中文工具书](https://tianchen-ai.github.io/Intent/manual/) · [First Route](https://tianchen-ai.github.io/Intent/en/first-route/) · [Scoreboard](https://tianchen-ai.github.io/Intent/en/scoreboard/)
- **Agent & Developer Entry (`5 min`):** build out-of-source (`cmake -S . -B build/intent`), then run `compilerinfra-dev --workspace . layout`, `components`, `doctor`, and `bench-describe <workload>` (or `intentdsl layout` / `intentdsl doctor` / `intentdsl manual --lang en`; see [`AGENTS.md`](AGENTS.md) and [`agent/skills/intentdsl-development.md`](agent/skills/intentdsl-development.md)).
- **Extending Examples & Benchmarks:** place new `@I.kernel` / `@I.program` operators under `examples/<domain>/` and minimal IR reproducers under `test/<domain>/`. In [Intent-Bench](https://github.com/Tianchen-ai/Intent-Bench), register workloads in `src/intent_bench/workloads/<domain>.py`, library providers in `src/intent_bench/providers/<lib>.py`, and licensed upstream baselines in `src/intent_bench/references/<upstream>/`, then run `INTENT_ROOT="$PWD" intent-bench run <workload> ...` in-place on the target machine.
- **Contributing & Issues:** start with [`doc/architecture.en.md`](doc/architecture.en.md) and [`doc/manual.en.md`](doc/manual.en.md); enable `.githooks/pre-push` (`git config core.hooksPath .githooks`) before pushing, and report reproducible issues in [GitHub Issues](https://github.com/Tianchen-ai/Intent/issues).

IntentDSL is licensed under [Apache-2.0](LICENSE). Third-party components retain their own notices and licenses.
