26#include <cuda_runtime.h>
32#define CUDA_CHECK(call) gpuErrchk(call)
65 static const bool on = std::getenv(
"EMBER_KERNEL_DEBUG") !=
nullptr;
103#if __CUDA_ARCH__ >= 800
119#if __CUDA_ARCH__ >= 800
153 for (
unsigned int s =
blockDim.x / 2;
s > 0;
s >>= 1) {
197 for (
unsigned int s =
blockDim.x / 2;
s > 0;
s >>= 1) {
315 const float alpha = 1.0f;
316 const float beta = 0.0f;
325 throw std::runtime_error(
"cuBLAS Sgemm failed in matvec_f32_f32_cuda: " +
360 const float alpha = 1.0f;
361 const float beta = 0.0f;
376 Logger::info(
"[CUDA_KERNEL] matvec_bf16_f32_cuda: Tensor Cores requested, but disabling for matrix-vector (N=1) operation. Mixed precision BF16->FP32 with N=1 returns CUBLAS_STATUS_NOT_SUPPORTED in cublasGemmEx.");
384 Logger::warning(
"[CUDA_KERNEL] cublasSetStream failed with status: " + std::to_string(
stream_status) +
". Falling back to FP32 Sgemm.");
389 Logger::info(
"[CUDA_KERNEL] cublasGemmEx parameters: M=" + std::to_string(
rows) +
390 ", N=1, K=" + std::to_string(
cols) +
391 ", LDA=" + std::to_string(
cols) +
392 ", LDB=" + std::to_string(
cols) +
393 ", LDC=" + std::to_string(
rows) +
425 Logger::warning(
"[CUDA_KERNEL] cublasGemmEx with Tensor Cores failed for matvec_bf16_f32_cuda: " + std::to_string(
status) +
". Falling back to FP32 Sgemm.");
441 throw std::runtime_error(
"cublasSetStream failed in matvec_bf16_f32_cuda fallback: " + std::to_string(
stream_status));
464 throw std::runtime_error(
"cuBLAS Sgemm failed in matvec_bf16_f32_cuda fallback: " + std::to_string(
status));
475 throw std::runtime_error(
476 "matvec_f32_f32_cuda (host/host/host): mat size mismatch.");
479 throw std::runtime_error(
480 "matvec_f32_f32_cuda (host/host/host): vec size mismatch.");
524 throw std::runtime_error(
"SiLU CUDA: Input vector size mismatch.");
528 float*
x_dev =
nullptr;
558 for (
unsigned int s =
blockDim.x / 2;
s > 0;
s >>= 1) {
579 for (
unsigned int s =
blockDim.x / 2;
s > 0;
s >>= 1) {
602 throw std::runtime_error(
"Softmax CUDA: Input vector size mismatch.");
610 float*
x_dev =
nullptr;
695 int intermediate_size) {
722 int intermediate_size,
726 if (
num_tokens == 0 || intermediate_size == 0) {
727 Logger::info(
"[SWIGLU_BATCH_CUDA_SKIP] num_tokens or intermediate_size is 0. Nothing to do.");
728 Logger::info(
"[SWIGLU_BATCH_CUDA_EXIT] Skipped operation.");
734 if (
num_tokens > 0 && intermediate_size > 0) {
881 Logger::warning(
"[ROPE_BATCH_CUDA_SKIP] head_dim is 0. Skipping RoPE application.");
886 Logger::info(
"[ROPE_BATCH_CUDA_EXIT] Error: head_dim not even.");
890 Logger::info(
"[ROPE_BATCH_CUDA_EXIT] num_tokens is 0. Nothing to process.");
1150 for (
int d = 0; d <
head_dim; ++d) {
1191 Logger::warning(
"[ATTENTION_SELECTIVE_DEQUANT] Invalid parameters: num_heads=" +
1193 ", head_dim=" + std::to_string(
head_dim));
1284 Logger::info(
"[ADD_RESIDUAL_BATCH_CUDA_SKIP] num_tokens or hidden_size is 0. Nothing to do.");
1285 Logger::info(
"[ADD_RESIDUAL_BATCH_CUDA_EXIT] Skipped operation.");
1360 Logger::error(
"update_kv_cache_cuda: kv_head_idx out of bounds (" +
1366 Logger::error(
"update_kv_cache_cuda: Input K/V vector pointer is null.");
1419 Logger::error(
"rope_and_update_kv_cache_cuda: head_dim must be even.");
1423 Logger::error(
"rope_and_update_kv_cache_cuda: pos out of bounds.");
1427 Logger::error(
"rope_and_update_kv_cache_cuda: kv_head_idx out of bounds.");
1432 "rope_and_update_kv_cache_cuda: Received null device pointer(s).");
1447 int token_id,
int hidden_size,
1448 int vocab_size,
bool is_bf16) {
1451 if (idx >= hidden_size) {
1459 size_t offset = (
size_t)token_id * hidden_size + idx;
1475 int token_id,
int hidden_size,
int vocab_size,
1478 Logger::error(
"lookup_embedding_cuda: Received null device pointer(s).");
1482 if (hidden_size <= 0 || vocab_size <= 0) {
1483 Logger::error(
"lookup_embedding_cuda: Invalid hidden_size or vocab_size.");
1523 for (
unsigned int s =
blockDim.x / 2;
s > 0;
s >>= 1) {
1588 if (num_elements == 0)
return;
1632 if (num_elements == 0)
return;
1663 throw std::runtime_error(
"cublasSetStream failed");
1717 " m=" + std::to_string(
m_user) +
" n=" + std::to_string(
n_user) +
" k=" + std::to_string(
k_user) +
1722 throw std::runtime_error(
"cublasSgemm failed");
1817 for (
int d = 0; d <
head_dim; ++d) {
1840 for (
unsigned int s =
blockDim.x / 2;
s > 0;
s >>= 1) {
1863 for (
unsigned int s =
blockDim.x / 2;
s > 0;
s >>= 1) {
1915 for (
int i = 0;
i < hidden_size; ++
i) {
1921 for (
int i = 0;
i < hidden_size; ++
i) {
1953 for (
unsigned int s =
blockDim.x / 2;
s > 0;
s >>= 1) {
2002 for (
unsigned int s =
blockDim.x / 2;
s > 0;
s >>= 1) {
2039 Logger::info(
"[RMSNORM_BATCH_CUDA_EXIT] num_tokens or hidden_size is 0. Nothing to do.");
2045 int log_count = std::min(3, hidden_size);
2082 int log_count = std::min(3, hidden_size);
2124constexpr int FA_BM = 32;
2125constexpr int FA_BN = 32;
2136 constexpr int D =
DPL * 32;
2162 const int j = idx /
D, d = idx %
D;
2214 const float inv_l = 1.0f /
l[
i];
2252constexpr int FA2_BN = 32;
2260 constexpr int DPT =
D / 4;
2279 const int r = idx /
D, d = idx %
D;
2290 const int pr0 = (
tid / 4) * 2;
2291 const int pc =
tid % 4;
2294 for (
int i = 0;
i < 2; ++
i)
2299 const int sr0 = (
tid / 8) * 4;
2300 const int sc0 = (
tid % 8) * 4;
2305 const int j = idx /
D, d = idx %
D;
2317 for (
int i = 0;
i < 4; ++
i)
2319 for (
int j = 0;
j < 4; ++
j)
s[
i][
j] = 0.0f;
2321 for (
int d = 0; d <
D; ++d) {
2328 for (
int i = 0;
i < 4; ++
i)
2333 for (
int i = 0;
i < 4; ++
i) {
2334 const int r =
sr0 +
i;
2337 for (
int j = 0;
j < 4; ++
j) {
2338 const int key =
kv0 +
sc0 +
j;
2379 for (
int k = 0;
k <
DPT; ++
k) {
2387 for (
int k = 0;
k <
DPT; ++
k) {
2388 const float v =
sV[
j][
pc + 4 *
k];
2397 for (
int i = 0;
i < 2; ++
i) {
2398 const int r =
pr0 +
i;
2420 case 32:
attention_prefill_flash2_kernel<32><<<
grid,
block, 0,
stream>>>(
out, q,
k_cache,
v_cache,
num_tokens,
num_q_heads,
num_kv_heads,
start_pos,
scale);
break;
2421 case 64:
attention_prefill_flash2_kernel<64><<<
grid,
block, 0,
stream>>>(
out, q,
k_cache,
v_cache,
num_tokens,
num_q_heads,
num_kv_heads,
start_pos,
scale);
break;
2422 default:
return false;
2435 static const int mode = [] {
2436 const char*
v = std::getenv(
"EMBER_PREFILL_ATTN");
2437 if (
v ==
nullptr)
return 2;
2438 const std::string
s(
v);
2439 if (
s ==
"baseline")
return 0;
2440 return s ==
"flash" ? 1 : 2;
2456 case 1:
attention_prefill_flash_kernel<1><<<
grid,
block, 0,
stream>>>(
out, q,
k_cache,
v_cache,
num_tokens,
num_q_heads,
num_kv_heads,
start_pos,
scale);
break;
2457 case 2:
attention_prefill_flash_kernel<2><<<
grid,
block, 0,
stream>>>(
out, q,
k_cache,
v_cache,
num_tokens,
num_q_heads,
num_kv_heads,
start_pos,
scale);
break;
2458 case 3:
attention_prefill_flash_kernel<3><<<
grid,
block, 0,
stream>>>(
out, q,
k_cache,
v_cache,
num_tokens,
num_q_heads,
num_kv_heads,
start_pos,
scale);
break;
2459 case 4:
attention_prefill_flash_kernel<4><<<
grid,
block, 0,
stream>>>(
out, q,
k_cache,
v_cache,
num_tokens,
num_q_heads,
num_kv_heads,
start_pos,
scale);
break;
2460 default:
return false;
2485 Logger::info(
"[ATTN_BATCH_PREFILL_CUDA_SKIP] num_tokens_in_batch or head_dim is 0. Nothing to do.");
2486 Logger::info(
"[ATTN_BATCH_PREFILL_CUDA_EXIT] Skipped operation.");
2491 ") or num_q_heads (" + std::to_string(
num_q_heads) +
") for GQA.");
2492 Logger::info(
"[ATTN_BATCH_PREFILL_CUDA_EXIT] Error in head configuration.");
2500 static const bool debug = std::getenv(
"EMBER_PREFILL_DEBUG") !=
nullptr;
2682 Logger::debug(
"[UPDATE_KV_CACHE_BATCH_CUDA_SKIP] Nothing to update (num_tokens_in_batch, head_dim, or num_kv_heads is 0).");
2683 Logger::info(
"[UPDATE_KV_CACHE_BATCH_CUDA_EXIT] Skipped update.");
2689 ") might exceed KV cache capacity (" + std::to_string(
cache_max_seq_len) +
"). Kernel will clip if not careful, or this is an error.");
2759 for (
int offset =
warpSize / 2; offset > 0; offset >>= 1) {
2803 for (
int offset =
warpSize / 2; offset > 0; offset >>= 1) {
2817 for (
int offset =
warpSize / 2; offset > 0; offset >>= 1) {
2876 for (
int d = 0; d <
head_dim; ++d) {
2962 for (
int offset =
warpSize / 2; offset > 0; offset >>= 1) {
3017 for (
int d = 0; d <
head_dim; ++d) {
3166 const int n =
k1 -
k0;
3201 for (
int c = 0;
c <
nc; ++
c) {
3206 for (
int c = 0;
c <
nc; ++
c) {
3208 if (
p[1] > 0.0f)
L +=
expf(
p[0] -
M) *
p[1];
3210 const float inv =
L > 0.0f ? 1.0f /
L : 0.0f;
3213 for (
int c = 0;
c <
nc; ++
c) {
3215 if (
p[1] > 0.0f)
acc +=
expf(
p[0] -
M) *
p[2 + d];
3229 int nc = (max_len + 255) / 256;
3231 const int chunk = (max_len +
nc - 1) /
nc;
3278 Q, K,
V, seq_len,
head_dim,
scale,
num_kv_heads,
num_heads,
mn,
mx,
step_dev);
3290 return decode_attn_tiled(
Q, K,
V,
out,
num_heads, seq_len,
head_dim,
scale, max_len,
num_kv_heads,
step_dev,
3292 decode_attn_split(
Q, K,
V,
out,
num_heads, seq_len,
head_dim,
scale, max_len,
num_kv_heads,
step_dev,
3298 static const int kind = [] {
3299 const char*
e = std::getenv(
"EMBER_DECODE_ATTN");
3300 const std::string
s =
e ?
e :
"";
3301 return s ==
"legacy" ? 2 :
s ==
"split" ? 1 : 0;
3307 static const std::pair<int, int>
t = [] {
3309 const char*
e = std::getenv(
"EMBER_DECODE_ATTN_TILE");
3381 Logger::error(
"cublasSetStream failed in gemm_bf16_bf16_cuda with error: " + std::to_string(
status));
3382 throw std::runtime_error(
"cublasSetStream failed");
3387 ", K=" + std::to_string(
k_user) +
", LDA=" + std::to_string(
lda_user) +
3388 ", LDB=" + std::to_string(
ldb_user) +
", LDC=" + std::to_string(
ldc_user));
3436 Logger::error(
"cublasGemmEx BF16 failed in gemm_bf16_bf16_cuda with error: " + std::to_string(
status));
3438 " m=" + std::to_string(
m_user) +
" n=" + std::to_string(
n_user) +
" k=" + std::to_string(
k_user));
3439 throw std::runtime_error(
"cublasGemmEx BF16 failed");
3442 Logger::info(
"[GEMM_BF16_BF16] SUCCESS with Tensor Cores");
3459 throw std::runtime_error(
"cublasSetStream failed");
3497 Logger::error(
"cublasGemmEx BF16 (A_bf16*B_bf16->C_bf16) failed in gemm_f32_to_bf16_f32_cuda with error: " + std::to_string(
status));
3498 Logger::error(
"GEMM params: transA=" + std::to_string(
opA) +
" transB=" + std::to_string(
opB) +
3499 " m=" + std::to_string(
m_user) +
" n=" + std::to_string(
n_user) +
" k=" + std::to_string(
k_user) +
3503 throw std::runtime_error(
"BF16 Tensor Core GEMM failed");
3536 if (
i >= hidden_size)
return;
3587 if (
i >=
row)
return;
3637 const float one = 1.0f,
zero = 0.0f;
3638 cublasStatus_t s =
cublasSgemm(
h,
CUBLAS_OP_T,
CUBLAS_OP_N,
O,
T,
I, &
one,
W,
I,
X,
I, &
zero,
Y,
O);
3651 for (
int e = 0;
e < E; ++
e) { g[
e] = 0.0f;
s[
e] = 0.0f; }
3652 for (
int j = 0;
j <
k &&
j < E; ++
j) {
3654 for (
int e = 0;
e < E; ++
e)
3659 for (
int e = 0;
e < E; ++
e)
3662 for (
int e = 0;
e < E; ++
e)
3664 for (
int e = 0;
e < E; ++
e)
3671 const float gv = g[
i];
3682 for (
int e = 0;
e < E; ++
e)
3691 static const bool dense = [] {
3692 const char*
e = std::getenv(
"EMBER_MOE_DENSE");
3693 return e && std::string(
e) ==
"1";
3710 const float**
Ag,
const float**
Au,
const float**
Ad,
const float**
Bx,
3711 const float**
Bg,
float**
Cg,
float**
Cu,
float**
Cy) {
3717 for (
int e = 0;
e < E; ++
e)
taken[
e] =
false;
3718 for (
int j = 0;
j <
k; ++
j) {
3720 for (
int e = 0;
e < E; ++
e)
3725 for (
int a = 1;
a <
k; ++
a)
3726 for (
int b =
a;
b > 0 && idx[
b - 1] > idx[
b]; --
b) {
const int tmp = idx[
b]; idx[
b] = idx[
b - 1]; idx[
b - 1] =
tmp; }
3728 for (
int e = 0;
e < E; ++
e)
3731 for (
int e = 0;
e < E; ++
e)
3733 for (
int j = 0;
j <
k; ++
j) {
3734 const int e = idx[
j];
3774 const int tk =
T *
k;
3775 const float**
Ag =
reinterpret_cast<const float**
>(
scratch);
3776 const float**
Au =
Ag +
tk;
3777 const float**
Ad =
Au +
tk;
3778 const float**
Bx =
Ad +
tk;
3779 const float**
Bg =
Bx +
tk;
3780 float**
Cg =
reinterpret_cast<float**
>(
scratch) + 5 * (
size_t)
tk;
3783 float*
logits =
reinterpret_cast<float*
>(
Cy +
tk);
3790 moe_route_sel_kernel<<<T, 32, 0, stream>>>(
logits, E,
k,
renorm ? 1 : 0,
x, d,
d_ff,
gate_exps,
up_exps,
3791 down_exps,
gsel, g,
u,
y,
Ag,
Au,
Ad,
Bx,
Bg,
Cg,
Cu,
Cy);
3792 const float one = 1.0f,
zero = 0.0f;
3794 if (
cublasSgemmBatched(
handle,
CUBLAS_OP_T,
CUBLAS_OP_N,
d_ff, 1, d, &
one,
Ag, d,
Bx, d, &
zero,
Cg,
d_ff,
tk) !=
3796 cublasSgemmBatched(
handle,
CUBLAS_OP_T,
CUBLAS_OP_N,
d_ff, 1, d, &
one,
Au, d,
Bx, d, &
zero,
Cu,
d_ff,
tk) !=
3798 throw std::runtime_error(
"moe_ffn_cuda: cublasSgemmBatched failed");
3801 if (
cublasSgemmBatched(
handle,
CUBLAS_OP_T,
CUBLAS_OP_N, d, 1,
d_ff, &
one,
Ad,
d_ff,
Bg,
d_ff, &
zero,
Cy, d,
tk) !=
3803 throw std::runtime_error(
"moe_ffn_cuda: cublasSgemmBatched failed");
3818 for (
int e = 0;
e < E; ++
e) {
static void debug(const std::string &message)
static void warning(const std::string &message)
static void info(const std::string &message)
static void error(const std::string &message)
void gemm_rm_f32_cuda(cublasHandle_t handle, const float *X, const float *W, float *Y, int T, int O, int I, cudaStream_t stream)
void moe_ffn_cuda(cublasHandle_t handle, const float *x, int T, int d, int d_ff, int E, int k, bool renorm, const float *router, const float *gate_exps, const float *up_exps, const float *down_exps, float *scratch, float *out, cudaStream_t stream)
void decode_embed_cuda(const float *table_dev, float *out_dev, const int *step_dev, int hidden_size, int vocab_size, cudaStream_t stream)
void add_residual_batch_cuda(float *d_output_batch, const float *d_input_a_batch, const float *d_input_b_batch, int num_tokens, int hidden_size, cudaStream_t stream)
__global__ void lookup_embedding_kernel(const void *__restrict__ table_dev, float *__restrict__ output_dev, int token_id, int hidden_size, int vocab_size, bool is_bf16)
void softmax_vector_cuda(const std::vector< float > &x_host, std::vector< float > &out_host, int n)
void matvec_f32_f32_cuda(cublasHandle_t handle, const float *mat_f32_dev, const float *vec_f32_dev, float *out_f32_dev, int rows, int cols, cudaStream_t stream)
Implementation of matrix-vector multiplication with FP32.
__device__ float bf16_to_float32_device(uint16_t bf16_raw)
Converts BF16 to FP32 on the device.
void rope_and_update_kv_cache_cuda(float *cache_base_ptr, const float *kv_vector_head, const float *all_freqs_cis_base, int pos, int kv_head_idx, int max_seq_len, int num_kv_heads, int head_dim, cudaStream_t stream)
__global__ void attention_selective_dequant_kernel(const float *__restrict__ Q_current_dev, const int8_t *__restrict__ K_quantized_cache_base, const int8_t *__restrict__ V_quantized_cache_base, const float *__restrict__ K_scales_cache_base, const float *__restrict__ V_scales_cache_base, float *__restrict__ selective_k_dequant_buffer, float *__restrict__ selective_v_dequant_buffer, float *__restrict__ out_dev, int num_heads, int current_seq_len, int head_dim, float scale, int cache_max_seq_len, int cache_num_kv_heads)
size_t moe_ffn_scratch_floats(int T, int d, int d_ff, int E, int k)
__global__ void decode_embed_kernel(const float *__restrict__ table, float *__restrict__ out, const int *__restrict__ step_dev, int hidden_size, int vocab_size)
void kv_write_devpos_cuda(float *k_cache_dev, float *v_cache_dev, const float *k_dev, const float *v_dev, const int *step_dev, int row, cudaStream_t stream)
static bool kernel_debug_copies()
void rope_cuda(float *x_dev, int num_heads, int head_dim, const float *all_freqs_cis_dev_base, int pos, bool use_adjacent_pairing, cudaStream_t stream)
void attention_cuda_selective_dequant(const float *Q_current_dev, const int8_t *K_quantized_cache_base, const int8_t *V_quantized_cache_base, const float *K_scales_cache_base, const float *V_scales_cache_base, float *selective_k_dequant_buffer, float *selective_v_dequant_buffer, float *out_dev, int num_heads, int current_seq_len, int head_dim, float scale, int cache_max_seq_len, int cache_num_kv_heads, cudaStream_t stream)
void softmax_vector_cuda_optimized(const float *x_dev, float *out_dev, int n, cudaStream_t stream)
__global__ void swiglu_batch_cuda_kernel(float *d_out_batch, const float *d_gate_act_batch, const float *d_up_act_batch, int num_tokens, int intermediate_size)
__global__ void attention_optimized_det_kernel(const float *__restrict__ Q_current, const float *__restrict__ K_layer_cache_base, const float *__restrict__ V_layer_cache_base, float *__restrict__ out, int current_seq_len, int head_dim, float scale, int cache_num_kv_heads, int num_q_heads, const int *step_dev)
__global__ void rope_kernel(float *x, int num_heads, int head_dim, const float *all_freqs_cis_base, int pos, bool use_adjacent_pairing)
void rope_batch_cuda(float *d_q_batch, float *d_k_batch, const float *d_all_freqs_cis_base, int num_tokens, int num_q_heads, int num_kv_heads, int head_dim, int start_pos_in_kv_cache, bool use_adjacent_pairing, cudaStream_t stream)
__global__ void softmax_exp_sum_kernel(const float *x, float *partial_sums, int n, float max_val)
__global__ void softmax_normalize_kernel(const float *x, float *out, int n, float max_val, float inv_sum)
void convert_fp32_to_bf16_cuda(const float *fp32_in_dev, uint16_t *bf16_out_dev, size_t n_elements, cudaStream_t stream)
__global__ void softmax_find_max_kernel(const float *x, float *partial_max, int n)
void add_residual_cuda(const float *matvec_out_dev, const float *residual_dev, float *result_dev, int n, cudaStream_t stream)
void matvec_bf16_f32_cuda(cublasHandle_t handle, const uint16_t *mat_bf16_dev, const float *vec_f32_dev, float *out_f32_dev, int rows, int cols, bool use_tensor_cores, cudaStream_t stream)
Matrix-vector multiplication with BF16 matrix and FP32 vector/output on GPU.
void swiglu_cuda(const float *gate_dev, const float *up_dev, float *out_dev, int n, cudaStream_t stream)
__global__ void rope_devpos_kernel(float *x, int num_heads, int head_dim, const float *all_freqs_cis_base, const int *step_dev, bool use_adjacent_pairing)
__device__ uint16_t float32_to_bf16_device(float fp32_val)
__global__ void add_residual_kernel(const float *matvec_out, const float *residual, float *result, int n)
__global__ void silu_kernel(const float *x, float *out, int n)
__global__ void attention_kernel(const float *Q_current, const float *K_layer_cache_base, const float *V_layer_cache_base, float *out, int current_seq_len, int head_dim, float scale, int cache_num_kv_heads, int num_q_heads)
__global__ void rmsnorm_sum_squares_kernel(const float *x, float *partial_sums, int n)
Kernel for computing sum of squares in RMS normalization.
__global__ void rmsnorm_optimized_kernel(const float *__restrict__ x, const float *__restrict__ weight, float *__restrict__ out, int n, float eps)
Optimized RMS normalization using simple warp primitives and shared memory.
void decode_attention_tiles(int *min_chunk, int *max_chunks)
void rmsnorm_vector_cuda_optimized(const float *x_dev, const float *weight_dev, float *out_dev, int n, float eps, cudaStream_t stream)
Optimized wrappers using the new high-performance kernels.
void silu_cuda(const std::vector< float > &x_host, std::vector< float > &out_host, int n)
__global__ void add_residual_batch_cuda_kernel(float *d_output_batch, const float *d_input_a_batch, const float *d_input_b_batch, int num_tokens, int hidden_size)
__global__ void add_vectors_kernel(const float *a, const float *b, float *result, int n)
void convert_bf16_to_fp32_cuda(const uint16_t *bf16_in_dev, float *fp32_out_dev, size_t n_elements, cudaStream_t stream)
void attention_devpos_cuda(const float *Q_current_dev, const float *K_layer_cache_base, const float *V_layer_cache_base, float *out_dev, int num_heads, int head_dim, float scale, int cache_max_seq_len, int cache_num_kv_heads, const int *step_dev, cudaStream_t stream)
__global__ void reduce_partial_sums_kernel(const float *partial_sums, float *total_sum_sq_out, int num_partial_sums)
__global__ void softmax_optimized_kernel(const float *__restrict__ x, float *__restrict__ out, int n)
Optimized softmax using shared memory and warp primitives.
void gemm_bf16_bf16_cuda(cublasHandle_t handle, bool transa_user, bool transb_user, int m_user, int n_user, int k_user, const float *alpha_user, const uint16_t *A_bf16_user, int lda_user, const uint16_t *B_bf16_user, int ldb_user, const float *beta_user, uint16_t *C_bf16_user, int ldc_user, cudaStream_t stream)
void gemm_f32_to_bf16_f32_cuda(cublasHandle_t handle, bool transa_user, bool transb_user, int m_user, int n_user, int k_user, const float *alpha_user, const float *A_f32_user, int lda_user, const uint16_t *B_bf16_user, int ldb_user, const float *beta_user, float *C_f32_user, int ldc_user, cudaStream_t stream)
__global__ void update_kv_cache_kernel(float *cache_base_ptr, const float *current_kv_vector, int pos, int kv_head_idx, int max_seq_len, int num_kv_heads, int head_dim)
void rope_devpos_cuda(float *x_dev, int num_heads, int head_dim, const float *all_freqs_cis_dev_base, const int *step_dev, bool use_adjacent_pairing, cudaStream_t stream)
void swiglu_batch_cuda(float *d_out_batch, const float *d_gate_act_batch, const float *d_up_act_batch, int num_tokens, int intermediate_size, cudaStream_t stream)
int decode_attention_kind()
__device__ float atomicAddFloat(float *address, float val)
__global__ void rope_and_update_kv_cache_kernel(float *cache_base_ptr, const float *kv_vector_head, const float *all_freqs_cis_base, int pos, int kv_head_idx, int max_seq_len, int num_kv_heads, int head_dim)
void lookup_embedding_cuda(const void *table_dev, float *output_dev, int token_id, int hidden_size, int vocab_size, bool is_bf16, cudaStream_t stream)
__global__ void rmsnorm_apply_kernel(const float *x, const float *weight, float *out, int n, float inv_norm_factor)
Kernel for applying RMS normalization.
void attention_cuda(const float *Q_current_dev, const float *K_layer_cache_base, const float *V_layer_cache_base, float *out_dev, int num_heads, int current_seq_len, int head_dim, float scale, int cache_max_seq_len, int cache_num_kv_heads, cudaStream_t stream)
__global__ void convert_bf16_to_fp32_kernel(const uint16_t *__restrict__ bf16_in, float *__restrict__ fp32_out, size_t n_elements)
__global__ void attention_optimized_kernel(const float *__restrict__ Q_current, const float *__restrict__ K_layer_cache_base, const float *__restrict__ V_layer_cache_base, float *__restrict__ out, int current_seq_len, int head_dim, float scale, int cache_num_kv_heads, int num_q_heads, const int *step_dev)
Corrected and optimized attention kernel.
__global__ void swiglu_kernel(const float *gate, const float *up, float *out, int n)
__global__ void rope_batch_kernel_cuda(float *x_batch_tensor, int num_tokens, int num_heads_in_tensor, int head_dim, const float *d_all_freqs_cis_base, int start_pos_offset, bool use_adjacent_pairing)
__device__ float atomicMaxFloat(float *address, float val)
void update_kv_cache_cuda(float *cache_base_ptr, const float *current_kv_head_vector, int pos, int kv_head_idx, int max_seq_len, int num_kv_heads, int head_dim, cudaStream_t stream)
__global__ void rmsnorm_optimized_det_kernel(const float *__restrict__ x, const float *__restrict__ weight, float *__restrict__ out, int n, float eps)
void attention_decode_at_cuda(const float *Q_current_dev, const float *K_layer_cache_base, const float *V_layer_cache_base, float *out_dev, int num_heads, int seq_len, int head_dim, float scale, int cache_max_seq_len, int cache_num_kv_heads, cudaStream_t stream)
__global__ void convert_fp32_to_bf16_kernel(const float *__restrict__ fp32_in, uint16_t *__restrict__ bf16_out, size_t n_elements)
void rmsnorm_vector_cuda(const float *x_dev, const float *weight_dev, float *out_dev, int n, float eps, cudaStream_t stream)
Implementation of RMS normalization with device pointers.
__global__ void kv_write_devpos_kernel(float *k_cache, float *v_cache, const float *__restrict__ k, const float *__restrict__ v, const int *step_dev, int row)
void attention_cuda_optimized(const float *Q_current_dev, const float *K_layer_cache_base, const float *V_layer_cache_base, float *out_dev, int num_heads, int current_seq_len, int head_dim, float scale, int cache_max_seq_len, int cache_num_kv_heads, cudaStream_t stream)
void add_vectors_cuda(const float *a_dev, const float *b_dev, float *result_dev, int n, cudaStream_t stream)
const float * in[expr::Expr::kMaxInputs]
const float * p[kIrDepthSourcesMax]
Logging utilities for the TinyLlama implementation.
__host__ bool tiled_shape_ok(int heads, int head_dim, int max_len, int heads_max, int min_chunk, int max_chunks)
__host__ size_t tiled_shm_bytes(int head_dim, int max_len, int min_chunk, int max_chunks)
__device__ __forceinline__ void tiled_split_block(const float *__restrict__ Qh, const float *__restrict__ K, const float *__restrict__ V, int L, int c, int kvh, int num_kv_heads, int head_dim, float scale, int min_chunk, int max_chunks, float *__restrict__ part)
constexpr int kTiledThreads
constexpr int kTiledMaxChunks
constexpr int kTiledMinChunk
__device__ __forceinline__ void tiled_merge_block(const float *__restrict__ base, float *__restrict__ out_h, int L, int head_dim, int min_chunk, int max_chunks)
__host__ __device__ int tiled_grid_chunks(int max_len, int min_chunk, int max_chunks)