- Batched GEMM via cublasGemmStridedBatchedEx - Causal mask CUDA kernel (F32 + BF16) - Element-wise scale CUDA kernel (F32 + BF16) - attention() composing: batched_matmul + scale + causal_mask + softmax - Fixed to_device/contiguous infinite recursion (GPU contiguous via CPU round-trip) - 5 attention tests passing (max_err < 3e-7 F32) - Total: 61 tests passing across all crates Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
54 lines
1.7 KiB
Plaintext
54 lines
1.7 KiB
Plaintext
#include <cuda_bf16.h>
|
||
|
||
// Apply causal mask: set scores[row][col] = -inf where col > row + offset.
|
||
// offset is used for KV cache: when query starts at position `offset`,
|
||
// we allow attending to positions [0, offset + row].
|
||
// scores: [batch, rows, cols] (flattened batch×heads)
|
||
|
||
__global__ void causal_mask_f32(
|
||
float* __restrict__ scores,
|
||
int rows, int cols, int offset
|
||
) {
|
||
int batch_idx = blockIdx.z;
|
||
int row = blockIdx.y;
|
||
int col = blockIdx.x * blockDim.x + threadIdx.x;
|
||
|
||
if (col < cols && col > row + offset) {
|
||
scores[batch_idx * rows * cols + row * cols + col] = -INFINITY;
|
||
}
|
||
}
|
||
|
||
__global__ void causal_mask_bf16(
|
||
__nv_bfloat16* __restrict__ scores,
|
||
int rows, int cols, int offset
|
||
) {
|
||
int batch_idx = blockIdx.z;
|
||
int row = blockIdx.y;
|
||
int col = blockIdx.x * blockDim.x + threadIdx.x;
|
||
|
||
if (col < cols && col > row + offset) {
|
||
// BF16 doesn't have proper -inf literal, use a very large negative
|
||
scores[batch_idx * rows * cols + row * cols + col] = __float2bfloat16(-1e9f);
|
||
}
|
||
}
|
||
|
||
extern "C" {
|
||
|
||
void launch_causal_mask_f32(void* scores, int batch, int rows, int cols,
|
||
int offset, void* stream) {
|
||
int block = 256;
|
||
dim3 grid((cols + block - 1) / block, rows, batch);
|
||
causal_mask_f32<<<grid, block, 0, (cudaStream_t)stream>>>(
|
||
(float*)scores, rows, cols, offset);
|
||
}
|
||
|
||
void launch_causal_mask_bf16(void* scores, int batch, int rows, int cols,
|
||
int offset, void* stream) {
|
||
int block = 256;
|
||
dim3 grid((cols + block - 1) / block, rows, batch);
|
||
causal_mask_bf16<<<grid, block, 0, (cudaStream_t)stream>>>(
|
||
(__nv_bfloat16*)scores, rows, cols, offset);
|
||
}
|
||
|
||
}
|