constexpr

This commit is contained in:
Daniel Han 2025-03-13 18:23:29 -07:00
commit 5843d784f5
3 changed files with 30 additions and 25 deletions

View file

@ -37,12 +37,12 @@ def _cross_entropy_forward(
loss_ptr ,
logsumexp_ptr ,
labels_ptr ,
VOCAB_SIZE ,
VOCAB_SIZE : tl.constexpr,
BLOCK_SIZE : tl.constexpr,
DO_SOFTCAPPING ,
SOFTCAP ,
DO_LOGIT_SCALING ,
LOGIT_SCALE ,
DO_SOFTCAPPING : tl.constexpr,
SOFTCAP : tl.constexpr,
DO_LOGIT_SCALING : tl.constexpr,
LOGIT_SCALE : tl.constexpr,
):
"""
Cross Entropy Loss = 1/n sum [ -yi log(Pi) ]
@ -111,13 +111,13 @@ def _chunked_cross_entropy_forward(
loss_ptr ,
logsumexp_ptr ,
labels_ptr ,
VOCAB_SIZE ,
N_CHUNKS ,
VOCAB_SIZE : tl.constexpr,
N_CHUNKS : tl.constexpr,
BLOCK_SIZE : tl.constexpr,
DO_SOFTCAPPING ,
SOFTCAP ,
DO_LOGIT_SCALING ,
LOGIT_SCALE ,
DO_SOFTCAPPING : tl.constexpr,
SOFTCAP : tl.constexpr,
DO_LOGIT_SCALING : tl.constexpr,
LOGIT_SCALE : tl.constexpr,
):
"""
256K vocab divided in 4 chunks
@ -196,12 +196,12 @@ def _cross_entropy_backward(
dloss_row_stride ,
logsumexp_ptr ,
labels_ptr ,
VOCAB_SIZE ,
VOCAB_SIZE : tl.constexpr,
BLOCK_SIZE : tl.constexpr,
DO_SOFTCAPPING ,
SOFTCAP ,
DO_LOGIT_SCALING ,
LOGIT_SCALE ,
DO_SOFTCAPPING : tl.constexpr,
SOFTCAP : tl.constexpr,
DO_LOGIT_SCALING : tl.constexpr,
LOGIT_SCALE : tl.constexpr,
):
"""
CE_i = -y log(P) = y * (log[sum(exp(x))] - x)

View file

@ -30,7 +30,8 @@ def layernorm_forward(
b,
r,
mu,
n_cols, eps,
n_cols : tl.constexpr,
eps : tl.constexpr,
BLOCK_SIZE : tl.constexpr
):
row_idx = tl.program_id(0)
@ -68,7 +69,8 @@ def layernorm_backward(
b,
r,
mu,
n_cols, eps,
n_cols : tl.constexpr,
eps : tl.constexpr,
BLOCK_SIZE : tl.constexpr
):
# Approximately follows https://github.com/karpathy/llm.c/blob/master/doc/layernorm/layernorm.md

View file

@ -22,9 +22,10 @@ def _rms_layernorm_forward(
Y, Y_row_stride,
X, X_row_stride,
W, W_row_stride,
r, r_row_stride,
n_cols, eps,
BLOCK_SIZE : tl.constexpr
r, r_row_stride : tl.constexpr,
n_cols : tl.constexpr,
eps : tl.constexpr,
BLOCK_SIZE : tl.constexpr,
):
"""
Fast RMS Layernorm kernel
@ -57,9 +58,10 @@ def _rms_layernorm_backward(
dX, dX_row_stride,
X, X_row_stride,
W, W_row_stride,
r, r_row_stride,
r, r_row_stride : tl.constexpr,
# dW, dW_row_stride,
n_cols, eps,
n_cols : tl.constexpr,
eps : tl.constexpr,
GEMMA : tl.constexpr,
BLOCK_SIZE : tl.constexpr,
):
@ -107,8 +109,9 @@ def _gemma_rms_layernorm_forward(
Y, Y_row_stride,
X, X_row_stride,
W, W_row_stride,
r, r_row_stride,
n_cols, eps,
r, r_row_stride : tl.constexpr,
n_cols : tl.constexpr,
eps : tl.constexpr,
BLOCK_SIZE : tl.constexpr,
):
# Copies https://github.com/google-deepmind/gemma/blob/main/gemma/layers.py#L31