aboutsummaryrefslogtreecommitdiff
path: root/embed_cuda.cu
diff options
context:
space:
mode:
authorOnur Gündüz <ogunduz@gmail.com>2026-06-15 13:47:23 +0200
committerOnur Gündüz <ogunduz@gmail.com>2026-06-15 13:53:31 +0200
commit458563c163a885e336ef3365922af4d17e578f56 (patch)
tree52c7515d418e60f4923a69b5eade794a221cc5b4 /embed_cuda.cu
parentd58e523c14356ac8a29480528dbceb19290b18c1 (diff)
downloadffwd-458563c163a885e336ef3365922af4d17e578f56.tar.gz
ffwd-458563c163a885e336ef3365922af4d17e578f56.zip
all: add CLS first-token pooling
Diffstat (limited to 'embed_cuda.cu')
-rw-r--r--embed_cuda.cu13
1 files changed, 13 insertions, 0 deletions
diff --git a/embed_cuda.cu b/embed_cuda.cu
index 3100f44..0005a41 100644
--- a/embed_cuda.cu
+++ b/embed_cuda.cu
@@ -618,6 +618,16 @@ last_pool_kernel(float *out, const float *x, const int *offsets, int batch, int
out[(size_t)b * hidden + d] = x[(size_t)last * hidden + d];
}
+/* CLS pooling: gather the first token of each packed sequence. */
+__global__ static void
+first_pool_kernel(float *out, const float *x, const int *offsets, int batch, int hidden) {
+ int b = blockIdx.x;
+ int tid = threadIdx.x;
+ int first = offsets[b];
+ for (int d = tid; d < hidden; d += blockDim.x)
+ out[(size_t)b * hidden + d] = x[(size_t)first * hidden + d];
+}
+
__global__ static void span_pool_kernel(
float *out, const float *x, const int *starts, const int *lens, int n_spans, int hidden) {
int s = blockIdx.x;
@@ -1420,6 +1430,9 @@ int embed_cuda_encode_batch(embed_cuda_ctx_t *ctx,
if (c->pooling_mode == EMBED_POOL_LAST_TOKEN)
last_pool_kernel<<<batch, 256, 0, ctx->stream>>>(ctx->pooled_out, ctx->x, ctx->offsets,
batch, c->hidden_size);
+ else if (c->pooling_mode == EMBED_POOL_CLS)
+ first_pool_kernel<<<batch, 256, 0, ctx->stream>>>(ctx->pooled_out, ctx->x, ctx->offsets,
+ batch, c->hidden_size);
else
mean_pool_kernel<<<batch, 256, 0, ctx->stream>>>(ctx->pooled_out, ctx->x, ctx->offsets,
batch, c->hidden_size);