62 const float m =
static_cast<float>(
mean);
85 out[
i] = 0.0f <
v ?
v : 0.0f;
102 const int j = (
int)(
i %
w);
143 for (
int i = 0;
i <
S; ++
i) {
173 for (
int t = 0;
t <
T; ++
t) {
192 const float dd =
D[0];
194 for (
int t = 0;
t <
T; ++
t) {
225 const float v =
x[
i];
233 const float v =
x[
i];
236 const float c =
sqrtf(2.0f / 3.14159265358979323846f);
256 const float c =
f[2 *
i],
s =
f[2 *
i + 1];
257 const float x0 =
x[0],
x1 =
x[1];
273 const float c =
f[2 *
i],
s =
f[2 *
i + 1];
282 int T,
int C,
int K) {
285 for (
int t = 0;
t <
T; ++
t) {
288 for (
int k = 0;
k < K; ++
k) {
308 for (
int t = 0;
t <
T; ++
t) {
315 for (
int n = 0;
n <
N; ++
n) {
339 const size_t t =
i /
w;
342 for (
int k = 0;
k <
code.n; ++
k) {
343 const expr::Instr
ins =
code.code[
k];
373 default:
r =
a <=
b ?
a :
b;
break;
401 for (
int o = 16;
o > 0;
o >>= 1) {
446 const int j =
c0 +
i;
450 for (
int d =
lane; d <
hd; d += 32)
dot += q[d] *
kr[d];
474 for (
int i = 0;
i <
cn; ++
i) {
475 const int j =
c0 +
i;
484 const float inv = 1.0f / (
L + 1e-9f);
505 const int n =
row0 +
t + 1;
521 if ((
code >> 1) != qs && !(
code & 1)) {
527 for (
int d =
lane; d <
hd; d += 32)
dot += q[d] *
kr[d];
552 for (
int i = 0;
i <
cn; ++
i)
559 const float inv = 1.0f / (
L + 1e-9f);
571 for (
int w =
w0;
w <
n; ++
w) {
572 const int r = idx[
w];
586 const int id = tokens[
t];
609 const float ga = g[
a];
629 const float c =
f[2 *
i],
s =
f[2 *
i + 1];
630 const float x0 =
x[0],
x1 =
x[1];
644 const float c =
f[2 *
i],
s =
f[2 *
i + 1];
680 throw std::runtime_error(
"IR CUDA rope: n_rot " + std::to_string(
n_rot) +
" / head_dim " + std::to_string(
hd));
689 throw std::runtime_error(
"IR CUDA turn_state attention: unsupported shape (head_dim " + std::to_string(
hd) +
")");
691 turn_state_attention_kernel<<<
dim3(
T,
hq),
kThreads,
shm,
stream>>>(q, K,
V,
out,
hq,
hkv,
hd,
row0,
codes,
scale,
712 throw std::runtime_error(
"IR CUDA window attention: unsupported shape (window " + std::to_string(
window) +
713 ", head_dim " + std::to_string(
hd) +
")");
715 window_attention_kernel<<<
dim3(
T,
hq),
kThreads,
shm,
stream>>>(q, K,
V,
out,
hq,
hkv,
hd,
start_pos,
scale,
window,
723 kv_write_kernel<<<
blocks_for(
n),
kThreads, 0,
stream>>>(K,
V,
k,
v,
n,
row,
start_pos,
dpos);
762 throw std::runtime_error(
"IR CUDA depth_attend: " + std::to_string(
S) +
" sources (2.." +
772 linear_attention_kernel<<<(width + 127) / 128, 128, 0,
stream>>>(q,
k,
v,
num,
den,
out,
T, width,
den_floor);
779 throw std::runtime_error(
"IR CUDA ssm_scan: n " + std::to_string(
n) +
" (at most " + std::to_string(
kIrSsmMax) +
782 ssm_scan_kernel<<<1,
threads, (
size_t)2 *
n *
sizeof(
float),
stream>>>(
u,
A,
B,
C,
D,
s,
y,
T,
n);
811 check_launch(op == 0 ?
"add" : op == 1 ?
"mul" :
"sub");
826 rope_kernel<<<T, 128, 0, stream>>>(q,
k,
hq,
hk,
hd,
start_pos,
freqs,
dpos);
833 throw std::runtime_error(
"IR CUDA rope: n_rot " + std::to_string(
n_rot) +
" / head_dim " + std::to_string(
hd));
834 rope_rows_kernel<<<T, 128, 0, stream>>>(
x,
heads,
hd,
n_rot,
neox ? 1 : 0,
start_pos,
freqs,
dpos);
840 if (K < 1 ||
C <= 0)
throw std::runtime_error(
"IR CUDA conv1d_causal: kernel " + std::to_string(K));
841 conv1d_causal_kernel<<<(
C + 127) / 128, 128, 0,
stream>>>(
x,
w,
bias,
state,
out,
T,
C, K);
846 const float* d,
float*
s,
float*
y,
int T,
int D,
int N,
bool softplus,
848 selective_scan_kernel<<<(
D + 127) / 128, 128, 0,
stream>>>(
x,
dt,
B,
C,
A, d,
s,
y,
T,
D,
N,
softplus ? 1 : 0);
853 if (
stages.size() <= 1)
return 0;
854 return (
size_t)
T *
w + (
stages.size() - 1) * (
size_t)
T;
860 throw std::runtime_error(
"IR CUDA expr: " + std::to_string(
n_ins) +
" inputs, " + std::to_string(
stages.size()) +
867 for (
size_t k = 0;
k <
stages.size(); ++
k) {
869 if (
s.code.empty() ||
s.code.size() >
expr::kMaxCode)
throw std::runtime_error(
"IR CUDA expr: a stage's code");
872 for (
size_t i = 0;
i <
s.code.size(); ++
i)
code.code[
i] =
s.code[
i];
887 const int w =
r +
dr;
910 const int w =
r +
dr;
938 const int v =
c0 +
j;
941 float a = 0.0f,
b = 0.0f;
952 for (
int j = 0;
j <
cn; ++
j) {
964 for (
int j = 0;
j <
cn; ++
j) {
965 const int v =
c0 +
j;
991 mla_kv_write_kernel<<<
blocks_for(
n),
kThreads, 0,
stream>>>(
KV,
c,
kr,
T,
r,
dr,
start_pos,
dpos);
999 if (
smem > 48 * 1024)
1000 throw std::runtime_error(
"IR mla_attention on CUDA: kv_rank " + std::to_string(
r) +
" needs " + std::to_string(
smem) +
1001 " bytes of shared memory (at most 48 KiB)");
1002 mla_attention_kernel<<<static_cast<unsigned>(
T) *
H, 128,
smem,
stream>>>(
qn,
qr,
KV,
Kb,
Vb,
out,
H,
r,
dn,
dr,
dv,
static constexpr size_t kMaxInputs
__global__ void rope_kernel(float *x, int num_heads, int head_dim, const float *all_freqs_cis_base, int pos, bool use_adjacent_pairing)
__global__ void silu_kernel(const float *x, float *out, int n)
const float * in[expr::Expr::kMaxInputs]
const float * p[kIrDepthSourcesMax]
expr::Instr code[expr::kMaxCode]
const float * col[expr::kMaxColumns]
constexpr size_t kMaxCode
constexpr size_t kMaxColumns
constexpr size_t kMaxStack
void ir_linear_attention_cuda(const float *q, const float *k, const float *v, float *num, float *den, float *out, int T, int width, float den_floor, cudaStream_t stream)
void ir_conv1d_causal_cuda(const float *x, const float *w, const float *bias, float *state, float *out, int T, int C, int K, cudaStream_t stream)
void ir_turn_state_attention_cuda(const float *q, const float *K, const float *V, float *out, int T, int row0, const int *codes, int hq, int hkv, int hd, float scale, cudaStream_t stream, const int *drow)
void ir_binary_cuda(const float *a, const float *b, float *out, size_t n, int op, cudaStream_t stream)
void ir_bias_add_cuda(float *y, const float *b, int T, int O, cudaStream_t stream)
void ir_rope_rows_at_cuda(float *x, int T, int heads, int hd, int n_rot, bool neox, const int *pos, const float *freqs, cudaStream_t stream)
void ir_rows_gather_cuda(const float *src, const int *idx, float *dst, int n, int w, cudaStream_t stream)
void ir_rows_scatter_cuda(const float *src, const int *idx, float *dst, int n, int w, cudaStream_t stream)
void ir_kv_write_cuda(float *K, float *V, const float *k, const float *v, int T, int row, int start_pos, cudaStream_t stream, const int *dpos)
size_t ir_expr_scratch_floats(const std::vector< expr::Stage > &stages, int T, int w)
void ir_embedding_cuda(const float *table, const int *tokens, float *out, int T, int d, cudaStream_t stream)
void ir_gelu_cuda(const float *x, float *out, size_t n, bool tanh_form, cudaStream_t stream)
void ir_silu_cuda(const float *x, float *out, size_t n, cudaStream_t stream)
void ir_depth_attend_cuda(const float *const *sources, int S, const float *w, float *out, int T, int d, float eps, cudaStream_t stream)
void ir_rope_rows_cuda(float *x, int T, int heads, int hd, int n_rot, bool neox, int start_pos, const float *freqs, cudaStream_t stream, const int *dpos)
void ir_kv_compact_cuda(float *K, float *V, const int *idx, int w0, int n, int row, cudaStream_t stream)
void ir_pos_embedding_cuda(const float *x, const float *P, float *out, int T, int d, int start_pos, cudaStream_t stream, const int *dpos)
void ir_window_attention_cuda(const float *q, const float *K, const float *V, float *out, int T, int start_pos, int hq, int hkv, int hd, float scale, int window, int sinks, cudaStream_t stream, const int *dpos, float softcap)
void ir_scale_cuda(const float *x, const float *g, const float *b, float *out, int T, int w, cudaStream_t stream)
void ir_expr_cuda(const std::vector< expr::Stage > &stages, const float *const *ins, int n_ins, float *scratch, float *out, int T, int w, cudaStream_t stream)
void ir_rope_at_cuda(float *q, float *k, int T, int hq, int hk, int hd, const int *pos, const float *freqs, cudaStream_t stream)
void ir_rms_row_cuda(const float *x, float *out, int T, int w, float eps, cudaStream_t stream)
void ir_layernorm_cuda(const float *x, const float *w, const float *b, float *out, int T, int d, float eps, cudaStream_t stream)
void ir_relu_cuda(const float *x, float *out, size_t n, cudaStream_t stream)
void ir_affine_cuda(const float *x, float *out, size_t n, float scale, float shift, cudaStream_t stream)
void ir_selective_scan_cuda(const float *x, const float *dt, const float *B, const float *C, const float *A, const float *d, float *s, float *y, int T, int D, int N, bool softplus, cudaStream_t stream)
void ir_mla_attention_cuda(const float *qn, const float *qr, const float *KV, const float *Kb, const float *Vb, float *out, int T, int start_pos, int H, int r, int dn, int dr, int dv, float scale, int window, int sinks, cudaStream_t stream, const int *dpos)
void ir_mul_col_cuda(const float *x, const float *c, float *out, int T, int w, cudaStream_t stream)
void ir_embedding_state_cuda(const float *table, const float *state, int vocab, const int *tokens, float *out, int T, int d, cudaStream_t stream)
void ir_mor_gate_cuda(const float *x_in, float *x, const float *g, int n, int w, cudaStream_t stream)
void ir_mla_kv_write_cuda(float *KV, const float *c, const float *kr, int T, int r, int dr, int start_pos, cudaStream_t stream, const int *dpos)
void ir_rope_cuda(float *q, float *k, int T, int hq, int hk, int hd, int start_pos, const float *freqs, cudaStream_t stream, const int *dpos)
void ir_ssm_scan_cuda(const float *u, const float *A, const float *B, const float *C, const float *D, float *s, float *y, int T, int n, cudaStream_t stream)
void ir_sigmoid_cuda(const float *x, float *out, size_t n, cudaStream_t stream)