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
Tests: keep gloo FSDP2 test ranks on the CPU on GPU hosts (#14383)
Daniel Han17b4b59a456b
diff --git a/tests/test_fsdp2_native_accelerator_loop_gloo.py b/tests/test_fsdp2_native_accelerator_loop_gloo.pyindex cb079f8f572..d70f4995c5f 100644--- a/tests/test_fsdp2_native_accelerator_loop_gloo.py+++ b/tests/test_fsdp2_native_accelerator_loop_gloo.py@@ -19,7 +19,9 @@ scheduler, before any FP8 conversion; non-native objects still prepare."""import multiprocessingimport os-if multiprocessing.parent_process() is not None:+if multiprocessing.parent_process() is not None or getattr(+ multiprocessing.current_process(), "_inheriting", False+):os.environ["CUDA_VISIBLE_DEVICES"] = "" # spawned gloo ranks: CPU only, also on GPU hostsimport datetime # noqa: E402diff --git a/tests/test_fsdp2_native_dora_resume_gloo.py b/tests/test_fsdp2_native_dora_resume_gloo.pyindex 053e73e0adb..20a52f47f02 100644--- a/tests/test_fsdp2_native_dora_resume_gloo.py+++ b/tests/test_fsdp2_native_dora_resume_gloo.py@@ -11,7 +11,9 @@ adapter tensor exactly."""import multiprocessingimport os-if multiprocessing.parent_process() is not None:+if multiprocessing.parent_process() is not None or getattr(+ multiprocessing.current_process(), "_inheriting", False+):os.environ["CUDA_VISIBLE_DEVICES"] = "" # spawned gloo ranks: CPU only, also on GPU hostsimport datetime # noqa: E402diff --git a/tests/test_fsdp2_native_generic_media_gloo.py b/tests/test_fsdp2_native_generic_media_gloo.pyindex 0c19f03c370..9d858d4eb21 100644--- a/tests/test_fsdp2_native_generic_media_gloo.py+++ b/tests/test_fsdp2_native_generic_media_gloo.py@@ -18,7 +18,9 @@ installs nothing."""import multiprocessingimport os-if multiprocessing.parent_process() is not None:+if multiprocessing.parent_process() is not None or getattr(+ multiprocessing.current_process(), "_inheriting", False+):os.environ["CUDA_VISIBLE_DEVICES"] = "" # spawned gloo ranks: CPU only, also on GPU hostsimport datetime # noqa: E402diff --git a/tests/test_fsdp2_native_guards_gloo.py b/tests/test_fsdp2_native_guards_gloo.pyindex 49b509c20e2..aea9d9ef36d 100644--- a/tests/test_fsdp2_native_guards_gloo.py+++ b/tests/test_fsdp2_native_guards_gloo.py@@ -13,7 +13,9 @@import multiprocessingimport os-if multiprocessing.parent_process() is not None:+if multiprocessing.parent_process() is not None or getattr(+ multiprocessing.current_process(), "_inheriting", False+):os.environ["CUDA_VISIBLE_DEVICES"] = "" # spawned gloo ranks: CPU only, also on GPU hostsimport sys # noqa: E402diff --git a/tests/test_fsdp2_native_infer_embed_audio_gloo.py b/tests/test_fsdp2_native_infer_embed_audio_gloo.pyindex d8688ade7fd..451d80ee978 100644--- a/tests/test_fsdp2_native_infer_embed_audio_gloo.py+++ b/tests/test_fsdp2_native_infer_embed_audio_gloo.py@@ -15,7 +15,9 @@import multiprocessingimport os-if multiprocessing.parent_process() is not None:+if multiprocessing.parent_process() is not None or getattr(+ multiprocessing.current_process(), "_inheriting", False+):os.environ["CUDA_VISIBLE_DEVICES"] = "" # spawned gloo ranks: CPU only, also on GPU hostsimport ast # noqa: E402diff --git a/tests/test_fsdp2_native_int8_gloo.py b/tests/test_fsdp2_native_int8_gloo.pyindex a9850315625..a0c44c94581 100644--- a/tests/test_fsdp2_native_int8_gloo.py+++ b/tests/test_fsdp2_native_int8_gloo.py@@ -11,7 +11,9 @@ unsharded one; a torch without fully_shard(ignored_params) refuses."""import multiprocessingimport os-if multiprocessing.parent_process() is not None:+if multiprocessing.parent_process() is not None or getattr(+ multiprocessing.current_process(), "_inheriting", False+):os.environ["CUDA_VISIBLE_DEVICES"] = "" # spawned gloo ranks: CPU only, also on GPU hostsimport copy # noqa: E402diff --git a/tests/test_fsdp2_native_layout_agree_gloo.py b/tests/test_fsdp2_native_layout_agree_gloo.pyindex f3a4955494a..7ad3e115fa4 100644--- a/tests/test_fsdp2_native_layout_agree_gloo.py+++ b/tests/test_fsdp2_native_layout_agree_gloo.py@@ -12,7 +12,9 @@import multiprocessingimport os-if multiprocessing.parent_process() is not None:+if multiprocessing.parent_process() is not None or getattr(+ multiprocessing.current_process(), "_inheriting", False+):os.environ["CUDA_VISIBLE_DEVICES"] = "" # spawned gloo ranks: CPU only, also on GPU hostsimport importlib # noqa: E402diff --git a/tests/test_fsdp2_native_layout_cpu_split_gloo.py b/tests/test_fsdp2_native_layout_cpu_split_gloo.pyindex e26ddd09a65..0799596c622 100644--- a/tests/test_fsdp2_native_layout_cpu_split_gloo.py+++ b/tests/test_fsdp2_native_layout_cpu_split_gloo.py@@ -12,7 +12,9 @@import multiprocessingimport os-if multiprocessing.parent_process() is not None:+if multiprocessing.parent_process() is not None or getattr(+ multiprocessing.current_process(), "_inheriting", False+):os.environ["CUDA_VISIBLE_DEVICES"] = "" # spawned gloo ranks: CPU only, also on GPU hostsimport datetime # noqa: E402diff --git a/tests/test_fsdp2_native_modality_mesh_gloo.py b/tests/test_fsdp2_native_modality_mesh_gloo.pyindex 3234d85a7c5..913f5e03422 100644--- a/tests/test_fsdp2_native_modality_mesh_gloo.py+++ b/tests/test_fsdp2_native_modality_mesh_gloo.py@@ -15,7 +15,9 @@import multiprocessingimport os-if multiprocessing.parent_process() is not None:+if multiprocessing.parent_process() is not None or getattr(+ multiprocessing.current_process(), "_inheriting", False+):os.environ["CUDA_VISIBLE_DEVICES"] = "" # spawned gloo ranks: CPU only, also on GPU hostsimport importlib # noqa: E402diff --git a/tests/test_fsdp2_native_optim_reentry_gloo.py b/tests/test_fsdp2_native_optim_reentry_gloo.pyindex 1bbfe8442fa..426e7303505 100644--- a/tests/test_fsdp2_native_optim_reentry_gloo.py+++ b/tests/test_fsdp2_native_optim_reentry_gloo.py@@ -14,7 +14,9 @@import multiprocessingimport os-if multiprocessing.parent_process() is not None:+if multiprocessing.parent_process() is not None or getattr(+ multiprocessing.current_process(), "_inheriting", False+):os.environ["CUDA_VISIBLE_DEVICES"] = "" # spawned gloo ranks: CPU only, also on GPU hostsimport sys # noqa: E402diff --git a/tests/test_fsdp2_native_optim_save_wait_gloo.py b/tests/test_fsdp2_native_optim_save_wait_gloo.pyindex c6fc3593cc5..ee16ebfd83c 100644--- a/tests/test_fsdp2_native_optim_save_wait_gloo.py+++ b/tests/test_fsdp2_native_optim_save_wait_gloo.py@@ -14,7 +14,9 @@ import timeimport pytestimport torch-if __import__("multiprocessing").parent_process() is not None:+if __import__("multiprocessing").parent_process() is not None or getattr(+ __import__("multiprocessing").current_process(), "_inheriting", False+):os.environ["CUDA_VISIBLE_DEVICES"] = ""sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))diff --git a/tests/test_fsdp2_native_per_input_gloo.py b/tests/test_fsdp2_native_per_input_gloo.pyindex a6e1773f569..334fbb8abd5 100644--- a/tests/test_fsdp2_native_per_input_gloo.py+++ b/tests/test_fsdp2_native_per_input_gloo.py@@ -16,7 +16,9 @@ once.import multiprocessingimport os-if multiprocessing.parent_process() is not None:+if multiprocessing.parent_process() is not None or getattr(+ multiprocessing.current_process(), "_inheriting", False+):os.environ["CUDA_VISIBLE_DEVICES"] = "" # spawned gloo ranks: CPU only, also on GPU hostsimport importlib # noqa: E402diff --git a/tests/test_fsdp2_native_pg_tp_media_gloo.py b/tests/test_fsdp2_native_pg_tp_media_gloo.pyindex d1379025e56..f59192e3158 100644--- a/tests/test_fsdp2_native_pg_tp_media_gloo.py+++ b/tests/test_fsdp2_native_pg_tp_media_gloo.py@@ -15,7 +15,9 @@import multiprocessingimport os-if multiprocessing.parent_process() is not None:+if multiprocessing.parent_process() is not None or getattr(+ multiprocessing.current_process(), "_inheriting", False+):os.environ["CUDA_VISIBLE_DEVICES"] = "" # spawned gloo ranks: CPU only, also on GPU hostsimport ast # noqa: E402diff --git a/tests/test_fsdp2_native_qlora_clef_place_gloo.py b/tests/test_fsdp2_native_qlora_clef_place_gloo.pyindex abd33e4a3db..9693e52f35f 100644--- a/tests/test_fsdp2_native_qlora_clef_place_gloo.py+++ b/tests/test_fsdp2_native_qlora_clef_place_gloo.py@@ -13,7 +13,9 @@import multiprocessingimport os-if multiprocessing.parent_process() is not None:+if multiprocessing.parent_process() is not None or getattr(+ multiprocessing.current_process(), "_inheriting", False+):os.environ["CUDA_VISIBLE_DEVICES"] = "" # spawned gloo ranks: CPU only, also on GPU hostsimport datetime # noqa: E402diff --git a/tests/test_fsdp2_native_quant_gloo.py b/tests/test_fsdp2_native_quant_gloo.pyindex e83d9b3d16c..71c7c2648de 100644--- a/tests/test_fsdp2_native_quant_gloo.py+++ b/tests/test_fsdp2_native_quant_gloo.py@@ -22,7 +22,9 @@ import torch# Spawned gloo ranks re-import this module: keep them on the CPU (deterministic on GPU hosts; CUDA# reads the mask at its lazy first use, after this).-if __import__("multiprocessing").parent_process() is not None:+if __import__("multiprocessing").parent_process() is not None or getattr(+ __import__("multiprocessing").current_process(), "_inheriting", False+):os.environ["CUDA_VISIBLE_DEVICES"] = ""_ROOT = Path(__file__).resolve().parents[1]diff --git a/tests/test_fsdp2_native_resume_gloo.py b/tests/test_fsdp2_native_resume_gloo.pyindex 2b81d06f4f7..30454d54e13 100644--- a/tests/test_fsdp2_native_resume_gloo.py+++ b/tests/test_fsdp2_native_resume_gloo.py@@ -15,7 +15,9 @@ import torch# Spawned gloo ranks re-import this module: keep them on the CPU (deterministic on GPU hosts; CUDA# reads the mask at its lazy first use, after this).-if __import__("multiprocessing").parent_process() is not None:+if __import__("multiprocessing").parent_process() is not None or getattr(+ __import__("multiprocessing").current_process(), "_inheriting", False+):os.environ["CUDA_VISIBLE_DEVICES"] = ""sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))diff --git a/tests/test_fsdp2_native_subclass_optimizer_gloo.py b/tests/test_fsdp2_native_subclass_optimizer_gloo.pyindex 093462be393..d161745f4f1 100644--- a/tests/test_fsdp2_native_subclass_optimizer_gloo.py+++ b/tests/test_fsdp2_native_subclass_optimizer_gloo.py@@ -12,7 +12,9 @@ schedule during training."""import multiprocessingimport os-if multiprocessing.parent_process() is not None:+if multiprocessing.parent_process() is not None or getattr(+ multiprocessing.current_process(), "_inheriting", False+):os.environ["CUDA_VISIBLE_DEVICES"] = "" # spawned gloo ranks: CPU only, also on GPU hostsimport ast # noqa: E402diff --git a/tests/test_fsdp2_native_submesh_gloo.py b/tests/test_fsdp2_native_submesh_gloo.pyindex 8a17f247eae..0abdab3c7ce 100644--- a/tests/test_fsdp2_native_submesh_gloo.py+++ b/tests/test_fsdp2_native_submesh_gloo.py@@ -15,7 +15,9 @@ sharding its own model replica on its own data):import multiprocessingimport os-if multiprocessing.parent_process() is not None:+if multiprocessing.parent_process() is not None or getattr(+ multiprocessing.current_process(), "_inheriting", False+):os.environ["CUDA_VISIBLE_DEVICES"] = "" # spawned gloo ranks: CPU only, also on GPU hostsimport datetime # noqa: E402diff --git a/tests/test_fsdp2_native_trainer_args_gloo.py b/tests/test_fsdp2_native_trainer_args_gloo.pyindex 71e76e7da78..38be2a3adcc 100644--- a/tests/test_fsdp2_native_trainer_args_gloo.py+++ b/tests/test_fsdp2_native_trainer_args_gloo.py@@ -13,7 +13,9 @@ Plus the launcher env: an explicit native backend turns accelerate's FSDP launchimport multiprocessingimport os-if multiprocessing.parent_process() is not None:+if multiprocessing.parent_process() is not None or getattr(+ multiprocessing.current_process(), "_inheriting", False+):os.environ["CUDA_VISIBLE_DEVICES"] = "" # spawned gloo ranks: CPU only, also on GPU hostsimport ast # noqa: E402diff --git a/tests/test_fsdp2_native_trainer_ckpt_gloo.py b/tests/test_fsdp2_native_trainer_ckpt_gloo.pyindex 8bdfec28bda..edc8fb2663a 100644--- a/tests/test_fsdp2_native_trainer_ckpt_gloo.py+++ b/tests/test_fsdp2_native_trainer_ckpt_gloo.py@@ -12,7 +12,9 @@ Plus the CPU-only unit checks of the process-group bootstrap and the bitsandbyteimport multiprocessingimport os-if multiprocessing.parent_process() is not None:+if multiprocessing.parent_process() is not None or getattr(+ multiprocessing.current_process(), "_inheriting", False+):os.environ["CUDA_VISIBLE_DEVICES"] = "" # spawned gloo ranks: CPU only, also on GPU hostsimport ast # noqa: E402diff --git a/tests/test_fsdp2_native_trainer_fsdp_clip_gloo.py b/tests/test_fsdp2_native_trainer_fsdp_clip_gloo.pyindex 0943116e0a0..2b3ec88212f 100644--- a/tests/test_fsdp2_native_trainer_fsdp_clip_gloo.py+++ b/tests/test_fsdp2_native_trainer_fsdp_clip_gloo.py@@ -13,7 +13,9 @@import multiprocessingimport os-if multiprocessing.parent_process() is not None:+if multiprocessing.parent_process() is not None or getattr(+ multiprocessing.current_process(), "_inheriting", False+):os.environ["CUDA_VISIBLE_DEVICES"] = "" # spawned gloo ranks: CPU only, also on GPU hostsimport math # noqa: E402diff --git a/tests/test_fsdp2_native_units.py b/tests/test_fsdp2_native_units.pyindex fc0a434af4a..b1cbf0a4932 100644--- a/tests/test_fsdp2_native_units.py+++ b/tests/test_fsdp2_native_units.py@@ -16,7 +16,9 @@ import torch# Spawned gloo ranks re-import this module: keep them on the CPU (deterministic on GPU hosts; CUDA# reads the mask at its lazy first use, after this).-if __import__("multiprocessing").parent_process() is not None:+if __import__("multiprocessing").parent_process() is not None or getattr(+ __import__("multiprocessing").current_process(), "_inheriting", False+):os.environ["CUDA_VISIBLE_DEVICES"] = ""REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))diff --git a/tests/test_fsdp2_native_vision_gloo.py b/tests/test_fsdp2_native_vision_gloo.pyindex 613ef6dc70e..e35e6554072 100644--- a/tests/test_fsdp2_native_vision_gloo.py+++ b/tests/test_fsdp2_native_vision_gloo.py@@ -20,7 +20,9 @@ import torch# Spawned gloo ranks re-import this module: keep them on the CPU (deterministic on GPU hosts; CUDA# reads the mask at its lazy first use, after this).-if __import__("multiprocessing").parent_process() is not None:+if __import__("multiprocessing").parent_process() is not None or getattr(+ __import__("multiprocessing").current_process(), "_inheriting", False+):os.environ["CUDA_VISIBLE_DEVICES"] = ""REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))diff --git a/tests/test_fsdp2_native_wrappers_video_gloo.py b/tests/test_fsdp2_native_wrappers_video_gloo.pyindex 106730e0a59..1ed5898a6d3 100644--- a/tests/test_fsdp2_native_wrappers_video_gloo.py+++ b/tests/test_fsdp2_native_wrappers_video_gloo.py@@ -14,7 +14,9 @@import multiprocessingimport os-if multiprocessing.parent_process() is not None:+if multiprocessing.parent_process() is not None or getattr(+ multiprocessing.current_process(), "_inheriting", False+):os.environ["CUDA_VISIBLE_DEVICES"] = "" # spawned gloo ranks: CPU only, also on GPU hostsimport importlib # noqa: E402diff --git a/tests/test_fsdp2_perf_layout_gloo.py b/tests/test_fsdp2_perf_layout_gloo.pyindex ef52aa4971e..095d80cbc21 100644--- a/tests/test_fsdp2_perf_layout_gloo.py+++ b/tests/test_fsdp2_perf_layout_gloo.py@@ -17,7 +17,9 @@ import pytest# Spawned gloo ranks re-import this module: keep them on the CPU (deterministic on GPU hosts; CUDA# reads the mask at its lazy first use, after this).-if __import__("multiprocessing").parent_process() is not None:+if __import__("multiprocessing").parent_process() is not None or getattr(+ __import__("multiprocessing").current_process(), "_inheriting", False+):os.environ["CUDA_VISIBLE_DEVICES"] = ""torch = pytest.importorskip("torch")diff --git a/tests/test_gloo_spawn_cpu_guard.py b/tests/test_gloo_spawn_cpu_guard.pynew file mode 100644index 00000000000..bf03abe8f96--- /dev/null+++ b/tests/test_gloo_spawn_cpu_guard.py@@ -0,0 +1,45 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.++"""A spawned child imports the test module before `parent_process()` is set, so the gloo-test CPU+guard must also check `current_process()._inheriting` or the "CPU" ranks run on the GPU."""++import multiprocessing+import os++if multiprocessing.parent_process() is not None or getattr(+ multiprocessing.current_process(), "_inheriting", False+):+ os.environ["CUDA_VISIBLE_DEVICES"] = ""+ os.environ["_UNSLOTH_TEST_GUARD_FIRED"] = "1"++import glob # noqa: E402++import pytest # noqa: E402++GUARDED = "parent_process() is not None"+++def _child(out):+ out.put((os.environ.get("_UNSLOTH_TEST_GUARD_FIRED"), os.environ.get("CUDA_VISIBLE_DEVICES")))+++def test_guard_fires_in_a_spawned_child():+ ctx = multiprocessing.get_context("spawn")+ out = ctx.Queue()+ p = ctx.Process(target = _child, args = (out,))+ p.start()+ fired, mask = out.get(timeout = 120)+ p.join(timeout = 60)+ assert fired == "1" and mask == "", (fired, mask)+ assert os.environ.get("_UNSLOTH_TEST_GUARD_FIRED") is None+++def test_every_gloo_guard_also_checks_inheriting():+ here = os.path.dirname(os.path.abspath(__file__))+ stale = []+ for path in sorted(glob.glob(os.path.join(here, "*.py"))):+ text = open(path, encoding = "utf-8").read()+ if GUARDED in text and "_inheriting" not in text:+ stale.append(os.path.basename(path))+ assert not stale, f"guard without the import-time check: {stale}"diff --git a/tests/test_grpo_fsdp2_scoring_head.py b/tests/test_grpo_fsdp2_scoring_head.pyindex 4a4f85578a0..b2d0d67fc09 100644--- a/tests/test_grpo_fsdp2_scoring_head.py+++ b/tests/test_grpo_fsdp2_scoring_head.py@@ -142,8 +142,11 @@ def _worker(rank, port, separate_head, setup_source, loop_source, errors):try:os.environ.update(MASTER_ADDR = "127.0.0.1", MASTER_PORT = str(port))dist.init_process_group("gloo", rank = rank, world_size = 2)+ from torch.distributed.device_mesh import init_device_meshfrom torch.distributed.fsdp import fully_shard+ # Default mesh follows CUDA when visible, moving the model off the CPU inputs.+ mesh = init_device_mesh("cpu", (2,))torch.manual_seed(0)model = TinyCausalLM()reference_model = copy.deepcopy(model)@@ -151,9 +154,9 @@ def _worker(rank, port, separate_head, setup_source, loop_source, errors):0, VOCAB, (BATCH, SEQ), generator = torch.Generator().manual_seed(1))if separate_head:- fully_shard(model.block)- fully_shard(model.lm_head)- fully_shard(model)+ fully_shard(model.block, mesh = mesh)+ fully_shard(model.lm_head, mesh = mesh)+ fully_shard(model, mesh = mesh)if separate_head:# Mirrors unsloth/models/fsdp2.py: the root gathers units whose weights are read directly.model.register_forward_pre_hook(lambda module, args: model.lm_head.unshard())