This repository has been archived on 2026-11-24. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
Deadcode 769b4c64a6 radiance 1.16.0
New models:
- Hunyuan3D-2 (hunyuan3d2): an untextured 3-D mesh from one picture of an object. DINOv2-giant
  over the picture, a FLUX-shaped flow-matching DiT (16 double-stream and 32 single-stream blocks
  of 1024) over 3072 latents with both guidance branches in one step, and the shape VAE's
  geometry decoder. Converted from tencent/Hunyuan3D-2's hunyuan3d-dit-v2-0 folder with
  data/recipes/hunyuan3d-2-bf16.recipe (fp16 cast to bf16, 4.4 GiB, one card). The licence
  (Tencent Hunyuan 3D 2.0 Community License) does not cover the EU, the UK or South Korea
- Served on Hunyuan3D-2's own API server routes, so its Blender add-on and scripts work
  unchanged: POST /generate answers with the mesh, POST /send runs it in the background and
  GET /status/{uid} reports steps_done and then the file. GLB, OBJ, PLY or STL; the field is
  sampled coarse to fine as FlashVDM's hierarchical decoding does, or densely with
  "volume_decoding": "dense"; seed, steps, guidance and octree_resolution as the reference.
  Texturing (Hunyuan3D-Paint) is refused for now (docs/GUIDE.md §5.17)

Merged:
- libr3d rewritten for RDNA3's wave matrix (@RikshaDriver): bf16, f16, int8-activation and vocab
  GEMMs retiled to 16x128 blocks with a double-buffered k-walk; fp8, mxfp4a8, w2a8, w4a16, w5a16
  and w8a8 quantised GEMMs and the gated form; grouped MoE GEMMs (bf16, fp8a8f32, mxfp4a8); the
  norms and quantisers, the QSA state ops and the MoE router, scatter and gather. Brought onto the
  op contract before release (13 schemas, the MoE GEMMs on weight tables with a_order and bf16
  output, moe_gather's gate pitch, moe_scatter above 1024 slots, the mxfp4 m16 dispatch) and its
  gated-delta-net kernels replaced by libhalo's; every op it serves now passes rad-kbench against
  libref on a gfx1151, and r3d_selftest checks its MoE GEMMs by value
- Gemma 4 31B: asymmetric int5 linears (w5a8i8 above 64 rows, w5a16 up to 64, a scale per 128),
  the norm, GELU and two-rank all-reduce junctions at quant_act_i8g's grid (rmsnorm_quant_i8g,
  gelu_mul_quant_i8g, ar_rmsnorm_quant_i8g) so the prefill overlap runs on the i8a8, a4 and w5
  trunks, and compose files for the 31B i8g, heretic-ara-i8g and w5 on two cards (@keptn42)
- The norms' lane sum reads eight at a time: TranslateGemma 27B decode is back from 225 to 239
  tok/s a stream at four streams, bit for bit the same output (@mici81)
- The head-256 prefill softmax rescales each row by its own maximum, not by a vote of the wave, so
  a row's rounding no longer depends on which rows share its launch (@gostar)

Anthropic Messages API:
- /v1/messages takes min_p, presence_penalty, frequency_penalty, repetition_penalty and
  repeat_last_n as top-level fields with the chat path's names, ranges and meaning, so clients that
  speak only the Anthropic format (Claude Code) can set a model card's sampling, such as Qwen's
  presence_penalty 1.5. The penalties count the last repeat_last_n tokens of prompt and reply (64
  by default, --repeat-last-n), as llama.cpp does

Plugin ABI 33:
- Shape generators: rad_declare_shape_generator, RadShapeGenBatch (RadBatch::sgen_b) and
  RadArchProbe::gen_shape, with the SCOND, SSTEP, SLATENT and SQUERY passes. The layout
  fingerprint table is regenerated; out-of-tree plugins rebuild against 33
- New op point_fourier ([x | sin(x 2^j) | cos(x 2^j)], times pi on request) in libref, libavx,
  libr4d and libhalo
- A checkpoint for a model that reads no text gets a one-token vocabulary

More cards from the one image:
- stilldeadcode/radiance now carries libr3d (RDNA3: gfx11-generic, every gfx11 chip) and libhalo
  (Strix Halo: gfx1151, preferred there over libr3d) beside libr4d (gfx1201). docker/build.sh and
  the Dockerfile default to gfx1201;gfx11-generic;gfx1151
- libr3d loads only where the KFD topology lists a gfx11 card and libhalo only where it lists a
  gfx1151; a process without /dev/kfd sees neither. On an RDNA4 card, or with no card at all,
  the image therefore behaves exactly as before, and rad-convert lays weights out as before
- libhalo and libr3d now declare gemm_nt_q's w_rb, and libhalo gram_accum's upper, as libr4d
  does. Before, any build carrying them refused both at load ("9 parameters vs 8"), so a build
  for Strix Halo or RDNA3 had no device kernel library
- libhalo does not yet have every op libr4d added after it was ported (the IQR rows, Pixtral and
  Ministral rope, the tree-attention and DFlash-tree rows, among others); halo_parity lists them
  and is left out of the image's test stage until they are ported

Fixed -- container and checkpoint parsing:
- A .rad expert-1 entry inside or before expert 0 is refused; a vocab section whose count names
  an array at offset 0 is refused rather than read from the file header; offset checks no longer
  wrap; header tables must sit on their element alignment
- An imatrix array of arrays (unbounded recursion) and a safetensors header naming a tensor twice
  are refused; a failed RadWriter undo says the container is damaged; a tune cache measured on
  another machine is refused, as its format contract says

Fixed -- plugins:
- Two plugins declaring one op with different parameter roles are refused; before, the core
  applied the first one's chunk/capacity roles to the other's rows
- Null tables behind nonzero counts, unknown roles, keyless parameters or constraints and more
  operands than an issue carries are refused by name at load; on the host backend device rows are
  dropped even when no fat binary was registered; rad-schemas checks the ABI layout fingerprint as
  the engine does

Fixed -- device:
- Stream slots are recycled under a new generation: before, 1024 stream create/destroy cycles
  (LoRA loads, lanes, uplinks) made every later rad_stream_create fail
- A stream whose drain fails keeps its queue and kernarg ring instead of freeing them under the
  card; the kernarg ring no longer leaks when its access grant fails; a block over 1024 work-items
  is refused
- rad_dev_free takes the allocation off the ledger the pin-cap check reads; the host backend
  charges pages to the card the range was reserved for

Fixed -- engine and server:
- Two NDEBUG-only asserts (runtime-window totals, ring blocks per group) are refusals; a
  plugin-declared lcm past int64 saturates so the quantum check refuses it; every share view rect
  and part span is bounded by its entry
- Video: the timestep regions hold the refine rungs a schedule appends, a zero-frame encode is
  refused, /v1/videos bounds each side before the area product, and a realtime session needs a
  clip length to cut a turn at

Fixed -- kernels and quantisation:
- A trellis run shorter than a state's codes (tcq_rows=1, or N % tcq_rows == 1) read its seam
  out of bounds; the encoder now refuses it as the decoder does, and tcq_rows below one state is
  refused at parse
- qsa_tail_store bounds its slot index by the state plane (libr4d and libhalo); paged attention
  refuses kv_heads <= 0; libavx row_amax skips a NaN as libref does

Fixed -- tools:
- pad_tp.py no longer runs cp through a shell with shard names from the checkpoint's index
  (quote injection, path traversal): names must be bare file names
- ops_doc_check.sh runs under mawk (Ubuntu's awk); rad-kbench --max-cases/--max-bytes are checked
  integers; oot_plugins fails unless the installed replay passed a case and failed none

Thanks to @RikshaDriver, @keptn42, @mici81 and @gostar.
2026-11-02 18:35:02 +01:00

333 lines
14 KiB
Plaintext

/* r3d_moe_scatter.hip -- moe_scatter: the routed slots' stable counting sort.
*
* One workgroup does the whole sort (the "small" form, T <= 1024, which covers every decode and
* verify step): histogram in LDS, offsets an exclusive prefix over n_expert <= 512 counts, and
* each slot placed by its rank inside its expert's run. Stable and byte-identical to the
* reference: a slot's place is its position among the same expert's slots in ascending i.
*
* expert_ids [T] -> sorted_tok [T], expert_offset [n_expert+1], expert_count [n_expert]
*
* `expert_base` is this rank's first expert, subtracted from every routed id so the offsets,
* counts and (downstream) weight table are indexed locally; an id outside the slice is dropped
* (sorted to the tail as -1). `protect` is a comma list of local expert indices placed last.
*
* gfx1102 note: the prefix scan's per-wave inclusive sum goes through LDS, not the DPP row-shift
* butterfly (a DPP exchange inside the scan's tile loop miscompiles here). Each wave writes its
* lane's count to LDS, barriers, and takes the serial prefix of its 32 lanes.
*
* Above 1024 slots the sort is three passes (libhalo's general form): a histogram per 256-slot
* tile into the caller's scratch, a per-expert scan across the tiles, and the placement (an
* expert's offset, plus its count in earlier tiles, plus the slot's rank in its own tile). Stable
* too, so both forms give the same bytes. The scan's cross-expert prefix is a serial LDS loop, not
* a wave scan, for the same gfx1102 reason as above.
*/
#include <hip/hip_runtime.h>
#include <cstdint>
#include <cstdlib>
#include "r3d.h"
#include "r3d_args.h"
#include "r3d_launch.h"
#define R3D_BARRIER() do { __syncthreads(); asm volatile("" ::: "memory"); } while (0)
namespace {
constexpr uint32_t kSortWave = 32;
constexpr uint32_t kMaxExpert = 512;
constexpr uint32_t kMaxProtect = 8;
constexpr uint32_t kScatterSmall = 1024;
constexpr uint32_t kScatWaves = 8; // 256 threads
struct Protect {
uint32_t n;
uint16_t e[kMaxProtect];
};
/* Local expert `e`'s place in the sort: the protected ones last, the rest closed up in order. */
__device__ __forceinline__ int32_t sort_place(int32_t e, uint32_t n_local, const Protect& p) {
int32_t below = 0;
for (uint32_t j = 0; j < p.n; ++j) {
if ((int32_t)p.e[j] == e) return (int32_t)(n_local - p.n + j);
below += (int32_t)p.e[j] < e;
}
return e - below;
}
template <uint32_t NW>
struct ScatterLds {
static constexpr uint32_t kTiles = (kMaxExpert + NW * kSortWave - 1) / (NW * kSortWave);
int32_t e[kScatterSmall];
int32_t tk[kScatterSmall];
int32_t run[kScatterSmall];
int32_t cnt[kMaxExpert + 1];
int32_t off[kMaxExpert + 1];
int32_t wt[kTiles][NW];
int32_t scan[NW * kSortWave]; // the LDS inclusive-scan scratch (gfx1102: no DPP here)
};
/* The sort, from a filled slot list on: histogram, offsets and counts, placement. The caller has
* written sl.e[0,T) and zeroed sl.cnt[0,n_expert), followed by a barrier. */
template <uint32_t NW, bool INV = false>
__device__ __forceinline__ void scatter_small_place(
int32_t* __restrict__ sorted, int32_t* __restrict__ off, int32_t* __restrict__ cnt,
uint32_t T, uint32_t n_expert, ScatterLds<NW>& sl, int32_t* __restrict__ inv) {
const uint32_t tid = threadIdx.x, lane = tid % kSortWave, wave = tid / kSortWave;
for (uint32_t i = tid; i < T; i += blockDim.x) {
const int32_t e = sl.e[i];
if (e >= 0) sl.tk[i] = atomicAdd(&sl.cnt[e], 1);
}
R3D_BARRIER();
int32_t carry = 0;
for (uint32_t b = 0, t = 0; b < n_expert; b += NW * kSortWave, ++t) {
const uint32_t e = b + tid;
const int32_t v = (e < n_expert) ? sl.cnt[e] : 0;
// Inclusive wave scan through LDS (a DPP row-shift miscompiles on gfx1102).
sl.scan[wave * kSortWave + lane] = v;
R3D_BARRIER();
int32_t x = 0;
for (int i = 0; i <= (int)lane; ++i) x += sl.scan[wave * kSortWave + i];
if (lane == kSortWave - 1) sl.wt[t][wave] = x;
R3D_BARRIER();
int32_t below = 0, tot = 0;
#pragma unroll
for (uint32_t k = 0; k < NW; ++k) {
const int32_t c = sl.wt[t][k];
below += (k < wave) ? c : 0;
tot += c;
}
if (e < n_expert) {
const int32_t o = carry + below + x - v; // inclusive -> exclusive
sl.off[e] = o;
off[e] = o;
cnt[e] = v;
}
carry += tot;
}
if (tid == 0) off[n_expert] = carry;
R3D_BARRIER();
for (uint32_t i = (uint32_t)carry + tid; i < T; i += blockDim.x) sorted[i] = -1;
for (uint32_t i = tid; i < T; i += blockDim.x) {
const int32_t e = sl.e[i];
if (e >= 0) sl.run[sl.off[e] + sl.tk[i]] = (int32_t)i;
}
R3D_BARRIER();
for (uint32_t i = tid; i < T; i += blockDim.x) {
const int32_t e = sl.e[i];
if (e < 0) {
if (INV) inv[i] = -1;
continue;
}
const int32_t o = sl.off[e], n = sl.cnt[e];
int32_t rank = 0;
for (int32_t k = 0; k < n; ++k) rank += sl.run[o + k] < (int32_t)i;
sorted[o + rank] = (int32_t)i;
if (INV) inv[i] = o + rank;
}
}
template <bool INV>
__global__ __launch_bounds__(kScatWaves * kSortWave) void scatter_small_kernel(
const int32_t* __restrict__ ids, int32_t* __restrict__ sorted, int32_t* __restrict__ off,
int32_t* __restrict__ cnt, uint32_t T, uint32_t n_expert, int32_t base, Protect prot,
int32_t* __restrict__ inv) {
__shared__ ScatterLds<kScatWaves> sl;
const uint32_t tid = threadIdx.x;
for (uint32_t e = tid; e < n_expert; e += blockDim.x) sl.cnt[e] = 0;
for (uint32_t i = tid; i < T; i += blockDim.x) {
const int32_t e = ids[i] - base;
sl.e[i] = (e >= 0 && e < (int32_t)n_expert) ? sort_place(e, n_expert, prot) : -1;
}
R3D_BARRIER();
scatter_small_place<kScatWaves, INV>(sorted, off, cnt, T, n_expert, sl, inv);
}
/* ================================================================== the general form */
constexpr uint32_t kScatterTile = 256;
/* Pass 1: each tile's count of every expert, bcnt[tile][expert]. */
__global__ __launch_bounds__(kScatterTile) void scatter_hist_kernel(
const int32_t* __restrict__ ids, int32_t* __restrict__ bcnt, uint32_t T, uint32_t n_expert,
int32_t base, Protect prot) {
__shared__ int32_t s_h[kMaxExpert];
const uint32_t tid = threadIdx.x, b = blockIdx.x;
for (uint32_t e = tid; e < n_expert; e += kScatterTile) s_h[e] = 0;
R3D_BARRIER();
const uint32_t i = b * kScatterTile + tid;
if (i < T) {
const int32_t e = ids[i] - base;
if (e >= 0 && e < (int32_t)n_expert) atomicAdd(&s_h[sort_place(e, n_expert, prot)], 1);
}
R3D_BARRIER();
for (uint32_t e = tid; e < n_expert; e += kScatterTile)
bcnt[(size_t)b * n_expert + e] = s_h[e];
}
/* Pass 2: per expert, the count in earlier tiles (bcnt rewritten in place to it) and the total;
* then the offsets, an exclusive prefix over the totals. */
__global__ __launch_bounds__(kMaxExpert) void scatter_scan_kernel(
int32_t* __restrict__ bcnt, int32_t* __restrict__ off, int32_t* __restrict__ cnt,
uint32_t n_tile, uint32_t n_expert) {
__shared__ int32_t s_run[kMaxExpert];
const uint32_t e = threadIdx.x;
int32_t run = 0;
if (e < n_expert) {
for (uint32_t b = 0; b < n_tile; ++b) {
const int32_t v = bcnt[(size_t)b * n_expert + e];
bcnt[(size_t)b * n_expert + e] = run;
run += v;
}
cnt[e] = run;
}
s_run[e] = run;
R3D_BARRIER();
if (e == 0) {
int32_t acc = 0;
for (uint32_t x = 0; x < n_expert; ++x) { off[x] = acc; acc += s_run[x]; }
off[n_expert] = acc;
}
}
/* Pass 3: each routed slot at its expert's offset plus the expert's count in earlier tiles plus
* its rank in this tile. Sorted rows past the live count are -1; no placement lands there. INV
* also writes each slot's row, -1 when dropped. */
template <bool INV>
__global__ __launch_bounds__(kScatterTile) void scatter_place_kernel(
const int32_t* __restrict__ ids, const int32_t* __restrict__ bcnt,
const int32_t* __restrict__ off, int32_t* __restrict__ sorted,
uint32_t T, uint32_t n_expert, int32_t base, Protect prot, int32_t* __restrict__ inv) {
__shared__ int32_t s_id[kScatterTile];
const uint32_t tid = threadIdx.x, b = blockIdx.x;
const uint32_t i = b * kScatterTile + tid;
const int32_t l = (i < T) ? ids[i] - base : -1;
const int32_t e = (l >= 0 && l < (int32_t)n_expert) ? sort_place(l, n_expert, prot) : -1;
s_id[tid] = e;
const uint32_t live = (uint32_t)off[n_expert];
if (i >= live && i < T) sorted[i] = -1;
R3D_BARRIER();
if (INV && i < T && e < 0) inv[i] = -1;
if (i >= T || e < 0) return;
int32_t rank = 0;
for (uint32_t j = 0; j < tid; ++j) rank += (s_id[j] == e);
const int32_t at = off[e] + bcnt[(size_t)b * n_expert + e] + rank;
sorted[at] = (int32_t)i;
if (INV) inv[i] = at;
}
/* Both entry points: the small form up to kScatterSmall slots, the three passes above it. */
int scatter_body(const RadArgs* a, RadStream s, bool with_inv)
{
if (!a || a->n_t < (with_inv ? 5 : 4)) return R3D_NO(RAD_E_INVAL);
const RadTensor* ids = r3d_opt(a, 0), *sorted = r3d_opt(a, 1), *off = r3d_opt(a, 2),
*cnt = r3d_opt(a, 3), *inv = with_inv ? r3d_opt(a, 4) : nullptr;
if (!r3d_p(ids) || !r3d_wp(sorted) || !r3d_wp(off) || !r3d_wp(cnt) ||
(with_inv && !r3d_wp(inv)))
return R3D_NO(RAD_E_INVAL);
if (ids->dtype != RAD_I32 || sorted->dtype != RAD_I32 || off->dtype != RAD_I32 ||
cnt->dtype != RAD_I32 || (inv && inv->dtype != RAD_I32))
return R3D_NO(RAD_E_DTYPE);
if (!r3d_contig(ids) || !r3d_contig(sorted) || !r3d_contig(off) || !r3d_contig(cnt) ||
(inv && !r3d_contig(inv)))
return R3D_NO(RAD_E_STRIDE);
const long long n_expert = r3d_geti_or(a, "n_expert", 0);
if (n_expert <= 0 || n_expert > (long long)kMaxExpert) return R3D_NO(RAD_E_SHAPE);
const long long T = r3d_numel(ids);
if (T <= 0) return R3D_NO(RAD_E_SHAPE);
if (r3d_numel(sorted) < T || (inv && r3d_numel(inv) < T)) return R3D_NO(RAD_E_SHAPE);
if (r3d_numel(off) < n_expert + 1 || r3d_numel(cnt) < n_expert) return R3D_NO(RAD_E_SHAPE);
const long long base = r3d_geti_or(a, "expert_base", 0);
if (base < 0) return R3D_NO(RAD_E_SHAPE);
// protect: a comma list of local expert indices, strictly ascending, at most kMaxProtect.
Protect prot{};
if (const char* str = r3d_gets(a, "protect")) {
const char* p = str;
long long prev = -1;
while (*p) {
char* end = nullptr;
const long long v = std::strtoll(p, &end, 10);
if (end == p || v <= prev || v >= n_expert || prot.n >= kMaxProtect)
return R3D_NO(RAD_E_SHAPE);
prot.e[prot.n++] = (uint16_t)v;
prev = v;
p = end;
if (*p == ',') ++p;
else if (*p) return R3D_NO(RAD_E_SHAPE);
}
}
const int32_t* idp = (const int32_t*)r3d_p(ids);
int32_t* sp = (int32_t*)r3d_wp(sorted);
int32_t* op = (int32_t*)r3d_wp(off);
int32_t* cp = (int32_t*)r3d_wp(cnt);
int32_t* ip = inv ? (int32_t*)r3d_wp(inv) : nullptr;
if (T <= (long long)kScatterSmall) {
if (ip)
R3D_KLAUNCH(scatter_small_kernel<true>, dim3(1), dim3(kScatWaves * kSortWave),
(hipStream_t)s, idp, sp, op, cp, (uint32_t)T, (uint32_t)n_expert,
(int32_t)base, prot, ip);
else
R3D_KLAUNCH(scatter_small_kernel<false>, dim3(1), dim3(kScatWaves * kSortWave),
(hipStream_t)s, idp, sp, op, cp, (uint32_t)T, (uint32_t)n_expert,
(int32_t)base, prot, ip);
return (int)hipGetLastError();
}
const long long n_tile = (T + kScatterTile - 1) / kScatterTile;
const long long need = n_tile * n_expert * (long long)sizeof(int32_t);
if (!a->scratch || a->scratch_bytes < need) return R3D_NO(RAD_E_SCRATCH);
int32_t* bcnt = (int32_t*)a->scratch;
R3D_KLAUNCH(scatter_hist_kernel, dim3((unsigned)n_tile), dim3(kScatterTile), (hipStream_t)s,
idp, bcnt, (uint32_t)T, (uint32_t)n_expert, (int32_t)base, prot);
R3D_KLAUNCH(scatter_scan_kernel, dim3(1), dim3(kMaxExpert), (hipStream_t)s,
bcnt, op, cp, (uint32_t)n_tile, (uint32_t)n_expert);
if (ip)
R3D_KLAUNCH(scatter_place_kernel<true>, dim3((unsigned)n_tile), dim3(kScatterTile),
(hipStream_t)s, idp, bcnt, op, sp, (uint32_t)T, (uint32_t)n_expert,
(int32_t)base, prot, ip);
else
R3D_KLAUNCH(scatter_place_kernel<false>, dim3((unsigned)n_tile), dim3(kScatterTile),
(hipStream_t)s, idp, bcnt, op, sp, (uint32_t)T, (uint32_t)n_expert,
(int32_t)base, prot, ip);
return (int)hipGetLastError();
}
} // namespace
extern "C" {
int r3d_rad_moe_scatter(const RadArgs* a, RadStream s)
{
return scatter_body(a, s, false);
}
/* moe_scatter_inv: the same sort, plus the inverse permutation (each slot's row in sorted_tok). */
int r3d_rad_moe_scatter_inv(const RadArgs* a, RadStream s)
{
return scatter_body(a, s, true);
}
/* The three-pass form's per-tile counts, [n_tile, n_expert] i32; the small form needs none. */
int64_t r3d_rad_moe_scatter_scratch(const RadArgs* a)
{
if (!a) return 0;
const long long n_expert = r3d_geti_or(a, "n_expert", 0);
/* Called at declare with the band's bound, when the ids may carry no data yet: then
* M x top_k. */
const RadTensor* ids = r3d_opt(a, 0);
const long long T = ids ? r3d_numel(ids) : r3d_geti_or(a, "M", 0) * r3d_geti_or(a, "top_k", 1);
if (n_expert <= 0 || T <= (long long)kScatterSmall) return 0;
const long long n_tile = (T + kScatterTile - 1) / kScatterTile;
return n_tile * n_expert * (long long)sizeof(int32_t);
}
} // extern "C"