Skip to main content

hk-dsl

PyPI CI License: MIT

Fast AMD GPU kernels in a few lines of Python. Write the math of an element, a row or a GEMM epilogue; hk-dsl turns it into a tuned kernel for the Radeon (gfx1100) or Instinct (gfx950) GPU you are on. For full control, write the kernel in HipKittens' tiles. Either way it is traced into an MLIR dialect whose passes refuse silent failures, translated to HipKittens C++, compiled by hipcc and called from torch. python -m hk.check says whether it is right and whether it beats torch.compile.

Quick start

pip install hk-dsl (ROCm's hipcc and a ROCm torch besides), then save a file:

# my_kernels.py
import hk
from hk import templates as T

gelu_and_mul = T.elementwise(lambda g, u: hk.math.gelu(g) * u)  # the math of one element

@T.rowwise                                                       # the math of one row
def rms_norm(x, w, *, EPS=1e-6):
    return x * (T.row_mean(x * x) + EPS).rsqrt() * w

def bias_gelu(acc, ctx):                                         # traced into a tuned GEMM's store
    acc += ctx.bias
    return hk.templates.gemm.gelu_tanh(acc)

def linear_bias_gelu(x, w, b):
    return hk.templates.gemm(x, w, epilogue=bias_gelu, bias=b)

call it with torch tensors:

import torch
from my_kernels import gelu_and_mul, rms_norm, linear_bias_gelu

x = torch.randn(4096, 2 * 14336, device="cuda", dtype=torch.bfloat16)  # a gate/up output
h = torch.randn(4096, 4096, device="cuda", dtype=torch.bfloat16)
w = torch.randn(4096, device="cuda", dtype=torch.bfloat16)
W = torch.randn(12288, 4096, device="cuda", dtype=torch.bfloat16) / 64
b = torch.randn(12288, device="cuda", dtype=torch.bfloat16)

y = gelu_and_mul(x[:, :14336], x[:, 14336:])  # the two halves, read in place
n = rms_norm(h, w)
z = linear_bias_gelu(h, W, b)                  # F.gelu(F.linear(h, W, b), approximate="tanh")

and check a kernel against torch:

python -m hk.check my_kernels.py:rms_norm \
    --ref "lambda x, w: x * torch.rsqrt((x * x).mean(-1, keepdim=True) + 1e-6) * w" \
    --inputs "bf16[4096,4096], bf16[4096]"

hk.check reports registers, occupancy and spills, the error against the reference, and the time against torch eager and torch.compile; it exits 0 (pass), 1 (wrong) or 2 (error). Each block runs as shown on a W7900D and an MI355X. Guides: elementwise/rowwise, GEMM epilogues, hk.check.

Results at a glance

Speedup of hk.templates over torch.compile, W7900D then MI355X: GELU-and-mul 1.03x, 1.11x; the same op on 16x64 tiles 0.83x, 0.78x; add+RMSNorm+int8 1.17x, 1.45x; cross-entropy 1.58x, 2.50x; MoE router 7.37x, 9.06x; linear+bias+GELU 1.68x, 1.20x; linear+residual 1.71x, 1.20x.

Hollow: the same op by hand on 16x64 tiles, the slow way. The chart's script reads the committed tables.

Radeon PRO W7900D (gfx1100) Instinct MI355X (gfx950)
templates against hk's tuned kernel for the same job 0.92-1.02x 0.98-1.03x
GEMM + epilogue against torch.compile faster at 48 of 48 layer shapes, geomean 1.69x faster at 46 of 48, level at 2
vLLM, Qwen3-8B, per request against stock (A1, B1) 1.22-1.85x at 1-32 requests (vLLM 0.31.0, torch 2.13.0+rocm7.2, triton-rocm 3.7.1) 1.13x at 16, 1.21x at 64
bf16 GEMM against the field (A2, B2) ties FlyDSL, 1.28-1.39x hipBLASLt 0.9x or better of the best at all 122 LLM shapes
where it loses (A4, B4) paged decode 0.86-1.12x Triton's GEMMs 0.79-1.21x aiter's, norms 0.74-1.05x, MoE 0.66-0.94x CK's

One GPU of each; every comparison interleaved in one process. Performance has the method, the software stack of every table and every loss.

The language, when you need control

import hk

@hk.kernel(grid=lambda p: (p.o.rows * hk.cdiv(p.o.cols, p.CW), 1, 1))
def gelu_mul(a: hk.Tensor[hk.bf16], b: hk.Tensor[hk.bf16], o: hk.Tensor[hk.bf16], *, CW=512):
    S = hk.RS(hk.fp32, 1, CW, 8)                  # a register strip: one row, 8 bf16 (16 B) a lane
    per_row = hk.cdiv(o.cols, CW)
    row = hk.arch.block_idx.x // per_row
    c0 = (hk.arch.block_idx.x - row * per_row) * CW
    x = a.at[row, c0].load(S, bounded=True)       # bf16 -> fp32 on the load; past the end reads 0
    x.gelu(into=x)                                # hk.math.gelu: the tanh form, fp32 only
    x *= b.at[row, c0].load(S, bounded=True)
    o.at[row, c0].store(x, bounded=True)          # fp32 -> bf16 on the store

print(gelu_mul.trace().dump())      # the hk MLIR module -- needs nothing
print(gelu_mul.source())            # the HipKittens C++ -- needs nothing
print(gelu_mul.build().report())    # hipcc and the resource gate -- no GPU

It ties torch.compile on an MI355X (check). More: the language, examples.

flowchart LR
    A["kernel body<br/>or template hook"] -->|trace| B["hk MLIR dialect"]
    B -->|"passes: waits, LDS,<br/>layouts, races"| C["HipKittens C++"]
    C -->|"hipcc + resource gate"| D["code object"]
    D -->|launch| E["torch, vLLM, SGLang"]

The passes refuse what fails silently: an MMA above its wait, a spill, a slow operand layout, a copy racing a barrier (the traps).

Where it runs

GPU arch status
Radeon PRO W7900 / W7900D, RX 7900 gfx1100, RDNA3 primary: every kernel, template and engine patch
Instinct MI355X gfx950, CDNA4 every kernel and template; vLLM end to end (CDNA)
Radeon RX 9070 / Instinct MI300X gfx1201 / gfx942 compile-only, no templates

Install

pip install hk-dsl, for CPython 3.10 (glibc 2.35+) or 3.12 (glibc 2.38+) on Linux x86_64. The release is 0.7.0; it carries the HipKittens headers, so at run time only hipcc is needed, and a ROCm build of torch (not a dependency) to launch. From source, with the HipKittens submodule:

git clone --recursive https://github.com/vivienfanghuagood/hk-dsl.git && cd hk-dsl
scripts/build_mlir.sh && scripts/build_hk_mlir.sh   # MLIR 23.1.3 (~20 min on 64 cores), then hk (~1 min)
export PYTHONPATH=$PWD/python

Details: getting started, building.

In vLLM and SGLang

hk is on by default. Installed, hk's vLLM and SGLang plugins apply every route that was validated with real weights on your GPU: its outputs were bit-identical to the engine's, or its error was within the engine's own noise. On an MI355X that is K1 and K2 for DeepSeek-V4.1. On a W7900D it is linear, MoE, AWQ and attention. Each route sends a shape to hk's kernel only where hk measured faster (integration). Turning it off is for comparisons:

vllm serve Qwen/Qwen3-8B                         # hk on: the routes validated on this GPU
HK_DISABLE=1 vllm serve Qwen/Qwen3-8B            # stock vLLM: nothing patched
HK_VLLM_GEMM=0 vllm serve Qwen/Qwen3-8B          # one route off (HK_<ROUTE>=0)
python3 -m hk.integration --status               # which routes are on, and why

If the installed engine does not match the contract the routes were measured against, hk patches nothing and prints one warning line. ConvRot INT8 is lossy, so it stays opt-in (HK_SGLD=attn,linear,convrot).

For coding agents

llms.txt (or llms-full.txt, every doc in one file), the hk-kernel skill, the generated API reference, and python -m hk.check FILE.py:NAME --json - as the loop's one command (--arch gfx1100,gfx950 builds for the GPU that is not in the machine).

Documentation

Getting started · templates · hk.check · the language · API · performance · everything, and why hk-dsl exists · changelog

Contributing, license, acknowledgements

Issues and pull requests welcome: testing, how numbers are taken. MIT (LICENSE). hk-dsl began in a fork of HipKittens (HazyResearch), whose headers stay under its MIT License (NOTICE) and which brings ThunderKittens' tiles to AMD GPUs. The control-flow rewrite follows CUTLASS's CuTe DSL.

Metadata

Release files for hk-dsl 0.7.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distributions (wheels)

Table of built distributions (wheels) for hk-dsl 0.7.0
File Interpreter ABI Platform Actions
hk_dsl-0.7.0-cp312-cp312-manylinux_2_38_x86_64.whl CPython 3.12 CPython 3.12 Linux glibc 2.38+ x86-64 Details for hk_dsl-0.7.0-cp312-cp312-manylinux_2_38_x86_64.whl
hk_dsl-0.7.0-cp312-cp312-manylinux_2_35_x86_64.whl CPython 3.12 CPython 3.12 Linux glibc 2.35+ x86-64 Details for hk_dsl-0.7.0-cp312-cp312-manylinux_2_35_x86_64.whl
hk_dsl-0.7.0-cp310-cp310-manylinux_2_35_x86_64.whl CPython 3.10 CPython 3.10 Linux glibc 2.35+ x86-64 Details for hk_dsl-0.7.0-cp310-cp310-manylinux_2_35_x86_64.whl

Total release size: 36.5 MB

Release files / hk_dsl-0.7.0-cp312-cp312-manylinux_2_38_x86_64.whl

Download URL hk_dsl-0.7.0-cp312-cp312-manylinux_2_38_x86_64.whl
Size 12.2 MB
Tags CPython 3.12 Linux glibc 2.38+ x86-64
SHA-256 checksum
How to use checksums
f376967cdc173974c7ff9b39a75fa22723873c6f5baee73105f103f50d32e716
BLAKE2b-256 checksum
How to use checksums
3ef4485856793c625096ba0bb7560a737fcb095390ce48f27a34a8441740fe0c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.12

Release files / hk_dsl-0.7.0-cp312-cp312-manylinux_2_35_x86_64.whl

Download URL hk_dsl-0.7.0-cp312-cp312-manylinux_2_35_x86_64.whl
Size 12.2 MB
Tags CPython 3.12 Linux glibc 2.35+ x86-64
SHA-256 checksum
How to use checksums
4cde86b85a628858dd04922d2fadc2135587626355fbfc36172a1c0c74298dc8
BLAKE2b-256 checksum
How to use checksums
6a80fb045284167167c14bbbc9d812e115667c8c9dda3def300f51578052b6e2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.12

Release files / hk_dsl-0.7.0-cp310-cp310-manylinux_2_35_x86_64.whl

Download URL hk_dsl-0.7.0-cp310-cp310-manylinux_2_35_x86_64.whl
Size 12.2 MB
Tags CPython 3.10 Linux glibc 2.35+ x86-64
SHA-256 checksum
How to use checksums
483361d68848a8acce3f8875f7ffdb97e324db204604ba2e5df04662ffe7fe08
BLAKE2b-256 checksum
How to use checksums
e780ca589fe3b7f85b8fc6fdfbd4e9a62fa515aec0d0f3339c9309d61aa4ea46
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.12

Security for hk-dsl 0.7.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.7.0 This release

3 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page