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
Padding choice: per compute capability thresholds, RTX PRO 6000 / RTX 5090 peaks (#15216)
Daniel Hanfd6d9f683006
diff --git a/tests/test_mfu_logging.py b/tests/test_mfu_logging.pyindex 215a48de02d..1d67cf2d3af 100644--- a/tests/test_mfu_logging.py+++ b/tests/test_mfu_logging.py@@ -674,3 +674,37 @@ def test_blackwell_dense_peaks(monkeypatch):):monkeypatch.setattr(torch.cuda, "get_device_name", lambda device = None, n = name: n)assert mfu.get_peak_tflops(dtype = torch.bfloat16) == peak, name+++def test_sm120_entries_and_name_order(monkeypatch):+ # NVIDIA RTX PRO Blackwell whitepaper v1.1 Table 4, RTX PRO 6000 Blackwell Server Edition page,+ # RTX Blackwell (GeForce) whitepaper v1.1 Table 3; dense 16 bit with FP32 accumulate.+ monkeypatch.delenv("UNSLOTH_PEAK_TFLOPS", raising = False)+ monkeypatch.setattr(torch.cuda, "is_available", lambda: True)+ cases = (+ ("NVIDIA RTX PRO 6000 Blackwell Server Edition", 500.0), # Colab G4 reports this name+ ("NVIDIA RTX PRO 6000 Blackwell Workstation Edition", 503.8),+ ("NVIDIA RTX PRO 6000 Blackwell Max-Q Workstation Edition", 438.9),+ ("NVIDIA GeForce RTX 5090", 209.5),+ ("NVIDIA GeForce RTX 5090 D", None),+ ("NVIDIA GeForce RTX 5090 Laptop GPU", None),+ # Neighbours that must not pick up an sm120 entry.+ ("NVIDIA RTX 6000 Ada Generation", None),+ ("Quadro RTX 6000", None),+ ("NVIDIA RTX PRO 5000 Blackwell", None),+ ("NVIDIA GeForce RTX 5080", None),+ )+ for name, peak in cases:+ monkeypatch.setattr(torch.cuda, "get_device_name", lambda device = None, n = name: n)+ for dtype in (torch.float16, torch.bfloat16):+ assert mfu.get_peak_tflops(dtype = dtype) == peak, name+++def test_every_table_name_resolves_to_its_own_entry(monkeypatch):+ # Substring order: a key listed later must never be shadowed by an earlier key that+ # matches inside it (B200 inside GB200, H100 inside H100 NVL, L4 inside L40S ...).+ monkeypatch.delenv("UNSLOTH_PEAK_TFLOPS", raising = False)+ monkeypatch.setattr(torch.cuda, "is_available", lambda: True)+ for key, value in mfu._PEAK_TFLOPS_16BIT:+ monkeypatch.setattr(torch.cuda, "get_device_name", lambda device = None, n = key: "NVIDIA " + n)+ assert mfu.get_peak_tflops(dtype = torch.float16) == value, keydiff --git a/tests/test_padding_choice.py b/tests/test_padding_choice.pyindex 0703609a02c..91a737573ab 100644--- a/tests/test_padding_choice.py+++ b/tests/test_padding_choice.py@@ -75,6 +75,50 @@ def test_rule_picks_padded_in_clear_host_bound_cells():assert not use_pf, (gpu, t, reason)+# Colab G4 = RTX PRO 6000 Blackwell Server Edition (sm120, 500 dense TFLOPs), 16 bit, 3 reps; 4 bit+# agreed within the A/A band. (model, padded tokens / pairs, gain torch 2.14.1 %, gain torch 2.10.0 %)+SM120 = (12, 0)+MEASURED_SM120 = [+ (Q06, ALP2, +5.4, +23.6), (Q06, ALP8, +5.4, +64.5), (Q06, ALP16, -29.2, +59.6),+ (Q06, LONG2, -14.5, -4.9), (Q06, LONG8, -37.0, -16.5),+ (Q4, ALP2, +5.2, +24.1), (Q4, ALP8, -39.6, -19.3), (Q4, ALP16, -53.6, -40.3),+ (Q4, LONG2, -15.2, -12.5), (Q4, LONG8, -35.2, -25.4),+] # fmt: skip++[email protected]("flops, tokens, gain_214, gain_210", MEASURED_SM120)[email protected]("peak", [500.0, 503.8, 438.9]) # Server, Workstation, Max-Q+def test_sm120_never_picks_padded_where_it_lost(flops, tokens, gain_214, gain_210, peak):+ use_pf, reason, _ = pc.choose_padding(+ flops[0], flops[1], tokens[0], tokens[1], peak, 10**9, capability = SM120+ )+ if min(gain_214, gain_210) < 0:+ assert use_pf, (tokens, gain_214, gain_210, reason)+ if tokens is ALP2: # the measured wins on both torch versions+ assert not use_pf, (tokens, reason)+++def test_sm120_with_the_old_threshold_would_pick_a_losing_cell():+ # Why sm120 has its own threshold: at 0.10 s the 0.6B Alpaca batch 16 cell (-29% on torch+ # 2.14.1) is picked.+ assert pc.choose_padding(*Q06, *ALP16, 500.0, 10**9)[0] is False+ assert pc.choose_padding(*Q06, *ALP16, 500.0, 10**9, capability = SM120)[0] is True++[email protected]("gpu, cap", [("A100", (8, 0)), ("L4", (8, 9)), ("T4", (7, 5))])+def test_measured_capabilities_keep_their_decisions(gpu, cap):+ for _, flops, tokens, _ in [m for m in MEASURED if m[0] == gpu]:+ args = (flops[0], flops[1], tokens[0], tokens[1], PEAK[gpu], 10**9)+ assert pc.choose_padding(*args)[0] == pc.choose_padding(*args, capability = cap)[0]++[email protected]("cap", [(9, 0), (10, 0), (10, 3), (8, 6), (12, 1)])+def test_unmeasured_capability_keeps_padding_free(cap):+ # H100 / B200 / B300 / consumer Ampere: the datasheet peak alone would pick padded here.+ use_pf, reason, _ = pc.choose_padding(*Q06, *ALP2, 989.0, 10**9, capability = cap)+ assert use_pf and "not measured" in reason++def test_short_runs_keep_padding_free():# 60 steps x grad accum 4 cannot repay the extra block compile.use_pf, reason, _ = pc.choose_padding(*Q06, *ALP2, PEAK["A100"], total_microbatches = 240)diff --git a/unsloth/utils/mfu.py b/unsloth/utils/mfu.pyindex ada666b1e58..6940f327065 100644--- a/unsloth/utils/mfu.py+++ b/unsloth/utils/mfu.py@@ -66,6 +66,19 @@ _PEAK_TFLOPS_16BIT = (("GB200", 2500.0),("B300", 2250.0),("B200", 2250.0),+ # sm120. NVIDIA RTX PRO Blackwell GPU Architecture whitepaper v1.1, Table 4 (dense FP16 / BF16+ # tensor with FP32 accumulate): Workstation 503.8, Max-Q 438.9. Server Edition: nvidia.com+ # rtx-pro-6000-blackwell-server-edition lists 1 PFLOP FP16 / BF16 with sparsity -> 500 dense.+ # GeForce RTX 5090: NVIDIA RTX Blackwell GPU Architecture whitepaper v1.1, Table 3, 209.5 dense+ # with FP32 accumulate (GeForce runs FP32 accumulate at half the FP16 accumulate rate).+ # Specific editions first; every key needs "PRO", so "RTX 6000 Ada Generation" never matches.+ ("RTX PRO 6000 BLACKWELL MAX-Q", 438.9),+ ("RTX PRO 6000 BLACKWELL SERVER", 500.0),+ ("RTX PRO 6000 BLACKWELL", 503.8),+ # Unknown (None): the China variant and the laptop part (a smaller GB203) have no figure there.+ ("RTX 5090 D", None),+ ("RTX 5090 LAPTOP", None),+ ("RTX 5090", 209.5),("A100", 312.0),("L40S", 362.05),("L40", 181.05),diff --git a/unsloth/utils/padding_choice.py b/unsloth/utils/padding_choice.pyindex cd1ab56b7f9..457fa8c9f52 100644--- a/unsloth/utils/padding_choice.py+++ b/unsloth/utils/padding_choice.py@@ -6,19 +6,23 @@Padding-free removes pad tokens, but its varlen batches skip the per-block compile, so each steplaunches more kernels from Python. Padded batches pay for their pad tokens on the GPU but run thecompiled blocks. Which one is faster depends on whether a micro batch is bound by the host or by the-GPU: measured on A100, L4 and T4 with Qwen3 0.6B / 4B LoRA (16 bit and 4 bit), padded + block-compile was 10-17% faster only when a padded micro batch costs the GPU well under the ~0.25-0.4 s-the host spends launching it, and up to 57% slower once the GPU is the bottleneck.+GPU: measured on A100, L4, T4 and RTX PRO 6000 Blackwell with Qwen3 0.6B / 4B LoRA (16 bit and+4 bit), padded + block compile was 5-17% faster only when a padded micro batch costs the GPU well+under the ~0.25-0.4 s the host spends launching it, and up to 57% slower once the GPU is the+bottleneck.Rule (all estimates, every unknown keeps padding-free, the old default):padded GPU time = (linear FLOPs per token * padded tokens + attention FLOPs per pair * pairs)/ (datasheet 16 bit peak * _EFFICIENCY)- pick padded iff padded GPU time < _MAX_PADDED_GPU_S+ pick padded iff padded GPU time < the measured threshold for the GPU's compute capability+ (_MAX_PADDED_GPU_S_BY_CAPABILITY; any other capability keeps padding-free)and the run has enough micro batches to repay the extra block compile(about 25 s in 16 bit, 100 s in 4 bit) at ~_SAVED_S_PER_MICROBATCH each.-Only Linux + NVIDIA CUDA is ever switched: ROCm / HIP, Windows, XPU, MPS and CPU keep padding-free-(no measurements there yet), unless `UNSLOTH_PADDING_CHOICE=padded` is set explicitly.+Only Linux + NVIDIA CUDA on a measured compute capability is ever switched: ROCm / HIP, Windows,+XPU, MPS, CPU and unmeasured capabilities (H100 / H200 sm90, B200 / B300 sm100 / sm103 have+datasheet peaks but no measurement yet) keep padding-free, unless `UNSLOTH_PADDING_CHOICE=padded`+is set explicitly.`UNSLOTH_PADDING_CHOICE=padding_free` restores the previous behaviour (always padding-free),`=padded` forces padded batches, `=auto` (default) applies the rule. An explicit@@ -44,6 +48,17 @@ _EFFICIENCY = (0.5 # fraction of the datasheet peak a LoRA step reaches when GPU bound (A100 measured 0.5))_MAX_PADDED_GPU_S = 0.10 # padded micro batch GPU estimate below which the host dominates+# Per compute capability, only where measured (padded + block compile vs padding-free, Qwen3 0.6B /+# 4B LoRA r16, 16 and 4 bit, 3 interleaved reps). A capability missing here keeps padding-free.+# sm75 T4, sm80 A100, sm89 L4 (torch 2.14.1): 0.10 s.+# sm120 RTX PRO 6000 Blackwell (Colab G4, torch 2.14.1 and 2.10.0): padded lost 24-29% at a+# ~92 ms estimate on torch 2.14.1 and won (+5% on 2.14.1, +15-64% on 2.10.0) at <= 45 ms.+_MAX_PADDED_GPU_S_BY_CAPABILITY = {+ (7, 5): 0.10,+ (8, 0): 0.10,+ (8, 9): 0.10,+ (12, 0): 0.06,+}_SAVED_S_PER_MICROBATCH = 0.04 # host time block compile saved per micro batch where it won_EXTRA_COMPILE_S_16BIT = 25.0_EXTRA_COMPILE_S_4BIT = 100.0@@ -96,15 +111,25 @@ def choose_padding(peak_tflops,total_microbatches = None,load_in_4bit = False,+ capability = None,):- """Pure rule. Returns (use_padding_free: bool, reason: str, padded_gpu_s: float | None)."""+ """Pure rule. Returns (use_padding_free: bool, reason: str, padded_gpu_s: float | None).++ `capability` is the CUDA (major, minor); one that was never measured keeps padding-free.+ None applies the default threshold (callers that know the device always pass it)."""if not peak_tflops or peak_tflops <= 0 or not linear_flops_per_token or not padded_tokens:return True, "no estimate", None+ max_gpu_s = _MAX_PADDED_GPU_S+ if capability is not None:+ capability = tuple(int(x) for x in capability)+ max_gpu_s = _MAX_PADDED_GPU_S_BY_CAPABILITY.get(capability)+ if max_gpu_s is None:+ return True, f"sm{capability[0]}{capability[1]} not measured", Noneflops = linear_flops_per_token * padded_tokens + (attention_flops_per_pair or 0) * (padded_pairs or 0)gpu_s = flops / (peak_tflops * 1e12 * _EFFICIENCY)- if gpu_s >= _MAX_PADDED_GPU_S:+ if gpu_s >= max_gpu_s:return True, f"GPU bound (padded micro batch ~{gpu_s * 1e3:.0f} ms of GPU work)", gpu_sextra = _EXTRA_COMPILE_S_4BIT if load_in_4bit else _EXTRA_COMPILE_S_16BITneeded = math.ceil(_REPAY_FACTOR * extra / _SAVED_S_PER_MICROBATCH)@@ -313,6 +338,7 @@ def _decide(model, config, train_dataset, processing_class, formatting_func):dtype = torch.bfloat16 if getattr(config, "bf16", False) else torch.float16device = next((p.device for p in model.parameters() if p.device.type == "cuda"), None)peak = get_peak_tflops(device, dtype)+ capability = torch.cuda.get_device_capability(device)try:n_rows = len(train_dataset)except Exception:@@ -325,5 +351,6 @@ def _decide(model, config, train_dataset, processing_class, formatting_func):peak,total_microbatches = _total_microbatches(config, n_rows),load_in_4bit = _is_4bit(model),+ capability = capability,)return use_pf, reason