Archived
libr4d: hc_project takes one direction for every stream, reduced at the wave's width
The device kernel did not implement the operation core/cvec.h states, in two ways
found with a card probe (one row, n 2560, hc 4, through the plugin's own entry
point):
- it offset the direction by the stream (`v + blockIdx.y * v_ld`, with v_ld
falling back to n), so each hyper-connection stream was projected along a
NEIGHBOUR layer's direction -- rows of the table the caller never named, and
past its end when the range ends at the last row. libref and libavx both use
one v for all streams, as the package's JSON does;
- its block reduction used `__shfl_down(acc, off, 64)` under a 64-lane "wave"
bookkeeping, but gfx12 runs wave32 and nothing sets -mwavefrontsize64, so half
of each 64-group's partials never reached LDS: one application left h.v at
0.45 where the operation leaves 0.
Now one workgroup per (row, stream), the direction read once per stream, and the
reduction is the house one (r4d_wave_sum, a partial a wave in LDS, kWaves 32).
The entry refuses a [hc, n] direction table by name instead of reading it as one
direction a stream.
The kernel moves into the gated residual's unit (r4d_hc_bf16.hip), which already
has kWave/kThreads/bf2f/f2bf and the hc_common operand checks: one file fewer and
89 lines fewer than the standalone unit, and no duplicated reduction or conversion
code.
Measured through r4d_rad_hc_project after the change: each stream's h.v ends at
1e-3..6e-3 (bf16 rounding; rel_l2 1.6e-3 against the operation computed in
double), a row with `on` 0 changes 0 of its 10240 bf16 values, and a [hc, n]
table is refused. ctest -L fast: 80 of 82, the two failures environmental
(videos_test needs FFmpeg, ops_doc needs gawk, absent from rad-toolchain).
This commit is contained in:
1 parent
40f7390766
commit
cac79bb3ba
3 files changed
+57
-147
No files matched your search
@@ -294,6 +294,39 @@ void hc_write_kernel(const unsigned short* __restrict__ y, const unsigned short*
|
||||
}
|
||||
}
|
||||
|
||||
/* ============================================================== hc_project
|
||||
*
|
||||
* The control vector (core/cvec.h): h[s] -= scale * (h[s] . v) * v for each of the hc streams of a
|
||||
* row whose `on` flag is 1 -- llama.cpp's `--cvec-mode project`, issued after a layer's write into
|
||||
* the stream. ONE direction for every stream: the dot is per stream, against the same v. One
|
||||
* workgroup per (row, stream), and a zero flag is not read and not written, so an unprojected
|
||||
* request's stream is the stock model's. */
|
||||
__global__ __launch_bounds__(kThreads)
|
||||
void hc_project_kernel(unsigned short* __restrict__ h, const float* __restrict__ v,
|
||||
const int32_t* __restrict__ on, long rows, long n, long hrow, float scale) {
|
||||
const long r = blockIdx.y;
|
||||
if (r >= rows || on[r] == 0) return; /* a row's switch: the whole workgroup takes it */
|
||||
unsigned short* hp = h + r * hrow + (long)blockIdx.x * n;
|
||||
|
||||
float acc = 0.0f;
|
||||
for (long i = threadIdx.x; i < n; i += kThreads) acc += bf2f(hp[i]) * v[i];
|
||||
/* A wave sums itself and the waves meet in LDS, at kWave 32: the shuttle is the wave's own
|
||||
* width here, not the 64 a 64-wide __shfl_down would assume but gfx12 does not run. */
|
||||
__shared__ float s_g[kWaves];
|
||||
const float w = r4d_wave_sum(acc);
|
||||
if ((threadIdx.x % kWave) == 0) s_g[threadIdx.x / kWave] = w;
|
||||
__syncthreads();
|
||||
if (threadIdx.x == 0) {
|
||||
float g = 0.0f;
|
||||
for (int k = 0; k < kWaves; ++k) g += s_g[k];
|
||||
s_g[0] = g * scale;
|
||||
}
|
||||
__syncthreads();
|
||||
|
||||
const float g = s_g[0];
|
||||
for (long i = threadIdx.x; i < n; i += kThreads) hp[i] = f2bf(bf2f(hp[i]) - g * v[i]);
|
||||
}
|
||||
|
||||
/* A wave's dot product of the LDS vector with a contiguous bf16 row, using 512-byte loads but
|
||||
* summing in the scalar form's order.
|
||||
*
|
||||
@@ -3241,6 +3274,30 @@ extern "C" int r4d_rad_hc_write(const RadArgs* a, RadStream stream) {
|
||||
(long)rows, (long)n, (int)hc, (long)yrow, (long)irow, (long)hrow);
|
||||
}
|
||||
|
||||
enum { HCP_H = 0, HCP_V, HCP_ON };
|
||||
|
||||
/* The control vector's projection. `v` is one layer's unit direction, [n], used for EVERY stream
|
||||
* (the dot is per stream against the same direction): a [hc, n] table is refused here rather than
|
||||
* read as one direction a stream, which is what a per-stream offset would silently do. */
|
||||
extern "C" int r4d_rad_hc_project(const RadArgs* a, RadStream stream) {
|
||||
const RadTensor* h = r4d_opt(a, HCP_H);
|
||||
const RadTensor* v = r4d_opt(a, HCP_V);
|
||||
const RadTensor* on = r4d_opt(a, HCP_ON);
|
||||
if (!h || !v || !on) return R4D_NO(RAD_E_INVAL);
|
||||
R4D_DT(h, RAD_BF16); R4D_DT(v, RAD_F32); R4D_DT(on, RAD_I32);
|
||||
long long rows = 0, n = 0, hc = 0, hrow = 0;
|
||||
const int rc = hc_common(a, h, &rows, &n, &hc, &hrow);
|
||||
if (rc != RAD_OK) return rc;
|
||||
long long vrows = 0, onrows = 0;
|
||||
if (r4d_rows_pitch(v, n, &vrows) < 0 || vrows != 1) return R4D_NO(RAD_E_STRIDE);
|
||||
if (r4d_rows_pitch(on, 1, &onrows) < 0 || onrows < rows) return R4D_NO(RAD_E_STRIDE);
|
||||
|
||||
return R4D_EMIT(stream, hc_project_kernel, dim3((unsigned)hc, (unsigned)rows), dim3(kThreads),
|
||||
0, (unsigned short*)h->data, (const float*)v->data,
|
||||
(const int32_t*)on->data, (long)rows, (long)n, (long)hrow,
|
||||
(float)r4d_getf(a, "scale", 1.0));
|
||||
}
|
||||
|
||||
enum { MTE_H = 0, MTE_E, MTE_WH, MTE_WE, MTE_FCH, MTE_FCE, MTE_X };
|
||||
|
||||
extern "C" int r4d_rad_mtp_enter(const RadArgs* a, RadStream stream) {
|
||||
|
||||
Reference in new issue
Block a user