From 2420b2b38303599ea56292f29a0111ffed9fd690 Mon Sep 17 00:00:00 2001 From: keptn42 Date: Sat, 31 Oct 2026 10:46:28 +0000 Subject: [PATCH] ops: act_mul's relu, relu(x)^2 over one tensor act_mul's `act` takes "relu": with one tensor in both operands it is relu(x)^2, Nemotron-H's ungated expert activation and its vision projector's. libref, libavx and libr4d. --- docs/OPS.md | 7 ++++--- libavx/avx_cases.cpp | 2 +- libavx/avx_elementwise.cpp | 5 +++-- libavx/avx_registry.cpp | 3 ++- libr4d/r4d_encoder_bf16.hip | 9 ++++++--- libr4d/r4d_rows.cpp | 6 +++--- libr4d/r4d_selftest.cpp | 2 +- libref/ref_elementwise.cpp | 8 +++++--- libref/ref_registry.cpp | 7 ++++--- tests/ref_test.cpp | 14 +++++++++++++- 10 files changed, 42 insertions(+), 21 deletions(-) diff --git a/docs/OPS.md b/docs/OPS.md index 3a47167c..dc5dce71 100644 --- a/docs/OPS.md +++ b/docs/OPS.md @@ -293,9 +293,10 @@ weight. **`act_mul`** is `silu_mul` with its halves in two operands, so the multiplier can be any tensor: two column slices of a fused `gate_up`, or a gate and a separate per-layer input (EmbeddingGemma 2's per-layer-embedding block multiplies `gelu_tanh(gate(x))` by one). `act` is `silu`, `gelu` (exact -erf), `gelu_tanh`, `sigmoid` (a GLU: `a` the gate half, `b` the value half) or `quick_gelu` -(`sigmoid` of the bf16-rounded `1.702 a`, the CLIP tower's fused form). The activation is -rounded to `y`'s dtype before the product, as the unfused pair stores it. +erf), `gelu_tanh`, `sigmoid` (a GLU: `a` the gate half, `b` the value half), `quick_gelu` +(`sigmoid` of the bf16-rounded `1.702 a`, the CLIP tower's fused form) or `relu`, which over one +tensor in both operands is `relu(x)²` (Nemotron-H's ungated experts). The activation is rounded +to `y`'s dtype before the product, as the unfused pair stores it. **`seg_mean`** is mean pooling over a ragged batch: row `s` of `y` is the mean of `x`'s rows `[cu[s], cu[s+1])`, summed in double and rounded once. `M` is the step's token count and bands the diff --git a/libavx/avx_cases.cpp b/libavx/avx_cases.cpp index 67ab11c0..0b92683f 100644 --- a/libavx/avx_cases.cpp +++ b/libavx/avx_cases.cpp @@ -58,7 +58,7 @@ std::vector build_cases() { add("scale", { HI("M", 6), HI("n", 645), HS("dtype", dt) }, EB(6, 645, w, 2)); add("softmax", { HI("M", 6), HI("n", 645), HS("dtype", dt) }, EB(6, 645, w, 2)); add("gather_rows", { HI("M", 6), HI("n", 640), HS("dtype", dt) }, EB(6, 640, w, 2)); - for (const char* act : { "silu", "gelu", "gelu_tanh", "sigmoid", "quick_gelu" }) + for (const char* act : { "silu", "gelu", "gelu_tanh", "sigmoid", "quick_gelu", "relu" }) add("act_mul", { HI("M", 6), HI("n", 645), HS("act", act), HS("dtype", dt) }, EB(6, 645, w, 3)); add("mul_weight", { HI("M", 9), HI("n", 645), HS("dtype", dt) }, EB(9, 645, w, 2)); diff --git a/libavx/avx_elementwise.cpp b/libavx/avx_elementwise.cpp index c924e6f9..197c3a7f 100644 --- a/libavx/avx_elementwise.cpp +++ b/libavx/avx_elementwise.cpp @@ -133,6 +133,7 @@ static inline float act_of(int act, float g, uint32_t dt) { if (act == 0) return g / (1.0f + sc_exp(-g)); if (act == 1) return 0.5f * g * (1.0f + sc_erf(g * 0.70710678118654752f)); if (act == 3) return 1.0f / (1.0f + sc_exp(-g)); + if (act == 5) return g > 0.0f ? g : 0.0f; if (act == 4) // quick_gelu: sigmoid of the 1.702*g the unfused mul would have stored return 1.0f / (1.0f + sc_exp(-round1(dt, g * round1(RAD_BF16, 1.702f)))); const float k0 = 0.7978845608028654f, k1 = 0.044715f; @@ -169,8 +170,8 @@ AVX_KERNEL(act_mul) { if (n <= 0) return RAD_E_SHAPE; const int64_t rows = rows_of(Y, n); if (rows <= 0 || numel(A) < rows * n || numel(B) < rows * n) return RAD_E_SHAPE; - static const char* kActs[] = { "silu", "gelu", "gelu_tanh", "sigmoid", "quick_gelu" }; - const int act = p_enum(a, "act", kActs, 5, -1); + static const char* kActs[] = { "silu", "gelu", "gelu_tanh", "sigmoid", "quick_gelu", "relu" }; + const int act = p_enum(a, "act", kActs, 6, -1); if (act < 0) return RAD_E_INVAL; AVX_PARALLEL_FOR_IF(rows * n >= AVX_PAR_MIN) diff --git a/libavx/avx_registry.cpp b/libavx/avx_registry.cpp index 39f7961b..22242e45 100644 --- a/libavx/avx_registry.cpp +++ b/libavx/avx_registry.cpp @@ -1023,7 +1023,8 @@ static const RadOpSchema kSchemas[] = { { "mul_weight", ARR(pMulWeight), ARR(oMulWeight), "y = x * w, `w` a weight of one element or one row, broadcast; f32 product rounded once." }, { "act_mul", ARR(pActMul), ARR(oBinary), - "y = act(a) * b, act silu / gelu / gelu_tanh, the activation rounded to y's dtype first." }, + "y = act(a) * b, act silu / gelu / gelu_tanh / sigmoid / quick_gelu / relu, the activation " + "rounded to y's dtype first." }, { "seg_mean_bf16_sum", ARR(pSegMean), ARR(oSegMean), "BF16 masked mean: f32 segment sum rounded to BF16 before division, then BF16 output." }, { "seg_mean", ARR(pSegMean), ARR(oSegMean), diff --git a/libr4d/r4d_encoder_bf16.hip b/libr4d/r4d_encoder_bf16.hip index 9fcc179d..30a3eb81 100644 --- a/libr4d/r4d_encoder_bf16.hip +++ b/libr4d/r4d_encoder_bf16.hip @@ -28,6 +28,7 @@ __device__ __forceinline__ float act_of(float g) { if (ACT == 0) return g / (1.0f + expf(-g)); if (ACT == 1) return 0.5f * g * (1.0f + erff(g * 0.70710678118654752f)); if (ACT == 3) return 1.0f / (1.0f + expf(-g)); + if (ACT == 5) return fmaxf(g, 0.0f); if (ACT == 4) // quick_gelu: sigmoid of the bf16-rounded 1.702*g, as the unfused mul stores it return 1.0f / (1.0f + expf(-bf2f(f32_to_bf16(g * bf2f(f32_to_bf16(1.702f)))))); return 0.5f * g * (1.0f + tanhf(0.7978845608028654f * (g + 0.044715f * g * g * g))); @@ -782,11 +783,11 @@ __global__ __launch_bounds__(kLrThreads) void attn_local_rel_kernel( // ============================================================== launchers -// act: 0 silu, 1 gelu (erf), 2 gelu_tanh, 3 sigmoid, 4 quick_gelu (sigmoid(bf16(1.702*a))). +// act: 0 silu, 1 gelu (erf), 2 gelu_tanh, 3 sigmoid, 4 quick_gelu (sigmoid(bf16(1.702*a))), 5 relu. extern "C" int r4d_act_mul_bf16(long a, long b, long y, int M, int n, int act, int a_ld, int b_ld, int y_ld, long stream) { if (!a || !b || !y) return -1; - if (M <= 0 || n <= 0 || act < 0 || act > 4) return -2; + if (M <= 0 || n <= 0 || act < 0 || act > 5) return -2; const dim3 grid((uint32_t)((n + kThreads - 1) / kThreads), grid_rows(M)); #define R4D_AM(A) \ hipLaunchKernelGGL(act_mul_kernel, grid, dim3(kThreads), 0, (hipStream_t)stream, \ @@ -796,7 +797,8 @@ extern "C" int r4d_act_mul_bf16(long a, long b, long y, int M, int n, int act, i else if (act == 1) R4D_AM(1); else if (act == 2) R4D_AM(2); else if (act == 3) R4D_AM(3); - else R4D_AM(4); + else if (act == 4) R4D_AM(4); + else R4D_AM(5); #undef R4D_AM return hipGetLastError() == hipSuccess ? 0 : -4; } @@ -1138,6 +1140,7 @@ extern "C" int r4d_rad_act_mul(const RadArgs* a, RadStream s) { else if (act && !std::strcmp(act, "gelu_tanh")) ai = 2; else if (act && !std::strcmp(act, "sigmoid")) ai = 3; else if (act && !std::strcmp(act, "quick_gelu")) ai = 4; + else if (act && !std::strcmp(act, "relu")) ai = 5; if (ai < 0) return R4D_NO(RAD_E_INVAL); 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); diff --git a/libr4d/r4d_rows.cpp b/libr4d/r4d_rows.cpp index 3f77ec71..5a8f407e 100644 --- a/libr4d/r4d_rows.cpp +++ b/libr4d/r4d_rows.cpp @@ -3185,9 +3185,9 @@ static const RadOpSchema kSchemas[] = { "op, not gelu, because gated feed-forwards are trained with the tanh form and the " "unary gelu is the exact erf one."), SCHEMA("act_mul", pActMul, oBinary, - "y = act(a) * b, the activation (silu, gelu, gelu_tanh, sigmoid or quick_gelu) rounded " - "to y's dtype before " - "the product: silu_mul with its two halves in two operands."), + "y = act(a) * b, the activation (silu, gelu, gelu_tanh, sigmoid, quick_gelu or relu) " + "rounded to y's dtype before the product: silu_mul with its two halves in two " + "operands."), SCHEMA("seg_mean", pSegMean, oSegMean, "Row s of y is the mean of x's rows [cu_seqlens[s], cu_seqlens[s+1]), summed in f64 " "and rounded once; an empty segment is zeros."), diff --git a/libr4d/r4d_selftest.cpp b/libr4d/r4d_selftest.cpp index a3f67ab8..46221cb0 100644 --- a/libr4d/r4d_selftest.cpp +++ b/libr4d/r4d_selftest.cpp @@ -31107,7 +31107,7 @@ static void device_phase(const Plugin& pl) { const RadKernelInfo* r = find(pl, "act_mul_bf16"); if (!r) ok("act_mul_bf16", false, "row absent"); else - for (const char* act : { "silu", "gelu", "gelu_tanh", "sigmoid", "quick_gelu" }) { + for (const char* act : { "silu", "gelu", "gelu_tanh", "sigmoid", "quick_gelu", "relu" }) { std::vector ts = { T(nullptr, RAD_BF16, { 5, 645 }), T(nullptr, RAD_BF16, { 5, 645 }), T(nullptr, RAD_BF16, { 5, 645 }) }; diff --git a/libref/ref_elementwise.cpp b/libref/ref_elementwise.cpp index 84351bb8..43c1f015 100644 --- a/libref/ref_elementwise.cpp +++ b/libref/ref_elementwise.cpp @@ -377,7 +377,8 @@ int ref_silu_mul(const RadArgs* a, RadStream) { /* ------------------------------------------------------------------ act_mul */ /* y = act(a) * b, the gated product with its halves in two operands: two column slices of a fused * gate_up, or a gate whose multiplier is another tensor (Gemma's per-layer-input block multiplies - * gelu(gate(x)) by a per-layer embedding). + * gelu(gate(x)) by a per-layer embedding). With `relu` and the same tensor in both operands it is + * relu(x)^2, Nemotron-H's ungated expert activation. * * The activation is rounded to y's dtype before the product, as the unfused pair stores it. */ int ref_act_mul(const RadArgs* a, RadStream) { @@ -387,8 +388,8 @@ int ref_act_mul(const RadArgs* a, RadStream) { if (n <= 0) return RAD_E_SHAPE; const int64_t rows = rows_of(Y, n); if (rows <= 0 || numel(A) < rows * n || numel(B) < rows * n) return RAD_E_SHAPE; - static const char* kActs[] = { "silu", "gelu", "gelu_tanh", "sigmoid", "quick_gelu" }; - const int act = p_enum(a, "act", kActs, 5, -1); + static const char* kActs[] = { "silu", "gelu", "gelu_tanh", "sigmoid", "quick_gelu", "relu" }; + const int act = p_enum(a, "act", kActs, 6, -1); if (act < 0) return RAD_E_INVAL; REF_PAR_FOR(rows * n) @@ -402,6 +403,7 @@ int ref_act_mul(const RadArgs* a, RadStream) { const float v = act == 0 ? act_silu(g) : act == 1 ? act_gelu(g) : act == 2 ? act_gelu_tanh(g) : act == 3 ? act_sigmoid(g) + : act == 5 ? (g > 0.0f ? g : 0.0f) : act_sigmoid(round_dt(Y->dtype, g * round_dt(RAD_BF16, 1.702f))); stt(Y, yb + i * ys, round_dt(Y->dtype, v) * ldt(B, bb + i * bs)); diff --git a/libref/ref_registry.cpp b/libref/ref_registry.cpp index 38a29535..545e03f2 100644 --- a/libref/ref_registry.cpp +++ b/libref/ref_registry.cpp @@ -1259,9 +1259,10 @@ static const RadOpSchema kSchemas[] = { "y = x * w, `w` a WEIGHT of one element (a scalar for the whole output) or of one row " "(broadcast down the rows): a learned gain on an activation. f32 product, rounded once." }, { "act_mul", ARR(pActMul), ARR(oBinary), - "y = act(a) * b, `act` \"silu\", \"gelu\" (exact erf), \"gelu_tanh\" or \"sigmoid\". The activation is " - "rounded to y's dtype before the product, which is the unfused pair. `a` and `b` may be column " - "slices of one fused row." }, + "y = act(a) * b, `act` \"silu\", \"gelu\" (exact erf), \"gelu_tanh\", \"sigmoid\", \"quick_gelu\" or " + "\"relu\" (relu(x)^2 with one tensor in both operands). The activation is rounded to y's dtype " + "before the product, which is the unfused pair. `a` and `b` may be column slices of one fused " + "row." }, { "seg_mean_bf16_sum", ARR(pSegMean), ARR(oSegMean), "BF16 masked mean: f32 segment sum rounded to BF16 before division, then BF16 output." }, { "seg_mean", ARR(pSegMean), ARR(oSegMean), diff --git a/tests/ref_test.cpp b/tests/ref_test.cpp index 59e3ba5f..58a706db 100644 --- a/tests/ref_test.cpp +++ b/tests/ref_test.cpp @@ -3597,10 +3597,22 @@ TEST(op_act_mul) { CHECK_NEAR(y.at(1), gt(-2) * 4, 1e-5); CHECK_NEAR(y.at(2), gt(0.5f) * -1, 1e-5); CHECK_NEAR(y.at(3), 0.0f, 1e-6); - A bad; bad.t(g).t(u).t(y).i("M", 2).i("n", 2).s("act", "relu").s("dtype", "f32"); + A bad; bad.t(g).t(u).t(y).i("M", 2).i("n", 2).s("act", "tanh").s("dtype", "f32"); CHECK(run("act_mul", bad) < 0); } +/* act_mul's relu over one tensor in both operands: relu(x)^2, zero below. */ +TEST(op_act_mul_relu_of_one_tensor_squares_it) { + T x(RAD_F32, {1, 4}); x.f({ 3, -2, 0.5f, 0 }); + T y(RAD_F32, {1, 4}); + A a; a.t(x).t(x).t(y).i("M", 1).i("n", 4).s("act", "relu").s("dtype", "f32"); + CHECK_OK(run("act_mul", a)); + CHECK_NEAR(y.at(0), 9.0f, 1e-6); + CHECK_NEAR(y.at(1), 0.0f, 1e-6); + CHECK_NEAR(y.at(2), 0.25f, 1e-6); + CHECK_NEAR(y.at(3), 0.0f, 1e-6); +} + /* seg_mean: one row per segment, an empty segment a zero row. */ TEST(op_seg_mean) { T x(RAD_F32, {4, 2}); x.f({ 1, 2, 3, 4, 5, 6, 7, 9 });