diff --git a/libr4d/r4d_ar_entry.hip b/libr4d/r4d_ar_entry.hip index 17458f71..6faed029 100644 --- a/libr4d/r4d_ar_entry.hip +++ b/libr4d/r4d_ar_entry.hip @@ -343,10 +343,10 @@ int build(ArInst* in, int rank, int world, long long max_numel, int dtype_code, if (hipGetDevice(&dev) != hipSuccess) return RAD_E_DEVICE; if (host) { sc = r4d_ar_host_alloc((long)nslots * in->slot_bytes); - fl = r4d_ar_host_alloc(flag_words * 4); + fl = r4d_ar_host_alloc((flag_words + 2L * maxb) * 4); } else { sc = r4d_ar_ipc_alloc((long)nslots * in->slot_bytes, 1, handle, &hlen); - fl = r4d_ar_ipc_alloc(flag_words * 4, 1, handle, &hlen); + fl = r4d_ar_ipc_alloc((flag_words + 2L * maxb) * 4, 1, handle, &hlen); } sq = r4d_ar_ipc_alloc(kSeqWords(maxb) * 4, 0, handle, &hlen)); diff --git a/libr4d/r4d_ar_gather_2rank.hip b/libr4d/r4d_ar_gather_2rank.hip index fdbacfd3..d6fb113f 100644 --- a/libr4d/r4d_ar_gather_2rank.hip +++ b/libr4d/r4d_ar_gather_2rank.hip @@ -72,6 +72,7 @@ __global__ void r4d_ar_gather_2rank_kernel( const int w = (n16 + nb - 1) / nb; const int start = b * w; int end = start + w; if (end > n16) end = n16; + unsigned int hck = 0u; // the push check (r4d_ar_seq.h) // Push the chunk to the peer slot and place this rank's own slice (a local store, no handshake) // while the push is in flight. @@ -82,6 +83,8 @@ __global__ void r4d_ar_gather_2rank_kernel( v2 = in4[r4d_g_src(k + 2 * nt, row16, n16, sp16)], v3 = in4[r4d_g_src(k + 3 * nt, row16, n16, sp16)]; ps[k] = v0; ps[k + nt] = v1; ps[k + 2 * nt] = v2; ps[k + 3 * nt] = v3; + hck += r4d_archk_u4(v0, 4u * (unsigned)k) + r4d_archk_u4(v1, 4u * (unsigned)(k + nt)) + + r4d_archk_u4(v2, 4u * (unsigned)(k + 2 * nt)) + r4d_archk_u4(v3, 4u * (unsigned)(k + 3 * nt)); out4[r4d_g_dst(k, row16, n16, rank, dp16)] = v0; out4[r4d_g_dst(k + nt, row16, n16, rank, dp16)] = v1; out4[r4d_g_dst(k + 2 * nt, row16, n16, rank, dp16)] = v2; @@ -90,11 +93,13 @@ __global__ void r4d_ar_gather_2rank_kernel( for (; k < end; k += nt) { int4 v = in4[r4d_g_src(k, row16, n16, sp16)]; ps[k] = v; + hck += r4d_archk_u4(v, 4u * (unsigned)k); out4[r4d_g_dst(k, row16, n16, rank, dp16)] = v; } } // Drain modes, as in the all-reduce: order the pushes before the release flag so the peer cannot // see the flag before the data. + r4d_archk_put(hck, peer_flags, nbw, (unsigned)slot, (unsigned)b, s); if (drain == 1) __threadfence_system(); else if (drain == 3) asm volatile("s_wait_storecnt 0x0" ::: "memory"); __syncthreads(); @@ -110,6 +115,9 @@ __global__ void r4d_ar_gather_2rank_kernel( } __syncthreads(); if (acq) __threadfence_system(); + r4d_archk_wait(my_flags, nbw, (unsigned)slot, (unsigned)b, s, [&](unsigned int& h) { + for (int k = start + tid; k < end; k += nt) h += r4d_archk_u4(ms[k], 4u * (unsigned)k); + }); // Place the peer's slice, now staged in this rank's scratch. { @@ -165,6 +173,7 @@ void r4d_ar_gather_2rank(long peer_scratch, long my_scratch, long peer_flags, lo int nt = (int)nthreads; if (nt < 64) nt = 64; if (nt > 1024) nt = 1024; + nt &= ~31; // whole waves: the push check's wave sum reads all 32 lanes r4d_ar_gather_2rank_kernel<<>>( (const int4*)inp, (int4*)peer_scratch, (const int4*)my_scratch, (int4*)out, (unsigned*)peer_flags, (unsigned*)my_flags, (unsigned*)seq_ctrs, n16, diff --git a/libr4d/r4d_ar_ln_had_quant_i8.hip b/libr4d/r4d_ar_ln_had_quant_i8.hip index 98d548ba..ccf5c776 100644 --- a/libr4d/r4d_ar_ln_had_quant_i8.hip +++ b/libr4d/r4d_ar_ln_had_quant_i8.hip @@ -163,6 +163,7 @@ __global__ __launch_bounds__(THR) void r4d_ar_ln_had_quant_i8_kernel( float* Lsc = (float*)(Lpk + (size_t)cpb * ARN_WV * ARN_NWORD); const int lig = lane & (ARN_LPG - 1), gw = lane / ARN_LPG; + unsigned int hck = 0u; // the push check (r4d_ar_seq.h) if constexpr (WIRE == 0) { // Push my rows to the peer slot. A row is K/8 16B words and rows are contiguous, so a row // range is a word range. @@ -173,8 +174,10 @@ __global__ __launch_bounds__(THR) void r4d_ar_ln_had_quant_i8_kernel( for (; k + 3 * THR < wend; k += 4 * THR) { int4 v0 = in4[k], v1 = in4[k + THR], v2 = in4[k + 2 * THR], v3 = in4[k + 3 * THR]; ps[k] = v0; ps[k + THR] = v1; ps[k + 2 * THR] = v2; ps[k + 3 * THR] = v3; + hck += r4d_archk_u4(v0, 4u * (unsigned)k) + r4d_archk_u4(v1, 4u * (unsigned)(k + THR)) + + r4d_archk_u4(v2, 4u * (unsigned)(k + 2 * THR)) + r4d_archk_u4(v3, 4u * (unsigned)(k + 3 * THR)); } - for (; k < wend; k += THR) ps[k] = in4[k]; + for (; k < wend; k += THR) { const int4 v = in4[k]; ps[k] = v; hck += r4d_archk_u4(v, 4u * (unsigned)k); } } else { // Rotate, amax, quantise and pack straight into the peer's slot, keeping the same words in // LDS. A wave owns whole chunks, so every lane emits exactly ARN_NWORD dwords per chunk and the @@ -201,8 +204,10 @@ __global__ __launch_bounds__(THR) void r4d_ar_ln_had_quant_i8_kernel( float scl = (a > 0.f) ? arn_bf16_round(a * (1.0f / (float)ARN_QMAX)) : 1.0f; if (!(scl > 0.f)) scl = 1.0f; if (lig == 0) { - p_sc[c * ARN_CG + gl] = (unsigned short)(__float_as_uint(scl) >> 16); + const unsigned short sc16 = (unsigned short)(__float_as_uint(scl) >> 16); + p_sc[c * ARN_CG + gl] = sc16; Lsc[(size_t)ci * ARN_CG + gl] = scl; + hck += r4d_archk_mix(sc16, R4D_ARCHK_U16 + (unsigned)(c * ARN_CG + gl)); } const float inv = 1.0f / scl; #pragma unroll @@ -217,20 +222,30 @@ __global__ __launch_bounds__(THR) void r4d_ar_ln_had_quant_i8_kernel( // Flush lane-contiguously region by region, widest first, so each region is one coalesced // store of ARN_WV x width bytes (ARN_NWORD is 3). char* pb = p_pay + (size_t)c * ARN_CBYTES; + const unsigned int cd = (unsigned)c * (ARN_CBYTES / 4); // the chunk's first dword #pragma unroll - for (int w4 = 0; w4 + 4 <= ARN_NWORD; w4 += 4) + for (int w4 = 0; w4 + 4 <= ARN_NWORD; w4 += 4) { *(uint4*)(pb + w4 * 4 * ARN_WV + lane * 16) = make_uint4(acc[w4], acc[w4 + 1], acc[w4 + 2], acc[w4 + 3]); - if constexpr (ARN_NWORD % 4 >= 2) + hck += r4d_archk_u4(make_uint4(acc[w4], acc[w4 + 1], acc[w4 + 2], acc[w4 + 3]), + cd + (unsigned)(w4 * ARN_WV + lane * 4)); + } + if constexpr (ARN_NWORD % 4 >= 2) { *(uint2*)(pb + (ARN_NWORD & ~3) * 4 * ARN_WV + lane * 8) = make_uint2(acc[ARN_NWORD & ~3], acc[(ARN_NWORD & ~3) + 1]); - if constexpr (ARN_NWORD % 2 == 1) + const unsigned int e = cd + (unsigned)((ARN_NWORD & ~3) * ARN_WV + lane * 2); + hck += r4d_archk_mix(acc[ARN_NWORD & ~3], e) + r4d_archk_mix(acc[(ARN_NWORD & ~3) + 1], e + 1u); + } + if constexpr (ARN_NWORD % 2 == 1) { *(unsigned int*)(pb + (ARN_NWORD - 1) * 4 * ARN_WV + lane * 4) = acc[ARN_NWORD - 1]; + hck += r4d_archk_mix(acc[ARN_NWORD - 1], cd + (unsigned)((ARN_NWORD - 1) * ARN_WV + lane)); + } #pragma unroll for (int w = 0; w < ARN_NWORD; ++w) Lpk[((size_t)ci * ARN_NWORD + w) * ARN_WV + lane] = acc[w]; } } + r4d_archk_put(hck, peer_flags, nbw, s_slot, (unsigned)b, sq); if (drain == 1) __threadfence_system(); else if (drain == 3) asm volatile("s_wait_storecnt 0x0" ::: "memory"); __syncthreads(); @@ -245,6 +260,35 @@ __global__ __launch_bounds__(THR) void r4d_ar_ln_had_quant_i8_kernel( } __syncthreads(); if (acq) __threadfence_system(); + r4d_archk_wait(my_flags, nbw, s_slot, (unsigned)b, sq, [&](unsigned int& h) { + if constexpr (WIRE == 0) { + const int wpr = K >> 3; + for (int k = r0 * wpr + tid; k < r1 * wpr; k += THR) h += r4d_archk_u4(ms[k], 4u * (unsigned)k); + } else { + for (int ci = wave; ci < cpb; ci += NW) { + const int c = c00 + ci; + const char* pb = m_pay + (size_t)c * ARN_CBYTES; + const unsigned int cd = (unsigned)c * (ARN_CBYTES / 4); +#pragma unroll + for (int w4 = 0; w4 + 4 <= ARN_NWORD; w4 += 4) + h += r4d_archk_u4(*(const uint4*)(pb + w4 * 4 * ARN_WV + lane * 16), + cd + (unsigned)(w4 * ARN_WV + lane * 4)); + if constexpr (ARN_NWORD % 4 >= 2) { + const uint2 u = *(const uint2*)(pb + (ARN_NWORD & ~3) * 4 * ARN_WV + lane * 8); + const unsigned int e = cd + (unsigned)((ARN_NWORD & ~3) * ARN_WV + lane * 2); + h += r4d_archk_mix(u.x, e) + r4d_archk_mix(u.y, e + 1u); + } + if constexpr (ARN_NWORD % 2 == 1) + h += r4d_archk_mix(*(const unsigned int*)(pb + (ARN_NWORD - 1) * 4 * ARN_WV + lane * 4), + cd + (unsigned)((ARN_NWORD - 1) * ARN_WV + lane)); +#pragma unroll + for (int t = 0; t < ARN_ITERS; ++t) { + const int gl = t * ARN_GPW + gw; + if (lig == 0) h += r4d_archk_mix(m_sc[c * ARN_CG + gl], R4D_ARCHK_U16 + (unsigned)(c * ARN_CG + gl)); + } + } + } + }); // The peer's rows are now in my scratch, in the same [M, K] layout as X. const __hip_bfloat16* P = (const __hip_bfloat16*)ms; @@ -404,9 +448,10 @@ static size_t ar_ln_lds_bytes(int rows_max, int K, int thr, int wire) { } return b; } -// Dynamic LDS ceiling: a workgroup's 64 KiB less the kernel's static words (seq and slot), which -// the runtime places in front of it. -static constexpr size_t kArnLdsCap = 65536 - 16; +// Dynamic LDS ceiling: a workgroup's 64 KiB less the kernel's static words, which the runtime places +// in front of it: seq and slot, and the push check's (r4d_ar_seq.h: two 32-word sums and a flag, +// 260 bytes), rounded up. +static constexpr size_t kArnLdsCap = 65536 - 16 - 512; // Fewest blocks at which this shape fits: each block owns ceil(M / nb) rows, and on the compressed // wire each row costs a staging slice and its chunks' packed half. -1 when no block count fits (a diff --git a/libr4d/r4d_ar_oneshot_2rank_exact.hip b/libr4d/r4d_ar_oneshot_2rank_exact.hip index 37bbd373..f9e8893b 100644 --- a/libr4d/r4d_ar_oneshot_2rank_exact.hip +++ b/libr4d/r4d_ar_oneshot_2rank_exact.hip @@ -76,6 +76,7 @@ __global__ void r4d_ar_oneshot_2rank_exact_kernel( const int w = (n16 + nb - 1) / nb; // 16B words per block const int start = b * w; int end = start + w; if (end > n16) end = n16; + unsigned int hck = 0u; // the push check (r4d_ar_seq.h) // Push the chunk to the peer slot, 4x unrolled for memory-level parallelism. { @@ -83,8 +84,10 @@ __global__ void r4d_ar_oneshot_2rank_exact_kernel( for (; k + 3 * nt < end; k += 4 * nt) { int4 v0 = in4[k], v1 = in4[k + nt], v2 = in4[k + 2 * nt], v3 = in4[k + 3 * nt]; ps[k] = v0; ps[k + nt] = v1; ps[k + 2 * nt] = v2; ps[k + 3 * nt] = v3; + hck += r4d_archk_u4(v0, 4u * (unsigned)k) + r4d_archk_u4(v1, 4u * (unsigned)(k + nt)) + + r4d_archk_u4(v2, 4u * (unsigned)(k + 2 * nt)) + r4d_archk_u4(v3, 4u * (unsigned)(k + 3 * nt)); } - for (; k < end; k += nt) ps[k] = in4[k]; + for (; k < end; k += nt) { const int4 v = in4[k]; ps[k] = v; hck += r4d_archk_u4(v, 4u * (unsigned)k); } } // Drain modes, ordering the pushes before the release flag so the peer sees data first: // 0 = none (relies on PCIe posted-write ordering and the release flag) @@ -94,6 +97,7 @@ __global__ void r4d_ar_oneshot_2rank_exact_kernel( // 3 = all-thread s_wait_storecnt 0: every wave drains its posted writes with no L2 // writeback-invalidate; PCIe keeps posted writes to one peer ordered, so the release flag // lands after the data. Correct only for fine-grained memory (coarse leaves data in L2). + r4d_archk_put(hck, peer_flags, nbw, (unsigned)slot, (unsigned)b, s); if (drain == 1) __threadfence_system(); else if (drain == 3) asm volatile("s_wait_storecnt 0x0" ::: "memory"); __syncthreads(); @@ -108,6 +112,9 @@ __global__ void r4d_ar_oneshot_2rank_exact_kernel( } __syncthreads(); if (acq) __threadfence_system(); + r4d_archk_wait(my_flags, nbw, (unsigned)slot, (unsigned)b, s, [&](unsigned int& h) { + for (int k = start + tid; k < end; k += nt) h += r4d_archk_u4(ms[k], 4u * (unsigned)k); + }); // Reduce the chunk, 4x unrolled: out = local input + peer input (staged in local scratch). { int k = start + tid; @@ -147,15 +154,19 @@ __global__ void r4d_ar_hc_write_kernel( const int w = (n16 + nb - 1) / nb; // 16B words per block const int start = b * w; int end = start + w; if (end > n16) end = n16; + unsigned int hck = 0u; // the push check (r4d_ar_seq.h) { int k = start + tid; for (; k + 3 * nt < end; k += 4 * nt) { int4 v0 = y4[k], v1 = y4[k + nt], v2 = y4[k + 2 * nt], v3 = y4[k + 3 * nt]; ps[k] = v0; ps[k + nt] = v1; ps[k + 2 * nt] = v2; ps[k + 3 * nt] = v3; + hck += r4d_archk_u4(v0, 4u * (unsigned)k) + r4d_archk_u4(v1, 4u * (unsigned)(k + nt)) + + r4d_archk_u4(v2, 4u * (unsigned)(k + 2 * nt)) + r4d_archk_u4(v3, 4u * (unsigned)(k + 3 * nt)); } - for (; k < end; k += nt) ps[k] = y4[k]; + for (; k < end; k += nt) { const int4 v = y4[k]; ps[k] = v; hck += r4d_archk_u4(v, 4u * (unsigned)k); } } + r4d_archk_put(hck, peer_flags, nbw, (unsigned)slot, (unsigned)b, s); if (drain == 1) __threadfence_system(); else if (drain == 3) asm volatile("s_wait_storecnt 0x0" ::: "memory"); __syncthreads(); @@ -168,8 +179,11 @@ __global__ void r4d_ar_hc_write_kernel( } } __syncthreads(); - r4d_ar_link_landed(gate, gate_word, gate_ctr, (unsigned int)nb, (unsigned int)tid); if (acq) __threadfence_system(); + r4d_archk_wait(my_flags, nbw, (unsigned)slot, (unsigned)b, s, [&](unsigned int& h) { + for (int k = start + tid; k < end; k += nt) h += r4d_archk_u4(ms[k], 4u * (unsigned)k); + }); + r4d_ar_link_landed(gate, gate_word, gate_ctr, (unsigned int)nb, (unsigned int)tid); const int wpr = n >> 3; // 16B words a row for (int k = start + tid; k < end; k += nt) { @@ -231,6 +245,7 @@ __global__ void r4d_ar_gather_hc_write_kernel( const int w = (n16 + nb - 1) / nb; // 16B words per block const int start = b * w; int end = start + w; if (end > n16) end = n16; + unsigned int hck = 0u; // the push check (r4d_ar_seq.h) const int wpr = n >> 3; // 16B words a row const int row_lo = start / wpr; const int nrow = end > start ? (end - 1) / wpr - row_lo + 1 : 0; @@ -264,7 +279,9 @@ __global__ void r4d_ar_gather_hc_write_kernel( const int4 out = r4d_ar_gather_word(G, inv, s_i, s_w, s_g, row, row - row_lo, i0); y4[k] = out; ps[k] = out; + hck += r4d_archk_u4(out, 4u * (unsigned)k); } + r4d_archk_put(hck, peer_flags, nbw, (unsigned)slot, (unsigned)b, s); if (drain == 1) __threadfence_system(); else if (drain == 3) asm volatile("s_wait_storecnt 0x0" ::: "memory"); __syncthreads(); @@ -277,8 +294,11 @@ __global__ void r4d_ar_gather_hc_write_kernel( } } __syncthreads(); - r4d_ar_link_landed(gate, gate_word, gate_ctr, (unsigned int)nb, (unsigned int)tid); if (acq) __threadfence_system(); + r4d_archk_wait(my_flags, nbw, (unsigned)slot, (unsigned)b, s, [&](unsigned int& h) { + for (int k = start + tid; k < end; k += nt) h += r4d_archk_u4(ms[k], 4u * (unsigned)k); + }); + r4d_ar_link_landed(gate, gate_word, gate_ctr, (unsigned int)nb, (unsigned int)tid); for (int k = start + tid; k < end; k += nt) { const int4 r = vadd<__hip_bfloat16, 8>(y4[k], ms[k]); @@ -398,6 +418,7 @@ void r4d_ar_hc_write(long peer_scratch, long my_scratch, long peer_flags, long m int nt = (int)nthreads; if (nt < 64) nt = 64; if (nt > 1024) nt = 1024; + nt &= ~31; // whole waves: the push check's wave sum reads all 32 lanes r4d_ar_hc_write_kernel<<>>( (int4*)y, (int4*)peer_scratch, (const int4*)my_scratch, (unsigned*)peer_flags, (unsigned*)my_flags, (unsigned*)seq_ctrs, (const unsigned short*)inj, (unsigned short*)h, @@ -436,6 +457,7 @@ int r4d_ar_gather_hc_write(long peer_scratch, long my_scratch, long peer_flags, int nt = (int)nthreads; if (nt < 64) nt = 64; if (nt > 1024) nt = 1024; + nt &= ~31; // whole waves: the push check's wave sum reads all 32 lanes /* Zeroed including padding: the struct is passed by value, so its tail padding is part of the * kernel-argument bytes a recorded pass's audit compares. */ R4dArGather G; @@ -527,6 +549,7 @@ void r4d_ar_oneshot_2rank_exact(long peer_scratch, long my_scratch, long peer_fl int nt = (int)nthreads; if (nt < 64) nt = 64; if (nt > 1024) nt = 1024; + nt &= ~31; // whole waves: the push check's wave sum reads all 32 lanes #define L(T, LN) r4d_ar_oneshot_2rank_exact_kernel<<>>( \ (const int4*)inp, (int4*)peer_scratch, (const int4*)my_scratch, (int4*)out, \ (unsigned*)peer_flags, (unsigned*)my_flags, (unsigned*)seq_ctrs, n16, \ diff --git a/libr4d/r4d_ar_oneshot_2rank_wht6.h b/libr4d/r4d_ar_oneshot_2rank_wht6.h index 576747dc..0a2844cc 100644 --- a/libr4d/r4d_ar_oneshot_2rank_wht6.h +++ b/libr4d/r4d_ar_oneshot_2rank_wht6.h @@ -129,7 +129,8 @@ struct Wht6Chunk { template __device__ __forceinline__ void wht6_push_chunk(const Ld& ld, int c, int n_groups, int lane, int lig, int gw, uint4* p_pay, - unsigned short* p_sc, uint4* l_pay, float* l_sc) { + unsigned short* p_sc, uint4* l_pay, float* l_sc, + unsigned int& hck) { using Ch = Wht6Chunk; const int qmin = -(1 << (BITS - 1)), qmax = (1 << (BITS - 1)) - 1; unsigned int acc[Ch::NW]; @@ -157,8 +158,10 @@ __device__ __forceinline__ void wht6_push_chunk(const Ld& ld, int c, int n_group float sc = (a > 0.f) ? bf16_round(a * (1.0f / (float)qmax)) : 1.0f; if (!(sc > 0.f)) sc = 1.0f; if (lig == 0 && live) { - p_sc[g] = (unsigned short)(__float_as_uint(sc) >> 16); + const unsigned short sc16 = (unsigned short)(__float_as_uint(sc) >> 16); + p_sc[g] = sc16; l_sc[g] = sc; + hck += r4d_archk_mix(sc16, R4D_ARCHK_U16 + (unsigned)g); } const float inv = 1.0f / sc; #pragma unroll @@ -177,6 +180,7 @@ __device__ __forceinline__ void wht6_push_chunk(const Ld& ld, int c, int n_group const long o = (long)c * (Ch::NW / 4 * WAVE) + (long)u * WAVE + lane; p_pay[o] = q; l_pay[o] = q; + hck += r4d_archk_u4(q, 4u * (unsigned)o); } } else { unsigned int* pp = reinterpret_cast(p_pay); @@ -186,6 +190,7 @@ __device__ __forceinline__ void wht6_push_chunk(const Ld& ld, int c, int n_group const long o = (long)c * (Ch::NW * WAVE) + (long)u * WAVE + lane; pp[o] = acc[u]; lp[o] = acc[u]; + hck += r4d_archk_mix(acc[u], (unsigned)o); } } } @@ -209,6 +214,34 @@ __device__ __forceinline__ void wht6_handshake(unsigned int* peer_flags, unsigne if (acq) __threadfence_system(); } +// The push check's re-hash of one chunk (r4d_ar_seq.h): the payload words and live scales the +// reduce below reads, at the indices wht6_push_chunk hashed them. +template +__device__ __forceinline__ void wht6_rehash_chunk(unsigned int& h, int c, int n_groups, int lane, + int lig, int gw, const uint4* m_pay, + const unsigned short* m_sc) { + using Ch = Wht6Chunk; + if constexpr (Ch::U4) { +#pragma unroll + for (int u = 0; u < Ch::NW / 4; ++u) { + const long o = (long)c * (Ch::NW / 4 * WAVE) + (long)u * WAVE + lane; + h += r4d_archk_u4(m_pay[o], 4u * (unsigned)o); + } + } else { + const unsigned int* mp = reinterpret_cast(m_pay); +#pragma unroll + for (int u = 0; u < Ch::NW; ++u) { + const long o = (long)c * (Ch::NW * WAVE) + (long)u * WAVE + lane; + h += r4d_archk_mix(mp[o], (unsigned)o); + } + } +#pragma unroll + for (int t = 0; t < Ch::ITERS; ++t) { + const int g = c * CG + t * GPW + gw; + if (lig == 0 && g < n_groups) h += r4d_archk_mix(m_sc[g], R4D_ARCHK_U16 + (unsigned)g); + } +} + // Reduce: unpack both halves, add in fp32, inverse-rotate, pass each live group's EPL elements to st. template __device__ __forceinline__ void wht6_reduce_chunk(const St& st, int c, int n_groups, int lane, @@ -300,10 +333,16 @@ __global__ void r4d_ar_oneshot_2rank_wht6_kernel( #pragma unroll for (int i = 0; i < EPL; ++i) v[i] = rd_to_f(in[off + i]); }; + unsigned int hck = 0u; // the push check (r4d_ar_seq.h) for (int c = c0 + wid; c < c1; c += nwarp) - wht6_push_chunk(ld, c, n_groups, lane, lig, gw, w.p_pay, w.p_sc, w.l_pay, w.l_sc); + wht6_push_chunk(ld, c, n_groups, lane, lig, gw, w.p_pay, w.p_sc, w.l_pay, w.l_sc, hck); + r4d_archk_put(hck, peer_flags, nbw, s_slot, (unsigned)b, s); wht6_handshake(peer_flags, my_flags, b, s, tid, drain, acq); + r4d_archk_wait(my_flags, nbw, s_slot, (unsigned)b, s, [&](unsigned int& h) { + for (int c = c0 + wid; c < c1; c += nwarp) + wht6_rehash_chunk(h, c, n_groups, lane, lig, gw, w.m_pay, w.m_sc); + }); const auto st = [&](long off, const float* v) { #pragma unroll diff --git a/libr4d/r4d_ar_oneshot_2rank_wht6.hip b/libr4d/r4d_ar_oneshot_2rank_wht6.hip index 67a2575a..0047c121 100644 --- a/libr4d/r4d_ar_oneshot_2rank_wht6.hip +++ b/libr4d/r4d_ar_oneshot_2rank_wht6.hip @@ -139,10 +139,16 @@ __global__ void r4d_ar_hc_write_wht6_kernel( r4d_ar_hc_write_word(inj, h, row, i0, n, hc, irow, hrow, reinterpret_cast(&q)); }; + unsigned int hck = 0u; // the push check (r4d_ar_seq.h) for (int c = c0 + wid; c < c1; c += nwarp) - wht6_push_chunk(ld, c, n_groups, lane, lig, gw, w.p_pay, w.p_sc, w.l_pay, w.l_sc); + wht6_push_chunk(ld, c, n_groups, lane, lig, gw, w.p_pay, w.p_sc, w.l_pay, w.l_sc, hck); + r4d_archk_put(hck, peer_flags, nbw, s_slot, (unsigned)b, s); wht6_handshake(peer_flags, my_flags, b, s, tid, drain, acq); + r4d_archk_wait(my_flags, nbw, s_slot, (unsigned)b, s, [&](unsigned int& h) { + for (int c = c0 + wid; c < c1; c += nwarp) + wht6_rehash_chunk(h, c, n_groups, lane, lig, gw, w.m_pay, w.m_sc); + }); r4d_ar_link_landed(gate, gate_word, gate_ctr, (unsigned int)nb, (unsigned int)tid); for (int c = c0 + wid; c < c1; c += nwarp) diff --git a/libr4d/r4d_ar_seq.h b/libr4d/r4d_ar_seq.h index e9ae425c..2ce45081 100644 --- a/libr4d/r4d_ar_seq.h +++ b/libr4d/r4d_ar_seq.h @@ -87,3 +87,114 @@ __device__ __forceinline__ void r4d_ar_link_landed(long long* gate, long long wo __device__ __forceinline__ bool r4d_ar_seq_behind(unsigned int flag, unsigned int want) { return (int)(flag - want) < 0; } + +// ---- The push check. A peer's push over PCIe P2P can land after its release flag (measured on 2x +// R9700: the reader sometimes found the slot's previous contents right after the handshake, and +// the new bytes ~1 us later), so the flag alone does not say the bytes are there. The pushed bytes +// wait in the writer's L2 for the release's writeback, and that writeback and the flag store reach +// the peer by different paths. The writer sums +// mix(dword, index) over what it pushes and stores the block's sum, salted with its handshake +// value, into the peer's check word for (slot, block): fine-grained words after the flags +// (r4d_ar_entry.hip sizes the allocation). The reader sums the same over the bytes it is about to +// read and reads again until the two agree. A stale element and a stale check word (an older +// handshake value) both disagree, so only the landed push passes; the order the two land in does +// not matter. The sum is order-free, so writer and reader may split the bytes differently. +#define R4D_ARCHK_SALT 0x6C8E9CF5u +#ifndef R4D_ARCHK_DEADLINE +#define R4D_ARCHK_DEADLINE 200000000ull // wall_clock64 ticks (100 MHz): 2 s; a push that never lands is a lockstep fault +#endif +#define R4D_ARCHK_U16 0x80000000u // index space of 16-bit fields (wire scales), apart from dwords + +// MurmurHash3's finalizer over (value, index): every bit of the value reaches every bit of the term, +// so a sum of terms changes when any value does. A linear mix is not enough: (v ^ c_e) summed over +// a run of indices can lose v entirely, and a message of one repeated value (a test pattern, a +// zero-filled row) would then pass whatever the slot held. +__device__ __forceinline__ unsigned int r4d_archk_mix(unsigned int v, unsigned int e) { + unsigned int x = v ^ (e * 0x9E3779B9u); + x ^= x >> 16; x *= 0x85EBCA6Bu; + x ^= x >> 13; x *= 0xC2B2AE35u; + x ^= x >> 16; + return x; +} +// A 16B word at dword index `e`. +__device__ __forceinline__ unsigned int r4d_archk_u4(int4 v, unsigned int e) { + return r4d_archk_mix((unsigned int)v.x, e) + r4d_archk_mix((unsigned int)v.y, e + 1u) + + r4d_archk_mix((unsigned int)v.z, e + 2u) + r4d_archk_mix((unsigned int)v.w, e + 3u); +} +__device__ __forceinline__ unsigned int r4d_archk_u4(uint4 v, unsigned int e) { + return r4d_archk_mix(v.x, e) + r4d_archk_mix(v.y, e + 1u) + r4d_archk_mix(v.z, e + 2u) + + r4d_archk_mix(v.w, e + 3u); +} +// The check word of (slot, block) in a flags allocation: two parity slots of nbw words after the +// nbw flags. +__device__ __forceinline__ unsigned int* r4d_archk_word(unsigned int* flags, unsigned int nbw, + unsigned int slot, unsigned int b) { + return flags + nbw + slot * nbw + b; +} +__device__ __forceinline__ unsigned int r4d_archk_wave_sum(unsigned int h) { + for (int o = 16; o > 0; o >>= 1) h += __shfl_xor(h, o, 32); + return h; +} + +// Writer: every thread of the block calls this after its pushes, before the handshake's barrier. +// Thread 0 stores the block's sum into the peer's check word. +__device__ __forceinline__ void r4d_archk_put(unsigned int h, unsigned int* peer_flags, + unsigned int nbw, unsigned int slot, + unsigned int b, unsigned int seq, + bool release = false) { + __shared__ unsigned int s_ckp[32]; + const unsigned int tid = threadIdx.x, nt = blockDim.x; + h = r4d_archk_wave_sum(h); + if ((tid & 31u) == 0u) s_ckp[tid >> 5] = h; + __syncthreads(); + if (tid == 0) { + unsigned int t = 0u; + for (unsigned int w = 0; w < (nt + 31u) / 32u; ++w) t += s_ckp[w]; + /* The pushed bytes sit in this card's L2 until a system-scope release writes them back. The + * flag's release, right after this, does that, so the word itself can be relaxed (the reader + * re-reads until the bytes agree with it). Where the flag went out before the push (the test + * mode, drain 4) nothing else would write them back before the kernel ends: a release. */ + unsigned int* const cw = r4d_archk_word(peer_flags, nbw, slot, b); + if (release) + __hip_atomic_store(cw, t + seq * R4D_ARCHK_SALT, __ATOMIC_RELEASE, __HIP_MEMORY_SCOPE_SYSTEM); + else + __hip_atomic_store(cw, t + seq * R4D_ARCHK_SALT, __ATOMIC_RELAXED, __HIP_MEMORY_SCOPE_SYSTEM); + } +} + +// Reader: every thread of the block calls this after the handshake; `rehash(h)` adds the thread's +// share of the bytes the block is about to read, with the same loads the kernel then makes. +template +__device__ __forceinline__ void r4d_archk_wait(unsigned int* my_flags, unsigned int nbw, + unsigned int slot, unsigned int b, + unsigned int seq, const F& rehash) { + __shared__ unsigned int s_ckw[32]; + __shared__ unsigned int s_ckok; + const unsigned int tid = threadIdx.x, nt = blockDim.x; + const unsigned long long t0 = tid == 0 ? wall_clock64() : 0ull; + for (;;) { + /* The check word first, so its round trip overlaps the re-hash's loads. Read before the bytes + * it is compared with, it can only be older than them, and an older word just fails this try. */ + const unsigned int want = tid == 0 ? __hip_atomic_load(r4d_archk_word(my_flags, nbw, slot, b), + __ATOMIC_RELAXED, __HIP_MEMORY_SCOPE_SYSTEM) + : 0u; + unsigned int h = 0u; + rehash(h); + h = r4d_archk_wave_sum(h); + if ((tid & 31u) == 0u) s_ckw[tid >> 5] = h; + __syncthreads(); + if (tid == 0) { + unsigned int t = 0u; + for (unsigned int w = 0; w < (nt + 31u) / 32u; ++w) t += s_ckw[w]; + const bool ok = t + seq * R4D_ARCHK_SALT == want; + if (!ok && wall_clock64() - t0 > R4D_ARCHK_DEADLINE) __builtin_trap(); + s_ckok = ok ? 1u : 0u; + } + __syncthreads(); + if (s_ckok) return; + __syncthreads(); // s_ckw and s_ckok are rewritten by the next try + /* Drop the vector caches, so the next try reads the bytes from where the push lands. */ + __builtin_amdgcn_fence(__ATOMIC_ACQUIRE, ""); + __builtin_amdgcn_s_sleep(2); + } +} diff --git a/libr4d/r4d_fused_quant_fp8.hip b/libr4d/r4d_fused_quant_fp8.hip index 4f9853f0..23c615d6 100644 --- a/libr4d/r4d_fused_quant_fp8.hip +++ b/libr4d/r4d_fused_quant_fp8.hip @@ -415,13 +415,18 @@ __global__ __launch_bounds__(THR) void ar_rmsnorm_quant_fp8_kernel( unsigned int* const Lpk = (unsigned int*)loc_pack; float* const Lsc = (float*)(loc_pack + loc_scale_off); + unsigned int hck = 0u; // the push check (r4d_ar_seq.h) if constexpr (WIRE == 0) { /* Push my rows into the peer's slot. `x` is a tight [M][n] of bf16 (the launcher refuses a * pitched one), so a row range is a 16B-word range. */ const int wpr = (int)(n >> 3); const int wend = r1 * wpr; const int4* const in4 = (const int4*)x; - for (int k = r0 * wpr + (int)tid; k < wend; k += (int)THR) ps[k] = in4[k]; + for (int k = r0 * wpr + (int)tid; k < wend; k += (int)THR) { + const int4 v = in4[k]; + ps[k] = v; + hck += r4d_archk_u4(v, 4u * (unsigned)k); + } } else { /* Rotate, amax, quantise and pack straight into the peer's slot. A wave owns whole chunks, * so every lane emits ARQ_NWORD dwords per chunk and the flush is lane-uniform. No group is @@ -449,8 +454,10 @@ __global__ __launch_bounds__(THR) void ar_rmsnorm_quant_fp8_kernel( float sc = (a > 0.0f) ? bf2f(f2bf(a * (1.0f / (float)ARQ_QMAX))) : 1.0f; if (!(sc > 0.0f)) sc = 1.0f; if (lig == 0) { - p_sc[g] = (unsigned short)(__float_as_uint(sc) >> 16); + const unsigned short sc16 = (unsigned short)(__float_as_uint(sc) >> 16); + p_sc[g] = sc16; Lsc[g] = sc; + hck += r4d_archk_mix(sc16, R4D_ARCHK_U16 + (unsigned)g); } const float inv = 1.0f / sc; #pragma unroll @@ -464,21 +471,33 @@ __global__ __launch_bounds__(THR) void ar_rmsnorm_quant_fp8_kernel( } /* Flush lane-contiguously, widest region first, so each is one coalesced store. */ char* const pb = p_pay + (size_t)c * ARQ_CBYTES; + const unsigned int cd = (unsigned)c * (ARQ_CBYTES / 4); // the chunk's first dword #pragma unroll - for (int w4 = 0; w4 + 4 <= ARQ_NWORD; w4 += 4) + for (int w4 = 0; w4 + 4 <= ARQ_NWORD; w4 += 4) { *(uint4*)(pb + w4 * 4 * ARQ_WV + lane * 16) = make_uint4(acc[w4], acc[w4 + 1], acc[w4 + 2], acc[w4 + 3]); - if constexpr (ARQ_NWORD % 4 >= 2) + hck += r4d_archk_u4(make_uint4(acc[w4], acc[w4 + 1], acc[w4 + 2], acc[w4 + 3]), + cd + (unsigned)(w4 * ARQ_WV) + lane * 4u); + } + if constexpr (ARQ_NWORD % 4 >= 2) { *(uint2*)(pb + (ARQ_NWORD & ~3) * 4 * ARQ_WV + lane * 8) = make_uint2(acc[ARQ_NWORD & ~3], acc[(ARQ_NWORD & ~3) + 1]); - if constexpr (ARQ_NWORD % 2 == 1) + const unsigned int e = cd + (unsigned)((ARQ_NWORD & ~3) * ARQ_WV) + lane * 2u; + hck += r4d_archk_mix(acc[ARQ_NWORD & ~3], e) + + r4d_archk_mix(acc[(ARQ_NWORD & ~3) + 1], e + 1u); + } + if constexpr (ARQ_NWORD % 2 == 1) { *(unsigned int*)(pb + (ARQ_NWORD - 1) * 4 * ARQ_WV + lane * 4) = acc[ARQ_NWORD - 1]; + hck += r4d_archk_mix(acc[ARQ_NWORD - 1], + cd + (unsigned)((ARQ_NWORD - 1) * ARQ_WV) + lane); + } #pragma unroll for (int wi = 0; wi < ARQ_NWORD; ++wi) Lpk[((size_t)c * ARQ_NWORD + wi) * ARQ_WV + lane] = acc[wi]; } } + r4d_archk_put(hck, peer_flags, nbw, s_slot, (unsigned)b, sq); /* The drain modes are r4d_ar_oneshot_2rank_exact's, selected by the same variant table. */ if (drain == 1) __threadfence_system(); else if (drain == 3) asm volatile("s_wait_storecnt 0x0" ::: "memory"); @@ -495,6 +514,37 @@ __global__ __launch_bounds__(THR) void ar_rmsnorm_quant_fp8_kernel( } __syncthreads(); if (acq) __threadfence_system(); + r4d_archk_wait(my_flags, nbw, s_slot, (unsigned)b, sq, [&](unsigned int& h) { + if constexpr (WIRE == 0) { + const int wpr = (int)(n >> 3); + const int4* const m4 = (const int4*)ms; + for (int k = r0 * wpr + (int)tid; k < r1 * wpr; k += (int)THR) + h += r4d_archk_u4(m4[k], 4u * (unsigned)k); + } else { + for (int ci = (int)wave; ci < cpb; ci += (int)(THR / ARQ_WV)) { + const int c = c00 + ci; + const char* const pb = m_pay + (size_t)c * ARQ_CBYTES; + const unsigned int cd = (unsigned)c * (ARQ_CBYTES / 4); +#pragma unroll + for (int w4 = 0; w4 + 4 <= ARQ_NWORD; w4 += 4) + h += r4d_archk_u4(*(const uint4*)(pb + w4 * 4 * ARQ_WV + lane * 16), + cd + (unsigned)(w4 * ARQ_WV) + lane * 4u); + if constexpr (ARQ_NWORD % 4 >= 2) { + const uint2 u = *(const uint2*)(pb + (ARQ_NWORD & ~3) * 4 * ARQ_WV + lane * 8); + const unsigned int e = cd + (unsigned)((ARQ_NWORD & ~3) * ARQ_WV) + lane * 2u; + h += r4d_archk_mix(u.x, e) + r4d_archk_mix(u.y, e + 1u); + } + if constexpr (ARQ_NWORD % 2 == 1) + h += r4d_archk_mix(*(const unsigned int*)(pb + (ARQ_NWORD - 1) * 4 * ARQ_WV + lane * 4), + cd + (unsigned)((ARQ_NWORD - 1) * ARQ_WV) + lane); +#pragma unroll + for (int t = 0; t < ARQ_ITERS; ++t) { + const int g = c * ARQ_CG + t * ARQ_GPW + gw; + if (lig == 0) h += r4d_archk_mix(m_sc[g], R4D_ARCHK_U16 + (unsigned)g); + } + } + } + }); if constexpr (WIRE == 6) { /* Reduce: unpack both halves of every chunk this block owns, add in fp32, inverse-rotate, diff --git a/libr4d/r4d_rmsnorm_quant_i8.hip b/libr4d/r4d_rmsnorm_quant_i8.hip index b7b7596b..ef4371cf 100644 --- a/libr4d/r4d_rmsnorm_quant_i8.hip +++ b/libr4d/r4d_rmsnorm_quant_i8.hip @@ -324,7 +324,13 @@ __global__ __launch_bounds__(THR) void ar_rmsnorm_quant_i8_kernel( /* push my rows into the peer's slot; `x` is a tight [M][n] of bf16 */ const int wpr = (int)(n >> 3); const int4* const in4 = (const int4*)x; - for (int k = r0 * wpr + (int)tid; k < r1 * wpr; k += (int)THR) ps[k] = in4[k]; + unsigned int hck = 0u; // the push check (r4d_ar_seq.h) + for (int k = r0 * wpr + (int)tid; k < r1 * wpr; k += (int)THR) { + const int4 v = in4[k]; + ps[k] = v; + hck += r4d_archk_u4(v, 4u * (unsigned)k); + } + r4d_archk_put(hck, peer_flags, nbw, s_slot, (unsigned)b, sq); if (drain == 1) __threadfence_system(); else if (drain == 3) asm volatile("s_wait_storecnt 0x0" ::: "memory"); __syncthreads(); @@ -338,6 +344,10 @@ __global__ __launch_bounds__(THR) void ar_rmsnorm_quant_i8_kernel( } __syncthreads(); if (acq) __threadfence_system(); + r4d_archk_wait(my_flags, nbw, s_slot, (unsigned)b, sq, [&](unsigned int& h) { + for (int k = r0 * wpr + (int)tid; k < r1 * wpr; k += (int)THR) + h += r4d_archk_u4(ms[k], 4u * (unsigned)k); + }); const uint16_t* __restrict__ P = (const uint16_t*)ms; /* the peer's rows, in my scratch */ const float sv = SCL ? scalar_of(sc, sc_f32) : 1.0f;