Upload GPTQ-INT4 quantized model
Browse files- README.md +41 -0
- config.json +50 -0
- configuration_ovis.py +3 -3
- generation_config.json +14 -0
- model-00001-of-00002.safetensors +3 -0
- model-00002-of-00002.safetensors +3 -0
- model.safetensors.index.json +0 -0
- modeling_ovis.py +590 -0
- preprocessor_config.json +28 -0
- quant_log.csv +253 -0
- quant_meta.json +9 -0
- quantize_config.json +42 -0
README.md
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: apache-2.0
|
| 3 |
+
base_model: AIDC-AI/Ovis2-4B
|
| 4 |
+
tags:
|
| 5 |
+
- quantized
|
| 6 |
+
- gptq
|
| 7 |
+
- vision-language-model
|
| 8 |
+
- vlm
|
| 9 |
+
---
|
| 10 |
+
|
| 11 |
+
# AIDC-AI__Ovis2-4B__gptq_int4
|
| 12 |
+
|
| 13 |
+
This is a **GPTQ** (4-bit) quantized version of [AIDC-AI/Ovis2-4B](https://huggingface.co/AIDC-AI/Ovis2-4B).
|
| 14 |
+
|
| 15 |
+
## Quantization Details
|
| 16 |
+
|
| 17 |
+
- **Method**: GPTQ
|
| 18 |
+
- **Bits**: 4
|
| 19 |
+
- **Base model**: AIDC-AI/Ovis2-4B
|
| 20 |
+
- **Group size**: 128
|
| 21 |
+
|
| 22 |
+
## Usage
|
| 23 |
+
|
| 24 |
+
```python
|
| 25 |
+
from transformers import AutoProcessor, AutoModelForImageTextToText
|
| 26 |
+
import torch
|
| 27 |
+
|
| 28 |
+
model = AutoModelForImageTextToText.from_pretrained(
|
| 29 |
+
"{REPO_ID}",
|
| 30 |
+
torch_dtype=torch.float16,
|
| 31 |
+
device_map="auto",
|
| 32 |
+
trust_remote_code=True,
|
| 33 |
+
)
|
| 34 |
+
processor = AutoProcessor.from_pretrained("{REPO_ID}", trust_remote_code=True)
|
| 35 |
+
```
|
| 36 |
+
|
| 37 |
+
Replace `{REPO_ID}` with the repo ID of this model.
|
| 38 |
+
|
| 39 |
+
## Original Model
|
| 40 |
+
|
| 41 |
+
See [AIDC-AI/Ovis2-4B](https://huggingface.co/AIDC-AI/Ovis2-4B) for the original FP16 model.
|
config.json
CHANGED
|
@@ -9,6 +9,8 @@
|
|
| 9 |
"conversation_formatter_class": "QwenConversationFormatter",
|
| 10 |
"disable_tie_weight": false,
|
| 11 |
"dtype": "bfloat16",
|
|
|
|
|
|
|
| 12 |
"hidden_size": 2048,
|
| 13 |
"llm_attn_implementation": "flash_attention_2",
|
| 14 |
"llm_config": {
|
|
@@ -111,7 +113,55 @@
|
|
| 111 |
},
|
| 112 |
"model_type": "ovis",
|
| 113 |
"multimodal_max_length": 32768,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
"transformers_version": "5.3.0",
|
|
|
|
| 115 |
"visual_tokenizer_config": {
|
| 116 |
"_attn_implementation_autoset": true,
|
| 117 |
"_name_or_path": "",
|
|
|
|
| 9 |
"conversation_formatter_class": "QwenConversationFormatter",
|
| 10 |
"disable_tie_weight": false,
|
| 11 |
"dtype": "bfloat16",
|
| 12 |
+
"eos_token": "<|im_end|>",
|
| 13 |
+
"eos_token_id": 151645,
|
| 14 |
"hidden_size": 2048,
|
| 15 |
"llm_attn_implementation": "flash_attention_2",
|
| 16 |
"llm_config": {
|
|
|
|
| 113 |
},
|
| 114 |
"model_type": "ovis",
|
| 115 |
"multimodal_max_length": 32768,
|
| 116 |
+
"pad_token_id": 151643,
|
| 117 |
+
"quantization_config": {
|
| 118 |
+
"bits": 4,
|
| 119 |
+
"checkpoint_format": "gptq",
|
| 120 |
+
"desc_act": false,
|
| 121 |
+
"format": "gptq",
|
| 122 |
+
"group_size": 128,
|
| 123 |
+
"lm_head": false,
|
| 124 |
+
"meta": {
|
| 125 |
+
"act_group_aware": true,
|
| 126 |
+
"auto_forward_data_parallel": true,
|
| 127 |
+
"damp_auto_increment": 0.01,
|
| 128 |
+
"damp_percent": 0.05,
|
| 129 |
+
"failsafe": {
|
| 130 |
+
"smooth": null,
|
| 131 |
+
"strategy": "rtn",
|
| 132 |
+
"threshold": "0.5%"
|
| 133 |
+
},
|
| 134 |
+
"gc_mode": "interval",
|
| 135 |
+
"gptaq": null,
|
| 136 |
+
"hessian": {
|
| 137 |
+
"chunk_bytes": null,
|
| 138 |
+
"chunk_size": null,
|
| 139 |
+
"staging_dtype": "float32"
|
| 140 |
+
},
|
| 141 |
+
"mock_quantization": false,
|
| 142 |
+
"mse": 0.0,
|
| 143 |
+
"offload_to_disk": true,
|
| 144 |
+
"offload_to_disk_path": "./gptqmodel_offload/lzyvwgbq-jskkldgv/",
|
| 145 |
+
"pack_impl": "cpu",
|
| 146 |
+
"quantizer": [
|
| 147 |
+
"gptqmodel:5.8.0"
|
| 148 |
+
],
|
| 149 |
+
"static_groups": false,
|
| 150 |
+
"true_sequential": true,
|
| 151 |
+
"uri": "https://github.com/modelcloud/gptqmodel",
|
| 152 |
+
"vram_strategy": "exclusive",
|
| 153 |
+
"wait_for_submodule_finalizers": false
|
| 154 |
+
},
|
| 155 |
+
"pack_dtype": "int32",
|
| 156 |
+
"quant_method": "gptq",
|
| 157 |
+
"sym": true
|
| 158 |
+
},
|
| 159 |
+
"rope_parameters": {
|
| 160 |
+
"rope_theta": 10000.0,
|
| 161 |
+
"rope_type": "default"
|
| 162 |
+
},
|
| 163 |
"transformers_version": "5.3.0",
|
| 164 |
+
"use_cache": false,
|
| 165 |
"visual_tokenizer_config": {
|
| 166 |
"_attn_implementation_autoset": true,
|
| 167 |
"_name_or_path": "",
|
configuration_ovis.py
CHANGED
|
@@ -11,7 +11,7 @@ IMAGE_TOKEN = "<image>"
|
|
| 11 |
IMAGE_ATOM_ID = -300
|
| 12 |
IMAGE_INDICATOR_IDS = [-301, -302, -303, -304, -305]
|
| 13 |
|
| 14 |
-
AutoConfig.register("aimv2", AIMv2Config
|
| 15 |
AutoModel.register(AIMv2Config, AIMv2Model)
|
| 16 |
|
| 17 |
# ----------------------------------------------------------------------
|
|
@@ -61,7 +61,7 @@ class Aimv2VisualTokenizerConfig(BaseVisualTokenizerConfig):
|
|
| 61 |
self.backbone_kwargs['num_hidden_layers'] = self.depths[0]
|
| 62 |
|
| 63 |
|
| 64 |
-
AutoConfig.register("aimv2_visual_tokenizer", Aimv2VisualTokenizerConfig
|
| 65 |
|
| 66 |
|
| 67 |
# ----------------------------------------------------------------------
|
|
@@ -141,7 +141,7 @@ class ConversationFormatter(ABC):
|
|
| 141 |
|
| 142 |
|
| 143 |
class QwenConversationFormatter(ConversationFormatter):
|
| 144 |
-
support_tokenizer_types = ['QWenTokenizer', 'Qwen2TokenizerFast'
|
| 145 |
|
| 146 |
def __init__(self, tokenizer):
|
| 147 |
super().__init__(tokenizer)
|
|
|
|
| 11 |
IMAGE_ATOM_ID = -300
|
| 12 |
IMAGE_INDICATOR_IDS = [-301, -302, -303, -304, -305]
|
| 13 |
|
| 14 |
+
AutoConfig.register("aimv2", AIMv2Config)
|
| 15 |
AutoModel.register(AIMv2Config, AIMv2Model)
|
| 16 |
|
| 17 |
# ----------------------------------------------------------------------
|
|
|
|
| 61 |
self.backbone_kwargs['num_hidden_layers'] = self.depths[0]
|
| 62 |
|
| 63 |
|
| 64 |
+
AutoConfig.register("aimv2_visual_tokenizer", Aimv2VisualTokenizerConfig)
|
| 65 |
|
| 66 |
|
| 67 |
# ----------------------------------------------------------------------
|
|
|
|
| 141 |
|
| 142 |
|
| 143 |
class QwenConversationFormatter(ConversationFormatter):
|
| 144 |
+
support_tokenizer_types = ['QWenTokenizer', 'Qwen2TokenizerFast']
|
| 145 |
|
| 146 |
def __init__(self, tokenizer):
|
| 147 |
super().__init__(tokenizer)
|
generation_config.json
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"bos_token_id": 151643,
|
| 3 |
+
"do_sample": true,
|
| 4 |
+
"eos_token_id": [
|
| 5 |
+
151645,
|
| 6 |
+
151643
|
| 7 |
+
],
|
| 8 |
+
"multimodal_max_length": 32768,
|
| 9 |
+
"repetition_penalty": 1.05,
|
| 10 |
+
"temperature": 0.7,
|
| 11 |
+
"top_k": 20,
|
| 12 |
+
"top_p": 0.8,
|
| 13 |
+
"transformers_version": "5.3.0"
|
| 14 |
+
}
|
model-00001-of-00002.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:1289d202add65fd0192f43e8e322f173e340d5d106c57812483a910c0cc91939
|
| 3 |
+
size 4293719116
|
model-00002-of-00002.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c8c1546900294fd4e694c2d65e9e58755f88585a60b4218e7bd8d73c75537436
|
| 3 |
+
size 212071848
|
model.safetensors.index.json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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright (C) 2025 AIDC-AI
|
| 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 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 7 |
+
#
|
| 8 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 9 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 10 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 11 |
+
#
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
|
| 15 |
+
import logging
|
| 16 |
+
import os
|
| 17 |
+
import importlib.metadata
|
| 18 |
+
|
| 19 |
+
from packaging import version
|
| 20 |
+
from importlib import import_module
|
| 21 |
+
from typing import List, Callable, Union, Optional, Dict
|
| 22 |
+
|
| 23 |
+
import PIL.Image
|
| 24 |
+
import torch
|
| 25 |
+
from torch import Tensor
|
| 26 |
+
from torch.nn import init
|
| 27 |
+
from torch.nn.functional import softmax, gumbel_softmax, pad
|
| 28 |
+
from transformers.utils import is_flash_attn_2_available
|
| 29 |
+
from transformers import PreTrainedModel, AutoModel, AutoTokenizer, AutoModelForCausalLM, AutoImageProcessor
|
| 30 |
+
from transformers.generation.utils import GenerateOutput
|
| 31 |
+
|
| 32 |
+
from .configuration_ovis import BaseVisualTokenizerConfig, Aimv2VisualTokenizerConfig
|
| 33 |
+
from .configuration_ovis import OvisConfig, ConversationFormatter
|
| 34 |
+
from .configuration_ovis import IGNORE_ID, IMAGE_ATOM_ID, IMAGE_INDICATOR_IDS, IMAGE_TOKEN_ID
|
| 35 |
+
|
| 36 |
+
# ----------------------------------------------------------------------
|
| 37 |
+
# Visual Tokenizer
|
| 38 |
+
# ----------------------------------------------------------------------
|
| 39 |
+
class BaseVisualTokenizer(PreTrainedModel):
|
| 40 |
+
base_model_prefix = "backbone"
|
| 41 |
+
main_input_name = None
|
| 42 |
+
_image_processor_class = None
|
| 43 |
+
_image_processor_kwargs = {}
|
| 44 |
+
_backbone_class = None
|
| 45 |
+
_backbone_name_or_path = None
|
| 46 |
+
|
| 47 |
+
def __init__(self, config: BaseVisualTokenizerConfig, *inputs, **kwargs):
|
| 48 |
+
super().__init__(config, *inputs, **kwargs)
|
| 49 |
+
self.image_processor = AutoImageProcessor.from_pretrained(kwargs['image_processor_name_or_path'])
|
| 50 |
+
self.backbone = AutoModel.from_config(self.config.backbone_config)
|
| 51 |
+
head_dim = self.config.vocab_size - len(IMAGE_INDICATOR_IDS) # reserved tokens for IMAGE_INDICATORS
|
| 52 |
+
self.head = torch.nn.Sequential(
|
| 53 |
+
torch.nn.Linear(
|
| 54 |
+
self.backbone.config.hidden_size * self.config.hidden_stride * self.config.hidden_stride, head_dim,
|
| 55 |
+
bias=False
|
| 56 |
+
),
|
| 57 |
+
torch.nn.LayerNorm(head_dim)
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
assert all((self.image_processor.do_resize,
|
| 61 |
+
not getattr(self.image_processor, 'do_center_crop', False),
|
| 62 |
+
self.image_processor.do_rescale,
|
| 63 |
+
self.image_processor.do_normalize
|
| 64 |
+
)), f"image_processor `{self.image_processor}` is not supported currently"
|
| 65 |
+
|
| 66 |
+
def get_backbone(self):
|
| 67 |
+
return self.backbone
|
| 68 |
+
|
| 69 |
+
def get_image_processor(self):
|
| 70 |
+
return self.image_processor
|
| 71 |
+
|
| 72 |
+
def mock_input(self):
|
| 73 |
+
height, width = self.get_image_size()
|
| 74 |
+
return torch.zeros(1, 3, height, width), self.construct_image_placeholders((1, 1))
|
| 75 |
+
|
| 76 |
+
def get_head(self):
|
| 77 |
+
return self.head
|
| 78 |
+
|
| 79 |
+
def get_image_size(self):
|
| 80 |
+
raise NotImplementedError
|
| 81 |
+
|
| 82 |
+
@staticmethod
|
| 83 |
+
def construct_image_placeholders(grid):
|
| 84 |
+
image_placeholders = [IMAGE_INDICATOR_IDS[0], IMAGE_ATOM_ID, IMAGE_INDICATOR_IDS[1]]
|
| 85 |
+
if grid[0] * grid[1] > 1:
|
| 86 |
+
for r in range(grid[0]):
|
| 87 |
+
for c in range(grid[1]):
|
| 88 |
+
image_placeholders.append(IMAGE_ATOM_ID)
|
| 89 |
+
if c < grid[1] - 1:
|
| 90 |
+
image_placeholders.append(IMAGE_INDICATOR_IDS[2])
|
| 91 |
+
if r < grid[0] - 1:
|
| 92 |
+
image_placeholders.append(IMAGE_INDICATOR_IDS[3])
|
| 93 |
+
image_placeholders.append(IMAGE_INDICATOR_IDS[4])
|
| 94 |
+
return image_placeholders
|
| 95 |
+
|
| 96 |
+
def preprocess_image(self, image: PIL.Image.Image, max_partition=9, covering_threshold=0.9, convert_to_rgb=True):
|
| 97 |
+
def _preprocess(img: PIL.Image.Image, side):
|
| 98 |
+
# first resize and preprocess
|
| 99 |
+
w, h = img.size
|
| 100 |
+
if w == h:
|
| 101 |
+
new_width = new_height = side
|
| 102 |
+
elif w > h:
|
| 103 |
+
new_width = side
|
| 104 |
+
new_height = int(h / w * new_width)
|
| 105 |
+
else:
|
| 106 |
+
new_height = side
|
| 107 |
+
new_width = int(w / h * new_height)
|
| 108 |
+
new_size = dict(height=new_height, width=new_width)
|
| 109 |
+
pixel_values = self.image_processor.preprocess(img, size=new_size, return_tensors='pt')['pixel_values']
|
| 110 |
+
|
| 111 |
+
# then pad to square
|
| 112 |
+
square_values = torch.zeros([1, 3, side, side], dtype=pixel_values.dtype, device=pixel_values.device)
|
| 113 |
+
new_height, new_width = pixel_values.shape[2:]
|
| 114 |
+
if new_height == new_width:
|
| 115 |
+
square_values[:, :, :, :] = pixel_values
|
| 116 |
+
elif new_height > new_width:
|
| 117 |
+
from_index = (side - new_width) // 2
|
| 118 |
+
square_values[:, :, :, from_index:from_index + new_width] = pixel_values
|
| 119 |
+
else:
|
| 120 |
+
from_index = (side - new_height) // 2
|
| 121 |
+
square_values[:, :, from_index:from_index + new_height, :] = pixel_values
|
| 122 |
+
|
| 123 |
+
return square_values
|
| 124 |
+
|
| 125 |
+
def _partition(img, grid):
|
| 126 |
+
w, h = img.size
|
| 127 |
+
row_height = h // grid[0]
|
| 128 |
+
col_width = w // grid[1]
|
| 129 |
+
|
| 130 |
+
partition = []
|
| 131 |
+
for row in range(grid[0]):
|
| 132 |
+
for col in range(grid[1]):
|
| 133 |
+
left = col * col_width
|
| 134 |
+
upper = row * row_height
|
| 135 |
+
right = w if col == grid[1] - 1 else (col + 1) * col_width
|
| 136 |
+
lower = h if row == grid[0] - 1 else (row + 1) * row_height
|
| 137 |
+
partition.append((left, upper, right, lower))
|
| 138 |
+
|
| 139 |
+
return partition
|
| 140 |
+
|
| 141 |
+
def _covering_area(left, upper, right, lower, side):
|
| 142 |
+
w = right - left
|
| 143 |
+
h = lower - upper
|
| 144 |
+
w, h = max(w, h), min(w, h)
|
| 145 |
+
if w > side:
|
| 146 |
+
h = h / w * side
|
| 147 |
+
w = side
|
| 148 |
+
return w * h
|
| 149 |
+
|
| 150 |
+
def _get_best_grid(img, side):
|
| 151 |
+
img_area = img.size[0] * img.size[1]
|
| 152 |
+
|
| 153 |
+
candidate_grids = []
|
| 154 |
+
for i in range(1, max_partition + 1):
|
| 155 |
+
for j in range(1, max_partition + 1):
|
| 156 |
+
if i * j <= max_partition:
|
| 157 |
+
candidate_grids.append((i, j))
|
| 158 |
+
|
| 159 |
+
all_grids = []
|
| 160 |
+
good_grids = []
|
| 161 |
+
for grid in candidate_grids:
|
| 162 |
+
partition = _partition(img, grid)
|
| 163 |
+
covering_ratio = sum([_covering_area(*p, side) for p in partition]) / img_area
|
| 164 |
+
assert covering_ratio <= 1.0
|
| 165 |
+
all_grids.append((grid, covering_ratio))
|
| 166 |
+
if covering_ratio > covering_threshold:
|
| 167 |
+
good_grids.append((grid, covering_ratio))
|
| 168 |
+
|
| 169 |
+
if len(good_grids) > 0:
|
| 170 |
+
# pick the good partition with minimum #sub_images and break the tie using covering_ratio
|
| 171 |
+
return sorted(good_grids, key=lambda x: (x[0][0] * x[0][1], -x[1]))[0][0]
|
| 172 |
+
else:
|
| 173 |
+
# pick the partition with maximum covering_ratio and break the tie using #sub_images
|
| 174 |
+
return sorted(all_grids, key=lambda x: (-x[1], x[0][0] * x[0][1]))[0][0]
|
| 175 |
+
|
| 176 |
+
if convert_to_rgb and image.mode != 'RGB':
|
| 177 |
+
image = image.convert('RGB')
|
| 178 |
+
|
| 179 |
+
sides = self.get_image_size()
|
| 180 |
+
if sides[0] != sides[1]:
|
| 181 |
+
raise ValueError('get_image_size() returns non-square size')
|
| 182 |
+
side = sides[0]
|
| 183 |
+
grid = _get_best_grid(image, side)
|
| 184 |
+
partition = _partition(image, grid)
|
| 185 |
+
crops = [image.crop(p) for p in partition]
|
| 186 |
+
if len(crops) > 1:
|
| 187 |
+
crops.insert(0, image)
|
| 188 |
+
pixel_values = torch.cat([_preprocess(crop, side) for crop in crops], dim=0)
|
| 189 |
+
image_placeholders = self.construct_image_placeholders(grid)
|
| 190 |
+
return pixel_values, image_placeholders
|
| 191 |
+
|
| 192 |
+
def tokenize(self, logits):
|
| 193 |
+
def st_argmax(y_soft, dim): # straight-through softmax
|
| 194 |
+
index = y_soft.max(dim, keepdim=True)[1]
|
| 195 |
+
y_hard = torch.zeros_like(y_soft, memory_format=torch.legacy_contiguous_format).scatter_(dim, index, 1.0)
|
| 196 |
+
ret = y_hard - y_soft.detach() + y_soft
|
| 197 |
+
return ret
|
| 198 |
+
|
| 199 |
+
if self.config.tokenize_function == 'softmax':
|
| 200 |
+
tokens = softmax(logits, dim=-1)
|
| 201 |
+
elif self.config.tokenize_function == 'gumbel_argmax':
|
| 202 |
+
tokens = gumbel_softmax(logits, tau=self.config.tau, hard=True)
|
| 203 |
+
elif self.config.tokenize_function == 'st_argmax':
|
| 204 |
+
tokens = st_argmax(logits, dim=-1)
|
| 205 |
+
else:
|
| 206 |
+
raise ValueError(
|
| 207 |
+
f'Invalid `max_type`, expected softmax or gumbel_argmax or st_argmax, but got {self.config.tokenize_function}')
|
| 208 |
+
return tokens
|
| 209 |
+
|
| 210 |
+
def encode(self, pixel_values):
|
| 211 |
+
output = self.backbone(pixel_values, output_hidden_states=True, return_dict=True)
|
| 212 |
+
features = output.hidden_states[-1]
|
| 213 |
+
if self.config.drop_cls_token:
|
| 214 |
+
features = features[:, 1:, :]
|
| 215 |
+
|
| 216 |
+
# merge number of `hidden_stride * hidden_stride` hidden states together to reduce token sequence length
|
| 217 |
+
# e.g., for hidden_stride=2, this leads to a token length reduction: 1024 -> 256 for aimv2
|
| 218 |
+
if self.config.hidden_stride > 1:
|
| 219 |
+
n, l, d = features.shape # this `d` maybe different from the above `d
|
| 220 |
+
sqrt_l = int(l ** 0.5)
|
| 221 |
+
assert sqrt_l ** 2 == l, "The token sequence length should be a perfect square."
|
| 222 |
+
features = features.reshape(n, sqrt_l, sqrt_l, d)
|
| 223 |
+
pl = (self.config.hidden_stride - (sqrt_l % self.config.hidden_stride)) % self.config.hidden_stride
|
| 224 |
+
features = pad(features, (0, 0, 0, pl, 0, pl), "constant", 0)
|
| 225 |
+
sqrt_l += pl
|
| 226 |
+
features = features.reshape(n, sqrt_l // self.config.hidden_stride, self.config.hidden_stride,
|
| 227 |
+
sqrt_l // self.config.hidden_stride, self.config.hidden_stride, d)
|
| 228 |
+
features = features.permute(0, 1, 3, 2, 4, 5) # [n, sqrt_l/hs, sqrt_l/hs, hs, hs, d]
|
| 229 |
+
features = features.flatten(3) # [n, sqrt_l/hs, sqrt_l/hs, hs*hs*d]
|
| 230 |
+
features = features.reshape(
|
| 231 |
+
n, -1, self.config.hidden_stride * self.config.hidden_stride * d)
|
| 232 |
+
|
| 233 |
+
return features
|
| 234 |
+
|
| 235 |
+
def forward(self, pixel_values) -> torch.Tensor: # [BatchSize, ImageShape] -> [BatchSize, #Token, VocabSize]
|
| 236 |
+
features = self.encode(pixel_values)
|
| 237 |
+
logits = self.head(features)
|
| 238 |
+
tokens = self.tokenize(logits)
|
| 239 |
+
# tokens' shape is [BatchSize, #Token, VocabSize-5], so padding with [BatchSize, #Token, 5], after
|
| 240 |
+
# which, tokens' shape should become [BatchSize, #Token, VocabSize]
|
| 241 |
+
batch_size, token_len, _ = tokens.shape
|
| 242 |
+
padding_tensor = torch.zeros(size=(batch_size, token_len, len(IMAGE_INDICATOR_IDS)),
|
| 243 |
+
dtype=tokens.dtype,
|
| 244 |
+
device=tokens.device,
|
| 245 |
+
layout=tokens.layout,
|
| 246 |
+
requires_grad=False)
|
| 247 |
+
tokens = torch.cat((tokens, padding_tensor), dim=2)
|
| 248 |
+
return tokens
|
| 249 |
+
|
| 250 |
+
|
| 251 |
+
class Aimv2VisualTokenizer(BaseVisualTokenizer):
|
| 252 |
+
config_class = Aimv2VisualTokenizerConfig
|
| 253 |
+
supports_gradient_checkpointing = True
|
| 254 |
+
_no_split_modules = ["AIMv2ViTPreprocessor", "AIMv2Block"]
|
| 255 |
+
_image_processor_kwargs = dict(do_center_crop=False)
|
| 256 |
+
|
| 257 |
+
def get_image_size(self):
|
| 258 |
+
height = self.image_processor.crop_size["height"]
|
| 259 |
+
width = self.image_processor.crop_size["width"]
|
| 260 |
+
return height, width
|
| 261 |
+
|
| 262 |
+
|
| 263 |
+
AutoModel.register(Aimv2VisualTokenizerConfig, Aimv2VisualTokenizer)
|
| 264 |
+
|
| 265 |
+
|
| 266 |
+
# ----------------------------------------------------------------------
|
| 267 |
+
# Ovis
|
| 268 |
+
# ----------------------------------------------------------------------
|
| 269 |
+
class VisualEmbedding(torch.nn.Embedding):
|
| 270 |
+
def forward(self, visual_tokens: Tensor) -> Tensor:
|
| 271 |
+
if visual_tokens.dtype in [torch.int8, torch.int16, torch.int32, torch.int64, torch.long]:
|
| 272 |
+
return super().forward(visual_tokens)
|
| 273 |
+
return torch.matmul(visual_tokens, self.weight)
|
| 274 |
+
|
| 275 |
+
def reset_parameters(self, mean=0., std=1.) -> None:
|
| 276 |
+
init.normal_(self.weight, mean=mean, std=std)
|
| 277 |
+
self._fill_padding_idx_with_zero()
|
| 278 |
+
|
| 279 |
+
|
| 280 |
+
class OvisPreTrainedModel(PreTrainedModel):
|
| 281 |
+
config_class = OvisConfig
|
| 282 |
+
base_model_prefix = "ovis"
|
| 283 |
+
|
| 284 |
+
|
| 285 |
+
class Ovis(OvisPreTrainedModel):
|
| 286 |
+
|
| 287 |
+
def __init__(self, config: OvisConfig, *inputs, **kwargs):
|
| 288 |
+
super().__init__(config, *inputs, **kwargs)
|
| 289 |
+
attn_kwargs = dict()
|
| 290 |
+
if self.config.llm_attn_implementation:
|
| 291 |
+
if self.config.llm_attn_implementation == "flash_attention_2":
|
| 292 |
+
assert (is_flash_attn_2_available() and
|
| 293 |
+
version.parse(importlib.metadata.version("flash_attn")) >= version.parse("2.6.3")), \
|
| 294 |
+
"Using `flash_attention_2` requires having `flash_attn>=2.6.3` installed."
|
| 295 |
+
attn_kwargs["attn_implementation"] = self.config.llm_attn_implementation
|
| 296 |
+
self.llm = AutoModelForCausalLM.from_config(self.config.llm_config, **attn_kwargs)
|
| 297 |
+
assert self.config.hidden_size == self.llm.config.hidden_size, "hidden size mismatch"
|
| 298 |
+
self.text_tokenizer = AutoTokenizer.from_pretrained(self.config.name_or_path)
|
| 299 |
+
self.visual_tokenizer = AutoModel.from_config(self.config.visual_tokenizer_config,
|
| 300 |
+
image_processor_name_or_path=self.config.name_or_path)
|
| 301 |
+
self.vte = VisualEmbedding(
|
| 302 |
+
self.config.visual_tokenizer_config.vocab_size,
|
| 303 |
+
self.config.hidden_size,
|
| 304 |
+
device=self.visual_tokenizer.device,
|
| 305 |
+
dtype=self.visual_tokenizer.dtype
|
| 306 |
+
)
|
| 307 |
+
|
| 308 |
+
def _merge_modules(modules_list: tuple):
|
| 309 |
+
merged_modules = []
|
| 310 |
+
for modules in modules_list:
|
| 311 |
+
merged_modules.extend(modules if modules else [])
|
| 312 |
+
return merged_modules
|
| 313 |
+
|
| 314 |
+
self._no_split_modules = _merge_modules((self.llm._no_split_modules, self.visual_tokenizer._no_split_modules))
|
| 315 |
+
self._skip_keys_device_placement = self.llm._skip_keys_device_placement
|
| 316 |
+
self._keep_in_fp32_modules = _merge_modules(
|
| 317 |
+
(self.llm._keep_in_fp32_modules, self.visual_tokenizer._keep_in_fp32_modules))
|
| 318 |
+
self.is_parallelizable = all((self.llm.is_parallelizable, self.visual_tokenizer.is_parallelizable))
|
| 319 |
+
self.supports_gradient_checkpointing = True
|
| 320 |
+
self._supports_flash_attn_2 = True
|
| 321 |
+
|
| 322 |
+
def get_text_tokenizer(self):
|
| 323 |
+
return self.text_tokenizer
|
| 324 |
+
|
| 325 |
+
def get_visual_tokenizer(self):
|
| 326 |
+
return self.visual_tokenizer
|
| 327 |
+
|
| 328 |
+
def tie_weights(self):
|
| 329 |
+
if not self.config.disable_tie_weight:
|
| 330 |
+
self.get_llm().tie_weights()
|
| 331 |
+
|
| 332 |
+
def get_llm(self):
|
| 333 |
+
return self.llm
|
| 334 |
+
|
| 335 |
+
def get_vte(self):
|
| 336 |
+
return self.vte
|
| 337 |
+
|
| 338 |
+
def get_wte(self):
|
| 339 |
+
return self.llm.get_input_embeddings()
|
| 340 |
+
|
| 341 |
+
def get_conversation_formatter(self) -> ConversationFormatter:
|
| 342 |
+
if getattr(self, 'conversation_formatter', None) is None:
|
| 343 |
+
self.conversation_formatter = getattr(import_module(".configuration_ovis", __package__),
|
| 344 |
+
self.config.conversation_formatter_class)(self.text_tokenizer)
|
| 345 |
+
return self.conversation_formatter
|
| 346 |
+
|
| 347 |
+
def forward(
|
| 348 |
+
self,
|
| 349 |
+
input_ids: torch.Tensor,
|
| 350 |
+
attention_mask: torch.Tensor,
|
| 351 |
+
labels: Optional[torch.Tensor],
|
| 352 |
+
pixel_values: List[Optional[torch.Tensor]],
|
| 353 |
+
**kwargs
|
| 354 |
+
):
|
| 355 |
+
# assert self.training, "`forward` can only be used in training. For inference, use `generate`."
|
| 356 |
+
_, inputs_embeds, labels, attention_mask = self.merge_multimodal(
|
| 357 |
+
text_input_ids=input_ids,
|
| 358 |
+
text_attention_masks=attention_mask,
|
| 359 |
+
text_labels=labels,
|
| 360 |
+
pixel_values=pixel_values
|
| 361 |
+
)
|
| 362 |
+
return self.llm(inputs_embeds=inputs_embeds, labels=labels, attention_mask=attention_mask, **kwargs)
|
| 363 |
+
|
| 364 |
+
def merge_multimodal(
|
| 365 |
+
self,
|
| 366 |
+
text_input_ids: torch.Tensor,
|
| 367 |
+
text_attention_masks: torch.Tensor,
|
| 368 |
+
text_labels: Optional[torch.Tensor],
|
| 369 |
+
pixel_values: List[Optional[torch.Tensor]],
|
| 370 |
+
left_padding: bool = False
|
| 371 |
+
):
|
| 372 |
+
input_device = text_input_ids.device
|
| 373 |
+
visual_vocab_szie = self.get_visual_tokenizer().config.vocab_size
|
| 374 |
+
visual_indicator_embeds = self.get_vte()(
|
| 375 |
+
torch.tensor(
|
| 376 |
+
list(range(visual_vocab_szie - 5, visual_vocab_szie)),
|
| 377 |
+
dtype=torch.long,
|
| 378 |
+
device=self.get_visual_tokenizer().device
|
| 379 |
+
)
|
| 380 |
+
).to(device=input_device)
|
| 381 |
+
|
| 382 |
+
if self.training:
|
| 383 |
+
# When training, to be compatible with deepspeed zero, each sample has to include pixel_value tensor.
|
| 384 |
+
# For text-only sample, one can simply use a full zero tensor as pixel_value, which will be ignored
|
| 385 |
+
# (see below in this function); so, the gradient will not be affected.
|
| 386 |
+
num_images = [x.shape[0] for x in pixel_values]
|
| 387 |
+
visual_tokens = self.visual_tokenizer(torch.cat([x for x in pixel_values], dim=0))
|
| 388 |
+
visual_embeds = torch.split(self.get_vte()(visual_tokens).to(dtype=self.dtype, device=input_device),
|
| 389 |
+
split_size_or_sections=num_images, dim=0)
|
| 390 |
+
visual_input_ids = torch.split(torch.argmax(visual_tokens, dim=-1).to(device=input_device),
|
| 391 |
+
split_size_or_sections=num_images, dim=0)
|
| 392 |
+
visual_labels = [torch.full(x.shape, IGNORE_ID, dtype=torch.long, device=input_device) for x in
|
| 393 |
+
visual_input_ids]
|
| 394 |
+
else:
|
| 395 |
+
# When inference, sample can include only text with `None` pixel_value
|
| 396 |
+
num_images = [x.shape[0] if x is not None else 0 for x in pixel_values]
|
| 397 |
+
if sum(num_images) > 0:
|
| 398 |
+
visual_tokens = self.visual_tokenizer(torch.cat([x for x in pixel_values if x is not None], dim=0))
|
| 399 |
+
visual_embeds = torch.split(self.get_vte()(visual_tokens).to(dtype=self.dtype, device=input_device),
|
| 400 |
+
split_size_or_sections=num_images, dim=0)
|
| 401 |
+
visual_input_ids = torch.split(torch.argmax(visual_tokens, dim=-1).to(device=input_device),
|
| 402 |
+
split_size_or_sections=num_images, dim=0)
|
| 403 |
+
visual_labels = [torch.full(x.shape, IGNORE_ID, dtype=torch.long, device=input_device) for x in
|
| 404 |
+
visual_input_ids]
|
| 405 |
+
else:
|
| 406 |
+
# just placeholders
|
| 407 |
+
visual_embeds = [None] * len(num_images)
|
| 408 |
+
visual_input_ids = [None] * len(num_images)
|
| 409 |
+
visual_labels = [None] * len(num_images)
|
| 410 |
+
# just placeholders
|
| 411 |
+
if text_labels is None:
|
| 412 |
+
text_labels = torch.full(text_input_ids.shape, IGNORE_ID, dtype=torch.long, device=input_device)
|
| 413 |
+
|
| 414 |
+
input_embeds = []
|
| 415 |
+
attention_masks = []
|
| 416 |
+
labels = []
|
| 417 |
+
for text_input_id, text_label, text_attention_mask, visual_embed, visual_input_id, visual_label in zip(
|
| 418 |
+
text_input_ids, text_labels, text_attention_masks, visual_embeds, visual_input_ids, visual_labels
|
| 419 |
+
):
|
| 420 |
+
placeholder_token_mask = torch.lt(text_input_id, 0)
|
| 421 |
+
text_embed = self.get_wte()(torch.masked_fill(text_input_id, placeholder_token_mask, 0))
|
| 422 |
+
for i, indicator_id in enumerate(IMAGE_INDICATOR_IDS):
|
| 423 |
+
text_embed[text_input_id == indicator_id] = visual_indicator_embeds[i]
|
| 424 |
+
image_atom_positions = torch.where(torch.eq(text_input_id, IMAGE_ATOM_ID))[0].tolist()
|
| 425 |
+
if len(image_atom_positions) > 0:
|
| 426 |
+
input_embed_parts = []
|
| 427 |
+
attention_mask_parts = []
|
| 428 |
+
label_parts = []
|
| 429 |
+
prev_image_atom_position = -1
|
| 430 |
+
for index, image_atom_position in enumerate(image_atom_positions):
|
| 431 |
+
input_embed_parts.append(
|
| 432 |
+
text_embed[prev_image_atom_position + 1:image_atom_position, :])
|
| 433 |
+
label_parts.append(
|
| 434 |
+
text_label[prev_image_atom_position + 1:image_atom_position])
|
| 435 |
+
attention_mask_parts.append(
|
| 436 |
+
text_attention_mask[prev_image_atom_position + 1:image_atom_position])
|
| 437 |
+
input_embed_parts.append(visual_embed[index])
|
| 438 |
+
attention_mask_parts.append(
|
| 439 |
+
torch.ones_like(visual_label[index], dtype=torch.bool))
|
| 440 |
+
label_parts.append(visual_label[index])
|
| 441 |
+
prev_image_atom_position = image_atom_position
|
| 442 |
+
if prev_image_atom_position + 1 < text_input_id.shape[0]:
|
| 443 |
+
input_embed_parts.append(
|
| 444 |
+
text_embed[prev_image_atom_position + 1:, :])
|
| 445 |
+
attention_mask_parts.append(
|
| 446 |
+
text_attention_mask[prev_image_atom_position + 1:])
|
| 447 |
+
label_parts.append(
|
| 448 |
+
text_label[prev_image_atom_position + 1:])
|
| 449 |
+
input_embed = torch.cat(input_embed_parts, dim=0)
|
| 450 |
+
attention_mask = torch.cat(attention_mask_parts, dim=0)
|
| 451 |
+
label = torch.cat(label_parts, dim=0)
|
| 452 |
+
else:
|
| 453 |
+
input_embed = text_embed
|
| 454 |
+
attention_mask = text_attention_mask
|
| 455 |
+
label = text_label
|
| 456 |
+
if self.training:
|
| 457 |
+
# Make visual_embed & visual_indicator_embeds involved in the backward graph,
|
| 458 |
+
# to be compatible with deepspeed zero and ddp.
|
| 459 |
+
input_embed += torch.sum(visual_embed * 0.0) + torch.sum(visual_indicator_embeds * 0.0)
|
| 460 |
+
input_embeds.append(input_embed)
|
| 461 |
+
attention_masks.append(attention_mask)
|
| 462 |
+
labels.append(label)
|
| 463 |
+
|
| 464 |
+
if self.training: # padding to self.config.multimodal_max_length for increased training speed
|
| 465 |
+
padding_size = max(0, self.config.multimodal_max_length - len(input_embeds[0]))
|
| 466 |
+
input_embeds[0] = torch.nn.ConstantPad2d((0, 0, 0, padding_size), 0.0)(input_embeds[0])
|
| 467 |
+
attention_masks[0] = torch.nn.ConstantPad1d((0, padding_size), False)(attention_masks[0])
|
| 468 |
+
labels[0] = torch.nn.ConstantPad1d((0, padding_size), IGNORE_ID)(labels[0])
|
| 469 |
+
batch_input_embeds = self.pad_truncate_sequence(input_embeds, batch_first=True, padding_value=0.0, left_padding=left_padding)
|
| 470 |
+
batch_attention_mask = self.pad_truncate_sequence(attention_masks, batch_first=True, padding_value=False, left_padding=left_padding)
|
| 471 |
+
batch_labels = self.pad_truncate_sequence(labels, batch_first=True, padding_value=IGNORE_ID, left_padding=left_padding)
|
| 472 |
+
|
| 473 |
+
return visual_input_ids, batch_input_embeds, batch_labels, batch_attention_mask
|
| 474 |
+
|
| 475 |
+
def pad_truncate_sequence(self, sequences: List[torch.Tensor], batch_first: bool = True, padding_value: float = 0.0, left_padding: bool = False) -> torch.Tensor:
|
| 476 |
+
if not left_padding:
|
| 477 |
+
pad_sequence = torch.nn.utils.rnn.pad_sequence(sequences, batch_first=batch_first, padding_value=padding_value)
|
| 478 |
+
return pad_sequence[:,:self.config.multimodal_max_length]
|
| 479 |
+
else:
|
| 480 |
+
pad_sequence = torch.nn.utils.rnn.pad_sequence([i.flip(dims=[0]) for i in sequences],batch_first=True, padding_value=padding_value).flip(dims=[1])
|
| 481 |
+
return pad_sequence[:,-self.config.multimodal_max_length:]
|
| 482 |
+
|
| 483 |
+
def preprocess_inputs(
|
| 484 |
+
self,
|
| 485 |
+
text_or_conversations: Union[List[Dict], str],
|
| 486 |
+
images: Optional[List[PIL.Image.Image]],
|
| 487 |
+
max_partition=9,
|
| 488 |
+
generation_preface='',
|
| 489 |
+
return_labels=False,
|
| 490 |
+
propagate_exception=True,
|
| 491 |
+
frame_selector=None,
|
| 492 |
+
frame_selector_kwargs=None
|
| 493 |
+
):
|
| 494 |
+
# convert text to conversations
|
| 495 |
+
if isinstance(text_or_conversations, str):
|
| 496 |
+
conversations = [{
|
| 497 |
+
"from": "human",
|
| 498 |
+
"value": text_or_conversations
|
| 499 |
+
}]
|
| 500 |
+
elif isinstance(text_or_conversations, list):
|
| 501 |
+
conversations = text_or_conversations
|
| 502 |
+
else:
|
| 503 |
+
raise ValueError(f'Invalid type of `text_or_conversations`, expected `List[Dict]` or `str`,'
|
| 504 |
+
f' but got {type(text_or_conversations)}')
|
| 505 |
+
|
| 506 |
+
if frame_selector is not None:
|
| 507 |
+
frame_selector_kwargs = frame_selector_kwargs or {}
|
| 508 |
+
conversations, images = frame_selector(conversations=conversations, frames=images, **frame_selector_kwargs)
|
| 509 |
+
|
| 510 |
+
# format conversations
|
| 511 |
+
prompt, raw_input_ids, raw_labels = self.get_conversation_formatter().format(
|
| 512 |
+
conversations, generation_preface=generation_preface)
|
| 513 |
+
|
| 514 |
+
# place image placeholders
|
| 515 |
+
input_ids = []
|
| 516 |
+
labels = []
|
| 517 |
+
pixel_values = []
|
| 518 |
+
invalidate_label = False
|
| 519 |
+
image_token_indices = [i for i, v in enumerate(raw_input_ids) if v == IMAGE_TOKEN_ID]
|
| 520 |
+
last_image_token_index = -1
|
| 521 |
+
for i in range(len(image_token_indices)):
|
| 522 |
+
head = 0 if i == 0 else image_token_indices[i - 1] + 1
|
| 523 |
+
tail = image_token_indices[i]
|
| 524 |
+
last_image_token_index = tail
|
| 525 |
+
input_ids.extend(raw_input_ids[head:tail])
|
| 526 |
+
labels.extend(raw_labels[head:tail])
|
| 527 |
+
try:
|
| 528 |
+
image = images[i]
|
| 529 |
+
raw_pixel_values, image_placeholders = self.visual_tokenizer.preprocess_image(
|
| 530 |
+
image, max_partition=max_partition)
|
| 531 |
+
except Exception as e:
|
| 532 |
+
if propagate_exception:
|
| 533 |
+
raise e
|
| 534 |
+
logging.exception(e)
|
| 535 |
+
invalidate_label = True
|
| 536 |
+
raw_pixel_values, image_placeholders = self.visual_tokenizer.mock_input()
|
| 537 |
+
input_ids.extend(image_placeholders)
|
| 538 |
+
labels.extend([IGNORE_ID] * len(image_placeholders))
|
| 539 |
+
pixel_values.append(raw_pixel_values)
|
| 540 |
+
input_ids.extend(raw_input_ids[last_image_token_index + 1:])
|
| 541 |
+
labels.extend(raw_labels[last_image_token_index + 1:])
|
| 542 |
+
|
| 543 |
+
# return tensors
|
| 544 |
+
input_ids = torch.tensor(input_ids, dtype=torch.long)
|
| 545 |
+
labels = torch.tensor([IGNORE_ID] * len(labels) if invalidate_label else labels, dtype=torch.long)
|
| 546 |
+
pixel_values = torch.cat(pixel_values, dim=0) if len(pixel_values) > 0 else None
|
| 547 |
+
|
| 548 |
+
if return_labels:
|
| 549 |
+
return prompt, input_ids, pixel_values, labels
|
| 550 |
+
else:
|
| 551 |
+
return prompt, input_ids, pixel_values
|
| 552 |
+
|
| 553 |
+
def save_pretrained(
|
| 554 |
+
self,
|
| 555 |
+
save_directory: Union[str, os.PathLike],
|
| 556 |
+
is_main_process: bool = True,
|
| 557 |
+
state_dict: Optional[dict] = None,
|
| 558 |
+
save_function: Callable = torch.save,
|
| 559 |
+
push_to_hub: bool = False,
|
| 560 |
+
max_shard_size: Union[int, str] = "5GB",
|
| 561 |
+
safe_serialization: bool = True,
|
| 562 |
+
variant: Optional[str] = None,
|
| 563 |
+
token: Optional[Union[str, bool]] = None,
|
| 564 |
+
save_peft_format: bool = True,
|
| 565 |
+
**kwargs
|
| 566 |
+
):
|
| 567 |
+
super().save_pretrained(save_directory,
|
| 568 |
+
is_main_process=is_main_process,
|
| 569 |
+
state_dict=state_dict,
|
| 570 |
+
save_function=save_function,
|
| 571 |
+
safe_serialization=safe_serialization)
|
| 572 |
+
self.get_text_tokenizer().save_pretrained(save_directory)
|
| 573 |
+
self.get_visual_tokenizer().get_image_processor().save_pretrained(save_directory)
|
| 574 |
+
|
| 575 |
+
def generate(
|
| 576 |
+
self,
|
| 577 |
+
inputs: Optional[torch.Tensor] = None,
|
| 578 |
+
**kwargs
|
| 579 |
+
) -> Union[GenerateOutput, torch.LongTensor]:
|
| 580 |
+
_, inputs_embeds, labels, attention_mask = self.merge_multimodal(
|
| 581 |
+
text_input_ids=inputs,
|
| 582 |
+
text_attention_masks=kwargs.pop('attention_mask'),
|
| 583 |
+
text_labels=None,
|
| 584 |
+
pixel_values=kwargs.pop('pixel_values'),
|
| 585 |
+
left_padding=True
|
| 586 |
+
)
|
| 587 |
+
inputs_embeds = inputs_embeds.detach()
|
| 588 |
+
torch.cuda.empty_cache()
|
| 589 |
+
|
| 590 |
+
return self.llm.generate(inputs=None, inputs_embeds=inputs_embeds, attention_mask=attention_mask, **kwargs)
|
preprocessor_config.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"crop_size": {
|
| 3 |
+
"height": 448,
|
| 4 |
+
"width": 448
|
| 5 |
+
},
|
| 6 |
+
"data_format": "channels_first",
|
| 7 |
+
"do_center_crop": false,
|
| 8 |
+
"do_convert_rgb": true,
|
| 9 |
+
"do_normalize": true,
|
| 10 |
+
"do_rescale": true,
|
| 11 |
+
"do_resize": true,
|
| 12 |
+
"image_mean": [
|
| 13 |
+
0.48145466,
|
| 14 |
+
0.4578275,
|
| 15 |
+
0.40821073
|
| 16 |
+
],
|
| 17 |
+
"image_processor_type": "CLIPImageProcessorFast",
|
| 18 |
+
"image_std": [
|
| 19 |
+
0.26862954,
|
| 20 |
+
0.26130258,
|
| 21 |
+
0.27577711
|
| 22 |
+
],
|
| 23 |
+
"resample": 3,
|
| 24 |
+
"rescale_factor": 0.00392156862745098,
|
| 25 |
+
"size": {
|
| 26 |
+
"shortest_edge": 448
|
| 27 |
+
}
|
| 28 |
+
}
|
quant_log.csv
ADDED
|
@@ -0,0 +1,253 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
layer,module,loss,samples,damp,time
|
| 2 |
+
0,self_attn.k_proj,0.0000926561,0.05000,1.479
|
| 3 |
+
0,self_attn.q_proj,0.0004441526,0.05000,1.486
|
| 4 |
+
0,self_attn.v_proj,0.0000118854,0.05000,1.526
|
| 5 |
+
0,self_attn.o_proj,0.0000509747,0.05000,0.488
|
| 6 |
+
0,mlp.gate_proj,0.0005754575,0.05000,0.925
|
| 7 |
+
0,mlp.up_proj,0.0004449969,0.05000,0.925
|
| 8 |
+
0,mlp.down_proj,0.0000614704,0.05000,2.638
|
| 9 |
+
1,self_attn.v_proj,0.0000047415,0.05000,1.379
|
| 10 |
+
1,self_attn.q_proj,0.0000868213,0.05000,1.386
|
| 11 |
+
1,self_attn.k_proj,0.0000241810,0.05000,1.393
|
| 12 |
+
1,self_attn.o_proj,0.0000163117,0.05000,0.436
|
| 13 |
+
1,mlp.gate_proj,0.0385717081,0.05000,0.929
|
| 14 |
+
1,mlp.up_proj,0.0290021339,0.05000,0.933
|
| 15 |
+
1,mlp.down_proj,0.0000056345,0.05000,2.672
|
| 16 |
+
2,self_attn.q_proj,0.0001292669,0.05000,1.324
|
| 17 |
+
2,self_attn.v_proj,0.0000073232,0.05000,1.331
|
| 18 |
+
2,self_attn.k_proj,0.0000284607,0.05000,1.363
|
| 19 |
+
2,self_attn.o_proj,0.0000094392,0.05000,0.446
|
| 20 |
+
2,mlp.gate_proj,0.0082810673,0.05000,0.926
|
| 21 |
+
2,mlp.up_proj,0.0080136446,0.05000,0.929
|
| 22 |
+
2,mlp.down_proj,0.0269350808,0.05000,2.572
|
| 23 |
+
3,self_attn.k_proj,0.0001170079,0.05000,1.427
|
| 24 |
+
3,self_attn.q_proj,0.0005366353,0.05000,1.437
|
| 25 |
+
3,self_attn.v_proj,0.0000215521,0.05000,1.439
|
| 26 |
+
3,self_attn.o_proj,0.0000148144,0.05000,0.429
|
| 27 |
+
3,mlp.gate_proj,0.0101124701,0.05000,0.956
|
| 28 |
+
3,mlp.up_proj,0.0087271334,0.05000,0.963
|
| 29 |
+
3,mlp.down_proj,0.0001437532,0.05000,2.713
|
| 30 |
+
4,self_attn.q_proj,0.0004495623,0.05000,1.299
|
| 31 |
+
4,self_attn.v_proj,0.0000305555,0.05000,1.323
|
| 32 |
+
4,self_attn.k_proj,0.0000884943,0.05000,1.351
|
| 33 |
+
4,self_attn.o_proj,0.0000298310,0.05000,0.433
|
| 34 |
+
4,mlp.gate_proj,0.0096624994,0.05000,0.973
|
| 35 |
+
4,mlp.up_proj,0.0069845970,0.05000,0.986
|
| 36 |
+
4,mlp.down_proj,0.0000694115,0.05000,2.615
|
| 37 |
+
5,self_attn.q_proj,0.0009599595,0.05000,1.503
|
| 38 |
+
5,self_attn.v_proj,0.0000786844,0.05000,1.520
|
| 39 |
+
5,self_attn.k_proj,0.0001727102,0.05000,1.529
|
| 40 |
+
5,self_attn.o_proj,0.0000174932,0.05000,0.429
|
| 41 |
+
5,mlp.gate_proj,0.0131947269,0.05000,0.968
|
| 42 |
+
5,mlp.up_proj,0.0111081641,0.05000,0.971
|
| 43 |
+
5,mlp.down_proj,0.0001038428,0.05000,2.544
|
| 44 |
+
6,self_attn.q_proj,0.0006202141,0.05000,1.369
|
| 45 |
+
6,self_attn.k_proj,0.0000994150,0.05000,1.376
|
| 46 |
+
6,self_attn.v_proj,0.0000700387,0.05000,1.398
|
| 47 |
+
6,self_attn.o_proj,0.0000451209,0.05000,0.440
|
| 48 |
+
6,mlp.up_proj,0.0139774917,0.05000,0.940
|
| 49 |
+
6,mlp.gate_proj,0.0164863941,0.05000,0.951
|
| 50 |
+
6,mlp.down_proj,0.0001430597,0.05000,2.530
|
| 51 |
+
7,self_attn.q_proj,0.0007890122,0.05000,1.466
|
| 52 |
+
7,self_attn.v_proj,0.0000788149,0.05000,1.467
|
| 53 |
+
7,self_attn.k_proj,0.0001257278,0.05000,1.466
|
| 54 |
+
7,self_attn.o_proj,0.0000292612,0.05000,0.455
|
| 55 |
+
7,mlp.gate_proj,0.0158752722,0.05000,0.954
|
| 56 |
+
7,mlp.up_proj,0.0129439407,0.05000,0.957
|
| 57 |
+
7,mlp.down_proj,0.0001964390,0.05000,2.723
|
| 58 |
+
8,self_attn.q_proj,0.0007963218,0.05000,1.280
|
| 59 |
+
8,self_attn.k_proj,0.0001210402,0.05000,1.289
|
| 60 |
+
8,self_attn.v_proj,0.0000847841,0.05000,1.323
|
| 61 |
+
8,self_attn.o_proj,0.0000442622,0.05000,0.432
|
| 62 |
+
8,mlp.gate_proj,0.0170453784,0.05000,0.913
|
| 63 |
+
8,mlp.up_proj,0.0121827184,0.05000,0.917
|
| 64 |
+
8,mlp.down_proj,0.0002522487,0.05000,2.543
|
| 65 |
+
9,self_attn.k_proj,0.0001248283,0.05000,1.415
|
| 66 |
+
9,self_attn.v_proj,0.0000729903,0.05000,1.415
|
| 67 |
+
9,self_attn.q_proj,0.0007056552,0.05000,1.434
|
| 68 |
+
9,self_attn.o_proj,0.0000737344,0.05000,0.427
|
| 69 |
+
9,mlp.up_proj,0.0105305310,0.05000,0.946
|
| 70 |
+
9,mlp.gate_proj,0.0156497282,0.05000,0.945
|
| 71 |
+
9,mlp.down_proj,0.0002750355,0.05000,2.662
|
| 72 |
+
10,self_attn.q_proj,0.0010030885,0.05000,1.415
|
| 73 |
+
10,self_attn.v_proj,0.0001205626,0.05000,1.425
|
| 74 |
+
10,self_attn.k_proj,0.0001500157,0.05000,1.433
|
| 75 |
+
10,self_attn.o_proj,0.0000719801,0.05000,0.423
|
| 76 |
+
10,mlp.up_proj,0.0074459834,0.05000,0.876
|
| 77 |
+
10,mlp.gate_proj,0.0113039666,0.05000,0.877
|
| 78 |
+
10,mlp.down_proj,0.0003157377,0.05000,2.603
|
| 79 |
+
11,self_attn.q_proj,0.0007359214,0.05000,1.301
|
| 80 |
+
11,self_attn.v_proj,0.0000977395,0.05000,1.325
|
| 81 |
+
11,self_attn.k_proj,0.0001095498,0.05000,1.329
|
| 82 |
+
11,self_attn.o_proj,0.0001212573,0.05000,0.425
|
| 83 |
+
11,mlp.up_proj,0.0042120424,0.05000,0.937
|
| 84 |
+
11,mlp.gate_proj,0.0058166186,0.05000,0.943
|
| 85 |
+
11,mlp.down_proj,0.0003497882,0.05000,2.562
|
| 86 |
+
12,self_attn.k_proj,0.0001183058,0.05000,1.428
|
| 87 |
+
12,self_attn.v_proj,0.0000878876,0.05000,1.438
|
| 88 |
+
12,self_attn.q_proj,0.0007691479,0.05000,1.445
|
| 89 |
+
12,self_attn.o_proj,0.0001109989,0.05000,0.416
|
| 90 |
+
12,mlp.gate_proj,0.0063900089,0.05000,0.951
|
| 91 |
+
12,mlp.up_proj,0.0045523345,0.05000,0.961
|
| 92 |
+
12,mlp.down_proj,0.0003277593,0.05000,2.648
|
| 93 |
+
13,self_attn.q_proj,0.0009233296,0.05000,1.406
|
| 94 |
+
13,self_attn.v_proj,0.0000616283,0.05000,1.438
|
| 95 |
+
13,self_attn.k_proj,0.0001702990,0.05000,1.441
|
| 96 |
+
13,self_attn.o_proj,0.0000684895,0.05000,0.456
|
| 97 |
+
13,mlp.gate_proj,0.0034981644,0.05000,0.951
|
| 98 |
+
13,mlp.up_proj,0.0033732841,0.05000,0.956
|
| 99 |
+
13,mlp.down_proj,0.0002857845,0.05000,2.568
|
| 100 |
+
14,self_attn.k_proj,0.0001107448,0.05000,1.253
|
| 101 |
+
14,self_attn.q_proj,0.0007179622,0.05000,1.262
|
| 102 |
+
14,self_attn.v_proj,0.0000722210,0.05000,1.286
|
| 103 |
+
14,self_attn.o_proj,0.0001577273,0.05000,0.435
|
| 104 |
+
14,mlp.up_proj,0.0032660126,0.05000,0.936
|
| 105 |
+
14,mlp.gate_proj,0.0033751456,0.05000,0.940
|
| 106 |
+
14,mlp.down_proj,0.0002750711,0.05000,2.512
|
| 107 |
+
15,self_attn.q_proj,0.0007260779,0.05000,1.240
|
| 108 |
+
15,self_attn.k_proj,0.0001140344,0.05000,1.259
|
| 109 |
+
15,self_attn.v_proj,0.0000717924,0.05000,1.279
|
| 110 |
+
15,self_attn.o_proj,0.0001466886,0.05000,0.435
|
| 111 |
+
15,mlp.gate_proj,0.0027777215,0.05000,0.956
|
| 112 |
+
15,mlp.up_proj,0.0029199004,0.05000,0.969
|
| 113 |
+
15,mlp.down_proj,0.0002450036,0.05000,2.557
|
| 114 |
+
16,self_attn.k_proj,0.0001236907,0.05000,1.357
|
| 115 |
+
16,self_attn.q_proj,0.0007415169,0.05000,1.370
|
| 116 |
+
16,self_attn.v_proj,0.0000728653,0.05000,1.394
|
| 117 |
+
16,self_attn.o_proj,0.0001548824,0.05000,0.427
|
| 118 |
+
16,mlp.up_proj,0.0029338877,0.05000,0.930
|
| 119 |
+
16,mlp.gate_proj,0.0029728621,0.05000,0.936
|
| 120 |
+
16,mlp.down_proj,0.0002227558,0.05000,2.520
|
| 121 |
+
17,self_attn.q_proj,0.0011978788,0.05000,1.411
|
| 122 |
+
17,self_attn.k_proj,0.0002049842,0.05000,1.442
|
| 123 |
+
17,self_attn.v_proj,0.0001110038,0.05000,1.449
|
| 124 |
+
17,self_attn.o_proj,0.0001096319,0.05000,0.456
|
| 125 |
+
17,mlp.gate_proj,0.0025995242,0.05000,0.938
|
| 126 |
+
17,mlp.up_proj,0.0026509292,0.05000,0.941
|
| 127 |
+
17,mlp.down_proj,0.0002133363,0.05000,2.545
|
| 128 |
+
18,self_attn.q_proj,0.0007654526,0.05000,1.373
|
| 129 |
+
18,self_attn.v_proj,0.0000810104,0.05000,1.386
|
| 130 |
+
18,self_attn.k_proj,0.0001118958,0.05000,1.415
|
| 131 |
+
18,self_attn.o_proj,0.0001209933,0.05000,0.437
|
| 132 |
+
18,mlp.gate_proj,0.0026745959,0.05000,0.988
|
| 133 |
+
18,mlp.up_proj,0.0026052926,0.05000,0.993
|
| 134 |
+
18,mlp.down_proj,0.0001979110,0.05000,2.526
|
| 135 |
+
19,self_attn.q_proj,0.0008727050,0.05000,1.484
|
| 136 |
+
19,self_attn.v_proj,0.0000879996,0.05000,1.511
|
| 137 |
+
19,self_attn.k_proj,0.0001525676,0.05000,1.517
|
| 138 |
+
19,self_attn.o_proj,0.0001613215,0.05000,0.445
|
| 139 |
+
19,mlp.gate_proj,0.0025457988,0.05000,0.981
|
| 140 |
+
19,mlp.up_proj,0.0026727604,0.05000,0.990
|
| 141 |
+
19,mlp.down_proj,0.0001955881,0.05000,2.512
|
| 142 |
+
20,self_attn.q_proj,0.0013551767,0.05000,1.482
|
| 143 |
+
20,self_attn.v_proj,0.0001676866,0.05000,1.505
|
| 144 |
+
20,self_attn.k_proj,0.0001561945,0.05000,1.517
|
| 145 |
+
20,self_attn.o_proj,0.0001528362,0.05000,0.437
|
| 146 |
+
20,mlp.gate_proj,0.0026263935,0.05000,0.962
|
| 147 |
+
20,mlp.up_proj,0.0027138740,0.05000,0.977
|
| 148 |
+
20,mlp.down_proj,0.0002239240,0.05000,2.573
|
| 149 |
+
21,self_attn.v_proj,0.0001106188,0.05000,1.444
|
| 150 |
+
21,self_attn.k_proj,0.0001505467,0.05000,1.450
|
| 151 |
+
21,self_attn.q_proj,0.0009562430,0.05000,1.456
|
| 152 |
+
21,self_attn.o_proj,0.0001582452,0.05000,0.433
|
| 153 |
+
21,mlp.gate_proj,0.0027915318,0.05000,0.984
|
| 154 |
+
21,mlp.up_proj,0.0026419421,0.05000,0.997
|
| 155 |
+
21,mlp.down_proj,0.0002030738,0.05000,2.577
|
| 156 |
+
22,self_attn.q_proj,0.0011346004,0.05000,1.464
|
| 157 |
+
22,self_attn.v_proj,0.0001487743,0.05000,1.496
|
| 158 |
+
22,self_attn.k_proj,0.0001729702,0.05000,1.499
|
| 159 |
+
22,self_attn.o_proj,0.0001293588,0.05000,0.436
|
| 160 |
+
22,mlp.gate_proj,0.0027813923,0.05000,0.973
|
| 161 |
+
22,mlp.up_proj,0.0027358021,0.05000,0.979
|
| 162 |
+
22,mlp.down_proj,0.0002278643,0.05000,2.698
|
| 163 |
+
23,self_attn.q_proj,0.0011565852,0.05000,1.424
|
| 164 |
+
23,self_attn.k_proj,0.0001676210,0.05000,1.443
|
| 165 |
+
23,self_attn.v_proj,0.0000908016,0.05000,1.451
|
| 166 |
+
23,self_attn.o_proj,0.0001457047,0.05000,0.434
|
| 167 |
+
23,mlp.gate_proj,0.0028181392,0.05000,0.982
|
| 168 |
+
23,mlp.up_proj,0.0029544076,0.05000,0.990
|
| 169 |
+
23,mlp.down_proj,0.0002631970,0.05000,2.507
|
| 170 |
+
24,self_attn.k_proj,0.0001165944,0.05000,1.458
|
| 171 |
+
24,self_attn.v_proj,0.0001046166,0.05000,1.466
|
| 172 |
+
24,self_attn.q_proj,0.0008574808,0.05000,1.481
|
| 173 |
+
24,self_attn.o_proj,0.0001873872,0.05000,0.441
|
| 174 |
+
24,mlp.up_proj,0.0029398206,0.05000,0.966
|
| 175 |
+
24,mlp.gate_proj,0.0028506532,0.05000,0.975
|
| 176 |
+
24,mlp.down_proj,0.0002885577,0.05000,2.585
|
| 177 |
+
25,self_attn.q_proj,0.0012196636,0.05000,1.466
|
| 178 |
+
25,self_attn.k_proj,0.0001167471,0.05000,1.479
|
| 179 |
+
25,self_attn.v_proj,0.0001735481,0.05000,1.490
|
| 180 |
+
25,self_attn.o_proj,0.0001489266,0.05000,0.451
|
| 181 |
+
25,mlp.gate_proj,0.0032135869,0.05000,0.950
|
| 182 |
+
25,mlp.up_proj,0.0032790664,0.05000,0.953
|
| 183 |
+
25,mlp.down_proj,0.0003838621,0.05000,2.757
|
| 184 |
+
26,self_attn.v_proj,0.0001645723,0.05000,1.358
|
| 185 |
+
26,self_attn.q_proj,0.0009637461,0.05000,1.371
|
| 186 |
+
26,self_attn.k_proj,0.0001212483,0.05000,1.376
|
| 187 |
+
26,self_attn.o_proj,0.0002090202,0.05000,0.427
|
| 188 |
+
26,mlp.up_proj,0.0038802780,0.05000,0.938
|
| 189 |
+
26,mlp.gate_proj,0.0036459990,0.05000,0.942
|
| 190 |
+
26,mlp.down_proj,0.0005390834,0.05000,2.532
|
| 191 |
+
27,self_attn.k_proj,0.0001402346,0.05000,1.335
|
| 192 |
+
27,self_attn.v_proj,0.0002450933,0.05000,1.359
|
| 193 |
+
27,self_attn.q_proj,0.0016638996,0.05000,1.369
|
| 194 |
+
27,self_attn.o_proj,0.0003133276,0.05000,0.422
|
| 195 |
+
27,mlp.up_proj,0.0040160986,0.05000,0.946
|
| 196 |
+
27,mlp.gate_proj,0.0039604067,0.05000,0.953
|
| 197 |
+
27,mlp.down_proj,0.0006700770,0.05000,2.505
|
| 198 |
+
28,self_attn.v_proj,0.0002193391,0.05000,1.531
|
| 199 |
+
28,self_attn.q_proj,0.0011876772,0.05000,1.540
|
| 200 |
+
28,self_attn.k_proj,0.0001271059,0.05000,1.549
|
| 201 |
+
28,self_attn.o_proj,0.0002864604,0.05000,0.440
|
| 202 |
+
28,mlp.up_proj,0.0045377837,0.05000,0.981
|
| 203 |
+
28,mlp.gate_proj,0.0045106539,0.05000,0.991
|
| 204 |
+
28,mlp.down_proj,0.0007840230,0.05000,2.636
|
| 205 |
+
29,self_attn.q_proj,0.0011711047,0.05000,1.465
|
| 206 |
+
29,self_attn.k_proj,0.0001328981,0.05000,1.484
|
| 207 |
+
29,self_attn.v_proj,0.0002036485,0.05000,1.487
|
| 208 |
+
29,self_attn.o_proj,0.0002445588,0.05000,0.433
|
| 209 |
+
29,mlp.gate_proj,0.0053584442,0.05000,0.863
|
| 210 |
+
29,mlp.up_proj,0.0054983478,0.05000,0.867
|
| 211 |
+
29,mlp.down_proj,0.0010751190,0.05000,2.710
|
| 212 |
+
30,self_attn.q_proj,0.0016243049,0.05000,1.236
|
| 213 |
+
30,self_attn.k_proj,0.0001383619,0.05000,1.274
|
| 214 |
+
30,self_attn.v_proj,0.0004685805,0.05000,1.281
|
| 215 |
+
30,self_attn.o_proj,0.0002138880,0.05000,0.422
|
| 216 |
+
30,mlp.up_proj,0.0074140683,0.05000,0.931
|
| 217 |
+
30,mlp.gate_proj,0.0067897284,0.05000,0.931
|
| 218 |
+
30,mlp.down_proj,0.0253920822,0.05000,2.541
|
| 219 |
+
31,self_attn.q_proj,0.0018998680,0.05000,1.392
|
| 220 |
+
31,self_attn.k_proj,0.0001888821,0.05000,1.397
|
| 221 |
+
31,self_attn.v_proj,0.0005093985,0.05000,1.399
|
| 222 |
+
31,self_attn.o_proj,0.0004122503,0.05000,0.442
|
| 223 |
+
31,mlp.gate_proj,0.0078921620,0.05000,1.006
|
| 224 |
+
31,mlp.up_proj,0.0090096026,0.05000,1.026
|
| 225 |
+
31,mlp.down_proj,0.0036010571,0.05000,2.663
|
| 226 |
+
32,self_attn.v_proj,0.0013319228,0.05000,1.439
|
| 227 |
+
32,self_attn.k_proj,0.0002597295,0.05000,1.439
|
| 228 |
+
32,self_attn.q_proj,0.0029558921,0.05000,1.455
|
| 229 |
+
32,self_attn.o_proj,0.0003896669,0.05000,0.489
|
| 230 |
+
32,mlp.gate_proj,0.0077764657,0.05000,0.945
|
| 231 |
+
32,mlp.up_proj,0.0088612360,0.05000,0.960
|
| 232 |
+
32,mlp.down_proj,0.0033728618,0.05000,2.690
|
| 233 |
+
33,self_attn.q_proj,0.0028402272,0.05000,1.341
|
| 234 |
+
33,self_attn.k_proj,0.0002332372,0.05000,1.346
|
| 235 |
+
33,self_attn.v_proj,0.0027896581,0.05000,1.362
|
| 236 |
+
33,self_attn.o_proj,0.0004359603,0.05000,0.474
|
| 237 |
+
33,mlp.gate_proj,0.0072181967,0.05000,0.921
|
| 238 |
+
33,mlp.up_proj,0.0090274869,0.05000,0.927
|
| 239 |
+
33,mlp.down_proj,0.0057725686,0.05000,2.693
|
| 240 |
+
34,self_attn.q_proj,0.0011860414,0.05000,1.332
|
| 241 |
+
34,self_attn.v_proj,0.0003992968,0.05000,1.340
|
| 242 |
+
34,self_attn.k_proj,0.0001209296,0.05000,1.356
|
| 243 |
+
34,self_attn.o_proj,0.0005546022,0.05000,0.431
|
| 244 |
+
34,mlp.gate_proj,0.0077909217,0.05000,1.025
|
| 245 |
+
34,mlp.up_proj,0.0090878079,0.05000,1.049
|
| 246 |
+
34,mlp.down_proj,0.0041094714,0.05000,2.748
|
| 247 |
+
35,self_attn.q_proj,0.0010943316,0.05000,1.456
|
| 248 |
+
35,self_attn.v_proj,0.0003437671,0.05000,1.478
|
| 249 |
+
35,self_attn.k_proj,0.0001138098,0.05000,1.483
|
| 250 |
+
35,self_attn.o_proj,0.0005312666,0.05000,0.440
|
| 251 |
+
35,mlp.up_proj,0.0124718128,0.05000,0.965
|
| 252 |
+
35,mlp.gate_proj,0.0117675475,0.05000,0.969
|
| 253 |
+
35,mlp.down_proj,0.0163082102,0.05000,2.571
|
quant_meta.json
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"model_id": "AIDC-AI/Ovis2-4B",
|
| 3 |
+
"family": "ovis2",
|
| 4 |
+
"method": "gptq",
|
| 5 |
+
"bits": 4,
|
| 6 |
+
"group_size": 128,
|
| 7 |
+
"quant_time_s": 231.2,
|
| 8 |
+
"n_calib": 32
|
| 9 |
+
}
|
quantize_config.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"bits": 4,
|
| 3 |
+
"group_size": 128,
|
| 4 |
+
"desc_act": false,
|
| 5 |
+
"lm_head": false,
|
| 6 |
+
"quant_method": "gptq",
|
| 7 |
+
"checkpoint_format": "gptq",
|
| 8 |
+
"pack_dtype": "int32",
|
| 9 |
+
"meta": {
|
| 10 |
+
"quantizer": [
|
| 11 |
+
"gptqmodel:5.8.0"
|
| 12 |
+
],
|
| 13 |
+
"uri": "https://github.com/modelcloud/gptqmodel",
|
| 14 |
+
"damp_percent": 0.05,
|
| 15 |
+
"damp_auto_increment": 0.01,
|
| 16 |
+
"static_groups": false,
|
| 17 |
+
"true_sequential": true,
|
| 18 |
+
"mse": 0.0,
|
| 19 |
+
"gptaq": null,
|
| 20 |
+
"act_group_aware": true,
|
| 21 |
+
"failsafe": {
|
| 22 |
+
"strategy": "rtn",
|
| 23 |
+
"threshold": "0.5%",
|
| 24 |
+
"smooth": null
|
| 25 |
+
},
|
| 26 |
+
"offload_to_disk": true,
|
| 27 |
+
"offload_to_disk_path": "./gptqmodel_offload/lzyvwgbq-jskkldgv/",
|
| 28 |
+
"pack_impl": "cpu",
|
| 29 |
+
"mock_quantization": false,
|
| 30 |
+
"gc_mode": "interval",
|
| 31 |
+
"wait_for_submodule_finalizers": false,
|
| 32 |
+
"auto_forward_data_parallel": true,
|
| 33 |
+
"hessian": {
|
| 34 |
+
"chunk_size": null,
|
| 35 |
+
"chunk_bytes": null,
|
| 36 |
+
"staging_dtype": "float32"
|
| 37 |
+
},
|
| 38 |
+
"vram_strategy": "exclusive"
|
| 39 |
+
},
|
| 40 |
+
"sym": true,
|
| 41 |
+
"format": "gptq"
|
| 42 |
+
}
|