Layer-pruned Ovis2-8B (PPL-based (Shortened LLaMA), removed 8 layers, 20.9% reduction)
Browse files- .gitattributes +1 -0
- chat_template.jinja +54 -0
- config.json +213 -0
- configuration_aimv2.py +63 -0
- configuration_ovis.py +204 -0
- model.safetensors +3 -0
- modeling_aimv2.py +198 -0
- pruning_info.json +43 -0
- tokenizer.json +3 -0
- tokenizer_config.json +29 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
tokenizer.json filter=lfs diff=lfs merge=lfs -text
|
chat_template.jinja
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{%- if tools %}
|
| 2 |
+
{{- '<|im_start|>system\n' }}
|
| 3 |
+
{%- if messages[0]['role'] == 'system' %}
|
| 4 |
+
{{- messages[0]['content'] }}
|
| 5 |
+
{%- else %}
|
| 6 |
+
{{- 'You are Qwen, created by Alibaba Cloud. You are a helpful assistant.' }}
|
| 7 |
+
{%- endif %}
|
| 8 |
+
{{- "\n\n# Tools\n\nYou may call one or more functions to assist with the user query.\n\nYou are provided with function signatures within <tools></tools> XML tags:\n<tools>" }}
|
| 9 |
+
{%- for tool in tools %}
|
| 10 |
+
{{- "\n" }}
|
| 11 |
+
{{- tool | tojson }}
|
| 12 |
+
{%- endfor %}
|
| 13 |
+
{{- "\n</tools>\n\nFor each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:\n<tool_call>\n{\"name\": <function-name>, \"arguments\": <args-json-object>}\n</tool_call><|im_end|>\n" }}
|
| 14 |
+
{%- else %}
|
| 15 |
+
{%- if messages[0]['role'] == 'system' %}
|
| 16 |
+
{{- '<|im_start|>system\n' + messages[0]['content'] + '<|im_end|>\n' }}
|
| 17 |
+
{%- else %}
|
| 18 |
+
{{- '<|im_start|>system\nYou are Qwen, created by Alibaba Cloud. You are a helpful assistant.<|im_end|>\n' }}
|
| 19 |
+
{%- endif %}
|
| 20 |
+
{%- endif %}
|
| 21 |
+
{%- for message in messages %}
|
| 22 |
+
{%- if (message.role == "user") or (message.role == "system" and not loop.first) or (message.role == "assistant" and not message.tool_calls) %}
|
| 23 |
+
{{- '<|im_start|>' + message.role + '\n' + message.content + '<|im_end|>' + '\n' }}
|
| 24 |
+
{%- elif message.role == "assistant" %}
|
| 25 |
+
{{- '<|im_start|>' + message.role }}
|
| 26 |
+
{%- if message.content %}
|
| 27 |
+
{{- '\n' + message.content }}
|
| 28 |
+
{%- endif %}
|
| 29 |
+
{%- for tool_call in message.tool_calls %}
|
| 30 |
+
{%- if tool_call.function is defined %}
|
| 31 |
+
{%- set tool_call = tool_call.function %}
|
| 32 |
+
{%- endif %}
|
| 33 |
+
{{- '\n<tool_call>\n{"name": "' }}
|
| 34 |
+
{{- tool_call.name }}
|
| 35 |
+
{{- '", "arguments": ' }}
|
| 36 |
+
{{- tool_call.arguments | tojson }}
|
| 37 |
+
{{- '}\n</tool_call>' }}
|
| 38 |
+
{%- endfor %}
|
| 39 |
+
{{- '<|im_end|>\n' }}
|
| 40 |
+
{%- elif message.role == "tool" %}
|
| 41 |
+
{%- if (loop.index0 == 0) or (messages[loop.index0 - 1].role != "tool") %}
|
| 42 |
+
{{- '<|im_start|>user' }}
|
| 43 |
+
{%- endif %}
|
| 44 |
+
{{- '\n<tool_response>\n' }}
|
| 45 |
+
{{- message.content }}
|
| 46 |
+
{{- '\n</tool_response>' }}
|
| 47 |
+
{%- if loop.last or (messages[loop.index0 + 1].role != "tool") %}
|
| 48 |
+
{{- '<|im_end|>\n' }}
|
| 49 |
+
{%- endif %}
|
| 50 |
+
{%- endif %}
|
| 51 |
+
{%- endfor %}
|
| 52 |
+
{%- if add_generation_prompt %}
|
| 53 |
+
{{- '<|im_start|>assistant\n' }}
|
| 54 |
+
{%- endif %}
|
config.json
ADDED
|
@@ -0,0 +1,213 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"architectures": [
|
| 3 |
+
"Ovis"
|
| 4 |
+
],
|
| 5 |
+
"auto_map": {
|
| 6 |
+
"AutoConfig": "configuration_ovis.OvisConfig",
|
| 7 |
+
"AutoModelForCausalLM": "modeling_ovis.Ovis"
|
| 8 |
+
},
|
| 9 |
+
"conversation_formatter_class": "QwenConversationFormatter",
|
| 10 |
+
"disable_tie_weight": false,
|
| 11 |
+
"dtype": "bfloat16",
|
| 12 |
+
"hidden_size": 3584,
|
| 13 |
+
"llm_attn_implementation": "flash_attention_2",
|
| 14 |
+
"llm_config": {
|
| 15 |
+
"_attn_implementation_autoset": true,
|
| 16 |
+
"_name_or_path": "Qwen/Qwen2.5-7B-Instruct",
|
| 17 |
+
"add_cross_attention": false,
|
| 18 |
+
"architectures": [
|
| 19 |
+
"Qwen2ForCausalLM"
|
| 20 |
+
],
|
| 21 |
+
"attention_dropout": 0.0,
|
| 22 |
+
"bos_token_id": 151643,
|
| 23 |
+
"chunk_size_feed_forward": 0,
|
| 24 |
+
"cross_attention_hidden_size": null,
|
| 25 |
+
"decoder_start_token_id": null,
|
| 26 |
+
"dtype": "bfloat16",
|
| 27 |
+
"eos_token_id": 151645,
|
| 28 |
+
"finetuning_task": null,
|
| 29 |
+
"hidden_act": "silu",
|
| 30 |
+
"hidden_size": 3584,
|
| 31 |
+
"id2label": {
|
| 32 |
+
"0": "LABEL_0",
|
| 33 |
+
"1": "LABEL_1"
|
| 34 |
+
},
|
| 35 |
+
"initializer_range": 0.02,
|
| 36 |
+
"intermediate_size": 18944,
|
| 37 |
+
"is_decoder": false,
|
| 38 |
+
"is_encoder_decoder": false,
|
| 39 |
+
"label2id": {
|
| 40 |
+
"LABEL_0": 0,
|
| 41 |
+
"LABEL_1": 1
|
| 42 |
+
},
|
| 43 |
+
"layer_types": [
|
| 44 |
+
"full_attention",
|
| 45 |
+
"full_attention",
|
| 46 |
+
"full_attention",
|
| 47 |
+
"full_attention",
|
| 48 |
+
"full_attention",
|
| 49 |
+
"full_attention",
|
| 50 |
+
"full_attention",
|
| 51 |
+
"full_attention",
|
| 52 |
+
"full_attention",
|
| 53 |
+
"full_attention",
|
| 54 |
+
"full_attention",
|
| 55 |
+
"full_attention",
|
| 56 |
+
"full_attention",
|
| 57 |
+
"full_attention",
|
| 58 |
+
"full_attention",
|
| 59 |
+
"full_attention",
|
| 60 |
+
"full_attention",
|
| 61 |
+
"full_attention",
|
| 62 |
+
"full_attention",
|
| 63 |
+
"full_attention",
|
| 64 |
+
"full_attention",
|
| 65 |
+
"full_attention",
|
| 66 |
+
"full_attention",
|
| 67 |
+
"full_attention",
|
| 68 |
+
"full_attention",
|
| 69 |
+
"full_attention",
|
| 70 |
+
"full_attention",
|
| 71 |
+
"full_attention"
|
| 72 |
+
],
|
| 73 |
+
"max_position_embeddings": 32768,
|
| 74 |
+
"max_window_layers": 28,
|
| 75 |
+
"model_type": "qwen2",
|
| 76 |
+
"num_attention_heads": 28,
|
| 77 |
+
"num_hidden_layers": 28,
|
| 78 |
+
"num_key_value_heads": 4,
|
| 79 |
+
"output_attentions": false,
|
| 80 |
+
"output_hidden_states": false,
|
| 81 |
+
"pad_token_id": null,
|
| 82 |
+
"prefix": null,
|
| 83 |
+
"problem_type": null,
|
| 84 |
+
"pruned_heads": {},
|
| 85 |
+
"return_dict": true,
|
| 86 |
+
"rms_norm_eps": 1e-06,
|
| 87 |
+
"rope_parameters": {
|
| 88 |
+
"rope_theta": 1000000.0,
|
| 89 |
+
"rope_type": "default"
|
| 90 |
+
},
|
| 91 |
+
"sep_token_id": null,
|
| 92 |
+
"sliding_window": null,
|
| 93 |
+
"task_specific_params": null,
|
| 94 |
+
"tf_legacy_loss": false,
|
| 95 |
+
"tie_encoder_decoder": false,
|
| 96 |
+
"tie_word_embeddings": false,
|
| 97 |
+
"tokenizer_class": null,
|
| 98 |
+
"torchscript": false,
|
| 99 |
+
"use_bfloat16": false,
|
| 100 |
+
"use_cache": true,
|
| 101 |
+
"use_sliding_window": false,
|
| 102 |
+
"vocab_size": 152064
|
| 103 |
+
},
|
| 104 |
+
"model_type": "ovis",
|
| 105 |
+
"multimodal_max_length": 32768,
|
| 106 |
+
"num_hidden_layers": 20,
|
| 107 |
+
"transformers_version": "5.3.0",
|
| 108 |
+
"visual_tokenizer_config": {
|
| 109 |
+
"_attn_implementation_autoset": true,
|
| 110 |
+
"_name_or_path": "",
|
| 111 |
+
"add_cross_attention": false,
|
| 112 |
+
"architectures": null,
|
| 113 |
+
"backbone_config": {
|
| 114 |
+
"_attn_implementation_autoset": true,
|
| 115 |
+
"_name_or_path": "apple/aimv2-huge-patch14-448",
|
| 116 |
+
"add_cross_attention": false,
|
| 117 |
+
"architectures": [
|
| 118 |
+
"AIMv2Model"
|
| 119 |
+
],
|
| 120 |
+
"attention_dropout": 0.0,
|
| 121 |
+
"auto_map": {
|
| 122 |
+
"AutoConfig": "configuration_aimv2.AIMv2Config",
|
| 123 |
+
"AutoModel": "modeling_aimv2.AIMv2Model",
|
| 124 |
+
"FlaxAutoModel": "modeling_flax_aimv2.FlaxAIMv2Model"
|
| 125 |
+
},
|
| 126 |
+
"bos_token_id": null,
|
| 127 |
+
"chunk_size_feed_forward": 0,
|
| 128 |
+
"cross_attention_hidden_size": null,
|
| 129 |
+
"decoder_start_token_id": null,
|
| 130 |
+
"dtype": "bfloat16",
|
| 131 |
+
"eos_token_id": null,
|
| 132 |
+
"finetuning_task": null,
|
| 133 |
+
"hidden_size": 1536,
|
| 134 |
+
"id2label": {
|
| 135 |
+
"0": "LABEL_0",
|
| 136 |
+
"1": "LABEL_1"
|
| 137 |
+
},
|
| 138 |
+
"image_size": 448,
|
| 139 |
+
"intermediate_size": 4096,
|
| 140 |
+
"is_decoder": false,
|
| 141 |
+
"is_encoder_decoder": false,
|
| 142 |
+
"label2id": {
|
| 143 |
+
"LABEL_0": 0,
|
| 144 |
+
"LABEL_1": 1
|
| 145 |
+
},
|
| 146 |
+
"model_type": "aimv2",
|
| 147 |
+
"num_attention_heads": 12,
|
| 148 |
+
"num_channels": 3,
|
| 149 |
+
"num_hidden_layers": 24,
|
| 150 |
+
"output_attentions": false,
|
| 151 |
+
"output_hidden_states": false,
|
| 152 |
+
"pad_token_id": null,
|
| 153 |
+
"patch_size": 14,
|
| 154 |
+
"prefix": null,
|
| 155 |
+
"problem_type": null,
|
| 156 |
+
"projection_dropout": 0.0,
|
| 157 |
+
"pruned_heads": {},
|
| 158 |
+
"qkv_bias": false,
|
| 159 |
+
"return_dict": true,
|
| 160 |
+
"rms_norm_eps": 1e-05,
|
| 161 |
+
"sep_token_id": null,
|
| 162 |
+
"task_specific_params": null,
|
| 163 |
+
"tf_legacy_loss": false,
|
| 164 |
+
"tie_encoder_decoder": false,
|
| 165 |
+
"tie_word_embeddings": true,
|
| 166 |
+
"tokenizer_class": null,
|
| 167 |
+
"torchscript": false,
|
| 168 |
+
"use_bfloat16": false,
|
| 169 |
+
"use_bias": false
|
| 170 |
+
},
|
| 171 |
+
"backbone_kwargs": {},
|
| 172 |
+
"bos_token_id": null,
|
| 173 |
+
"chunk_size_feed_forward": 0,
|
| 174 |
+
"cross_attention_hidden_size": null,
|
| 175 |
+
"decoder_start_token_id": null,
|
| 176 |
+
"depths": null,
|
| 177 |
+
"drop_cls_token": false,
|
| 178 |
+
"dtype": null,
|
| 179 |
+
"eos_token_id": null,
|
| 180 |
+
"finetuning_task": null,
|
| 181 |
+
"hidden_stride": 2,
|
| 182 |
+
"id2label": {
|
| 183 |
+
"0": "LABEL_0",
|
| 184 |
+
"1": "LABEL_1"
|
| 185 |
+
},
|
| 186 |
+
"is_decoder": false,
|
| 187 |
+
"is_encoder_decoder": false,
|
| 188 |
+
"label2id": {
|
| 189 |
+
"LABEL_0": 0,
|
| 190 |
+
"LABEL_1": 1
|
| 191 |
+
},
|
| 192 |
+
"model_type": "aimv2_visual_tokenizer",
|
| 193 |
+
"output_attentions": false,
|
| 194 |
+
"output_hidden_states": false,
|
| 195 |
+
"pad_token_id": null,
|
| 196 |
+
"prefix": null,
|
| 197 |
+
"problem_type": null,
|
| 198 |
+
"pruned_heads": {},
|
| 199 |
+
"return_dict": true,
|
| 200 |
+
"sep_token_id": null,
|
| 201 |
+
"task_specific_params": null,
|
| 202 |
+
"tau": 1.0,
|
| 203 |
+
"tf_legacy_loss": false,
|
| 204 |
+
"tie_encoder_decoder": false,
|
| 205 |
+
"tie_word_embeddings": true,
|
| 206 |
+
"tokenize_function": "softmax",
|
| 207 |
+
"tokenizer_class": null,
|
| 208 |
+
"torchscript": false,
|
| 209 |
+
"use_bfloat16": false,
|
| 210 |
+
"use_indicators": false,
|
| 211 |
+
"vocab_size": 65536
|
| 212 |
+
}
|
| 213 |
+
}
|
configuration_aimv2.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# copied from https://huggingface.co/apple/aimv2-huge-patch14-448
|
| 2 |
+
from typing import Any
|
| 3 |
+
|
| 4 |
+
from transformers.configuration_utils import PretrainedConfig
|
| 5 |
+
|
| 6 |
+
__all__ = ["AIMv2Config"]
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class AIMv2Config(PretrainedConfig):
|
| 10 |
+
"""This is the configuration class to store the configuration of an [`AIMv2Model`].
|
| 11 |
+
|
| 12 |
+
Instantiating a configuration with the defaults will yield a similar configuration
|
| 13 |
+
to that of the [apple/aimv2-large-patch14-224](https://huggingface.co/apple/aimv2-large-patch14-224).
|
| 14 |
+
|
| 15 |
+
Args:
|
| 16 |
+
hidden_size: Dimension of the hidden representations.
|
| 17 |
+
intermediate_size: Dimension of the SwiGLU representations.
|
| 18 |
+
num_hidden_layers: Number of hidden layers in the Transformer.
|
| 19 |
+
num_attention_heads: Number of attention heads for each attention layer
|
| 20 |
+
in the Transformer.
|
| 21 |
+
num_channels: Number of input channels.
|
| 22 |
+
image_size: Image size.
|
| 23 |
+
patch_size: Patch size.
|
| 24 |
+
rms_norm_eps: Epsilon value used for the RMS normalization layer.
|
| 25 |
+
attention_dropout: Dropout ratio for attention probabilities.
|
| 26 |
+
projection_dropout: Dropout ratio for the projection layer after the attention.
|
| 27 |
+
qkv_bias: Whether to add a bias to the queries, keys and values.
|
| 28 |
+
use_bias: Whether to add a bias in the feed-forward and projection layers.
|
| 29 |
+
kwargs: Keyword arguments for the [`PretrainedConfig`].
|
| 30 |
+
"""
|
| 31 |
+
|
| 32 |
+
model_type: str = "aimv2"
|
| 33 |
+
|
| 34 |
+
def __init__(
|
| 35 |
+
self,
|
| 36 |
+
hidden_size: int = 1024,
|
| 37 |
+
intermediate_size: int = 2816,
|
| 38 |
+
num_hidden_layers: int = 24,
|
| 39 |
+
num_attention_heads: int = 8,
|
| 40 |
+
num_channels: int = 3,
|
| 41 |
+
image_size: int = 224,
|
| 42 |
+
patch_size: int = 14,
|
| 43 |
+
rms_norm_eps: float = 1e-5,
|
| 44 |
+
attention_dropout: float = 0.0,
|
| 45 |
+
projection_dropout: float = 0.0,
|
| 46 |
+
qkv_bias: bool = False,
|
| 47 |
+
use_bias: bool = False,
|
| 48 |
+
**kwargs: Any,
|
| 49 |
+
):
|
| 50 |
+
super().__init__(**kwargs)
|
| 51 |
+
self.hidden_size = hidden_size
|
| 52 |
+
self.intermediate_size = intermediate_size
|
| 53 |
+
self.num_hidden_layers = num_hidden_layers
|
| 54 |
+
self.num_attention_heads = num_attention_heads
|
| 55 |
+
self.num_channels = num_channels
|
| 56 |
+
self.patch_size = patch_size
|
| 57 |
+
self.image_size = image_size
|
| 58 |
+
self.attention_dropout = attention_dropout
|
| 59 |
+
self.rms_norm_eps = rms_norm_eps
|
| 60 |
+
|
| 61 |
+
self.projection_dropout = projection_dropout
|
| 62 |
+
self.qkv_bias = qkv_bias
|
| 63 |
+
self.use_bias = use_bias
|
configuration_ovis.py
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from abc import ABC, abstractmethod
|
| 2 |
+
from typing import List, Dict, Union, Optional
|
| 3 |
+
|
| 4 |
+
from transformers import PretrainedConfig, AutoConfig, AutoModel
|
| 5 |
+
from .configuration_aimv2 import AIMv2Config
|
| 6 |
+
from .modeling_aimv2 import AIMv2Model
|
| 7 |
+
|
| 8 |
+
IGNORE_ID = -100
|
| 9 |
+
IMAGE_TOKEN_ID = -200
|
| 10 |
+
IMAGE_TOKEN = "<image>"
|
| 11 |
+
IMAGE_ATOM_ID = -300
|
| 12 |
+
IMAGE_INDICATOR_IDS = [-301, -302, -303, -304, -305]
|
| 13 |
+
|
| 14 |
+
AutoConfig.register("aimv2", AIMv2Config, exist_ok=True)
|
| 15 |
+
AutoModel.register(AIMv2Config, AIMv2Model)
|
| 16 |
+
|
| 17 |
+
# ----------------------------------------------------------------------
|
| 18 |
+
# Visual Tokenizer Configuration
|
| 19 |
+
# ----------------------------------------------------------------------
|
| 20 |
+
class BaseVisualTokenizerConfig(PretrainedConfig):
|
| 21 |
+
def __init__(
|
| 22 |
+
self,
|
| 23 |
+
vocab_size=16384,
|
| 24 |
+
tokenize_function="softmax",
|
| 25 |
+
tau=1.0,
|
| 26 |
+
depths=None,
|
| 27 |
+
drop_cls_token=False,
|
| 28 |
+
backbone_config: Optional[Union[PretrainedConfig, dict]] = None,
|
| 29 |
+
hidden_stride: int = 1,
|
| 30 |
+
**kwargs
|
| 31 |
+
):
|
| 32 |
+
super().__init__(**kwargs)
|
| 33 |
+
self.vocab_size = vocab_size
|
| 34 |
+
self.tokenize_function = tokenize_function
|
| 35 |
+
self.tau = tau
|
| 36 |
+
if isinstance(depths, str):
|
| 37 |
+
depths = [int(x) for x in depths.split('|')]
|
| 38 |
+
self.depths = depths
|
| 39 |
+
self.backbone_kwargs = {}
|
| 40 |
+
self.drop_cls_token = drop_cls_token
|
| 41 |
+
if backbone_config is not None:
|
| 42 |
+
assert isinstance(backbone_config, (PretrainedConfig, dict)), \
|
| 43 |
+
f"expect `backbone_config` to be instance of PretrainedConfig or dict, but got {type(backbone_config)} type"
|
| 44 |
+
if not isinstance(backbone_config, PretrainedConfig):
|
| 45 |
+
model_type = backbone_config['model_type']
|
| 46 |
+
backbone_config.pop('model_type')
|
| 47 |
+
backbone_config = AutoConfig.for_model(model_type, **backbone_config)
|
| 48 |
+
self.backbone_config = backbone_config
|
| 49 |
+
self.hidden_stride = hidden_stride
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
class Aimv2VisualTokenizerConfig(BaseVisualTokenizerConfig):
|
| 53 |
+
model_type = "aimv2_visual_tokenizer"
|
| 54 |
+
|
| 55 |
+
def __init__(self, **kwargs):
|
| 56 |
+
super().__init__(**kwargs)
|
| 57 |
+
if self.drop_cls_token:
|
| 58 |
+
self.drop_cls_token = False
|
| 59 |
+
if self.depths:
|
| 60 |
+
assert len(self.depths) == 1
|
| 61 |
+
self.backbone_kwargs['num_hidden_layers'] = self.depths[0]
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
AutoConfig.register("aimv2_visual_tokenizer", Aimv2VisualTokenizerConfig, exist_ok=True)
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
# ----------------------------------------------------------------------
|
| 68 |
+
# Ovis Configuration
|
| 69 |
+
# ----------------------------------------------------------------------
|
| 70 |
+
class OvisConfig(PretrainedConfig):
|
| 71 |
+
model_type = "ovis"
|
| 72 |
+
|
| 73 |
+
def __init__(
|
| 74 |
+
self,
|
| 75 |
+
llm_config: Optional[Union[PretrainedConfig, dict]] = None,
|
| 76 |
+
visual_tokenizer_config: Optional[Union[PretrainedConfig, dict]] = None,
|
| 77 |
+
multimodal_max_length=8192,
|
| 78 |
+
hidden_size=None,
|
| 79 |
+
conversation_formatter_class=None,
|
| 80 |
+
llm_attn_implementation=None,
|
| 81 |
+
disable_tie_weight=False,
|
| 82 |
+
**kwargs
|
| 83 |
+
):
|
| 84 |
+
super().__init__(**kwargs)
|
| 85 |
+
if llm_config is not None:
|
| 86 |
+
assert isinstance(llm_config, (PretrainedConfig, dict)), \
|
| 87 |
+
f"expect `llm_config` to be instance of PretrainedConfig or dict, but got {type(llm_config)} type"
|
| 88 |
+
if not isinstance(llm_config, PretrainedConfig):
|
| 89 |
+
model_type = llm_config['model_type']
|
| 90 |
+
llm_config.pop('model_type')
|
| 91 |
+
llm_config = AutoConfig.for_model(model_type, **llm_config)
|
| 92 |
+
self.llm_config = llm_config
|
| 93 |
+
if visual_tokenizer_config is not None:
|
| 94 |
+
assert isinstance(visual_tokenizer_config, (PretrainedConfig, dict)), \
|
| 95 |
+
f"expect `visual_tokenizer_config` to be instance of PretrainedConfig or dict, but got {type(visual_tokenizer_config)} type"
|
| 96 |
+
if not isinstance(visual_tokenizer_config, PretrainedConfig):
|
| 97 |
+
model_type = visual_tokenizer_config['model_type']
|
| 98 |
+
visual_tokenizer_config.pop('model_type')
|
| 99 |
+
visual_tokenizer_config = AutoConfig.for_model(model_type, **visual_tokenizer_config)
|
| 100 |
+
self.visual_tokenizer_config = visual_tokenizer_config
|
| 101 |
+
self.multimodal_max_length = multimodal_max_length
|
| 102 |
+
self.hidden_size = hidden_size
|
| 103 |
+
self.conversation_formatter_class = conversation_formatter_class
|
| 104 |
+
self.llm_attn_implementation = llm_attn_implementation
|
| 105 |
+
self.disable_tie_weight = disable_tie_weight
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
# ----------------------------------------------------------------------
|
| 109 |
+
# Conversation Formatter
|
| 110 |
+
# ----------------------------------------------------------------------
|
| 111 |
+
class ConversationFormatter(ABC):
|
| 112 |
+
support_tokenizer_types = None
|
| 113 |
+
|
| 114 |
+
def __init__(self, tokenizer):
|
| 115 |
+
tokenizer_type = type(tokenizer).__name__
|
| 116 |
+
assert tokenizer_type in self.support_tokenizer_types, \
|
| 117 |
+
f'Invalid tokenizer type, expected one from `{self.support_tokenizer_types}`, but got `{tokenizer_type}`'
|
| 118 |
+
self.tokenizer = tokenizer
|
| 119 |
+
self.image_token = IMAGE_TOKEN
|
| 120 |
+
self.image_token_id = IMAGE_TOKEN_ID
|
| 121 |
+
self.ignore_id = IGNORE_ID
|
| 122 |
+
|
| 123 |
+
def _tokenize_with_image_symbol(self, text):
|
| 124 |
+
text_chunks = [self.tokenizer(chunk, add_special_tokens=False).input_ids for chunk in
|
| 125 |
+
text.split(self.image_token)]
|
| 126 |
+
token_ids = []
|
| 127 |
+
num_chuck = len(text_chunks)
|
| 128 |
+
for i, chunk in enumerate(text_chunks):
|
| 129 |
+
token_ids.extend(chunk)
|
| 130 |
+
if i < num_chuck - 1:
|
| 131 |
+
token_ids.append(self.image_token_id)
|
| 132 |
+
return token_ids
|
| 133 |
+
|
| 134 |
+
@abstractmethod
|
| 135 |
+
def format(self, conversations: List[Dict], generation_preface=None):
|
| 136 |
+
pass
|
| 137 |
+
|
| 138 |
+
@abstractmethod
|
| 139 |
+
def format_query(self, query, generation_preface=""):
|
| 140 |
+
pass
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
class QwenConversationFormatter(ConversationFormatter):
|
| 144 |
+
support_tokenizer_types = ['QWenTokenizer', 'Qwen2TokenizerFast', 'Qwen2Tokenizer']
|
| 145 |
+
|
| 146 |
+
def __init__(self, tokenizer):
|
| 147 |
+
super().__init__(tokenizer)
|
| 148 |
+
self.from2role = {
|
| 149 |
+
"system": "<|im_start|>system\n",
|
| 150 |
+
"human": "<|im_start|>user\n",
|
| 151 |
+
"gpt": "<|im_start|>assistant\n",
|
| 152 |
+
}
|
| 153 |
+
self.gpt_token_num = None
|
| 154 |
+
self.im_end = "<|im_end|>\n"
|
| 155 |
+
self.default_system_prompt = "You are a helpful assistant."
|
| 156 |
+
|
| 157 |
+
def format(self, conversations: List[Dict], generation_preface=None):
|
| 158 |
+
if self.gpt_token_num is None:
|
| 159 |
+
self.gpt_token_num = len(self.tokenizer(self.from2role["gpt"], add_special_tokens=False).input_ids)
|
| 160 |
+
|
| 161 |
+
if conversations[0]["from"] != "system":
|
| 162 |
+
conversations.insert(0, {
|
| 163 |
+
"from": "system",
|
| 164 |
+
"value": self.default_system_prompt
|
| 165 |
+
})
|
| 166 |
+
|
| 167 |
+
if generation_preface is not None:
|
| 168 |
+
conversations.append({
|
| 169 |
+
"from": "gpt",
|
| 170 |
+
"value": generation_preface
|
| 171 |
+
})
|
| 172 |
+
|
| 173 |
+
prompt = ""
|
| 174 |
+
input_ids = []
|
| 175 |
+
labels = []
|
| 176 |
+
num_conversation = len(conversations)
|
| 177 |
+
for i, conversation in enumerate(conversations):
|
| 178 |
+
frm = conversation["from"]
|
| 179 |
+
role = self.from2role[frm]
|
| 180 |
+
message = conversation["value"]
|
| 181 |
+
text = role + message
|
| 182 |
+
if i < num_conversation - 1 or generation_preface is None:
|
| 183 |
+
text += self.im_end
|
| 184 |
+
prompt += text
|
| 185 |
+
token_ids = self._tokenize_with_image_symbol(text)
|
| 186 |
+
input_ids.extend(token_ids)
|
| 187 |
+
label_ids = [self.ignore_id] * len(token_ids)
|
| 188 |
+
if frm == "gpt" and generation_preface is None:
|
| 189 |
+
# learning `\n` following `im_end` is meaningless, so the last `\n` token is ignored in label
|
| 190 |
+
label_ids[self.gpt_token_num:-1] = token_ids[self.gpt_token_num:-1]
|
| 191 |
+
labels.extend(label_ids)
|
| 192 |
+
|
| 193 |
+
assert self._tokenize_with_image_symbol(prompt) == input_ids
|
| 194 |
+
assert len(input_ids) == len(labels)
|
| 195 |
+
|
| 196 |
+
return prompt, input_ids, labels
|
| 197 |
+
|
| 198 |
+
def format_query(self, query, generation_preface=""):
|
| 199 |
+
prompt, input_ids, _ = self.format([{
|
| 200 |
+
"from": "human",
|
| 201 |
+
"value": query
|
| 202 |
+
}], generation_preface=generation_preface)
|
| 203 |
+
|
| 204 |
+
return prompt, input_ids
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c2f5b587fdc9c375c91766ff53633dd2376db1c80d1cb31c0ba91e7e0df74cc3
|
| 3 |
+
size 14141693348
|
modeling_aimv2.py
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# adapted from https://huggingface.co/apple/aimv2-huge-patch14-448 (modification: add gradient checkpoint support)
|
| 2 |
+
from typing import Optional, Tuple, Union
|
| 3 |
+
|
| 4 |
+
import torch
|
| 5 |
+
from .configuration_aimv2 import AIMv2Config
|
| 6 |
+
from torch import nn
|
| 7 |
+
from torch.nn import functional as F
|
| 8 |
+
from transformers.modeling_outputs import BaseModelOutputWithNoAttention
|
| 9 |
+
from transformers.modeling_utils import PreTrainedModel
|
| 10 |
+
|
| 11 |
+
__all__ = ["AIMv2Model"]
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
class RMSNorm(nn.Module):
|
| 15 |
+
def __init__(self, dim: int, eps: float = 1e-6):
|
| 16 |
+
super().__init__()
|
| 17 |
+
self.weight = nn.Parameter(torch.ones(dim))
|
| 18 |
+
self.eps = eps
|
| 19 |
+
|
| 20 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 21 |
+
output = self._norm(x.float()).type_as(x)
|
| 22 |
+
return output * self.weight
|
| 23 |
+
|
| 24 |
+
def extra_repr(self) -> str:
|
| 25 |
+
return f"{tuple(self.weight.shape)}, eps={self.eps}"
|
| 26 |
+
|
| 27 |
+
def _norm(self, x: torch.Tensor) -> torch.Tensor:
|
| 28 |
+
return x * torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + self.eps)
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
class AIMv2SwiGLUFFN(nn.Module):
|
| 32 |
+
def __init__(self, config: AIMv2Config):
|
| 33 |
+
super().__init__()
|
| 34 |
+
hidden_features = config.intermediate_size
|
| 35 |
+
in_features = config.hidden_size
|
| 36 |
+
bias = config.use_bias
|
| 37 |
+
|
| 38 |
+
self.fc1 = nn.Linear(in_features, hidden_features, bias=bias)
|
| 39 |
+
self.fc2 = nn.Linear(hidden_features, in_features, bias=bias)
|
| 40 |
+
self.fc3 = nn.Linear(in_features, hidden_features, bias=bias)
|
| 41 |
+
|
| 42 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 43 |
+
x = F.silu(self.fc1(x)) * self.fc3(x)
|
| 44 |
+
x = self.fc2(x)
|
| 45 |
+
return x
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
class AIMv2PatchEmbed(nn.Module):
|
| 49 |
+
def __init__(self, config: AIMv2Config):
|
| 50 |
+
super().__init__()
|
| 51 |
+
self.proj = nn.Conv2d(
|
| 52 |
+
config.num_channels,
|
| 53 |
+
config.hidden_size,
|
| 54 |
+
kernel_size=(config.patch_size, config.patch_size),
|
| 55 |
+
stride=(config.patch_size, config.patch_size),
|
| 56 |
+
)
|
| 57 |
+
self.norm = RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
| 58 |
+
|
| 59 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 60 |
+
x = self.proj(x).flatten(2).transpose(1, 2)
|
| 61 |
+
x = self.norm(x)
|
| 62 |
+
return x
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
class AIMv2ViTPreprocessor(nn.Module):
|
| 66 |
+
def __init__(self, config: AIMv2Config):
|
| 67 |
+
super().__init__()
|
| 68 |
+
num_patches = (config.image_size // config.patch_size) ** 2
|
| 69 |
+
|
| 70 |
+
self.patchifier = AIMv2PatchEmbed(config)
|
| 71 |
+
self.pos_embed = nn.Parameter(torch.zeros((1, num_patches, config.hidden_size)))
|
| 72 |
+
|
| 73 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 74 |
+
tokens = self.patchifier(x)
|
| 75 |
+
_, N, _ = tokens.shape
|
| 76 |
+
pos_embed = self.pos_embed.to(tokens.device)
|
| 77 |
+
tokens = tokens + pos_embed[:, :N]
|
| 78 |
+
return tokens
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
class AIMv2Attention(nn.Module):
|
| 82 |
+
def __init__(self, config: AIMv2Config):
|
| 83 |
+
super().__init__()
|
| 84 |
+
dim = config.hidden_size
|
| 85 |
+
|
| 86 |
+
self.num_heads = config.num_attention_heads
|
| 87 |
+
self.qkv = nn.Linear(dim, dim * 3, bias=config.qkv_bias)
|
| 88 |
+
self.attn_drop = nn.Dropout(config.attention_dropout)
|
| 89 |
+
self.proj = nn.Linear(dim, dim, bias=config.use_bias)
|
| 90 |
+
self.proj_drop = nn.Dropout(config.projection_dropout)
|
| 91 |
+
|
| 92 |
+
def forward(
|
| 93 |
+
self, x: torch.Tensor, mask: Optional[torch.Tensor] = None
|
| 94 |
+
) -> torch.Tensor:
|
| 95 |
+
B, N, C = x.shape
|
| 96 |
+
qkv = (
|
| 97 |
+
self.qkv(x)
|
| 98 |
+
.reshape(B, N, 3, self.num_heads, C // self.num_heads)
|
| 99 |
+
.permute(2, 0, 3, 1, 4)
|
| 100 |
+
)
|
| 101 |
+
q, k, v = qkv.unbind(0)
|
| 102 |
+
|
| 103 |
+
x = F.scaled_dot_product_attention(q, k, v, attn_mask=mask)
|
| 104 |
+
x = x.transpose(1, 2).contiguous().reshape(B, N, C)
|
| 105 |
+
x = self.proj(x)
|
| 106 |
+
x = self.proj_drop(x)
|
| 107 |
+
return x
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
class AIMv2Block(nn.Module):
|
| 111 |
+
def __init__(self, config: AIMv2Config):
|
| 112 |
+
super().__init__()
|
| 113 |
+
self.attn = AIMv2Attention(config)
|
| 114 |
+
self.norm_1 = RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
| 115 |
+
self.mlp = AIMv2SwiGLUFFN(config)
|
| 116 |
+
self.norm_2 = RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
| 117 |
+
|
| 118 |
+
def forward(
|
| 119 |
+
self, x: torch.Tensor, mask: Optional[torch.Tensor] = None
|
| 120 |
+
) -> torch.Tensor:
|
| 121 |
+
x = x + self.attn(self.norm_1(x), mask)
|
| 122 |
+
x = x + self.mlp(self.norm_2(x))
|
| 123 |
+
return x
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
class AIMv2Transformer(nn.Module):
|
| 127 |
+
def __init__(self, config: AIMv2Config):
|
| 128 |
+
super().__init__()
|
| 129 |
+
self.blocks = nn.ModuleList(
|
| 130 |
+
[AIMv2Block(config) for _ in range(config.num_hidden_layers)]
|
| 131 |
+
)
|
| 132 |
+
self.post_trunk_norm = RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
| 133 |
+
self.gradient_checkpointing = False
|
| 134 |
+
|
| 135 |
+
def forward(
|
| 136 |
+
self,
|
| 137 |
+
tokens: torch.Tensor,
|
| 138 |
+
mask: Optional[torch.Tensor] = None,
|
| 139 |
+
output_hidden_states: bool = False,
|
| 140 |
+
) -> Tuple[torch.Tensor, Optional[Tuple[torch.Tensor, ...]]]:
|
| 141 |
+
hidden_states = () if output_hidden_states else None
|
| 142 |
+
for block in self.blocks:
|
| 143 |
+
if self.gradient_checkpointing and self.training:
|
| 144 |
+
tokens = self._gradient_checkpointing_func(block.__call__, tokens, mask)
|
| 145 |
+
else:
|
| 146 |
+
tokens = block(tokens, mask)
|
| 147 |
+
if output_hidden_states:
|
| 148 |
+
hidden_states += (tokens,)
|
| 149 |
+
tokens = self.post_trunk_norm(tokens)
|
| 150 |
+
return tokens, hidden_states
|
| 151 |
+
|
| 152 |
+
|
| 153 |
+
class AIMv2PretrainedModel(PreTrainedModel):
|
| 154 |
+
config_class = AIMv2Config
|
| 155 |
+
base_model_prefix = "aimv2"
|
| 156 |
+
supports_gradient_checkpointing = True
|
| 157 |
+
main_input_name = "pixel_values"
|
| 158 |
+
_no_split_modules = ["AIMv2ViTPreprocessor", "AIMv2Block"]
|
| 159 |
+
_supports_sdpa = True
|
| 160 |
+
|
| 161 |
+
|
| 162 |
+
class AIMv2Model(AIMv2PretrainedModel):
|
| 163 |
+
def __init__(self, config: AIMv2Config):
|
| 164 |
+
super().__init__(config)
|
| 165 |
+
self.preprocessor = AIMv2ViTPreprocessor(config)
|
| 166 |
+
self.trunk = AIMv2Transformer(config)
|
| 167 |
+
|
| 168 |
+
def forward(
|
| 169 |
+
self,
|
| 170 |
+
pixel_values: torch.Tensor,
|
| 171 |
+
mask: Optional[torch.Tensor] = None,
|
| 172 |
+
output_hidden_states: Optional[bool] = None,
|
| 173 |
+
return_dict: Optional[bool] = None,
|
| 174 |
+
) -> Union[
|
| 175 |
+
Tuple[torch.Tensor],
|
| 176 |
+
Tuple[torch.Tensor, Tuple[torch.Tensor, ...]],
|
| 177 |
+
BaseModelOutputWithNoAttention,
|
| 178 |
+
]:
|
| 179 |
+
if output_hidden_states is None:
|
| 180 |
+
output_hidden_states = self.config.output_hidden_states
|
| 181 |
+
if return_dict is None:
|
| 182 |
+
return_dict = self.config.use_return_dict
|
| 183 |
+
|
| 184 |
+
x = self.preprocessor(pixel_values)
|
| 185 |
+
x, hidden_states = self.trunk(
|
| 186 |
+
x, mask, output_hidden_states=output_hidden_states
|
| 187 |
+
)
|
| 188 |
+
|
| 189 |
+
if not return_dict:
|
| 190 |
+
res = (x,)
|
| 191 |
+
res += (hidden_states,) if output_hidden_states else ()
|
| 192 |
+
return res
|
| 193 |
+
|
| 194 |
+
return BaseModelOutputWithNoAttention(
|
| 195 |
+
last_hidden_state=x,
|
| 196 |
+
hidden_states=hidden_states,
|
| 197 |
+
)
|
| 198 |
+
|
pruning_info.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"base_model": "AIDC-AI/Ovis2-8B",
|
| 3 |
+
"method": "layer_pruning_ppl",
|
| 4 |
+
"method_name": "PPL-based (Shortened LLaMA)",
|
| 5 |
+
"layers_removed": [
|
| 6 |
+
4,
|
| 7 |
+
5,
|
| 8 |
+
7,
|
| 9 |
+
8,
|
| 10 |
+
10,
|
| 11 |
+
11,
|
| 12 |
+
13,
|
| 13 |
+
15
|
| 14 |
+
],
|
| 15 |
+
"n_layers_original": 28,
|
| 16 |
+
"n_layers_remaining": 20,
|
| 17 |
+
"params_before_M": 8935.3,
|
| 18 |
+
"params_after_M": 7070.8,
|
| 19 |
+
"param_reduction_pct": 20.9,
|
| 20 |
+
"benchmarks": {
|
| 21 |
+
"vqav2": {
|
| 22 |
+
"accuracy": 0.0,
|
| 23 |
+
"avg_latency_s": 0.5913,
|
| 24 |
+
"peak_memory_mb": 18675.1,
|
| 25 |
+
"avg_memory_mb": 18638.7,
|
| 26 |
+
"throughput_sps": 1.69,
|
| 27 |
+
"avg_power_w": 289.8,
|
| 28 |
+
"avg_gpu_util_pct": 94.2,
|
| 29 |
+
"n_samples": 50,
|
| 30 |
+
"n_evaluated": 50,
|
| 31 |
+
"n_skipped": 0,
|
| 32 |
+
"all_failed": false,
|
| 33 |
+
"zero_accuracy_warning": true,
|
| 34 |
+
"metrics": {
|
| 35 |
+
"exact_match": 0.0,
|
| 36 |
+
"contains": 0.26,
|
| 37 |
+
"token_f1": 0.0731,
|
| 38 |
+
"bleu": 0.0457,
|
| 39 |
+
"rouge_l": 0.0731
|
| 40 |
+
}
|
| 41 |
+
}
|
| 42 |
+
}
|
| 43 |
+
}
|
tokenizer.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3fd169731d2cbde95e10bf356d66d5997fd885dd8dbb6fb4684da3f23b2585d8
|
| 3 |
+
size 11421892
|
tokenizer_config.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"add_prefix_space": false,
|
| 3 |
+
"backend": "tokenizers",
|
| 4 |
+
"bos_token": null,
|
| 5 |
+
"clean_up_tokenization_spaces": false,
|
| 6 |
+
"eos_token": "<|im_end|>",
|
| 7 |
+
"errors": "replace",
|
| 8 |
+
"extra_special_tokens": [
|
| 9 |
+
"<|im_start|>",
|
| 10 |
+
"<|im_end|>",
|
| 11 |
+
"<|object_ref_start|>",
|
| 12 |
+
"<|object_ref_end|>",
|
| 13 |
+
"<|box_start|>",
|
| 14 |
+
"<|box_end|>",
|
| 15 |
+
"<|quad_start|>",
|
| 16 |
+
"<|quad_end|>",
|
| 17 |
+
"<|vision_start|>",
|
| 18 |
+
"<|vision_end|>",
|
| 19 |
+
"<|vision_pad|>",
|
| 20 |
+
"<|image_pad|>",
|
| 21 |
+
"<|video_pad|>"
|
| 22 |
+
],
|
| 23 |
+
"is_local": false,
|
| 24 |
+
"model_max_length": 131072,
|
| 25 |
+
"pad_token": "<|endoftext|>",
|
| 26 |
+
"split_special_tokens": false,
|
| 27 |
+
"tokenizer_class": "Qwen2Tokenizer",
|
| 28 |
+
"unk_token": null
|
| 29 |
+
}
|