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.

Studio: accept zero-based shard numbering in cached model weights (#10857)

Johannesb36d75392c09
diff --git a/studio/backend/routes/training.py b/studio/backend/routes/training.py
index f8a71b3c5f5..a2dfac1d642 100644
--- a/studio/backend/routes/training.py
+++ b/studio/backend/routes/training.py
@@ -398,7 +398,7 @@ def _has_complete_indexed_weights(path: Path, index_name: str, expected_suffix:
if match is not None:
part = int(match.group("part"))
total = int(match.group("total"))
- if total < 1 or part < 1 or part > total:
+ if total < 1 or part < 0 or part > total:
return False
family = (
os.path.normcase(str(shard_path.parent)),
@@ -407,7 +407,12 @@ def _has_complete_indexed_weights(path: Path, index_name: str, expected_suffix:
total,
)
families.setdefault(family, set()).add(part)
- return all(len(parts) == family[3] for family, parts in families.items())
+ # Some exporters number from zero (model-00000-of-00001.safetensors), so accept either
+ # complete run and never a mix; count/min/max avoids a range built from an untrusted total.
+ return all(
+ len(parts) == total and (min(parts), max(parts)) in ((1, total), (0, total - 1))
+ for (_, _, _, total), parts in families.items()
+ )
def _trainable_local_roots(path: Path, model_name: Optional[str] = None) -> list[Path]:
diff --git a/studio/backend/tests/test_training_cached_start.py b/studio/backend/tests/test_training_cached_start.py
index 48c915ba3a2..86427b81a76 100644
--- a/studio/backend/tests/test_training_cached_start.py
+++ b/studio/backend/tests/test_training_cached_start.py
@@ -827,6 +827,82 @@ def test_client_error_probe_uses_complete_sharded_cache(tmp_path):
assert result.cached_model_pin == ("unsloth/test", str(snapshot.resolve()))
+ ("shards", "complete"),
+ [
+ (["model-00000-of-00001.safetensors"], True),
+ (["model-00000-of-00002.safetensors", "model-00001-of-00002.safetensors"], True),
+ (["model-00000-of-00002.safetensors"], False),
+ (["model-00000-of-00002.safetensors", "model-00002-of-00002.safetensors"], False),
+ (["model-00000-of-1000000000.safetensors"], False),
+ ],
+)
+def test_zero_based_shard_numbering_uses_complete_cache(tmp_path, shards, complete):
+ total = shards[0].rsplit("-", 1)[-1].split(".")[0]
+ route = _load_route_module(f"training_route_zero_based_shards_{len(shards)}_{total}_{complete}")
+ snapshot = tmp_path / "models--unsloth--test" / "snapshots" / "rev"
+ snapshot.mkdir(parents = True)
+ (snapshot / "config.json").write_text("{}")
+ for shard in shards:
+ (snapshot / shard).write_bytes(b"x")
+ (snapshot / "model.safetensors.index.json").write_text(
+ json.dumps(
+ {"weight_map": {f"layer.{index}": shard for index, shard in enumerate(shards)}},
+ )
+ )
+
+ if not complete:
+ _shared_setup_1(route)
+ return
+
+ with patch.object(
+ route,
+ "_remote_untrainable_model_format",
+ side_effect = HTTPException(status_code = 403, detail = "unavailable"),
+ ):
+ result = route._reject_untrainable_model_request(_request())
+
+ assert result.cached_model_pin == ("unsloth/test", str(snapshot.resolve()))
+
+
[email protected]("persisted", ["repo", "snapshot"])
+ "shards",
+ [
+ ["model-00000-of-00001.safetensors"],
+ ["model-00000-of-00002.safetensors", "model-00001-of-00002.safetensors"],
+ ["model-00001-of-00001.safetensors"],
+ ["model-00001-of-00002.safetensors", "model-00002-of-00002.safetensors"],
+ ],
+)
+def test_persisted_cache_path_starts_again_with_zero_based_shards(tmp_path, shards, persisted):
+ """From run two on Studio sends a modelLocalPath, so the preflight takes the local branch:
+ the persisted cache repo dir and a resolved snapshot must agree, and 1-based names must not move."""
+ route = _load_route_module(f"training_route_persisted_{len(shards)}_{shards[0]}_{persisted}")
+ snapshot = _model_repo_with_ref(tmp_path, "unsloth/test")
+ for shard in shards:
+ (snapshot / shard).write_bytes(b"x")
+ (snapshot / "model.safetensors.index.json").write_text(
+ json.dumps(
+ {"weight_map": {f"layer.{index}": shard for index, shard in enumerate(shards)}},
+ )
+ )
+ target = snapshot if persisted == "snapshot" else snapshot.parent.parent
+
+ with patch.object(
+ route,
+ "_remote_untrainable_model_format",
+ side_effect = AssertionError("a cached model must not need the hub"),
+ ):
+ result = route._reject_untrainable_model_request(
+ _request(model_known_cached = True, model_local_path = str(target)),
+ )
+
+ assert result.model_name == "unsloth/test"
+ # The route posix-normalizes this field, so compare against the same shape.
+ assert result.model_local_path == target.as_posix()
+
+
def test_incomplete_safetensors_index_is_not_masked_by_pytorch_weights(tmp_path):
route = _load_route_module("training_route_incomplete_safe_index_with_pytorch")
first_shard, second_shard, snapshot = _shared_setup_7(tmp_path)