Skip to content

hk.placement

Stability: experimental. These names may change in any release (stability).

Workgroup placement on multi-XCD GPUs: which XCD runs which workgroup.

docs/design/placement-pass.md has the why. In short: a gfx950 deals workgroup w to the XCD of w % 8, each XCD has its own 4 MB L2, and a grid whose workgroups share input bytes (attention's K/V of one KV head, a GEMM's A and B panels) fetches them once per XCD that reads them and, numbered the wrong way, holds many unrelated streams on each L2 at once. Renumbering changes nothing a workgroup computes -- only which XCD and when -- so outputs stay bitwise the same; the time does not (PR #55: +12-17% for attention at 128 KV heads, -10% for a short causal grid).

Three parts, all on the traced module before the hk passes run:

analyze(module) each global read's sharing key over the grid axes (digits: an axis whole, or axis // r and axis % r for a radix r the kernel divides by), weighted by bytes; the kernel's classification (cluster / raster / none) and what would make reordering unsafe. rewrite(module, plan) a flat grid, and at kernel entry the logical (x, y, z) decoded from the workgroup id through place -- a bijection for any total -- replacing every hk.block_idx. choose(...) the policy hk.kernel(placement="auto") launches by.

place is written once over an arithmetic backend: the rewrite emits it as arith ops, and the host evaluates the same formula (hk.check --placement, the tests).

hk.placement.Placement

class hk.placement.Placement(kind: str = 'none', key: Optional[Tuple[Digit, ...]] = None, chunk: Union[int, str] = 0, tail: str = 'spread') -> None

How a kernel's workgroups are numbered.

Placement.cluster() one sharing cluster an XCD at a time; the key from analyze (the dominant read's), or key= digits the analysis cannot see (an index read from memory) Placement.chunked(chunk, tail) block_idx.x only, within each (y, z) slice and with the grid as it is: rounds of xcds * chunk consecutive ids an XCD (a GEMM's xcd_chunk, whose y is the split-K share); chunk="even" is an eighth of the axis an XCD Placement.none() the identity

tail is what happens past the last whole round: "stay" (in round-robin order, what the hand-written GEMM remaps did) or "spread" (dealt as contiguous ranges, balanced to one). chunk 0 is contiguous ranges outright.

Placement.none

Placement.none() -> Placement

The hardware's order (what placement=None means).

Placement.cluster

Placement.cluster(key: Optional[Sequence[Digit]] = None, chunk: int = 0, tail: str = 'spread') -> Placement

One sharing cluster an XCD at a time; key the cluster's digits, or None for the analysis's. The grid is flattened and decoded at entry.

Placement.chunked

Placement.chunked(chunk: Union[int, str] = 0, tail: str = 'stay') -> Placement

block_idx.x renumbered in place, chunk consecutive ids an XCD a round ("even": an eighth of the axis; 0: contiguous ranges); tail past the last whole round: "stay" or "spread".

Placement.suffix

Placement.suffix(self, xcds: int) -> str

The kernel-name suffix of this placement.

hk.placement.Digit

class hk.placement.Digit(axis: str, kind: str = 'whole', radix: Optional[host.HostExpr] = None, radix_text: str = '') -> None

A grid axis whole (kind "whole"), or one digit of it: axis // radix ("div") or axis % radix ("mod"). radix is an int, a tensor extent spelled "tensor.dim" (Digit.of("x", "mod", "k.depth"): the KV heads of a (B, H_KV, N, D) k), or a host expression of extents and constexprs.

Digit.of

Digit.of(axis: str, kind: str = 'whole', radix=None) -> Digit

A digit of grid axis axis ("x", "y" or "z"): whole, or axis // radix ("div") / axis % radix ("mod"); radix an int, "tensor.dim" or a host expression.

hk.placement.xcd_count

hk.placement.xcd_count(arch: str) -> int

XCDs of this process's arch GPUs: HK_XCDS if set, else the KFD topology's num_xcc when every node of that arch agrees (an MI350P or a DPX partition says 4, an MI300A 6), else the arch's default.

hk.placement.renumber

hk.placement.renumber(w, total, *, chunk: Union[int, str, Any] = 0, tail: str = 'spread', live=None)

place inside a kernel body, on device ints: the logical unit of id w of total as the XCDs of the GPU being traced for deal them. For a persistent kernel, whose units are not workgroups (a workgroup's i-th virtual id is w + i * grid): what Placement.chunked / cluster install at entry for a grid.

live: only units [0, live) do work, the rest exit at once -- a grid sized for the most work a launch can have (a MoE's sorted blocks padded per expert), with the count known at run time. Ids [0, live) are then placed among themselves and the rest stay: the live units are dealt over every XCD. Placing all of [0, total) would hand them to the first XCDs' ranges and leave the others idle (K1 at M = 64: 18-20% slower).

hk.placement.xcd_range

hk.placement.xcd_range(d, total)

(first, count): XCD d's contiguous range of total units, the first total % xcds XCDs one unit more -- what contiguous place gives each XCD. For a persistent kernel that deals units to its XCDs itself.

hk.placement.analyze

hk.placement.analyze(module: ir.Module, kernel_name: Optional[str] = None) -> Analysis

The sharing structure of the kernel func in module (see the module docstring). Pure: the module is not changed.

hk.placement.choose

hk.placement.choose(analysis: Analysis, plan: Optional[Plan], grid: Tuple[int, int, int], radii: Dict[str, int], xcds: int, resident_per_xcd: int) -> Choice

Whether hk.kernel(placement="auto") launches the clustered kernel for this grid (docs/design/placement-pass.md §3).

hk.placement.Analysis

class hk.placement.Analysis(reads: List[Read], kind: str, key: Tuple[Digit, ...], digits: Dict[str, List[Digit]], order_dependent: List[str], nonuniform: bool, broadcast_bytes: int, note: str = '') -> None

What analyze found in one kernel.

Analysis.summary

Analysis.summary(self) -> str

One line: the class, the key, and the flags.

hk.placement.Choice

class hk.placement.Choice(placed: bool, reason: str, streams: int = 0, rounds: float = 0.0) -> None

What choose decided for one launch geometry: placed (launch the cluster placement), reason (the rule that decided, in words), and the numbers it looked at -- streams, the most distinct cluster keys among an XCD's first resident workgroups under the default order, and rounds, workgroups an XCD over those it holds at once.

hk.placement.check_kernel

hk.placement.check_kernel(kernel, shapes: Sequence[Tuple[int, ...]], consts=None) -> List[str]

hk.check --placement for one launch geometry: the placement kernel runs with (its own, or for placement="auto" the cluster placement it may pick) decodes to every grid point exactly once. Lines for a report; an AssertionError if a decode is not a bijection.

hk.placement.check_emitted

hk.placement.check_emitted(traced, extents) -> int

hk.check --placement: over every workgroup id of the launch, the decode the kernel computes covers the grid its body numbers -- each (x, y, z) exactly once. Returns the number of workgroups; raises if not.

hk.placement.emitted_decode

hk.placement.emitted_decode(module: ir.Module, kernel_name: str) -> Optional[Callable]

The decode the kernel func actually computes: a function (bx, by, bz, extents) -> (x, y, z) of the hardware's block ids, built from the ops tagged hk.placement_axis (an untagged axis is the hardware's), or None if the kernel has no placement. extents maps a tensor parameter to its (batch, depth, rows, cols).

hk.placement.recording

with recording() as launches: the (kernel, shapes, constexprs) of every hk kernel launched inside, in order.

hk.placement.place_host

hk.placement.place_host(w: int, total: int, xcds: int, chunk: Union[int, str] = 0, tail: str = 'spread') -> int

place on host ints: the logical unit workgroup w of total runs on xcds XCDs (what a placed kernel computes at entry).

hk.placement.MIN_STREAMS

The policy's thresholds, measured on gfx950's attention (PR #55, the 2511.02132 study's report, §5): cluster from five KV streams an XCD; for non-uniform work (a trip count that depends on the grid), not below twelve rounds of workgroups an XCD.

hk.placement.MIN_ROUNDS_NONUNIFORM

12