Upload 8 files
Browse files- .gitattributes +38 -0
- README.md +13 -0
- app.py +129 -0
- configuration_llava_qwen2.py +202 -0
- demo_1.jpg +3 -0
- demo_2.jpeg +0 -0
- modeling_llava_qwen2.py +0 -0
- requirements.txt +8 -0
.gitattributes
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
| 13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
| 29 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 30 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 31 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 32 |
+
*.xz 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 |
+
baklava.png filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
bee.jpg filter=lfs diff=lfs merge=lfs -text
|
| 38 |
+
demo_1.jpg filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: nanoLLaVA-1.5
|
| 3 |
+
emoji: 🚀
|
| 4 |
+
colorFrom: yellow
|
| 5 |
+
colorTo: green
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 4.22.0
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
license: apache-2.0
|
| 11 |
+
---
|
| 12 |
+
|
| 13 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer, StoppingCriteria
|
| 4 |
+
from modeling_llava_qwen2 import LlavaQwen2ForCausalLM
|
| 5 |
+
from threading import Thread
|
| 6 |
+
import re
|
| 7 |
+
import time
|
| 8 |
+
from PIL import Image
|
| 9 |
+
import torch
|
| 10 |
+
import spaces
|
| 11 |
+
import subprocess
|
| 12 |
+
subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
|
| 13 |
+
|
| 14 |
+
torch.set_default_device('cuda')
|
| 15 |
+
|
| 16 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 17 |
+
'qnguyen3/nanoLLaVA-1.5',
|
| 18 |
+
trust_remote_code=True)
|
| 19 |
+
|
| 20 |
+
model = LlavaQwen2ForCausalLM.from_pretrained(
|
| 21 |
+
'qnguyen3/nanoLLaVA-1.5',
|
| 22 |
+
torch_dtype=torch.float16,
|
| 23 |
+
attn_implementation="flash_attention_2",
|
| 24 |
+
trust_remote_code=True)
|
| 25 |
+
|
| 26 |
+
model.to('cuda')
|
| 27 |
+
|
| 28 |
+
class KeywordsStoppingCriteria(StoppingCriteria):
|
| 29 |
+
def __init__(self, keywords, tokenizer, input_ids):
|
| 30 |
+
self.keywords = keywords
|
| 31 |
+
self.keyword_ids = []
|
| 32 |
+
self.max_keyword_len = 0
|
| 33 |
+
for keyword in keywords:
|
| 34 |
+
cur_keyword_ids = tokenizer(keyword).input_ids
|
| 35 |
+
if len(cur_keyword_ids) > 1 and cur_keyword_ids[0] == tokenizer.bos_token_id:
|
| 36 |
+
cur_keyword_ids = cur_keyword_ids[1:]
|
| 37 |
+
if len(cur_keyword_ids) > self.max_keyword_len:
|
| 38 |
+
self.max_keyword_len = len(cur_keyword_ids)
|
| 39 |
+
self.keyword_ids.append(torch.tensor(cur_keyword_ids))
|
| 40 |
+
self.tokenizer = tokenizer
|
| 41 |
+
self.start_len = input_ids.shape[1]
|
| 42 |
+
|
| 43 |
+
def call_for_batch(self, output_ids: torch.LongTensor, scores: torch.FloatTensor, **kwargs) -> bool:
|
| 44 |
+
offset = min(output_ids.shape[1] - self.start_len, self.max_keyword_len)
|
| 45 |
+
self.keyword_ids = [keyword_id.to(output_ids.device) for keyword_id in self.keyword_ids]
|
| 46 |
+
for keyword_id in self.keyword_ids:
|
| 47 |
+
truncated_output_ids = output_ids[0, -keyword_id.shape[0]:]
|
| 48 |
+
if torch.equal(truncated_output_ids, keyword_id):
|
| 49 |
+
return True
|
| 50 |
+
outputs = self.tokenizer.batch_decode(output_ids[:, -offset:], skip_special_tokens=True)[0]
|
| 51 |
+
for keyword in self.keywords:
|
| 52 |
+
if keyword in outputs:
|
| 53 |
+
return True
|
| 54 |
+
return False
|
| 55 |
+
|
| 56 |
+
def __call__(self, output_ids: torch.LongTensor, scores: torch.FloatTensor, **kwargs) -> bool:
|
| 57 |
+
outputs = []
|
| 58 |
+
for i in range(output_ids.shape[0]):
|
| 59 |
+
outputs.append(self.call_for_batch(output_ids[i].unsqueeze(0), scores))
|
| 60 |
+
return all(outputs)
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
@spaces.GPU
|
| 64 |
+
def bot_streaming(message, history):
|
| 65 |
+
messages = []
|
| 66 |
+
if message["files"]:
|
| 67 |
+
image = message["files"][-1]["path"]
|
| 68 |
+
else:
|
| 69 |
+
for i, hist in enumerate(history):
|
| 70 |
+
if type(hist[0])==tuple:
|
| 71 |
+
image = hist[0][0]
|
| 72 |
+
image_turn = i
|
| 73 |
+
|
| 74 |
+
if len(history) > 0 and image is not None:
|
| 75 |
+
messages.append({"role": "user", "content": f'<image>\n{history[1][0]}'})
|
| 76 |
+
messages.append({"role": "assistant", "content": history[1][1] })
|
| 77 |
+
for human, assistant in history[2:]:
|
| 78 |
+
messages.append({"role": "user", "content": human })
|
| 79 |
+
messages.append({"role": "assistant", "content": assistant })
|
| 80 |
+
messages.append({"role": "user", "content": message['text']})
|
| 81 |
+
elif len(history) > 0 and image is None:
|
| 82 |
+
for human, assistant in history:
|
| 83 |
+
messages.append({"role": "user", "content": human })
|
| 84 |
+
messages.append({"role": "assistant", "content": assistant })
|
| 85 |
+
messages.append({"role": "user", "content": message['text']})
|
| 86 |
+
elif len(history) == 0 and image is not None:
|
| 87 |
+
messages.append({"role": "user", "content": f"<image>\n{message['text']}"})
|
| 88 |
+
elif len(history) == 0 and image is None:
|
| 89 |
+
messages.append({"role": "user", "content": message['text'] })
|
| 90 |
+
|
| 91 |
+
# if image is None:
|
| 92 |
+
# gr.Error("You need to upload an image for LLaVA to work.")
|
| 93 |
+
image = Image.open(image).convert("RGB")
|
| 94 |
+
text = tokenizer.apply_chat_template(
|
| 95 |
+
messages,
|
| 96 |
+
tokenize=False,
|
| 97 |
+
add_generation_prompt=True)
|
| 98 |
+
text_chunks = [tokenizer(chunk).input_ids for chunk in text.split('<image>')]
|
| 99 |
+
input_ids = torch.tensor(text_chunks[0] + [-200] + text_chunks[1], dtype=torch.long).unsqueeze(0)
|
| 100 |
+
stop_str = '<|im_end|>'
|
| 101 |
+
keywords = [stop_str]
|
| 102 |
+
stopping_criteria = KeywordsStoppingCriteria(keywords, tokenizer, input_ids)
|
| 103 |
+
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
| 104 |
+
|
| 105 |
+
image_tensor = model.process_images([image], model.config).to(dtype=model.dtype)
|
| 106 |
+
generation_kwargs = dict(input_ids=input_ids.to('cuda'),
|
| 107 |
+
images=image_tensor.to('cuda'),
|
| 108 |
+
streamer=streamer, max_new_tokens=512,
|
| 109 |
+
stopping_criteria=[stopping_criteria], temperature=0.01)
|
| 110 |
+
generated_text = ""
|
| 111 |
+
thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
| 112 |
+
thread.start()
|
| 113 |
+
text_prompt =f"<|im_start|>user\n{message['text']}<|im_end|>"
|
| 114 |
+
|
| 115 |
+
buffer = ""
|
| 116 |
+
for new_text in streamer:
|
| 117 |
+
|
| 118 |
+
buffer += new_text
|
| 119 |
+
|
| 120 |
+
generated_text_without_prompt = buffer[:]
|
| 121 |
+
time.sleep(0.04)
|
| 122 |
+
yield generated_text_without_prompt
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
demo = gr.ChatInterface(fn=bot_streaming, title="🚀nanoLLaVA-1.5", examples=[{"text": "Who is this guy?", "files":["./demo_1.jpg"]},
|
| 126 |
+
{"text": "What does the text say?", "files":["./demo_2.jpeg"]}],
|
| 127 |
+
description="Try [nanoLLaVA](https://huggingface.co/qnguyen3/nanoLLaVA-1.5) in this demo. Built on top of [Quyen-SE-v0.1](https://huggingface.co/vilm/Quyen-SE-v0.1) (Qwen1.5-0.5B) and [Google SigLIP-400M](https://huggingface.co/google/siglip-so400m-patch14-384). Upload an image and start chatting about it, or simply try one of the examples below. If you don't upload an image, you will receive an error.",
|
| 128 |
+
stop_btn="Stop Generation", multimodal=True)
|
| 129 |
+
demo.queue().launch()
|
configuration_llava_qwen2.py
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# coding=utf-8
|
| 2 |
+
# Copyright 2024 The Qwen team, Alibaba Group and the HuggingFace Inc. team. All rights reserved.
|
| 3 |
+
#
|
| 4 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 5 |
+
# you may not use this file except in compliance with the License.
|
| 6 |
+
# You may obtain a copy of the License at
|
| 7 |
+
#
|
| 8 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 9 |
+
#
|
| 10 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 11 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 12 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 13 |
+
# See the License for the specific language governing permissions and
|
| 14 |
+
# limitations under the License.
|
| 15 |
+
""" Qwen2 model configuration"""
|
| 16 |
+
|
| 17 |
+
from transformers.configuration_utils import PretrainedConfig
|
| 18 |
+
from transformers.utils import logging
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
logger = logging.get_logger(__name__)
|
| 22 |
+
|
| 23 |
+
QWEN2_PRETRAINED_CONFIG_ARCHIVE_MAP = {
|
| 24 |
+
"Qwen/Qwen2-7B-beta": "https://huggingface.co/Qwen/Qwen2-7B-beta/resolve/main/config.json",
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
class Qwen2Config(PretrainedConfig):
|
| 29 |
+
r"""
|
| 30 |
+
This is the configuration class to store the configuration of a [`Qwen2Model`]. It is used to instantiate a
|
| 31 |
+
Qwen2 model according to the specified arguments, defining the model architecture. Instantiating a configuration
|
| 32 |
+
with the defaults will yield a similar configuration to that of
|
| 33 |
+
Qwen2-7B-beta [Qwen/Qwen2-7B-beta](https://huggingface.co/Qwen/Qwen2-7B-beta).
|
| 34 |
+
|
| 35 |
+
Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
|
| 36 |
+
documentation from [`PretrainedConfig`] for more information.
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
Args:
|
| 40 |
+
vocab_size (`int`, *optional*, defaults to 151936):
|
| 41 |
+
Vocabulary size of the Qwen2 model. Defines the number of different tokens that can be represented by the
|
| 42 |
+
`inputs_ids` passed when calling [`Qwen2Model`]
|
| 43 |
+
hidden_size (`int`, *optional*, defaults to 4096):
|
| 44 |
+
Dimension of the hidden representations.
|
| 45 |
+
intermediate_size (`int`, *optional*, defaults to 22016):
|
| 46 |
+
Dimension of the MLP representations.
|
| 47 |
+
num_hidden_layers (`int`, *optional*, defaults to 32):
|
| 48 |
+
Number of hidden layers in the Transformer encoder.
|
| 49 |
+
num_attention_heads (`int`, *optional*, defaults to 32):
|
| 50 |
+
Number of attention heads for each attention layer in the Transformer encoder.
|
| 51 |
+
num_key_value_heads (`int`, *optional*, defaults to 32):
|
| 52 |
+
This is the number of key_value heads that should be used to implement Grouped Query Attention. If
|
| 53 |
+
`num_key_value_heads=num_attention_heads`, the model will use Multi Head Attention (MHA), if
|
| 54 |
+
`num_key_value_heads=1 the model will use Multi Query Attention (MQA) otherwise GQA is used. When
|
| 55 |
+
converting a multi-head checkpoint to a GQA checkpoint, each group key and value head should be constructed
|
| 56 |
+
by meanpooling all the original heads within that group. For more details checkout [this
|
| 57 |
+
paper](https://arxiv.org/pdf/2305.13245.pdf). If it is not specified, will default to `32`.
|
| 58 |
+
hidden_act (`str` or `function`, *optional*, defaults to `"silu"`):
|
| 59 |
+
The non-linear activation function (function or string) in the decoder.
|
| 60 |
+
max_position_embeddings (`int`, *optional*, defaults to 32768):
|
| 61 |
+
The maximum sequence length that this model might ever be used with.
|
| 62 |
+
initializer_range (`float`, *optional*, defaults to 0.02):
|
| 63 |
+
The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
|
| 64 |
+
rms_norm_eps (`float`, *optional*, defaults to 1e-06):
|
| 65 |
+
The epsilon used by the rms normalization layers.
|
| 66 |
+
use_cache (`bool`, *optional*, defaults to `True`):
|
| 67 |
+
Whether or not the model should return the last key/values attentions (not used by all models). Only
|
| 68 |
+
relevant if `config.is_decoder=True`.
|
| 69 |
+
tie_word_embeddings (`bool`, *optional*, defaults to `False`):
|
| 70 |
+
Whether the model's input and output word embeddings should be tied.
|
| 71 |
+
rope_theta (`float`, *optional*, defaults to 10000.0):
|
| 72 |
+
The base period of the RoPE embeddings.
|
| 73 |
+
use_sliding_window (`bool`, *optional*, defaults to `False`):
|
| 74 |
+
Whether to use sliding window attention.
|
| 75 |
+
sliding_window (`int`, *optional*, defaults to 4096):
|
| 76 |
+
Sliding window attention (SWA) window size. If not specified, will default to `4096`.
|
| 77 |
+
max_window_layers (`int`, *optional*, defaults to 28):
|
| 78 |
+
The number of layers that use SWA (Sliding Window Attention). The bottom layers use SWA while the top use full attention.
|
| 79 |
+
attention_dropout (`float`, *optional*, defaults to 0.0):
|
| 80 |
+
The dropout ratio for the attention probabilities.
|
| 81 |
+
|
| 82 |
+
```python
|
| 83 |
+
>>> from transformers import Qwen2Model, Qwen2Config
|
| 84 |
+
|
| 85 |
+
>>> # Initializing a Qwen2 style configuration
|
| 86 |
+
>>> configuration = Qwen2Config()
|
| 87 |
+
|
| 88 |
+
>>> # Initializing a model from the Qwen2-7B style configuration
|
| 89 |
+
>>> model = Qwen2Model(configuration)
|
| 90 |
+
|
| 91 |
+
>>> # Accessing the model configuration
|
| 92 |
+
>>> configuration = model.config
|
| 93 |
+
```"""
|
| 94 |
+
|
| 95 |
+
model_type = "qwen2"
|
| 96 |
+
keys_to_ignore_at_inference = ["past_key_values"]
|
| 97 |
+
|
| 98 |
+
def __init__(
|
| 99 |
+
self,
|
| 100 |
+
vocab_size=151936,
|
| 101 |
+
hidden_size=4096,
|
| 102 |
+
intermediate_size=22016,
|
| 103 |
+
num_hidden_layers=32,
|
| 104 |
+
num_attention_heads=32,
|
| 105 |
+
num_key_value_heads=32,
|
| 106 |
+
hidden_act="silu",
|
| 107 |
+
max_position_embeddings=32768,
|
| 108 |
+
initializer_range=0.02,
|
| 109 |
+
rms_norm_eps=1e-6,
|
| 110 |
+
use_cache=True,
|
| 111 |
+
tie_word_embeddings=False,
|
| 112 |
+
rope_theta=10000.0,
|
| 113 |
+
use_sliding_window=False,
|
| 114 |
+
sliding_window=4096,
|
| 115 |
+
max_window_layers=28,
|
| 116 |
+
attention_dropout=0.0,
|
| 117 |
+
**kwargs,
|
| 118 |
+
):
|
| 119 |
+
self.vocab_size = vocab_size
|
| 120 |
+
self.max_position_embeddings = max_position_embeddings
|
| 121 |
+
self.hidden_size = hidden_size
|
| 122 |
+
self.intermediate_size = intermediate_size
|
| 123 |
+
self.num_hidden_layers = num_hidden_layers
|
| 124 |
+
self.num_attention_heads = num_attention_heads
|
| 125 |
+
self.use_sliding_window = use_sliding_window
|
| 126 |
+
self.sliding_window = sliding_window
|
| 127 |
+
self.max_window_layers = max_window_layers
|
| 128 |
+
|
| 129 |
+
# for backward compatibility
|
| 130 |
+
if num_key_value_heads is None:
|
| 131 |
+
num_key_value_heads = num_attention_heads
|
| 132 |
+
|
| 133 |
+
self.num_key_value_heads = num_key_value_heads
|
| 134 |
+
self.hidden_act = hidden_act
|
| 135 |
+
self.initializer_range = initializer_range
|
| 136 |
+
self.rms_norm_eps = rms_norm_eps
|
| 137 |
+
self.use_cache = use_cache
|
| 138 |
+
self.rope_theta = rope_theta
|
| 139 |
+
self.attention_dropout = attention_dropout
|
| 140 |
+
|
| 141 |
+
super().__init__(
|
| 142 |
+
tie_word_embeddings=tie_word_embeddings,
|
| 143 |
+
**kwargs,
|
| 144 |
+
)
|
| 145 |
+
|
| 146 |
+
from typing import Union
|
| 147 |
+
from transformers import PretrainedConfig
|
| 148 |
+
import os
|
| 149 |
+
|
| 150 |
+
|
| 151 |
+
class SigLipVisionConfig(PretrainedConfig):
|
| 152 |
+
model_type = "siglip_vision_model"
|
| 153 |
+
|
| 154 |
+
def __init__(
|
| 155 |
+
self,
|
| 156 |
+
hidden_size=1152,
|
| 157 |
+
image_mean=(0.5, 0.5, 0.5),
|
| 158 |
+
intermediate_size=4304,
|
| 159 |
+
num_hidden_layers=27,
|
| 160 |
+
num_attention_heads=16,
|
| 161 |
+
num_channels=3,
|
| 162 |
+
image_size=384,
|
| 163 |
+
patch_size=14,
|
| 164 |
+
hidden_act="gelu_pytorch_tanh",
|
| 165 |
+
layer_norm_eps=1e-6,
|
| 166 |
+
attention_dropout=0.0,
|
| 167 |
+
**kwargs,
|
| 168 |
+
):
|
| 169 |
+
super().__init__(**kwargs)
|
| 170 |
+
|
| 171 |
+
self.hidden_size = hidden_size
|
| 172 |
+
self.intermediate_size = intermediate_size
|
| 173 |
+
self.num_hidden_layers = num_hidden_layers
|
| 174 |
+
self.num_attention_heads = num_attention_heads
|
| 175 |
+
self.num_channels = num_channels
|
| 176 |
+
self.patch_size = patch_size
|
| 177 |
+
self.image_size = image_size
|
| 178 |
+
self.attention_dropout = attention_dropout
|
| 179 |
+
self.layer_norm_eps = layer_norm_eps
|
| 180 |
+
self.hidden_act = hidden_act
|
| 181 |
+
self.image_mean = image_mean
|
| 182 |
+
|
| 183 |
+
@classmethod
|
| 184 |
+
def from_pretrained(cls, pretrained_model_name_or_path: Union[str, os.PathLike], **kwargs) -> "PretrainedConfig":
|
| 185 |
+
cls._set_token_in_kwargs(kwargs)
|
| 186 |
+
|
| 187 |
+
config_dict, kwargs = cls.get_config_dict(pretrained_model_name_or_path, **kwargs)
|
| 188 |
+
|
| 189 |
+
# get the vision config dict if we are loading from SigLipConfig
|
| 190 |
+
if config_dict.get("model_type") == "siglip":
|
| 191 |
+
config_dict = config_dict["vision_config"]
|
| 192 |
+
|
| 193 |
+
if "model_type" in config_dict and hasattr(cls, "model_type") and config_dict["model_type"] != cls.model_type:
|
| 194 |
+
logger.warning(
|
| 195 |
+
f"You are using a model of type {config_dict['model_type']} to instantiate a model of type "
|
| 196 |
+
f"{cls.model_type}. This is not supported for all configurations of models and can yield errors."
|
| 197 |
+
)
|
| 198 |
+
|
| 199 |
+
return cls.from_dict(config_dict, **kwargs)
|
| 200 |
+
|
| 201 |
+
class LlavaQwen2Config(Qwen2Config):
|
| 202 |
+
model_type = "llava-qwen2"
|
demo_1.jpg
ADDED
|
Git LFS Details
|
demo_2.jpeg
ADDED
|
modeling_llava_qwen2.py
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
git+https://github.com/huggingface/transformers.git
|
| 3 |
+
spaces
|
| 4 |
+
pillow
|
| 5 |
+
accelerate
|
| 6 |
+
pypandoc
|
| 7 |
+
fastapi
|
| 8 |
+
wheel
|