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
FSDP2 / DDP: only keep torchao weights FSDP2 cannot shard out of fully_shard and the DDP broadcast (#15373)
Daniel Hanf93c87183d78
diff --git a/tests/test_fp8_torchao_distributed_gloo.py b/tests/test_fp8_torchao_distributed_gloo.pyindex 3133f4945ab..cd3009b822c 100644--- a/tests/test_fp8_torchao_distributed_gloo.py+++ b/tests/test_fp8_torchao_distributed_gloo.py@@ -60,6 +60,8 @@ def _ddp_helpers():{"_ROTARY_INV_FREQ_BUFFER_NAMES","_is_tensor_subclass_weight",+ "_holds_float8",+ "_ddp_can_flatten","_frozen_tensor_subclass_names_for_ddp","_exclude_rope_inv_freq_from_ddp",},@@ -147,6 +149,72 @@ def _fsdp_case(native, rank, shard_frozen):return res+def _other_torchao_llama(kind):+ """A 256 wide llama whose MLP weights are torchao subclasses that store no float8: frozen NF4 (QLoRA)+ or trainable int8 mixed precision training weights. Both implement FSDP2's all-gather hooks."""+ import transformers++ torch.manual_seed(0)+ config = transformers.LlamaConfig(+ hidden_size = 256,+ intermediate_size = 256,+ num_hidden_layers = 1,+ num_attention_heads = 4,+ num_key_value_heads = 4,+ vocab_size = 128,+ )+ model = transformers.LlamaForCausalLM(config)+ for name, p in model.named_parameters():+ p.requires_grad_("norm" in name)+ mlp = model.model.layers[0].mlp+ for mod in (mlp.up_proj, mlp.down_proj):+ w = mod.weight.detach().clone()+ if kind == "nf4":+ from torchao.quantization.quantize_.workflows.nf4.nf4_tensor import to_nf4+ mod.weight = torch.nn.Parameter(to_nf4(w), requires_grad = False)+ else:+ from torchao.prototype.quantized_training.int8_mixed_precision import (+ Int8MixedPrecisionTrainingConfig,+ Int8MixedPrecisionTrainingLinearWeight,+ )+ mod.weight = torch.nn.Parameter(+ Int8MixedPrecisionTrainingLinearWeight(w, Int8MixedPrecisionTrainingConfig()),+ requires_grad = True,+ )+ return model+++def _fsdp_other_case(native, rank, kind):+ from torch.distributed.tensor import DTensor++ model = _other_torchao_llama(kind)+ cfg = native.FSDP2Config(param_dtype = torch.float32, shard_frozen = True)+ try:+ model = native.shard_model(model, cfg)+ except Exception as exc:+ return {"wrap": f"{type(exc).__name__}: {str(exc)[:300]}"}+ mlp = model.model.layers[0].mlp+ weights = [mlp.up_proj.weight, mlp.down_proj.weight]+ local = [w.to_local() if isinstance(w, DTensor) else w for w in weights]+ if kind == "nf4":+ nbytes = sum(t.quantized_data.numel() * t.quantized_data.element_size() for t in local)+ else:+ nbytes = sum(t._data.numel() * t._data.element_size() for t in local)+ ids = _rows(rank)+ model(input_ids = ids, labels = ids).loss.backward()+ grads = [w.grad for w in weights if w.requires_grad]+ return {+ "wrap": "ok",+ "dtensor": sum(isinstance(w, DTensor) for w in weights),+ "local_bytes": nbytes,+ "grads": len(grads),+ "finite": all(+ torch.isfinite(g.full_tensor() if isinstance(g, DTensor) else g).all().item()+ for g in grads+ ),+ }++def _ddp_case(lu, ut, rank, via):from torch.nn.parallel import DistributedDataParallel as DDP@@ -181,7 +249,7 @@ def _worker(rank, world, port, out, kind, arg):LOCAL_RANK = str(rank),LOCAL_WORLD_SIZE = str(world),)- if kind == "fsdp":+ if kind.startswith("fsdp"):os.environ["UNSLOTH_FSDP2"] = "1"import torch.distributed as dist@@ -191,6 +259,8 @@ def _worker(rank, world, port, out, kind, arg):try:if kind == "fsdp":res = _fsdp_case(_load_native(), rank, arg)+ elif kind == "fsdp_other":+ res = _fsdp_other_case(_load_native(), rank, arg)else:res = _ddp_case(*_ddp_helpers(), rank, arg)out.put((rank, res))@@ -295,3 +365,65 @@ def test_float8_training_weights_stay_with_ddp():Float8Tensor.from_hp(torch.randn(8, 8), granularity = PerRow()), requires_grad = False)assert lu._is_tensor_subclass_weight(frozen) is True+++def test_fsdp2_still_shards_frozen_nf4_weights():+ # NF4Tensor stores uint8 / int8, not float8, and implements FSDP2's hooks: with shard_frozen each rank+ # holds half the packed bytes (256 x 256 nf4 = 32768 bytes per weight), not a replicated copy.+ pytest.importorskip("torchao.quantization.quantize_.workflows.nf4.nf4_tensor")+ for rank, r in _run("fsdp_other", "nf4").items():+ assert r["wrap"] == "ok", (rank, r)+ assert r["dtensor"] == 2, (rank, r)+ assert r["local_bytes"] == 2 * 32768 // 2, (rank, r)+++def test_fsdp2_shards_trainable_int8_mixed_precision_weights():+ pytest.importorskip("torchao.prototype.quantized_training.int8_mixed_precision")+ for rank, r in _run("fsdp_other", "int8").items():+ assert r["wrap"] == "ok", (rank, r)+ assert r["dtensor"] == 2 and r["grads"] == 2 and r["finite"], (rank, r)+++def test_ddp_keeps_trainable_non_float8_torchao_weights():+ # Only float8 storage is refused for DDP training: the int8 mixed precision training weight+ # implements the flatten DDP's broadcast needs, and frozen NF4 is simply left out of the broadcast.+ nf4 = pytest.importorskip("torchao.quantization.quantize_.workflows.nf4.nf4_tensor")+ int8 = pytest.importorskip("torchao.prototype.quantized_training.int8_mixed_precision")+ lu, _ = _ddp_helpers()+ model = torch.nn.Sequential(+ torch.nn.Linear(256, 256, bias = False), torch.nn.Linear(256, 256, bias = False)+ )+ model[0].weight = torch.nn.Parameter(+ int8.Int8MixedPrecisionTrainingLinearWeight(+ model[0].weight.detach().clone(), int8.Int8MixedPrecisionTrainingConfig()+ ),+ requires_grad = True,+ )+ model[1].weight = torch.nn.Parameter(+ nf4.to_nf4(model[1].weight.detach().clone()), requires_grad = False+ )+ assert lu._frozen_tensor_subclass_names_for_ddp(model, refuse_trainable = True) == ["1.weight"]+ native = _load_native()+ assert native._float8_params(model) == set()+++def test_frozen_int8_inference_weights_stay_replicated_and_trainable_ones_refused():+ # torchao's Int8Tensor (Int8WeightOnlyConfig, the portable INT8 export) stores int8, not float8, but+ # implements no FSDP2 all-gather extension: fully_shard fails on it, so frozen ones stay replicated as+ # under #15001, and DDP's broadcast cannot flatten it, so a trainable one is refused up front.+ Q = pytest.importorskip("torchao.quantization")+ if not hasattr(Q, "Int8Tensor"):+ pytest.skip("torchao without Int8Tensor")+ lin = torch.nn.Linear(64, 64, bias = False)+ Q.quantize_(lin, Q.Int8WeightOnlyConfig(version = 2))+ assert type(lin.weight).__name__ == "Int8Tensor"+ lin.weight.requires_grad_(False)+ native = _load_native()+ assert native._float8_params(lin) == {lin.weight}+ lu, _ = _ddp_helpers()+ assert lu._frozen_tensor_subclass_names_for_ddp(lin, refuse_trainable = True) == ["weight"]+ lin.weight.requires_grad_(True)+ with pytest.raises(NotImplementedError, match = "native FSDP2 cannot shard"):+ native._float8_params(lin)+ with pytest.raises(NotImplementedError, match = "DDP cannot train"):+ lu._frozen_tensor_subclass_names_for_ddp(lin, refuse_trainable = True)diff --git a/unsloth/models/fsdp2_native.py b/unsloth/models/fsdp2_native.pyindex 8fc7657b5d0..6f5b8763058 100644--- a/unsloth/models/fsdp2_native.py+++ b/unsloth/models/fsdp2_native.py@@ -1265,11 +1265,15 @@ _FLOAT8 = tuple()-def _is_torchao_weight(t):- """A torchao tensor subclass weight (load_in_fp8's Float8Tensor, other torchao quantized tensors). It- reports the high precision dtype (bfloat16), not float8, and implements only the ops its linear needs:- fully_shard's sharding (aten.new_zeros) and DDP's flattening broadcast (reshape to [-1]) fail on it."""- return any(c.__module__.split(".")[0] == "torchao" for c in type(t).__mro__)+def _is_torchao_float8_weight(t):+ """A torchao inference weight that fully_shard cannot shard: load_in_fp8's Float8Tensor, and likewise+ Int8Tensor / IntxUnpackedToInt8Tensor / AffineQuantizedTensor / MX / NVFP4. They report the high+ precision dtype, implement only the ops their linear needs (fully_shard's aten.new_zeros / slice fail),+ and implement no FSDP2 all-gather extension. torchao subclasses that do implement it (NF4Tensor, the+ int8 / BitNet quantized training weights, torchao.float8's training weights) go to fully_shard."""+ if not any(c.__module__.split(".")[0] == "torchao" for c in type(t).__mro__):+ return False+ return not hasattr(type(t), "fsdp_pre_all_gather")def _float8_params(model):@@ -1282,7 +1286,7 @@ def _float8_params(model):# torchao's Float8 training weights (torchao.float8, enable_fsdp_float8_all_gather) implement# FSDP2's all-gather hooks, so fully_shard keeps them.if p.dtype in _FLOAT8- or (_is_torchao_weight(p) and not type(p).__module__.startswith("torchao.float8"))+ or (_is_torchao_float8_weight(p) and not type(p).__module__.startswith("torchao.float8"))]trainable = [n for n, p in found if p.requires_grad]if trainable:diff --git a/unsloth/models/loader_utils.py b/unsloth/models/loader_utils.pyindex 7b1c81d9dd0..5e445773e54 100644--- a/unsloth/models/loader_utils.py+++ b/unsloth/models/loader_utils.py@@ -4570,14 +4570,47 @@ def _is_tensor_subclass_weight(t):return False+def _holds_float8(t, depth = 0):+ """True when a tensor, or a tensor subclass's inner tensors (__tensor_flatten__), stores float8."""+ if "float8" in str(t.dtype):+ return True+ if depth > 3 or not hasattr(t, "__tensor_flatten__"):+ return False+ try:+ names = t.__tensor_flatten__()[0]+ except Exception:+ return False+ for name in names:+ inner = getattr(t, name, None)+ if isinstance(inner, torch.Tensor) and _holds_float8(inner, depth + 1):+ return True+ return False+++def _ddp_can_flatten(t):+ """DDP's start-up broadcast flattens every parameter (_flatten_dense_tensors); probe that on the weight."""+ try:+ torch._utils._flatten_dense_tensors([t.detach()])+ return True+ except Exception:+ return False++def _frozen_tensor_subclass_names_for_ddp(model, refuse_trainable = False):"""Frozen tensor subclass weights (load_in_fp8's Float8Tensor) stay out of DDP: every rank loads andquantizes the same checkpoint, so there is nothing to broadcast, and a frozen weight has no gradient toreduce. A trainable one would need both, which DDP cannot do for it: refused when asked."""frozen, trainable = [], []for name, p in model.named_parameters():- if _is_tensor_subclass_weight(p):- (trainable if p.requires_grad else frozen).append(name)+ if not _is_tensor_subclass_weight(p):+ continue+ if not p.requires_grad:+ frozen.append(name)+ elif _holds_float8(p) or not _ddp_can_flatten(p):+ # FP8 storage, and weights DDP's broadcast cannot flatten (Int8Tensor, AffineQuantizedTensor),+ # are refused: trainable subclasses that flatten (torchao's int8 mixed precision training+ # weight, NF4Tensor) train as before.+ trainable.append(name)if trainable and refuse_trainable:raise NotImplementedError(f"Unsloth: DDP cannot train the {type(model.get_parameter(trainable[0])).__name__} weight "