Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

constexpr

Daniel Han5843d784f5e5
diff --git a/unsloth/kernels/cross_entropy_loss.py b/unsloth/kernels/cross_entropy_loss.py
index 006dfff6310..834a74c66d3 100644
--- a/unsloth/kernels/cross_entropy_loss.py
+++ b/unsloth/kernels/cross_entropy_loss.py
@@ -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)
diff --git a/unsloth/kernels/layernorm.py b/unsloth/kernels/layernorm.py
index 26a77f03a0d..ed8182014ef 100644
--- a/unsloth/kernels/layernorm.py
+++ b/unsloth/kernels/layernorm.py
@@ -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
diff --git a/unsloth/kernels/rms_layernorm.py b/unsloth/kernels/rms_layernorm.py
index 1cde6388eab..ce61cef72ed 100644
--- a/unsloth/kernels/rms_layernorm.py
+++ b/unsloth/kernels/rms_layernorm.py
@@ -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