Image-Text-to-Text
Transformers
PyTorch
multilingual
internvl_chat
feature-extraction
internvl
custom_code
conversational
Instructions to use OpenGVLab/InternVL2-40B-AWQ with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use OpenGVLab/InternVL2-40B-AWQ with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="OpenGVLab/InternVL2-40B-AWQ", trust_remote_code=True) messages = [ { "role": "user", "content": [ {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"}, {"type": "text", "text": "What animal is on the candy?"} ] }, ] pipe(text=messages)# Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("OpenGVLab/InternVL2-40B-AWQ", trust_remote_code=True, dtype="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps
- vLLM
How to use OpenGVLab/InternVL2-40B-AWQ with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "OpenGVLab/InternVL2-40B-AWQ" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "OpenGVLab/InternVL2-40B-AWQ", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'Use Docker
docker model run hf.co/OpenGVLab/InternVL2-40B-AWQ
- SGLang
How to use OpenGVLab/InternVL2-40B-AWQ with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "OpenGVLab/InternVL2-40B-AWQ" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "OpenGVLab/InternVL2-40B-AWQ", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "OpenGVLab/InternVL2-40B-AWQ" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "OpenGVLab/InternVL2-40B-AWQ", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }' - Docker Model Runner
How to use OpenGVLab/InternVL2-40B-AWQ with Docker Model Runner:
docker model run hf.co/OpenGVLab/InternVL2-40B-AWQ
Upload folder using huggingface_hub
Browse files
modeling_internvl_chat.py
CHANGED
|
@@ -33,6 +33,7 @@ def version_cmp(v1, v2, op='eq'):
|
|
| 33 |
class InternVLChatModel(PreTrainedModel):
|
| 34 |
config_class = InternVLChatConfig
|
| 35 |
main_input_name = 'pixel_values'
|
|
|
|
| 36 |
_supports_flash_attn_2 = True
|
| 37 |
_no_split_modules = ['InternVisionModel', 'LlamaDecoderLayer']
|
| 38 |
|
|
@@ -97,7 +98,7 @@ class InternVLChatModel(PreTrainedModel):
|
|
| 97 |
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
| 98 |
|
| 99 |
image_flags = image_flags.squeeze(-1)
|
| 100 |
-
input_embeds = self.language_model.get_input_embeddings()(input_ids)
|
| 101 |
|
| 102 |
vit_embeds = self.extract_feature(pixel_values)
|
| 103 |
vit_embeds = vit_embeds[image_flags == 1]
|
|
@@ -230,8 +231,8 @@ class InternVLChatModel(PreTrainedModel):
|
|
| 230 |
|
| 231 |
tokenizer.padding_side = 'left'
|
| 232 |
model_inputs = tokenizer(queries, return_tensors='pt', padding=True)
|
| 233 |
-
input_ids = model_inputs['input_ids'].
|
| 234 |
-
attention_mask = model_inputs['attention_mask'].
|
| 235 |
eos_token_id = tokenizer.convert_tokens_to_ids(template.sep)
|
| 236 |
generation_config['eos_token_id'] = eos_token_id
|
| 237 |
generation_output = self.generate(
|
|
@@ -279,8 +280,8 @@ class InternVLChatModel(PreTrainedModel):
|
|
| 279 |
query = query.replace('<image>', image_tokens, 1)
|
| 280 |
|
| 281 |
model_inputs = tokenizer(query, return_tensors='pt')
|
| 282 |
-
input_ids = model_inputs['input_ids'].
|
| 283 |
-
attention_mask = model_inputs['attention_mask'].
|
| 284 |
generation_config['eos_token_id'] = eos_token_id
|
| 285 |
generation_output = self.generate(
|
| 286 |
pixel_values=pixel_values,
|
|
|
|
| 33 |
class InternVLChatModel(PreTrainedModel):
|
| 34 |
config_class = InternVLChatConfig
|
| 35 |
main_input_name = 'pixel_values'
|
| 36 |
+
base_model_prefix = 'language_model'
|
| 37 |
_supports_flash_attn_2 = True
|
| 38 |
_no_split_modules = ['InternVisionModel', 'LlamaDecoderLayer']
|
| 39 |
|
|
|
|
| 98 |
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
| 99 |
|
| 100 |
image_flags = image_flags.squeeze(-1)
|
| 101 |
+
input_embeds = self.language_model.get_input_embeddings()(input_ids).clone()
|
| 102 |
|
| 103 |
vit_embeds = self.extract_feature(pixel_values)
|
| 104 |
vit_embeds = vit_embeds[image_flags == 1]
|
|
|
|
| 231 |
|
| 232 |
tokenizer.padding_side = 'left'
|
| 233 |
model_inputs = tokenizer(queries, return_tensors='pt', padding=True)
|
| 234 |
+
input_ids = model_inputs['input_ids'].to(self.device)
|
| 235 |
+
attention_mask = model_inputs['attention_mask'].to(self.device)
|
| 236 |
eos_token_id = tokenizer.convert_tokens_to_ids(template.sep)
|
| 237 |
generation_config['eos_token_id'] = eos_token_id
|
| 238 |
generation_output = self.generate(
|
|
|
|
| 280 |
query = query.replace('<image>', image_tokens, 1)
|
| 281 |
|
| 282 |
model_inputs = tokenizer(query, return_tensors='pt')
|
| 283 |
+
input_ids = model_inputs['input_ids'].to(self.device)
|
| 284 |
+
attention_mask = model_inputs['attention_mask'].to(self.device)
|
| 285 |
generation_config['eos_token_id'] = eos_token_id
|
| 286 |
generation_output = self.generate(
|
| 287 |
pixel_values=pixel_values,
|