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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
torch_cuda_device
Daniel Haned75ff330ac1
diff --git a/unsloth/kernels/cross_entropy_loss.py b/unsloth/kernels/cross_entropy_loss.pyindex 1c9998e1c95..006dfff6310 100644--- a/unsloth/kernels/cross_entropy_loss.py+++ b/unsloth/kernels/cross_entropy_loss.py@@ -15,7 +15,13 @@import tritonimport triton.language as tlimport torch-from .utils import calculate_settings, MAX_FUSED_SIZE, triton_tanh, triton_cast+from .utils import (+ calculate_settings,+ MAX_FUSED_SIZE,+ triton_tanh,+ triton_cast,+ torch_cuda_device,+)from transformers.models.llama.modeling_llama import loggerfrom packaging.version import Version@@ -295,37 +301,39 @@ class Fast_CrossEntropyLoss(torch.autograd.Function):BLOCK_SIZE, num_warps = calculate_settings(vocab_size)logsumexp = torch.empty(n_rows, dtype = torch.float32, device = device)- _cross_entropy_forward[(n_rows,)](- logits, logits.stride(0),- losses,- logsumexp,- labels,- VOCAB_SIZE = vocab_size,- BLOCK_SIZE = BLOCK_SIZE,- DO_SOFTCAPPING = DO_SOFTCAPPING,- SOFTCAP = logit_softcapping,- DO_LOGIT_SCALING = DO_LOGIT_SCALING,- LOGIT_SCALE = logit_scaling,- num_warps = num_warps,- )+ with torch_cuda_device(device):+ _cross_entropy_forward[(n_rows,)](+ logits, logits.stride(0),+ losses,+ logsumexp,+ labels,+ VOCAB_SIZE = vocab_size,+ BLOCK_SIZE = BLOCK_SIZE,+ DO_SOFTCAPPING = DO_SOFTCAPPING,+ SOFTCAP = logit_softcapping,+ DO_LOGIT_SCALING = DO_LOGIT_SCALING,+ LOGIT_SCALE = logit_scaling,+ num_warps = num_warps,+ )else:# For large vocabs > 65336 like Gemma 256Klogsumexp = torch.empty((n_rows, n_chunks,), dtype = torch.float32, device = device)- _chunked_cross_entropy_forward[(n_rows, n_chunks,)](- logits, logits.stride(0),- losses,- logsumexp,- labels,- VOCAB_SIZE = vocab_size,- N_CHUNKS = n_chunks,- BLOCK_SIZE = MAX_FUSED_SIZE,- DO_SOFTCAPPING = DO_SOFTCAPPING,- SOFTCAP = logit_softcapping,- DO_LOGIT_SCALING = DO_LOGIT_SCALING,- LOGIT_SCALE = logit_scaling,- num_warps = 32,- )+ with torch_cuda_device(device):+ _chunked_cross_entropy_forward[(n_rows, n_chunks,)](+ logits, logits.stride(0),+ losses,+ logsumexp,+ labels,+ VOCAB_SIZE = vocab_size,+ N_CHUNKS = n_chunks,+ BLOCK_SIZE = MAX_FUSED_SIZE,+ DO_SOFTCAPPING = DO_SOFTCAPPING,+ SOFTCAP = logit_softcapping,+ DO_LOGIT_SCALING = DO_LOGIT_SCALING,+ LOGIT_SCALE = logit_scaling,+ num_warps = 32,+ )# logsumexp(chunked_logsumexp) - x# Do the -x separatelylogsumexp = torch.logsumexp(logsumexp, dim = 1) # Row sum@@ -355,19 +363,20 @@ class Fast_CrossEntropyLoss(torch.autograd.Function):div, mod = divmod(vocab_size, BLOCK_SIZE)n_blocks : int = div + (mod != 0)- _cross_entropy_backward[(n_rows, n_blocks,)](- logits, logits.stride(0),- dlosses, dlosses.stride(0),- logsumexp,- labels,- VOCAB_SIZE = vocab_size,- BLOCK_SIZE = BLOCK_SIZE,- DO_SOFTCAPPING = ctx.DO_SOFTCAPPING,- SOFTCAP = ctx.logit_softcapping,- DO_LOGIT_SCALING = ctx.DO_LOGIT_SCALING,- LOGIT_SCALE = ctx.logit_scaling,- num_warps = 8,- )+ with torch_cuda_device(dlosses.device):+ _cross_entropy_backward[(n_rows, n_blocks,)](+ logits, logits.stride(0),+ dlosses, dlosses.stride(0),+ logsumexp,+ labels,+ VOCAB_SIZE = vocab_size,+ BLOCK_SIZE = BLOCK_SIZE,+ DO_SOFTCAPPING = ctx.DO_SOFTCAPPING,+ SOFTCAP = ctx.logit_softcapping,+ DO_LOGIT_SCALING = ctx.DO_LOGIT_SCALING,+ LOGIT_SCALE = ctx.logit_scaling,+ num_warps = 8,+ )return logits, None, None, None,passpassdiff --git a/unsloth/kernels/geglu.py b/unsloth/kernels/geglu.pyindex 9fedae769e5..d5a69aa67fa 100644--- a/unsloth/kernels/geglu.py+++ b/unsloth/kernels/geglu.py@@ -15,7 +15,11 @@import tritonimport triton.language as tlimport torch-from .utils import calculate_settings, triton_tanh+from .utils import (+ calculate_settings,+ triton_tanh,+ torch_cuda_device,+)@triton.jit@@ -43,7 +47,8 @@ def geglu_exact_forward_kernel(gate, up):n_elements = gate.numel()out = torch.empty((batch, seq_len, hd), dtype = gate.dtype, device = "cuda:0")grid = lambda meta: (triton.cdiv(n_elements, meta['BLOCK_SIZE']),)- _exact_forward_kernel[grid](gate, up, out, n_elements, BLOCK_SIZE = 1024,)+ with torch_cuda_device(gate.device):+ _exact_forward_kernel[grid](gate, up, out, n_elements, BLOCK_SIZE = 1024,)return outpass@@ -99,7 +104,8 @@ def geglu_exact_backward_kernel(DW, e, g):batch_seq_len, hd = e.shapen_elements = e.numel()grid = lambda meta: (triton.cdiv(n_elements, meta['BLOCK_SIZE']),)- _exact_backward_kernel[grid](DW, e, g, n_elements, BLOCK_SIZE = 1024,)+ with torch_cuda_device(e.device):+ _exact_backward_kernel[grid](DW, e, g, n_elements, BLOCK_SIZE = 1024,)return DW, e, gpass@@ -135,7 +141,8 @@ def geglu_approx_forward_kernel(gate, up):n_elements = gate.numel()out = torch.empty((batch, seq_len, hd), dtype = gate.dtype, device = "cuda:0")grid = lambda meta: (triton.cdiv(n_elements, meta['BLOCK_SIZE']),)- _approx_forward_kernel[grid](gate, up, out, n_elements, BLOCK_SIZE = 1024,)+ with torch_cuda_device(gate.device):+ _approx_forward_kernel[grid](gate, up, out, n_elements, BLOCK_SIZE = 1024,)return outpass@@ -198,6 +205,7 @@ def geglu_approx_backward_kernel(DW, e, g):batch_seq_len, hd = e.shapen_elements = e.numel()grid = lambda meta: (triton.cdiv(n_elements, meta['BLOCK_SIZE']),)- _approx_backward_kernel[grid](DW, e, g, n_elements, BLOCK_SIZE = 1024,)+ with torch_cuda_device(e.device):+ _approx_backward_kernel[grid](DW, e, g, n_elements, BLOCK_SIZE = 1024,)return DW, e, gpassdiff --git a/unsloth/kernels/layernorm.py b/unsloth/kernels/layernorm.pyindex ffcc5cc13c5..26a77f03a0d 100644--- a/unsloth/kernels/layernorm.py+++ b/unsloth/kernels/layernorm.py@@ -16,7 +16,7 @@import tritonimport triton.language as tlimport torch-from .utils import calculate_settings+from .utils import calculate_settings, torch_cuda_devicefrom unsloth_zoo.patching_utils import (patch_layernorm,)@@ -111,17 +111,18 @@ class Fast_Layernorm(torch.autograd.Function):r = torch.empty(n_rows, dtype = torch.float32, device = device)mu = torch.empty(n_rows, dtype = torch.float32, device = device)- layernorm_forward[(n_rows,)](- Y, Y.stride(0),- X, X.stride(0),- W,- b,- r,- mu,- n_cols, eps,- BLOCK_SIZE = BLOCK_SIZE,- num_warps = num_warps,- )+ with torch_cuda_device(device):+ layernorm_forward[(n_rows,)](+ Y, Y.stride(0),+ X, X.stride(0),+ W,+ b,+ r,+ mu,+ n_cols, eps,+ BLOCK_SIZE = BLOCK_SIZE,+ num_warps = num_warps,+ )ctx.eps = epsctx.BLOCK_SIZE = BLOCK_SIZEctx.num_warps = num_warps@@ -137,17 +138,18 @@ class Fast_Layernorm(torch.autograd.Function):X, W, b, r, mu = ctx.saved_tensorsn_rows, n_cols = dY.shape- layernorm_backward[(n_rows,)](- dY, dY.stride(0),- X, X .stride(0),- W,- b,- r,- mu,- n_cols, ctx.eps,- BLOCK_SIZE = ctx.BLOCK_SIZE,- num_warps = ctx.num_warps,- )+ with torch_cuda_device(dY.device):+ layernorm_backward[(n_rows,)](+ dY, dY.stride(0),+ X, X .stride(0),+ W,+ b,+ r,+ mu,+ n_cols, ctx.eps,+ BLOCK_SIZE = ctx.BLOCK_SIZE,+ num_warps = ctx.num_warps,+ )dX = dY.view(*shape)return dX, None, None, None, Nonepassdiff --git a/unsloth/kernels/rms_layernorm.py b/unsloth/kernels/rms_layernorm.pyindex 7487c10eeb8..1cde6388eab 100644--- a/unsloth/kernels/rms_layernorm.py+++ b/unsloth/kernels/rms_layernorm.py@@ -15,8 +15,7 @@import tritonimport triton.language as tlimport torch-from .utils import calculate_settings-+from .utils import calculate_settings, torch_cuda_device@triton.jitdef _rms_layernorm_forward(@@ -154,15 +153,16 @@ class Fast_RMS_Layernorm(torch.autograd.Function):r = torch.empty(n_rows, dtype = torch.float32, device = device)fx = _gemma_rms_layernorm_forward if gemma else _rms_layernorm_forward- fx[(n_rows,)](- Y, Y.stride(0),- X, X.stride(0),- W, W.stride(0),- r, r.stride(0),- n_cols, eps,- BLOCK_SIZE = BLOCK_SIZE,- num_warps = num_warps,- )+ with torch_cuda_device(device):+ fx[(n_rows,)](+ Y, Y.stride(0),+ X, X.stride(0),+ W, W.stride(0),+ r, r.stride(0),+ n_cols, eps,+ BLOCK_SIZE = BLOCK_SIZE,+ num_warps = num_warps,+ )ctx.eps = epsctx.BLOCK_SIZE = BLOCK_SIZEctx.num_warps = num_warps@@ -183,18 +183,19 @@ class Fast_RMS_Layernorm(torch.autograd.Function):# dW = XdX = torch.empty_like(dY) if ctx.GEMMA else dY- _rms_layernorm_backward[(n_rows,)](- dY, dY.stride(0),- dX, dX.stride(0),- X, X .stride(0),- W, W .stride(0),- r, r .stride(0),- # dW, dW.stride(0),- n_cols, ctx.eps,- GEMMA = ctx.GEMMA,- BLOCK_SIZE = ctx.BLOCK_SIZE,- num_warps = ctx.num_warps,- )+ with torch_cuda_device(dY.device):+ _rms_layernorm_backward[(n_rows,)](+ dY, dY.stride(0),+ dX, dX.stride(0),+ X, X .stride(0),+ W, W .stride(0),+ r, r .stride(0),+ # dW, dW.stride(0),+ n_cols, ctx.eps,+ GEMMA = ctx.GEMMA,+ BLOCK_SIZE = ctx.BLOCK_SIZE,+ num_warps = ctx.num_warps,+ )dX = dX.view(*shape)return dX, None, None, Nonepassdiff --git a/unsloth/kernels/rope_embedding.py b/unsloth/kernels/rope_embedding.pyindex 88b9ccadb4e..a14a485352b 100644--- a/unsloth/kernels/rope_embedding.py+++ b/unsloth/kernels/rope_embedding.py@@ -15,7 +15,7 @@import tritonimport triton.language as tlimport torch-from .utils import calculate_settings+from .utils import calculate_settings, torch_cuda_deviceROPE_GROUP_SIZE : int = 4def _rope_embedding(@@ -100,16 +100,17 @@ class Fast_RoPE_Embedding(torch.autograd.Function):div, mod = divmod(n_heads, ROPE_GROUP_SIZE)n_groups : int = div + (mod != 0)- _rope_embedding[(n_rows, n_groups, )](- Q, Q.stride(0),- cos, cos.stride(0),- sin, sin.stride(0),- seq_len,- head_dim, n_heads,- BACKWARD_PASS = False,- BLOCK_SIZE = BLOCK_SIZE,- num_warps = num_warps,- )+ with torch_cuda_device(Q.device):+ _rope_embedding[(n_rows, n_groups, )](+ Q, Q.stride(0),+ cos, cos.stride(0),+ sin, sin.stride(0),+ seq_len,+ head_dim, n_heads,+ BACKWARD_PASS = False,+ BLOCK_SIZE = BLOCK_SIZE,+ num_warps = num_warps,+ )ctx.BLOCK_SIZE = BLOCK_SIZEctx.num_warps = num_warpsctx.n_groups = n_groups@@ -134,15 +135,16 @@ class Fast_RoPE_Embedding(torch.autograd.Function):cos = ctx.cossin = ctx.sin- _rope_embedding[(n_rows, ctx.n_groups, )](- dY, dY .stride(0),- cos, cos.stride(0),- sin, sin.stride(0),- seq_len, head_dim, n_heads,- BACKWARD_PASS = True,- BLOCK_SIZE = ctx.BLOCK_SIZE,- num_warps = ctx.num_warps,- )+ with torch_cuda_device(dY.device):+ _rope_embedding[(n_rows, ctx.n_groups, )](+ dY, dY .stride(0),+ cos, cos.stride(0),+ sin, sin.stride(0),+ seq_len, head_dim, n_heads,+ BACKWARD_PASS = True,+ BLOCK_SIZE = ctx.BLOCK_SIZE,+ num_warps = ctx.num_warps,+ )dY = dY.view(batch, seq_len, n_heads, head_dim)return dY, None, None,passdiff --git a/unsloth/kernels/swiglu.py b/unsloth/kernels/swiglu.pyindex 688e9f9a48e..12f1f5e0639 100644--- a/unsloth/kernels/swiglu.py+++ b/unsloth/kernels/swiglu.py@@ -15,7 +15,7 @@import tritonimport triton.language as tlimport torch-from .utils import calculate_settings+from .utils import calculate_settings, torch_cuda_device@triton.jit@@ -43,7 +43,8 @@ def swiglu_fg_kernel(e, g):n_elements = e.numel()h = torch.empty((batch, seq_len, hd), dtype = e.dtype, device = e.device)grid = lambda meta: (triton.cdiv(n_elements, meta['BLOCK_SIZE']),)- _fg_kernel[grid](e, g, h, n_elements, BLOCK_SIZE = 1024,)+ with torch_cuda_device(e.device):+ _fg_kernel[grid](e, g, h, n_elements, BLOCK_SIZE = 1024,)return hpass@@ -94,6 +95,7 @@ def swiglu_DWf_DW_dfg_kernel(DW, e, g):batch_seq_len, hd = e.shapen_elements = e.numel()grid = lambda meta: (triton.cdiv(n_elements, meta['BLOCK_SIZE']),)- _DWf_DW_dfg_kernel[grid](DW, e, g, n_elements, BLOCK_SIZE = 1024,)+ with torch_cuda_device(e.device):+ _DWf_DW_dfg_kernel[grid](DW, e, g, n_elements, BLOCK_SIZE = 1024,)return DW, e, gpassdiff --git a/unsloth/kernels/utils.py b/unsloth/kernels/utils.pyindex 985adaaa44b..4439a47f237 100644--- a/unsloth/kernels/utils.py+++ b/unsloth/kernels/utils.py@@ -27,6 +27,7 @@ else:torch_amp_custom_fwd = torch.amp.custom_fwd(device_type = "cuda")torch_amp_custom_bwd = torch.amp.custom_bwd(device_type = "cuda")pass+torch_cuda_device = torch.cuda.device# tl.math.tanh now is libdevice.tanh