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
@@ -86,6 +86,7 @@ int ref_mla_attn(const RadArgs*, RadStream);
|
||||
int ref_gelu_mul (const RadArgs*, RadStream);
|
||||
int ref_act_mul (const RadArgs*, RadStream);
|
||||
int ref_mul_weight (const RadArgs*, RadStream);
|
||||
int ref_seg_mean_bf16_sum (const RadArgs*, RadStream);
|
||||
int ref_seg_mean (const RadArgs*, RadStream);
|
||||
int ref_pool_rows (const RadArgs*, RadStream);
|
||||
int ref_pool_cols (const RadArgs*, RadStream);
|
||||
@@ -362,6 +363,7 @@ REF_SHAPE_FN(ref_gelu_mul);
|
||||
REF_SHAPE_FN(ref_act_mul);
|
||||
REF_SHAPE_FN(ref_mul_weight);
|
||||
REF_SHAPE_FN(ref_seg_mean);
|
||||
REF_SHAPE_FN(ref_seg_mean_bf16_sum);
|
||||
REF_SHAPE_FN(ref_pool_rows);
|
||||
REF_SHAPE_FN(ref_pool_cols);
|
||||
REF_SHAPE_FN(ref_relu);
|
||||
|
||||
@@ -191,3 +191,44 @@ int ref_embed_attn(const RadArgs* a, RadStream) {
|
||||
}
|
||||
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. */
|
||||
int ref_seg_mean_bf16_sum(const RadArgs* a, RadStream) {
|
||||
if (!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 ld_int(cu->dtype, cu->data, 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],
|
||||
round_dt(RAD_BF16, sum) / (float)(at(s + 1) - at(s)));
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
@@ -1179,6 +1179,8 @@ static const RadOpSchema kSchemas[] = {
|
||||
"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." },
|
||||
{ "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] = mean of x's rows [cu[s], cu[s+1]): one row a segment, summed in double and rounded "
|
||||
"once to y's dtype. An empty segment is a zero row." },
|
||||
@@ -2256,6 +2258,8 @@ SIMPLE("ref_mix_rows", "mix_rows", "elem", "a weighted sum of k consecutive rows
|
||||
ref_mix_rows),
|
||||
SIMPLE("ref_chan_stats", "chan_stats", "elem", "per-channel statistics held as weights",
|
||||
DT_FLOAT, ref_chan_stats),
|
||||
SIMPLE("ref_seg_mean_bf16_sum", "seg_mean_bf16_sum", "elem", "BF16 sum/division mean pooling",
|
||||
"bf16 in and out; f32 sum, BF16 sum and quotient", ref_seg_mean_bf16_sum),
|
||||
SIMPLE("ref_seg_mean", "seg_mean", "elem", "mean of each cu_seqlens segment's rows", DT_FLOAT,
|
||||
ref_seg_mean),
|
||||
SIMPLE("ref_pool_rows", "pool_rows", "elem", "mean of every k consecutive rows, then a multiplier",
|
||||
|
||||
@@ -592,6 +592,12 @@ int ref_seg_mean_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out)
|
||||
return o.finish("seg_mean", operand, out);
|
||||
}
|
||||
|
||||
int ref_seg_mean_bf16_sum_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
||||
if (act_dt(p, n_p) != RAD_BF16)
|
||||
return RAD_E_UNSUPPORTED;
|
||||
return ref_seg_mean_shape(p, n_p, operand, out);
|
||||
}
|
||||
|
||||
int ref_pool_rows_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
||||
const uint32_t D = act_dt(p, n_p);
|
||||
const int64_t n = gi(p, n_p, "n", 0), k = gi(p, n_p, "k", 0);
|
||||
|
||||
Reference in new issue
Block a user