Image-Text-to-Text
Transformers
Safetensors
multilingual
internvl_chat
feature-extraction
internvl
custom_code
conversational
Instructions to use OpenGVLab/InternVL2-2B with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use OpenGVLab/InternVL2-2B with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="OpenGVLab/InternVL2-2B", 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-2B", trust_remote_code=True, dtype="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps
- vLLM
How to use OpenGVLab/InternVL2-2B with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "OpenGVLab/InternVL2-2B" # 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-2B", "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-2B
- SGLang
How to use OpenGVLab/InternVL2-2B 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-2B" \ --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-2B", "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-2B" \ --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-2B", "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-2B with Docker Model Runner:
docker model run hf.co/OpenGVLab/InternVL2-2B
Upload folder using huggingface_hub
Browse files- README.md +1 -1
- modeling_internvl_chat.py +3 -1
README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
| 2 |
license: mit
|
| 3 |
pipeline_tag: image-text-to-text
|
| 4 |
library_name: transformers
|
| 5 |
-
base_model:
|
| 6 |
- OpenGVLab/InternViT-300M-448px
|
| 7 |
- internlm/internlm2-chat-1_8b
|
| 8 |
base_model_relation: merge
|
|
|
|
| 2 |
license: mit
|
| 3 |
pipeline_tag: image-text-to-text
|
| 4 |
library_name: transformers
|
| 5 |
+
base_model:
|
| 6 |
- OpenGVLab/InternViT-300M-448px
|
| 7 |
- internlm/internlm2-chat-1_8b
|
| 8 |
base_model_relation: merge
|
modeling_internvl_chat.py
CHANGED
|
@@ -38,7 +38,7 @@ class InternVLChatModel(PreTrainedModel):
|
|
| 38 |
_supports_flash_attn_2 = True
|
| 39 |
_no_split_modules = ['InternVisionModel', 'LlamaDecoderLayer', 'InternLM2DecoderLayer']
|
| 40 |
|
| 41 |
-
def __init__(self, config: InternVLChatConfig, vision_model=None, language_model=None):
|
| 42 |
super().__init__(config)
|
| 43 |
|
| 44 |
assert version_cmp(transformers.__version__, '4.36.2', 'ge')
|
|
@@ -50,6 +50,8 @@ class InternVLChatModel(PreTrainedModel):
|
|
| 50 |
self.num_image_token = int((image_size // patch_size) ** 2 * (config.downsample_ratio ** 2))
|
| 51 |
self.downsample_ratio = config.downsample_ratio
|
| 52 |
self.ps_version = config.ps_version
|
|
|
|
|
|
|
| 53 |
|
| 54 |
logger.info(f'num_image_token: {self.num_image_token}')
|
| 55 |
logger.info(f'ps_version: {self.ps_version}')
|
|
|
|
| 38 |
_supports_flash_attn_2 = True
|
| 39 |
_no_split_modules = ['InternVisionModel', 'LlamaDecoderLayer', 'InternLM2DecoderLayer']
|
| 40 |
|
| 41 |
+
def __init__(self, config: InternVLChatConfig, vision_model=None, language_model=None, use_flash_attn=True):
|
| 42 |
super().__init__(config)
|
| 43 |
|
| 44 |
assert version_cmp(transformers.__version__, '4.36.2', 'ge')
|
|
|
|
| 50 |
self.num_image_token = int((image_size // patch_size) ** 2 * (config.downsample_ratio ** 2))
|
| 51 |
self.downsample_ratio = config.downsample_ratio
|
| 52 |
self.ps_version = config.ps_version
|
| 53 |
+
config.vision_config.use_flash_attn = True if use_flash_attn else False
|
| 54 |
+
config.llm_config.attn_implementation = 'flash_attention_2' if use_flash_attn else 'eager'
|
| 55 |
|
| 56 |
logger.info(f'num_image_token: {self.num_image_token}')
|
| 57 |
logger.info(f'ps_version: {self.ps_version}')
|