-
Notifications
You must be signed in to change notification settings - Fork 365
Add support for lite-whisper #1886
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
Draft
Eyoel-gebre
wants to merge
63
commits into
OpenNMT:master
Choose a base branch
from
Eyoel-gebre:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
63 commits
Select commit
Hold shift + click to select a range
ba51314
testing spec
Eyoel-gebre 63cb8ff
testing spec
Eyoel-gebre 24fe394
added test
Eyoel-gebre d258577
update
Eyoel-gebre 607a649
fix
Eyoel-gebre cf840ad
loading fixes
Eyoel-gebre 0412e6f
fix
Eyoel-gebre 7dbd2d8
fixed
Eyoel-gebre a5ac7cd
fixed
Eyoel-gebre a667e1e
test
Eyoel-gebre 9220303
test
Eyoel-gebre 80e6b7b
test
Eyoel-gebre e501642
test
Eyoel-gebre e17f23f
test
Eyoel-gebre eb83092
test
Eyoel-gebre f786c6d
test_axis_1
Eyoel-gebre e7cfdad
test_axis_1
Eyoel-gebre a6a4c42
test_axis_1
Eyoel-gebre ff9a3af
test_axis_1
Eyoel-gebre b8d0c7a
test_axis_1
Eyoel-gebre 566e35b
test_axis_1
Eyoel-gebre 5230ee4
test_axis_1
Eyoel-gebre 220ae07
test_axis_1
Eyoel-gebre 5259c69
test_axis_1
Eyoel-gebre c016c41
test_axis_1
Eyoel-gebre bfd3386
test_axis_1
Eyoel-gebre 58d2b5a
logging
Eyoel-gebre 2b0fbf1
logging
Eyoel-gebre 4aa77d8
logging
Eyoel-gebre 84f54d4
logging
Eyoel-gebre e08a06e
logging
Eyoel-gebre ab31afc
logging
Eyoel-gebre d6b924b
logging
Eyoel-gebre 58c7547
logging
Eyoel-gebre 94e6b34
logging
Eyoel-gebre 97aeeeb
logging
Eyoel-gebre b113821
logging
Eyoel-gebre 77837b9
logging
Eyoel-gebre 98e8941
logging
Eyoel-gebre b6c8edc
logging
Eyoel-gebre c1c5c47
spec
Eyoel-gebre 99ab963
remove-test
Eyoel-gebre 367b6c6
remove-garbage
Eyoel-gebre d7dc107
remove-garbage
Eyoel-gebre 93e4a70
updated model executor for lite-whisper
Eyoel-gebre a29eef3
debugging
Eyoel-gebre 0b5fa40
debugging
Eyoel-gebre 7266663
debugging
Eyoel-gebre dacb949
debugging
Eyoel-gebre ee0527c
debugging
Eyoel-gebre 5cf90b9
debugging
Eyoel-gebre 3098c95
naming fix
Eyoel-gebre 5caaee5
.
Eyoel-gebre 23df460
shapes
Eyoel-gebre 6bdce85
shapes2
Eyoel-gebre 2821169
shape3
Eyoel-gebre b4b35b3
preprocessor
Eyoel-gebre f2b7b22
small
Eyoel-gebre af19fb4
small
Eyoel-gebre 84e346a
dims
Eyoel-gebre 6ac5325
tweaks
Eyoel-gebre e6a83fc
error handling
Eyoel-gebre 76c0bb4
minor
Eyoel-gebre File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
*.pyc | ||
/.vs | ||
.vscode | ||
/build | ||
|
||
CMake*.json | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
import gc | ||
import itertools | ||
import os | ||
import re | ||
|
||
from typing import List, Optional | ||
|
||
|
@@ -96,6 +97,13 @@ def __init__( | |
trust_remote_code: Allow converting models using custom code. | ||
""" | ||
self._model_name_or_path = model_name_or_path | ||
self._model_processor_name = model_name_or_path | ||
if model_name_or_path.startswith('efficient-speech/lite-whisper'): | ||
# If this is a lite-whisper model, use openai's | ||
# corresponding preprocessor. | ||
regex = r'whisper-[a-z0-9-]+?(?=-(?:fast|acc)|$)' | ||
regex_result = re.search(regex, model_name_or_path) | ||
self._model_processor_name = f"openai/{regex_result.group()}" | ||
self._activation_scales = activation_scales | ||
self._copy_files = copy_files | ||
self._load_as_float16 = load_as_float16 | ||
|
@@ -119,7 +127,6 @@ def _load(self): | |
% (config_name, ", ".join(sorted(_MODEL_LOADERS.keys()))) | ||
) | ||
|
||
model_class = getattr(transformers, loader.architecture_name) | ||
tokenizer_class = transformers.AutoTokenizer | ||
|
||
kwargs = { | ||
|
@@ -137,14 +144,21 @@ def _load(self): | |
if self._trust_remote_code: | ||
kwargs["trust_remote_code"] = self._trust_remote_code | ||
|
||
model = self.load_model(model_class, self._model_name_or_path, **kwargs) | ||
if hasattr(transformers, loader.architecture_name): | ||
model_class = getattr(transformers, loader.architecture_name) | ||
model = self.load_model(model_class, self._model_name_or_path, **kwargs) | ||
elif self._model_name_or_path.startswith('efficient-speech/lite-whisper'): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above |
||
model = transformers.AutoModel.from_pretrained(self._model_name_or_path, **kwargs) | ||
else: | ||
raise ValueError( | ||
"The model %s is not supported by the converter. " % self._model_name_or_path) | ||
|
||
tokenizer_kwargs = {} | ||
if self._trust_remote_code: | ||
tokenizer_kwargs["trust_remote_code"] = self._trust_remote_code | ||
|
||
tokenizer = self.load_tokenizer( | ||
tokenizer_class, self._model_name_or_path, **tokenizer_kwargs | ||
tokenizer_class, self._model_processor_name, **tokenizer_kwargs | ||
) | ||
|
||
spec = loader(model, tokenizer) | ||
|
@@ -237,6 +251,19 @@ def set_linear(self, spec, module, quant_type=common_spec.Quantization.CT2): | |
spec.weight = spec.weight.transpose(0, 1) | ||
if module.bias is not None: | ||
spec.bias = module.bias | ||
|
||
def set_low_rank_linear(self, spec, module, quant_type=common_spec.Quantization.CT2): | ||
if quant_type == common_spec.Quantization.CT2: | ||
spec.low_rank_weight_1 = module.weight1 | ||
spec.low_rank_weight_2 = module.weight2 | ||
else: | ||
spec.low_rank_weight_1 = module.qweight1 | ||
spec.low_rank_weight_2 = module.qweight2 | ||
spec.weight_scale = module.scales | ||
spec.weight_zero = module.qzeros | ||
|
||
if module.bias is not None: | ||
spec.bias = module.bias | ||
|
||
def set_embeddings(self, spec, module): | ||
spec.weight = module.weight | ||
|
@@ -996,6 +1023,71 @@ def set_conv1d(self, spec, module): | |
spec.weight = module.weight | ||
spec.bias = module.bias | ||
|
||
@register_loader("LiteWhisperConfig") | ||
class LiteWhisperLoader(WhisperLoader): | ||
@property | ||
def architecture_name(self): | ||
return "LiteWhisperForConditionalGeneration" | ||
|
||
def get_model_spec(self, model): | ||
spec = whisper_spec.WhisperSpec( | ||
model.config.encoder_layers, | ||
model.config.encoder_attention_heads, | ||
model.config.decoder_layers, | ||
model.config.decoder_attention_heads, | ||
low_rank=True, | ||
) | ||
|
||
self.set_encoder(spec.encoder, model.model.encoder) | ||
self.set_decoder(spec.decoder, model.model.decoder) | ||
self.set_linear(spec.decoder.projection, model.proj_out) | ||
|
||
return spec | ||
|
||
def set_encoder(self, spec, encoder): | ||
self.set_conv1d(spec.conv1, encoder.conv1) | ||
self.set_conv1d(spec.conv2, encoder.conv2) | ||
|
||
self.set_common_layers(spec, encoder) | ||
|
||
for layer_spec, layer in zip(spec.layer, encoder.layers): | ||
self.set_low_rank_attention( | ||
layer_spec.self_attention, | ||
layer.self_attn, | ||
) | ||
self.set_layer_norm( | ||
layer_spec.self_attention.layer_norm, | ||
layer.self_attn_layer_norm, | ||
) | ||
|
||
# Double check if these are low rank or not because of potential | ||
# fall backs to full precision. | ||
if hasattr(layer.fc1, 'weight1'): | ||
self.set_low_rank_linear(layer_spec.ffn.linear_0, layer.fc1) | ||
else: | ||
layer_spec.ffn.linear_0 = common_spec.LinearSpec() | ||
self.set_linear(layer_spec.ffn.linear_0, layer.fc1) | ||
|
||
if hasattr(layer.fc2, 'weight1'): | ||
self.set_low_rank_linear(layer_spec.ffn.linear_1, layer.fc2) | ||
else: | ||
layer_spec.ffn.linear_1 = common_spec.LinearSpec() | ||
self.set_linear(layer_spec.ffn.linear_1, layer.fc2) | ||
|
||
self.set_layer_norm(layer_spec.ffn.layer_norm, layer.final_layer_norm) | ||
|
||
def set_low_rank_or_linear_router(self, spec, module, i): | ||
if hasattr(module, "weight1"): | ||
self.set_low_rank_linear(spec.linear[i], module) | ||
else: | ||
spec.linear[i] = common_spec.LinearSpec() | ||
self.set_linear(spec.linear[i], module) | ||
|
||
def set_low_rank_attention(self, spec, attention): | ||
self.set_low_rank_or_linear_router(spec, attention.q_proj, 0) | ||
self.set_low_rank_or_linear_router(spec, attention.k_proj, 1) | ||
self.set_low_rank_or_linear_router(spec, attention.v_proj, 2) | ||
self.set_low_rank_or_linear_router(spec, attention.out_proj, 3) | ||
|
||
@register_loader("Wav2Vec2Config") | ||
class Wav2Vec2Loader(BartLoader): | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is the best approach, since a different org might upload their own model and then this condition will evaluate to false