Archived
nemotron 3.5 rerank 8b implementation
This commit is contained in:
1 parent
d773992abc
commit
74e7fddd79
35 files changed
+1788
-90
No files matched your search
@@ -65,6 +65,9 @@ std::vector<Case> build_cases() {
|
||||
add("mul_weight", { HI("M", 9), HI("n", 645), HS("dtype", dt), HI("scalar", 1) },
|
||||
EB(9, 645, w, 2));
|
||||
add("seg_mean", { HI("M", 23), HI("n", 645), HS("dtype", dt) }, EB(23, 645, w, 1));
|
||||
if (!std::strcmp(dt, "bf16"))
|
||||
add("seg_mean_bf16_sum", { HI("M", 23), HI("n", 645), HS("dtype", dt) },
|
||||
EB(23, 645, w, 1));
|
||||
add("relu", { HI("M", 7), HI("n", 645), HS("dtype", dt) }, EB(7, 645, w, 2));
|
||||
add("clamp", { HI("M", 7), HI("n", 645), HS("dtype", dt) }, EB(7, 645, w, 2));
|
||||
add("rmsnorm", { HI("M", 5), HI("n", 645), HF("eps", 1e-6), HS("dtype", dt),
|
||||
|
||||
@@ -87,6 +87,7 @@ int avx_par_force(int threads);
|
||||
X(scatter_rows) X(ngram_ids) \
|
||||
X(ple_gate) X(ple_conv) X(cast) X(softmax) \
|
||||
X(scale_rows) X(scale) X(fill) X(act_mul) \
|
||||
X(seg_mean_bf16_sum) \
|
||||
X(seg_mean) X(mul_weight) X(pool_rows) X(pool_cols) \
|
||||
X(relu) X(clamp) X(conv2d) X(conv1d_dw) \
|
||||
X(rel_sinusoid) X(seg_sinusoid) X(conv2d_unfold) \
|
||||
|
||||
@@ -188,3 +188,44 @@ AVX_KERNEL(embed_attn) {
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
/* Native masked mean: the sum is stored in BF16 before dividing, then stored in BF16 again.
|
||||
* Keep this separate from seg_mean's one-rounding sentence-embedding contract. */
|
||||
AVX_KERNEL(seg_mean_bf16_sum) {
|
||||
if (!rad_args_have(a, 3))
|
||||
return RAD_E_INVAL;
|
||||
const RadTensor *x = &a->t[0], *cu = &a->t[1], *y = &a->t[2];
|
||||
if (x->dtype != RAD_BF16 || y->dtype != RAD_BF16 || cu->dtype != RAD_I32)
|
||||
return RAD_E_DTYPE;
|
||||
if (!x->data || !cu->data || !y->data)
|
||||
return RAD_E_INVAL;
|
||||
const int64_t M = x->rank == 2 ? x->shape[0] : 0, N = p_int(a, "n", 0);
|
||||
const int64_t S = cu->rank == 1 && cu->shape[0] > 1 ? cu->shape[0] - 1 : 0;
|
||||
const auto matrix = [](const RadTensor& t, int64_t rows, int64_t cols) {
|
||||
constexpr int64_t limit = INT64_MAX / 2;
|
||||
if (t.rank != 2 || t.shape[0] != rows || t.shape[1] != cols || t.stride[0] <= 0 ||
|
||||
t.stride[1] <= 0 || cols - 1 > limit / t.stride[1])
|
||||
return false;
|
||||
const int64_t columns = (cols - 1) * t.stride[1];
|
||||
return t.stride[0] > columns && rows - 1 <= (limit - columns) / t.stride[0];
|
||||
};
|
||||
if (M <= 0 || M > INT32_MAX || M > p_int(a, "M", 0) || N <= 0 || S <= 0 || S > M ||
|
||||
cu->stride[0] <= 0 || cu->stride[0] > (INT64_MAX / 4) / S || !matrix(*x, M, N) ||
|
||||
!matrix(*y, S, N))
|
||||
return RAD_E_SHAPE;
|
||||
const auto at = [&](int64_t s) { return ldi(cu, s * cu->stride[0]); };
|
||||
if (at(0) != 0 || at(S) != M)
|
||||
return RAD_E_INVAL;
|
||||
for (int64_t s = 0; s < S; ++s)
|
||||
if (at(s) < 0 || at(s + 1) <= at(s) || at(s + 1) > M)
|
||||
return RAD_E_INVAL;
|
||||
for (int64_t s = 0; s < S; ++s)
|
||||
for (int64_t c = 0; c < N; ++c) {
|
||||
float sum = 0;
|
||||
for (int64_t r = at(s); r < at(s + 1); ++r)
|
||||
sum += ldt(x, r * x->stride[0] + c * x->stride[1]);
|
||||
stt(y, s * y->stride[0] + c * y->stride[1],
|
||||
narrow1(RAD_BF16, sum) / (float)(at(s + 1) - at(s)));
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
@@ -961,6 +961,8 @@ static const RadOpSchema kSchemas[] = {
|
||||
"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." },
|
||||
{ "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),
|
||||
"y[s] = the mean of x's rows in cu_seqlens segment s, summed in double, rounded once." },
|
||||
{ "relu", ARR(pMnD), ARR(oUnary), "y = max(x, 0)." },
|
||||
@@ -1580,6 +1582,8 @@ SIMPLE("avx_mul", "mul", "elem", "elementwise product, broadcasting b", DT_FLOAT
|
||||
SIMPLE("avx_act_mul", "act_mul", "elem", "act(a) * b over two operands", DT_FLOAT, L(act_mul)),
|
||||
SIMPLE("avx_mul_weight", "mul_weight", "elem", "x * a scalar or one-row weight", DT_FLOAT,
|
||||
L(mul_weight)),
|
||||
SIMPLE("avx_seg_mean_bf16_sum", "seg_mean_bf16_sum", "elem", "BF16 sum/division mean pooling",
|
||||
"bf16 in and out; f32 sum, BF16 sum and quotient", L(seg_mean_bf16_sum)),
|
||||
SIMPLE("avx_seg_mean", "seg_mean", "elem", "mean of each cu_seqlens segment's rows", DT_FLOAT,
|
||||
L(seg_mean)),
|
||||
SIMPLE("avx_pool_rows", "pool_rows", "elem", "mean of every k consecutive rows, then a multiplier",
|
||||
|
||||
Reference in new issue
Block a user