Re-migrated (dpct of 0.1.29 and 0.1.31, 3-way merge per file): 45 files changed upstream, 38 conflicts resolved; verify.cpp, mtp.cpp and the new conversation_state.cpp ported by hand; 7 new upstream files. Fixed on the way: - the port's grouped expert kernels stay the default: upstream's new multi kernels (8 rows a group) ran on the port's 32-row grid and wrote a quarter of each expert (end-of-text output); STRATA_EXPERT_SPLIT=1 runs them with their own grid. Lanes per row now selectable (STRATA_GU/DOWN_LANES) - dpct's unparenthesised __fadd_rn: the native QSA indexer's per-token pooled key (decode), the native QSA scores and the s2 activation rounding replaced instead of added - from the first migration; fixups.py fixed - doorbell waits: fixups.py bounds upstream's new spin spelling again - prompt-slot borrowing hangs since upstream's rework: off by default - the Turing PTX branch in qsa_prompt_attn, dp4a.hpp's __nanosleep, the coupled-draft capture signature, a duplicate kernel name - gr_parity: the single-token GR read runs the multi kernels (bitwise) - qsa_parity: a cross-queue copy race in the test itself Decode 78.2 / 75.7 tok/s (Coder), 58.6 / 64.2 (IQ2_XS); 128K-256K complete.
166 lines
7.9 KiB
C++
166 lines
7.9 KiB
C++
// src/kernels/cuda/rope.cu - P2.S2: NEOX partial RoPE.
|
|
//
|
|
// Semantics from `ref/qsa.py` (`rope_freqs` + `rope_neox`), which `bench/micro/rope_xcheck` validates against
|
|
// the oracle:
|
|
//
|
|
// inv[i] = theta ** (-2i / n_rot) i in [0, n_rot/2)
|
|
// ang = pos * inv
|
|
// cos, sin = cos(ang), sin(ang)
|
|
// out[:half] = a*cos - b*sin a = x[:half], b = x[half:n_rot]
|
|
// out[half:n_rot] = a*sin + b*cos
|
|
//
|
|
// THE PAIRING IS NEOX - (i, i + n_rot/2), NOT the adjacent pair (2i, 2i+1). `ggml.h` writes NEOX
|
|
// n_dims=8 as [ccccssss]; the adjacent-pair ("NORMAL") ordering is the other common convention and choosing
|
|
// wrongly rotates the wrong dimensions together, which produces correctly-shaped output with scrambled
|
|
// content. It is also PARTIAL: only the first n_rot of head_dim are touched, here 64 of 256.
|
|
//
|
|
// WHY THE TABLE IS BUILT ON THE HOST. The reference computes the frequencies in FLOAT64 (`theta ** (...)`
|
|
// on a float64 arange, cos/sin in float64). Reproducing that on device means double-precision pow and cos,
|
|
// which are slow on a consumer GPU and - more to the point - not guaranteed to agree with the host's libm.
|
|
// Building the table once on the host and passing it in makes the TABLE exactly the reference's values and
|
|
// leaves the kernel a pure rotation, so each half can be checked tightly instead of both halves sharing one
|
|
// loose tolerance. The engine wants a cached table anyway: it is per (n_rot, theta, position), not per token.
|
|
//
|
|
// THE SCALED TABLE IS THE SAME IDEA WITH MORE MATH IN THE HOST LOOP. Linear rescales the angle by
|
|
// `freq_scale`; YaRN blends extrapolation and interpolation along the pairs (ggml's `rope_yarn`) and folds
|
|
// the mscale magnitude correction into the same cos/sin values. All of it float64, in the reference's
|
|
// order, so `rope_parity` can hold every variant to a bit-exact float64 transcription of the same spec.
|
|
#define DPCT_PROFILING_ENABLED
|
|
#include <sycl/sycl.hpp>
|
|
#include <dpct/dpct.hpp>
|
|
#include "strata/sycl_queue.hpp"
|
|
#include "strata/kernels/rope.hpp"
|
|
#include "strata/kernels/mrope.hpp"
|
|
|
|
#include <cmath>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
|
|
namespace strata::kernels {
|
|
|
|
namespace {
|
|
// The process's rope config (rope_scaling.hpp). One writer - the engine's startup thread, before
|
|
// session_init builds any table or captures any graph - and readers after it.
|
|
RopeScaling g_rope_scaling;
|
|
} // namespace
|
|
|
|
void rope_scaling_set(const RopeScaling& scaling) { g_rope_scaling = scaling; }
|
|
const RopeScaling& rope_scaling() { return g_rope_scaling; }
|
|
|
|
void build_rope_table(int n_rot, double theta, int max_pos, float* cos_tab, float* sin_tab) {
|
|
const int half = n_rot / 2;
|
|
for (int p = 0; p < max_pos; ++p) {
|
|
for (int i = 0; i < half; ++i) {
|
|
// float64 throughout, in the reference's order: inv, then ang, then cos/sin
|
|
const double inv = std::pow(theta, -2.0 * (double) i / (double) n_rot);
|
|
const double ang = (double) p * inv;
|
|
cos_tab[(size_t) p * half + i] = (float) std::cos(ang);
|
|
sin_tab[(size_t) p * half + i] = (float) std::sin(ang);
|
|
}
|
|
}
|
|
}
|
|
|
|
void build_rope_table(int n_rot, const RopeScaling& sc, int max_pos, float* cos_tab, float* sin_tab) {
|
|
if (sc.type == RopeScalingType::None) {
|
|
build_rope_table(n_rot, sc.freq_base, max_pos, cos_tab, sin_tab); // the original loop, verbatim
|
|
return;
|
|
}
|
|
const int half = n_rot / 2;
|
|
const double fs = sc.freq_scale();
|
|
const double ms = sc.mscale();
|
|
double cd[2];
|
|
sc.corr_dims(n_rot, cd);
|
|
const bool correct = sc.ext_factor != 0; // ggml: the correction rides on ext_factor, not the type
|
|
for (int p = 0; p < max_pos; ++p) {
|
|
for (int i = 0; i < half; ++i) {
|
|
const double inv = std::pow(sc.freq_base, -2.0 * (double) i / (double) n_rot);
|
|
const double extrap = (double) p * inv; // the trained angle, ggml's theta_extrap
|
|
const double interp = fs * extrap; // ggml's theta_interp
|
|
double ang = interp;
|
|
if (correct) {
|
|
const double ramp = (double) rope_yarn_ramp((float) cd[0], (float) cd[1], i) * sc.ext_factor;
|
|
ang = interp * (1.0 - ramp) + extrap * ramp;
|
|
}
|
|
cos_tab[(size_t) p * half + i] = (float) (std::cos(ang) * ms);
|
|
sin_tab[(size_t) p * half + i] = (float) (std::sin(ang) * ms);
|
|
}
|
|
}
|
|
}
|
|
|
|
namespace {
|
|
|
|
// ONE THREAD PER ROW, not per (row, pair). The first version had one thread per pair and did the tail copy
|
|
// with hreadIdx.x strided loops - so up to 32 threads wrote into the SAME row, with no barrier between the
|
|
// copy and the rotation, and a thread's copy of orow[i] could land AFTER another thread had rotated it. A
|
|
// race that silently writes the unrotated value is worse than a wrong answer: it is intermittent.
|
|
//
|
|
// A row is head_dim floats (256 here) and there are batch x heads of them, so one thread per row is a
|
|
// handful of threads for a token and removes the ordering question entirely rather than synchronising it.
|
|
__dpct_inline__ void rope_neox_kernel(const float *__restrict__ x,
|
|
float *__restrict__ out, long long rows,
|
|
int head_dim, int n_rot,
|
|
const float *__restrict__ cos_tab,
|
|
const float *__restrict__ sin_tab,
|
|
const int *__restrict__ pos,
|
|
const int32_t *__restrict__ mtab) {
|
|
auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item<3>();
|
|
const long long r =
|
|
(long long)item_ct1.get_group(2) * item_ct1.get_local_range(2) +
|
|
item_ct1.get_local_id(2);
|
|
if (r >= rows) return;
|
|
const int half = n_rot / 2;
|
|
const float* xr = x + r * head_dim;
|
|
float* orow = out + r * head_dim;
|
|
|
|
#pragma unroll
|
|
for (int d = n_rot; d < head_dim; ++d)
|
|
orow[d] = xr[d]; // the PARTIAL rotation's untouched tail
|
|
for (int i = 0; i < half; ++i) {
|
|
const size_t toff = (size_t) mrope_pos(mtab, pos[r], i) * half; // the image path's per-pair position
|
|
// THE PAIRING AND THE SIGNS COME FROM `rope_neox_pair`, shared with the indexer's pooling kernel.
|
|
rope_neox_pair(xr[i], xr[half + i], cos_tab[toff + i], sin_tab[toff + i], orow[i], orow[half + i]);
|
|
}
|
|
}
|
|
|
|
} // namespace
|
|
|
|
void rope_neox_apply(const float* x, float* out, int64_t rows, int head_dim, int n_rot, const float* cos_tab,
|
|
const float* sin_tab, const int* pos, void* stream) {
|
|
if (rows <= 0 || n_rot <= 0) return;
|
|
if (n_rot % 2 != 0 || n_rot > head_dim) {
|
|
std::fprintf(stderr, "rope_neox_apply: n_rot %d must be even and <= head_dim %d\n", n_rot, head_dim);
|
|
std::exit(1);
|
|
}
|
|
const int threads = 128;
|
|
const unsigned grid = (unsigned) ((rows + threads - 1) / threads);
|
|
{
|
|
auto exp_props = sycl::ext::oneapi::experimental::properties{
|
|
sycl::ext::oneapi::experimental::use_root_sync};
|
|
|
|
strata::q_of(stream)
|
|
->submit([&](sycl::handler &cgh) {
|
|
auto mrope_table_ct8 = mrope_table();
|
|
|
|
cgh.parallel_for<
|
|
dpct_kernel_name<class rope_neox_kernel_e291c2>>(
|
|
sycl::nd_range<3>(sycl::range(1, 1, grid) *
|
|
sycl::range(1, 1, threads),
|
|
sycl::range(1, 1, threads)),
|
|
exp_props, [=](sycl::nd_item<3> item_ct1) {
|
|
rope_neox_kernel(x, out, rows, head_dim, n_rot, cos_tab,
|
|
sin_tab, pos, mrope_table_ct8);
|
|
});
|
|
});
|
|
}
|
|
/*
|
|
DPCT1010: SYCL uses exceptions to report errors and does not use the
|
|
error codes. The cudaGetLastError function call was replaced with 0. You
|
|
need to rewrite this code.
|
|
*/
|
|
const dpct::err0 e = 0;
|
|
|
|
if (stream == nullptr) dpct::get_current_device().queues_wait_and_throw();
|
|
}
|
|
|
|
} // namespace strata::kernels
|