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.

Format four files merged without the repo formatter in #14913 and #14823 (#14930)

Daniel Hancf9438c75a6d
diff --git a/tests/test_fp8_scale_dequant_one_pass.py b/tests/test_fp8_scale_dequant_one_pass.py
index a0225d4257b..dc67c4bac6f 100644
--- a/tests/test_fp8_scale_dequant_one_pass.py
+++ b/tests/test_fp8_scale_dequant_one_pass.py
@@ -192,7 +192,9 @@ def test_compiled_dequant_reads_no_device_capability(F, monkeypatch):
raise AssertionError("device capability queried while tracing")
monkeypatch.setattr(F, "_fp8_device_lacks_kernel", no_query)
- monkeypatch.setattr(F, "_FP8_DEVICE_LACKS_KERNEL", (True,) * torch.cuda.device_count(), raising = False)
+ monkeypatch.setattr(
+ F, "_FP8_DEVICE_LACKS_KERNEL", (True,) * torch.cuda.device_count(), raising = False
+ )
q, s = _weight(64, 128)
out, ops = _compiled_graph_ops(lambda x, y: F.weight_dequant(x, y, torch.float16), q, s)
assert any("fp8_scale_dequant" in op for op in ops), ops
diff --git a/tests/test_fsdp2_merged_save_memory_cuda.py b/tests/test_fsdp2_merged_save_memory_cuda.py
index 4de09d86fcd..9a08fd16d98 100644
--- a/tests/test_fsdp2_merged_save_memory_cuda.py
+++ b/tests/test_fsdp2_merged_save_memory_cuda.py
@@ -53,12 +53,20 @@ def _model(device):
super().__init__()
torch.manual_seed(seed)
self.q = bnb.nn.Linear4bit(
- DIM, DIM, bias = False, compute_dtype = torch.bfloat16,
- quant_type = "nf4", quant_storage = torch.bfloat16,
+ DIM,
+ DIM,
+ bias = False,
+ compute_dtype = torch.bfloat16,
+ quant_type = "nf4",
+ quant_storage = torch.bfloat16,
)
self.q.weight = bnb.nn.Params4bit(
- torch.randn(DIM, DIM) * 0.02, requires_grad = False, quant_type = "nf4",
- quant_storage = torch.bfloat16, blocksize = 64, module = self.q,
+ torch.randn(DIM, DIM) * 0.02,
+ requires_grad = False,
+ quant_type = "nf4",
+ quant_storage = torch.bfloat16,
+ blocksize = 64,
+ module = self.q,
)
def forward(self, x):
@@ -96,8 +104,10 @@ def _worker(rank, world_size, port, queue):
try:
torch.cuda.set_device(rank if torch.cuda.device_count() >= world_size else 0)
os.environ.update(
- MASTER_ADDR = "127.0.0.1", MASTER_PORT = str(port),
- RANK = str(rank), WORLD_SIZE = str(world_size),
+ MASTER_ADDR = "127.0.0.1",
+ MASTER_PORT = str(port),
+ RANK = str(rank),
+ WORLD_SIZE = str(world_size),
)
import torch.distributed as dist
@@ -159,7 +169,6 @@ def _worker(rank, world_size, port, queue):
queue.put((rank, out))
except BaseException as error:
import traceback
-
queue.put((rank, {"error": f"{type(error).__name__}: {error}\n{traceback.format_exc()}"}))
@@ -171,7 +180,9 @@ def test_merged_saves_hold_one_packed_layer_on_the_writer_gpu():
ctx = mp.get_context("spawn")
queue = ctx.Queue()
port = free_port()
- procs = [ctx.Process(target = _worker, args = (r, world_size, port, queue)) for r in range(world_size)]
+ procs = [
+ ctx.Process(target = _worker, args = (r, world_size, port, queue)) for r in range(world_size)
+ ]
for p in procs:
p.start()
results = dict(queue.get(timeout = 600) for _ in procs)
diff --git a/tests/test_fsdp2_save_gloo.py b/tests/test_fsdp2_save_gloo.py
index 9967d30c373..bacd9a93f97 100644
--- a/tests/test_fsdp2_save_gloo.py
+++ b/tests/test_fsdp2_save_gloo.py
@@ -303,12 +303,20 @@ class _QLoraBlock(torch.nn.Module):
bnb = pytest.importorskip("bitsandbytes")
torch.manual_seed(seed)
self.q = bnb.nn.Linear4bit(
- 128, 128, bias = False, compute_dtype = torch.bfloat16,
- quant_type = "nf4", quant_storage = torch.bfloat16,
+ 128,
+ 128,
+ bias = False,
+ compute_dtype = torch.bfloat16,
+ quant_type = "nf4",
+ quant_storage = torch.bfloat16,
)
self.q.weight = bnb.nn.Params4bit(
- torch.randn(128, 128) * 0.05, requires_grad = False, quant_type = "nf4",
- quant_storage = torch.bfloat16, blocksize = 64, module = self.q,
+ torch.randn(128, 128) * 0.05,
+ requires_grad = False,
+ quant_type = "nf4",
+ quant_storage = torch.bfloat16,
+ blocksize = 64,
+ module = self.q,
)
self.o = torch.nn.Linear(128, 128, bias = False).requires_grad_(False)
@@ -398,9 +406,7 @@ def _case_merged_4bit_streamed(rank, world_size, m):
torch.Tensor.cpu = original_cpu
packed_moved = sum(len(s) == 2 and s[-1] == 1 for s in moved)
if writer:
- staged = (
- m.merge_4bit_lora_layer_by_layer(model) if mode == "streamed" else []
- )
+ staged = m.merge_4bit_lora_layer_by_layer(model) if mode == "streamed" else []
on_cpu_before_unload = [
layer.q.base_layer.weight.quant_state.absmax.device.type
for layer in model.base_model.model.layers
@@ -415,7 +421,8 @@ def _case_merged_4bit_streamed(rank, world_size, m):
states_cpu = on_cpu_before_unload,
same_keys = sorted(sd) == sorted(reference),
same_bytes = all(
- torch.equal(sd[k], reference[k]) if isinstance(sd[k], torch.Tensor)
+ torch.equal(sd[k], reference[k])
+ if isinstance(sd[k], torch.Tensor)
else sd[k] == reference[k]
for k in reference
),
@@ -1169,6 +1176,7 @@ def test_merged_4bit_save_streams_the_packed_base_one_layer_at_a_time():
assert all(device == "cpu" for device in streamed["states_cpu"]), streamed
# One layer on its device at a time: each merge is bracketed by its own two restages, and PEFT's
# unload merges nothing more.
- assert streamed["events"] == ["restage", "merge", "restage"] * N_LAYERS + ["unload"] + [
- "merge"
- ] * N_LAYERS, streamed["events"]
+ assert (
+ streamed["events"]
+ == ["restage", "merge", "restage"] * N_LAYERS + ["unload"] + ["merge"] * N_LAYERS
+ ), streamed["events"]
diff --git a/unsloth/models/fsdp2_save.py b/unsloth/models/fsdp2_save.py
index a1cafa42bc8..f18cd05a7e0 100644
--- a/unsloth/models/fsdp2_save.py
+++ b/unsloth/models/fsdp2_save.py
@@ -376,7 +376,9 @@ def merge_4bit_lora_layer_by_layer(model):
merged = []
for module in model.modules():
base_layer = getattr(module, "base_layer", None)
- if not isinstance(base_layer, torch.nn.Module) or not callable(getattr(module, "merge", None)):
+ if not isinstance(base_layer, torch.nn.Module) or not callable(
+ getattr(module, "merge", None)
+ ):
continue
weight = base_layer._parameters.get("weight", None)
home = staged.get(id(weight)) if weight is not None else None