Skip to content

Commit b4328ae

Browse files
Sahil FaizalSahil Faizal
authored andcommitted
Fix Whisper safetensors conversion compatibility
1 parent 796f5b5 commit b4328ae

3 files changed

Lines changed: 32 additions & 3 deletions

File tree

whisper/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ python convert.py --help
112112
```
113113

114114
By default, the conversion script will make the directory `mlx_models`
115-
and save the converted `weights.npz` and `config.json` there.
115+
and save the converted `weights.safetensors` and `config.json` there.
116116

117117
Each time it is run, `convert.py` will overwrite any model in the provided
118118
path. To save different models, make sure to set `--mlx-path` to a unique

whisper/convert.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from tqdm import tqdm
2222

2323
_VALID_DTYPES = {"float16", "float32"}
24+
_WEIGHTS_FILE_NAME = "weights.safetensors"
2425

2526
_MODELS = {
2627
"tiny.en": "https://openaipublic.azureedge.net/main/whisper/models/d3dd57d32accea0b295c96e26691aa14d8822fac7d9d27d5dc00b4ca2826dd03/tiny.en.pt",
@@ -109,6 +110,11 @@ def available_models() -> List[str]:
109110
return list(_MODELS.keys())
110111

111112

113+
def save_weights(mlx_path: Path, weights) -> None:
114+
"""Save converted weights using the filename supported by released clients."""
115+
mx.save_safetensors(str(mlx_path / _WEIGHTS_FILE_NAME), weights)
116+
117+
112118
def hf_to_pt(weights, config):
113119
config = {
114120
"n_mels": config["num_mel_bins"],
@@ -382,7 +388,7 @@ def quantize(weights, config, args):
382388

383389
# Save weights
384390
print("[INFO] Saving")
385-
mx.save_safetensors(str(mlx_path / "model.safetensors"), weights)
391+
save_weights(mlx_path, weights)
386392

387393
# Save config.json with model_type
388394
with open(str(mlx_path / "config.json"), "w") as f:

whisper/test.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import json
44
import os
5+
import tempfile
56
import unittest
67
from dataclasses import asdict
78
from pathlib import Path
@@ -13,7 +14,7 @@
1314
import mlx_whisper.load_models as load_models
1415
import numpy as np
1516
import torch
16-
from convert import convert, load_torch_model, quantize
17+
from convert import convert, load_torch_model, quantize, save_weights
1718
from mlx.utils import tree_flatten
1819

1920
MODEL_NAME = "tiny"
@@ -75,6 +76,28 @@ def forward_mlx(model, mels, tokens):
7576
return np.array(logits)
7677

7778

79+
class TestWeightSerialization(unittest.TestCase):
80+
@classmethod
81+
def setUpClass(cls):
82+
cls.model = convert(MODEL_NAME, dtype=mx.float32)
83+
84+
def test_save_weights_uses_released_client_filename(self):
85+
weights = dict(tree_flatten(self.model.parameters()))
86+
with tempfile.TemporaryDirectory() as temp_dir:
87+
model_dir = Path(temp_dir)
88+
save_weights(model_dir, weights)
89+
with open(model_dir / "config.json", "w") as f:
90+
config = asdict(self.model.dims)
91+
config["model_type"] = "whisper"
92+
json.dump(config, f)
93+
94+
weights_path = model_dir / "weights.safetensors"
95+
self.assertTrue(weights_path.is_file())
96+
self.assertEqual(set(mx.load(str(weights_path))), set(weights))
97+
loaded_model = load_models.load_model(str(model_dir))
98+
self.assertEqual(loaded_model.dims, self.model.dims)
99+
100+
78101
class TestWhisper(unittest.TestCase):
79102
@classmethod
80103
def setUpClass(cls):

0 commit comments

Comments
 (0)