Instructions to use Salesforce/blip2-opt-2.7b with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use Salesforce/blip2-opt-2.7b with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="Salesforce/blip2-opt-2.7b")# Load model directly from transformers import AutoProcessor, AutoModelForMultimodalLM processor = AutoProcessor.from_pretrained("Salesforce/blip2-opt-2.7b") model = AutoModelForMultimodalLM.from_pretrained("Salesforce/blip2-opt-2.7b") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use Salesforce/blip2-opt-2.7b with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "Salesforce/blip2-opt-2.7b" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "Salesforce/blip2-opt-2.7b", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/Salesforce/blip2-opt-2.7b
- SGLang
How to use Salesforce/blip2-opt-2.7b 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 "Salesforce/blip2-opt-2.7b" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "Salesforce/blip2-opt-2.7b", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'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 "Salesforce/blip2-opt-2.7b" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "Salesforce/blip2-opt-2.7b", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use Salesforce/blip2-opt-2.7b with Docker Model Runner:
docker model run hf.co/Salesforce/blip2-opt-2.7b
why max length is -21?????
I am running example code on an Ubuntu 20.04 system, but I am encountering an error with max length=-21. I checked the debugging process, and max length has consistently been 20. Why is this happening?
import sys; print('Python %s on %s' % (sys.version, sys.platform))
/mnt/Ubuntu/miniconda3/envs/ptg/bin/python3.8 /mnt/Ubuntu/pycharm-2024.3.1/plugins/python-ce/helpers/pydev/pydevd.py --multiprocess --qt-support=auto --port 29781 --file /mnt/02-WorkDir/someScript/blip2_opt.py
Loading checkpoint shards: 100%|ββββββββββ| 2/2 [00:13<00:00, 6.55s/it]
Traceback (most recent call last):
File "/mnt/Ubuntu/miniconda3/envs/ptg/lib/python3.8/site-packages/torch/utils/_contextlib.py", line 116, in decorate_context
return func(*args, **kwargs)
File "/mnt/Ubuntu/miniconda3/envs/ptg/lib/python3.8/site-packages/transformers/models/blip_2/modeling_blip_2.py", line 2337, in generate
outputs = self.language_model.generate(
File "/mnt/Ubuntu/miniconda3/envs/ptg/lib/python3.8/site-packages/torch/utils/_contextlib.py", line 116, in decorate_context
return func(*args, **kwargs)
File "/mnt/Ubuntu/miniconda3/envs/ptg/lib/python3.8/site-packages/transformers/generation/utils.py", line 2068, in generate
self._validate_generated_length(generation_config, input_ids_length, has_default_max_length)
File "/mnt/Ubuntu/miniconda3/envs/ptg/lib/python3.8/site-packages/transformers/generation/utils.py", line 1383, in _validate_generated_length
raise ValueError(
ValueError: Input length of input_ids is 0, but max_length is set to -21. This can lead to unexpected behavior. You should consider increasing max_length or, better yet, setting max_new_tokens.
python-BaseException
Have you solved it yet? I had the same problem.
Have you solved it yet? I had the same problem.
Solved the same problem. When calling .generate(...), you should set max_new_tokens=20 instead of max_length=20. In the latter situation the actual max_length inside the generation code is 20 - len(input_embeds).