hk.profile¶
Stability: experimental. These names may change in any release (stability).
Why a kernel runs at the speed it does: rocprofv3's counters and thread trace, the disassembled code object and a verdict; the guide, with the JSON schema, is docs/profile.md. hk.profile(...) is hk.profile.profile(...).
hk.profile.profilehk.profile.Profilehk.profile.replayhk.profile.comparehk.profile.call_verdicthk.profile.SCHEMAhk.profile.COMPAREDhk.profile.analyze.kernel_reporthk.profile.analyze.findingshk.profile.analyze.verdicthk.profile.analyze.grid_fithk.profile.analyze.KINDShk.profile.analyze.CEILINGShk.profile.counters.derivehk.profile.counters.PASSEShk.profile.counters.CANNOThk.profile.isa.analyzehk.profile.isa.classifyhk.profile.isa.mma_flopshk.profile.isa.code_objecthk.profile.isa.disassemblehk.profile.isa.CLASSES
hk.profile.profile¶
hk.profile.profile(fn, inputs=None, *, consts: Optional[Dict[str, Any]] = None, name: Optional[str] = None, vs: Optional[Union[str, Dict[str, Any]]] = None, counters: bool = True, att: bool = True, att_kernels: int = 1, iters: int = 20, gap_ms: float = 2.0, timeout: float = 180.0, flops: Optional[float] = None, bytes: Optional[float] = None, shape: Optional[Sequence[int]] = None, workdir: Optional[str] = None, keep: bool = False, gpu: Optional[bool] = None, record: Optional[str] = None, log: Optional[Callable[[str], None]] = None) -> Profile
Profile the kernels fn(*inputs) launches; see the module docstring
and docs/profile.md.
fn what to run: 'FILE.py:name', 'module:name', or an object that
can be named so (an @hk.kernel or function at a module's top
level)
inputs a spec string ("bf16[4096,4096], bf16[4096,4096]"), a list of
tensors (saved and loaded in the profiled process), a
generator named as fn is, or None for the file's own inputs
consts constexprs for a raw kernel
vs another target on the same inputs, profiled the same way and
compared kernel by kernel: 'module:name' or {label: target}
counters, att the counter passes and the thread trace (each skipped
with a note where it cannot run)
iters calls timed in the kernel trace (the median is the time)
gap_ms host sleep between those calls (0: back to back, where a
power limit can slow a long run of GEMMs by tens of percent)
flops, bytes the work of the call's main kernel, where the caller
knows it better than the counters
gpu False: find, build and disassemble only (no profiler)
record write the raw material (per kernel: the profiler's record
without the trace, the build row, the ISA analysis) to this
JSON path: what tests/hk/ir/test_profile.py replays the
verdict logic on
hk.profile.Profile¶
class hk.profile.Profile(name: str, arch: str, device: Optional[str] = None, agent: Dict[str, Any] = <factory>, versions: Dict[str, str] = <factory>, call: Dict[str, Any] = <factory>, kernels: List[Dict[str, Any]] = <factory>, sources: Dict[str, str] = <factory>, passes: Dict[str, Any] = <factory>, notes: List[str] = <factory>, errors: List[Dict[str, str]] = <factory>, compare: Optional[Dict[str, Any]] = None, seconds: Optional[float] = None) -> None
What hk.profile found. str(p) is the human summary, p.json()
the dict (SCHEMA), p.verdict the call's one-line verdict.
Profile.ok (property)¶
True when it ran: no errors (a pass that could not run is a note).
Profile.verdict (property)¶
The call's verdict: its slowest kernel's line, bound and losses
(call_verdict).
Profile.json¶
Profile.json(self) -> Dict[str, Any]
The report as a dict of JSON types (layout version SCHEMA;
docs/profile.md#the-json).
Profile.dumps¶
Profile.dumps(self, **kw) -> str
json() as text.
hk.profile.replay¶
hk.profile.replay(path) -> List[Dict[str, Any]]
The per-kernel reports of a record= file, recomputed from its raw
measurements -- no GPU, no profiler: how the verdict logic is tested
and how a change to it is checked against past runs.
hk.profile.compare¶
hk.profile.compare(a: Profile, b: Profile) -> Dict[str, Any]
b against a: the calls' times, and their slowest kernels' metrics
side by side with a sentence on what differs.
hk.profile.call_verdict¶
hk.profile.call_verdict(kernels: List[Dict[str, Any]]) -> Dict[str, Any]
The call's verdict: its slowest kernel's, with that kernel's share of the call's kernel time.
hk.profile.SCHEMA¶
1
hk.profile.COMPARED¶
[
"time.us",
"roofline.of_compute_peak",
"roofline.of_memory_peak",
"roofline.clock_mhz",
"matrix_core.busy",
"occupancy.achieved_waves_per_simd",
"occupancy.theoretical_waves_per_simd",
"occupancy.cu_busy",
"grid.workgroups",
"grid.waves_per_workgroup",
"grid.workgroups_per_slot",
"grid.rounds",
"grid.lds_bytes",
"isa.main_loop.instructions",
"isa.main_loop.mix.per_matrix_instruction",
"isa.main_loop.cycle_share",
"isa.epilogue.cycle_share",
"isa.drain.cycle_share",
"isa.prologue.cycle_share",
"instructions.valu",
"instructions.mfma",
"instructions.salu",
"instructions.lds",
"instructions.vmem_load",
"instructions.vmem_store",
"lds.bank_conflict",
"cache.l2_hit",
"cache.fetch_bytes",
"latency_cycles.vmem",
"stalls.counters.dependency_wait",
"stalls.counters.issue_wait",
"stalls.matrix_idle.busy",
"stalls.matrix_idle.idle_by_region.main_loop",
"stalls.matrix_idle.idle_by_region.drain",
"stalls.matrix_idle.idle_by_region.epilogue",
"stalls.matrix_idle.idle_by_region.prologue"
]
The parts¶
hk.profile.analyze.kernel_report¶
hk.profile.analyze.kernel_report(arch: str, agent: Dict[str, Any], rec: Dict[str, Any], *, build: Optional[Dict[str, Any]] = None, isa: Optional[Dict[str, Any]] = None, call_bytes: Optional[float] = None, flops: Optional[float] = None, bytes_: Optional[float] = None, single: bool = False) -> Dict[str, Any]
The per-kernel JSON (docs/profile.md#the-json): time, grid, roofline, matrix core, occupancy, stalls, LDS, caches, ISA, findings, verdict.
rec hk.profile.run.collect's kernel record (durations, grid, raw counters, thread trace) build the hk build row of this kernel, if hk built it isa isa.analyze()'s summary of its code call_bytes the call's footprint (inputs and outputs once): which memory ceiling applies (the cache's if it fits) flops, bytes_ the kernel's work if the caller knows it better than the counters (a single-kernel call's --flops/--bytes) single the call launches only this kernel (call_bytes is then its bytes when the counters have none)
hk.profile.analyze.findings¶
hk.profile.analyze.findings(arch: str, k: Dict[str, Any]) -> List[Dict[str, Any]]
What the numbers say, each {kind, cost, text, evidence}: cost is
the share of the kernel's time the finding accounts for, where it can be
estimated (None where it is a bound, not a loss). Sorted by cost.
hk.profile.analyze.verdict¶
hk.profile.analyze.verdict(arch: str, k: Dict[str, Any]) -> Dict[str, Any]
The one line: what bounds the kernel, then what it loses, largest first -- {line, bound, losses: [kind...]}.
hk.profile.analyze.grid_fit¶
hk.profile.analyze.grid_fit(arch: str, agent: Dict[str, Any], grid: Dict[str, Any], build: Optional[Dict[str, Any]] = None) -> Dict[str, Any]
How the workgroups fill the GPU: workgroups a slot (a CU on CDNA, a WGP -- the LDS pool -- on RDNA) holds at once and what binds it, the slots, and the rounds the grid takes. From the dispatch's resources (the compiler's, from the build row, where hk built it).
hk.profile.analyze.KINDS¶
[
"memory_bound",
"compute_bound",
"latency_bound",
"bound_unknown",
"tail",
"underfilled",
"epilogue",
"prologue",
"stall",
"occupancy",
"issue_bound",
"lds_bank_conflict",
"l2_refetch",
"narrow_access",
"small",
"noisy",
"spill"
]
hk.profile.analyze.CEILINGS¶
{
"gfx950": {
"matrix_flops": {
"bf16": 2480000000000000.0,
"fp16": 2480000000000000.0,
"fp8": 4960000000000000.0
},
"dram_bytes_s": 6600000000000.0,
"cache_bytes_s": 9100000000000.0,
"cache_bytes": 268435456,
"source": "MI355X measured (hk.integration.headroom.PEAKS: a register-only v_mfma bf16 loop on every CU, 2.47-2.48 PFLOP/s; a 1 GiB read past the 256 MB Infinity Cache, 6.54-6.60 TB/s); cache-resident 9.1 TB/s (hk.check.CARDS). fp8 is 2x bf16 (datasheet ratio, not measured)"
},
"gfx1201": {
"matrix_flops": {
"bf16": 197000000000000.0,
"fp16": 198500000000000.0,
"fp8": 372000000000000.0,
"i8": 374000000000000.0
},
"dram_bytes_s": 640000000000.0,
"cache_bytes_s": 1810000000000.0,
"cache_bytes": 67108864,
"source": "Radeon AI PRO R9700 measured: back-to-back WMMAs on every CU (bf16 197.1, fp16 198.5, fp8 371.8 TFLOP/s, iu8 373.8 TOP/s); GDDR6 20 Gbps x 256 bits (0.62 TB/s reached past the 64 MB cache); the fastest cache-resident rate measured, an add at 48 MB, 1.81 TB/s (hk.check.CARDS)"
},
"gfx1100": {
"matrix_flops": {
"bf16": 122600000000000.0,
"fp16": 122600000000000.0
},
"dram_bytes_s": 864000000000.0,
"cache_bytes_s": 2430000000000.0,
"cache_bytes": 100663296,
"source": "W7900D: the bf16 WMMA peak and GDDR6 at 18 Gbps x 384 bits (hk.integration.headroom.PEAKS); the fastest cache-resident rate measured, 2.43 TB/s at <= 96 MB (hk.check.CARDS). A measured WMMA loop reaches 100.5 TFLOP/s (hk.target.gfx1100.CEILINGS)"
}
}
hk.profile.counters.derive¶
hk.profile.counters.derive(arch: str, raw: Dict[str, float], agent: Dict[str, Any], by_pass: Optional[Dict[str, Dict[str, float]]] = None) -> Dict[str, Any]
{metric: value} from one kernel's raw counters (medians over the profiled dispatches) and the agent's facts (Cu_Count, Simd_Count, Num_Xcc). A metric whose counters are missing is absent.
by_pass ({pass: {counter: value}}) keeps each ratio inside one pass:
a share of cycles is taken against the GRBM_GUI_ACTIVE or
SQ_WAVE_CYCLES counted with it, so one disturbed dispatch in one pass
does not skew another pass's metrics.
hk.profile.counters.PASSES¶
{
"gfx950": [
[
"core",
[
"GRBM_GUI_ACTIVE",
"SQ_WAVES",
"SQ_WAVE_CYCLES",
"SQ_BUSY_CU_CYCLES",
"SQ_INSTS_VALU",
"SQ_INSTS_MFMA",
"SQ_INSTS_SALU",
"SQ_INSTS_LDS",
"TCC_EA0_RDREQ_DRAM_32B",
"TCC_EA0_WRREQ_WRITE_DRAM_32B",
"TCC_EA0_RDREQ_sum",
"TCC_EA0_WRREQ_sum"
]
],
[
"mix",
[
"GRBM_GUI_ACTIVE",
"SQ_INSTS_VMEM_RD",
"SQ_INSTS_VMEM_WR",
"SQ_INSTS_SMEM",
"SQ_VALU_MFMA_BUSY_CYCLES",
"SQ_INSTS_VALU_MFMA_MOPS_BF16",
"SQ_INSTS_VALU_MFMA_MOPS_F16",
"SQ_INSTS_VALU_MFMA_MOPS_F8",
"SQ_INSTS_BRANCH",
"TCC_BUBBLE_sum",
"TCC_HIT_sum",
"TCC_MISS_sum"
]
],
[
"wait",
[
"GRBM_GUI_ACTIVE",
"SQ_WAVE_CYCLES",
"SQ_WAIT_ANY",
"SQ_WAIT_INST_ANY",
"SQ_ACTIVE_INST_ANY",
"SQ_ACTIVE_INST_VALU",
"SQ_ACTIVE_INST_VMEM",
"SQ_ACTIVE_INST_LDS",
"SQ_ACTIVE_INST_SCA"
]
],
[
"lds",
[
"GRBM_GUI_ACTIVE",
"SQ_WAVE_CYCLES",
"SQ_LDS_BANK_CONFLICT",
"SQ_LDS_IDX_ACTIVE",
"SQ_LDS_ADDR_CONFLICT",
"SQ_LDS_UNALIGNED_STALL",
"SQ_WAIT_INST_LDS",
"SQ_IFETCH",
"SQ_INSTS_VALU_MFMA_MOPS_F32"
]
],
[
"latency",
[
"VmemLatency",
"LdsLatency"
]
]
],
"gfx1100": [
[
"core",
[
"GRBM_GUI_ACTIVE",
"SQ_WAVES",
"SQ_WAVE_CYCLES",
"SQ_BUSY_CYCLES",
"SQ_INSTS_VALU",
"SQ_INSTS_SALU",
"SQ_INSTS_LDS",
"SQ_INSTS_SMEM",
"GL2C_HIT_sum",
"GL2C_MISS_sum",
"GL2C_EA_RDREQ_32B_sum",
"GL2C_EA_RDREQ_64B_sum"
]
],
[
"wait",
[
"GRBM_GUI_ACTIVE",
"SQ_WAVE_CYCLES",
"SQ_WAIT_ANY",
"SQ_WAIT_INST_ANY",
"SQ_WAIT_INST_LDS",
"SQ_INSTS_TEX_LOAD",
"SQ_INSTS_TEX_STORE",
"SQ_INSTS_FLAT",
"GL2C_EA_RDREQ_96B_sum",
"GL2C_EA_RDREQ_128B_sum",
"GL2C_EA_WRREQ_64B_sum",
"GL2C_MC_WRREQ_sum"
]
],
[
"lds",
[
"GRBM_GUI_ACTIVE",
"SQC_LDS_BANK_CONFLICT",
"SQC_LDS_IDX_ACTIVE",
"TA_TA_BUSY_max"
]
],
[
"latency",
[
"LdsLatency"
]
]
],
"gfx1201": [
[
"core",
[
"GRBM_GUI_ACTIVE",
"SQ_WAVES",
"SQ_BUSY_CYCLES"
]
]
]
}
hk.profile.counters.CANNOT¶
{
"gfx950": [
"MALL (Infinity Cache) hits: the L2's misses all count as requests to the fabric (TCC_EA0_RDREQ_DRAM = TCC_EA0_RDREQ); a MALL hit and an HBM read look alike, so `dram_bytes` is bytes past the L2",
"stall reasons by instruction: the counters give the share of wave time waiting for a dependency (s_waitcnt, barrier) or to issue, not which; the thread trace (att) attributes them per instruction on one CU"
],
"gfx1100": [
"matrix-core (WMMA) activity: no counter counts WMMA instructions or busy cycles; hk.profile counts them from the thread trace (executions x FLOPs each) or reports the achieved FLOP/s against the peak instead",
"MFMA-style per-type instruction counts: only VALU, SALU, LDS, SMEM, texture load/store and FLAT are counted",
"Infinity Cache (MALL) hits: GL2C_EA/MC requests are past the L2, whether the MALL or GDDR6 served them",
"per-type active cycles (SQ_ACTIVE_INST_*), VMEM latency and CU busy: not exposed on gfx11; the thread trace attributes the waits"
],
"gfx1201": [
"matrix FLOPs: no counter counts WMMA instructions, and the instruction counters (SQ_INSTS_*) read 0 under ROCm 7.2.3, so the main loop's trip count cannot be read either; only the thread trace counts them. Without one, give the work with --flops",
"bytes past the L2, cache hits, wave cycles and waits (GL2C_*, SQ_WAVE_CYCLES, SQ_WAIT_*): listed by rocprofv3 but read 0 under ROCm 7.2.3; the call's own bytes against DRAM are what the roofline has"
]
}
hk.profile.isa.analyze¶
hk.profile.isa.analyze(f: Function, *, att: Optional[Dict[int, Dict[str, int]]] = None, waves_traced: Optional[int] = None) -> Dict[str, Any]
One kernel's ISA summary (the JSON's isa): the whole kernel's mix,
the loops and the main one, the prologue before it and the epilogue
after it, and with a thread trace the cycles each part took.
hk.profile.isa.classify¶
hk.profile.isa.classify(mnemonic: str, operands: str = '') -> str
The class (CLASSES) of one instruction, from its mnemonic (and, for
a buffer load, whether it writes LDS).
hk.profile.isa.mma_flops¶
hk.profile.isa.mma_flops(mnemonic: str, wave: int = 64) -> int
FLOPs (multiply + add, so 2 a product) one wave's matrix instruction does: 2MN*K from its name (v_mfma_f32_16x16x32_bf16 -> 16384, v_wmma_f32_16x16x16_bf16 -> 8192). 0 for anything else. The legacy multi-block MFMAs (4x4x4 with 16 blocks, 32x32x2 with 2) are counted by their block count.
hk.profile.isa.code_object¶
hk.profile.isa.code_object(path, arch: Optional[str] = None) -> bytes
The AMDGPU code object hk built at path: a device-only .co as it
is, or the one embedded in a host .so's .hip_fatbin.
hk.profile.isa.disassemble¶
hk.profile.isa.disassemble(co: bytes, arch: str) -> List[Function]
The kernels in a code object, disassembled (needs llvm-objdump).
hk.profile.isa.CLASSES¶
[
"mfma",
"wmma",
"valu",
"valu_trans",
"salu",
"smem",
"vmem_load",
"vmem_lds",
"vmem_store",
"vmem_atomic",
"ds_read",
"ds_write",
"ds_other",
"waitcnt",
"barrier",
"branch",
"nop",
"other"
]
Command line¶
python -m hk.profile TARGET [options]; python -m hk.profile --help prints the forms TARGET and --inputs take.
| option | what |
|---|---|
target |
FILE.py[:name], module:name or package.module.name |
--inputs INPUTS |
the inputs (a spec, or FILE.py:name) |
--shape SHAPE |
for a generator that takes a shape: comma list |
--const K=V |
a constexpr for a raw kernel (repeatable) |
--vs NAME=TARGET |
also profile TARGET on the same inputs and compare |
--no-counters |
skip the counter passes |
--no-att |
skip the thread trace |
--att-kernels ATT_KERNELS |
thread-trace this many of the slowest kernels (default 1) (default 1) |
--iters ITERS |
calls timed in the kernel trace (default 20) |
--gap-ms GAP_MS |
host sleep between the timed calls (0: back to back) (default 2.0) |
--timeout TIMEOUT |
seconds a pass may take (default 180.0) |
--flops FLOPS |
FLOPs of the call's main kernel |
--bytes BYTES |
bytes of the call's main kernel |
--keep DIR |
keep the profiler's output in DIR |
--record PATH |
write the raw measurements (no trace) as JSON, for replaying the verdict logic without a GPU |
--build-only |
no GPU work: build and disassemble only |
--json PATH |
write the report as JSON ('-' for stdout) |
-q, --quiet |
no summary |
-v, --verbose |
say each pass as it runs |