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

add whisper model #458

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
189 changes: 189 additions & 0 deletions mergekit/_data/architectures/whisper.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
{
"model_type": "whisper",
"architectures": [
"WhisperForConditionalGeneration"
],
"pre_weights": [
{
"name": "model.encoder.conv1.weight"
},
{
"name": "model.encoder.conv1.bias"
},
{
"name": "model.encoder.conv2.weight"
},
{
"name": "model.encoder.conv2.bias"
},
{
"name": "model.encoder.embed_positions.weight"
},
{
"name": "model.decoder.embed_tokens.weight"
},
{
"name": "model.decoder.embed_positions.weight"
}
],
"post_weights": [
{
"name": "model.encoder.layer_norm.weight"
},
{
"name": "model.encoder.layer_norm.bias"
},
{
"name": "model.decoder.layer_norm.weight"
},
{
"name": "model.decoder.layer_norm.bias"
}
],
"num_layers_config_key": "num_hidden_layers",
"layer_templates": {
"weights": [
{
"name": "model.encoder.layers.${layer_index}.self_attn.k_proj.weight"
},
{
"name": "model.encoder.layers.${layer_index}.self_attn.v_proj.weight"
},
{
"name": "model.encoder.layers.${layer_index}.self_attn.v_proj.bias"
},
{
"name": "model.encoder.layers.${layer_index}.self_attn.q_proj.weight"
},
{
"name": "model.encoder.layers.${layer_index}.self_attn.q_proj.bias"
},
{
"name": "model.encoder.layers.${layer_index}.self_attn.out_proj.weight"
},
{
"name": "model.encoder.layers.${layer_index}.self_attn.out_proj.bias"
},
{
"name": "model.encoder.layers.${layer_index}.self_attn_layer_norm.weight"
},
{
"name": "model.encoder.layers.${layer_index}.self_attn_layer_norm.bias"
},
{
"name": "model.encoder.layers.${layer_index}.fc1.weight"
},
{
"name": "model.encoder.layers.${layer_index}.fc1.bias"
},
{
"name": "model.encoder.layers.${layer_index}.fc2.weight"
},
{
"name": "model.encoder.layers.${layer_index}.fc2.bias"
},
{
"name": "model.encoder.layers.${layer_index}.final_layer_norm.weight"
},
{
"name": "model.encoder.layers.${layer_index}.final_layer_norm.bias"
},
{
"name": "model.decoder.layers.${layer_index}.self_attn.k_proj.weight",
"optional": true
},
{
"name": "model.decoder.layers.${layer_index}.self_attn.v_proj.weight",
"optional": true
},
{
"name": "model.decoder.layers.${layer_index}.self_attn.v_proj.bias",
"optional": true
},
{
"name": "model.decoder.layers.${layer_index}.self_attn.q_proj.weight",
"optional": true
},
{
"name": "model.decoder.layers.${layer_index}.self_attn.q_proj.bias",
"optional": true
},
{
"name": "model.decoder.layers.${layer_index}.self_attn.out_proj.weight",
"optional": true
},
{
"name": "model.decoder.layers.${layer_index}.self_attn.out_proj.bias",
"optional": true
},
{
"name": "model.decoder.layers.${layer_index}.self_attn_layer_norm.weight",
"optional": true
},
{
"name": "model.decoder.layers.${layer_index}.self_attn_layer_norm.bias",
"optional": true
},
{
"name": "model.decoder.layers.${layer_index}.encoder_attn.k_proj.weight",
"optional": true
},
{
"name": "model.decoder.layers.${layer_index}.encoder_attn.v_proj.weight",
"optional": true
},
{
"name": "model.decoder.layers.${layer_index}.encoder_attn.v_proj.bias",
"optional": true
},
{
"name": "model.decoder.layers.${layer_index}.encoder_attn.q_proj.weight",
"optional": true
},
{
"name": "model.decoder.layers.${layer_index}.encoder_attn.q_proj.bias",
"optional": true
},
{
"name": "model.decoder.layers.${layer_index}.encoder_attn.out_proj.weight",
"optional": true
},
{
"name": "model.decoder.layers.${layer_index}.encoder_attn.out_proj.bias",
"optional": true
},
{
"name": "model.decoder.layers.${layer_index}.encoder_attn_layer_norm.weight",
"optional": true
},
{
"name": "model.decoder.layers.${layer_index}.encoder_attn_layer_norm.bias",
"optional": true
},
{
"name": "model.decoder.layers.${layer_index}.fc1.weight",
"optional": true
},
{
"name": "model.decoder.layers.${layer_index}.fc1.bias",
"optional": true
},
{
"name": "model.decoder.layers.${layer_index}.fc2.weight",
"optional": true
},
{
"name": "model.decoder.layers.${layer_index}.fc2.bias",
"optional": true
},
{
"name": "model.decoder.layers.${layer_index}.final_layer_norm.weight",
"optional": true
},
{
"name": "model.decoder.layers.${layer_index}.final_layer_norm.bias",
"optional": true
}
]
}
}
43 changes: 43 additions & 0 deletions mergekit/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,19 @@ def run_merge(
"Chat template specified but no tokenizer found. Chat template will not be saved."
)

# Copy feature_extractor if it is a whisper model
if options.copy_feature_extractor and arch_info.definition.expected_model_type == "whisper":
try:
_copy_feature_extractor(
merge_config, out_path, trust_remote_code=options.trust_remote_code
)
except Exception as e:
logging.error(
"Failed to copy feature_extractor. The merge was still successful, just copy it from somewhere else.",
exc_info=e,
)


if tokenizer:
logging.info("Saving tokenizer")
_set_chat_template(tokenizer, merge_config)
Expand Down Expand Up @@ -229,6 +242,36 @@ def _copy_tokenizer(
tokenizer.save_pretrained(out_path, safe_serialization=True)


def _copy_feature_extractor(
merge_config: MergeConfiguration, out_path: str, trust_remote_code: bool = False
):
donor_model = merge_config.base_model or (merge_config.referenced_models()[0])

if (os.path.exists(
os.path.join(donor_model.model.path, "preprocessor_config.json")
)
):
logging.info(f"Copying feature_extractor from {donor_model}")

for file_name in [
"preprocessor_config.json",
]:
if os.path.exists(os.path.join(donor_model.model.path, file_name)):
shutil.copy(
os.path.join(donor_model.model.path, file_name),
os.path.join(out_path, file_name),
)
return

# fallback: try actually loading the feature_extractor and saving it
logging.info(f"Reserializing feature_extractor from {donor_model}")
feature_extractor = transformers.AutoFeatureExtractor.from_pretrained(
donor_model.model.path,
revision=donor_model.model.revision,
trust_remote_code=trust_remote_code,
)
_set_chat_template(feature_extractor, merge_config)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is incorrect - feature extractors don't have a chat template do they? Not that it seems to be breaking anything but it's extra work anyway.

feature_extractor.save_pretrained(out_path, safe_serialization=True)
def _model_out_config(
config: MergeConfiguration,
arch_info: ArchitectureInfo,
Expand Down
1 change: 1 addition & 0 deletions mergekit/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class MergeOptions(BaseModel):
low_cpu_memory: bool = False
out_shard_size: int = parse_kmb("5B")
copy_tokenizer: bool = True
copy_feature_extractor: bool = True
clone_tensors: bool = False
trust_remote_code: bool = False
random_seed: Optional[int] = None
Expand Down
Loading