Building¶
What has to be built, how, and what a wheel carries. For the short version, see getting started.
The submodule¶
This repository is hk, the language and its compiler. Its backend, the
HipKittens C++ tile library with its RDNA3 and RDNA4 ports and its CDNA3 and
CDNA4 headers, is the third_party/HipKittens submodule, pinned to the commit the passes were
validated against. Clone with --recursive, or run git submodule update
--init.
The submodule tracks the rdna branch of
https://github.com/vivienfanghuagood/HipKittens.git (.gitmodules). The pin
matters: the passes encode facts about those headers, down to how many
ds_reads a load issues (python/hk/runtime/compile.py).
MLIR and the dialect¶
The backend is the stock hipcc compiling the translated C++ against
HipKittens' include/rdna3, so nothing at run time needs LLVM. The dialect
does, once:
git clone --recursive https://github.com/vivienfanghuagood/hk-dsl.git && cd hk-dsl
scripts/build_mlir.sh # MLIR 23.1.3 only, X86 target, Python bindings (~20 min on 64 cores)
scripts/build_hk_mlir.sh # the dialect, passes, translation and hk._mlir (~1 min)
scripts/build_wheel.sh # optional: a wheel for this interpreter, headers included
On Ubuntu 22.04 with Python 3.10, CI's prebuilt MLIR replaces build_mlir.sh. It is the
mlir-23.1.3-install-cp310-ubuntu22.04-x86_64.tar.zst asset of the ci-mlir-23.1.3 release (889 MB).
Unpack it into /opt/hk-llvm and build the dialect with HK_LLVM_ROOT=/opt/hk-llvm (the commands are in
CONTRIBUTING).
build_mlir.sh takes LLVM_TAG, PYTHON, HK_LLVM_ROOT, JOBS and
LINK_JOBS; keep JOBS × ~2 GB under the memory limit (48 GiB of memory takes
JOBS=6 LINK_JOBS=1). The bindings land in python/hk/_mlir, about 36 MB.
From the scripts themselves:
scripts/build_mlir.shbuilds MLIR only, for the host target only (hk lowers to HipKittens C++ and hipcc does the GPU code generation, so nothing needs AMDGPU), Release with assertions, with the Python bindings for$PYTHON.LLVM_TAGdefaults tollvmorg-23.1.3andHK_LLVM_ROOTto/dev/shm/hk-llvm; source, build and install directories default to$HK_LLVM_ROOT/llvm-project,build-cpXYandinstall-cpXYand can be set withLLVM_SRC,LLVM_BUILDandLLVM_INSTALL. It needsnanobind>=2.9,<3.0,numpyandPyYAMLfor$PYTHON. With no network, put a checkout at$HK_LLVM_ROOT/llvm-projectfirst; the clone is skipped when one is there.JOBSdefaults tonproc, which inside a container is the node's core count, not the cgroup's.scripts/build_hk_mlir.shbuildsmlir/against that install and installs thehk._mlirpackage intopython/hk/_mlir. The build directory is$HK_MLIR_BUILD(default$HK_LLVM_ROOT/hk-build-cpXY);LITnames thelitto use for the tests. Afterwardsninja -C $HK_MLIR_BUILD check-hkruns the lit tests (testing).
Bindings are per CPython version, so a machine that runs two interpreters
builds both scripts twice, once with each PYTHON.
Wheels¶
Wheels. scripts/build_wheel.sh builds one per interpreter, because
hk._mlir is nanobind extension modules for one CPython:
hk_dsl-<version>-cp3XY-cp3XY-linux_x86_64.whl, about 11-13 MB each. Each
carries the package, include/ (the generated kernels include it at JIT time)
and the bindings. A wheel built for another interpreter installs and
then fails at import, so the script refuses to build one. The link-time
libHKPythonCAPI.so symlink is dropped, because a wheel would carry it as a
second 34 MB copy. Installed outside the checkout, each wheel
traces, translates, compiles against its own headers and launches on the
W7900D, with a result equal to torch's bit for bit.
scripts/build_wheel.sh # python3, wheel in dist/
PYTHON=/opt/venv/bin/python3 ISOLATION=0 scripts/build_wheel.sh
pip install dist/hk_dsl-*-cp310-cp310-linux_x86_64.whl
Releases carry the cp310, cp311, cp312 and cp313 wheels as assets
(gh release create vX.Y.Z dist/*.whl), and a manylinux copy of each, because
PyPI refuses the linux_x86_64 tag. scripts/manylinux_tag.sh makes them: it
retags each wheel with the tag auditwheel says it already satisfies. All four
wheels are built on glibc 2.35 and so are manylinux_2_35:
- cp310 on Ubuntu 22.04 with its python3;
- cp312 in the vLLM ROCm image (Ubuntu 22.04, gcc 11.4, /usr/bin/python3 3.12);
- cp311 and cp313 in scripts/wheel-builder.Dockerfile (Ubuntu 22.04, gcc 11.4, deadsnakes'
3.11 and 3.13, each in a venv with pinned setuptools and wheel).
Each builds hk._mlir against the same MLIR install. Nothing is grafted and the binaries are not
touched, since the wheels link only libc, libm, libgcc_s and libstdc++. A cp311 or cp313 build takes
about 40 s on top of the MLIR install, and adds 12.5 MB per wheel to the release.
Build cp312 on glibc 2.35, not newer. A wheel's tag is the glibc it was
built against, and a newer one is not accepted by an older system. A wheel
built on Ubuntu 24.04 needs GLIBC_2.38 and does not install in the vLLM ROCm
images (glibc 2.35). Building in the vLLM image takes three changes from the rocm/pytorch recipe: run it with
--entrypoint bash; use /usr/bin/python3 with explicit
-DCMAKE_C_COMPILER=/usr/bin/gcc -DCMAKE_CXX_COMPILER=/usr/bin/g++; and, with
cmake from pip, put its cmake/data/bin on PATH, because the bin/cmake
wrapper names an interpreter the image lacks.
scripts/check_wheel_glibc.py checks the result. For every ELF file in a
wheel it reads the symbol versions it requires (readelf -V). It fails if the
highest GLIBC_2.N is above the wheel's manylinux_2_M tag, since such a
wheel installs on glibc 2.M and then fails at import. With auditwheel
importable, it also checks every GLIBCXX, CXXABI and GCC version against
the tag's policy. CI runs it on the IR leg against the cp310 wheel it builds,
and tests/hk/ir/test_check_wheel_glibc.py tests it on wheels made from a
system binary.
PYTHON=/path/with/auditwheel/python3 scripts/manylinux_tag.sh dist/*linux_x86_64.whl
/path/with/auditwheel/python3 scripts/check_wheel_glibc.py dist/*.whl dist/manylinux/*.whl
gh release upload vX.Y.Z dist/manylinux/*.whl
.github/workflows/pypi.yml uploads a release's manylinux wheels to PyPI. It
builds nothing, because a hosted runner has neither the ROCm nor the MLIR
toolchain. It runs by hand with gh workflow run pypi.yml -f tag=vX.Y.Z, and
on every published release once the repository variable PYPI_PUBLISH is
true. It uses the PYPI_API_TOKEN secret of the pypi environment, or PyPI
trusted publishing when that secret is not set. Neither is configured yet:
the fix is the PyPI project owner's (a GitHub trusted publisher for this
repository, .github/workflows/pypi.yml, environment pypi, or a
PYPI_API_TOKEN secret in that environment). Until then a release goes up
with twine from the manylinux assets, after checking their sha256 against the
GitHub release. The release steps, and the checks that must pass before a
tag, are in releasing.
pyproject.toml and hk.__version__ say the last release's version until
the next release raises both, so a wheel built from main between releases
carries the released version's number.
What is in the wheel (scripts/build_wheel.sh, pyproject.toml):
python/hk, the package;hk/include: HipKittens' headers from the submodule and hk's own frominclude/, merged;hk/_mlir, built for that interpreter byscripts/build_hk_mlir.shwith the samePYTHON— the script checks the extension'sSOABIand stops if it does not match;hk/tuned/*.json, the shipped tuning records and routing tables, so a fresh install picks the measured schedule on its first call (autotune).
OUT changes the output directory (default dist/), and ISOLATION=0
builds with the interpreter's own setuptools (>= 64), for a machine without an
index to fetch one from. An older setuptools does not fail. Ubuntu 22.04's
system 59.6 ignores [project] and builds UNKNOWN-0.0.0-*.whl, so build the
cp310 wheel from a venv with a current setuptools. The package's dependencies are pybind11>=2.10,
numpy and typing_extensions. torch is deliberately not one: hk traces and
compiles without it, only launching needs it, and on a ROCm machine it is
already installed by something with strong opinions about which build. The
wheel also registers two entry points, vllm.general_plugins and
sglang.srt.plugins, each of which does nothing until one of its switches
is set (integration).
Wheels are Linux x86_64 only, one CPython each, and are built by hand today on the machines that have the toolchain.
Reproducible wheels¶
The same commit, built with the same toolchain, gives the same wheel byte for byte, wherever the checkout and the build directory are.
What makes a build reproducible, and where it is done:
- Paths.
mlir/CMakeLists.txtcompiles with-ffile-prefix-mapfor the checkout (/hk/src), the build directory (/hk/build), the MLIR install (/hk/mlir) and nanobind (/hk/nanobind), each both as given and with symlinks resolved, so neither assertion messages (__FILE__) nor debug info name a build-machine path. The configure output lists the maps (-- hk: -ffile-prefix-map=...).-DHK_REPRODUCIBLE_PATHS=OFFturns them off. Arguments toscripts/build_hk_mlir.shgo to the cmake configure, so extra flags need no edit:scripts/build_hk_mlir.sh -DCMAKE_CXX_FLAGS=...(CFLAGSandCXXFLAGSin the environment work too, on a fresh build directory). - RPATH. The bindings are linked with their install RUNPATH,
$ORIGIN(mlir/python/CMakeLists.txt). - Timestamps.
scripts/build_wheel.shsetsSOURCE_DATE_EPOCHto the checkout's last commit time unless it is already set, and wheel uses it for every zip entry.scripts/manylinux_tag.shrewritesRECORD, which takes the sameSOURCE_DATE_EPOCH, so export it for that step too. Without git (a worktree mounted into a container, whose.gitnames a host path) the script cannot find the commit: passSOURCE_DATE_EPOCHin. - File modes. The wheel records each file's mode, and a checkout made under
umask 077has 0600 files.build_wheel.shmakes every staged file 0644, or 0755 if it was executable, and builds underumask 022. - Order. wheel writes the entries sorted by path and
RECORDin the same order, so neither depends on the file system. The wheel carries no bytecode (__pycache__is dropped from the stage, and setuptools compiles none).
What stays fixed by the toolchain rather than by hk, and so has to be the same for two builds to match:
- the compiler and linker, whose versions are in each library's
.comment(GCC 11.4 from Ubuntu 22.04 for both, on the host for cp310 and in the vLLM image for cp312), and the system headers, named as they are (/usr/include/c++/11/...); - the MLIR install. Its static libraries carry the paths of its own build,
/dev/shm/hk-llvm/...for the CI's MLIR 23.1.3, in about 300 assertion strings. They are the same for every hk build against that install, and only a rebuild of MLIR (with LLVM's-DLLVM_USE_RELATIVE_PATHS_IN_FILES=ON) removes them; - nanobind's, setuptools' and wheel's versions.
A release build that checks itself: build twice, from two worktrees of the tag in different directories with different build directories, and compare.
SOURCE_DATE_EPOCH=$(git log -1 --format=%ct vX.Y.Z); export SOURCE_DATE_EPOCH
# ... scripts/build_hk_mlir.sh + scripts/build_wheel.sh + scripts/manylinux_tag.sh, twice ...
sha256sum a/*.whl b/*.whl # equal pairs: done
python3 -c 'import sys, zipfile; a, b = (zipfile.ZipFile(p) for p in sys.argv[1:]); \
print([n for n in a.namelist() if a.read(n) != b.read(n)])' a/X.whl b/X.whl
If the sha256s differ, the second line names the files that do. For a .so,
readelf -n shows whether the build-id differs and strings -a | grep /
finds a path that leaked; for the zip layer, compare ZipInfo.date_time and
external_attr. The maintainers' release script does all of this, builds cp310 on the host and cp312 in the
vLLM image, runs the glibc check, and writes SHA256SUMS; a rebuild of the same tag checks
against it with sha256sum -c SHA256SUMS.
Using a checkout without a wheel¶
With hk._mlir built, put python/ on PYTHONPATH — which is what
examples/ and tools/hk-bench/ do — or run pip install -e . from the repo
root. In a checkout the generated C++ finds its headers in include/ and the
submodule; HK_INCLUDE_DIR overrides that (compiling).
CI¶
CI is two workflows. ci.yml, on GitHub's runners, on every push and pull
request, builds the dialect against a prebuilt MLIR 23.1.3 and runs the two
tiers that need no GPU. gpu.yml, on self-hosted runners that the maintainers
operate, on pushes to main and by hand, runs the GPU tier on gfx950, gfx1100
and gfx1201; its jobs gfx950 and gfx1100 are main's required checks. How,
what each costs, and how to make a new MLIR archive:
testing and the GPU tier.
Layout¶
hk/dsl/ the language: @kernel / @func, values and operators, the AST
rewrite, control flow, hk.arch / lds / math / group / collective
hk/types.py dtypes and the tile / vector / tensor types a kernel is written in
hk/compiler/ the MLIR context, the pass pipeline, the translation entry point
hk/_mlir/ the built bindings (hk dialect + MLIR core), not checked in
hk/target/ gfx1100 / gfx1201 / gfx942 / gfx950 facts, as data
hk/codegen/ the module boundary around a translated kernel (scaffold.py)
hk/runtime/ arch detection, hipcc + content-hash cache, the resource gate,
the code-object checker (cocheck.py), and the torch.ops
registration (torch_ext.py)
hk/autotune/ schedule search: build the space, keep what passes the gate, time;
and the linear routes' measurement (routes.py)
hk/tuned/ the schedules that search picked, and the measured routing
tables, as shipped data
hk/ops/ kernels written in the DSL, the SDPA drop-in (sdpa.py), and
HipKittens' CDNA4 kernels wrapped as hk ops (hipkittens.py)
hk/integration/ patching a running vLLM or SGLang; engine.py is what every
patch shares (op, routes, counters, CUDA-graph contract)
mlir/ the hk dialect (ODS), its passes, the C++ translation,
hk-opt / hk-translate, lit tests
include/ hk's own headers: pyutils/hk_bind.cuh (the fast pybind entry)
and hk/cdna.cuh (what CDNA needs beside HipKittens)
third_party/HipKittens/
the backend: kittens.cuh and include/rdna3, rdna4, cdna3,
cdna4; also the C++ kernels hk is measured against
scripts/ building MLIR, the dialect and the wheels; the GPU suite, the
performance gate, and the GPU CI jobs that run them (ci/)
tools/hk-bench/ the benchmarks, the scoreboard, the performance gate and the
engine harnesses behind every number here
tools/cdna-probes/
the CDNA probes: coverage, the bind hazard, occupancy timing
tests/perf/baselines/
the performance gate's measured ratios and tolerances
examples/ complete kernels, each checked against torch and timed
docs/ these pages, and the development record (docs/history)
The hk/ paths are under python/. tests/hk/ is the test suite
(testing).