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
radiance-sycl/libavx/avx_support.cpp
T
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

388 lines
17 KiB
C++

/* avx_support.cpp -- dtype-generic row conversion and row reductions, compiled once per ISA level.
*
* Out of line rather than in the header: every kernel calls these with a runtime dtype, so inlining
* would paste the dtype switch into every call site. Out of line, the switch is taken once per row
* and lands in a straight vector loop.
*
* Conversion rates per level come from `rad-avx-bench --cvt` (libavx/README.md). The two fp8
* decodes differ (avx_vec.h explains): e4m3 is a branchless bit reconstruction, e5m2 a table
* gather; the scalar level and tail elements use the table for both.
*/
#include "avx_vec.h"
#include "avx_common.h"
namespace avx {
inline namespace AVX_SUFFIX {
/* ================================================================== packed weight codes
* The canonical planes a container stores (abi/rad_encoding.h), read as stored: libavx rearranges
* no plane, its relayouts only join whole planes (avx_planes.h). Definition: libref/ref_common.h.
* W4: two nibbles a byte, element 2i in the low nibble, code (n ^ 8) - 8. W2: four codes a byte,
* code (c ^ 2) - 2. MXFP4: two e2m1 nibbles, as avx_common.h: ld_elem decodes them.
*
* W4 unpacks 32 elements from one 128-bit load: AND for the low nibbles, shift for the high, then
* interleave with punpcklbw; MXFP4 takes the same unpack and a register table. W2 stays scalar: no
* plain W2 weight is served. */
static void w4_to_f32(const uint8_t* src, float* dst, int64_t n) {
int64_t i = 0;
#if AVX_LEVEL >= 2
const __m128i lomask = _mm_set1_epi8(0x0f);
for (; i + 32 <= n; i += 32) {
__m128i b = _mm_loadu_si128((const __m128i*)(src + (i >> 1)));
__m128i lo = _mm_and_si128(b, lomask);
__m128i hi = _mm_and_si128(_mm_srli_epi16(b, 4), lomask);
lo = _mm_sub_epi8(_mm_xor_si128(lo, _mm_set1_epi8(8)), _mm_set1_epi8(8));
hi = _mm_sub_epi8(_mm_xor_si128(hi, _mm_set1_epi8(8)), _mm_set1_epi8(8));
__m128i e0 = _mm_unpacklo_epi8(lo, hi); /* codes 0..15 */
__m128i e1 = _mm_unpackhi_epi8(lo, hi); /* codes 16..31 */
#if AVX_LEVEL >= 3
_mm512_storeu_ps(dst + i, _mm512_cvtepi32_ps(_mm512_cvtepi8_epi32(e0)));
_mm512_storeu_ps(dst + i + 16, _mm512_cvtepi32_ps(_mm512_cvtepi8_epi32(e1)));
#else
_mm256_storeu_ps(dst + i, _mm256_cvtepi32_ps(_mm256_cvtepi8_epi32(e0)));
_mm256_storeu_ps(dst + i + 8,
_mm256_cvtepi32_ps(_mm256_cvtepi8_epi32(_mm_srli_si128(e0, 8))));
_mm256_storeu_ps(dst + i + 16, _mm256_cvtepi32_ps(_mm256_cvtepi8_epi32(e1)));
_mm256_storeu_ps(dst + i + 24,
_mm256_cvtepi32_ps(_mm256_cvtepi8_epi32(_mm_srli_si128(e1, 8))));
#endif
}
#endif
for (; i < n; ++i) dst[i] = ld_elem(src, RAD_I4, i);
}
/* MXFP4: the same nibble unpack, then the e2m1 value of each code from a register table. AVX-512
* looks all sixteen up in one vpermps; AVX2's vpermps indexes eight, so it takes the magnitude by
* the low three bits and ors in the sign (bit 3). Code 8 is -0, as ld_elem gives it. */
static void fp4_to_f32(const uint8_t* src, float* dst, int64_t n) {
int64_t i = 0;
#if AVX_LEVEL >= 2
const __m128i lomask = _mm_set1_epi8(0x0f);
#if AVX_LEVEL >= 3
const __m512 tab = _mm512_setr_ps(0.0f, 0.5f, 1.0f, 1.5f, 2.0f, 3.0f, 4.0f, 6.0f, -0.0f,
-0.5f, -1.0f, -1.5f, -2.0f, -3.0f, -4.0f, -6.0f);
#else
const __m256 mag = _mm256_setr_ps(0.0f, 0.5f, 1.0f, 1.5f, 2.0f, 3.0f, 4.0f, 6.0f);
auto emit8 = [&](__m128i codes8, float* d) {
const __m256i ix = _mm256_cvtepu8_epi32(codes8);
const __m256 m = _mm256_permutevar8x32_ps(mag, ix);
const __m256i sg = _mm256_slli_epi32(_mm256_and_si256(ix, _mm256_set1_epi32(8)), 28);
_mm256_storeu_ps(d, _mm256_or_ps(m, _mm256_castsi256_ps(sg)));
};
#endif
for (; i + 32 <= n; i += 32) {
const __m128i b = _mm_loadu_si128((const __m128i*)(src + (i >> 1)));
const __m128i lo = _mm_and_si128(b, lomask);
const __m128i hi = _mm_and_si128(_mm_srli_epi16(b, 4), lomask);
const __m128i e0 = _mm_unpacklo_epi8(lo, hi); /* codes 0..15 */
const __m128i e1 = _mm_unpackhi_epi8(lo, hi); /* codes 16..31 */
#if AVX_LEVEL >= 3
_mm512_storeu_ps(dst + i, _mm512_permutexvar_ps(_mm512_cvtepu8_epi32(e0), tab));
_mm512_storeu_ps(dst + i + 16, _mm512_permutexvar_ps(_mm512_cvtepu8_epi32(e1), tab));
#else
emit8(e0, dst + i);
emit8(_mm_srli_si128(e0, 8), dst + i + 8);
emit8(e1, dst + i + 16);
emit8(_mm_srli_si128(e1, 8), dst + i + 24);
#endif
}
#endif
for (; i < n; ++i) dst[i] = ld_elem(src, RAD_FP4E2M1, i);
}
/* ================================================================== the generic row */
void row_to_f32(uint32_t dt, const void* src, float* dst, int64_t n) {
switch (dt) {
case RAD_F32: std::memcpy(dst, src, (size_t)n * 4); break;
case RAD_BF16: cvt_bf16_to_f32((const uint16_t*)src, dst, n); break;
case RAD_F16: cvt_f16_to_f32((const uint16_t*)src, dst, n); break;
case RAD_F8E4M3: cvt_fp8e4m3_to_f32((const uint8_t*)src, dst, n); break;
case RAD_F8E5M2: cvt_fp8e5m2_to_f32((const uint8_t*)src, dst, n); break;
case RAD_I8: cvt_i8_to_f32((const int8_t*)src, dst, n); break;
case RAD_I4: w4_to_f32((const uint8_t*)src, dst, n); break;
case RAD_I2: for (int64_t i = 0; i < n; ++i) dst[i] = ld_elem(src, RAD_I2, i); break;
case RAD_FP4E2M1: fp4_to_f32((const uint8_t*)src, dst, n); break;
default:
/* Remaining integer and bool dtypes are not hot; use the ABI accessor. */
for (int64_t i = 0; i < n; ++i) dst[i] = rad_load_f32(src, dt, i);
break;
}
}
void row_from_f32(uint32_t dt, const float* src, void* dst, int64_t n) {
switch (dt) {
case RAD_F32: std::memcpy(dst, src, (size_t)n * 4); break;
case RAD_BF16: cvt_f32_to_bf16(src, (uint16_t*)dst, n); break;
case RAD_F16: cvt_f32_to_f16(src, (uint16_t*)dst, n); break;
case RAD_F8E4M3: cvt_f32_to_fp8e4m3(src, (uint8_t*)dst, n); break;
case RAD_F8E5M2: cvt_f32_to_fp8e5m2(src, (uint8_t*)dst, n); break;
case RAD_I8: cvt_f32_to_i8(src, (int8_t*)dst, n); break;
default:
/* Every other integer width rounds and saturates as i8 does (avx_common.h: st_elem). */
for (int64_t i = 0; i < n; ++i) st_elem(dst, dt, i, src[i]);
break;
}
}
void row_to_f32_strided(uint32_t dt, const void* src, int64_t stride, float* dst, int64_t n) {
if (stride == 1) { row_to_f32(dt, src, dst, n); return; }
/* Scalar gather. vpgatherdd issues one cache line per lane, while scalar loads coalesce in
* the load queue; and strided operands here are only column slices of a fused projection
* (docs/OPS.md on `z`), a few rows per launch. */
for (int64_t i = 0; i < n; ++i) dst[i] = ld_elem(src, dt, i * stride);
}
/* ================================================================== row reductions
* Fixed summation trees. The accumulators are written out rather than left to the unroller so the
* summation order is fixed by the source, not the optimiser, and results do not move by an ulp
* between builds. Four accumulators cover the FMA latency on current cores (`rad-avx-bench
* --reduce`). */
float row_sumsq(const float* x, int64_t n) {
vf a0 = vf_zero(), a1 = vf_zero(), a2 = vf_zero(), a3 = vf_zero();
int64_t i = 0;
for (; i + 4 * VF_N <= n; i += 4 * VF_N) {
vf v0 = vf_loadu(x + i), v1 = vf_loadu(x + i + VF_N);
vf v2 = vf_loadu(x + i + 2 * VF_N), v3 = vf_loadu(x + i + 3 * VF_N);
a0 = vf_fma(v0, v0, a0);
a1 = vf_fma(v1, v1, a1);
a2 = vf_fma(v2, v2, a2);
a3 = vf_fma(v3, v3, a3);
}
for (; i + VF_N <= n; i += VF_N) { vf v = vf_loadu(x + i); a0 = vf_fma(v, v, a0); }
float s = vf_hsum(vf_add(vf_add(a0, a1), vf_add(a2, a3)));
for (; i < n; ++i) s += x[i] * x[i];
return s;
}
float row_sum(const float* x, int64_t n) {
vf a0 = vf_zero(), a1 = vf_zero(), a2 = vf_zero(), a3 = vf_zero();
int64_t i = 0;
for (; i + 4 * VF_N <= n; i += 4 * VF_N) {
a0 = vf_add(a0, vf_loadu(x + i));
a1 = vf_add(a1, vf_loadu(x + i + VF_N));
a2 = vf_add(a2, vf_loadu(x + i + 2 * VF_N));
a3 = vf_add(a3, vf_loadu(x + i + 3 * VF_N));
}
for (; i + VF_N <= n; i += VF_N) a0 = vf_add(a0, vf_loadu(x + i));
float s = vf_hsum(vf_add(vf_add(a0, a1), vf_add(a2, a3)));
for (; i < n; ++i) s += x[i];
return s;
}
/* A NaN element is skipped, as libref's std::max(amax, |x|) skips it: vmaxps returns its second
* operand when either is NaN, so the running maximum goes second. */
float row_amax(const float* x, int64_t n) {
vf m0 = vf_zero(), m1 = vf_zero();
int64_t i = 0;
for (; i + 2 * VF_N <= n; i += 2 * VF_N) {
m0 = vf_max(vf_abs(vf_loadu(x + i)), m0);
m1 = vf_max(vf_abs(vf_loadu(x + i + VF_N)), m1);
}
for (; i + VF_N <= n; i += VF_N) m0 = vf_max(vf_abs(vf_loadu(x + i)), m0);
float m = vf_hmax(vf_max(m0, m1));
for (; i < n; ++i) { float av = x[i] < 0 ? -x[i] : x[i]; if (av > m) m = av; }
return m;
}
float row_max(const float* x, int64_t n) {
if (n <= 0) return -3.402823466e+38f;
vf m0 = vf_set1(x[0]), m1 = vf_set1(x[0]);
int64_t i = 0;
for (; i + 2 * VF_N <= n; i += 2 * VF_N) {
m0 = vf_max(m0, vf_loadu(x + i));
m1 = vf_max(m1, vf_loadu(x + i + VF_N));
}
for (; i + VF_N <= n; i += VF_N) m0 = vf_max(m0, vf_loadu(x + i));
float m = vf_hmax(vf_max(m0, m1));
for (; i < n; ++i) if (x[i] > m) m = x[i];
return m;
}
/* ================================================================== an operand's row, as f32 */
/* Whether a dense row starting at element `base` can be addressed by a byte pointer. A sub-byte
* dtype starting mid-byte cannot (`byte_at` maps elements 2i and 2i+1 to the same byte), so it
* takes the element path rather than reading the row shifted by a nibble. Real W4 rows start on a
* group boundary. */
static inline bool byte_aligned(const RadTensor* t, int64_t base) {
const int sub = (t->dtype == RAD_I4 || t->dtype == RAD_FP4E2M1) ? 1
: (t->dtype == RAD_I2) ? 3 : 0;
return (base & sub) == 0;
}
const float* in_row(const RadTensor* t, int64_t base, int64_t n, float* scr) {
const int64_t st = laststride(t);
if (st == 1 && byte_aligned(t, base))
return row_f32_in(t->dtype, byte_at(t->data, t->dtype, base), scr, n);
for (int64_t i = 0; i < n; ++i) scr[i] = ldt(t, base + i * st);
return scr;
}
void out_row(const RadTensor* t, int64_t base, int64_t n, const float* src) {
const int64_t st = laststride(t);
if (st == 1 && byte_aligned(t, base)) {
row_from_f32(t->dtype, src, (void*)byte_at(t->data, t->dtype, base), n);
return;
}
for (int64_t i = 0; i < n; ++i) stt(t, base + i * st, src[i]);
}
/* ================================================================== exact top-R
*
* One step of an exact selection: the next element in descending value order, ties to the lower
* index (libref's `topk_next`).
*
* Eligibility ("not above the last pick, and not the last pick or an equal element before it")
* splits at the previous pick into two value-only tests, which vectorise as compare + blend + max:
*
* i <= pi eligible iff !(v > pv) and v != pv
* i > pi eligible iff !(v > pv)
*
* A second pass finds the index of the maximum and exits at the first match.
*
* `!(v > pv)` (the unordered NGT compare) rather than `v <= pv` matches libref, which treats a NaN
* as eligible. One known difference: libref keeps an eligible NaN that it meets first as the
* winner, while the vector max returns the largest non-NaN. No path produces NaN logits (a masked
* logit is -inf, which is handled). */
bool topk_next_f32(const float* v, int64_t n, float* pv, int64_t* pi) {
const float prev = *pv;
const int64_t pidx = *pi;
#if AVX_LEVEL == 0
/* Scalar level: libref's single pass. The two-pass form below only pays when the first pass
* is vector work; scalar, it is slower than one pass. */
float best = 0.0f;
int64_t bi = -1;
for (int64_t j = 0; j < n; ++j) {
const float x = v[j];
if (x > prev) continue;
if (x == prev && j <= pidx) continue;
if (bi < 0 || x > best) { best = x; bi = j; }
}
if (bi < 0) return false;
*pv = best;
*pi = bi;
return true;
#else
const float NEG = -INFINITY;
/* Indices below `split` (at or below `pidx`) must also differ from `prev`. */
const int64_t split = (pidx + 1 > 0) ? (pidx + 1 < n ? pidx + 1 : n) : 0;
float best = NEG;
int64_t i = 0;
{
const vf vprev = vf_set1(prev), vneg = vf_set1(NEG);
vf acc = vf_set1(NEG);
/* At or below the previous pick: equality excluded. */
for (; i + VF_N <= split; i += VF_N) {
const vf x = vf_loadu(v + i);
#if AVX_LEVEL == 3
const __mmask16 ok = _kand_mask16(_mm512_cmp_ps_mask(x, vprev, _CMP_NGT_UQ),
_mm512_cmp_ps_mask(x, vprev, _CMP_NEQ_UQ));
acc = vf_max(acc, _mm512_mask_blend_ps(ok, vneg, x));
#else
const __m256 ok = _mm256_and_ps(_mm256_cmp_ps(x, vprev, _CMP_NGT_UQ),
_mm256_cmp_ps(x, vprev, _CMP_NEQ_UQ));
acc = vf_max(acc, _mm256_blendv_ps(vneg, x, ok));
#endif
}
for (; i < split; ++i) {
const float x = v[i];
if (x > prev || x == prev) continue;
if (x > best) best = x;
}
/* Past the previous pick: equality allowed. */
for (; i + VF_N <= n; i += VF_N) {
const vf x = vf_loadu(v + i);
#if AVX_LEVEL == 3
const __mmask16 ok = _mm512_cmp_ps_mask(x, vprev, _CMP_NGT_UQ);
acc = vf_max(acc, _mm512_mask_blend_ps(ok, vneg, x));
#else
const __m256 ok = _mm256_cmp_ps(x, vprev, _CMP_NGT_UQ);
acc = vf_max(acc, _mm256_blendv_ps(vneg, x, ok));
#endif
}
const float vm = vf_hmax(acc);
if (vm > best) best = vm;
}
for (; i < n; ++i) {
const float x = v[i];
if (x > prev) continue;
if (x == prev && i <= pidx) continue;
if (x > best) best = x;
}
/* Lowest index holding the maximum (ties to the lower column). Vectorised as well: a scalar
* scan reads n/2 elements on average and would dominate `sample_argmax`.
*
* The range below the previous pick must be scanned first, since the lowest index over the
* whole row wins. Scanning the vector range first returns the wrong index when the maximum
* occurs on both sides, which is common at bf16.
*
* That low range needs the extra `x != prev` test and is at most pidx+1 long (0 for an argmax,
* R-1 for the R-th pick), so it stays scalar. Past it, `x == best` implies eligibility, since
* best <= prev. */
for (int64_t j = 0; j < split; ++j) {
const float x = v[j];
if (x != best || x == prev) continue;
*pv = best;
*pi = j;
return true;
}
{
int64_t j = split;
const vf vbest = vf_set1(best);
for (; j + VF_N <= n; j += VF_N) {
const vf x = vf_loadu(v + j);
#if AVX_LEVEL == 3
const unsigned msk = (unsigned)_mm512_cmpeq_ps_mask(x, vbest);
#else
const unsigned msk =
(unsigned)_mm256_movemask_ps(_mm256_cmp_ps(x, vbest, _CMP_EQ_OQ));
#endif
if (msk) { *pv = best; *pi = j + __builtin_ctz(msk); return true; }
}
for (; j < n; ++j)
if (v[j] == best) { *pv = best; *pi = j; return true; }
}
return false;
#endif
}
} /* inline namespace */
} /* namespace avx */
/* ================================================================== the machine's FMA ceiling
*
* Measured here, with the kernels' per-level flags, because rad-avx-bench is baseline-compiled
* and could only measure a scalar chain far below what a GEMM at this level faces.
*
* Independent FMA accumulators in registers, no memory traffic. The result is what the core
* retires at the clock it holds under sustained vector load (below advertised boost on parts with
* an AVX-512 frequency offset).
*
* The accumulator count must fit the register file; a spill inside the loop understates the
* ceiling. 16 accumulators plus 2 constants fit AVX-512's 32 registers but spill on AVX/AVX2's 16.
* 10 covers a 4-5 cycle FMA latency on 2 FMA ports and leaves six registers free.
*
* The `volatile` sink keeps the compiler from deleting the loop. */
extern "C" double AVX_FN(avx_peak_fma)(int64_t iters) {
using namespace avx;
#if AVX_LEVEL == 1 || AVX_LEVEL == 2
enum { NACC = 10 }; /* 16 architectural YMM registers */
#else
enum { NACC = 16 }; /* 32 ZMM at AVX-512; scalar has no such limit */
#endif
vf acc[NACC];
for (int i = 0; i < NACC; ++i) acc[i] = vf_set1((float)(i + 1) * 1e-6f);
const vf m = vf_set1(1.0000001f), b = vf_set1(1e-7f);
for (int64_t it = 0; it < iters; ++it)
for (int i = 0; i < NACC; ++i) acc[i] = vf_fma(acc[i], m, b);
float sink = 0;
for (int i = 0; i < NACC; ++i) sink += vf_hsum(acc[i]);
volatile float keep = sink;
(void)keep;
/* 2 flops per FMA, NACC FMAs per iteration, VF_N lanes each. */
return 2.0 * (double)iters * (double)NACC * (double)VF_N;
}