libr4d: r4d_selftest folds the exact all-reduces' oracle from inputs whose sum depends on the order -- the old pattern passed any order

The eight-rank two-shot claims the same bits whatever the shard weights and piece count: a word is
reduced by one thread of its owner, acc = 0.0f, then src[0..7] added in ascending rank order in
fp32 and rounded once (reduce2 in r4d_ar_twoshot_Nrank_exact.hip), the one-shot's ar_vsum fold;
weights move only which rank owns a word, pieces only which step reduces it, and every form
(lockstep, ring, split) keeps both. The selftest's check of that claim was blind, though:
ar_pattern_value is k / 64 with |k| <= 254, and eight such values sum exactly in fp32 in any
order, so a kernel folding in another order, or twice-rounding a partial sum, passed.

ar_rough_value gives bf16 values (8 significant bits) whose exponents span 2^-12 to 2^12, so
partial sums round and the order shows: of 20000 elements, 547 fp32 sums differ between
ascending order and rank 3 first. The exact wide rows' pattern runs (ar_wide_pattern, the
weighted eight-rank runs over weights, pieces and forms) and --perfar8 now fold their oracle
from it, ascending from 0.0f, and must match it bit for bit; the lossy and fused cases keep
ar_pattern_value, whose bounds assume its range.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
NunzeandClaude Fable 5.1 authored and StillDeadcode committed 2026-10-22 11:54:56 +02:00
1 parent c25bca83d5
commit 7aa51e5b4a
1 file changed
+17 -4
+17 -4
View File
@@ -21986,6 +21986,19 @@ static float ar_pattern_value(int rank, int64_t i) {
return (float)((int)((i * 37 + (int64_t)rank * 101) % 509) - 254) / 64.0f;
}
/* The exact all-reduces' input. ar_pattern_value's sums are exact in fp32 in any order (eight
* values of 8 significant bits within 2^8 of each other), so a fold in another order would pass
* against them. These are bf16 values (8 significant bits) whose exponents span 2^-12 to 2^12,
* so an fp32 partial sum rounds and only the order the oracle uses, ascending rank from 0.0f,
* gives its bits: whoever owns a word, in however many pieces, must fold it that way. */
static float ar_rough_value(int rank, int64_t i) {
uint64_t h = (uint64_t)i * 0x9E3779B97F4A7C15ull ^ ((uint64_t)rank + 1) * 0xC2B2AE3D27D4EB4Full;
h ^= h >> 29; h *= 0xBF58476D1CE4E5B9ull; h ^= h >> 32;
const float m = (float)(128 + (int)(h & 127)) / 128.0f;
const int e = (int)((h >> 7) % 25) - 12;
return ((h >> 12) & 1 ? -1.0f : 1.0f) * std::ldexp(m, e);
}
/* The pattern for the two-shot's coded form (r4d_ar_twoshot_Nrank_exact.hip), which codes a group
* of 64 consecutive values on a shared exponent and sends a group it cannot code raw: the
* per-element pattern, with groups sent down each path -- an exponent range of exactly 14 (coded)
@@ -22007,7 +22020,7 @@ static float ar_coded_value(int rank, int64_t i) {
}
}
static float ar_value(int coded_pattern, int rank, int64_t i) {
return coded_pattern ? ar_coded_value(rank, i) : ar_pattern_value(rank, i);
return coded_pattern ? ar_coded_value(rank, i) : ar_rough_value(rank, i);
}
static void ar_pattern_worker(ArPattern* t) {
@@ -22102,7 +22115,7 @@ static bool ar_pattern_check(const RadKernelInfo* row, const std::vector<int>& d
for (int64_t i = 0; i < n; ++i) {
float acc = 0.0f;
for (int r = 0; r < world; ++r) {
const float v = ar_pattern_value(r, i);
const float v = ar_rough_value(r, i);
acc += bf ? h_bf16(h_to_bf16(v)) : v;
}
const float want = bf ? h_bf16(h_to_bf16(acc)) : acc;
@@ -22440,10 +22453,10 @@ static void ar8_perf_worker(Ar8Perf* t) {
for (int64_t i = 0; i < n; ++i) {
float acc = 0.0f;
for (int r = 0; r < t->world; ++r) {
const float v = ar_pattern_value(r, i);
const float v = ar_rough_value(r, i);
acc += bf ? h_bf16(h_to_bf16(v)) : v;
}
const float v = ar_pattern_value(t->rank, i);
const float v = ar_rough_value(t->rank, i);
if (bf) {
const uint16_t b = h_to_bf16(v), w = h_to_bf16(acc);
std::memcpy(&h[(size_t)i * 2], &b, 2); std::memcpy(&want[(size_t)i * 2], &w, 2);