Archived
libr4d: a fill op
y = value over an [M, n] extent at a row pitch, bf16, default 0. The op a rank issues where it has nothing to compute and a reader downstream -- an all-reduce, a residual -- still expects a defined buffer: a wire shared across layers cannot be zeroed once a step, and a memset knows neither the pitch nor the dtype. Smoke-tested for the exact zero, the pitch, and the RNE of a value.
This commit is contained in:
1 parent
fd54038b8c
commit
cd48c61daf
6 files changed
+115
No files matched your search
@@ -860,6 +860,7 @@ int r4d_rmsnorm_bf16(long x, long w, long y, int M, int n, float eps, float wadd
|
||||
int r4d_binary_bf16(long a, long b, long y, int M, int n, int op,
|
||||
int a_ld, int b_ld, int y_ld, long stream);
|
||||
int r4d_sigmoid_bf16(long x, long y, int M, int n, int x_ld, int y_ld, long stream);
|
||||
int r4d_fill_bf16(long y, int M, int n, int y_ld, float value, long stream);
|
||||
// y[.., i] = silu(gu[.., i]) * gu[.., n + i]. The GATE IS THE FIRST HALF -- the order rad-convert
|
||||
// fuses gate_proj and up_proj in.
|
||||
int r4d_silu_mul_bf16(long gu, long y, int M, int n, int gu_ld, int y_ld, long stream);
|
||||
|
||||
@@ -725,6 +725,32 @@ extern "C" int r4d_sigmoid_bf16(long x, long y, int M, int n, int x_ld, int y_ld
|
||||
return hipGetLastError() == hipSuccess ? 0 : -4;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------- fill
|
||||
// y[m, i] = v for every element of an [M, n] extent with a row pitch -- the op a rank issues where
|
||||
// it has nothing to compute and a downstream reader (an all-reduce, a residual) still expects a
|
||||
// defined buffer. A memset cannot do this: the wire may have a pitch and bf16 is two bytes.
|
||||
__global__ __launch_bounds__(kThreads) void fill_kernel(uint16_t* __restrict__ y, uint32_t n,
|
||||
uint32_t y_ld, uint16_t v) {
|
||||
uint16_t* __restrict__ yr = y + (size_t)blockIdx.x * y_ld;
|
||||
for (uint32_t i = threadIdx.x; i < n; i += kThreads) yr[i] = v;
|
||||
}
|
||||
|
||||
// The host form of r4d_common.h's f32_to_bf16 (which is __device__): the same round-to-nearest-even.
|
||||
static inline uint16_t f32_to_bf16_host(float f) {
|
||||
uint32_t u;
|
||||
std::memcpy(&u, &f, sizeof u);
|
||||
return (uint16_t)((u + 0x7fffu + ((u >> 16) & 1u)) >> 16);
|
||||
}
|
||||
|
||||
extern "C" int r4d_fill_bf16(long y, int M, int n, int y_ld, float value, long stream) {
|
||||
if (!y) return -1;
|
||||
if (M <= 0 || n <= 0) return -2;
|
||||
if (!y_ld) y_ld = n;
|
||||
hipLaunchKernelGGL(fill_kernel, dim3((uint32_t)M), dim3(kThreads), 0, (hipStream_t)stream,
|
||||
(uint16_t*)y, (uint32_t)n, (uint32_t)y_ld, f32_to_bf16_host(value));
|
||||
return hipGetLastError() == hipSuccess ? 0 : -4;
|
||||
}
|
||||
|
||||
extern "C" int r4d_silu_mul_bf16(long gu, long y, int M, int n, int gu_ld, int y_ld, long stream) {
|
||||
if (!gu || !y) return -1;
|
||||
if (M <= 0 || n <= 0) return -2;
|
||||
@@ -1258,6 +1284,23 @@ extern "C" int r4d_rad_sigmoid(const RadArgs* a, RadStream s) {
|
||||
(long)(uintptr_t)s) == 0 ? RAD_OK : RAD_E_DEVICE;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ fill */
|
||||
|
||||
extern "C" int r4d_rad_fill(const RadArgs* a, RadStream s) {
|
||||
const RadTensor* y = r4d_opt(a, 0);
|
||||
if (!y) return R4D_NO(RAD_E_INVAL);
|
||||
R4D_NEED(dt_bf16(y));
|
||||
const long long n = r4d_geti_or(a, "n", r4d_dim(y, (int)y->rank - 1));
|
||||
if (n <= 0) return R4D_NO(RAD_E_SHAPE);
|
||||
long long M = 0;
|
||||
const long long y_ld = r4d_rows_pitch(y, n, &M);
|
||||
if (y_ld < 0) return R4D_NO(RAD_E_STRIDE);
|
||||
if (M <= 0) return R4D_NO(RAD_E_SHAPE);
|
||||
const double v = r4d_getf(a, "value", 0.0);
|
||||
return r4d_fill_bf16(r4d_addr(y), (int)M, (int)n, (int)y_ld, (float)v,
|
||||
(long)(uintptr_t)s) == 0 ? RAD_OK : RAD_E_DEVICE;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ silu_mul */
|
||||
|
||||
extern "C" int r4d_rad_silu_mul(const RadArgs* a, RadStream s) {
|
||||
|
||||
@@ -396,6 +396,7 @@ int r4d_rad_rmsnorm (const RadArgs*, RadStream);
|
||||
int r4d_rad_add (const RadArgs*, RadStream);
|
||||
int r4d_rad_mul (const RadArgs*, RadStream);
|
||||
int r4d_rad_sigmoid (const RadArgs*, RadStream);
|
||||
int r4d_rad_fill (const RadArgs*, RadStream);
|
||||
int r4d_rad_silu_mul (const RadArgs*, RadStream);
|
||||
int r4d_rad_rope (const RadArgs*, RadStream);
|
||||
/* -- the vision tower's ops (r4d_vit_bf16.hip) -- */
|
||||
@@ -456,6 +457,7 @@ int r4d_shape_layernorm (const RadParam*, int, int operand, RadOpdDesc*);
|
||||
int r4d_shape_grid_embed (const RadParam*, int, int operand, RadOpdDesc*);
|
||||
int r4d_shape_gemm_nt_bias (const RadParam*, int, int operand, RadOpdDesc*);
|
||||
int r4d_shape_unary (const RadParam*, int, int operand, RadOpdDesc*);
|
||||
int r4d_shape_fill (const RadParam*, int, int operand, RadOpdDesc*);
|
||||
int r4d_shape_silu_mul (const RadParam*, int, int operand, RadOpdDesc*);
|
||||
int r4d_shape_rope (const RadParam*, int, int operand, RadOpdDesc*);
|
||||
int r4d_shape_kv_store (const RadParam*, int, int operand, RadOpdDesc*);
|
||||
|
||||
@@ -1535,6 +1535,13 @@ static const RadOperandSpec oUnary[] = {
|
||||
{ "x", RAD_OPD_IN, 0 },
|
||||
{ "y", RAD_OPD_OUT, 0 },
|
||||
};
|
||||
/* `fill` has no input: `value` is the whole of what it writes. */
|
||||
static const RadParamSpec pFill[] = {
|
||||
P_INT("M"), P_INT("n"), P_STR("dtype"), P_F64O("value"),
|
||||
};
|
||||
static const RadOperandSpec oFill[] = {
|
||||
{ "y", RAD_OPD_OUT, 0 },
|
||||
};
|
||||
/* `cast`'s two dtype keys are STRINGS and not one `dtype`, because the whole point of the op is
|
||||
* that the two ends differ. ref's spelling, matched field for field so the two plugins agree. */
|
||||
static const RadParamSpec pCast[] = {
|
||||
@@ -1819,6 +1826,10 @@ static const RadOpSchema kSchemas[] = {
|
||||
"y[.., i] = silu(gate_up[.., i]) * gate_up[.., n + i]. The GATE IS THE FIRST HALF, "
|
||||
"which is the order rad-convert fuses gate_proj and up_proj in."),
|
||||
SCHEMA("sigmoid", pMnDtype, oUnary, "y = 1 / (1 + exp(-x))."),
|
||||
SCHEMA("fill", pFill, oFill,
|
||||
"y = value, every element of an [M, n] extent (default 0). The op a rank issues where "
|
||||
"it computes nothing and a reader downstream -- an all-reduce, a residual -- still "
|
||||
"expects a defined buffer: a wire shared across layers cannot be zeroed once a step."),
|
||||
SCHEMA("cast", pCast, oUnary,
|
||||
"y = x, converted. `from` and `to` are for the selector; the bytes are described by the "
|
||||
"tensors' own dtypes and those are what the kernel reads."),
|
||||
@@ -3339,6 +3350,15 @@ static const RadKernelInfo kKernels[] = {
|
||||
nullptr, nullptr, r4d_rad_mul, nullptr, nullptr, nullptr,
|
||||
r4d_shape_binary },
|
||||
|
||||
{ "fill_bf16", "fill", "elem",
|
||||
"constant fill, a row a workgroup",
|
||||
"any M, n; a row pitch is honoured",
|
||||
"bf16 out; the value rounds to nearest even once",
|
||||
RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY,
|
||||
ROW(cBf16Row), NOTUNE,
|
||||
nullptr, nullptr, r4d_rad_fill, nullptr, nullptr, nullptr,
|
||||
r4d_shape_fill },
|
||||
|
||||
{ "sigmoid_bf16", "sigmoid", "elem",
|
||||
"logistic sigmoid, grid-strided over the whole extent",
|
||||
"any extent; contiguous in and out",
|
||||
|
||||
@@ -9853,6 +9853,47 @@ static void device_phase(const Plugin& pl) {
|
||||
}
|
||||
}
|
||||
}
|
||||
/* --- fill: the op a rank with nothing to compute writes a defined buffer with --- */
|
||||
if (sel("fill_bf16")) {
|
||||
const RadKernelInfo* r = find(pl, "fill_bf16");
|
||||
if (!r) ok("fill_bf16", false, "row absent");
|
||||
else {
|
||||
const int64_t M = 5, n = 48, wide = 2 * n; /* a column slice at a wide pitch */
|
||||
void* d = dalloc((size_t)M * wide * 2);
|
||||
if (!d) ok("fill alloc", false, "hipMalloc failed");
|
||||
else {
|
||||
std::vector<uint16_t> pre((size_t)(M * wide), h_to_bf16(-7.5f));
|
||||
st_memcpy(d, pre.data(), (size_t)M * wide * 2, hipMemcpyHostToDevice);
|
||||
RadTensor ty = T(d, RAD_BF16, { M, n });
|
||||
ty.stride[0] = wide;
|
||||
std::vector<RadTensor> ts = { ty };
|
||||
/* Default value: zero. The whole point of the op is that this is exactly 0x0000. */
|
||||
std::vector<RadParam> p0 = { PI("M", M), PI("n", n), PS("dtype", "bf16") };
|
||||
run_case(r, ts, p0);
|
||||
std::vector<uint16_t> got((size_t)(M * wide));
|
||||
st_memcpy(got.data(), d, (size_t)M * wide * 2, hipMemcpyDeviceToHost);
|
||||
bool zero = true, spilled = false;
|
||||
for (int64_t m = 0; m < M; ++m)
|
||||
for (int64_t c = 0; c < wide; ++c) {
|
||||
const uint16_t g = got[(size_t)(m * wide + c)];
|
||||
if (c < n) { if (g != 0) zero = false; }
|
||||
else if (g != pre[0]) spilled = true;
|
||||
}
|
||||
ok("fill_bf16 writes exactly zero into the slice by default", zero);
|
||||
ok("fill_bf16 leaves the columns past the pitch alone", !spilled);
|
||||
/* A value: rounds to nearest even once, like every store in this library. */
|
||||
std::vector<RadParam> p1 = { PI("M", M), PI("n", n), PS("dtype", "bf16"),
|
||||
PF("value", 1.37) };
|
||||
run_case(r, ts, p1);
|
||||
st_memcpy(got.data(), d, (size_t)M * wide * 2, hipMemcpyDeviceToHost);
|
||||
const uint16_t want = h_to_bf16(1.37f);
|
||||
bool val = true;
|
||||
for (int64_t m = 0; m < M && val; ++m)
|
||||
for (int64_t c = 0; c < n; ++c) if (got[(size_t)(m * wide + c)] != want) { val = false; break; }
|
||||
ok("fill_bf16 writes a given value, RNE-rounded", val);
|
||||
}
|
||||
}
|
||||
}
|
||||
/* --- byte rows: the KV tier's transfer pair --- */
|
||||
/* PACK AND UNPACK A POOL, which is the only thing these two are ever asked to do. The layout
|
||||
* here is the real one in miniature: a "layer" is `rows` blocks of `n` bytes, the blocks a
|
||||
|
||||
@@ -528,6 +528,14 @@ extern "C" int r4d_shape_binary(const RadParam* p, int n_p, int operand,
|
||||
R4D_PICK(l);
|
||||
}
|
||||
|
||||
extern "C" int r4d_shape_fill(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
||||
const int64_t M = pi(p, n_p, "M");
|
||||
const int64_t n = pi(p, n_p, "n");
|
||||
const uint32_t D = dt_act(p, n_p, "dtype");
|
||||
const RadOpdDesc l[] = { opd(D, M, n) };
|
||||
R4D_PICK(l);
|
||||
}
|
||||
|
||||
extern "C" int r4d_shape_unary(const RadParam* p, int n_p, int operand,
|
||||
RadOpdDesc* out) {
|
||||
|
||||
|
||||
Reference in new issue
Block a user