perf(rope): fused per-head RMSNorm + RoPE for the verify window and the MTP drafter, off on HIP (#783 PR-f)
native_qsa_rms_norm_rope does norm<256> and the rope in one launch with the same FMA order as the pair (register barriers keep nvcc from contracting across the norm/rope boundary); rope_parity check 6 compares it with native_qsa_rms_norm_weighted + native_rope_apply by memcmp for head_dim 128/256, strided q|gate and contiguous inputs, unscaled and YaRN. The in-place rope also launches one 32-thread block per row. Verify's norm_rope and q split, and the MTP drafter's q path (all T tokens at once; K for a one-token window) use it. native_norm_rope_usable() gates it: on the CUDA build it is on unless STRATA_NO_NORM_ROPE=1; on the HIP build it is off until rope_parity check 6 passes on AMD hardware, and STRATA_NORM_ROPE=1 turns it on there. The MTP q path's split, FWHT and gate-apply now run over all T tokens in one call each (row-independent). Ported from #783 (stuchapin909, head 4327807), split per piece. Co-authored-by: stuchapin909 <178119976+stuchapin909@users.noreply.github.com>
This commit is contained in:
1 parent
fe4de5de85
commit
088e8a8220
5 files changed
+228
-16
No files matched your search
@@ -17,4 +17,15 @@ bool native_rope_enabled();
|
||||
// The struct's knobs are process constants, so kernel arguments baked at graph capture stay valid.
|
||||
void native_rope_apply(const float* x, float* out, int rows, int head_dim,
|
||||
int n_rot, const RopeScaling& scaling, const int* positions, void* stream);
|
||||
|
||||
/// (#783 PR-f, stuchapin909) native_qsa_rms_norm_weighted + native_rope_apply in one launch, bit-identical to the pair
|
||||
/// (rope_parity check 6): x rows are `in_stride` apart (2 * head_dim reads a q row out of a q|gate row), out rows are
|
||||
/// head_dim apart, x may equal out when in_stride == head_dim. head_dim 128 or 256 and n_rot 64 only.
|
||||
void native_qsa_rms_norm_rope(const float* x, int in_stride, const float* gamma, float* out,
|
||||
int rows, int head_dim, int n_rot, float epsilon,
|
||||
const RopeScaling& scaling, const int* positions, void* stream);
|
||||
/// Whether callers should take the fused kernel (and the in-place rope's 32-thread launch) for this geometry. On the
|
||||
/// CUDA build it is on unless STRATA_NO_NORM_ROPE=1; on the HIP build it is off until the AMD parity check passes there
|
||||
/// (rope_parity check 6) and only STRATA_NORM_ROPE=1 turns it on.
|
||||
bool native_norm_rope_usable(int head_dim, int n_rot);
|
||||
}
|
||||
+25
-13
@@ -707,11 +707,20 @@ bool MtpDrafter::record_front(int T, int row0, cudaStream_t cs, std::string& err
|
||||
const char* v = std::getenv("STRATA_NO_BATCH_KV_STEP");
|
||||
return v != nullptr && v[0] != '\0' && v[0] != '0';
|
||||
}();
|
||||
native_qsa_rms_norm_weighted(kcur_, f32("self_attn.k_norm.weight"), kcur_, (int) HD, (int) (T * NKV), EPS, cs);
|
||||
for (int t = 0; t < T; ++t) {
|
||||
float* kc = kcur_ + t * NKV * HD;
|
||||
if (native_rope_enabled()) native_rope_apply(kc, kc, (int) NKV, (int) HD, (int) s.n_rot, rope_scaling(), pos + t * NH, cs);
|
||||
else rope_neox_apply(kc, kc, (int) NKV, (int) HD, (int) s.n_rot, st_.cos_tab, st_.sin_tab, pos + t * NH, cs);
|
||||
// #783 PR-f (stuchapin909): the per-head RMSNorm and the rope fused (bit-identical to the pair, rope_parity
|
||||
// check 6; STRATA_NO_NORM_ROPE=1 keeps the two; off on HIP until its parity check passes). K rows of one token
|
||||
// are consecutive in `pos`, tokens are NH apart, so a one-token window is the fusable case.
|
||||
const bool fuse_nr = native_rope_enabled() && native_norm_rope_usable((int) HD, (int) s.n_rot);
|
||||
if (T == 1 && fuse_nr) {
|
||||
native_qsa_rms_norm_rope(kcur_, (int) HD, f32("self_attn.k_norm.weight"), kcur_, (int) NKV, (int) HD,
|
||||
(int) s.n_rot, EPS, rope_scaling(), pos, cs);
|
||||
} else {
|
||||
native_qsa_rms_norm_weighted(kcur_, f32("self_attn.k_norm.weight"), kcur_, (int) HD, (int) (T * NKV), EPS, cs);
|
||||
for (int t = 0; t < T; ++t) {
|
||||
float* kc = kcur_ + t * NKV * HD;
|
||||
if (native_rope_enabled()) native_rope_apply(kc, kc, (int) NKV, (int) HD, (int) s.n_rot, rope_scaling(), pos + t * NH, cs);
|
||||
else rope_neox_apply(kc, kc, (int) NKV, (int) HD, (int) s.n_rot, st_.cos_tab, st_.sin_tab, pos + t * NH, cs);
|
||||
}
|
||||
}
|
||||
if (st_.kv_rot) { // rotated K and V (kv_q4.hpp): Q4_0, and INT8 with STRATA_KV_ROT=1
|
||||
fwht256_inplace_cuda(kcur_, (int64_t) T * NKV, cs);
|
||||
@@ -764,25 +773,28 @@ bool MtpDrafter::record_rest(int step_row, cudaStream_t cs, std::string& err) {
|
||||
int wt_q_proj, wt_o_proj;
|
||||
const void* wp_q_proj = wq("self_attn.q_proj.weight", wt_q_proj);
|
||||
const void* wp_o_proj = wq("self_attn.o_proj.weight", wt_o_proj);
|
||||
const bool fuse_nr = native_rope_enabled() && native_norm_rope_usable((int) HD, (int) s.n_rot); // #783 PR-f
|
||||
try {
|
||||
// ---- dense attention over every cell
|
||||
native_mmvq(wt_q_proj, wp_q_proj, xq_, qfull_, (int) N, (int) (NH * 2 * HD), T, cs);
|
||||
for (int t = 0; t < T; ++t) {
|
||||
float* qc = qcur_ + t * NH * HD;
|
||||
if (cudaMemcpy2DAsync(qc, (size_t) HD * 4, qfull_ + t * NH * 2 * HD, (size_t) HD * 2 * 4, (size_t) HD * 4,
|
||||
(size_t) NH, cudaMemcpyDeviceToDevice, cs) != cudaSuccess) {
|
||||
// all T tokens' q rows at once (the rows of token t sit at pos[t * NH ..], so row r reads pos[r])
|
||||
if (fuse_nr) {
|
||||
native_qsa_rms_norm_rope(qfull_, (int) (2 * HD), f32("self_attn.q_norm.weight"), qcur_, (int) (T * NH),
|
||||
(int) HD, (int) s.n_rot, EPS, rope_scaling(), pos, cs);
|
||||
} else {
|
||||
if (cudaMemcpy2DAsync(qcur_, (size_t) HD * 4, qfull_, (size_t) HD * 2 * 4, (size_t) HD * 4,
|
||||
(size_t) (T * NH), cudaMemcpyDeviceToDevice, cs) != cudaSuccess) {
|
||||
err = "mtp: q split failed";
|
||||
return false;
|
||||
}
|
||||
norm_rope(qc, f32("self_attn.q_norm.weight"), (int) NH, (int) HD, pos + t * NH, cs);
|
||||
if (st_.kv_rot) fwht256_inplace_cuda(qc, NH, cs);
|
||||
norm_rope(qcur_, f32("self_attn.q_norm.weight"), (int) (T * NH), (int) HD, pos, cs);
|
||||
}
|
||||
if (st_.kv_rot) fwht256_inplace_cuda(qcur_, (int64_t) T * NH, cs);
|
||||
const QsaAttnPools pools = qsa_attn_pools(st_);
|
||||
if (window_ > 0) window_ids(const_cast<int32_t*>(step), T, (int) window_, ident_, cap_, cs);
|
||||
qsa_decode_attn_batch(qcur_, pools, ident_, step, cap_, s, attn_scratch_, attn_, T, cs);
|
||||
if (st_.kv_rot) fwht256_inplace_cuda(attn_, (int64_t) T * NH, cs);
|
||||
for (int t = 0; t < T; ++t)
|
||||
native_qsa_gate_apply(attn_ + t * NH * HD, qfull_ + t * NH * 2 * HD, attn32_ + t * NH * HD, (int) NH, (int) HD, cs);
|
||||
native_qsa_gate_apply(attn_, qfull_, attn32_, (int) (T * NH), (int) HD, cs);
|
||||
native_quantize_q8_1(attn32_, xq_, (int) (NH * HD), T, cs);
|
||||
native_mmvq(wt_o_proj, wp_o_proj, xq_, bo_, (int) (NH * HD), (int) N, T, cs);
|
||||
// ---- the MLP hyper-connection (the attention write folded in)
|
||||
|
||||
@@ -895,7 +895,15 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
|
||||
if (!native_of(wq, v.name("attn_q.weight"), err) || !native_of(wk, v.name("attn_k.weight"), err) ||
|
||||
!native_of(wv, v.name("attn_v.weight"), err) || !native_of(wo, v.name("attn_output.weight"), err))
|
||||
return false;
|
||||
// #783 PR-f (stuchapin909): the per-head RMSNorm and the rope in one launch, bit-identical to the pair
|
||||
// (rope_parity check 6); STRATA_NO_NORM_ROPE=1 keeps the two; off on HIP until its parity check passes
|
||||
const bool fuse_nr = native_qsa_enabled() && native_rope_enabled() && native_norm_rope_usable((int) HD, (int) s.n_rot);
|
||||
auto norm_rope = [&](float* data, const WeightRef* norm, int rows, int cols, const int32_t* pos) {
|
||||
if (fuse_nr && native_norm_rope_usable(cols, (int) s.n_rot)) {
|
||||
native_qsa_rms_norm_rope(data, cols, (const float*) norm->data, data, rows, cols, (int) s.n_rot, EPS,
|
||||
rope_scaling(), pos, cs);
|
||||
return;
|
||||
}
|
||||
if (native_qsa_enabled()) native_qsa_rms_norm_weighted(data, (const float*) norm->data, data, cols, rows, EPS, cs);
|
||||
else rms_norm_weighted(data, (const float*) norm->data, rows, cols, EPS, cs);
|
||||
if (native_rope_enabled()) native_rope_apply(data, data, rows, cols, (int) s.n_rot, rope_scaling(), pos, cs);
|
||||
@@ -988,12 +996,18 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
|
||||
native_mmvq(wq->native_type, wq->native_data, xq_, qfull_ + tb * NH * 2 * HD, (int) N, (int) (NH * 2 * HD),
|
||||
n, cs);
|
||||
if (qb) {
|
||||
if (fuse_nr) { // the q/gate split reads q straight out of the q|gate rows (stride 2 * HD)
|
||||
native_qsa_rms_norm_rope(qfull_ + tb * NH * 2 * HD, (int) (2 * HD), (const float*) wqn->data,
|
||||
qcur_ + tb * NH * HD, (int) (n * NH), (int) HD, (int) s.n_rot, EPS,
|
||||
rope_scaling(), pos_ + tb * NH, cs);
|
||||
} else {
|
||||
if (cudaMemcpy2DAsync(qcur_ + tb * NH * HD, (size_t) HD * 4, qfull_ + tb * NH * 2 * HD, (size_t) HD * 2 * 4,
|
||||
(size_t) HD * 4, (size_t) (n * NH), cudaMemcpyDeviceToDevice, cs) != cudaSuccess) {
|
||||
err = "verify: the q/gate split failed";
|
||||
return false;
|
||||
}
|
||||
norm_rope(qcur_ + tb * NH * HD, wqn, (int) (n * NH), (int) HD, pos_ + tb * NH);
|
||||
}
|
||||
if (st.kv_rot) fwht256_inplace_cuda(qcur_ + tb * NH * HD, (int64_t) n * NH, cs); // <Hq, Hk> = <q, k>
|
||||
bf16_gemv_fp32_mmvf_multi(mixed_ + tb * N, N, (const uint16_t*) wiq->data, qidx_ + tb * IQ * ID, IQ * ID,
|
||||
N, IQ * ID, n, cs);
|
||||
@@ -1001,12 +1015,17 @@ bool Verifier::record_window(int T, cudaStream_t cs, std::string& err) {
|
||||
} else {
|
||||
for (int t = tb; t < te; ++t) {
|
||||
float* qc = qcur_ + t * NH * HD;
|
||||
if (fuse_nr) {
|
||||
native_qsa_rms_norm_rope(qfull_ + t * NH * 2 * HD, (int) (2 * HD), (const float*) wqn->data, qc,
|
||||
(int) NH, (int) HD, (int) s.n_rot, EPS, rope_scaling(), pos_ + t * NH, cs);
|
||||
} else {
|
||||
if (cudaMemcpy2DAsync(qc, (size_t) HD * 4, qfull_ + t * NH * 2 * HD, (size_t) HD * 2 * 4,
|
||||
(size_t) HD * 4, (size_t) NH, cudaMemcpyDeviceToDevice, cs) != cudaSuccess) {
|
||||
err = "verify: the q/gate split failed";
|
||||
return false;
|
||||
}
|
||||
norm_rope(qc, wqn, (int) NH, (int) HD, pos_ + t * NH);
|
||||
}
|
||||
if (st.kv_rot) fwht256_inplace_cuda(qc, NH, cs); // <Hq, Hk> = <q, k>
|
||||
}
|
||||
for (int t = tb; t < te; ++t) {
|
||||
|
||||
@@ -66,6 +66,63 @@ __global__ void apply(const float* x, float* out, int rows, int width,
|
||||
out[start + pair] = a * c - b * s;
|
||||
out[start + pair + n_rot / 2] = a * s + b * c;
|
||||
}
|
||||
|
||||
template <bool TAB>
|
||||
__global__ void norm_rope_kernel(const float* x, int in_stride, const float* __restrict__ gamma, float* out,
|
||||
int n_cols, int n_rot, float epsilon, float theta_scale,
|
||||
float freq_scale, float corr_low, float corr_high,
|
||||
float ext_factor, float mscale, const int* positions,
|
||||
const int32_t* mtab, RopeTab rt) {
|
||||
const int tid = threadIdx.x;
|
||||
const int row = blockIdx.x;
|
||||
const float* in_row = x + size_t(row) * in_stride;
|
||||
float* out_row = out + size_t(row) * n_cols;
|
||||
float partial = 0.0f;
|
||||
for (size_t col = tid; col < size_t(n_cols); col += 256) {
|
||||
const float xv = in_row[col];
|
||||
partial += xv * xv;
|
||||
}
|
||||
const int half_rot = n_rot / 2;
|
||||
const float x_tid = tid < n_cols ? in_row[tid] : 0.0f;
|
||||
const float x_hi = tid < half_rot ? in_row[tid + half_rot] : 0.0f;
|
||||
__shared__ float sums[32];
|
||||
#pragma unroll
|
||||
for (int offset = 16; offset; offset >>= 1)
|
||||
partial += __shfl_xor_sync(0xffffffffu, partial, offset, 32);
|
||||
const int lane = tid % 32;
|
||||
if (lane == 0) sums[tid / 32] = partial;
|
||||
__syncthreads();
|
||||
partial = lane < 256 / 32 ? sums[lane] : 0.0f;
|
||||
#pragma unroll
|
||||
for (int offset = 16; offset; offset >>= 1)
|
||||
partial += __shfl_xor_sync(0xffffffffu, partial, offset, 32);
|
||||
const float mean = partial / n_cols;
|
||||
const float scale = rsqrtf(mean + epsilon);
|
||||
if (tid >= n_rot && tid < n_cols) {
|
||||
out_row[tid] = (scale * x_tid) * gamma[tid];
|
||||
} else if (tid < half_rot) {
|
||||
const int pair = tid;
|
||||
float a = (scale * x_tid) * gamma[pair];
|
||||
float b = (scale * x_hi) * gamma[pair + half_rot];
|
||||
#if defined(__CUDA_ARCH__) && !defined(__HIP_DEVICE_COMPILE__)
|
||||
asm volatile("mov.f32 %0, %1;" : "=f"(a) : "f"(a));
|
||||
asm volatile("mov.f32 %0, %1;" : "=f"(b) : "f"(b));
|
||||
#endif
|
||||
float c, s;
|
||||
if (!(TAB && rope_tab_cs(rt, mrope_pos(mtab, positions[row], pair), pair, c, s))) {
|
||||
const float theta_extrap = mrope_pos(mtab, positions[row], pair) * powf(theta_scale, float(pair));
|
||||
rope_scaled_angle(theta_extrap, freq_scale, corr_low, corr_high, ext_factor, mscale, pair, c, s);
|
||||
}
|
||||
float bs = b * s;
|
||||
float bc = b * c;
|
||||
#if defined(__CUDA_ARCH__) && !defined(__HIP_DEVICE_COMPILE__)
|
||||
asm volatile("mov.f32 %0, %1;" : "=f"(bs) : "f"(bs));
|
||||
asm volatile("mov.f32 %0, %1;" : "=f"(bc) : "f"(bc));
|
||||
#endif
|
||||
out_row[pair] = fmaf(a, c, -bs);
|
||||
out_row[pair + half_rot] = fmaf(a, s, bc);
|
||||
}
|
||||
}
|
||||
}
|
||||
// one table per device (a layer split runs the rope kernels on several): set and read for the current device
|
||||
namespace {
|
||||
@@ -115,6 +172,20 @@ RopeTab rope_table_for(const RopeScaling& scaling) {
|
||||
}
|
||||
void native_rope_set_enabled(bool value) { enabled.store(value, std::memory_order_relaxed); }
|
||||
bool native_rope_enabled() { return enabled.load(std::memory_order_relaxed); }
|
||||
bool native_norm_rope_usable(int head_dim, int n_rot) {
|
||||
static const bool on = [] {
|
||||
const char* off = std::getenv("STRATA_NO_NORM_ROPE");
|
||||
if (off != nullptr && off[0] != '\0' && off[0] != '0') return false;
|
||||
#if defined(__HIPCC__)
|
||||
const char* v = std::getenv("STRATA_NORM_ROPE"); // AMD: opt in until rope_parity check 6 passes there
|
||||
return v != nullptr && v[0] != '\0' && v[0] != '0';
|
||||
#else
|
||||
return true;
|
||||
#endif
|
||||
}();
|
||||
return on && (head_dim == 128 || head_dim == 256) && n_rot == 64;
|
||||
}
|
||||
|
||||
void native_rope_apply(const float* x, float* out, int rows, int head_dim,
|
||||
int n_rot, const RopeScaling& scaling, const int* positions, void* stream) {
|
||||
if (!x || !out || !positions || !stream || rows < 1 || rows > 65535 ||
|
||||
@@ -134,12 +205,39 @@ void native_rope_apply(const float* x, float* out, int rows, int head_dim,
|
||||
const float theta_scale = powf((float) scaling.freq_base, -2.0f / n_rot);
|
||||
const RopeKernelArgs k = scaling.kernel_args(n_rot); // none: the identity constants
|
||||
const RopeTab rt = rope_table_for(scaling);
|
||||
const dim3 grid((head_dim / 2 + 127) / 128, rows);
|
||||
const unsigned threads = (x == out && n_rot == 64 && native_norm_rope_usable(head_dim, n_rot)) ? 32u : 128u;
|
||||
const dim3 grid(threads == 32u ? 1u : unsigned((head_dim / 2 + 127) / 128), rows);
|
||||
if (rt.cos != nullptr)
|
||||
apply<true><<<grid, 128, 0, static_cast<cudaStream_t>(stream)>>>(x, out, rows, head_dim, n_rot, theta_scale,
|
||||
apply<true><<<grid, threads, 0, static_cast<cudaStream_t>(stream)>>>(x, out, rows, head_dim, n_rot, theta_scale,
|
||||
k.freq_scale, k.corr_low, k.corr_high, k.ext_factor, k.attn_factor, positions, mrope_table(), rt);
|
||||
else
|
||||
apply<false><<<grid, 128, 0, static_cast<cudaStream_t>(stream)>>>(x, out, rows, head_dim, n_rot, theta_scale,
|
||||
apply<false><<<grid, threads, 0, static_cast<cudaStream_t>(stream)>>>(x, out, rows, head_dim, n_rot, theta_scale,
|
||||
k.freq_scale, k.corr_low, k.corr_high, k.ext_factor, k.attn_factor, positions, mrope_table(), rt);
|
||||
const auto error = cudaGetLastError();
|
||||
if (error != cudaSuccess) throw std::runtime_error(cudaGetErrorString(error));
|
||||
}
|
||||
|
||||
void native_qsa_rms_norm_rope(const float* x, int in_stride, const float* gamma, float* out,
|
||||
int rows, int head_dim, int n_rot, float epsilon,
|
||||
const RopeScaling& scaling, const int* positions, void* stream) {
|
||||
if (!x || !gamma || !out || !positions || !stream || rows < 1 || rows > 65535 ||
|
||||
(head_dim != 128 && head_dim != 256) || in_stride < head_dim || n_rot != 64 ||
|
||||
!std::isfinite(epsilon) || epsilon < 0.0f ||
|
||||
rope_scaling_invalid(scaling) != nullptr ||
|
||||
reinterpret_cast<uintptr_t>(x) % 4 || reinterpret_cast<uintptr_t>(gamma) % 4 ||
|
||||
reinterpret_cast<uintptr_t>(out) % 4 || reinterpret_cast<uintptr_t>(positions) % 4) {
|
||||
throw std::invalid_argument("native_qsa_rms_norm_rope: invalid arguments");
|
||||
}
|
||||
const float theta_scale = powf((float) scaling.freq_base, -2.0f / n_rot);
|
||||
const RopeKernelArgs k = scaling.kernel_args(n_rot);
|
||||
const RopeTab rt = rope_table_for(scaling);
|
||||
if (rt.cos != nullptr)
|
||||
norm_rope_kernel<true><<<unsigned(rows), 256, 0, static_cast<cudaStream_t>(stream)>>>(
|
||||
x, in_stride, gamma, out, head_dim, n_rot, epsilon, theta_scale,
|
||||
k.freq_scale, k.corr_low, k.corr_high, k.ext_factor, k.attn_factor, positions, mrope_table(), rt);
|
||||
else
|
||||
norm_rope_kernel<false><<<unsigned(rows), 256, 0, static_cast<cudaStream_t>(stream)>>>(
|
||||
x, in_stride, gamma, out, head_dim, n_rot, epsilon, theta_scale,
|
||||
k.freq_scale, k.corr_low, k.corr_high, k.ext_factor, k.attn_factor, positions, mrope_table(), rt);
|
||||
const auto error = cudaGetLastError();
|
||||
if (error != cudaSuccess) throw std::runtime_error(cudaGetErrorString(error));
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
// fast-math device trig must answer to the same `RopeScaling`, yarn and none alike, so one cache never
|
||||
// mixes two rotations.
|
||||
#include "strata/kernels/rope.hpp"
|
||||
#include "strata/kernels/native_qsa.hpp"
|
||||
#include "strata/kernels/native_rope.hpp"
|
||||
|
||||
#include <cuda_runtime.h>
|
||||
@@ -433,6 +434,77 @@ int main(int argc, char** argv) {
|
||||
std::printf(" native path vs table path, none %s (%lld of %d over 3e-3, worst rel %.3e)\n",
|
||||
none_bad5 ? "*** WRONG ***" : "agrees", none_bad5, nrows * head_dim, worst5);
|
||||
bad += (int) none_bad5;
|
||||
|
||||
// ---- 6. FUSED RMSNorm + RoPE (`native_qsa_rms_norm_rope`) MUST BE BIT-IDENTICAL to
|
||||
// `native_qsa_rms_norm_weighted` followed by `native_rope_apply` for both contiguous
|
||||
// (`in_stride == head_dim`) and strided Q/gate split (`in_stride == 2 * head_dim`) inputs,
|
||||
// across supported head_dims (128, 256) and both unscaled and YaRN scaling.
|
||||
for (int hd6 : {128, 256}) {
|
||||
const int nr6 = 64;
|
||||
const int rows6 = 96;
|
||||
std::vector<float> x6_strided((size_t) rows6 * 2 * hd6);
|
||||
std::vector<float> gamma6((size_t) hd6);
|
||||
std::vector<int> pos6((size_t) rows6);
|
||||
for (auto& v : x6_strided) v = gauss2(rng2);
|
||||
for (auto& g : gamma6) g = 0.5f + std::fabs(gauss2(rng2));
|
||||
for (int r = 0; r < rows6; ++r) pos6[(size_t) r] = (r * 53 + 1) % npos;
|
||||
|
||||
float *d_xs6 = nullptr, *d_g6 = nullptr, *d_ref6 = nullptr, *d_fus6 = nullptr;
|
||||
int* d_pos6 = nullptr;
|
||||
check(cudaMalloc(&d_xs6, x6_strided.size() * sizeof(float)), "malloc xs6");
|
||||
check(cudaMalloc(&d_g6, gamma6.size() * sizeof(float)), "malloc g6");
|
||||
check(cudaMalloc(&d_ref6, (size_t) rows6 * hd6 * sizeof(float)), "malloc ref6");
|
||||
check(cudaMalloc(&d_fus6, (size_t) rows6 * hd6 * sizeof(float)), "malloc fus6");
|
||||
check(cudaMalloc(&d_pos6, pos6.size() * sizeof(int)), "malloc pos6");
|
||||
check(cudaMemcpy(d_xs6, x6_strided.data(), x6_strided.size() * sizeof(float), cudaMemcpyHostToDevice), "copy xs6");
|
||||
check(cudaMemcpy(d_g6, gamma6.data(), gamma6.size() * sizeof(float), cudaMemcpyHostToDevice), "copy g6");
|
||||
check(cudaMemcpy(d_pos6, pos6.data(), pos6.size() * sizeof(int), cudaMemcpyHostToDevice), "copy pos6");
|
||||
|
||||
long long fbad = 0;
|
||||
for (const auto& sc6 : {none5, yarn}) {
|
||||
// (a) strided Q/gate split (in_stride = 2 * hd6) vs cudaMemcpy2DAsync + norm + rope
|
||||
check(cudaMemcpy2DAsync(d_ref6, (size_t) hd6 * sizeof(float), d_xs6, (size_t) 2 * hd6 * sizeof(float),
|
||||
(size_t) hd6 * sizeof(float), (size_t) rows6, cudaMemcpyDeviceToDevice, cs5),
|
||||
"split6");
|
||||
strata::kernels::native_qsa_rms_norm_weighted(d_ref6, d_g6, d_ref6, hd6, rows6, 1e-6f, cs5);
|
||||
strata::kernels::native_rope_apply(d_ref6, d_ref6, rows6, hd6, nr6, sc6, d_pos6, cs5);
|
||||
|
||||
strata::kernels::native_qsa_rms_norm_rope(d_xs6, 2 * hd6, d_g6, d_fus6, rows6, hd6, nr6, 1e-6f, sc6,
|
||||
d_pos6, cs5);
|
||||
check(cudaStreamSynchronize(cs5), "sync6a");
|
||||
std::vector<float> href((size_t) rows6 * hd6), hfus((size_t) rows6 * hd6);
|
||||
check(cudaMemcpy(href.data(), d_ref6, href.size() * sizeof(float), cudaMemcpyDeviceToHost), "back ref6a");
|
||||
check(cudaMemcpy(hfus.data(), d_fus6, hfus.size() * sizeof(float), cudaMemcpyDeviceToHost), "back fus6a");
|
||||
for (size_t i = 0; i < href.size(); ++i) {
|
||||
if (std::memcmp(&href[i], &hfus[i], 4) != 0) ++fbad;
|
||||
}
|
||||
|
||||
// (b) contiguous in-place (in_stride = hd6, output == input)
|
||||
check(cudaMemcpy2DAsync(d_ref6, (size_t) hd6 * sizeof(float), d_xs6, (size_t) 2 * hd6 * sizeof(float),
|
||||
(size_t) hd6 * sizeof(float), (size_t) rows6, cudaMemcpyDeviceToDevice, cs5),
|
||||
"contig6_ref");
|
||||
check(cudaMemcpy(d_fus6, d_ref6, (size_t) rows6 * hd6 * sizeof(float), cudaMemcpyDeviceToDevice),
|
||||
"contig6_fus");
|
||||
strata::kernels::native_qsa_rms_norm_weighted(d_ref6, d_g6, d_ref6, hd6, rows6, 1e-6f, cs5);
|
||||
strata::kernels::native_rope_apply(d_ref6, d_ref6, rows6, hd6, nr6, sc6, d_pos6, cs5);
|
||||
strata::kernels::native_qsa_rms_norm_rope(d_fus6, hd6, d_g6, d_fus6, rows6, hd6, nr6, 1e-6f, sc6,
|
||||
d_pos6, cs5);
|
||||
check(cudaStreamSynchronize(cs5), "sync6b");
|
||||
check(cudaMemcpy(href.data(), d_ref6, href.size() * sizeof(float), cudaMemcpyDeviceToHost), "back ref6b");
|
||||
check(cudaMemcpy(hfus.data(), d_fus6, hfus.size() * sizeof(float), cudaMemcpyDeviceToHost), "back fus6b");
|
||||
for (size_t i = 0; i < href.size(); ++i) {
|
||||
if (std::memcmp(&href[i], &hfus[i], 4) != 0) ++fbad;
|
||||
}
|
||||
}
|
||||
std::printf(" native_qsa_rms_norm_rope hd=%3d nr=%2d %s (%lld of %d elements differ)\n",
|
||||
hd6, nr6, fbad ? "*** WRONG ***" : "bit-identical", fbad, rows6 * hd6 * 4);
|
||||
bad += (int) fbad;
|
||||
cudaFree(d_xs6);
|
||||
cudaFree(d_g6);
|
||||
cudaFree(d_ref6);
|
||||
cudaFree(d_fus6);
|
||||
cudaFree(d_pos6);
|
||||
}
|
||||
}
|
||||
|
||||
std::printf("\nrope: %d failures\n", bad);
|
||||
|
||||
Reference in new issue
Block a user