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.

feat(export): GGUF shard size control, save directory override, fix private Hub push (#6107)

Leo Borcherding8f91a3f872a5
diff --git a/studio/backend/core/export/export.py b/studio/backend/core/export/export.py
index c8b26d554ad..78d12afee33 100644
--- a/studio/backend/core/export/export.py
+++ b/studio/backend/core/export/export.py
@@ -181,6 +181,17 @@ def _supports_kwarg(fn, name):
)
+def _gguf_shard_export_supported(fn):
+ """True when the exporter explicitly implements GGUF shard-size control."""
+ import inspect
+
+ try:
+ params = inspect.signature(fn).parameters
+ except (TypeError, ValueError):
+ return False
+ return _accepts_by_keyword(params, "gguf_shard_size")
+
+
def _imatrix_export_supported(save_fn):
"""True when this build can apply an imatrix, not merely swallow the keyword: the MLX binding
takes `**kwargs` and filters them, so only unsloth_zoo itself settles it."""
@@ -1063,6 +1074,7 @@ class ExportBackend:
hf_token: Optional[str] = None,
imatrix_file = None,
private: bool = False,
+ gguf_shard_size: Optional[str] = None,
) -> Tuple[bool, str, Optional[str]]:
"""
Export model in GGUF format.
@@ -1077,6 +1089,7 @@ class ExportBackend:
hf_token: Hugging Face token
imatrix_file: Optional importance matrix file path or boolean
private: Whether to make the Hub repository private
+ gguf_shard_size: Maximum final full-precision GGUF shard size
Returns:
Tuple of (success: bool, message: str, output_path: Optional[str])
@@ -1097,6 +1110,21 @@ class ExportBackend:
)
# Truthiness, as above: a disabled imatrix must not reach an exporter without the kwarg.
imatrix_kw = {"imatrix_file": imatrix_file} if imatrix_file else {}
+ shard_hooks = []
+ if save_directory:
+ shard_hooks.append(self.current_model.save_pretrained_gguf)
+ if push_to_hub:
+ shard_hooks.append(self.current_model.push_to_hub_gguf)
+ if gguf_shard_size is not None and not all(
+ _gguf_shard_export_supported(hook) for hook in shard_hooks
+ ):
+ return (
+ False,
+ "This Unsloth build does not support GGUF shard-size control. "
+ "Upgrade unsloth and unsloth_zoo, or clear the shard-size option.",
+ None,
+ )
+ shard_kw = {"gguf_shard_size": gguf_shard_size} if gguf_shard_size is not None else {}
# Resolution reads a Hub repo, so the local save needs the token too -- kept out of
# imatrix_kw, which the push below shares and already names token= itself.
local_token_kw = (
@@ -1164,6 +1192,7 @@ class ExportBackend:
quantization_method = quant_method,
**imatrix_kw,
**local_token_kw,
+ **shard_kw,
)
# Scan only the owned root; exact reported paths cover external outputs.
@@ -1270,6 +1299,7 @@ class ExportBackend:
token = hf_token,
private = private,
**imatrix_kw,
+ **shard_kw,
)
logger.info(f"GGUF model pushed successfully to {repo_id}")
diff --git a/studio/backend/core/export/orchestrator.py b/studio/backend/core/export/orchestrator.py
index 7523a8eccb9..e8f258ebe84 100644
--- a/studio/backend/core/export/orchestrator.py
+++ b/studio/backend/core/export/orchestrator.py
@@ -576,6 +576,7 @@ class ExportOrchestrator:
hf_token: Optional[str] = None,
imatrix_file = None,
private: bool = False,
+ gguf_shard_size: Optional[str] = None,
) -> Tuple[bool, str, Optional[str]]:
"""Export model in GGUF format. `quantization_method` may be a single method or a list."""
return self._run_export(
@@ -588,6 +589,7 @@ class ExportOrchestrator:
"hf_token": hf_token,
"imatrix_file": imatrix_file,
"private": private,
+ "gguf_shard_size": gguf_shard_size,
},
)
diff --git a/studio/backend/core/export/worker.py b/studio/backend/core/export/worker.py
index 2ffadbf0908..5388780bd0b 100644
--- a/studio/backend/core/export/worker.py
+++ b/studio/backend/core/export/worker.py
@@ -451,6 +451,7 @@ def _handle_export(backend, cmd: dict, resp_queue: Any) -> None:
hf_token = cmd.get("hf_token"),
imatrix_file = cmd.get("imatrix_file"),
private = cmd.get("private", False),
+ gguf_shard_size = cmd.get("gguf_shard_size"),
)
elif export_type == "lora":
success, message, output_path = backend.export_lora_adapter(
diff --git a/studio/backend/hub/schemas/inventory.py b/studio/backend/hub/schemas/inventory.py
index 04a6bc60c7d..de83251bedb 100644
--- a/studio/backend/hub/schemas/inventory.py
+++ b/studio/backend/hub/schemas/inventory.py
@@ -24,6 +24,7 @@ class GgufVariantDetail(BaseModel):
)
size_bytes: int = Field(0, description = "File size in bytes")
download_size_bytes: int = Field(0, description = "Total bytes needed to download this variant")
+ shard_count: int = Field(0, description = "Part count for a complete canonical split GGUF")
download_remaining_bytes: Optional[int] = Field(
None,
description = (
diff --git a/studio/backend/hub/services/models/gguf_variants.py b/studio/backend/hub/services/models/gguf_variants.py
index 6d616ddab2f..89f8cf3d9e7 100644
--- a/studio/backend/hub/services/models/gguf_variants.py
+++ b/studio/backend/hub/services/models/gguf_variants.py
@@ -1113,6 +1113,7 @@ async def get_gguf_variants_answer(
quant = v.quant,
display_label = v.display_label,
size_bytes = v.size_bytes,
+ shard_count = int(getattr(v, "shard_count", 0) or 0),
download_size_bytes = v.size_bytes,
download_remaining_bytes = (
None
@@ -1144,6 +1145,7 @@ async def get_gguf_variants_answer(
quant = v.quant,
display_label = v.display_label,
size_bytes = v.size_bytes,
+ shard_count = int(getattr(v, "shard_count", 0) or 0),
download_size_bytes = v.download_size_bytes or v.size_bytes,
download_remaining_bytes = variant_remaining_bytes_from_state(
response_repo_id,
@@ -1185,6 +1187,7 @@ async def get_gguf_variants_answer(
quant = v.quant,
display_label = v.display_label,
size_bytes = v.size_bytes,
+ shard_count = int(getattr(v, "shard_count", 0) or 0),
download_size_bytes = v.download_size_bytes or v.size_bytes,
download_remaining_bytes = variant_remaining_bytes_from_state(
repo_id,
@@ -1243,6 +1246,14 @@ async def get_gguf_variants_answer(
# The load resolver's own extractor over the context it reads, so the quant is
# what the echoed load resolves (the hub one differs on F16-checkpoint-Q4_K_M).
from utils.models.model_config import _extract_quant_label
+ from utils.models.model_config import colocated_split_shards
+
+ shards, split_complete = colocated_split_shards(local_target)
+ if split_complete and len(shards) > 1:
+ try:
+ size = sum(shard.stat().st_size for shard in shards)
+ except OSError:
+ size = 0
variants = [
GgufVariantInfo(
@@ -1251,6 +1262,7 @@ async def get_gguf_variants_answer(
f"{local_target.parent.name}/{local_target.name}"
),
size_bytes = size,
+ shard_count = len(shards) if split_complete and len(shards) > 1 else 0,
)
]
# The shard scan resolves a file to its marked parent, so an unmarked one walks
@@ -1604,6 +1616,7 @@ async def get_gguf_variants_answer(
quant = v.quant,
display_label = v.display_label,
size_bytes = v.size_bytes,
+ shard_count = int(getattr(v, "shard_count", 0) or 0),
download_size_bytes = (
requirement.download_size_bytes if requirement is not None else v.size_bytes
),
diff --git a/studio/backend/hub/tests/test_custom_gguf_discovery.py b/studio/backend/hub/tests/test_custom_gguf_discovery.py
index 8e760607ba1..218fbe90b75 100644
--- a/studio/backend/hub/tests/test_custom_gguf_discovery.py
+++ b/studio/backend/hub/tests/test_custom_gguf_discovery.py
@@ -312,6 +312,42 @@ def test_incomplete_direct_split_is_not_collapsed(tmp_path):
assert {Path(row.path) for row in rows} == {first, third}
+def test_grouped_split_reports_parts_without_counting_companions(tmp_path):
+ root = tmp_path / "root"
+ model = root / "model"
+ _write_gguf(model / "model-F16-00001-of-00002.gguf", 11)
+ _write_gguf(model / "model-F16-00002-of-00002.gguf", 7)
+ _write_gguf(model / "model-mmproj-F16.gguf", 3)
+ _write_gguf(model / "mtp-model.gguf", 2)
+
+ rows = _custom_rows(root)
+ variants, _has_vision = gguf.list_local_gguf_variants(str(model), model_root = str(root))
+
+ assert len(rows) == 1
+ assert [(variant.quant, variant.shard_count) for variant in variants] == [("F16", 2)]
+
+
+def test_mixed_split_and_ordinary_models_stay_separate(tmp_path):
+ root = tmp_path / "root"
+ model = root / "model"
+ _write_gguf(model / "alpha-F16-00001-of-00002.gguf")
+ _write_gguf(model / "alpha-F16-00002-of-00002.gguf")
+ _write_gguf(model / "beta-Q8_0.gguf")
+
+ rows = _custom_rows(root)
+ variants, _has_vision = gguf.list_local_gguf_variants(str(model), model_root = str(root))
+
+ assert len(rows) == 2
+ split_row = next(row for row in rows if Path(row.path).name.startswith("alpha-F16-00001"))
+ ordinary_row = next(row for row in rows if Path(row.path).name == "beta-Q8_0.gguf")
+ assert Path(split_row.path).name == "alpha-F16-00001-of-00002.gguf"
+ assert Path(ordinary_row.path).name == "beta-Q8_0.gguf"
+ assert {(variant.quant, variant.shard_count) for variant in variants} == {
+ ("F16", 2),
+ ("Q8_0", 0),
+ }
+
+
def test_two_digit_split_like_names_stay_separate(tmp_path):
root = tmp_path / "root"
holder = root / "holder"
diff --git a/studio/backend/hub/utils/gguf.py b/studio/backend/hub/utils/gguf.py
index eb52d898cd6..181504eaa78 100644
--- a/studio/backend/hub/utils/gguf.py
+++ b/studio/backend/hub/utils/gguf.py
@@ -31,6 +31,7 @@ class GgufVariantInfo:
size_bytes: int
display_label: Optional[str] = None
download_size_bytes: int = 0
+ shard_count: int = 0
GGUF_QUANT_PREFERENCE = [
@@ -73,6 +74,10 @@ GGUF_QUANT_PREFERENCE = [
]
_GGUF_SPLIT_SUFFIX_RE = re.compile(r"-\d{3,}-of-\d{3,}", re.IGNORECASE)
+_GGUF_CANONICAL_SPLIT_RE = re.compile(
+ r"^(?P<prefix>.+)-(?P<index>\d{5})-of-(?P<total>\d{5})\.gguf$",
+ re.IGNORECASE,
+)
_GGUF_QUANT_RE = re.compile(
r"(UD-)?"
r"(MXFP[0-9]+(?:_[A-Z0-9]+)*"
@@ -456,6 +461,29 @@ def gguf_variant_family(filename: str) -> str:
return _unknown_gguf_variant_key(filename)
+def complete_gguf_shard_count(filenames: Sequence[str], first_filename: str) -> int:
+ """Return a complete canonical split's part count, otherwise zero."""
+ normalized_first = first_filename.replace("\\", "/")
+ first_match = _GGUF_CANONICAL_SPLIT_RE.match(normalized_first.rsplit("/", 1)[-1])
+ if first_match is None or int(first_match.group("index")) != 1:
+ return 0
+ total = int(first_match.group("total"))
+ if total < 2:
+ return 0
+
+ family = gguf_variant_family(normalized_first).casefold()
+ indices: list[int] = []
+ for filename in filenames:
+ normalized = filename.replace("\\", "/")
+ if gguf_variant_family(normalized).casefold() != family:
+ continue
+ match = _GGUF_CANONICAL_SPLIT_RE.match(normalized.rsplit("/", 1)[-1])
+ if match is None or match.group("total") != first_match.group("total"):
+ return 0
+ indices.append(int(match.group("index")))
+ return total if sorted(indices) == list(range(1, total + 1)) else 0
+
+
def gguf_checkpoint_family(filename: str) -> Optional[str]:
"""the exact checkpoint name shared by quant variants, or ``None`` when unnamed."""
path = filename.replace("\\", "/")
@@ -1210,6 +1238,7 @@ def list_partial_gguf_variants_from_state(
)
)
main_filename: Optional[str] = None
+ main_filenames: list[str] = []
size_bytes = 0
companion_bytes = 0
imatrix_only = False
@@ -1234,6 +1263,7 @@ def list_partial_gguf_variants_from_state(
continue
if main_filename is None:
main_filename = expected.path
+ main_filenames.append(expected.path)
size_bytes += max(0, int(expected.size or 0))
if main_filename is None:
# An older build could download the imatrix as a variant of its own, so its
@@ -1250,6 +1280,7 @@ def list_partial_gguf_variants_from_state(
quant = variant,
size_bytes = size_bytes,
download_size_bytes = size_bytes + companion_bytes,
+ shard_count = complete_gguf_shard_count(main_filenames, main_filename),
)
)
@@ -1362,7 +1393,12 @@ def list_gguf_variants(
main_files.append((filename, int(getattr(sibling, "size", 0) or 0)))
variants = [
- GgufVariantInfo(filename = filename, quant = quant, size_bytes = size)
+ GgufVariantInfo(
+ filename = filename,
+ quant = quant,
+ size_bytes = size,
+ shard_count = complete_gguf_shard_count([path for path, _size in main_files], filename),
+ )
for quant, (filename, size) in group_gguf_variant_files(main_files).items()
]
@@ -1446,7 +1482,12 @@ def list_local_gguf_variants(
main_files.append((rel, size))
variants = [
- GgufVariantInfo(filename = filename, quant = quant, size_bytes = size)
+ GgufVariantInfo(
+ filename = filename,
+ quant = quant,
+ size_bytes = size,
+ shard_count = complete_gguf_shard_count([path for path, _size in main_files], filename),
+ )
for quant, (filename, size) in group_gguf_variant_files(main_files).items()
]
variants.sort(key = lambda variant: -variant.size_bytes)
diff --git a/studio/backend/mcp_server.py b/studio/backend/mcp_server.py
index ba23762ae12..e950842fcf5 100644
--- a/studio/backend/mcp_server.py
+++ b/studio/backend/mcp_server.py
@@ -233,6 +233,7 @@ def create_studio_mcp() -> FastMCP:
imatrix: bool = False,
imatrix_path: str | None = None,
private: bool = False,
+ gguf_shard_size: str | None = None,
) -> dict[str, Any]:
"""Export the loaded model to GGUF using Unsloth's existing path validation.
@@ -253,6 +254,7 @@ def create_studio_mcp() -> FastMCP:
imatrix = imatrix,
imatrix_path = imatrix_path,
private = private,
+ gguf_shard_size = gguf_shard_size,
)
return _dump(await export(request, current_subject = "mcp"))
diff --git a/studio/backend/models/export.py b/studio/backend/models/export.py
index 426a8067d1e..3be2d1e3af6 100644
--- a/studio/backend/models/export.py
+++ b/studio/backend/models/export.py
@@ -3,6 +3,8 @@
"""Pydantic schemas for Export API."""
+import re
+import sys
from pathlib import Path, PureWindowsPath
from pydantic import BaseModel, Field, field_validator
@@ -33,6 +35,31 @@ def _validate_save_directory(value: str) -> str:
return raw
+_GGUF_SHARD_SIZE_RE = re.compile(r"^(\d+)\s*([MG])B?$", re.IGNORECASE)
+
+
+def _validate_gguf_shard_size(value: Optional[str]) -> Optional[str]:
+ """Validate and normalize a Studio GGUF shard-size request."""
+ if value is None:
+ return None
+ if not isinstance(value, str):
+ raise ValueError("gguf_shard_size must be a string or null")
+ raw = value.strip()
+ if raw.casefold() in ("", "0", "none"):
+ return "0"
+ match = _GGUF_SHARD_SIZE_RE.fullmatch(raw)
+ if match is None:
+ raise ValueError("gguf_shard_size must be a positive whole number in MB or GB, or '0'")
+ magnitude = int(match.group(1))
+ unit = match.group(2).upper()
+ if magnitude == 0:
+ raise ValueError("gguf_shard_size must be positive, or exactly '0'")
+ multiplier = 1_000_000 if unit == "M" else 1_000_000_000
+ if magnitude > sys.maxsize // multiplier:
+ raise ValueError("gguf_shard_size is too large for this platform")
+ return f"{magnitude}{unit}B"
+
+
class LoadCheckpointRequest(BaseModel):
"""Request for loading a checkpoint into the export backend."""
@@ -224,6 +251,17 @@ class ExportGGUFRequest(BaseModel):
None,
description = "Path to a custom imatrix file; overrides the auto-download when set.",
)
+ gguf_shard_size: Optional[str] = Field(
+ None,
+ description = "Maximum final f32, f16 or bf16 GGUF shard size in MB or GB. "
+ "Pass '0' for one file. Quantized outputs remain single-file.",
+ )
+
+ @field_validator("gguf_shard_size", mode = "before")
+ @classmethod
+ def _check_gguf_shard_size(cls, value):
+ return _validate_gguf_shard_size(value)
+
private: bool = Field(
False,
description = "If True, create a private Hugging Face Hub repository",
diff --git a/studio/backend/models/models.py b/studio/backend/models/models.py
index 9a40c364fba..33468d70550 100644
--- a/studio/backend/models/models.py
+++ b/studio/backend/models/models.py
@@ -161,6 +161,7 @@ class GgufVariantDetail(BaseModel):
)
size_bytes: int = Field(0, description = "File size in bytes")
download_size_bytes: int = Field(0, description = "Total bytes needed to download this variant")
+ shard_count: int = Field(0, description = "Part count for a complete canonical split GGUF")
downloaded: bool = Field(
False, description = "Whether this variant is already in the local HF cache"
)
diff --git a/studio/backend/routes/export.py b/studio/backend/routes/export.py
index a30fedc7203..2ae05e67f4b 100644
--- a/studio/backend/routes/export.py
+++ b/studio/backend/routes/export.py
@@ -412,6 +412,7 @@ async def export_gguf(
hf_token = request.hf_token,
imatrix_file = imatrix_file,
private = request.private,
+ gguf_shard_size = request.gguf_shard_size,
)
if not success:
diff --git a/studio/backend/routes/models.py b/studio/backend/routes/models.py
index 06b01bfbd3d..756608ecf11 100644
--- a/studio/backend/routes/models.py
+++ b/studio/backend/routes/models.py
@@ -4469,6 +4469,7 @@ async def get_gguf_variants(
# the row reads as its whole relative path.
display_label = getattr(v, "display_label", None),
size_bytes = v.size_bytes,
+ shard_count = int(getattr(v, "shard_count", 0) or 0),
download_size_bytes = int(
getattr(v, "download_size_bytes", v.size_bytes) or v.size_bytes
),
diff --git a/studio/backend/tests/test_export_gguf_sharding.py b/studio/backend/tests/test_export_gguf_sharding.py
new file mode 100644
index 00000000000..d0db4ce9d93
--- /dev/null
+++ b/studio/backend/tests/test_export_gguf_sharding.py
@@ -0,0 +1,195 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+from __future__ import annotations
+
+import importlib.util
+from pathlib import Path
+
+import pytest
+from pydantic import ValidationError
+
+from models.export import ExportGGUFRequest
+
+
+_HELPERS_SPEC = importlib.util.spec_from_file_location(
+ "export_absolute_path_helpers",
+ Path(__file__).with_name("test_export_absolute_paths.py"),
+)
+assert _HELPERS_SPEC is not None and _HELPERS_SPEC.loader is not None
+_HELPERS = importlib.util.module_from_spec(_HELPERS_SPEC)
+_HELPERS_SPEC.loader.exec_module(_HELPERS)
+_install_export_backend_stubs = _HELPERS._install_export_backend_stubs
+_load_module = _HELPERS._load_module
+
+
+ "value, expected",
+ [
+ (None, None),
+ ("", "0"),
+ ("none", "0"),
+ ("0", "0"),
+ ("500m", "500MB"),
+ (" 4 GB ", "4GB"),
+ ],
+)
+def test_gguf_request_normalizes_shard_size(value, expected):
+ request = ExportGGUFRequest(save_directory = "/tmp/export", gguf_shard_size = value)
+ assert request.gguf_shard_size == expected
+
+
+ "value",
+ ["0MB", "0GB", "1.5GB", "512", "64KB", "-2GB", "4TB", "4GBx"],
+)
+def test_gguf_request_rejects_invalid_shard_size(value):
+ with pytest.raises(ValidationError, match = "gguf_shard_size"):
+ ExportGGUFRequest(save_directory = "/tmp/export", gguf_shard_size = value)
+
+
+def test_orchestrator_preserves_shard_size_in_command():
+ from core.export.orchestrator import ExportOrchestrator
+
+ orchestrator = ExportOrchestrator.__new__(ExportOrchestrator)
+ seen = {}
+
+ def run_export(kind, params):
+ seen.update(kind = kind, params = params)
+ return True, "ok", None
+
+ orchestrator._run_export = run_export
+
+ result = orchestrator.export_gguf("output", gguf_shard_size = "2GB", private = True)
+
+ assert result == (True, "ok", None)
+ assert seen["kind"] == "gguf"
+ assert seen["params"]["gguf_shard_size"] == "2GB"
+ assert seen["params"]["private"] is True
+
+
+def test_worker_passes_shard_size_to_backend():
+ from core.export import worker
+
+ seen = {}
+
+ class Backend:
+ def export_gguf(self, **kwargs):
+ seen.update(kwargs)
+ return True, "ok", "/output"
+
+ class Queue:
+ def __init__(self):
+ self.items = []
+
+ def put(self, item):
+ self.items.append(item)
+
+ queue = Queue()
+ worker._handle_export(
+ Backend(),
+ {
+ "export_type": "gguf",
+ "save_directory": "/output",
+ "gguf_shard_size": "512MB",
+ "private": True,
+ },
+ queue,
+ )
+
+ assert seen["gguf_shard_size"] == "512MB"
+ assert seen["private"] is True
+ assert queue.items[-1]["success"] is True
+
+
+def test_backend_forwards_shard_size_to_local_and_hub_exports(tmp_path, monkeypatch):
+ _install_export_backend_stubs(monkeypatch)
+ export_module = _load_module(
+ "test_export_gguf_shard_backend",
+ "core/export/export.py",
+ monkeypatch,
+ )
+ save_directory = tmp_path / "export with spaces ü"
+ seen = {}
+
+ class Model:
+ def save_pretrained_gguf(
+ self,
+ model_save_path,
+ tokenizer,
+ quantization_method,
+ gguf_shard_size = None,
+ ):
+ seen["local"] = gguf_shard_size
+ output = Path(f"{model_save_path}_gguf")
+ output.mkdir(parents = True)
+ shard = output / "model.F16-00001-of-00002.gguf"
+ shard.write_bytes(b"GGUF")
+ return {"gguf_files": [str(shard)]}
+
+ def push_to_hub_gguf(
+ self,
+ repo_id,
+ tokenizer,
+ quantization_method,
+ token,
+ private,
+ gguf_shard_size = None,
+ ):
+ seen["hub"] = {
+ "repo_id": repo_id,
+ "private": private,
+ "gguf_shard_size": gguf_shard_size,
+ }
+
+ monkeypatch.setattr(export_module, "resolve_export_write_dir", lambda value: Path(value))
+ backend = export_module.ExportBackend.__new__(export_module.ExportBackend)
+ backend.current_model = Model()
+ backend.current_tokenizer = object()
+ backend.current_checkpoint = None
+
+ success, message, output_path = backend.export_gguf(
+ str(save_directory),
+ "F16",
+ push_to_hub = True,
+ repo_id = "owner/model",
+ hf_token = "token",
+ private = True,
+ gguf_shard_size = "512MB",
+ )
+
+ assert success is True, message
+ assert output_path == str(save_directory.resolve())
+ assert seen["local"] == "512MB"
+ assert seen["hub"] == {"repo_id": "owner/model", "private": True, "gguf_shard_size": "512MB"}
+
+
+def test_backend_rejects_old_exporter_only_when_option_is_set(tmp_path, monkeypatch):
+ _install_export_backend_stubs(monkeypatch)
+ export_module = _load_module(
+ "test_export_gguf_old_backend",
+ "core/export/export.py",
+ monkeypatch,
+ )
+
+ class OldModel:
+ def save_pretrained_gguf(self, model_save_path, tokenizer, quantization_method):
+ output = Path(f"{model_save_path}_gguf")
+ output.mkdir(parents = True)
+ (output / "model.Q4_K_M.gguf").write_bytes(b"GGUF")
+
+ monkeypatch.setattr(export_module, "resolve_export_write_dir", lambda value: Path(value))
+ backend = export_module.ExportBackend.__new__(export_module.ExportBackend)
+ backend.current_model = OldModel()
+ backend.current_tokenizer = object()
+ backend.current_checkpoint = None
+
+ unsupported = backend.export_gguf(
+ str(tmp_path / "unsupported"),
+ gguf_shard_size = "0",
+ )
+ compatible = backend.export_gguf(str(tmp_path / "compatible"))
+
+ assert unsupported[0] is False
+ assert "does not support GGUF shard-size control" in unsupported[1]
+ assert compatible[0] is True
diff --git a/studio/backend/tests/test_gguf_variants_local_resolution.py b/studio/backend/tests/test_gguf_variants_local_resolution.py
index 2dbdd826717..ccb9ad13b19 100644
--- a/studio/backend/tests/test_gguf_variants_local_resolution.py
+++ b/studio/backend/tests/test_gguf_variants_local_resolution.py
@@ -45,6 +45,16 @@ def test_direct_gguf_file_is_a_loadable_variant(in_tmp_cwd):
assert response.variants[0].partial is False
+def test_direct_complete_split_reports_the_whole_family_size(in_tmp_cwd):
+ first = in_tmp_cwd / "foo-Q4_K_M-00001-of-00002.gguf"
+ first.write_bytes(b"GGUF")
+ (in_tmp_cwd / "foo-Q4_K_M-00002-of-00002.gguf").write_bytes(b"GGUF" * 2)
+
+ row = _variants(os.fspath(first)).variants[0]
+ assert row.size_bytes == 12
+ assert row.shard_count == 2
+
+
def test_markerless_relative_gguf_file_resolves_locally(in_tmp_cwd):
(in_tmp_cwd / "models").mkdir()
(in_tmp_cwd / "models" / "foo.gguf").write_bytes(b"GGUF")
diff --git a/studio/backend/tests/test_mcp_server.py b/studio/backend/tests/test_mcp_server.py
index 572c5c590d3..067c0caa129 100644
--- a/studio/backend/tests/test_mcp_server.py
+++ b/studio/backend/tests/test_mcp_server.py
@@ -176,7 +176,7 @@ def test_clamp_restricts_to_inclusive_bounds():
def test_export_and_checkpoint_tools_expose_forwarded_fields():
export_props = set(_get_tool("export_gguf").parameters["properties"])
- assert {"hf_token", "imatrix", "imatrix_path", "private"} <= export_props
+ assert {"hf_token", "imatrix", "imatrix_path", "private", "gguf_shard_size"} <= export_props
checkpoint_props = set(_get_tool("load_checkpoint").parameters["properties"])
assert {"hf_token", "approved_remote_code_fingerprint"} <= checkpoint_props
@@ -220,6 +220,7 @@ def test_export_gguf_forwards_hf_token_and_imatrix(monkeypatch):
imatrix = True,
imatrix_path = "/tmp/imatrix.dat",
private = True,
+ gguf_shard_size = "2GB",
)
)
@@ -228,6 +229,7 @@ def test_export_gguf_forwards_hf_token_and_imatrix(monkeypatch):
assert captured["imatrix_path"] == "/tmp/imatrix.dat"
assert captured["quantization_method"] == ["Q4_K_M", "Q8_0"]
assert captured["private"] is True
+ assert captured["gguf_shard_size"] == "2GB"
assert result["current_subject"] == "mcp"
diff --git a/studio/frontend/src/features/chat/types/api.ts b/studio/frontend/src/features/chat/types/api.ts
index 07439761428..e7e78ad4809 100644
--- a/studio/frontend/src/features/chat/types/api.ts
+++ b/studio/frontend/src/features/chat/types/api.ts
@@ -179,6 +179,7 @@ export interface GgufVariantDetail {
display_label?: string | null;
size_bytes: number;
download_size_bytes?: number;
+ shard_count?: number;
downloaded?: boolean;
update_available?: boolean;
/** An interrupted download: some shards are missing, so it cannot load yet. */
diff --git a/studio/frontend/src/features/export/api/export-api.ts b/studio/frontend/src/features/export/api/export-api.ts
index ed9ef04e543..eee9d6eb472 100644
--- a/studio/frontend/src/features/export/api/export-api.ts
+++ b/studio/frontend/src/features/export/api/export-api.ts
@@ -169,6 +169,7 @@ export async function exportGGUF(params: {
imatrix?: boolean;
imatrix_path?: string | null;
private?: boolean;
+ gguf_shard_size?: string | null;
}): Promise<ExportOperationResponse> {
const response = await authFetch("/api/export/export/gguf", {
method: "POST",
diff --git a/studio/frontend/src/features/export/components/export-run-panel.tsx b/studio/frontend/src/features/export/components/export-run-panel.tsx
index 4aba1c624f1..c00833f5684 100644
--- a/studio/frontend/src/features/export/components/export-run-panel.tsx
+++ b/studio/frontend/src/features/export/components/export-run-panel.tsx
@@ -9,6 +9,13 @@ import {
InputGroupInput,
} from "@/components/ui/input-group";
import { Progress } from "@/components/ui/progress";
+import {
+ Select,
+ SelectContent,
+ SelectItem,
+ SelectTrigger,
+ SelectValue,
+} from "@/components/ui/select";
import { Spinner } from "@/components/ui/spinner";
import { Switch } from "@/components/ui/switch";
import {
@@ -17,6 +24,7 @@ import {
TooltipTrigger,
} from "@/components/ui/tooltip";
import { FolderBrowser } from "@/features/model-picker";
+import { cn } from "@/lib/utils";
import {
AlertCircleIcon,
ArrowRight01Icon,
@@ -34,6 +42,11 @@ import {
type ExportMethod,
findMergedFormat,
} from "../constants";
+import {
+ GGUF_SHARD_SIZE_PRESETS,
+ type GgufShardMode,
+ isValidGgufShardSize,
+} from "../lib/gguf-shard-size";
import { getExportLogLineClass } from "../lib/log-style";
import {
type ExportDestination,
@@ -113,6 +126,11 @@ export interface ExportRunPanelProps {
onHfTokenChange: (v: string) => void;
privateRepo: boolean;
onPrivateRepoChange: (v: boolean) => void;
+ supportsGgufSharding: boolean;
+ ggufShardMode: GgufShardMode;
+ onGgufShardModeChange: (v: GgufShardMode) => void;
+ ggufShardSize: string;
+ onGgufShardSizeChange: (v: string) => void;
/** Kick off the export (the page assembles params and calls the store). */
onStart: () => void;
/** Collapse the panel; only offered before a run or after a terminal one. */
@@ -140,6 +158,11 @@ export function ExportRunPanel(props: ExportRunPanelProps) {
onHfTokenChange,
privateRepo,
onPrivateRepoChange,
+ supportsGgufSharding,
+ ggufShardMode,
+ onGgufShardModeChange,
+ ggufShardSize,
+ onGgufShardSizeChange,
onStart,
onClose,
} = props;
@@ -212,6 +235,13 @@ export function ExportRunPanel(props: ExportRunPanelProps) {
(v) => findMergedFormat(v)?.label ?? v,
);
const showProgress = isExporting || isTerminal;
+ const shardSizeValid =
+ !supportsGgufSharding ||
+ ggufShardMode === "single" ||
+ isValidGgufShardSize(ggufShardSize);
+ const customShardSize = !GGUF_SHARD_SIZE_PRESETS.some(
+ (preset) => preset === ggufShardSize,
+ );
return (
<div className="flex flex-col gap-4 rounded-2xl border border-border/50 bg-muted/20 p-4">
@@ -316,6 +346,91 @@ export function ExportRunPanel(props: ExportRunPanelProps) {
</div>
)}
+ {supportsGgufSharding && (
+ <fieldset className="flex flex-col gap-1.5">
+ <legend className="text-xs font-medium text-muted-foreground">
+ Full-precision GGUF files
+ </legend>
+ <div className="flex gap-2">
+ <Button
+ type="button"
+ variant={ggufShardMode === "single" ? "dark" : "outline"}
+ onClick={() => onGgufShardModeChange("single")}
+ aria-pressed={ggufShardMode === "single"}
+ className="flex-1"
+ >
+ Single file
+ </Button>
+ <Button
+ type="button"
+ variant={ggufShardMode === "split" ? "dark" : "outline"}
+ onClick={() => onGgufShardModeChange("split")}
+ aria-pressed={ggufShardMode === "split"}
+ className="flex-1"
+ >
+ Split into shards
+ </Button>
+ </div>
+ {ggufShardMode === "split" ? (
+ <>
+ <Select
+ value={customShardSize ? "custom" : ggufShardSize}
+ onValueChange={(value) =>
+ onGgufShardSizeChange(value === "custom" ? "" : value)
+ }
+ >
+ <SelectTrigger className="w-full">
+ <SelectValue placeholder="Choose a shard size" />
+ </SelectTrigger>
+ <SelectContent>
+ {GGUF_SHARD_SIZE_PRESETS.map((preset) => (
+ <SelectItem key={preset} value={preset}>
+ {preset.replace("GB", " GB").replace("MB", " MB")}
+ </SelectItem>
+ ))}
+ <SelectItem value="custom">Custom</SelectItem>
+ </SelectContent>
+ </Select>
+ {customShardSize && (
+ <Input
+ className={cn(
+ "font-mono text-ui-12",
+ !shardSizeValid &&
+ "border-destructive focus-visible:ring-destructive",
+ )}
+ value={ggufShardSize}
+ onChange={(event) =>
+ onGgufShardSizeChange(event.target.value)
+ }
+ spellCheck={false}
+ aria-invalid={!shardSizeValid}
+ aria-describedby="gguf-shard-size-help"
+ maxLength={24}
+ placeholder="e.g. 512MB or 6GB"
+ />
+ )}
+ <p
+ id="gguf-shard-size-help"
+ className={cn(
+ "text-ui-11",
+ shardSizeValid
+ ? "text-muted-foreground/70"
+ : "text-destructive",
+ )}
+ >
+ {shardSizeValid
+ ? "F16 and BF16 outputs use this limit. Quantized outputs and companion files stay single-file."
+ : "Enter a positive whole number in MB or GB, such as 512MB or 4GB."}
+ </p>
+ </>
+ ) : (
+ <p className="text-ui-11 text-muted-foreground/70">
+ F16 and BF16 outputs stay in one file regardless of size.
+ </p>
+ )}
+ </fieldset>
+ )}
+
{destination === "hub" && (
<div className="flex flex-col gap-4 px-0.5">
<div className="grid grid-cols-1 gap-3 sm:grid-cols-2">
@@ -497,6 +612,16 @@ export function ExportRunPanel(props: ExportRunPanelProps) {
</span>
</div>
)}
+ {summaryMethod === "gguf" && summary?.ggufShardSize && (
+ <div className="flex justify-between">
+ <span>Full-precision files</span>
+ <span className="font-medium text-foreground">
+ {summary.ggufShardSize === "0"
+ ? "Single file"
+ : `Shards up to ${summary.ggufShardSize}`}
+ </span>
+ </div>
+ )}
</div>
{/* Progress */}
@@ -605,7 +730,9 @@ export function ExportRunPanel(props: ExportRunPanelProps) {
<Button variant="outline" onClick={onClose}>
Cancel
</Button>
- <Button onClick={onStart}>Start Export</Button>
+ <Button onClick={onStart} disabled={!shardSizeValid}>
+ Start Export
+ </Button>
</>
)}
{isExporting && (
diff --git a/studio/frontend/src/features/export/export-page.tsx b/studio/frontend/src/features/export/export-page.tsx
index df90180c0a5..f8b8d0e38c0 100644
--- a/studio/frontend/src/features/export/export-page.tsx
+++ b/studio/frontend/src/features/export/export-page.tsx
@@ -84,6 +84,12 @@ import {
refreshLocalModels,
} from "./export-navigation-cache";
import { useExportSizeEstimate } from "./hooks/use-export-size-estimate";
+import {
+ type GgufShardMode,
+ ggufShardSaveDirectory,
+ isValidGgufShardSize,
+ normalizeGgufShardSize,
+} from "./lib/gguf-shard-size";
import {
isExportPanelActive,
useExportRuntimeStore,
@@ -274,6 +280,8 @@ export function ExportPage() {
const [hfUsername, setHfUsername] = useState("");
const [modelName, setModelName] = useState("");
const [privateRepo, setPrivateRepo] = useState(false);
+ const [ggufShardMode, setGgufShardMode] = useState<GgufShardMode>("single");
+ const [ggufShardSize, setGgufShardSize] = useState("4GB");
// Export run state lives in the global runtime store so it keeps streaming in the background.
const runExport = useExportRuntimeStore((s) => s.runExport);
@@ -585,7 +593,7 @@ export function ExportPage() {
const estimatedSize = getEstimatedSize(exportMethod, quantLevels, fp16Bytes);
const selectedExportSource =
sourceMode === "checkpoint" ? checkpoint : selectedSourceModel;
- const defaultSaveDirectory = useMemo(() => {
+ const baseDefaultSaveDirectory = useMemo(() => {
const relative = buildRelativeSaveDirectory(
exportMethod,
sourceMode,
@@ -618,7 +626,6 @@ export function ExportPage() {
sourceBaseModelName,
sourceMode,
]);
- const saveDirectory = customSaveDirectory?.trim() || defaultSaveDirectory;
// Each merged format uploads a full model to the repo root, so several to one repo would collide.
// GGUF method exporting an adapter checkpoint as a GGUF LoRA; reuses the LoRA export path.
const ggufAsLora =
@@ -626,6 +633,25 @@ export function ExportPage() {
ggufTarget === "lora" &&
effectiveIsAdapter &&
!isMacHost;
+ const supportsGgufSharding =
+ exportMethod === "gguf" &&
+ !ggufAsLora &&
+ quantLevels.some((quant) => quant === "f16" || quant === "bf16");
+ const normalizedGgufShardSize =
+ supportsGgufSharding && ggufShardMode === "split"
+ ? normalizeGgufShardSize(ggufShardSize)
+ : supportsGgufSharding
+ ? "0"
+ : null;
+ const defaultSaveDirectory = ggufShardSaveDirectory(
+ baseDefaultSaveDirectory,
+ normalizedGgufShardSize,
+ );
+ const saveDirectory = customSaveDirectory?.trim() || defaultSaveDirectory;
+ const ggufShardSizeValid =
+ !supportsGgufSharding ||
+ ggufShardMode === "single" ||
+ isValidGgufShardSize(ggufShardSize);
// Restrict a Hub merged export to a single format; multi-format stays available for local export.
const hubMultiFormat =
@@ -638,6 +664,7 @@ export function ExportPage() {
exportMethod &&
!exportUnsupported &&
!hubMultiFormat &&
+ ggufShardSizeValid &&
(exportMethod !== "gguf" || ggufAsLora || quantLevels.length > 0) &&
(exportMethod !== "merged" || selectedFormats.length > 0)
);
@@ -717,6 +744,7 @@ export function ExportPage() {
if (exportMethod === "gguf" && !ggufAsLora && quantLevels.length === 0)
return;
if (exportMethod === "merged" && selectedFormats.length === 0) return;
+ if (!ggufShardSizeValid) return;
// A Hub merged push writes each format to the repo root; several would collide (mirrors canExport).
if (hubMultiFormat) return;
@@ -780,6 +808,7 @@ export function ExportPage() {
isAdapter: adapterExport,
quantLevels,
useImatrix: effectiveImatrix,
+ ggufShardSize: normalizedGgufShardSize,
mergedSelections: selectedFormats.map((v) => ({
...mergedFormatPayload(v),
label: MERGED_FORMATS.find((f) => f.value === v)?.label ?? v,
@@ -798,6 +827,7 @@ export function ExportPage() {
methodLabel,
method: effectiveMethod,
quantLevels,
+ ggufShardSize: normalizedGgufShardSize,
mergedFormats: exportMethod === "merged" ? selectedFormats : [],
destination,
},
@@ -814,6 +844,8 @@ export function ExportPage() {
isAdapter,
quantLevels,
effectiveImatrix,
+ normalizedGgufShardSize,
+ ggufShardSizeValid,
selectedFormats,
hubMultiFormat,
ggufAsLora,
@@ -1705,6 +1737,11 @@ export function ExportPage() {
onHfTokenChange={setHfToken}
privateRepo={privateRepo}
onPrivateRepoChange={setPrivateRepo}
+ supportsGgufSharding={supportsGgufSharding}
+ ggufShardMode={ggufShardMode}
+ onGgufShardModeChange={setGgufShardMode}
+ ggufShardSize={ggufShardSize}
+ onGgufShardSizeChange={setGgufShardSize}
onStart={handleStart}
onClose={handleClosePanel}
/>
diff --git a/studio/frontend/src/features/export/lib/gguf-shard-size.ts b/studio/frontend/src/features/export/lib/gguf-shard-size.ts
new file mode 100644
index 00000000000..4356d42eaa9
--- /dev/null
+++ b/studio/frontend/src/features/export/lib/gguf-shard-size.ts
@@ -0,0 +1,47 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+export type GgufShardMode = "single" | "split";
+
+export const GGUF_SHARD_SIZE_PRESETS = [
+ "256MB",
+ "512MB",
+ "1GB",
+ "2GB",
+ "4GB",
+ "8GB",
+ "16GB",
+] as const;
+
+const GGUF_SHARD_SIZE_RE = /^(\d+)\s*([MG])B?$/i;
+const MAX_PATH_COMPONENT_LENGTH = 255;
+
+export function normalizeGgufShardSize(value: string): string | null {
+ const match = GGUF_SHARD_SIZE_RE.exec(value.trim());
+ if (!match || BigInt(match[1]) === 0n) {
+ return null;
+ }
+ return `${BigInt(match[1])}${match[2].toUpperCase()}B`;
+}
+
+export function isValidGgufShardSize(value: string): boolean {
+ return normalizeGgufShardSize(value) !== null;
+}
+
+export function ggufShardSaveDirectory(
+ baseDirectory: string,
+ shardSize: string | null,
+): string {
+ if (!shardSize || shardSize === "0") {
+ return baseDirectory;
+ }
+
+ const suffix = `-split-${shardSize}`;
+ const separator = Math.max(
+ baseDirectory.lastIndexOf("/"),
+ baseDirectory.lastIndexOf("\\"),
+ );
+ const parent = baseDirectory.slice(0, separator + 1);
+ const name = baseDirectory.slice(separator + 1);
+ return `${parent}${name.slice(0, MAX_PATH_COMPONENT_LENGTH - suffix.length)}${suffix}`;
+}
diff --git a/studio/frontend/src/features/export/stores/export-runtime-store.ts b/studio/frontend/src/features/export/stores/export-runtime-store.ts
index 5c93ba90c68..53a4e22c989 100644
--- a/studio/frontend/src/features/export/stores/export-runtime-store.ts
+++ b/studio/frontend/src/features/export/stores/export-runtime-store.ts
@@ -118,6 +118,7 @@ export interface ExportRunSummary {
methodLabel: string;
method: ExportMethod;
quantLevels: string[];
+ ggufShardSize?: string | null;
/** Merged: the selected format values (for the summary "Formats" row and to reseed the picker). */
mergedFormats: string[];
destination: ExportDestination;
@@ -141,6 +142,8 @@ export interface RunExportParams {
quantLevels: string[];
/** GGUF: use an importance matrix (auto-download); required for the IQ quants. */
useImatrix?: boolean;
+ /** gguf: maximum size for full-precision shards, or "0" for one file. */
+ ggufShardSize?: string | null;
/** Merged: precision formats, each exported to its own sibling directory. Defaults to 16-bit.
* `label` is the display name for the success banner's per-format output line. */
mergedSelections?: {
@@ -496,6 +499,7 @@ export const useExportRuntimeStore = create<ExportRuntimeStore>()((set, get) =>
hf_token: params.token ?? params.loadToken ?? null,
imatrix: params.useImatrix,
private: params.privateRepo,
+ gguf_shard_size: params.ggufShardSize ?? null,
}),
);
if (outputPath) outputs.push({ label: "GGUF", path: outputPath });
diff --git a/studio/frontend/src/features/hub/inventory/api.ts b/studio/frontend/src/features/hub/inventory/api.ts
index b6cd241bae9..bd59503ac9d 100644
--- a/studio/frontend/src/features/hub/inventory/api.ts
+++ b/studio/frontend/src/features/hub/inventory/api.ts
@@ -181,6 +181,7 @@ export interface GgufVariantDetail {
display_label?: string | null;
size_bytes: number;
download_size_bytes?: number;
+ shard_count?: number;
/** Bytes a resume still has to fetch. Set only on a partial variant. */
download_remaining_bytes?: number | null;
downloaded?: boolean;
diff --git a/studio/frontend/src/features/model-picker/components/model-selector/pickers.tsx b/studio/frontend/src/features/model-picker/components/model-selector/pickers.tsx
index 008a4ca29c2..e04a6ef961e 100644
--- a/studio/frontend/src/features/model-picker/components/model-selector/pickers.tsx
+++ b/studio/frontend/src/features/model-picker/components/model-selector/pickers.tsx
@@ -1309,6 +1309,9 @@ function isValidGgufVariant(variant: unknown): variant is GgufVariantDetail {
typeof candidate.size_bytes === "number" &&
Number.isFinite(candidate.size_bytes) &&
candidate.size_bytes >= 0 &&
+ (candidate.shard_count === undefined ||
+ (Number.isSafeInteger(candidate.shard_count) &&
+ candidate.shard_count >= 0)) &&
(candidate.downloaded === undefined ||
typeof candidate.downloaded === "boolean") &&
// Carried through so each row can look up its own dependency group's
@@ -2065,6 +2068,11 @@ function GgufVariantExpander({
hideH3PrunedBuild,
})}
</span>
+ {(v.shard_count ?? 0) > 1 ? (
+ <span className="ml-1.5 text-ui-9 font-sans font-medium text-sky-700 dark:text-sky-300">
+ Sharded · {v.shard_count} parts
+ </span>
+ ) : null}
{unusableLocal ? (
<span className="ml-1.5 text-ui-9 font-sans font-medium text-amber-700 dark:text-amber-300">
incomplete
diff --git a/studio/frontend/tests/export-gguf-shard-size.test.ts b/studio/frontend/tests/export-gguf-shard-size.test.ts
new file mode 100644
index 00000000000..810fd183e22
--- /dev/null
+++ b/studio/frontend/tests/export-gguf-shard-size.test.ts
@@ -0,0 +1,111 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+import assert from "node:assert/strict";
+import { register } from "node:module";
+import test from "node:test";
+
+import {
+ ggufShardSaveDirectory,
+ isValidGgufShardSize,
+ normalizeGgufShardSize,
+} from "../src/features/export/lib/gguf-shard-size.ts";
+
+register("./helpers/export-store-resolver.mjs", import.meta.url);
+
+const stub = await import("./helpers/export-api-stub.mjs");
+const { useExportRuntimeStore } = await import(
+ "../src/features/export/stores/export-runtime-store.ts"
+);
+
+test("shard sizes match backend normalization", () => {
+ for (const [value, expected] of [
+ ["500m", "500MB"],
+ [" 4 GB ", "4GB"],
+ ["0002gb", "2GB"],
+ ["1MB", "1MB"],
+ ] as const) {
+ assert.equal(normalizeGgufShardSize(value), expected);
+ assert.equal(isValidGgufShardSize(value), true);
+ }
+});
+
+test("invalid and empty split sizes fail validation", () => {
+ for (const value of [
+ "",
+ "0",
+ "none",
+ "0MB",
+ "0GB",
+ "1.5GB",
+ "512",
+ "64KB",
+ "-2GB",
+ "4TB",
+ "4GBx",
+ ]) {
+ assert.equal(normalizeGgufShardSize(value), null, value);
+ assert.equal(isValidGgufShardSize(value), false, value);
+ }
+});
+
+test("split defaults use a separate save directory", () => {
+ assert.equal(
+ ggufShardSaveDirectory("Qwen-GGUF", "512MB"),
+ "Qwen-GGUF-split-512MB",
+ );
+ assert.equal(ggufShardSaveDirectory("Qwen-GGUF", "0"), "Qwen-GGUF");
+ assert.equal(ggufShardSaveDirectory("Qwen-GGUF", null), "Qwen-GGUF");
+});
+
+test("split defaults keep the final path component within 255 characters", () => {
+ for (const base of [
+ `models/${"m".repeat(250)}-GGUF`,
+ `C:\\models\\${"m".repeat(250)}-GGUF`,
+ ]) {
+ const directory = ggufShardSaveDirectory(base, "512MB");
+ const separator = Math.max(
+ directory.lastIndexOf("/"),
+ directory.lastIndexOf("\\"),
+ );
+ const component = directory.slice(separator + 1);
+ assert.equal(component.length, 255);
+ assert.equal(component.endsWith("-split-512MB"), true);
+ }
+});
+
+function params(ggufShardSize: string | null) {
+ return {
+ sourceMode: "model",
+ checkpointPath: null,
+ source: "unsloth/Qwen3-0.6B",
+ modelSource: "hf",
+ trustRemoteCode: false,
+ exportMethod: "gguf",
+ isAdapter: false,
+ quantLevels: ["f16"],
+ ggufShardSize,
+ saveDirectory: "out",
+ destination: "local",
+ privateRepo: false,
+ summary: {},
+ } as unknown as Parameters<
+ ReturnType<typeof useExportRuntimeStore.getState>["runExport"]
+ >[0];
+}
+
+async function request(ggufShardSize: string | null) {
+ stub.resetStub();
+ await useExportRuntimeStore.getState().runExport(params(ggufShardSize));
+ const call = stub.calls.find((entry) => entry.name === "exportGGUF");
+ if (!call) {
+ throw new Error("no gguf export request was made");
+ }
+ return call.args[0] as Record<string, unknown>;
+}
+
+test("the runtime preserves split, single-file, and legacy values", async () => {
+ assert.equal((await request("512MB")).gguf_shard_size, "512MB");
+ assert.equal((await request("0")).gguf_shard_size, "0");
+ assert.equal((await request(null)).gguf_shard_size, null);
+});
diff --git a/tests/saving/test_gguf_shard_size.py b/tests/saving/test_gguf_shard_size.py
new file mode 100644
index 00000000000..e4bdc5a6eb3
--- /dev/null
+++ b/tests/saving/test_gguf_shard_size.py
@@ -0,0 +1,221 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.
+
+from __future__ import annotations
+
+import subprocess
+from pathlib import Path
+
+import pytest
+
+import unsloth.save as save_mod
+
+
+ "value, expected",
+ [
+ (None, "50GB"),
+ ("", "0"),
+ (" ", "0"),
+ ("0", "0"),
+ ("none", "0"),
+ ("NONE", "0"),
+ ("1MB", "1MB"),
+ ("256m", "256MB"),
+ ("512 MB", "512MB"),
+ ("4G", "4GB"),
+ (" 8gb ", "8GB"),
+ ],
+)
+def test_resolve_gguf_shard_size(value, expected):
+ assert save_mod._resolve_gguf_shard_size(value) == expected
+
+
+ "value",
+ [
+ "0MB",
+ "0GB",
+ "-1GB",
+ "1.5GB",
+ "512",
+ "64KB",
+ "GB",
+ "2TB",
+ "2GBx",
+ ],
+)
+def test_invalid_gguf_shard_sizes_fail_before_conversion(value):
+ with pytest.raises(ValueError, match = "gguf_shard_size"):
+ save_mod._resolve_gguf_shard_size(value)
+
+
[email protected]("value", [0, False, 512, object()])
+def test_non_string_gguf_shard_sizes_are_rejected(value):
+ with pytest.raises(TypeError, match = "string or None"):
+ save_mod._resolve_gguf_shard_size(value)
+
+
+def test_oversized_gguf_shard_size_is_rejected(monkeypatch):
+ monkeypatch.setattr(save_mod.sys, "maxsize", 2_147_483_647)
+ with pytest.raises(ValueError, match = "too large"):
+ save_mod._resolve_gguf_shard_size("3GB")
+
+
+def test_public_save_rejects_invalid_size_before_model_work():
+ with pytest.raises(ValueError, match = "gguf_shard_size"):
+ save_mod.unsloth_save_pretrained_gguf(
+ object(),
+ "unused",
+ tokenizer = object(),
+ gguf_shard_size = "1.5GB",
+ )
+
+
+ "first_conversion, methods, is_vlm, expected",
+ [
+ ("f16", ["f16"], False, "256MB"),
+ ("bf16", ["bf16", "q4_k_m"], False, "256MB"),
+ ("f32", ["f32"], False, "256MB"),
+ ("q8_0", ["q8_0"], False, "0"),
+ ("bf16", ["q4_k_m"], False, "0"),
+ ("f16", ["f16"], True, "0"),
+ ],
+)
+def test_converter_only_shards_final_full_precision_outputs(
+ first_conversion, methods, is_vlm, expected
+):
+ assert (
+ save_mod._converter_gguf_shard_size(
+ "256MB",
+ first_conversion,
+ methods,
+ is_vlm,
+ )
+ == expected
+ )
+
+
+ "name, expected",
+ [
+ ("model.F16-mmproj.gguf", True),
+ ("mmproj-model.F16.gguf", True),
+ ("mtp-model.Q8_0.gguf", True),
+ ("model.Q8_0-MTP.gguf", True),
+ ("ordinary-mmprojector-model.gguf", False),
+ ("ordinary-mtp-model.gguf", False),
+ ("attempt.gguf", False),
+ ("model-00001-of-00002.gguf", False),
+ ],
+)
+def test_gguf_companion_detection_uses_exact_name_shapes(name, expected):
+ assert save_mod._is_gguf_companion(name) is expected
+
+
+def test_vlm_split_only_replaces_main_model(tmp_path, monkeypatch):
+ main = tmp_path / "vision model ü.F16.gguf"
+ mmproj = tmp_path / "vision model ü.BF16-mmproj.gguf"
+ mtp = tmp_path / "mtp-vision model ü.Q8_0.gguf"
+ main.write_bytes(b"m" * 2_000_001)
+ mmproj.write_bytes(b"projector")
+ mtp.write_bytes(b"drafter")
+
+ monkeypatch.setattr(save_mod, "_find_llama_gguf_split", lambda _: "splitter")
+
+ def fake_run(args, **kwargs):
+ assert args[2:4] == ["--split-max-size", "1M"]
+ prefix = Path(args[-1])
+ (prefix.parent / f"{prefix.name}-00001-of-00002.gguf").write_bytes(b"one")
+ (prefix.parent / f"{prefix.name}-00002-of-00002.gguf").write_bytes(b"two")
+ return subprocess.CompletedProcess(args, 0, stdout = "ok", stderr = "")
+
+ monkeypatch.setattr(save_mod.subprocess, "run", fake_run)
+ output = save_mod._split_main_gguf(
+ [str(main), str(mmproj), str(mtp)],
+ "1MB",
+ "quantizer",
+ )
+
+ assert [Path(path).name for path in output] == [
+ "vision model ü.F16-00001-of-00002.gguf",
+ "vision model ü.F16-00002-of-00002.gguf",
+ mmproj.name,
+ mtp.name,
+ ]
+ assert not main.exists()
+ assert mmproj.read_bytes() == b"projector"
+ assert mtp.read_bytes() == b"drafter"
+
+
+def test_vlm_main_below_boundary_stays_single_file(tmp_path, monkeypatch):
+ main = tmp_path / "model.F16.gguf"
+ mmproj = tmp_path / "model.BF16-mmproj.gguf"
+ main.write_bytes(b"main")
+ mmproj.write_bytes(b"projector")
+ monkeypatch.setattr(
+ save_mod,
+ "_find_llama_gguf_split",
+ lambda _: pytest.fail("splitter should not be required"),
+ )
+
+ output = save_mod._split_main_gguf([str(main), str(mmproj)], "1MB", "quantizer")
+
+ assert output == [str(main), str(mmproj)]
+
+
+def test_vlm_split_failure_keeps_single_file(tmp_path, monkeypatch):
+ main = tmp_path / "model.F16.gguf"
+ mmproj = tmp_path / "model.BF16-mmproj.gguf"
+ main.write_bytes(b"m" * 2_000_001)
+ mmproj.write_bytes(b"projector")
+ monkeypatch.setattr(save_mod, "_find_llama_gguf_split", lambda _: "splitter")
+
+ def fail_run(args, **kwargs):
+ raise subprocess.CalledProcessError(1, args, stderr = "invalid gguf")
+
+ monkeypatch.setattr(save_mod.subprocess, "run", fail_run)
+
+ with pytest.raises(RuntimeError, match = "invalid gguf"):
+ save_mod._split_main_gguf([str(main), str(mmproj)], "1MB", "quantizer")
+ assert main.exists()
+ assert mmproj.exists()
+
+
+def test_vlm_incomplete_split_keeps_single_file(tmp_path, monkeypatch):
+ main = tmp_path / "model.F16.gguf"
+ main.write_bytes(b"m" * 2_000_001)
+ monkeypatch.setattr(save_mod, "_find_llama_gguf_split", lambda _: "splitter")
+
+ def incomplete_run(args, **kwargs):
+ prefix = Path(args[-1])
+ (prefix.parent / f"{prefix.name}-00001-of-00002.gguf").write_bytes(b"one")
+ return subprocess.CompletedProcess(args, 0, stdout = "ok", stderr = "")
+
+ monkeypatch.setattr(save_mod.subprocess, "run", incomplete_run)
+
+ with pytest.raises(RuntimeError, match = "incomplete shard set"):
+ save_mod._split_main_gguf([str(main)], "1MB", "quantizer")
+ assert main.exists()
+
+
+def test_vlm_split_does_not_overwrite_existing_shards(tmp_path, monkeypatch):
+ main = tmp_path / "model.F16.gguf"
+ existing = tmp_path / "model.F16-00001-of-00002.gguf"
+ main.write_bytes(b"m" * 2_000_001)
+ existing.write_bytes(b"existing")
+ monkeypatch.setattr(save_mod, "_find_llama_gguf_split", lambda _: "splitter")
+
+ def fake_run(args, **kwargs):
+ prefix = Path(args[-1])
+ (prefix.parent / f"{prefix.name}-00001-of-00002.gguf").write_bytes(b"one")
+ (prefix.parent / f"{prefix.name}-00002-of-00002.gguf").write_bytes(b"two")
+ return subprocess.CompletedProcess(args, 0, stdout = "ok", stderr = "")
+
+ monkeypatch.setattr(save_mod.subprocess, "run", fake_run)
+
+ with pytest.raises(FileExistsError, match = "refusing to overwrite"):
+ save_mod._split_main_gguf([str(main)], "1MB", "quantizer")
+ assert main.exists()
+ assert existing.read_bytes() == b"existing"
diff --git a/tests/saving/test_gguf_single_pass_export.py b/tests/saving/test_gguf_single_pass_export.py
index 2ed131c814e..9d79d72a785 100644
--- a/tests/saving/test_gguf_single_pass_export.py
+++ b/tests/saving/test_gguf_single_pass_export.py
@@ -17,6 +17,7 @@ import contextlib
import os
import threading
import time
+from pathlib import Path
import pytest
@@ -116,10 +117,11 @@ class _Harness:
def _run(tmp_path, methods, **kwargs):
model_dir = tmp_path / "model_dir"
model_dir.mkdir(exist_ok = True)
+ model_dtype = kwargs.pop("model_dtype", "float16")
return save_mod.save_to_gguf(
model_name = "testmodel",
model_type = "llama",
- model_dtype = "float16",
+ model_dtype = model_dtype,
model_directory = str(model_dir),
quantization_method = methods,
**kwargs,
@@ -171,6 +173,39 @@ def test_k_quant_keeps_two_pass(monkeypatch, tmp_path):
assert len(locations) == 1 and locations[0].endswith("testmodel.Q4_K_M.gguf")
+def test_bf16_fallback_shards_the_final_output(monkeypatch, tmp_path):
+ h = _Harness(monkeypatch, tmp_path)
+ monkeypatch.setattr(save_mod.torch.cuda, "is_bf16_supported", lambda: False)
+ split_calls = []
+
+ def split(files, shard_size, quantizer_location):
+ split_calls.append((files, shard_size, quantizer_location))
+ return [files[0].replace(".gguf", f"-0000{i}-of-00002.gguf") for i in (1, 2)]
+
+ monkeypatch.setattr(save_mod, "_split_main_gguf", split)
+ locations, _, _ = _run(
+ tmp_path,
+ ["bf16"],
+ model_dtype = "bfloat16",
+ gguf_shard_size = "256MB",
+ )
+
+ assert h.convert_calls[0]["quantization_type"] == "f16"
+ assert h.convert_calls[0]["max_shard_size"] == "0"
+ assert [call["quant_type"] for call in h.quantize_calls] == ["bf16"]
+ assert split_calls == [
+ (
+ [str(tmp_path / "model_dir_gguf" / "testmodel.BF16.gguf")],
+ "256MB",
+ "llama-quantize",
+ )
+ ]
+ assert [Path(path).name for path in locations] == [
+ "testmodel.BF16-00001-of-00002.gguf",
+ "testmodel.BF16-00002-of-00002.gguf",
+ ]
+
+
def test_mixed_methods_share_16bit_base(monkeypatch, tmp_path):
h = _Harness(monkeypatch, tmp_path)
_run(tmp_path, ["q4_k_m", "q8_0"])
diff --git a/tests/saving/test_sentence_transformer_gguf_shards.py b/tests/saving/test_sentence_transformer_gguf_shards.py
new file mode 100644
index 00000000000..eed2cb9a6b9
--- /dev/null
+++ b/tests/saving/test_sentence_transformer_gguf_shards.py
@@ -0,0 +1,89 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.
+
+from __future__ import annotations
+
+import os
+import types
+
+import pytest
+
+
+def sentence_transformer_module():
+ return pytest.importorskip("unsloth.models.sentence_transformer")
+
+
+class _FakeSentenceTransformer:
+ tokenizer = object()
+
+ def save_pretrained(self, path):
+ os.makedirs(os.path.join(path, "0_Transformer"), exist_ok = True)
+
+ def __getitem__(self, index):
+ assert index == 0
+ return types.SimpleNamespace(auto_model = object())
+
+
+def test_sentence_transformer_save_forwards_gguf_shard_size(
+ sentence_transformer_module, tmp_path, monkeypatch
+):
+ seen = {}
+ monkeypatch.setattr(
+ sentence_transformer_module,
+ "unsloth_save_pretrained_gguf",
+ lambda *args, **kwargs: (seen.update(kwargs), {"gguf_files": []})[1],
+ )
+
+ sentence_transformer_module._save_pretrained_gguf(
+ _FakeSentenceTransformer(),
+ str(tmp_path / "sentence model ü"),
+ gguf_shard_size = "512MB",
+ )
+
+ assert seen["gguf_shard_size"] == "512MB"
+ assert seen["merge_is_disposable"] is False
+
+
+def test_sentence_transformer_push_forwards_gguf_shard_size(
+ sentence_transformer_module, monkeypatch
+):
+ seen = {}
+
+ class FakeApi:
+ def __init__(self, token):
+ assert token == "token"
+
+ def create_repo(self, **kwargs):
+ return None
+
+ def upload_file(self, **kwargs):
+ return None
+
+ def add_tags(self, **kwargs):
+ return None
+
+ monkeypatch.setattr(sentence_transformer_module, "HfApi", FakeApi)
+ monkeypatch.setattr(
+ sentence_transformer_module,
+ "_save_pretrained_gguf",
+ lambda *args, **kwargs: (
+ seen.update(kwargs),
+ {
+ "gguf_files": [],
+ "modelfile_location": None,
+ "is_vlm": False,
+ "fix_bos_token": False,
+ },
+ )[1],
+ )
+
+ result = sentence_transformer_module._push_to_hub_gguf(
+ object(),
+ "owner/model",
+ token = "token",
+ gguf_shard_size = "2GB",
+ )
+
+ assert result == "owner/model"
+ assert seen["gguf_shard_size"] == "2GB"
diff --git a/unsloth/models/sentence_transformer.py b/unsloth/models/sentence_transformer.py
index 1e37e1f6245..7707b30adf5 100644
--- a/unsloth/models/sentence_transformer.py
+++ b/unsloth/models/sentence_transformer.py
@@ -170,6 +170,7 @@ def _save_pretrained_gguf(
temporary_location = "_unsloth_temporary_saved_buffers",
maximum_memory_usage = 0.85,
imatrix_file = None,
+ gguf_shard_size = None,
**kwargs,
):
"""
@@ -224,6 +225,7 @@ def _save_pretrained_gguf(
# loads as a SentenceTransformer, so a short disk fails loudly instead.
merge_is_disposable = False,
imatrix_file = imatrix_file,
+ gguf_shard_size = gguf_shard_size,
)
# 5. Move GGUF files from the subdirectory (0_Transformer) to the root save_directory
@@ -311,6 +313,7 @@ def _push_to_hub_gguf(
revision = None,
tags = None,
imatrix_file = None,
+ gguf_shard_size = None,
**kwargs,
):
"""
@@ -356,6 +359,7 @@ def _push_to_hub_gguf(
create_pr (bool): Whether to create a pull request instead of pushing directly.
revision (str, optional): Branch/revision to push to.
tags (list, optional): Additional tags for the repo.
+ gguf_shard_size (str, optional): Maximum final f32, f16 or bf16 GGUF shard size.
Returns:
str: The full repo ID on Hugging Face Hub.
@@ -403,6 +407,7 @@ def _push_to_hub_gguf(
temporary_location = temporary_location,
maximum_memory_usage = maximum_memory_usage,
imatrix_file = imatrix_file,
+ gguf_shard_size = gguf_shard_size,
)
gguf_files = result.get("gguf_files", [])
diff --git a/unsloth/save.py b/unsloth/save.py
index c86478a0e61..2c401d6b0ef 100644
--- a/unsloth/save.py
+++ b/unsloth/save.py
@@ -1857,6 +1857,198 @@ def get_executable(executables):
# Output types convert_hf_to_gguf.py can emit directly via --outtype.
_DIRECT_CONVERT_OUTTYPES = ("f32", "f16", "bf16", "q8_0")
+_FULL_PRECISION_GGUF_TYPES = ("f32", "f16", "bf16")
+_GGUF_DEFAULT_SHARD_SIZE = "50GB"
+_GGUF_NO_SHARDING = "0"
+_GGUF_SHARD_SIZE_RE = re.compile(r"^(\d+)\s*([MG])B?$", re.IGNORECASE)
+
+
+def _resolve_gguf_shard_size(gguf_shard_size: Optional[str]) -> str:
+ """Validate and normalize the final GGUF shard size."""
+ if gguf_shard_size is None:
+ return _GGUF_DEFAULT_SHARD_SIZE
+ if not isinstance(gguf_shard_size, str):
+ raise TypeError("Unsloth: gguf_shard_size must be a string or None.")
+
+ value = gguf_shard_size.strip()
+ if value.casefold() in ("", "0", "none"):
+ return _GGUF_NO_SHARDING
+
+ match = _GGUF_SHARD_SIZE_RE.fullmatch(value)
+ if match is None:
+ raise ValueError(
+ f"Unsloth: gguf_shard_size={gguf_shard_size!r} is invalid. "
+ "Use a positive whole number in MB or GB, such as '500MB' or '4GB', "
+ "or pass '0' for one file."
+ )
+
+ magnitude = int(match.group(1))
+ unit = match.group(2).upper()
+ if magnitude == 0:
+ raise ValueError(
+ "Unsloth: gguf_shard_size must be positive. Pass '0' without a unit "
+ "to request one file."
+ )
+ multiplier = 1_000_000 if unit == "M" else 1_000_000_000
+ if magnitude > sys.maxsize // multiplier:
+ raise ValueError("Unsloth: gguf_shard_size is too large for this platform.")
+ return f"{magnitude}{unit}B"
+
+
+def _converter_gguf_shard_size(
+ gguf_shard_size: str, first_conversion: str, quantization_methods, is_vlm: bool
+) -> str:
+ """Choose the converter limit without splitting final quantized files or VLM companions."""
+ keeps_converter_output = first_conversion in quantization_methods
+ if not is_vlm and keeps_converter_output and first_conversion in _FULL_PRECISION_GGUF_TYPES:
+ return gguf_shard_size
+ return _GGUF_NO_SHARDING
+
+
+def _gguf_shard_size_bytes(gguf_shard_size: str) -> int:
+ """Convert a normalized GGUF shard size to decimal bytes."""
+ if gguf_shard_size == _GGUF_NO_SHARDING:
+ return 0
+ match = _GGUF_SHARD_SIZE_RE.fullmatch(gguf_shard_size)
+ if match is None:
+ raise ValueError(f"Unsloth: invalid normalized GGUF shard size {gguf_shard_size!r}.")
+ multiplier = 1_000_000 if match.group(2).upper() == "M" else 1_000_000_000
+ return int(match.group(1)) * multiplier
+
+
+def _is_gguf_companion(path: Union[str, os.PathLike]) -> bool:
+ """Return whether a GGUF is a vision or speculative-decoding companion."""
+ file_path = Path(path)
+ name = file_path.name.casefold()
+ stem = name[:-5] if name.endswith(".gguf") else name
+ return (
+ stem.endswith("-mmproj")
+ or stem.startswith("mmproj-")
+ or stem.startswith("mtp-")
+ or stem.endswith("-mtp")
+ or file_path.parent.name.casefold() == "mtp"
+ )
+
+
+def _find_llama_gguf_split(quantizer_location: str) -> str:
+ """Find the llama.cpp split utility beside supported install layouts."""
+ executable = "llama-gguf-split.exe" if IS_WINDOWS else "llama-gguf-split"
+ candidates = [shutil.which(executable)]
+ quantizer_dir = os.path.dirname(os.path.abspath(quantizer_location))
+ candidates.extend(
+ [
+ os.path.join(quantizer_dir, executable),
+ os.path.join(LLAMA_CPP_DEFAULT_DIR, executable),
+ os.path.join(LLAMA_CPP_DEFAULT_DIR, "build", "bin", executable),
+ os.path.join(
+ LLAMA_CPP_DEFAULT_DIR,
+ "build",
+ "bin",
+ "Release",
+ executable,
+ ),
+ ]
+ )
+ for candidate in dict.fromkeys(path for path in candidates if path):
+ if os.path.isfile(candidate) and (IS_WINDOWS or os.access(candidate, os.X_OK)):
+ return candidate
+ raise RuntimeError(
+ "Unsloth: post-conversion GGUF sharding requires llama-gguf-split. "
+ "Upgrade unsloth_zoo and reinstall llama.cpp, then retry."
+ )
+
+
+def _split_main_gguf(initial_files, gguf_shard_size: str, quantizer_location: str):
+ """Split one main GGUF while leaving mmproj and MTP companions untouched."""
+ max_bytes = _gguf_shard_size_bytes(gguf_shard_size)
+ if max_bytes == 0:
+ return initial_files
+
+ main_files = [os.fspath(path) for path in initial_files if not _is_gguf_companion(path)]
+ if len(main_files) != 1:
+ raise RuntimeError(
+ "Unsloth: expected one unsharded main GGUF before companion-safe "
+ f"splitting, found {len(main_files)}."
+ )
+ main_file = main_files[0]
+ if os.path.getsize(main_file) <= max_bytes:
+ return initial_files
+
+ splitter = _find_llama_gguf_split(quantizer_location)
+ parent = os.path.dirname(os.path.abspath(main_file))
+ import tempfile
+
+ with tempfile.TemporaryDirectory(prefix = ".unsloth_gguf_split_", dir = parent) as temp_dir:
+ output_prefix = os.path.join(temp_dir, Path(main_file).stem)
+ split_size = gguf_shard_size[:-1]
+ try:
+ result = subprocess.run(
+ [
+ splitter,
+ "--split",
+ "--split-max-size",
+ split_size,
+ main_file,
+ output_prefix,
+ ],
+ check = True,
+ capture_output = True,
+ text = True,
+ )
+ except subprocess.CalledProcessError as exception:
+ details = (exception.stderr or exception.stdout or "").strip()
+ suffix = f"\n{details}" if details else ""
+ raise RuntimeError(f"Unsloth: llama-gguf-split failed.{suffix}") from exception
+
+ pattern = re.compile(
+ rf"^{re.escape(Path(output_prefix).name)}-(\d{{5}})-of-(\d{{5}})\.gguf$"
+ )
+ shards = []
+ for candidate in Path(temp_dir).iterdir():
+ match = pattern.fullmatch(candidate.name)
+ if match is not None:
+ shards.append((int(match.group(1)), int(match.group(2)), candidate))
+ shards.sort(key = lambda item: item[0])
+ if not shards:
+ details = (result.stderr or result.stdout or "").strip()
+ suffix = f"\n{details}" if details else ""
+ raise RuntimeError(f"Unsloth: llama-gguf-split produced no shards.{suffix}")
+
+ total = shards[0][1]
+ indices = [index for index, declared_total, _ in shards if declared_total == total]
+ if len(indices) != len(shards) or indices != list(range(1, total + 1)):
+ raise RuntimeError("Unsloth: llama-gguf-split produced an incomplete shard set.")
+ if total == 1:
+ return initial_files
+
+ destinations = [os.path.join(parent, shard.name) for _, _, shard in shards]
+ existing = [path for path in destinations if os.path.exists(path)]
+ if existing:
+ raise FileExistsError(
+ "Unsloth: refusing to overwrite an existing GGUF shard set: " + ", ".join(existing)
+ )
+
+ moved = []
+ try:
+ for (_, _, source), destination in zip(shards, destinations):
+ os.replace(source, destination)
+ moved.append(destination)
+ os.unlink(main_file)
+ except Exception:
+ for destination in moved:
+ try:
+ os.unlink(destination)
+ except OSError:
+ pass
+ raise
+
+ output_files = []
+ for path in initial_files:
+ if os.path.abspath(os.fspath(path)) == os.path.abspath(main_file):
+ output_files.extend(destinations)
+ else:
+ output_files.append(path)
+ return output_files
def _choose_first_conversion(
@@ -1896,6 +2088,7 @@ def save_to_gguf(
gguf_directory: Optional[Union[str, os.PathLike]] = None,
merge_is_disposable: bool = False,
preexisting_weights = None,
+ gguf_shard_size: Optional[str] = None,
):
"""
Orchestrates the complete GGUF conversion process.
@@ -1909,6 +2102,8 @@ def save_to_gguf(
`preexisting_weights` is what `model_directory` held before the merge wrote it,
so reclamation can take only what this export produced. `None` means the caller
cannot say, and nothing is reclaimed.
+ `gguf_shard_size` applies to final f32, f16 and bf16 outputs. Quantized outputs
+ remain single-file. None preserves the historical 50GB converter limit.
"""
# print_output True only if UNSLOTH_ENABLE_LOGGING=1
if os.environ.get("UNSLOTH_ENABLE_LOGGING", "0") == "1":
@@ -2001,6 +2196,13 @@ def save_to_gguf(
first_conversion = "f16"
first_conversion_dtype = "" if first_conversion == "None" else first_conversion
+ gguf_shard_size = _resolve_gguf_shard_size(gguf_shard_size)
+ converter_shard_size = _converter_gguf_shard_size(
+ gguf_shard_size,
+ first_conversion,
+ quantization_method,
+ is_vlm,
+ )
# Print conversion info
needs_quantize_pass = any(m != first_conversion for m in quantization_method)
if needs_quantize_pass:
@@ -2056,7 +2258,7 @@ def save_to_gguf(
supported_vision_archs = supported_vision_archs,
is_vlm = is_vlm,
is_gpt_oss = is_gpt_oss,
- max_shard_size = "50GB",
+ max_shard_size = converter_shard_size,
print_output = print_output,
)
# update is_vlm switch
@@ -2095,6 +2297,17 @@ def save_to_gguf(
moved_files.append(dst)
initial_files = moved_files
+ if (
+ is_vlm
+ and first_conversion in _FULL_PRECISION_GGUF_TYPES
+ and first_conversion in quantization_method
+ ):
+ initial_files = _split_main_gguf(
+ initial_files,
+ gguf_shard_size,
+ quantizer_location,
+ )
+
print(f"Unsloth: Initial conversion completed! Files: {initial_files}")
# Step 4: Additional quantizations using llama-quantize
@@ -2184,8 +2397,7 @@ def save_to_gguf(
sum(
os.path.getsize(f)
for f in initial_files
- if os.path.isfile(f)
- and "-mmproj" not in os.path.basename(f).lower()
+ if os.path.isfile(f) and not _is_gguf_companion(f)
)
* _ratio
)
@@ -2269,11 +2481,7 @@ def save_to_gguf(
# once when the host has headroom for two copies, else a multi-quant export that
# fit sequentially could OOM.
try:
- base_bytes = sum(
- os.path.getsize(f)
- for f in initial_files
- if "-mmproj" not in os.path.basename(f).lower()
- )
+ base_bytes = sum(os.path.getsize(f) for f in initial_files if not _is_gguf_companion(f))
mem_ok = psutil.virtual_memory().available >= int(2.5 * base_bytes)
except Exception:
mem_ok = False
@@ -2338,9 +2546,8 @@ def save_to_gguf(
print("Unsloth: Model files cleanup...")
want_full_precision = first_conversion in quantization_method
if quants_created:
- # convert_to_gguf may return multiple base shards plus an mmproj entry,
- # so treat every initial file that is not an mmproj as part of the base set.
- base_files = [f for f in initial_files if "-mmproj" not in os.path.basename(f).lower()]
+ # convert_to_gguf can return main shards plus companion files.
+ base_files = [f for f in initial_files if not _is_gguf_companion(f)]
if not want_full_precision:
for f in base_files:
if f in all_saved_locations:
@@ -2358,6 +2565,17 @@ def save_to_gguf(
all_saved_locations.remove(f)
for i, f in enumerate(base_files):
all_saved_locations.insert(1 + i, f)
+
+ for quant_method, quantized_file in zip(methods_to_quantize, quantized_files):
+ if quant_method not in _FULL_PRECISION_GGUF_TYPES:
+ continue
+ split_files = _split_main_gguf(
+ [quantized_file],
+ gguf_shard_size,
+ quantizer_location,
+ )
+ index = all_saved_locations.index(quantized_file)
+ all_saved_locations[index : index + 1] = split_files
else:
print("Unsloth: GPT-OSS model - skipping additional quantizations")
want_full_precision = True
@@ -4562,6 +4780,7 @@ def unsloth_save_pretrained_gguf(
save_method: str = None,
imatrix_file = None,
merge_is_disposable: bool = True,
+ gguf_shard_size: Optional[str] = None,
):
"""
Same as .save_pretrained(...) except 4bit weights are auto
@@ -4576,6 +4795,9 @@ def unsloth_save_pretrained_gguf(
to keep the weights when `save_directory` is part of the caller's own deliverable (the
SentenceTransformer export writes its module directory there).
+ gguf_shard_size: maximum final f32, f16 or bf16 GGUF shard size in MB or GB. Pass
+ "0" for one file. None preserves the historical 50GB converter limit.
+
Choose for `quantization_method` to be:
"not_quantized" : "Recommended. Fast conversion. Slow inference, big files.",
"fast_quantized" : "Recommended. Fast conversion. OK inference, OK file size.",
@@ -4609,6 +4831,7 @@ def unsloth_save_pretrained_gguf(
if isinstance(tokenizer, (PreTrainedTokenizerBase, ProcessorMixin)):
tokenizer = patch_saving_functions(tokenizer)
save_directory = os.path.normpath(os.fspath(save_directory))
+ gguf_shard_size = _resolve_gguf_shard_size(gguf_shard_size)
# save_method="lora" exports the adapter itself as a GGUF LoRA (not a merged model).
if save_method is not None and str(save_method).lower() == "lora":
@@ -4714,6 +4937,7 @@ def unsloth_save_pretrained_gguf(
del arguments["imatrix_file"] # only used by the gguf quantize step, not the 16bit merge
del arguments["_gguf_prewarm_ok"] # a local decision, not a save_pretrained kwarg
del arguments["merge_is_disposable"] # decides reclamation, not how the merge is written
+ del arguments["gguf_shard_size"] # only used by the gguf converter
# Preserve the requested output before reusing a non-PEFT checkpoint as input.
# Same definition the preflight sized, so the disk it measured is the one
@@ -4882,6 +5106,7 @@ def unsloth_save_pretrained_gguf(
gguf_directory = gguf_directory,
merge_is_disposable = merge_is_disposable,
preexisting_weights = preexisting_weights,
+ gguf_shard_size = gguf_shard_size,
)
except Exception as e:
if _gguf_child_was_oom_killed(e):
@@ -5306,14 +5531,12 @@ def _free_merge_if_disk_is_tight(
if not quant_methods:
return 0
try:
- # llama-quantize copies a VLM's `-mmproj` projector rather than quantizing
- # it, so it is not part of what a pass writes and charging it once per
- # quant would call a disk tight that has the room. Filtered the same way
- # the RAM budget above filters it.
+ # llama-quantize copies companions rather than quantizing them, so they
+ # are excluded from the output and memory estimates.
base_bytes = sum(
os.path.getsize(f)
for f in initial_files
- if os.path.isfile(f) and "-mmproj" not in os.path.basename(f).lower()
+ if os.path.isfile(f) and not _is_gguf_companion(f)
)
except OSError:
return 0
@@ -5402,6 +5625,7 @@ def unsloth_push_to_hub_gguf(
save_method: str = None,
imatrix_file = None,
is_main_process: bool = True,
+ gguf_shard_size: Optional[str] = None,
):
"""
Same as .push_to_hub(...) except 4bit weights are auto
@@ -5498,6 +5722,7 @@ def unsloth_push_to_hub_gguf(
temporary_location = temporary_location,
maximum_memory_usage = maximum_memory_usage,
imatrix_file = imatrix_file,
+ gguf_shard_size = gguf_shard_size,
)
# Extract results