Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce whisper decoder file size with onnx export #328

Merged
merged 1 commit into from
Sep 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 33 additions & 5 deletions scripts/whisper/export-onnx.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,25 @@ def forward(

x = self.textDecoder.ln(x)

logits = (
x
@ torch.transpose(self.textDecoder.token_embedding.weight.to(x.dtype), 0, 1)
).float()
if False:
# x.shape (1, 3, 384)
# weight.shape (51684, 384)

logits = (
x
@ torch.transpose(
self.textDecoder.token_embedding.weight.to(x.dtype), 0, 1
)
).float()
else:
logits = (
torch.matmul(
self.textDecoder.token_embedding.weight.to(x.dtype),
x.permute(0, 2, 1),
)
.permute(0, 2, 1)
.float()
)

return logits, n_layer_self_k_cache, n_layer_self_v_cache

Expand Down Expand Up @@ -246,6 +261,19 @@ def main():
opset_version = 13

model = whisper.load_model(name)
print(
f"number of model parameters: {name}",
sum(p.numel() for p in model.parameters()),
)
print(
f"number of encoder parameters: {name}",
sum(p.numel() for p in model.encoder.parameters()),
)
print(
f"number of decoder parameters: {name}",
sum(p.numel() for p in model.decoder.parameters()),
)

convert_tokens(name=name, model=model)

# write tokens
Expand Down Expand Up @@ -419,7 +447,7 @@ def main():
},
)

if 'large' in args.model:
if "large" in args.model:
# it causes errors for large models, so skip it.
return
# Generate int8 quantization models
Expand Down
Loading