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
@@ -54,6 +54,22 @@ __global__ void validate(const int* x, int64_t stride, int64_t plane, int M, int
|
||||
*valid = 1;
|
||||
}
|
||||
|
||||
__global__ void pooled_mean(const uint16_t* x, int64_t xl, int64_t xs, const int* cu, int64_t cs,
|
||||
uint16_t* y, int64_t yl, int64_t ys, int S, int N, const int* valid) {
|
||||
if (*valid != 1)
|
||||
return;
|
||||
const int64_t c = (int64_t)blockIdx.x * NT + threadIdx.x;
|
||||
if (c >= N)
|
||||
return;
|
||||
for (int64_t s = blockIdx.y; s < S; s += gridDim.y) {
|
||||
const int begin = cu[s * cs], end = cu[(s + 1) * cs];
|
||||
float sum = 0;
|
||||
for (int r = begin; r < end; ++r)
|
||||
sum += bf2f(x[(int64_t)r * xl + c * xs]);
|
||||
y[s * yl + c * ys] = f32_to_bf16(narrow(sum) / (float)(end - begin));
|
||||
}
|
||||
}
|
||||
|
||||
__global__ void scale(uint16_t* x, int64_t ld, int64_t cs, const int* pos, int64_t ps, int M, int n,
|
||||
int original, float beta, const int* valid) {
|
||||
if (*valid != 1) return;
|
||||
@@ -286,6 +302,24 @@ extern "C" void r4d_rotary_fini(void* p) {
|
||||
extern "C" int64_t r4d_nemotron_scratch(const RadArgs*) {
|
||||
return 4;
|
||||
}
|
||||
extern "C" int r4d_rad_seg_mean_bf16_sum(const RadArgs* a, RadStream stream) {
|
||||
if (!rad_args_have(a, 3))
|
||||
return RAD_E_INVAL;
|
||||
const auto &x = a->t[0], &cu = a->t[1], &y = a->t[2];
|
||||
const int64_t M = x.rank == 2 ? x.shape[0] : 0, N = r4d_geti_or(a, "n", 0),
|
||||
S = cu.rank == 1 && cu.shape[0] > 1 ? cu.shape[0] - 1 : 0;
|
||||
if (M <= 0 || M > INT_MAX || M > r4d_geti_or(a, "M", 0) || N <= 0 || N > INT_MAX || S <= 0 ||
|
||||
S > INT_MAX || !matrix(x, M, N) || !matrix(y, S, N) || !indices(cu, S + 1))
|
||||
return RAD_E_SHAPE;
|
||||
int rc = prepare(a, stream, cu, (int)M, 2, (int)S, INT_MAX);
|
||||
if (rc < 0)
|
||||
return rc;
|
||||
hipLaunchKernelGGL(pooled_mean, dim3((N + NT - 1) / NT, std::min<int64_t>(S, 65535)), dim3(NT), 0,
|
||||
(hipStream_t)stream, (const uint16_t*)x.data, x.stride[0], x.stride[1],
|
||||
(const int*)cu.data, cu.stride[0], (uint16_t*)y.data, y.stride[0], y.stride[1],
|
||||
(int)S, (int)N, (const int*)a->scratch);
|
||||
return finish();
|
||||
}
|
||||
extern "C" int r4d_rad_position_scale(const RadArgs* a, RadStream stream) {
|
||||
if (!rad_args_have(a, 2)) return RAD_E_INVAL;
|
||||
const auto &x = a->t[0], &p = a->t[1];
|
||||
|
||||
@@ -765,6 +765,7 @@ int r4d_rad_mul (const RadArgs*, RadStream);
|
||||
int r4d_rad_mul_weight (const RadArgs*, RadStream);
|
||||
int r4d_rad_act_mul (const RadArgs*, RadStream);
|
||||
int r4d_rad_seg_mean (const RadArgs*, RadStream);
|
||||
int r4d_rad_seg_mean_bf16_sum(const RadArgs*, RadStream);
|
||||
int r4d_rad_qk_norm_rope (const RadArgs*, RadStream);
|
||||
int r4d_rad_cast_widen (const RadArgs*, RadStream);
|
||||
int r4d_rad_cast_f16 (const RadArgs*, RadStream);
|
||||
|
||||
@@ -2999,6 +2999,8 @@ static const RadOpSchema kSchemas[] = {
|
||||
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."),
|
||||
SCHEMA("seg_mean_bf16_sum", pSegMean, oSegMean,
|
||||
"BF16 masked mean: f32 segment sum rounded to BF16 before division, then BF16 output."),
|
||||
SCHEMA("qk_norm_rope", pQkNormRope, oQkNormRope,
|
||||
"In place over a fused [q | k | v] row: each q and k head RMS-normed against its gain "
|
||||
"(plus wadd) and rounded, then rotated (neox or gptj, rotary_dim of head_dim); with "
|
||||
@@ -5808,6 +5810,14 @@ static const RadKernelInfo kKernels[] = {
|
||||
nullptr, nullptr, r4d_rad_act_mul, nullptr, nullptr, nullptr,
|
||||
r4d_shape_binary },
|
||||
|
||||
{ "seg_mean_bf16_sum", "seg_mean_bf16_sum", "elem",
|
||||
"native BF16 sum/division mean pooling over whole nonempty segments",
|
||||
"M and segment count fit int32; pitched BF16 rows and int32 cumulative lengths",
|
||||
"f32 sum, BF16 sum rounding, BF16 quotient",
|
||||
RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY,
|
||||
ROW(cBf16Row), NOTUNE,
|
||||
nullptr, nullptr, r4d_rad_seg_mean_bf16_sum, r4d_nemotron_scratch },
|
||||
|
||||
{ "seg_mean_bf16", "seg_mean", "elem",
|
||||
"mean pooling: one row a cu_seqlens segment",
|
||||
"any n; 32 columns by 8 row lanes a workgroup, one workgroup row a segment",
|
||||
@@ -10281,6 +10291,7 @@ extern "C" int rad_kernel_concurrent(int i) {
|
||||
"kv_resolve",
|
||||
// Each model-local entry validates metadata before its dependent data launch.
|
||||
"position_scale_", "ministral_rope_", "pixtral_rope_", "pixtral_merge_", "embed_attn_",
|
||||
"seg_mean_bf16_sum",
|
||||
};
|
||||
for (const char* p : kStateful)
|
||||
if (std::strncmp(k->name, p, std::strlen(p)) == 0) return 0;
|
||||
|
||||
Reference in new issue
Block a user