radiance 1.8.0

- video: MiniMax-H3 and FastVideo's FastH3 8-Step V2, a video with its stereo soundtrack from text
  on OpenAI's /v1/videos, block fp8 on two cards
- music: MiniMax-Music3, songs from lyrics and a description on /v1/audio/speech
- speech: Breeze-TTS-2 on /v1/audio/speech and vLLM-Omni's realtime WebSocket
- runtime LoRA adapters beside the base model, picked per request by `model` (baKon, #105)
- several agent streams keep their cached history: --checkpoint-slots auto, the new default, sizes
  the snapshot store by --max-num-seqs (#108)
- the Swift 1.5 27B W4 releases convert and serve: compressed-tensors AWQ and GPTQ int4, AutoRound,
  ModelOpt NVFP4 (#110)
- the image's host tests pass where perf_event_open is refused, as in BuildKit's sandbox (#106)
- plugin ABI 21: out-of-tree plugins must be rebuilt
This commit is contained in:
Deadcode committed 2026-10-18 11:38:37 +02:00
1 parent 91127395ee
commit 7a7707a955
179 files changed
+37542 -1892

No files matched your search

+41 -4
View File
@@ -1,19 +1,20 @@
# libr4d/isa: reading the machine code the kernels became
Nine scripts and four microbenchmarks. A wall-clock timer cannot tell "this kernel is slow" from
Nine scripts and six microbenchmarks. A wall-clock timer cannot tell "this kernel is slow" from
"this kernel is at the memory wall"; the disassembly plus one calibration number can, without a GPU
run. The trace tools are the exception: they need the GPU, and they are the only instruments here
that report the geometry a kernel was launched with rather than every geometry it could have had.
BUILD=/path/to/build ./res.sh | head -40 # every kernel: vgpr/lds/scratch
BUILD=... ./dis.sh r4d_gemm_fp8a8 ILb1ELb0ELj8ELj1ELj8ELj64ELb0E
BUILD=... ./dis.sh r4d_gemm_fp8a8 gemm_8a8_prefill_kernelILb0E
BUILD=... ./waterfall.sh # divergent VGPR indices, per kernel
./loop.sh # its K loop, classified
./trace.sh traces/dec -- ./bin/radiance ... # then: ./tsum.sh traces/dec/k_kernel_trace.csv
`res.sh` prints mangled names; the substring `dis.sh` takes picks one template instantiation, and
the parameter order is in the kernel's declaration (`gemm_fp8a8_kernel` is
`<Full, AW, NT, MT, MW, FK, GATED, I8>`, so `ILb1ELb0ELj8ELj1ELj8ELj64ELb0E` is the prefill tile).
`<Full, NT, MW, FK, GATED, I8, BIAS>`, so `ILb1ELj4ELj8ELj256ELb0E` is the narrow tile at four token
tiles and eight waves; `gemm_8a8_prefill_kernelILb0E` is the fp8 prefill tile).
`loop.sh` anchors on `v_wmma` by default and takes the smallest backward branch containing every
anchor, because a largest-loop search finds the epilogue, a long unrolled store chain.
@@ -34,10 +35,13 @@ anchor, because a largest-loop search finds the epilogue, a long unrolled store
| `graphrate.hip` | what a dispatch costs the host to submit, and what capturing a fixed sequence saves |
| `launchreg.hip` + `launchreg_lib.hip` | whether a launch costs more when the module registers more kernels, and what crossing into a `.so` costs |
| `blockcopy.hip` | contiguous versus scattered device-to-host copy, at KV-block granularity |
| `wmmapeak.hip` | sustained fp8, iu8 and bf16 WMMA TFLOP/s at the card's clock and power limit, with the sclk, power and temperature it ran at |
| `wmmamix.hip` | what a VALU instruction, an LDS read and an LDS write cost next to fp8 WMMA, and LDS bandwidth per CU |
Each script's header states its traps and how to read its output. The microbenchmarks are built by
hand: `clang++ -x hip -O3 -std=c++17 --offload-arch=gfx1201 <name>.hip -o <name>` for `isarate`,
`graphrate` and `blockcopy`, and `build_launchreg.sh` for the two-object launchreg pair.
`graphrate`, `blockcopy`, `wmmapeak` and `wmmamix`, and `build_launchreg.sh` for the two-object
launchreg pair.
## Instruction slot cost on gfx1201
@@ -62,6 +66,39 @@ gfx1201 has no dense 16x16x32 fp8 WMMA (`llvm-mc -mcpu=gfx1201` rejects it). The
form is `v_swmmac_f32_16x16x32_fp8_fp8`, the 2:4 sparse one, so the GEMMs here already use the
widest dense op there is.
## Sustained WMMA rate, and what the rest of a K loop costs
`wmmapeak.hip` measures the ceiling a GEMM is held against: random operands rotating through eight
fragments, every block resident, eight seconds after eight of warm-up. At the 300 W limit, with
three blocks a WGP (the occupancy of the prefill tiles):
| op | TFLOP/s | sclk | per CU per clock |
|---|---|---|---|
| `v_wmma_f32_16x16x16_fp8_fp8` | 330 | 2536 MHz | 2037 FLOP (8 clocks a WMMA a SIMD) |
| `v_wmma_i32_16x16x16_iu8` | 334 | 2561 MHz | 2038 |
| `v_wmma_f32_16x16x16_bf16` | 170 | 2703 MHz | 984 (16 clocks) |
The power limit, not issue, sets these: with zero operands the same fp8 loop runs 418 TFLOP/s at
3.25 GHz, so how hard the multipliers switch is most of the power, and a GEMM timed on constant
operands reads fast (the fp8 prefill tile: 262 TFLOP/s on `--perfw8`'s 0x38 fill, 205 on random
codes; `--perfvideo` fills random ones). At full occupancy the fp8 run sometimes
settles at 260-270 TFLOP/s with hwmon reporting 2.8 GHz, a state in which the reported clock
overstates the delivered one; quote the three-blocks figure.
`wmmamix.hip` adds one kind of instruction at a time to eight independent WMMA chains a wave. With
zero operands (the clock is then real), against 8.36 SIMD clocks a WMMA at six waves a SIMD:
- a VALU instruction costs 1.05-1.4 SIMD clocks, serial with the WMMA (8 adds per 8 WMMA: 9.63
clocks a WMMA; 64: 16.7). A fold's arithmetic does not hide under the matrix work; VOPD pairs
are the only way to halve it.
- LDS serves 64 bytes a clock per CU, loads and stores alike (32 `ds_load_b64` or 16
`ds_load_b128` per 8 WMMA: 31.5 clocks a WMMA). At the WMMA rate that is 256 bytes of LDS per
fp8 WMMA and 512 per bf16 one. Below the limit a load costs about a clock of issue.
- nine fragment reads and two staging stores per 8 WMMA (a 1 x 8 tile staging both operands)
run at 73% with nothing else in the loop; four and one at 90%.
- one dependent chain a wave runs as fast as eight at six waves a SIMD (96%); occupancy moves the
rate from 94% (four waves) to 98% (twelve).
## The profiler
`rocprofv3 --pmc` does not work on this platform. With any counter set and any kernel filter, on
+1 -1
View File
@@ -1,7 +1,7 @@
#!/bin/sh
# Disassemble ONE kernel out of one translation unit, into $ISA_OUT/one.s.
#
# BUILD=... dis.sh r4d_gemm_fp8a8 ILb1ELb0ELj8ELj1ELj8ELj64ELb0E
# BUILD=... dis.sh r4d_gemm_fp8a8 gemm_8a8_prefill_kernelILb0E
#
# The pattern is a substring of the MANGLED name, which is how a template instantiation is named:
# res.sh prints those, and the template parameter order is in the kernel declaration. llvm-objdump
+229
View File
@@ -0,0 +1,229 @@
// What a VALU instruction, an LDS read and an LDS write cost next to fp8 WMMA on gfx1201, what LDS
// delivers per CU, and how the WMMA rate depends on waves per SIMD. A GEMM's K loop is a mix of
// these, and the answer decides its tile: whether the fold's arithmetic hides under the matrix work
// (it does not: VALU and WMMA issue serially on a SIMD) and how many LDS bytes a WMMA may cost
// (64 B a clock per CU, loads and stores together, so 256 B per WMMA at the WMMA rate).
//
// Every row is a kernel of eight independent WMMA chains a wave plus V VALU adds, L LDS loads and S
// LDS stores per eight WMMA, at the occupancy its LDS padding allows (every block resident, so no
// tail). It runs about a second; the result is SIMD clocks per WMMA from the elapsed time and the
// card's mean sclk (hwmon freq1_input), so it is clock-independent as long as the clock is real.
//
// clang++ -x hip -O3 -std=c++17 --offload-arch=gfx1201 wmmamix.hip -o wmmamix
// ./wmmamix [z]
//
// With `z` every operand is zero: the multipliers barely switch, the card stays under its power
// limit at its top clock, and the per-clock costs are clean. With random operands a power limit
// holds the clock down and, at some settings, stretches it below what hwmon reports; wmmapeak.hip
// is the throughput instrument for that case.
#include <hip/hip_runtime.h>
#include <atomic>
#include <chrono>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <dirent.h>
#include <string>
#include <thread>
#include <type_traits>
#include <vector>
typedef int i2v __attribute__((ext_vector_type(2)));
typedef float f8v __attribute__((ext_vector_type(8)));
typedef unsigned u2v __attribute__((ext_vector_type(2)));
typedef unsigned u4v __attribute__((ext_vector_type(4)));
// V VALU adds, L LDS loads of B bytes a lane, S ds_store_b64 per eight WMMA; C independent chains
// (8 = all independent, 1 = one dependent chain).
template <int LDS, int V, int L, int B, int S, int C>
__global__ __launch_bounds__(256) void mix(const uint32_t* src, float* out, int iters) {
__shared__ __attribute__((aligned(16))) char pad[LDS];
const uint32_t lane = threadIdx.x & 31u, wave = threadIdx.x >> 5;
pad[threadIdx.x] = (char)threadIdx.x;
__syncthreads();
const uint32_t* p = src + ((blockIdx.x & 63u) * 32u + lane) * 32u;
i2v a[8], b[8];
#pragma unroll
for (int i = 0; i < 8; ++i) {
a[i] = (i2v){ (int)p[2 * i], (int)p[2 * i + 1] };
b[i] = (i2v){ (int)p[16 + 2 * i], (int)p[17 + 2 * i] };
}
f8v c[8];
#pragma unroll
for (int i = 0; i < 8; ++i) c[i] = (f8v)(0.0f);
float f[32];
#pragma unroll
for (int i = 0; i < 32; ++i) f[i] = (float)(threadIdx.x + i);
const float kk = 1.0000001f;
typedef typename std::conditional<B == 8, u2v, u4v>::type RT;
RT r[L > 0 ? L : 1];
#pragma unroll
for (int i = 0; i < (L > 0 ? L : 1); ++i) r[i] = RT{};
// A fragment read's address pattern: lane row (l & 15) at a padded pitch, half k by lane half,
// so sixteen rows land on distinct banks.
const uint32_t la = (uint32_t)(uintptr_t)pad + (lane & 15u) * (B == 8 ? 72u : 144u) +
(lane >> 4) * B + wave * 2304u;
const uint32_t sa = (uint32_t)(uintptr_t)pad + threadIdx.x * 8u;
const u2v sv = { threadIdx.x, lane };
for (int it = 0; it < iters; ++it) {
#pragma unroll
for (int i = 0; i < 8; ++i) {
c[i % C] = __builtin_amdgcn_wmma_f32_16x16x16_fp8_fp8_w32_gfx12(a[i], b[(i + 3) & 7],
c[i % C]);
#pragma unroll
for (int v = i * V / 8; v < (i + 1) * V / 8; ++v)
asm volatile("v_add_f32 %0, %0, %1" : "+v"(f[v & 31]) : "v"(kk));
#pragma unroll
for (int l = i * L / 8; l < (i + 1) * L / 8; ++l) {
if constexpr (B == 8)
asm volatile("ds_load_b64 %0, %1 offset:%2" : "+v"(r[l]) : "v"(la), "i"((l % 8) * 16));
else
asm volatile("ds_load_b128 %0, %1 offset:%2" : "+v"(r[l]) : "v"(la), "i"((l % 4) * 32));
}
#pragma unroll
for (int s = i * S / 8; s < (i + 1) * S / 8; ++s)
asm volatile("ds_store_b64 %0, %1 offset:%2" ::"v"(sa), "v"(sv), "i"((s % 8) * 2048));
}
if (L > 0 || S > 0) asm volatile("s_wait_dscnt 0x0" ::: "memory");
}
float s = 0.0f;
#pragma unroll
for (int i = 0; i < 8; ++i)
#pragma unroll
for (int t = 0; t < 8; ++t) s += c[i][t];
#pragma unroll
for (int i = 0; i < 32; ++i) s += f[i];
#pragma unroll
for (int i = 0; i < (L > 0 ? L : 1); ++i) s += (float)(r[i].x ^ r[i].y);
s += (float)pad[(threadIdx.x * 7u) % LDS];
if (s == -1.0f) out[0] = s;
}
static long read_long(const std::string& path) {
FILE* f = std::fopen(path.c_str(), "r");
if (!f) return -1;
long v = -1;
if (std::fscanf(f, "%ld", &v) != 1) v = -1;
std::fclose(f);
return v;
}
static std::string g_hw; // the hwmon directory of device 0
template <int LDS, int V, int L, int B, int S, int C>
static void run(const char* name, const uint32_t* d, float* o) {
hipDeviceProp_t prop;
(void)hipGetDeviceProperties(&prop, 0);
int per = 0;
(void)hipOccupancyMaxActiveBlocksPerMultiprocessor(&per, mix<LDS, V, L, B, S, C>, 256, 0);
const int blocks = per * prop.multiProcessorCount;
const int iters = 20000;
hipEvent_t e0, e1;
(void)hipEventCreate(&e0);
(void)hipEventCreate(&e1);
(void)hipEventRecord(e0);
hipLaunchKernelGGL((mix<LDS, V, L, B, S, C>), dim3(blocks), dim3(256), 0, 0, d, o, iters);
(void)hipEventRecord(e1);
(void)hipEventSynchronize(e1);
float ms1 = 0;
(void)hipEventElapsedTime(&ms1, e0, e1);
const int reps = (int)(1000.0f / ms1) + 1;
for (int r = 0; r < reps / 3; ++r)
hipLaunchKernelGGL((mix<LDS, V, L, B, S, C>), dim3(blocks), dim3(256), 0, 0, d, o, iters);
(void)hipDeviceSynchronize();
std::atomic<bool> stop{ false };
double mhz = 0, watt = 0;
int n = 0;
std::thread th([&] {
while (!stop) {
const long f = read_long(g_hw + "/freq1_input");
if (f > 0) { mhz += f / 1e6; watt += read_long(g_hw + "/power1_average") / 1e6; ++n; }
std::this_thread::sleep_for(std::chrono::milliseconds(20));
}
});
(void)hipEventRecord(e0);
for (int r = 0; r < reps; ++r)
hipLaunchKernelGGL((mix<LDS, V, L, B, S, C>), dim3(blocks), dim3(256), 0, 0, d, o, iters);
(void)hipEventRecord(e1);
(void)hipEventSynchronize(e1);
stop = true;
th.join();
float ms = 0;
(void)hipEventElapsedTime(&ms, e0, e1);
const double clk = n ? mhz / n : 0.0;
const double wmma = 8.0 * iters * (double)blocks * 8.0 * reps;
// 64 CUs x 2 SIMDs; HIP's multiProcessorCount is the WGP count, 32.
const double simds = 4.0 * prop.multiProcessorCount;
const double clk_per = simds * (double)ms * 1e-3 * clk * 1e6 / wmma;
std::printf(" %-28s %4.1f waves/SIMD %5.0f MHz %4.0f W %6.1f TFLOP/s %6.2f clk/WMMA/SIMD"
" (%5.1f%% of 8)\n",
name, (double)blocks * 8.0 / simds, clk, n ? watt / n : 0.0,
wmma * 8192.0 / ((double)ms * 1e9), clk_per, 800.0 / clk_per);
(void)hipEventDestroy(e0);
(void)hipEventDestroy(e1);
}
int main(int argc, char** argv) {
const bool zero = argc > 1 && argv[1][0] == 'z';
char bus[64] = {};
if (hipDeviceGetPCIBusId(bus, sizeof(bus), 0) == hipSuccess) {
for (char* q = bus; *q; ++q) *q = (char)std::tolower(*q);
const std::string base = std::string("/sys/bus/pci/devices/") + bus + "/hwmon";
if (DIR* dd = opendir(base.c_str())) {
while (dirent* e = readdir(dd))
if (!std::strncmp(e->d_name, "hwmon", 5)) { g_hw = base + "/" + e->d_name; break; }
closedir(dd);
}
}
if (g_hw.empty()) { std::printf("no hwmon node for device 0: the clock cannot be read\n"); return 1; }
const size_t nw = 64 * 32 * 32;
std::vector<uint32_t> h(nw);
uint64_t x = 88172645463325252ull;
for (auto& w : h) {
uint32_t v = 0;
for (int b = 0; b < 4; ++b) {
x ^= x << 13; x ^= x >> 7; x ^= x << 17;
const uint32_t r = (uint32_t)x;
v |= (((r & 0x80u) | ((6u + (r >> 8) % 4u) << 3) | (r & 7u)) & 0xFFu) << (8 * b);
}
w = zero ? 0u : v;
}
uint32_t* d;
float* o;
(void)hipMalloc(&d, nw * 4);
(void)hipMalloc(&o, 4);
(void)hipMemcpy(d, h.data(), nw * 4, hipMemcpyHostToDevice);
std::printf("fp8 WMMA mixes, %s operands, sensors %s\n", zero ? "zero" : "random", g_hw.c_str());
std::printf("occupancy (blocks a WGP set by LDS)\n");
run<65536, 0, 0, 8, 0, 8>("wmma only", d, o);
run<40000, 0, 0, 8, 0, 8>("wmma only", d, o);
run<32768, 0, 0, 8, 0, 8>("wmma only", d, o);
run<20480, 0, 0, 8, 0, 8>("wmma only", d, o);
run<8192, 0, 0, 8, 0, 8>("wmma only", d, o);
std::printf("dependent chains\n");
run<40000, 0, 0, 8, 0, 1>("1 chain", d, o);
run<40000, 0, 0, 8, 0, 4>("4 chains", d, o);
std::printf("VALU adds per 8 WMMA\n");
run<40000, 8, 0, 8, 0, 8>("8 valu", d, o);
run<40000, 16, 0, 8, 0, 8>("16 valu", d, o);
run<40000, 32, 0, 8, 0, 8>("32 valu", d, o);
run<40000, 64, 0, 8, 0, 8>("64 valu", d, o);
std::printf("LDS loads per 8 WMMA (a bf16 or fp8 fragment read is 256 or 512 B a wave)\n");
run<40000, 0, 4, 8, 0, 8>("4 ds_load_b64", d, o);
run<40000, 0, 9, 8, 0, 8>("9 ds_load_b64", d, o);
run<40000, 0, 12, 8, 0, 8>("12 ds_load_b64", d, o);
run<40000, 0, 16, 8, 0, 8>("16 ds_load_b64", d, o);
run<40000, 0, 32, 8, 0, 8>("32 ds_load_b64", d, o);
run<40000, 0, 8, 16, 0, 8>("8 ds_load_b128", d, o);
run<40000, 0, 16, 16, 0, 8>("16 ds_load_b128", d, o);
std::printf("LDS stores per 8 WMMA\n");
run<40000, 0, 0, 8, 2, 8>("2 ds_store_b64", d, o);
run<40000, 0, 0, 8, 16, 8>("16 ds_store_b64", d, o);
std::printf("mixes\n");
run<40000, 0, 9, 8, 2, 8>("9 load + 2 store", d, o);
run<40000, 0, 4, 8, 1, 8>("4 load + 1 store", d, o);
run<40000, 16, 9, 8, 2, 8>("16 valu + 9 load + 2 store", d, o);
return 0;
}
+242
View File
@@ -0,0 +1,242 @@
// Sustained WMMA throughput on gfx1201 at the card's own clock and power limit: the ceiling a GEMM
// is measured against. isarate.hip answers a ratio of issue slots and needs no clock; this answers
// TFLOP/s, which at a power limit depends on how hard the multipliers switch, so the operands are
// random bits rotating through eight A and eight B fragments (consecutive instructions never see
// the same inputs) and the run is long enough for the clock to settle under the limit.
//
// Each wave issues eight independent accumulator chains back to back, every block of the launch is
// resident at once (the grid is the occupancy calculator's answer times the WGP count, so there is
// no tail), and launches repeat for the requested seconds after a warm-up of the same length. The
// card's own sensors (hwmon freq1_input, power1_average, temp edge) are sampled from a host thread
// during the timed window; the hwmon node is found from the HIP device's PCI bus id.
//
// clang++ -x hip -O3 -std=c++17 --offload-arch=gfx1201 wmmapeak.hip -o wmmapeak
// ./wmmapeak [device] [seconds] [tile]
//
// A non-zero `tile` pads every block to 36864 B of unused LDS, the fp8 prefill tile's footprint
// (three blocks a WGP), to read the ceiling at that occupancy; 0 is full occupancy.
#include <hip/hip_runtime.h>
#include <atomic>
#include <chrono>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <dirent.h>
#include <string>
#include <thread>
#include <vector>
typedef int i2v __attribute__((ext_vector_type(2)));
typedef int i8v __attribute__((ext_vector_type(8)));
typedef float f8v __attribute__((ext_vector_type(8)));
typedef __bf16 v8bf __attribute__((ext_vector_type(8)));
enum { kFp8 = 0, kBf16 = 1, kIu8 = 2 };
// Operand dwords a lane holds per fragment: two for the 8-bit forms, four for bf16.
template <int OP> struct Frag;
template <> struct Frag<kFp8> { typedef i2v T; static constexpr int dw = 2; };
template <> struct Frag<kIu8> { typedef i2v T; static constexpr int dw = 2; };
template <> struct Frag<kBf16> { typedef v8bf T; static constexpr int dw = 4; };
template <int OP>
__device__ __forceinline__ f8v mma(typename Frag<OP>::T a, typename Frag<OP>::T b, f8v c) {
if constexpr (OP == kFp8) return __builtin_amdgcn_wmma_f32_16x16x16_fp8_fp8_w32_gfx12(a, b, c);
if constexpr (OP == kBf16) return __builtin_amdgcn_wmma_f32_16x16x16_bf16_w32_gfx12(a, b, c);
if constexpr (OP == kIu8)
return __builtin_bit_cast(f8v, __builtin_amdgcn_wmma_i32_16x16x16_iu8_w32_gfx12(
true, a, true, b, __builtin_bit_cast(i8v, c), false));
}
template <int OP>
__device__ __forceinline__ typename Frag<OP>::T load_frag(const uint32_t* p) {
typedef typename Frag<OP>::T T;
union { uint32_t w[Frag<OP>::dw]; T f; } u;
#pragma unroll
for (int i = 0; i < Frag<OP>::dw; ++i) u.w[i] = p[i];
return u.f;
}
// `src` holds 16 fragments' worth of random dwords per lane (8 A, then 8 B); `out` receives a sum
// that is never -1 in practice, so the chains stay live.
template <int OP, int LDS>
__global__ __launch_bounds__(256) void peak(const uint32_t* __restrict__ src, float* out,
int iters) {
__shared__ char pad[LDS ? LDS : 4];
pad[threadIdx.x & 3] = (char)threadIdx.x;
typedef typename Frag<OP>::T T;
constexpr int dw = Frag<OP>::dw;
const uint32_t lane = threadIdx.x & 31u;
const uint32_t* p = src + ((size_t)(blockIdx.x & 63u) * 32u + lane) * 16u * dw;
T a[8], b[8];
#pragma unroll
for (int i = 0; i < 8; ++i) { a[i] = load_frag<OP>(p + i * dw); b[i] = load_frag<OP>(p + (8 + i) * dw); }
f8v c[8];
#pragma unroll
for (int i = 0; i < 8; ++i) c[i] = (f8v)(0.0f);
for (int it = 0; it < iters; ++it) {
#pragma unroll
for (int i = 0; i < 8; ++i) c[i] = mma<OP>(a[i], b[(i + 3) & 7], c[i]);
}
float s = 0.0f;
#pragma unroll
for (int i = 0; i < 8; ++i)
#pragma unroll
for (int t = 0; t < 8; ++t) s += c[i][t];
if (s == -1.0f) out[0] = s + (float)pad[0];
}
// ---- the card's sensors ------------------------------------------------------------------------
static std::string hwmon_dir(int dev) {
char bus[64] = {};
if (hipDeviceGetPCIBusId(bus, sizeof(bus), dev) != hipSuccess) return "";
for (char* q = bus; *q; ++q) *q = (char)std::tolower(*q);
const std::string base = std::string("/sys/bus/pci/devices/") + bus + "/hwmon";
DIR* d = opendir(base.c_str());
if (!d) return "";
std::string found;
while (dirent* e = readdir(d))
if (!std::strncmp(e->d_name, "hwmon", 5)) { found = base + "/" + e->d_name; break; }
closedir(d);
return found;
}
static long read_long(const std::string& path) {
FILE* f = std::fopen(path.c_str(), "r");
if (!f) return -1;
long v = -1;
if (std::fscanf(f, "%ld", &v) != 1) v = -1;
std::fclose(f);
return v;
}
struct Sensors {
double mhz_sum = 0, mhz_min = 1e30, mhz_max = 0, w_sum = 0, t_max = 0;
int n = 0;
};
static void sample(const std::string& hw, std::atomic<bool>* stop, Sensors* s) {
while (!stop->load()) {
const long f = read_long(hw + "/freq1_input"), p = read_long(hw + "/power1_average"),
t = read_long(hw + "/temp1_input");
if (f > 0) {
const double mhz = (double)f / 1e6;
s->mhz_sum += mhz;
if (mhz < s->mhz_min) s->mhz_min = mhz;
if (mhz > s->mhz_max) s->mhz_max = mhz;
s->w_sum += p > 0 ? (double)p / 1e6 : 0.0;
if ((double)t / 1e3 > s->t_max) s->t_max = (double)t / 1e3;
++s->n;
}
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
}
template <int OP, int LDS>
static void run(const char* name, int dev, double secs, const uint32_t* d_src, float* d_out,
const std::string& hw) {
hipDeviceProp_t prop;
(void)hipGetDeviceProperties(&prop, dev);
int per = 0;
(void)hipOccupancyMaxActiveBlocksPerMultiprocessor(&per, peak<OP, LDS>, 256, 0);
const int blocks = per * prop.multiProcessorCount;
// About 20 ms a launch at any rate: calibrate on one short launch.
int iters = 4096;
hipEvent_t e0, e1;
(void)hipEventCreate(&e0);
(void)hipEventCreate(&e1);
hipLaunchKernelGGL((peak<OP, LDS>), dim3(blocks), dim3(256), 0, 0, d_src, d_out, iters);
(void)hipEventRecord(e0);
hipLaunchKernelGGL((peak<OP, LDS>), dim3(blocks), dim3(256), 0, 0, d_src, d_out, iters);
(void)hipEventRecord(e1);
(void)hipEventSynchronize(e1);
float ms = 0;
(void)hipEventElapsedTime(&ms, e0, e1);
iters = (int)((double)iters * 20.0 / (ms > 0.01f ? ms : 0.01f));
if (iters < 256) iters = 256;
const int launches = (int)(secs * 1000.0 / 20.0) + 1;
// Warm-up of the same length, so the timed window starts at the settled clock.
for (int i = 0; i < launches; ++i)
hipLaunchKernelGGL((peak<OP, LDS>), dim3(blocks), dim3(256), 0, 0, d_src, d_out, iters);
(void)hipDeviceSynchronize();
Sensors s;
std::atomic<bool> stop{false};
std::thread th;
if (!hw.empty()) th = std::thread(sample, hw, &stop, &s);
(void)hipEventRecord(e0);
for (int i = 0; i < launches; ++i)
hipLaunchKernelGGL((peak<OP, LDS>), dim3(blocks), dim3(256), 0, 0, d_src, d_out, iters);
(void)hipEventRecord(e1);
(void)hipEventSynchronize(e1);
stop = true;
if (th.joinable()) th.join();
(void)hipEventElapsedTime(&ms, e0, e1);
const double flop = 8192.0 * 8.0 * 8.0 * (double)iters * (double)blocks * (double)launches;
const double tf = flop / ((double)ms * 1e9);
const double mhz = s.n ? s.mhz_sum / s.n : 0.0;
// Per CU per clock (64 CUs; HIP's multiProcessorCount is the WGP count, half of it).
const double per_clk = mhz > 0 ? tf * 1e12 / (2.0 * prop.multiProcessorCount * mhz * 1e6) : 0.0;
std::printf(" %-6s %3d blocks x 256 %8.1f ms %7.1f TFLOP/s sclk %6.0f MHz (min %4.0f max "
"%4.0f) %5.1f W %4.0f C %6.0f FLOP/clk/CU\n",
name, blocks, ms, tf, mhz, s.n ? s.mhz_min : 0.0, s.mhz_max, s.n ? s.w_sum / s.n : 0.0,
s.t_max, per_clk);
(void)hipEventDestroy(e0);
(void)hipEventDestroy(e1);
}
int main(int argc, char** argv) {
const int dev = argc > 1 ? std::atoi(argv[1]) : 0;
const double secs = argc > 2 ? std::atof(argv[2]) : 3.0;
const int tile = argc > 3 ? std::atoi(argv[3]) : 0;
(void)hipSetDevice(dev);
const std::string hw = hwmon_dir(dev);
std::printf(" WMMA sustained, device %d, %.1f s timed after %.1f s warm, sensors %s, power cap "
"%.0f W\n",
dev, secs, secs, hw.empty() ? "(none)" : hw.c_str(),
hw.empty() ? 0.0 : (double)read_long(hw + "/power1_cap") / 1e6);
// Random operand bits, 64 blocks' worth so neighbouring blocks differ. The 8-bit forms take
// every byte but E4M3's NaN codes and keep the exponent near one; bf16 likewise near one, so
// nothing overflows over the run.
const size_t nw = (size_t)64 * 32 * 16 * 4;
std::vector<uint32_t> h8(nw), h16(nw), hi8(nw);
uint64_t x = 0x9E3779B97F4A7C15ull;
auto rnd = [&]() { x ^= x << 13; x ^= x >> 7; x ^= x << 17; return (uint32_t)x; };
for (size_t i = 0; i < nw; ++i) {
uint32_t w = 0;
for (int b = 0; b < 4; ++b) {
const uint32_t r = rnd();
const uint32_t e = 6u + (r >> 8) % 4u; // exponent field 6..9
w |= (((r & 0x80u) | (e << 3) | (r & 7u)) & 0xFFu) << (8 * b);
}
h8[i] = w;
uint32_t v = 0;
for (int hh = 0; hh < 2; ++hh) {
const uint32_t r = rnd();
const uint32_t e = 125u + (r >> 16) % 4u; // 2^-2 .. 2^1
v |= (((r & 0x8000u) | (e << 7) | (r & 0x7Fu)) & 0xFFFFu) << (16 * hh);
}
h16[i] = v;
hi8[i] = rnd();
}
uint32_t *d8, *d16, *di8;
float* dout;
(void)hipMalloc(&d8, nw * 4);
(void)hipMalloc(&d16, nw * 4);
(void)hipMalloc(&di8, nw * 4);
(void)hipMalloc(&dout, 4);
(void)hipMemcpy(d8, h8.data(), nw * 4, hipMemcpyHostToDevice);
(void)hipMemcpy(d16, h16.data(), nw * 4, hipMemcpyHostToDevice);
(void)hipMemcpy(di8, hi8.data(), nw * 4, hipMemcpyHostToDevice);
if (tile == 0) {
run<kFp8, 0>("fp8", dev, secs, d8, dout, hw);
run<kIu8, 0>("iu8", dev, secs, di8, dout, hw);
run<kBf16, 0>("bf16", dev, secs, d16, dout, hw);
} else {
run<kFp8, 36864>("fp8", dev, secs, d8, dout, hw);
run<kIu8, 36864>("iu8", dev, secs, di8, dout, hw);
run<kBf16, 36864>("bf16", dev, secs, d16, dout, hw);
}
return 0;
}