Upload export_apochat_litert.py with huggingface_hub
Browse files- export_apochat_litert.py +17 -1
export_apochat_litert.py
CHANGED
|
@@ -134,6 +134,21 @@ def download_repo_files(repo_id: str, revision: str | None, local_dir: Path) ->
|
|
| 134 |
)
|
| 135 |
|
| 136 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
def patch_config_for_pytorch(config_path: Path) -> None:
|
| 138 |
"""Remove MLX quantization config and ensure torch_dtype is bfloat16."""
|
| 139 |
with open(config_path, "r", encoding="utf-8") as f:
|
|
@@ -218,7 +233,8 @@ def dequantize_mlx_to_pytorch(
|
|
| 218 |
arr = arr.astype(mx.bfloat16)
|
| 219 |
|
| 220 |
torch_tensor = mlx_bfloat16_to_torch(arr)
|
| 221 |
-
|
|
|
|
| 222 |
current_shard_bytes += torch_tensor.nbytes
|
| 223 |
|
| 224 |
if current_shard_bytes >= shard_size_bytes:
|
|
|
|
| 134 |
)
|
| 135 |
|
| 136 |
|
| 137 |
+
def mlx_key_to_pytorch_key(key: str) -> str:
|
| 138 |
+
"""Map MLX Gemma 4 key names to PyTorch / transformers key names."""
|
| 139 |
+
if key.startswith("language_model.model."):
|
| 140 |
+
return "model.language_model." + key[len("language_model.model."):]
|
| 141 |
+
if key.startswith("audio_tower."):
|
| 142 |
+
return "model.audio_tower." + key[len("audio_tower."):]
|
| 143 |
+
if key.startswith("vision_tower."):
|
| 144 |
+
return "model.vision_tower." + key[len("vision_tower."):]
|
| 145 |
+
if key.startswith("embed_audio.embedding_projection"):
|
| 146 |
+
return key.replace("embed_audio.embedding_projection", "model.embed_audio.embedding_projection", 1)
|
| 147 |
+
if key.startswith("embed_vision.embedding_projection"):
|
| 148 |
+
return key.replace("embed_vision.embedding_projection", "model.embed_vision.embedding_projection", 1)
|
| 149 |
+
raise ValueError(f"Unexpected MLX key prefix: {key}")
|
| 150 |
+
|
| 151 |
+
|
| 152 |
def patch_config_for_pytorch(config_path: Path) -> None:
|
| 153 |
"""Remove MLX quantization config and ensure torch_dtype is bfloat16."""
|
| 154 |
with open(config_path, "r", encoding="utf-8") as f:
|
|
|
|
| 233 |
arr = arr.astype(mx.bfloat16)
|
| 234 |
|
| 235 |
torch_tensor = mlx_bfloat16_to_torch(arr)
|
| 236 |
+
pytorch_name = mlx_key_to_pytorch_key(name)
|
| 237 |
+
current_shard[pytorch_name] = torch_tensor
|
| 238 |
current_shard_bytes += torch_tensor.nbytes
|
| 239 |
|
| 240 |
if current_shard_bytes >= shard_size_bytes:
|