Upload export_apochat_litert.py with huggingface_hub
Browse files- export_apochat_litert.py +29 -4
export_apochat_litert.py
CHANGED
|
@@ -89,12 +89,28 @@ def parse_args() -> argparse.Namespace:
|
|
| 89 |
parser.add_argument(
|
| 90 |
"--shard-size",
|
| 91 |
type=int,
|
| 92 |
-
default=
|
| 93 |
help="Target size in bytes per PyTorch safetensors shard",
|
| 94 |
)
|
| 95 |
return parser.parse_args()
|
| 96 |
|
| 97 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
def download_repo_files(repo_id: str, revision: str | None, local_dir: Path) -> None:
|
| 99 |
"""Download all non-weight files from the MLX repo into local_dir."""
|
| 100 |
print(f"Downloading aux files from {repo_id} ...")
|
|
@@ -104,6 +120,9 @@ def download_repo_files(repo_id: str, revision: str | None, local_dir: Path) ->
|
|
| 104 |
for fname in files:
|
| 105 |
if fname.endswith(".safetensors"):
|
| 106 |
continue
|
|
|
|
|
|
|
|
|
|
| 107 |
print(f" {fname}")
|
| 108 |
hf_hub_download(
|
| 109 |
repo_id=repo_id,
|
|
@@ -210,9 +229,15 @@ def dequantize_mlx_to_pytorch(
|
|
| 210 |
# Rewrite the final shard names with the actual count.
|
| 211 |
shards = sorted(output_dir.glob("model-?????-of-?????.safetensors"))
|
| 212 |
total = len(shards)
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 216 |
|
| 217 |
print(f"Wrote {total} safetensors shard(s) to {output_dir}")
|
| 218 |
|
|
|
|
| 89 |
parser.add_argument(
|
| 90 |
"--shard-size",
|
| 91 |
type=int,
|
| 92 |
+
default=10_000_000_000,
|
| 93 |
help="Target size in bytes per PyTorch safetensors shard",
|
| 94 |
)
|
| 95 |
return parser.parse_args()
|
| 96 |
|
| 97 |
|
| 98 |
+
def _write_sharded_index(checkpoint_dir: Path) -> None:
|
| 99 |
+
"""Write model.safetensors.index.json from shards in a directory."""
|
| 100 |
+
from safetensors import safe_open
|
| 101 |
+
|
| 102 |
+
weight_map: dict[str, str] = {}
|
| 103 |
+
shards = sorted(checkpoint_dir.glob("model-?????-of-?????.safetensors"))
|
| 104 |
+
for shard in shards:
|
| 105 |
+
with safe_open(str(shard), framework="np") as f:
|
| 106 |
+
for key in f.keys():
|
| 107 |
+
weight_map[key] = shard.name
|
| 108 |
+
index = {"metadata": {"total_size": sum(s.stat().st_size for s in shards)}, "weight_map": weight_map}
|
| 109 |
+
(checkpoint_dir / "model.safetensors.index.json").write_text(
|
| 110 |
+
json.dumps(index, indent=2, sort_keys=True), encoding="utf-8"
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
|
| 114 |
def download_repo_files(repo_id: str, revision: str | None, local_dir: Path) -> None:
|
| 115 |
"""Download all non-weight files from the MLX repo into local_dir."""
|
| 116 |
print(f"Downloading aux files from {repo_id} ...")
|
|
|
|
| 120 |
for fname in files:
|
| 121 |
if fname.endswith(".safetensors"):
|
| 122 |
continue
|
| 123 |
+
if fname.endswith(".safetensors.index.json"):
|
| 124 |
+
# We will regenerate the index if sharding, or use a single file.
|
| 125 |
+
continue
|
| 126 |
print(f" {fname}")
|
| 127 |
hf_hub_download(
|
| 128 |
repo_id=repo_id,
|
|
|
|
| 229 |
# Rewrite the final shard names with the actual count.
|
| 230 |
shards = sorted(output_dir.glob("model-?????-of-?????.safetensors"))
|
| 231 |
total = len(shards)
|
| 232 |
+
if total == 1:
|
| 233 |
+
# Transformers / litert expect a single "model.safetensors" for unsharded checkpoints.
|
| 234 |
+
shards[0].rename(output_dir / "model.safetensors")
|
| 235 |
+
else:
|
| 236 |
+
for i, old in enumerate(shards):
|
| 237 |
+
new = old.with_name(f"model-{i:05d}-of-{total:05d}.safetensors")
|
| 238 |
+
old.rename(new)
|
| 239 |
+
# Generate a fresh index so transformers can load the sharded checkpoint.
|
| 240 |
+
_write_sharded_index(output_dir)
|
| 241 |
|
| 242 |
print(f"Wrote {total} safetensors shard(s) to {output_dir}")
|
| 243 |
|