|
| 1 | +# Copyright 2024 The HuggingFace Inc. team. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +# This script generates tiny models used in the TRL library for unit tests. It pushes them to the Hub under the |
| 16 | +# `trl-internal-testing` organization. |
| 17 | +# This script is meant to be run when adding new tiny model to the TRL library. |
| 18 | + |
| 19 | +from huggingface_hub import HfApi, ModelCard |
| 20 | +from transformers import ( |
| 21 | + AutoProcessor, |
| 22 | + AutoTokenizer, |
| 23 | + BartConfig, |
| 24 | + BartModel, |
| 25 | + BloomConfig, |
| 26 | + BloomForCausalLM, |
| 27 | + CLIPVisionConfig, |
| 28 | + CohereConfig, |
| 29 | + CohereForCausalLM, |
| 30 | + DbrxConfig, |
| 31 | + DbrxForCausalLM, |
| 32 | + FalconMambaConfig, |
| 33 | + FalconMambaForCausalLM, |
| 34 | + Gemma2Config, |
| 35 | + Gemma2ForCausalLM, |
| 36 | + GemmaConfig, |
| 37 | + GemmaForCausalLM, |
| 38 | + GPT2Config, |
| 39 | + GPT2LMHeadModel, |
| 40 | + GPTNeoXConfig, |
| 41 | + GPTNeoXForCausalLM, |
| 42 | + Idefics2Config, |
| 43 | + Idefics2ForConditionalGeneration, |
| 44 | + LlamaConfig, |
| 45 | + LlamaForCausalLM, |
| 46 | + LlavaConfig, |
| 47 | + LlavaForConditionalGeneration, |
| 48 | + LlavaNextConfig, |
| 49 | + LlavaNextForConditionalGeneration, |
| 50 | + MistralConfig, |
| 51 | + MistralForCausalLM, |
| 52 | + OPTConfig, |
| 53 | + OPTForCausalLM, |
| 54 | + PaliGemmaConfig, |
| 55 | + PaliGemmaForConditionalGeneration, |
| 56 | + Phi3Config, |
| 57 | + Phi3ForCausalLM, |
| 58 | + Qwen2Config, |
| 59 | + Qwen2ForCausalLM, |
| 60 | + SiglipVisionConfig, |
| 61 | + T5Config, |
| 62 | + T5ForConditionalGeneration, |
| 63 | +) |
| 64 | +from transformers.models.idefics2.configuration_idefics2 import Idefics2VisionConfig |
| 65 | + |
| 66 | + |
| 67 | +ORGANIZATION = "trl-internal-testing" |
| 68 | + |
| 69 | +MODEL_CARD = """ |
| 70 | +--- |
| 71 | +library_name: transformers |
| 72 | +tags: [trl] |
| 73 | +--- |
| 74 | +
|
| 75 | +# Tiny {model_class_name} |
| 76 | +
|
| 77 | +This is a minimal model built for unit tests in the [TRL](https://github.com/huggingface/trl) library. |
| 78 | +""" |
| 79 | + |
| 80 | + |
| 81 | +api = HfApi() |
| 82 | + |
| 83 | + |
| 84 | +def push_to_hub(model, tokenizer, suffix=None): |
| 85 | + model_class_name = model.__class__.__name__ |
| 86 | + content = MODEL_CARD.format(model_class_name=model_class_name) |
| 87 | + model_card = ModelCard(content) |
| 88 | + repo_id = f"{ORGANIZATION}/tiny-{model_class_name}" |
| 89 | + if suffix is not None: |
| 90 | + repo_id += f"-{suffix}" |
| 91 | + |
| 92 | + if api.repo_exists(repo_id): |
| 93 | + print(f"Model {repo_id} already exists, skipping") |
| 94 | + else: |
| 95 | + model.push_to_hub(repo_id) |
| 96 | + tokenizer.push_to_hub(repo_id) |
| 97 | + model_card.push_to_hub(repo_id) |
| 98 | + |
| 99 | + |
| 100 | +# Decoder models |
| 101 | +for model_id, config_class, model_class, suffix in [ |
| 102 | + ("bigscience/bloomz-560m", BloomConfig, BloomForCausalLM, None), |
| 103 | + ("CohereForAI/aya-expanse-8b", CohereConfig, CohereForCausalLM, None), |
| 104 | + ("databricks/dbrx-instruct", DbrxConfig, DbrxForCausalLM, None), |
| 105 | + ("tiiuae/falcon-7b-instruct", FalconMambaConfig, FalconMambaForCausalLM, None), |
| 106 | + ("google/gemma-2-2b-it", Gemma2Config, Gemma2ForCausalLM, None), |
| 107 | + ("google/gemma-7b-it", GemmaConfig, GemmaForCausalLM, None), |
| 108 | + ("openai-community/gpt2", GPT2Config, GPT2LMHeadModel, None), |
| 109 | + ("EleutherAI/pythia-14m", GPTNeoXConfig, GPTNeoXForCausalLM, None), |
| 110 | + ("meta-llama/Meta-Llama-3-8B-Instruct", LlamaConfig, LlamaForCausalLM, "3"), |
| 111 | + ("meta-llama/Llama-3.1-8B-Instruct", LlamaConfig, LlamaForCausalLM, "3.1"), |
| 112 | + ("meta-llama/Llama-3.2-1B-Instruct", LlamaConfig, LlamaForCausalLM, "3.2"), |
| 113 | + ("mistralai/Mistral-7B-Instruct-v0.1", MistralConfig, MistralForCausalLM, "0.1"), |
| 114 | + ("mistralai/Mistral-7B-Instruct-v0.2", MistralConfig, MistralForCausalLM, "0.2"), |
| 115 | + ("facebook/opt-1.3b", OPTConfig, OPTForCausalLM, None), |
| 116 | + ("microsoft/Phi-3.5-mini-instruct", Phi3Config, Phi3ForCausalLM, None), |
| 117 | + ("Qwen/Qwen2.5-32B-Instruct", Qwen2Config, Qwen2ForCausalLM, "2.5"), |
| 118 | + ("Qwen/Qwen2.5-Coder-0.5B", Qwen2Config, Qwen2ForCausalLM, "2.5-Coder"), |
| 119 | +]: |
| 120 | + tokenizer = AutoTokenizer.from_pretrained(model_id) |
| 121 | + config = config_class( |
| 122 | + vocab_size=tokenizer.vocab_size + len(tokenizer.added_tokens_encoder.keys()), |
| 123 | + hidden_size=8, |
| 124 | + num_attention_heads=4, |
| 125 | + num_key_value_heads=2, |
| 126 | + num_hidden_layers=2, |
| 127 | + intermediate_size=32, |
| 128 | + ) |
| 129 | + model = model_class(config) |
| 130 | + push_to_hub(model, tokenizer, suffix) |
| 131 | + |
| 132 | + |
| 133 | +# Encoder-decoder models |
| 134 | +for model_id, config_class, model_class, suffix in [ |
| 135 | + ("google/flan-t5-small", T5Config, T5ForConditionalGeneration, None), |
| 136 | + ("facebook/bart-base", BartConfig, BartModel, None), |
| 137 | +]: |
| 138 | + tokenizer = AutoTokenizer.from_pretrained(model_id) |
| 139 | + config = config_class( |
| 140 | + vocab_size=tokenizer.vocab_size + len(tokenizer.added_tokens_encoder.keys()), |
| 141 | + d_model=16, |
| 142 | + encoder_layers=2, |
| 143 | + decoder_layers=2, |
| 144 | + d_kv=2, |
| 145 | + d_ff=64, |
| 146 | + num_layers=6, |
| 147 | + num_heads=8, |
| 148 | + decoder_start_token_id=0, |
| 149 | + is_encoder_decoder=True, |
| 150 | + ) |
| 151 | + model = model_class(config) |
| 152 | + push_to_hub(model, tokenizer, suffix) |
| 153 | + |
| 154 | + |
| 155 | +# Vision Language Models |
| 156 | +# fmt: off |
| 157 | +for model_id, config_class, text_config_class, vision_config_class, model_class in [ |
| 158 | + ("HuggingFaceM4/idefics2-8b", Idefics2Config, MistralConfig, Idefics2VisionConfig, Idefics2ForConditionalGeneration), |
| 159 | + ("llava-hf/llava-1.5-7b-hf", LlavaConfig, LlamaConfig, CLIPVisionConfig, LlavaForConditionalGeneration), |
| 160 | + ("llava-hf/llava-v1.6-mistral-7b-hf", LlavaNextConfig, MistralConfig, CLIPVisionConfig, LlavaNextForConditionalGeneration), |
| 161 | + ("google/paligemma-3b-pt-224", PaliGemmaConfig, GemmaConfig, SiglipVisionConfig, PaliGemmaForConditionalGeneration), |
| 162 | +]: |
| 163 | +# fmt: on |
| 164 | + processor = AutoProcessor.from_pretrained(model_id) |
| 165 | + kwargs = {} |
| 166 | + if config_class == PaliGemmaConfig: |
| 167 | + kwargs["projection_dim"] = 8 |
| 168 | + vision_kwargs = {} |
| 169 | + if vision_config_class in [CLIPVisionConfig, SiglipVisionConfig]: |
| 170 | + vision_kwargs["projection_dim"] = 8 |
| 171 | + if vision_config_class == CLIPVisionConfig: |
| 172 | + vision_kwargs["image_size"] = 336 |
| 173 | + vision_kwargs["patch_size"] = 14 |
| 174 | + config = config_class( |
| 175 | + text_config=text_config_class( |
| 176 | + vocab_size=processor.tokenizer.vocab_size + len(processor.tokenizer.added_tokens_encoder), |
| 177 | + hidden_size=8, |
| 178 | + num_attention_heads=4, |
| 179 | + num_key_value_heads=2, |
| 180 | + num_hidden_layers=2, |
| 181 | + intermediate_size=32, |
| 182 | + ), |
| 183 | + vision_config=vision_config_class( |
| 184 | + hidden_size=8, |
| 185 | + num_attention_heads=4, |
| 186 | + num_hidden_layers=2, |
| 187 | + intermediate_size=32, |
| 188 | + **vision_kwargs, |
| 189 | + ), |
| 190 | + **kwargs, |
| 191 | + ) |
| 192 | + model = model_class(config) |
| 193 | + push_to_hub(model, processor) |
0 commit comments