Skip to content

hk.check

Stability: stable, except where an entry says otherwise (stability).

Correctness, resources and timing of a kernel in one call; the guide is docs/check.md. hk.check(...) is hk.check.check(...).

hk.check.check

hk.check.check(fn, reference: Optional[Callable] = None, inputs=None, *, cases: Optional[Sequence] = None, consts: Optional[Dict[str, Any]] = None, outputs: Optional[Sequence] = None, inplace: Optional[Sequence] = None, sweep: Optional[Dict[str, Sequence]] = None, baselines: Sequence[str] = ('eager', 'compile'), extra: Optional[Dict[str, Callable]] = None, rtol: Optional[float] = None, atol: Optional[float] = None, exact: bool = False, ref_precision: str = 'fp32', time: bool = True, rounds: int = 15, warmup: int = 3, batch_ms: float = 1.0, burn: float = 0.5, cache: str = 'cold', compile_mode: Optional[str] = None, bytes: Optional[float] = None, flops: Optional[float] = None, name: Optional[str] = None, gpu: Optional[bool] = None, build: bool = True, seed: int = 0, profile: bool = False) -> Report

Check fn against reference on inputs; see the module docstring and docs/check.md.

fn an @hk.kernel, or a callable that launches hk kernels reference the torch reference: called with the inputs fn reads (for a raw kernel, the tensors it does not write) and returning the output(s), in the order fn returns or writes them inputs a sequence of tensors, or a generator called as inputs(device) (or inputs(device=..., shape=...)) cases several inputs, one case each (shapes to sweep) consts constexprs for a raw kernel, passed as k(*tensors, **consts) outputs which arguments are outputs (indices or a raw kernel's parameter names): for a function that writes into an argument instead of returning. A raw kernel's default is the tensors it stores into (TracedKernel.written_tensors). They are NaN-filled first and not passed to the reference sweep {CONSTEXPR: [values]} of a raw kernel: every combination is built, checked and timed beside it as hk[NAME=value,...]; one that does not build or is wrong is reported and not timed inplace arguments updated in place (read, then written): passed to the reference as they were, compared after the call. The reference returns the outputs and in-place arguments in argument order baselines which of "eager" (the reference itself) and "compile" (torch.compile(reference, mode=compile_mode)) to time extra more contenders, {name: callable taking the reference's arguments}; checked against the reference like fn rtol, atol override TOLERANCES (atol is then absolute); exact asks for bit equality ref_precision "fp32" runs the reference on fp32 copies of the 16-bit inputs (the default); "native" on the inputs as they are time, rounds, warmup, batch_ms, burn the timing (burn: seconds of memory traffic first, to settle the clocks) cache "cold" (the default): every contender rotates through copies of its inputs that together exceed twice the last-level cache (CARDS), so the rates are DRAM traffic; tiny inputs, or copies that would take over a quarter of the free memory, are timed hot with a note. "hot": the same inputs every call bytes, flops work a call does, for the rates (default bytes: every input and output once) gpu False forces build-only; None uses a GPU when one is there build False skips hipcc (trace only) profile also run hk.profile on the first case (counters, the thread trace, the ISA and a verdict: docs/profile.md); its report is report.profile. fn must be nameable (defined at a module's top level); the inputs are passed as tensors

hk.check.Report

class hk.check.Report(name: str, arch: str, mode: str, device: Optional[str] = None, versions: Dict[str, str] = <factory>, builds: List[Dict[str, Any]] = <factory>, cases: List[Dict[str, Any]] = <factory>, errors: List[Dict[str, str]] = <factory>, notes: List[str] = <factory>, profile: Optional[Dict[str, Any]] = None) -> None

What hk.check found. str(report) is the human summary, report.json() the same as a dict (SCHEMA), report.ok whether every step that ran passed.

Report.status (property)

"error" (something did not build or run), "fail" (a wrong answer), "pass" (correct at every case), or "built" (nothing ran on a GPU and everything that could be done without one worked).

Report.hints (property)

What to try next, read off the numbers (hints()).

Report.ok (property)

True for "pass" and "built".

Report.exit_code (property)

The CLI's: 0 pass or built, 1 a wrong answer, 2 an error.

Report.json

Report.json(self) -> Dict[str, Any]

The report as a dict of JSON types (layout version SCHEMA).

Report.dumps

Report.dumps(self, **kw) -> str

json() as text.

Report.raise_for_status

Report.raise_for_status(self) -> Report

Raise AssertionError unless ok; returns self, for chaining in a test: hk.check(k, ref, make).raise_for_status().

hk.check.compare

hk.check.compare(got, want, *, rtol=None, atol=None, exact=False) -> Dict[str, Any]

One output against its reference. want may be any dtype (the fp32 reference); the tolerance is for got's dtype.

hk.check.tolerance

hk.check.tolerance(dtype, rtol=None, atol=None, scale: float = 1.0) -> Tuple[float, float]

(rtol, absolute atol) for comparing an output of dtype against a reference whose largest magnitude is scale.

hk.check.interleave

hk.check.interleave(thunks: Dict[str, Callable[[], Any]], *, rounds: int = 15, warmup: int = 3, batch_ms: float = 1.0) -> Tuple[Dict[str, List[float]], Dict[str, int]]

{name: thunk} -> ({name: ms a call, one per round}, {name: calls a burst}). Every round runs every contender once, the order rotated round by round; a contender's burst is enough back-to-back calls to fill batch_ms, so a microsecond kernel is not timed against the launch gap and the event resolution.

hk.check.verdict

hk.check.verdict(ratio: float, band: float) -> str

win / loss past the band, tie inside it, noise when the band is too wide to tell (docs/agents.md, ab's rules).

hk.check.versions

hk.check.versions() -> Dict[str, str]

hk's version (and the checkout's commit), torch's, HIP's, Triton's, Python's: what a number needs beside it to mean anything.

hk.check.describe

hk.check.describe(args) -> str

"bf16[4096,14336], fp32[64]": what a case's inputs are.

hk.check.TOLERANCES

Output dtype -> (rtol, atol as a fraction of max|reference|). An element passes when |got - want| <= atol * max|want| + rtol * |want|. About two units in the last place for the 16-bit types: a kernel that computes in fp32 and rounds once is within one of the fp32 reference, and torch eager, which rounds every intermediate, typically within two.

{
 "float64": [
  1e-07,
  1e-09
 ],
 "float32": [
  0.0001,
  1e-05
 ],
 "float16": [
  0.002,
  0.0001
 ],
 "bfloat16": [
  0.016,
  0.001
 ],
 "float8_e4m3fn": [
  0.125,
  0.01
 ],
 "float8_e4m3fnuz": [
  0.125,
  0.01
 ],
 "float8_e5m2": [
  0.25,
  0.01
 ],
 "float8_e5m2fnuz": [
  0.25,
  0.01
 ]
}

hk.check.CARDS

Each card's ceilings, for the bandwidth and FLOP rates the summary prints (tools/hk-bench/_rounds.py's CARDS, where each is sourced): DRAM bytes/s, the fastest cache-resident bytes/s measured, the cache's size, bf16 FLOP/s.

{
 "gfx1100": {
  "dram_bytes_s": 864000000000.0,
  "cache_bytes_s": 2430000000000.0,
  "cache_bytes": 100663296,
  "bf16_flops": 122600000000000.0
 },
 "gfx1201": {
  "dram_bytes_s": 640000000000.0,
  "cache_bytes_s": 1810000000000.0,
  "cache_bytes": 67108864,
  "bf16_flops": 197000000000000.0
 },
 "gfx950": {
  "dram_bytes_s": 8000000000000.0,
  "cache_bytes_s": 9100000000000.0,
  "cache_bytes": 268435456,
  "bf16_flops": 2520000000000000.0
 }
}

Command line

python -m hk.check TARGET [options]; python -m hk.check --help prints the forms TARGET, --ref and --inputs take.

option what
target FILE.py[:name], module:name or package.module.name
--ref REF the torch reference (FILE.py:name, module:name, a lambda or an expression over the argument names)
--inputs INPUTS a case's inputs (a spec, or FILE.py:name); repeat for more cases
--const K=V a constexpr for a raw kernel (repeatable)
--sweep K=V1,V2,... time a raw kernel at each value of a constexpr (repeatable: every combination), each checked first
--outputs OUTPUTS comma list of output arguments (indices or a raw kernel's parameter names)
--inplace INPLACE comma list of arguments updated in place (read, then written): given to the reference, compared after
--baseline NAME=REF another contender, e.g. ops=hk.ops:silu_mul (repeatable)
--no-eager do not time torch eager
--no-compile do not time torch.compile
--compile-mode COMPILE_MODE torch.compile mode, e.g. max-autotune-no-cudagraphs
--rtol RTOL relative tolerance (default: the output dtype's, hk.check.TOLERANCES)
--atol ATOL absolute atol (default: relative to max|ref|)
--exact require bit equality
--ref-precision {fp32,native} run the reference on fp32 copies of 16-bit inputs, or as given (default fp32)
--no-time correctness only
--rounds ROUNDS timing rounds, contenders rotated (default 15)
--hot time on the same inputs every call (default: cold, rotated copies past the cache)
--warmup WARMUP untimed calls of each first (default 3)
--batch-ms BATCH_MS a round's burst of back-to-back calls fills this long (default 1.0)
--burn BURN seconds of clock warm-up (default 0.5)
--bytes BYTES bytes a call moves (for GB/s)
--flops FLOPS FLOPs a call does (for TFLOP/s)
--build-only no GPU work even if one is there
--trace-only no hipcc either
--arch ARCH build for this arch (comma list: one report each, built only where the GPU is another arch)
--dump {ir,cpp} print a raw kernel's IR or C++ and exit
--placement check that every launched kernel's workgroup placement (hk.dsl.placement) covers its grid exactly once, at these inputs, and exit (a raw kernel needs no GPU)
--profile then run hk.profile on the first case: counters, the thread trace, the main loop's instructions and a verdict (docs/profile.md)
--json PATH write the report as JSON ('-' for stdout)
--seed SEED torch.manual_seed for the inputs
-q, --quiet no summary