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.
This commit is contained in:
keptn42 committed 2026-11-04 22:08:53 +01:00
1 parent 63416510a0
commit 2420b2b383
10 files changed
+42 -21

No files matched your search

+5 -3
View File
@@ -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));