Image-Text-to-Text
Transformers
Safetensors
multilingual
internvl_chat
feature-extraction
internvl
custom_code
conversational
Instructions to use OpenGVLab/InternVL2-4B with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use OpenGVLab/InternVL2-4B with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="OpenGVLab/InternVL2-4B", 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-4B", trust_remote_code=True, dtype="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps
- vLLM
How to use OpenGVLab/InternVL2-4B with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "OpenGVLab/InternVL2-4B" # 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-4B", "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-4B
- SGLang
How to use OpenGVLab/InternVL2-4B 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-4B" \ --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-4B", "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-4B" \ --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-4B", "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-4B with Docker Model Runner:
docker model run hf.co/OpenGVLab/InternVL2-4B
Upload folder using huggingface_hub
Browse files- modeling_phi3.py +9 -0
modeling_phi3.py
CHANGED
|
@@ -53,6 +53,7 @@ try:
|
|
| 53 |
unpad_input)
|
| 54 |
|
| 55 |
_flash_supports_window_size = 'window_size' in list(inspect.signature(flash_attn_func).parameters)
|
|
|
|
| 56 |
except ImportError as error:
|
| 57 |
logger.warning(
|
| 58 |
f'`flash-attention` package not found, consider installing for better performance: {error}.'
|
|
@@ -61,6 +62,7 @@ except ImportError as error:
|
|
| 61 |
logger.warning(
|
| 62 |
"Current `flash-attenton` does not support `window_size`. Either upgrade or use `attn_implementation='eager'`."
|
| 63 |
)
|
|
|
|
| 64 |
|
| 65 |
_CHECKPOINT_FOR_DOC = 'microsoft/Phi-3-mini-4k-instruct'
|
| 66 |
_CONFIG_FOR_DOC = 'Phi3Config'
|
|
@@ -937,6 +939,12 @@ class Phi3PreTrainedModel(PreTrainedModel):
|
|
| 937 |
|
| 938 |
_version = '0.0.5'
|
| 939 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 940 |
def _init_weights(self, module):
|
| 941 |
std = self.config.initializer_range
|
| 942 |
if isinstance(module, nn.Linear):
|
|
@@ -1042,6 +1050,7 @@ class Phi3Model(Phi3PreTrainedModel):
|
|
| 1042 |
[Phi3DecoderLayer(config, layer_idx) for layer_idx in range(config.num_hidden_layers)]
|
| 1043 |
)
|
| 1044 |
self._attn_implementation = config._attn_implementation
|
|
|
|
| 1045 |
self.norm = Phi3RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
| 1046 |
|
| 1047 |
self.gradient_checkpointing = False
|
|
|
|
| 53 |
unpad_input)
|
| 54 |
|
| 55 |
_flash_supports_window_size = 'window_size' in list(inspect.signature(flash_attn_func).parameters)
|
| 56 |
+
has_flash_attn = True
|
| 57 |
except ImportError as error:
|
| 58 |
logger.warning(
|
| 59 |
f'`flash-attention` package not found, consider installing for better performance: {error}.'
|
|
|
|
| 62 |
logger.warning(
|
| 63 |
"Current `flash-attenton` does not support `window_size`. Either upgrade or use `attn_implementation='eager'`."
|
| 64 |
)
|
| 65 |
+
has_flash_attn = False
|
| 66 |
|
| 67 |
_CHECKPOINT_FOR_DOC = 'microsoft/Phi-3-mini-4k-instruct'
|
| 68 |
_CONFIG_FOR_DOC = 'Phi3Config'
|
|
|
|
| 939 |
|
| 940 |
_version = '0.0.5'
|
| 941 |
|
| 942 |
+
def __init__(self, config: Phi3Config):
|
| 943 |
+
if not has_flash_attn:
|
| 944 |
+
config._attn_implementation = 'eager'
|
| 945 |
+
print('Warning: Flash attention is not available, using eager attention instead.')
|
| 946 |
+
super().__init__(config)
|
| 947 |
+
|
| 948 |
def _init_weights(self, module):
|
| 949 |
std = self.config.initializer_range
|
| 950 |
if isinstance(module, nn.Linear):
|
|
|
|
| 1050 |
[Phi3DecoderLayer(config, layer_idx) for layer_idx in range(config.num_hidden_layers)]
|
| 1051 |
)
|
| 1052 |
self._attn_implementation = config._attn_implementation
|
| 1053 |
+
|
| 1054 |
self.norm = Phi3RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
| 1055 |
|
| 1056 |
self.gradient_checkpointing = False
|