Archived
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:
1 parent
63416510a0
commit
2420b2b383
10 files changed
+42
-21
No files matched your search
+4
-3
@@ -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
|
||||
|
||||
@@ -58,7 +58,7 @@ std::vector<Case> 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));
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -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<A>, 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);
|
||||
|
||||
+3
-3
@@ -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."),
|
||||
|
||||
@@ -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<RadTensor> ts = { T(nullptr, RAD_BF16, { 5, 645 }),
|
||||
T(nullptr, RAD_BF16, { 5, 645 }),
|
||||
T(nullptr, RAD_BF16, { 5, 645 }) };
|
||||
|
||||
@@ -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));
|
||||
|
||||
@@ -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),
|
||||
|
||||
+13
-1
@@ -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 });
|
||||
|
||||
Reference in new issue
Block a user