From 34f2d149597795feb6a28240e5a76a67e093bbd8 Mon Sep 17 00:00:00 2001 From: Xinyu Lian Date: Tue, 17 Nov 2026 07:49:32 +0000 Subject: [PATCH 1/2] ZeRO-3 NVMe offload: give swapped-out parameter partitions the model dtype A parameter partition swapped out to NVMe keeps a 1-element placeholder as ds_tensor. The swapper made it fp16 whatever the model dtype was. #8843 reads ds_tensor.dtype at ZeRO-3 init, and with parameter offload it raises when the partitions hold more than one dtype. With a bf16 or fp32 model, the partitions on NVMe report fp16, while the partitions too small to swap stay in CPU memory and report bf16 or fp32. So deepspeed.initialize raises NotImplementedError: ZeRO-3 parameter offload keeps parameter partitions in one dtype; found [torch.bfloat16, torch.float16] ([torch.float16, torch.float32] for fp32). fp16 models are not affected. The placeholder now takes the swapper's dtype, which every swap buffer already has. The test file trained in bf16 only, so add an fp32 variant of test_param_and_optimizer. Fixes #9095. Signed-off-by: Xinyu Lian --- .../swap_tensor/partitioned_param_swapper.py | 2 +- tests/unit/v1/zero/test_zero3_nvme_offload.py | 21 ++++++++++++------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/deepspeed/runtime/swap_tensor/partitioned_param_swapper.py b/deepspeed/runtime/swap_tensor/partitioned_param_swapper.py index 01bccd14564d..4b8ab3f0381e 100644 --- a/deepspeed/runtime/swap_tensor/partitioned_param_swapper.py +++ b/deepspeed/runtime/swap_tensor/partitioned_param_swapper.py @@ -71,7 +71,7 @@ def __init__(self, ds_config, model_dtype): self.partitioned_swap_buffer = None self.partitioned_swap_pool = None - self.invalid_buffer = torch.tensor(1).half() + self.invalid_buffer = torch.tensor(1, dtype=self.dtype) if dist.get_rank() == 0: exclude_list = ['aio_read_handle', 'aio_write_handle', 'buffers'] diff --git a/tests/unit/v1/zero/test_zero3_nvme_offload.py b/tests/unit/v1/zero/test_zero3_nvme_offload.py index 422ef34ed9d8..0f1c4b6d1aea 100644 --- a/tests/unit/v1/zero/test_zero3_nvme_offload.py +++ b/tests/unit/v1/zero/test_zero3_nvme_offload.py @@ -27,7 +27,7 @@ def _skip_if_unsupported(): pytest.skip("bf16 is not supported") -def _config(nvme_path, optimizer_device, param_device, pipeline=False, fast_init=False): +def _config(nvme_path, optimizer_device, param_device, pipeline=False, fast_init=False, dtype=torch.bfloat16): offload_optimizer = {"device": optimizer_device} if optimizer_device == "nvme": offload_optimizer.update(nvme_path=nvme_path, @@ -51,7 +51,7 @@ def _config(nvme_path, optimizer_device, param_device, pipeline=False, fast_init } }, "bf16": { - "enabled": True + "enabled": dtype == torch.bfloat16 }, "zero_optimization": { "stage": 3, @@ -63,7 +63,7 @@ def _config(nvme_path, optimizer_device, param_device, pipeline=False, fast_init } -def _train(config): +def _train(config, dtype): with deepspeed.zero.Init(config_dict_or_path=config): torch.manual_seed(0) model = torch.nn.Sequential(torch.nn.Linear(IN, ODD), torch.nn.LayerNorm(ODD), torch.nn.Linear(ODD, HIDDEN), @@ -72,7 +72,7 @@ def _train(config): generator = torch.Generator().manual_seed(1) losses = [] for _ in range(STEPS): - x = torch.randn(BATCH, IN, generator=generator).to(engine.device, torch.bfloat16) + x = torch.randn(BATCH, IN, generator=generator).to(engine.device, dtype) y = torch.randint(0, 10, (BATCH, ), generator=generator).to(engine.device) loss = torch.nn.functional.cross_entropy(engine(x).float(), y) engine.backward(loss) @@ -86,9 +86,10 @@ def _train(config): return losses, weights -def _assert_trains_like_cpu_offload(tmpdir, optimizer_device, param_device, **nvme_options): - expected_losses, expected_weights = _train(_config(None, "cpu", "cpu" if param_device == "nvme" else "none")) - losses, weights = _train(_config(str(tmpdir), optimizer_device, param_device, **nvme_options)) +def _assert_trains_like_cpu_offload(tmpdir, optimizer_device, param_device, dtype=torch.bfloat16, **nvme_options): + expected_config = _config(None, "cpu", "cpu" if param_device == "nvme" else "none", dtype=dtype) + expected_losses, expected_weights = _train(expected_config, dtype) + losses, weights = _train(_config(str(tmpdir), optimizer_device, param_device, dtype=dtype, **nvme_options), dtype) assert losses == expected_losses, f"losses {losses}, with CPU offload {expected_losses}" for i, (weight, expected) in enumerate(zip(weights, expected_weights)): assert torch.equal(weight, expected), f"parameter {i} differs by up to {(weight - expected).abs().max()}" @@ -110,3 +111,9 @@ def test_param(self, tmpdir): def test_param_and_optimizer(self, tmpdir, fast_init): _skip_if_unsupported() _assert_trains_like_cpu_offload(tmpdir, "nvme", "nvme", fast_init=fast_init) + + # The tests above train in bf16 only (#9095). + @pytest.mark.parametrize("fast_init", [False, True]) + def test_param_and_optimizer_fp32(self, tmpdir, fast_init): + _skip_if_unsupported() + _assert_trains_like_cpu_offload(tmpdir, "nvme", "nvme", dtype=torch.float32, fast_init=fast_init) From d34fe5271c69daf7e83df3e72d81bd8203966599 Mon Sep 17 00:00:00 2001 From: Xinyu Lian Date: Tue, 17 Nov 2026 09:12:26 +0000 Subject: [PATCH 2/2] test_zero3_nvme_offload: check bf16 support only for the bf16 tests The fp32 test does not need bf16, so a device without bf16 should still run it. Signed-off-by: Xinyu Lian --- tests/unit/v1/zero/test_zero3_nvme_offload.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/unit/v1/zero/test_zero3_nvme_offload.py b/tests/unit/v1/zero/test_zero3_nvme_offload.py index 0f1c4b6d1aea..8f208106631a 100644 --- a/tests/unit/v1/zero/test_zero3_nvme_offload.py +++ b/tests/unit/v1/zero/test_zero3_nvme_offload.py @@ -18,12 +18,12 @@ BATCH, STEPS = 4, 3 -def _skip_if_unsupported(): +def _skip_if_unsupported(dtype=torch.bfloat16): if not deepspeed.ops.__compatible_ops__[AsyncIOBuilder.NAME]: pytest.skip("async_io is not compatible") if not deepspeed.ops.__compatible_ops__[CPUAdamBuilder.NAME]: pytest.skip("cpu_adam is not compatible") - if not get_accelerator().is_bf16_supported(): + if dtype == torch.bfloat16 and not get_accelerator().is_bf16_supported(): pytest.skip("bf16 is not supported") @@ -115,5 +115,5 @@ def test_param_and_optimizer(self, tmpdir, fast_init): # The tests above train in bf16 only (#9095). @pytest.mark.parametrize("fast_init", [False, True]) def test_param_and_optimizer_fp32(self, tmpdir, fast_init): - _skip_if_unsupported() + _skip_if_unsupported(torch.float32) _assert_trains_like_cpu_offload(tmpdir, "nvme", "nvme", dtype=torch.float32, fast_init=fast_init)