Skip to content

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.

Tensor parallel: batch_rebalance and group_by_length samplers give tp peers the same rows (#15752)

Daniel Han25fd237aad94
diff --git a/tests/test_tp_native_gloo.py b/tests/test_tp_native_gloo.py
index 8a4113a9d5b..251f1b6e0f9 100644
--- a/tests/test_tp_native_gloo.py
+++ b/tests/test_tp_native_gloo.py
@@ -782,6 +782,149 @@ def _case_sharded_lora_init(rank, world, tmp, ckpt):
return out
+def _case_sampler(rank, world, tmp, dp, strategy):
+ """Rows each rank's dataloader yields under a length-aware sampler, after a rank-local RNG draw."""
+ import transformers
+
+ error = _import_checkout_unsloth()
+ if error is not None:
+ return {"skip": error}
+ fields = transformers.TrainingArguments.__dataclass_fields__
+ if strategy == "batch_rebalance" and "train_sampling_strategy" not in fields:
+ return {"skip": "this transformers has no train_sampling_strategy = 'batch_rebalance'"}
+ from torch.distributed.device_mesh import init_device_mesh
+ from unsloth.models import fsdp2_native, tp_native
+ from unsloth.models._utils import patch_gradient_accumulation_fix
+
+ patch_gradient_accumulation_fix(transformers.Trainer)
+ tp = world // dp
+ model = build(LLAMA)
+ mesh = init_device_mesh("cpu", (dp, tp), mesh_dim_names = ("dp", "tp"))
+ tp_native.apply_tensor_parallel(model, mesh["tp"])
+ if dp > 1:
+ try:
+ fsdp2_native.shard_model(
+ model, fsdp2_native.FSDP2Config(param_dtype = torch.float32, mesh = mesh["dp"])
+ )
+ except Exception as e:
+ if "torch < 2.7" in str(e) or "torch 2.7+" in str(e):
+ return {"skip": "torch < 2.7: tensor parallel x FSDP2 is refused"}
+ raise
+ g = torch.Generator().manual_seed(1)
+ rows = []
+ for i in range(48):
+ ids = torch.randint(3, 100, (4 + int(torch.randint(0, 9, (1,), generator = g)),), generator = g)
+ ids[0] = 3 + i
+ rows.append(ids)
+
+ def collate(features):
+ n = max(len(f["input_ids"]) for f in features)
+ ids = torch.zeros(len(features), n, dtype = torch.long)
+ for i, f in enumerate(features):
+ ids[i, : len(f["input_ids"])] = f["input_ids"]
+ return {"input_ids": ids, "labels": ids.clone()}
+
+ if strategy == "group_by_length" and "train_sampling_strategy" not in fields:
+ overrides = {"group_by_length": True}
+ else:
+ overrides = {"train_sampling_strategy": strategy}
+ trainer = transformers.Trainer(
+ model = model,
+ args = _trainer_args(tmp, 2, dataloader_drop_last = False, **overrides),
+ train_dataset = _Rows(rows),
+ data_collator = collate,
+ )
+ torch.rand(1 + 5 * rank)
+ seen = []
+ for step, batch in enumerate(trainer.get_train_dataloader()):
+ if step == 6:
+ break
+ seen.append([int(r[0]) - 3 for r in batch["input_ids"]])
+ return {"seen": seen}
+
+
+def _case_sampler_resume(rank, world, tmp, dp, strategy):
+ """Rows per rank for an uninterrupted run vs resumes at epoch boundaries and mid epoch."""
+ import transformers
+
+ error = _import_checkout_unsloth()
+ if error is not None:
+ return {"skip": error}
+ fields = transformers.TrainingArguments.__dataclass_fields__
+ if strategy == "batch_rebalance" and "train_sampling_strategy" not in fields:
+ return {"skip": "this transformers has no train_sampling_strategy = 'batch_rebalance'"}
+ from torch.distributed.device_mesh import init_device_mesh
+ from unsloth.models import fsdp2_native, tp_native
+ from unsloth.models._utils import patch_gradient_accumulation_fix
+
+ patch_gradient_accumulation_fix(transformers.Trainer)
+ tp = world // dp
+ mesh = init_device_mesh("cpu", (dp, tp), mesh_dim_names = ("dp", "tp"))
+ g = torch.Generator().manual_seed(1)
+ rows = []
+ for i in range(12 * dp): # 3 optimizer steps per epoch on every layout
+ ids = torch.randint(3, 100, (4 + int(torch.randint(0, 9, (1,), generator = g)),), generator = g)
+ ids[0] = 3 + i
+ rows.append(ids)
+
+ def collate(features):
+ n = max(len(f["input_ids"]) for f in features)
+ ids = torch.zeros(len(features), n, dtype = torch.long)
+ for i, f in enumerate(features):
+ ids[i, : len(f["input_ids"])] = f["input_ids"]
+ return {"input_ids": ids, "labels": ids.clone()}
+
+ if strategy == "group_by_length" and "train_sampling_strategy" not in fields:
+ overrides = {"group_by_length": True}
+ else:
+ overrides = {"train_sampling_strategy": strategy}
+ ckpt = os.path.join(tmp, "ckpt")
+
+ def run(resume = None):
+ model = build(LLAMA)
+ tp_native.apply_tensor_parallel(model, mesh["tp"])
+ if dp > 1:
+ try:
+ fsdp2_native.shard_model(
+ model, fsdp2_native.FSDP2Config(param_dtype = torch.float32, mesh = mesh["dp"])
+ )
+ except Exception as e:
+ if "torch < 2.7" in str(e) or "torch 2.7+" in str(e):
+ return None
+ raise
+ args = _trainer_args(
+ ckpt,
+ 2,
+ max_steps = 8,
+ dataloader_drop_last = False,
+ save_strategy = "steps",
+ save_steps = 1,
+ logging_steps = 100,
+ **overrides,
+ )
+ trainer = transformers.Trainer(
+ model = model, args = args, train_dataset = _Rows(rows), data_collator = collate
+ )
+ seen = []
+ training_step = trainer.training_step
+
+ def record(model, inputs, *args, **kwargs):
+ seen.append([int(r[0]) - 3 for r in inputs["input_ids"]])
+ return training_step(model, inputs, *args, **kwargs)
+
+ trainer.training_step = record
+ torch.rand(1 + 5 * rank) # a rank-local draw on the global RNG
+ trainer.train(resume_from_checkpoint = resume)
+ return seen
+
+ full = run()
+ if full is None:
+ return {"skip": "torch < 2.7: tensor parallel x FSDP2 is refused"}
+ # 3 steps per epoch, 2 micro-batches per step: 3 / 6 are epoch boundaries, 4 / 7 mid epoch 1 / 2.
+ resumed = {k: run(os.path.join(ckpt, f"checkpoint-{k}")) for k in (3, 4, 6, 7)}
+ return {"full": full, "resumed": resumed}
+
+
CASES = {
"parity": _case_parity,
"save_subgroup": _case_save_subgroup,
@@ -794,6 +937,8 @@ CASES = {
"tp_fsdp2": _case_tp_fsdp2,
"tp_fsdp2_trainable_norms": _case_tp_fsdp2_trainable_norms,
"trainer": _case_trainer,
+ "sampler": _case_sampler,
+ "sampler_resume": _case_sampler_resume,
}
@@ -1217,3 +1362,34 @@ def test_trainer_trains_a_tensor_parallel_model_like_one_process(dp, tp):
assert set(saved) == set(saved_ref)
for key in saved_ref:
torch.testing.assert_close(saved[key], saved_ref[key], rtol = 1e-4, atol = 1e-6, msg = key)
+
+
[email protected]("strategy", ["batch_rebalance", "group_by_length"])
[email protected]("dp", [1, 2])
+def test_trainer_length_samplers_give_tp_peers_the_same_rows(dp, strategy):
+ results = _spawn("sampler", 2 * dp, dp = dp, strategy = strategy)
+ if "skip" in results[0]:
+ pytest.skip(results[0]["skip"])
+ for rank, res in enumerate(results):
+ assert res["seen"] and res["seen"] == results[rank ^ 1]["seen"], (
+ rank,
+ res,
+ results[rank ^ 1],
+ )
+ if dp > 1:
+ replicas = [{i for batch in results[r]["seen"] for i in batch} for r in (0, 2)]
+ assert not replicas[0] & replicas[1], replicas
+
+
[email protected]("strategy", ["batch_rebalance", "group_by_length"])
[email protected]("dp", [1, 2])
+def test_trainer_length_samplers_resume_like_an_uninterrupted_run(dp, strategy):
+ results = _spawn("sampler_resume", 2 * dp, dp = dp, strategy = strategy)
+ if "skip" in results[0]:
+ pytest.skip(results[0]["skip"])
+ for rank, res in enumerate(results):
+ peer = results[rank ^ 1]
+ assert res["full"] == peer["full"], (rank, res["full"], peer["full"])
+ for k, seen in res["resumed"].items():
+ assert seen == peer["resumed"][k], (rank, k, seen, peer["resumed"][k])
+ assert seen == res["full"][2 * k :], (rank, k, seen, res["full"][2 * k :])
diff --git a/unsloth/models/tp_native.py b/unsloth/models/tp_native.py
index 59441a54e13..acd0fa62eee 100644
--- a/unsloth/models/tp_native.py
+++ b/unsloth/models/tp_native.py
@@ -2443,6 +2443,96 @@ def _refuse_tp_trainer_args(args, cpu_shards = False):
)
+def _tp_fix_train_sampler(sampler, args, dp_size, dp_rank):
+ """Make a Trainer train sampler identical on the tp ranks of one data parallel replica."""
+ from transformers import trainer_pt_utils
+
+ rebalance = getattr(trainer_pt_utils, "BatchRebalanceSampler", None)
+ if rebalance is not None and type(sampler) is rebalance:
+ # Built from the global world size / rank, so tp peers would draw different rows: rebuild over dp.
+ grad_accum = sampler.grad_accum
+ return rebalance(
+ lengths = sampler.lengths,
+ effective_batch_size = args.train_batch_size * grad_accum * dp_size,
+ dp_size = dp_size,
+ grad_accum = grad_accum,
+ shuffle = sampler.shuffle,
+ seed = sampler.seed,
+ rank = dp_rank,
+ drop_last = sampler.drop_last,
+ )
+ if (
+ type(sampler) is trainer_pt_utils.LengthGroupedSampler
+ and getattr(sampler, "generator", None) is None
+ ):
+ # Global torch RNG desyncs across tp peers; own generator seeded seed + epoch keeps resume exact.
+ sampler.__class__ = _tp_length_grouped_sampler_class(type(sampler))
+ sampler.seed = args.data_seed if getattr(args, "data_seed", None) is not None else args.seed
+ sampler.epoch = 0
+ sampler.generator = torch.Generator().manual_seed(sampler.seed)
+ return sampler
+
+
+_TP_LENGTH_GROUPED = {}
+
+
+def _tp_length_grouped_sampler_class(base):
+ cls = _TP_LENGTH_GROUPED.get(base)
+ if cls is None:
+
+ def set_epoch(self, epoch):
+ self.epoch = int(epoch)
+
+ def __iter__(self):
+ self.generator = torch.Generator().manual_seed(self.seed + self.epoch)
+ return base.__iter__(self)
+
+ cls = type(
+ "UnslothTensorParallelLengthGroupedSampler",
+ (base,),
+ {"set_epoch": set_epoch, "__iter__": __iter__, "_unsloth_tp_epoch_seeded": True},
+ )
+ _TP_LENGTH_GROUPED[base] = cls
+ return cls
+
+
+def _tp_forward_set_epoch(data_loader):
+ """BatchSamplerShard has no set_epoch, so on resume the inner sampler would replay epoch 0: forward it."""
+ shard = getattr(data_loader, "batch_sampler", None)
+ inner = getattr(shard, "batch_sampler", None)
+ if shard is None or inner is None or hasattr(shard, "set_epoch"):
+ return
+ from transformers import trainer_pt_utils
+
+ rebalance = getattr(trainer_pt_utils, "BatchRebalanceSampler", None)
+ targets = [
+ x
+ for x in (inner, getattr(inner, "sampler", None))
+ if getattr(type(x), "_unsloth_tp_epoch_seeded", False)
+ or (rebalance is not None and type(x) is rebalance)
+ ]
+ if not targets:
+ return
+
+ def set_epoch(epoch):
+ for target in targets:
+ target.set_epoch(epoch)
+
+ shard.set_epoch = set_epoch
+
+
+def _tp_train_sampler(trainer, dp_size, dp_rank):
+ get_sampler = getattr(trainer, "_get_train_sampler", None)
+ if get_sampler is None:
+ return
+
+ @functools.wraps(get_sampler)
+ def _get_train_sampler(*args, **kwargs):
+ return _tp_fix_train_sampler(get_sampler(*args, **kwargs), trainer.args, dp_size, dp_rank)
+
+ trainer.__dict__["_get_train_sampler"] = _get_train_sampler
+
+
def tp_trainer_setup(
model = None,
phase = "pre",
@@ -2477,6 +2567,7 @@ def tp_trainer_setup(
ddp_mesh = _ddp_mesh(model)
dp_size, dp_rank = (dp_mesh.size(), dp_mesh.get_local_rank()) if dp_mesh is not None else (1, 0)
trainer.accelerator.__dict__[_TP_DATA] = (dp_size, dp_rank)
+ _tp_train_sampler(trainer, dp_size, dp_rank)
trainer.__dict__["_unsloth_tensor_parallel"] = (
True # the generated TRL trainers skip for_inference()
)
@@ -2872,9 +2963,11 @@ def _install_tp_accelerate_hooks():
},
)
try:
- return original_data_loader(self, data_loader, *args, **kwargs)
+ prepared = original_data_loader(self, data_loader, *args, **kwargs)
finally:
self.__class__ = cls
+ _tp_forward_set_epoch(prepared)
+ return prepared
def clip_grad_norm_(
self,