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
Pipeline parallel: agree on overflow once when explicitly unscaling (#16417)
Daniel Hand0a16f986246
diff --git a/tests/test_pp_fsdp2_gloo.py b/tests/test_pp_fsdp2_gloo.pyindex d516a37a9fd..d2a0d79842d 100644--- a/tests/test_pp_fsdp2_gloo.py+++ b/tests/test_pp_fsdp2_gloo.py@@ -300,16 +300,16 @@ def _case_scaler(rank, world, tmp):ids[4 * dp_rank : 4 * dp_rank + 4], labels[4 * dp_rank : 4 * dp_rank + 4], scaler = scaler)if rank == world - 1:- # Inject found_inf directly: DTensor shard infinities already propagate over dp.+ # Inject before PP agreement; DTensor gradients already reduce infs over DP during unscale.+ unscale_grads = scaler._unscale_grads_- unscale = scaler.unscale_-- def unscale_with_inf(optimizer):- unscale(optimizer)- for t in scaler._per_optimizer_states[id(optimizer)]["found_inf_per_device"].values():+ def unscale_with_inf(*args, **kwargs):+ found = unscale_grads(*args, **kwargs)+ for t in found.values():t.fill_(1.0)+ return found- scaler.unscale_ = unscale_with_inf+ scaler._unscale_grads_ = unscale_with_infbefore = [tpn._local(p).detach().clone() for p in pp.parameters()]pp.unscale_(scaler, opt)scaler.step(opt)diff --git a/tests/test_pp_native_scaler_fused_gloo.py b/tests/test_pp_native_scaler_fused_gloo.pynew file mode 100644index 00000000000..e474907a807--- /dev/null+++ b/tests/test_pp_native_scaler_fused_gloo.py@@ -0,0 +1,173 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.++import multiprocessing+import os++if (+ __name__ == "__main__"+ or multiprocessing.parent_process() is not None+ or getattr(multiprocessing.current_process(), "_inheriting", False)+):+ os.environ["CUDA_VISIBLE_DEVICES"] = ""++import datetime+import importlib+import itertools+import pathlib+import pickle+import sys+import tempfile+import types+import unittest++import torch+import torch.distributed as dist+import torch.multiprocessing as mp+++def _pipeline_class():+ package = types.ModuleType("_pp_scaler_test")+ package.__path__ = [str(pathlib.Path(__file__).resolve().parents[1] / "unsloth/models")]+ sys.modules[package.__name__] = package+ return importlib.import_module(f"{package.__name__}.pp_native").PipelineParallel+++def _worker(rank, rendezvous, output):+ torch.set_num_threads(1)+ dist.init_process_group(+ "gloo",+ init_method = rendezvous,+ rank = rank,+ world_size = 2,+ timeout = datetime.timedelta(seconds = 30),+ )+ try:+ pipeline_class = _pipeline_class()+ results = {}+ cases = itertools.product(+ ("Adam", "AdamW"),+ (False, True),+ ("step_only", "scaler_unscale", "pp_unscale", "pp_unscale_unbound"),+ (-1, 0, 1, 2),+ )+ for name, fused, recipe, inf_rank in cases:+ parameter = torch.nn.Parameter(torch.tensor(1.0))+ reference = torch.nn.Parameter(torch.tensor(1.0))+ optimizer = getattr(torch.optim, name)([parameter], lr = 0.1, fused = fused)+ reference_optimizer = getattr(torch.optim, name)([reference], lr = 0.1, fused = False)+ scaler = torch.amp.GradScaler("cpu", init_scale = 1024.0, growth_interval = 2)+ pipeline = pipeline_class.__new__(pipeline_class)+ pipeline.module = torch.nn.ParameterList([parameter])+ pipeline.num_stages = 2+ pipeline.group = dist.group.WORLD+ pipeline._step_scaler = None+ history = []+ expected_scale, finite_steps = 1024.0, 0+ for step in range(3):+ if recipe != "pp_unscale_unbound":+ pipeline._bind_scaler(scaler)+ pipeline._bind_scaler(scaler)+ pipeline._step_scaler = None if recipe == "pp_unscale_unbound" else scaler+ scaler.scale(parameter * (step + 1)).backward()+ overflow = step == 1 and inf_rank != -1+ if overflow and (rank == inf_rank or inf_rank == 2):+ parameter.grad.fill_(float("inf"))+ reductions = []+ original_reduce = dist.all_reduce++ def counted(*args, **kwargs):+ reductions.append(1)+ return original_reduce(*args, **kwargs)++ dist.all_reduce = counted+ try:+ if recipe.startswith("pp_unscale"):+ pipeline.unscale_(scaler, optimizer)+ elif recipe == "scaler_unscale":+ scaler.unscale_(optimizer)+ scaler.step(optimizer)+ scaler.update()+ finally:+ dist.all_reduce = original_reduce+ if overflow:+ expected_scale *= 0.5+ finite_steps = 0+ else:+ reference.grad = torch.tensor(float(step + 1))+ reference_optimizer.step()+ finite_steps += 1+ if finite_steps == 2:+ expected_scale *= 2+ finite_steps = 0+ pickle.loads(pickle.dumps(scaler))+ history.append(+ {+ "weight": parameter.item(),+ "reference": reference.item(),+ "scale": scaler.get_scale(),+ "expected_scale": expected_scale,+ "reductions": len(reductions),+ "cleaned": pipeline._step_scaler is None+ and not any(+ key in scaler.__dict__+ for key in (+ "unscale_",+ "_check_inf_per_device",+ "update",+ "_unsloth_pp_agree",+ )+ ),+ }+ )+ optimizer.zero_grad(set_to_none = True)+ results[name, fused, recipe, inf_rank] = history+ torch.save(results, pathlib.Path(output) / f"rank{rank}.pt")+ finally:+ dist.destroy_process_group()+++class TestPipelineScalerFused(unittest.TestCase):+ @classmethod+ def setUpClass(cls):+ if not dist.is_available() or not dist.is_gloo_available():+ raise unittest.SkipTest("requires CPU gloo")+ with tempfile.TemporaryDirectory() as output:+ rendezvous = (pathlib.Path(output) / "store").resolve().as_uri()+ mp.spawn(_worker, args = (rendezvous, output), nprocs = 2, join = True)+ cls.ranks = [+ torch.load(pathlib.Path(output) / f"rank{rank}.pt", weights_only = False)+ for rank in range(2)+ ]++ def test_optimizer_skip_and_scale_agreement(self):+ for rank, results in enumerate(self.ranks):+ for case, history in results.items():+ with self.subTest(rank = rank, case = case):+ for step, result in enumerate(history):+ self.assertAlmostEqual(+ result["weight"],+ result["reference"],+ places = 6,+ msg = str((case, step, result)),+ )+ self.assertEqual(+ result["scale"], result["expected_scale"], (case, step, result)+ )++ def test_explicit_pp_unscale_agrees_once(self):+ for rank, results in enumerate(self.ranks):+ for case, history in results.items():+ if case[2].startswith("pp_unscale"):+ with self.subTest(rank = rank, case = case):+ self.assertEqual([r["reductions"] for r in history], [1, 1, 1])++ def test_update_removes_hooks_and_releases_pipeline(self):+ for rank, results in enumerate(self.ranks):+ for case, history in results.items():+ with self.subTest(rank = rank, case = case):+ self.assertTrue(all(r["cleaned"] for r in history))+++if __name__ == "__main__":+ unittest.main(verbosity = 2)diff --git a/unsloth/models/pp_native.py b/unsloth/models/pp_native.pyindex 507db389395..4400ef4cde4 100644--- a/unsloth/models/pp_native.py+++ b/unsloth/models/pp_native.py@@ -1845,12 +1845,10 @@ class PipelineParallel:return total.float()def unscale_(self, scaler, optimizer):- """scaler.unscale_(optimizer), then found_inf is the MAX over every stage, so all stages skip the- same steps (scaler.step / scaler.update after this)."""+ """Unscale gradients and agree on overflow across stages before scaler.step / scaler.update."""+ if scaler.is_enabled():+ self._bind_scaler(scaler)scaler.unscale_(optimizer)- if not scaler.is_enabled():- return- self._agree_found_inf(scaler, optimizer)def _bind_scaler(self, scaler):"""Make scaler.unscale_ / scaler.step agree found_inf over every stage, or one stage steps while another skips.