|
| 1 | +#!/usr/bin/env python |
| 2 | +# coding=utf-8 |
| 3 | +# Copyright 2022 The HuggingFace Team All rights reserved. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +""" |
| 17 | +Create a VisionEncoderDecoderModel instance from pretrained encoder/decoder models. |
| 18 | +
|
| 19 | +The cross-attention will be randomly initialized. |
| 20 | +""" |
| 21 | + |
| 22 | +from dataclasses import dataclass, field |
| 23 | +from typing import Optional |
| 24 | + |
| 25 | +from transformers import ( |
| 26 | + AutoConfig, |
| 27 | + AutoFeatureExtractor, |
| 28 | + AutoTokenizer, |
| 29 | + FlaxVisionEncoderDecoderModel, |
| 30 | + HfArgumentParser, |
| 31 | +) |
| 32 | + |
| 33 | + |
| 34 | +@dataclass |
| 35 | +class ModelArguments: |
| 36 | + """ |
| 37 | + Arguments pertaining to which model/config/tokenizer we are going to fine-tune, or train from scratch. |
| 38 | + """ |
| 39 | + |
| 40 | + output_dir: str = field( |
| 41 | + metadata={"help": "The output directory where the model will be written."}, |
| 42 | + ) |
| 43 | + encoder_model_name_or_path: str = field( |
| 44 | + metadata={ |
| 45 | + "help": "The encoder model checkpoint for weights initialization." |
| 46 | + "Don't set if you want to train an encoder model from scratch." |
| 47 | + }, |
| 48 | + ) |
| 49 | + decoder_model_name_or_path: str = field( |
| 50 | + metadata={ |
| 51 | + "help": "The decoder model checkpoint for weights initialization." |
| 52 | + "Don't set if you want to train a decoder model from scratch." |
| 53 | + }, |
| 54 | + ) |
| 55 | + encoder_config_name: Optional[str] = field( |
| 56 | + default=None, metadata={"help": "Pretrained encoder config name or path if not the same as encoder_model_name"} |
| 57 | + ) |
| 58 | + decoder_config_name: Optional[str] = field( |
| 59 | + default=None, metadata={"help": "Pretrained decoder config name or path if not the same as decoder_model_name"} |
| 60 | + ) |
| 61 | + |
| 62 | + |
| 63 | +def main(): |
| 64 | + parser = HfArgumentParser((ModelArguments,)) |
| 65 | + (model_args,) = parser.parse_args_into_dataclasses() |
| 66 | + |
| 67 | + # Load pretrained model and tokenizer |
| 68 | + |
| 69 | + # Use explicit specified encoder config |
| 70 | + if model_args.encoder_config_name: |
| 71 | + encoder_config = AutoConfig.from_pretrained(model_args.encoder_config_name) |
| 72 | + # Use pretrained encoder model's config |
| 73 | + else: |
| 74 | + encoder_config = AutoConfig.from_pretrained(model_args.encoder_model_name_or_path) |
| 75 | + |
| 76 | + # Use explicit specified decoder config |
| 77 | + if model_args.decoder_config_name: |
| 78 | + decoder_config = AutoConfig.from_pretrained(model_args.decoder_config_name) |
| 79 | + # Use pretrained decoder model's config |
| 80 | + else: |
| 81 | + decoder_config = AutoConfig.from_pretrained(model_args.decoder_model_name_or_path) |
| 82 | + |
| 83 | + # necessary for `from_encoder_decoder_pretrained` when `decoder_config` is passed |
| 84 | + decoder_config.is_decoder = True |
| 85 | + decoder_config.add_cross_attention = True |
| 86 | + |
| 87 | + model = FlaxVisionEncoderDecoderModel.from_encoder_decoder_pretrained( |
| 88 | + encoder_pretrained_model_name_or_path=model_args.encoder_model_name_or_path, |
| 89 | + decoder_pretrained_model_name_or_path=model_args.decoder_model_name_or_path, |
| 90 | + encoder_config=encoder_config, |
| 91 | + decoder_config=decoder_config, |
| 92 | + ) |
| 93 | + |
| 94 | + # GPT2 only has bos/eos tokens but not decoder_start/pad tokens |
| 95 | + decoder_start_token_id = decoder_config.decoder_start_token_id |
| 96 | + pad_token_id = decoder_config.pad_token_id |
| 97 | + if decoder_start_token_id is None: |
| 98 | + decoder_start_token_id = decoder_config.bos_token_id |
| 99 | + if pad_token_id is None: |
| 100 | + pad_token_id = decoder_config.eos_token_id |
| 101 | + |
| 102 | + # This is necessary to make Flax's generate() work |
| 103 | + model.config.eos_token_id = decoder_config.eos_token_id |
| 104 | + model.config.decoder_start_token_id = decoder_start_token_id |
| 105 | + model.config.pad_token_id = pad_token_id |
| 106 | + |
| 107 | + feature_extractor = AutoFeatureExtractor.from_pretrained(model_args.encoder_model_name_or_path) |
| 108 | + |
| 109 | + tokenizer = AutoTokenizer.from_pretrained(model_args.decoder_model_name_or_path) |
| 110 | + tokenizer.pad_token = tokenizer.convert_ids_to_tokens(model.config.pad_token_id) |
| 111 | + |
| 112 | + model.save_pretrained(model_args.output_dir) |
| 113 | + feature_extractor.save_pretrained(model_args.output_dir) |
| 114 | + tokenizer.save_pretrained(model_args.output_dir) |
| 115 | + |
| 116 | + |
| 117 | +if __name__ == "__main__": |
| 118 | + main() |
0 commit comments