Skip to content

Commit 3248c3f

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

3 files changed

Lines changed: 25 additions & 2 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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ def quantize(weights, config, args):
382382

383383
# Save weights
384384
print("[INFO] Saving")
385-
mx.save_safetensors(str(mlx_path / "model.safetensors"), weights)
385+
mx.save_safetensors(str(mlx_path / "weights.safetensors"), weights)
386386

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

whisper/test.py

Lines changed: 23 additions & 0 deletions
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
@@ -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_weights_safetensors_are_loadable(self):
85+
weights = dict(tree_flatten(self.model.parameters()))
86+
with tempfile.TemporaryDirectory() as temp_dir:
87+
model_dir = Path(temp_dir)
88+
weights_path = model_dir / "weights.safetensors"
89+
mx.save_safetensors(str(weights_path), weights)
90+
with open(model_dir / "config.json", "w") as f:
91+
config = asdict(self.model.dims)
92+
config["model_type"] = "whisper"
93+
json.dump(config, f)
94+
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)