Skip to content

Run frontier MoE models on hardware you already own — pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦

C

fix(download): keep FP8 metadata and fallback on pinned revisions

Rudy Celekli06b266b7365c
diff --git a/c/download_fp8.py b/c/download_fp8.py
index fd2707ef..0e9bc73e 100644
--- a/c/download_fp8.py
+++ b/c/download_fp8.py
@@ -6,6 +6,7 @@ Usage: python download_fp8.py
python download_fp8.py --dest /data/glm52_fp8 (or set $GLM_DEST)
"""
import os, time, threading, argparse, subprocess
+from urllib.parse import quote
REPO_MS = "ZhipuAI/GLM-5.2-FP8" # ModelScope
REPO_HF = "zai-org/GLM-5.2-FP8" # HuggingFace
@@ -36,7 +37,8 @@ def bar(cur, total, width=24):
def get_shard_list_hf():
from huggingface_hub import HfApi
- info=HfApi().repo_info(REPO_HF, files_metadata=True)
+ info=HfApi().repo_info(REPO_HF, revision=os.environ.get("GLM_HF_REVISION", "main"),
+ files_metadata=True)
shards=sorted(s.rfilename for s in info.siblings if s.rfilename.endswith(".safetensors"))
sizes={s.rfilename:s.size for s in info.siblings if s.rfilename.endswith(".safetensors")}
return shards, sizes
@@ -45,7 +47,9 @@ def get_shard_list_ms():
"""Get shard list from ModelScope API."""
import requests
# ModelScope API: list files
- r = requests.get(f"https://modelscope.cn/api/v1/models/{REPO_MS}/repo/files?Revision=master&Root=", timeout=30)
+ r = requests.get(f"https://modelscope.cn/api/v1/models/{REPO_MS}/repo/files",
+ params={"Revision": os.environ.get("GLM_MS_REVISION", "master"), "Root": ""},
+ timeout=30)
data = r.json()["Data"]["Files"]
shards = sorted(f["Path"] for f in data if f["Path"].endswith(".safetensors"))
sizes = {f["Path"]: f.get("Size", 0) for f in data if f["Path"].endswith(".safetensors")}
@@ -212,9 +216,11 @@ def main():
success = True; break
# Retry with curl fallback
if use_ms:
- base = f"https://modelscope.cn/api/v1/models/{REPO_MS}/repo?Revision=master&FilePath="
+ revision = quote(os.environ.get("GLM_MS_REVISION", "master"), safe="")
+ base = f"https://modelscope.cn/api/v1/models/{REPO_MS}/repo?Revision={revision}&FilePath="
else:
- base = f"https://huggingface.co/{REPO_HF}/resolve/main"
+ revision = quote(os.environ.get("GLM_HF_REVISION", "main"), safe="")
+ base = f"https://huggingface.co/{REPO_HF}/resolve/{revision}"
if download_file_curl(fn, base, expected):
success = True; break
except Exception as e:
diff --git a/c/tests/test_download_fp8.py b/c/tests/test_download_fp8.py
index 7c342c3f..926c5c0e 100644
--- a/c/tests/test_download_fp8.py
+++ b/c/tests/test_download_fp8.py
@@ -1,6 +1,8 @@
import os
import sys
import tempfile
+import types
+from urllib.parse import quote, parse_qs, urlsplit
import unittest
from unittest import mock
@@ -16,6 +18,50 @@ class DownloadExitStatusTests(unittest.TestCase):
mock.patch.object(sys, "argv", ["download_fp8.py", *args]):
return download_fp8.main()
+ def test_huggingface_listing_uses_download_revision(self):
+ for revision in (None, "refs/pr/12"):
+ api = mock.Mock()
+ api.repo_info.return_value = types.SimpleNamespace(siblings=[])
+ hub = types.SimpleNamespace(HfApi=mock.Mock(return_value=api))
+ with self.subTest(revision=revision), mock.patch.dict(sys.modules, {"huggingface_hub": hub}), \
+ mock.patch.dict(os.environ, {}, clear=True):
+ if revision:
+ os.environ["GLM_HF_REVISION"] = revision
+ download_fp8.get_shard_list_hf()
+ self.assertEqual(api.repo_info.call_args.kwargs.get("revision", "main"), revision or "main")
+
+ def test_modelscope_listing_uses_download_revision(self):
+ for revision in (None, "release/tag"):
+ requests = types.SimpleNamespace(get=mock.Mock())
+ requests.get.return_value.json.return_value = {"Data": {"Files": []}}
+ with self.subTest(revision=revision), mock.patch.dict(sys.modules, {"requests": requests}), \
+ mock.patch.dict(os.environ, {}, clear=True):
+ if revision:
+ os.environ["GLM_MS_REVISION"] = revision
+ download_fp8.get_shard_list_ms()
+ call = requests.get.call_args
+ query = parse_qs(urlsplit(call.args[0]).query, keep_blank_values=True)
+ query.update({key: [value] for key, value in call.kwargs.get("params", {}).items()})
+ self.assertEqual(query["Revision"], [revision or "master"])
+
+ def test_curl_fallback_uses_the_download_revision(self):
+ name = "model-00001.safetensors"
+ for source, variable in (("hf", "GLM_HF_REVISION"), ("ms", "GLM_MS_REVISION")):
+ with self.subTest(source=source), tempfile.TemporaryDirectory() as dest, \
+ mock.patch.dict(os.environ, {variable: "release/tag"}), \
+ mock.patch.object(download_fp8, f"get_shard_list_{source}", return_value=([name], {name: 4})), \
+ mock.patch.object(download_fp8, f"download_file_{source}"), \
+ mock.patch.object(download_fp8.time, "sleep"):
+ for metadata in self.META_FILES:
+ open(os.path.join(dest, metadata), "wb").close()
+ def fallback(fn, base, expected):
+ self.assertIn(quote("release/tag", safe=""), base)
+ with open(os.path.join(dest, fn), "wb") as out:
+ out.write(b"data")
+ return True
+ with mock.patch.object(download_fp8, "download_file_curl", side_effect=fallback):
+ self.assertEqual(self.run_main(dest, "--source", source), 0)
+
def test_failed_shard_returns_nonzero(self):
manifest = (["model-00001.safetensors"],
{"model-00001.safetensors": 4})