F-2: a half's hyper-connection write fused with the next half's norm (bit-identical; with F-1 +4.0% at 32K, +3.3% at 128K)
This commit is contained in:
1 parent
2a6bbc7304
commit
3945aac617
3 files changed
+61
-3
No files matched your search
@@ -19,6 +19,10 @@ void gr_norm_rs(const float* R, const float* w_norm, float eps, float* rs, uint1
|
||||
/// gr_mix with xn recomputed from R, rs and w_norm exactly as gr_norm computes it (the same bits).
|
||||
void gr_mix_r(const float* R, const float* rs, const float* w_norm, const float* gated, float* mixed, uint16_t* mixed16,
|
||||
int64_t T, void* stream, uint16_t* mixed_h = nullptr);
|
||||
/// F-2: gr_write, then gr_norm_rs of the next half (its norm weights) over the rows just written - the same bits as
|
||||
/// the two calls, without reading R back.
|
||||
void gr_write_norm_rs(float* R, const float* bo, const float* inj, int64_t inj_ld, const float* w_norm_next, float eps,
|
||||
float* rs, uint16_t* xn16, int64_t T, void* stream);
|
||||
/// lo16[t, k] = bf16(silu(lo[t, k] / hc))
|
||||
void gr_silu(const float* lo, uint16_t* lo16, int64_t T, void* stream);
|
||||
/// mixed[t, d] = mean_c xn[t, c, d] * sigmoid(gated[t, c, d]); FP32, BF16 and FP16 (either image may be null).
|
||||
|
||||
@@ -102,6 +102,36 @@ __global__ void gr_mix_r_kernel(const float* __restrict__ R, const float* __rest
|
||||
if (mixed16) mixed16[i] = bf(s);
|
||||
if (mixed_h) mixed_h[i] = hf(s);
|
||||
}
|
||||
// F-2: gr_write_kernel for one row (t, c), then gr_norm_rs_kernel's reduction over it with the next half's norm
|
||||
// weights - the same thread-to-element mapping (256 threads, stride 256) and block_sum, so rs and the BF16 image are
|
||||
// the same bits, and R is not read back
|
||||
constexpr int GRW_PER = (N + 255) / 256;
|
||||
__global__ void __launch_bounds__(256) gr_write_norm_rs_kernel(float* __restrict__ R, const float* __restrict__ bo,
|
||||
const float* __restrict__ inj, int64_t inj_ld,
|
||||
const float* __restrict__ w, float eps,
|
||||
float* __restrict__ rs_out, uint16_t* __restrict__ xn16) {
|
||||
__shared__ float sh[32];
|
||||
const int64_t row = blockIdx.x; // t * 4 + c
|
||||
const int64_t t = row / HC;
|
||||
const int c = (int) (row % HC);
|
||||
float* r = R + row * N;
|
||||
const float sc = 2.0f * sigm(inj[t * inj_ld + c] / (float) HC);
|
||||
float v[GRW_PER];
|
||||
float ss = 0.0f;
|
||||
int k = 0;
|
||||
#pragma unroll
|
||||
for (int d = threadIdx.x; d < N; d += 256, ++k) {
|
||||
const float x = fmaf(bo[t * N + d], sc, r[d]);
|
||||
r[d] = x;
|
||||
v[k] = x;
|
||||
ss += x * x;
|
||||
}
|
||||
const float rs = rsqrtf(block_sum(ss, sh) / (float) N + eps);
|
||||
if (threadIdx.x == 0) rs_out[row] = rs;
|
||||
k = 0;
|
||||
#pragma unroll
|
||||
for (int d = threadIdx.x; d < N; d += 256, ++k) xn16[row * N + d] = bf(v[k] * rs * w[c * N + d]);
|
||||
}
|
||||
__global__ void gr_silu_kernel(const float* __restrict__ lo, uint16_t* __restrict__ lo16, int64_t n) {
|
||||
const int64_t i = (int64_t) blockIdx.x * blockDim.x + threadIdx.x;
|
||||
if (i >= n) return;
|
||||
@@ -561,6 +591,12 @@ void gr_mix_r(const float* R, const float* rs, const float* w_norm, const float*
|
||||
mixed_h);
|
||||
check("gr_mix_r");
|
||||
}
|
||||
void gr_write_norm_rs(float* R, const float* bo, const float* inj, int64_t inj_ld, const float* w_norm_next, float eps,
|
||||
float* rs, uint16_t* xn16, int64_t T, void* stream) {
|
||||
gr_write_norm_rs_kernel<<<(unsigned) (T * HC), 256, 0, (cudaStream_t) stream>>>(R, bo, inj, inj_ld, w_norm_next, eps,
|
||||
rs, xn16);
|
||||
check("gr_write_norm_rs");
|
||||
}
|
||||
void gr_silu(const float* lo, uint16_t* lo16, int64_t T, void* stream) {
|
||||
gr_silu_kernel<<<blocks_for(T * LR), 256, 0, (cudaStream_t) stream>>>(lo, lo16, T * LR);
|
||||
check("gr_silu");
|
||||
|
||||
+21
-3
@@ -1039,6 +1039,7 @@ bool Prefill::run(const int64_t* tokens, int64_t n, int64_t pos0, std::string& e
|
||||
else issue_until(upto + (size_t) m.ring);
|
||||
};
|
||||
host_setup_ms += ms_since(tsetup);
|
||||
bool normed = false; // F-2: the previous half's write already normed R for this half (grs, xn16)
|
||||
for (int64_t l = LB; l < LE; ++l) {
|
||||
core::progress_beat(); // the serve watchdog: a prompt chunk of 8192 tokens is still moving
|
||||
const core::LayerView v(*m.wt, l);
|
||||
@@ -1104,7 +1105,8 @@ bool Prefill::run(const int64_t* tokens, int64_t n, int64_t pos0, std::string& e
|
||||
if (!wn || !wd || !wu || !wi) return false;
|
||||
pt.mark(kPfHc, cs);
|
||||
if (gr_unfused()) gr_norm(m.R, (const float*) wn->data, EPS, m.xn, m.xn16, T, m.cs);
|
||||
else gr_norm_rs(m.R, (const float*) wn->data, EPS, m.grs, m.xn16, T, m.cs);
|
||||
else if (!normed) gr_norm_rs(m.R, (const float*) wn->data, EPS, m.grs, m.xn16, T, m.cs);
|
||||
normed = false;
|
||||
if (!bf16_proj(m.gemm, wd, m.xn16, m.lo, T, sd, err)) return false;
|
||||
gr_silu(m.lo, m.lo16, T, m.cs);
|
||||
if (!bf16_proj(m.gemm, wu, m.lo16, m.gated, T, su, err)) return false;
|
||||
@@ -1589,8 +1591,24 @@ bool Prefill::run(const int64_t* tokens, int64_t n, int64_t pos0, std::string& e
|
||||
}
|
||||
}
|
||||
}
|
||||
// ---- the hyper-connection write of this half
|
||||
gr_write(m.R, m.bo, m.inj, HC, T, m.cs);
|
||||
// ---- the hyper-connection write of this half; F-2: fused with the next half's norm when nothing else
|
||||
// touches R in between (not the stage's last half, not before the PLE block of layer 1, not under a
|
||||
// control vector)
|
||||
const int64_t nl = half == 0 ? l : l + 1;
|
||||
const bool fuse = !gr_unfused() && nl < LE && !(half == 1 && nl == 1 && ple_on) &&
|
||||
!(half == 1 && strata::kernels::cvec().covers(l));
|
||||
const core::WeightRef* wnn = nullptr;
|
||||
if (fuse) {
|
||||
const core::LayerView vn(*m.wt, nl);
|
||||
wnn = need(vn, half == 0 ? "hc_ffn_norm.weight" : "hc_attn_norm.weight", err);
|
||||
if (!wnn) return false;
|
||||
}
|
||||
if (wnn) {
|
||||
gr_write_norm_rs(m.R, m.bo, m.inj, HC, (const float*) wnn->data, EPS, m.grs, m.xn16, T, m.cs);
|
||||
normed = true;
|
||||
} else {
|
||||
gr_write(m.R, m.bo, m.inj, HC, T, m.cs);
|
||||
}
|
||||
if (half == 1 && strata::kernels::cvec().covers(l)) // --control-vector-scaled
|
||||
strata::kernels::cvec_apply(m.R, l, T, D, nullptr, 0, nullptr, 0, false, m.cs);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user