Metadata-Version: 2.4
Name: dace-alloy
Version: 0.1.1
Summary: Speculative per-scope code generation backend for DaCe: sweep, verify, and report rendering candidates.
Author-email: Yakup Budanaz <budanaz.yakup@gmail.com>
License-Expression: GPL-3.0-or-later
Project-URL: Homepage, https://github.com/ThrudPrimrose/dace-alloy
Project-URL: Changelog, https://github.com/ThrudPrimrose/dace-alloy/blob/main/CHANGELOG.md
Keywords: dace,sdfg,code-generation,compilers,autotuning,hpc
Requires-Python: >=3.12
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy
Requires-Dist: pydantic>=2
Requires-Dist: dace-extended>=1.0.0
Requires-Dist: sympy
Requires-Dist: pydantic-core
Requires-Dist: tree-sitter-language-pack
Provides-Extra: bench
Requires-Dist: hpcagent-bench>=1.0.0; extra == "bench"
Provides-Extra: nvgpu
Requires-Dist: dace-alloy[bench]; extra == "nvgpu"
Requires-Dist: cupy-cuda13x; extra == "nvgpu"
Provides-Extra: amdgpu
Requires-Dist: dace-alloy[bench]; extra == "amdgpu"
Requires-Dist: cupy; platform_machine == "x86_64" and extra == "amdgpu"
Provides-Extra: dev
Requires-Dist: dace-alloy[bench]; extra == "dev"
Requires-Dist: mpmath; extra == "dev"
Requires-Dist: mypy; extra == "dev"
Requires-Dist: pre-commit; extra == "dev"
Requires-Dist: pyright; extra == "dev"
Requires-Dist: pytest; extra == "dev"
Requires-Dist: pytest-timeout; extra == "dev"
Requires-Dist: pytest-xdist; extra == "dev"
Requires-Dist: ruff==0.17.6; extra == "dev"
Provides-Extra: softhier
Requires-Dist: argparse==1.4.0; extra == "softhier"
Requires-Dist: beautifulsoup4==4.15.0; extra == "softhier"
Requires-Dist: hjson==3.1.0; extra == "softhier"
Requires-Dist: html2text==2025.4.15; extra == "softhier"
Requires-Dist: lz4==4.4.5; extra == "softhier"
Requires-Dist: mako==1.4.3; extra == "softhier"
Requires-Dist: marko==2.2.5; extra == "softhier"
Requires-Dist: matplotlib==3.11.2; extra == "softhier"
Requires-Dist: mistune==3.4.0; extra == "softhier"
Requires-Dist: ninja==1.13.2; extra == "softhier"
Requires-Dist: pandas==3.0.6; extra == "softhier"
Requires-Dist: pexpect==4.9.0; extra == "softhier"
Requires-Dist: ppk2_api==0.9.2; extra == "softhier"
Requires-Dist: prettytable==3.19.0; extra == "softhier"
Requires-Dist: psutil==7.2.2; extra == "softhier"
Requires-Dist: pycryptodome==4.5.0; extra == "softhier"
Requires-Dist: pyelftools==0.33; extra == "softhier"
Requires-Dist: pytablewriter==1.2.1; extra == "softhier"
Requires-Dist: pyvcd==0.5.1; extra == "softhier"
Requires-Dist: pyyaml==6.0.3; extra == "softhier"
Requires-Dist: rich==15.0.0; extra == "softhier"
Requires-Dist: six==1.17.0; extra == "softhier"
Requires-Dist: tabulate==0.10.0; extra == "softhier"
Provides-Extra: dev-nvidia
Requires-Dist: dace-alloy[dev,nvgpu]; extra == "dev-nvidia"
Provides-Extra: dev-amd
Requires-Dist: dace-alloy[amdgpu,dev]; extra == "dev-amd"
Dynamic: license-file

# Alloy

Per-scope code generation backend for DaCe. Input: a dace numpy program with symbolic sizes (as an SDFG).
Alloy splits it into scopes, renders each scope under many codegen knobs, compilers and FP levels,
verifies every candidate, times the survivors, and links the best into one program plus a report. On
HPCAgent-Bench it is a user plugin (`alloy/hb_plugin`): agents give a manifest with C or NumPy and read the report.

    pip install dace-alloy          # imports as `alloy`; pulls DaCe extended (PyPI dace-extended, imports as `dace`)
    uv sync --extra dev             # a checkout of https://github.com/ThrudPrimrose/dace-alloy

```python
import pathlib
from alloy import api
from alloy.frontend import inputs
from examples.two_scopes import two_scopes

manifest = inputs.load(pathlib.Path("examples/two_scopes.yaml"), "examples/two_scopes.py:two_scopes")
if __name__ == "__main__":  # Alloy's worker processes re-import this script
    result = api.generate(two_scopes.to_sdfg(), pathlib.Path("out"), manifest=manifest)  # result.report: report.md
```

As DaCe's backend no guard is needed: set `compiler.build_plugin = alloy-speculative` and call `sdfg.compile()`.

Extras: the core (outline, sweep, build) needs only DaCe. `bench` adds HPCAgent-Bench, which verification and
timing use for manifest inputs and the tolerance band. `nvgpu` / `amdgpu` add the matching cupy build. `dev` is
`bench` plus the test tools (CPU, what CI installs); `dev-nvidia` and `dev-amd` add the GPU extra.

Inputs come from an HPCAgent-Bench style manifest next to the program (`two_scopes.yaml`: sizes, a `fuzzed` range,
an `args` block); without one, candidates are built but not verified.

Layout:

- `alloy/frontend/`: `scopes` outlines a program, `inputs` draws its inputs from a bench manifest, `tree` prints the
  control-flow tree the report carries, `cnumpy` lifts a C kernel plus the bench's binding JSON to canonical NumPy and
  its SDFG (`python -m alloy.frontend.cnumpy kernel.c kernel_binding.json --sdfg kernel.sdfg`; the C it accepts is
  in `alloy/hb_plugin/skills/alloy-c/SKILL.md`).
- `alloy/backend/`: `candidates` enumerates the sweep, `build` renders, compiles and checks one candidate, `intrinsics`
  rewrites admitted innermost loops into checked intrinsic DaCe tasklets, `runtime` resolves and checks the OpenMP
  runtime, `sweep` builds every candidate.
- `alloy/evaluate/`: `link` builds whole programs from scope archives, `verify` checks candidates on fuzzed inputs,
  `timing` times them and links alloy-full.
- `alloy/isa/`: the machine abstract math ISA (`ops.py`, 37 ops across 414 CPU rows + 22 assists in `intrinsics.toml`,
  390 `llvm-mca` cost rows in `costs.toml`, 19 precision-bounded math functions in `mathkernels.toml`, 26 CUDA + 26 HIP
  device rows in `simt.toml`, 23 Tensix rows + 2 card-checked SFPI rungs in `tensix.toml`, 14 SoftHier ops in
  `softhier.toml`, and tile kernels in `alloy_tile_{cpu,sme,sm90,cdna}*`) exposed through one CLI:
  `python -m alloy.isa host|lookup|reverse|assist|kernel|tiers|time|validate|skeleton|ops|isas|schedule|tile|ask|ask-all`.
  `validate.py` runs a row's intrinsic or recipe in a compiled kernel, natively or under `qemu-aarch64` when that is on
  PATH, or re-measures its tier, before an agent sees it; a row that did not run stays `unverified`, however it compiled;
  `hints.py` builds the do-it-yourself response from a scope's tasklets when emission fails; `schedules.py` emits
  compile-and-run-verified C skeletons per loop schedule, with the compiler flags that ask gcc/clang for the same shape.
  This is the bridge between DaCe's auto-vectorized intrinsic code and the math an agent reasons in.
- `alloy/heuristics/`: what Alloy picks without timing: `predict`, `prune`, `fission`, `fp`, `ranges`.
- `alloy/tensixgen/`: Tenstorrent Tensix code generation from DaCe tile SDFGs (`backend.py`, `split.py`, `ttsim.py`),
  lowering checked `tensix.toml` rows and automatically substituting card-checked SFPI rungs where `tt-metal` 0.63 misses
  f32 tolerance.
- `alloy/softhiergen/`: SoftHier v1 code generation as a DaCe target (importing it registers the storages, the
  schedule and the expansions); `program.generate` lowers an SDFG to one C++ unit, and `gate/` checks the programs on
  GVSoC on ault. `docs/softhiergen.md` describes the code as built; update it with every change of behavior.
- `alloy/`: `api` (`generate`, the entry point), `dace_backend` (Alloy behind `SDFG.compile`), `gpu` / `gpu_build` /
  `gpu_matrix.toml` (GPU threadblock speculation, `scalar_decl`, intrinsic maps, and profile coordinate descent),
  `guidance` (advice for the scopes left short), `machine` (cores, cache), `report` (builds `report.json` schema 8,
  renders `report.md` from it, loads it back), `records` (the strict pydantic configuration of the timing, verdict,
  report and manifest records). Every CPU compiler, flag and option is in `alloy/configuration_matrix.toml`, validated
  by the pydantic models of `alloy/configuration_matrix.py`.
- `examples/`: input programs for Alloy (dace numpy with symbolic sizes).
- `samples/`: A/B studies. Each holds a slow and a fast rendering of one kernel, the harness, and the setup that
  measured the gap.
- `tests/`: one file per module, plus the cross-compiler OpenMP link test.
- `docs/`: design decisions with build status, measurement rules, related work.

Tests: `pytest -m "not icx"` on a box without Intel oneAPI (each compiler is a pytest mark); CI installs gcc,
clang, oneAPI and the NVIDIA HPC SDK and runs all but the `avx512`, `cuda` and `hip` marks, including the
cross-compiler OpenMP link test.

Notes: `docs/design.md` (decisions), `docs/measurement.md` (A/B rules), `docs/related_work.md` (citations).

## How to extend it

A run goes `frontend/scopes` (outline), `backend/candidates` (the sweep), `heuristics/prune` (drop what cannot change
the code), `backend/sweep` and `build` (render and compile), `evaluate/verify` (fuzzed inputs), `evaluate/timing`
(screen and final reps, link `alloy_full`), `report`. Add to the step that owns the decision.

- **A compiler, FP level, ISA, tail or vector library.** Edit `alloy/configuration_matrix.toml`; add a field to its
  pydantic model in `alloy/configuration_matrix.py` only for a new kind of setting. `one_factor_at_a_time` sweeps what
  the file lists, and every candidate is verified, so no Python changes for a new row.
- **A sweep axis.** Add the factor to `Candidate` and its flips to `backend/candidates.py`, then tell `heuristics/prune`
  when the factor cannot change the object. The prune reason goes in the report, so an axis that looks useless shows up
  there.
- **An ISA row.** Add `[isa.<isa>.<dtype>.<op>]` to `alloy/isa/intrinsics.toml`, with the spelling checked against the
  compiler's headers; a composite needs a `recipe` and the `code` that states its operand order. A new op also goes in
  `OPS` in `alloy/isa/ops.py`, which holds the math, never a spelling. Run `python -m alloy.isa validate`: a row is
  compiled and executed natively, or under `qemu-aarch64` when that and `aarch64-linux-gnu-gcc` are on PATH. A row that
  only cross-compiled stays `unverified`. Do not mark a row verified by hand.
- **A math kernel.** `alloy/isa/mathkernels.py` and `helpers/mathfit.py`, which fits the coefficients. State the level
  (`strict`, `relaxed`, `fast`) and re-measure the ulp bound the row cites.
- **A verification rule.** `alloy/evaluate/verify.py`. A candidate is judged by its outputs on fuzzed inputs against the
  reference, so a new check must be able to fail a candidate that builds and runs.
- **A hint for agents.** `alloy/isa/hints.py` reads a scope's tasklets when emission fails; `alloy/guidance.py` advises
  on the scopes left short.
- **A bench tool or packet.** `alloy/hb_plugin`: the tool in `tools/alloy.py`, its judge half in `routes/alloy.py`, the
  packet in `plugin.toml`, prompt changes in `prompt/overlay.yaml`, and the agent-facing text in `skills/`. The judge
  half runs Alloy in a process of its own, because the judge's threads never fork.

After any change to what a scope builds, run the whole suite: `uv run pytest -m "not icx" -n 3 --maxfail=10`, and
`BENCH_BRANCH=<branch> bash helpers/bump_pins.sh` when the bench pin or a sibling release moves.
