Archived
Two latent bugs that the packet processor's acquire (about 0.9 us a launch) has been hiding by timing. 1. Kernel arguments and the chain record are written into VRAM through the BAR, and the card's host data path keeps such a write until it is flushed. Nothing ordered that against the doorbell (the sfence only orders the CPU's own buffers). A kernel the card starts a microsecond or two after its doorbell reads the slot as the launch one lap of the ring before left it: old arguments, or no chain record, in which case the launch counts nothing and the chained launch behind it waits for good. The backend now stores to the card's HDP flush register (HSA_AMD_AGENT_INFO_HDP_FLUSH) in front of every doorbell, in publish() and in a played pass's ring(). Without a register (the argument ring in host memory, or a driver that gives none) nothing changes, and a warning says so at startup. 2. rad_chain_wait()/r4d_chain_wait() end in an agent-scope acquire fence, which invalidates the vector caches. A load whose address every lane shares is a scalar load, served from the scalar data cache, and nothing but the packet processor's acquire (which runs when the kernel is launched, before the launch in front has written) empties that. A chained kernel can therefore read a stale scale, count or activation from the arena address an earlier launch read. The waits now issue s_dcache_inv behind the fence (r4d_chain_acquire in libr4d, chain_kernel.h in the core's blit kernels). Evidence on an R9700 with the packet acquire left as it is: a probe of 1024-block readers behind a chained wait saw a stale shared value in 1 launch of 1800 without the s_dcache_inv and in none with it; card-side copies issued one by one with 50 us between them stalled the queue in its first lap, four runs in four, and six runs in nine of 300000 back to back ended with a unit moved by another launch's arguments, when the packets went without their acquire and without the flush, and in none with the flush. New device tests: a chained reader sees a shared value the launch in front wrote, a chained launch reads its own arguments after the ring has lapped, and a copy issued after a pause finds the record written for it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
110 lines
5.4 KiB
C
110 lines
5.4 KiB
C
// r4d_chain.h -- a chained launch's wait and completion (RadKernelChainFn, abi/rad_abi.h).
|
|
//
|
|
// Between two dependent kernels on one queue the packet processor waits for the first to finish
|
|
// before it launches the second (the AQL barrier bit): on 4x R9700, ~1.3 ms of a 14 ms DFlash2 step
|
|
// on the 31B. A chained launch drops the bit. The engine writes a record into the
|
|
// kernel's argument slot, and the kernel waits for its queue's completion counter to reach the
|
|
// blocks issued before it, then adds its own blocks as they finish. A kernel's chained form
|
|
// (`CHAIN` true) brackets its body with r4d_chain_wait / r4d_chain_done; the plain form never reads
|
|
// the record, so a launch from outside the engine (r4d_selftest, rad-kbench) is untouched.
|
|
//
|
|
// Every block must reach r4d_chain_done exactly once, so a kernel whose blocks return early keeps
|
|
// its body in a __device__ function called from the chained wrapper.
|
|
//
|
|
// The wait's acquire has to cover every read behind it, and an agent-scope fence does not: it
|
|
// invalidates the vector caches, while a load whose address every lane shares is a scalar load,
|
|
// served from the scalar data cache. A GEMM reads its rows' scales that way, the routed kernels
|
|
// their counts and offsets, the head its activation (r4dchain_lm_wmma, 16 words a load). What that
|
|
// cache holds is what a launch read from the same arena address a layer earlier. The packet
|
|
// processor's acquire empties it when it launches the kernel, which is before the kernels in front
|
|
// have written; r4d_chain_wait therefore empties it behind the fence itself (r4d_chain_acquire).
|
|
// With the packets' acquire on, a probe of 1024-block readers saw one launch in 1800 read a stale
|
|
// value without it.
|
|
#pragma once
|
|
#include <hip/hip_runtime.h>
|
|
#include "rad_abi.h"
|
|
|
|
/* A kernel template's chain parameter, for a kernel that brackets its own body: `r4dchain`
|
|
* instantiates the chained form and puts the name the backend knows a chained kernel by into the
|
|
* instantiation's symbol, R4dUnchained the plain form. */
|
|
struct r4dchain { static constexpr bool on = true; };
|
|
struct R4dUnchained { static constexpr bool on = false; };
|
|
|
|
#if !defined(__HIP__)
|
|
/* The host SIMT emulator (tests/r4d_emu) compiles some of these sources as C++: no engine record
|
|
* ever reaches a launch there, so a chained form is its plain form. */
|
|
__device__ __forceinline__ void r4d_chain_wait() {}
|
|
__device__ __forceinline__ void r4d_chain_done() {}
|
|
#else
|
|
|
|
struct R4dChainRec {
|
|
unsigned long long magic, target;
|
|
unsigned long long* ctr;
|
|
unsigned long long check;
|
|
};
|
|
|
|
// The record, or null where it is not the engine's. Read from the argument segment (scalar loads)
|
|
// at both ends of the kernel, so nothing is held across the body: a register-heavy body (the
|
|
// split-KV decode attention) would otherwise pay for the pointer.
|
|
__device__ __forceinline__ const R4dChainRec* r4d_chain_rec() {
|
|
const R4dChainRec* r = (const R4dChainRec*)((const char*)__builtin_amdgcn_kernarg_segment_ptr() +
|
|
RAD_CHAIN_KARG_OFF);
|
|
const unsigned long long magic = r->magic, target = r->target, check = r->check;
|
|
const unsigned long long* ctr = r->ctr;
|
|
if (magic != RAD_CHAIN_MAGIC ||
|
|
check != (magic ^ target ^ (unsigned long long)(uintptr_t)ctr ^ RAD_CHAIN_CHECK))
|
|
return nullptr;
|
|
return r;
|
|
}
|
|
|
|
// Before the first read of an operand: one lane waits for the counter to reach the target.
|
|
// Every resident block polls the same word, so a wide kernel launched behind a long one (a GEMM
|
|
// behind a junction's all-reduce) would put hundreds of loads into one L2 channel every few dozen
|
|
// clocks for as long as the wait lasts, slowing whatever else that channel serves. The first
|
|
// R4D_CHAIN_FAST polls are ~64 clocks apart, the rest R4D_CHAIN_SLOW x 64: a long wait ends at most
|
|
// that much late and puts 1/R4D_CHAIN_SLOW of the load on the channel.
|
|
#ifndef R4D_CHAIN_FAST
|
|
#define R4D_CHAIN_FAST 8
|
|
#endif
|
|
#ifndef R4D_CHAIN_SLOW
|
|
#define R4D_CHAIN_SLOW 16
|
|
#endif
|
|
// The acquire behind a chained wait: the vector caches by the fence, the scalar data cache by
|
|
// hand, which the fence leaves (see the head of this file). Every wave, for the compute unit it
|
|
// runs on.
|
|
__device__ __forceinline__ void r4d_chain_acquire() {
|
|
__builtin_amdgcn_fence(__ATOMIC_ACQUIRE, "agent");
|
|
asm volatile("s_dcache_inv" ::: "memory");
|
|
}
|
|
|
|
__device__ __forceinline__ void r4d_chain_wait() {
|
|
const R4dChainRec* r = r4d_chain_rec();
|
|
if (!r) return;
|
|
if (threadIdx.x == 0 && threadIdx.y == 0 && threadIdx.z == 0) {
|
|
const unsigned long long target = r->target;
|
|
unsigned long long* ctr = r->ctr;
|
|
int n = 0;
|
|
while (__hip_atomic_load(ctr, __ATOMIC_RELAXED, __HIP_MEMORY_SCOPE_AGENT) < target) {
|
|
if (n < R4D_CHAIN_FAST) {
|
|
__builtin_amdgcn_s_sleep(1);
|
|
++n;
|
|
} else {
|
|
__builtin_amdgcn_s_sleep(R4D_CHAIN_SLOW);
|
|
}
|
|
}
|
|
}
|
|
__syncthreads();
|
|
r4d_chain_acquire();
|
|
}
|
|
|
|
// After the block's last write: release, then one add a block.
|
|
__device__ __forceinline__ void r4d_chain_done() {
|
|
const R4dChainRec* r = r4d_chain_rec();
|
|
if (!r) return;
|
|
__builtin_amdgcn_fence(__ATOMIC_RELEASE, "agent");
|
|
__syncthreads();
|
|
if (threadIdx.x == 0 && threadIdx.y == 0 && threadIdx.z == 0)
|
|
__hip_atomic_fetch_add(r->ctr, 1ull, __ATOMIC_RELEASE, __HIP_MEMORY_SCOPE_AGENT);
|
|
}
|
|
#endif /* __HIP__ */
|