| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
import io |
| 35 |
import os |
| 36 |
import sys |
| 37 |
import struct |
| 38 |
import json |
| 39 |
import code |
| 40 |
import torch |
| 41 |
import numpy as np |
| 42 |
import base64 |
| 43 |
from pathlib import Path |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
def bytes_to_unicode(): |
| 172 |
""" |
| 173 |
Returns list of utf-8 byte and a corresponding list of unicode strings. |
| 174 |
The reversible bpe codes work on unicode strings. |
| 175 |
This means you need a large # of unicode characters in your vocab if you want to avoid UNKs. |
| 176 |
When you're at something like a 10B token dataset you end up needing around 5K for decent coverage. |
| 177 |
This is a signficant percentage of your normal, say, 32K bpe vocab. |
| 178 |
To avoid that, we want lookup tables between utf-8 bytes and unicode strings. |
| 179 |
And avoids mapping to whitespace/control characters the bpe code barfs on. |
| 180 |
""" |
| 181 |
bs = list(range(ord("!"), ord("~")+1))+list(range(ord("¡"), ord("¬")+1))+list(range(ord("®"), ord("ÿ")+1)) |
| 182 |
cs = bs[:] |
| 183 |
n = 0 |
| 184 |
for b in range(2**8): |
| 185 |
if b not in bs: |
| 186 |
bs.append(b) |
| 187 |
cs.append(2**8+n) |
| 188 |
n += 1 |
| 189 |
cs = [chr(n) for n in cs] |
| 190 |
return dict(zip(bs, cs)) |
| 191 |
|
| 192 |
|
| 193 |
if len(sys.argv) < 4: |
| 194 |
print("Usage: convert-pt-to-ggml.py model.pt path-to-whisper-repo dir-output [use-f32]\n") |
| 195 |
sys.exit(1) |
| 196 |
|
| 197 |
fname_inp = Path(sys.argv[1]) |
| 198 |
dir_whisper = Path(sys.argv[2]) |
| 199 |
dir_out = Path(sys.argv[3]) |
| 200 |
|
| 201 |
|
| 202 |
try: |
| 203 |
model_bytes = open(fname_inp, "rb").read() |
| 204 |
with io.BytesIO(model_bytes) as fp: |
| 205 |
checkpoint = torch.load(fp, map_location="cpu") |
| 206 |
except Exception: |
| 207 |
print("Error: failed to load PyTorch model file:" , fname_inp) |
| 208 |
sys.exit(1) |
| 209 |
|
| 210 |
hparams = checkpoint["dims"] |
| 211 |
print("hparams:", hparams) |
| 212 |
|
| 213 |
list_vars = checkpoint["model_state_dict"] |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
n_mels = hparams["n_mels"] |
| 221 |
with np.load(dir_whisper / "whisper" / "assets" / "mel_filters.npz") as f: |
| 222 |
filters = torch.from_numpy(f[f"mel_{n_mels}"]) |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
multilingual = hparams["n_vocab"] >= 51865 |
| 232 |
tokenizer = dir_whisper / "whisper" / "assets" / (multilingual and "multilingual.tiktoken" or "gpt2.tiktoken") |
| 233 |
tokenizer_type = "tiktoken" |
| 234 |
if not tokenizer.is_file(): |
| 235 |
tokenizer = dir_whisper / "whisper" / "assets" / (multilingual and "multilingual" or "gpt2") / "vocab.json" |
| 236 |
tokenizer_type = "hf_transformers" |
| 237 |
if not tokenizer.is_file(): |
| 238 |
print("Error: failed to find either tiktoken or hf_transformers tokenizer file:", tokenizer) |
| 239 |
sys.exit(1) |
| 240 |
|
| 241 |
byte_encoder = bytes_to_unicode() |
| 242 |
byte_decoder = {v:k for k, v in byte_encoder.items()} |
| 243 |
|
| 244 |
if tokenizer_type == "tiktoken": |
| 245 |
with open(tokenizer, "rb") as f: |
| 246 |
contents = f.read() |
| 247 |
tokens = {base64.b64decode(token): int(rank) for token, rank in (line.split() for line in contents.splitlines() if line)} |
| 248 |
elif tokenizer_type == "hf_transformers": |
| 249 |
with open(tokenizer, "r", encoding="utf8") as f: |
| 250 |
_tokens_raw = json.load(f) |
| 251 |
if '<|endoftext|>' in _tokens_raw: |
| 252 |
|
| 253 |
|
| 254 |
del _tokens_raw['<|endoftext|>'] |
| 255 |
tokens = {bytes([byte_decoder[c] for c in token]): int(idx) for token, idx in _tokens_raw.items()} |
| 256 |
|
| 257 |
|
| 258 |
fname_out = dir_out / "ggml-model.bin" |
| 259 |
|
| 260 |
|
| 261 |
use_f16 = True |
| 262 |
if len(sys.argv) > 4: |
| 263 |
use_f16 = False |
| 264 |
fname_out = dir_out / "ggml-model-f32.bin" |
| 265 |
|
| 266 |
fout = fname_out.open("wb") |
| 267 |
|
| 268 |
fout.write(struct.pack("i", 0x67676d6c)) |
| 269 |
fout.write(struct.pack("i", hparams["n_vocab"])) |
| 270 |
fout.write(struct.pack("i", hparams["n_audio_ctx"])) |
| 271 |
fout.write(struct.pack("i", hparams["n_audio_state"])) |
| 272 |
fout.write(struct.pack("i", hparams["n_audio_head"])) |
| 273 |
fout.write(struct.pack("i", hparams["n_audio_layer"])) |
| 274 |
fout.write(struct.pack("i", hparams["n_text_ctx"])) |
| 275 |
fout.write(struct.pack("i", hparams["n_text_state"])) |
| 276 |
fout.write(struct.pack("i", hparams["n_text_head"])) |
| 277 |
fout.write(struct.pack("i", hparams["n_text_layer"])) |
| 278 |
fout.write(struct.pack("i", hparams["n_mels"])) |
| 279 |
fout.write(struct.pack("i", use_f16)) |
| 280 |
|
| 281 |
|
| 282 |
fout.write(struct.pack("i", filters.shape[0])) |
| 283 |
fout.write(struct.pack("i", filters.shape[1])) |
| 284 |
for i in range(filters.shape[0]): |
| 285 |
for j in range(filters.shape[1]): |
| 286 |
fout.write(struct.pack("f", filters[i][j])) |
| 287 |
|
| 288 |
|
| 289 |
fout.write(struct.pack("i", len(tokens))) |
| 290 |
|
| 291 |
for key in tokens: |
| 292 |
fout.write(struct.pack("i", len(key))) |
| 293 |
fout.write(key) |
| 294 |
|
| 295 |
for name in list_vars.keys(): |
| 296 |
data = list_vars[name].squeeze().numpy() |
| 297 |
print("Processing variable: " , name , " with shape: ", data.shape) |
| 298 |
|
| 299 |
|
| 300 |
if name in ["encoder.conv1.bias", "encoder.conv2.bias"]: |
| 301 |
data = data.reshape(data.shape[0], 1) |
| 302 |
print(f" Reshaped variable: {name} to shape: ", data.shape) |
| 303 |
|
| 304 |
n_dims = len(data.shape) |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
ftype = 1 |
| 310 |
if use_f16: |
| 311 |
if n_dims < 2 or \ |
| 312 |
name == "encoder.conv1.bias" or \ |
| 313 |
name == "encoder.conv2.bias" or \ |
| 314 |
name == "encoder.positional_embedding" or \ |
| 315 |
name == "decoder.positional_embedding": |
| 316 |
print(" Converting to float32") |
| 317 |
data = data.astype(np.float32) |
| 318 |
ftype = 0 |
| 319 |
else: |
| 320 |
data = data.astype(np.float32) |
| 321 |
ftype = 0 |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
str_ = name.encode('utf-8') |
| 331 |
fout.write(struct.pack("iii", n_dims, len(str_), ftype)) |
| 332 |
for i in range(n_dims): |
| 333 |
fout.write(struct.pack("i", data.shape[n_dims - 1 - i])) |
| 334 |
fout.write(str_) |
| 335 |
|
| 336 |
|
| 337 |
data.tofile(fout) |
| 338 |
|
| 339 |
fout.close() |
| 340 |
|
| 341 |
print("Done. Output file: " , fname_out) |
| 342 |
print("") |