In this directory, you will find examples on how you could apply BigDL-LLM INT4 optimizations on LLaVA models. For illustration purposes, we utilize the liuhaotian/llava-v1.5-13b as a reference LLaVA model.
To run these examples with BigDL-LLM, we have some recommended requirements for your machine, please refer to here for more information.
In the example generate.py, we show a basic use case for a LLaVA model to start a multi-turn chat centered around an image using generate()
API, with BigDL-LLM INT4 optimizations.
We suggest using conda to manage the Python environment. For more information about conda installation, please refer to here.
After installing conda, create a Python environment for BigDL-LLM:
conda create -n llm python=3.9 # recommend to use Python 3.9
conda activate llm
pip install --pre --upgrade bigdl-llm[all] # install the latest bigdl-llm nightly build with 'all' option
git clone -b v1.1.1 --depth=1 https://github.com/haotian-liu/LLaVA.git # clone the llava libary
pip install einops # install dependencies required by llava
cp generate.py ./LLaVA/ # copy our example to the LLaVA folder
cd LLaVA # change the working directory to the LLaVA folder
After setting up the Python environment, you could run the example by following steps.
Note: When loading the model in 4-bit, BigDL-LLM converts linear layers in the model into INT4 format. In theory, a XB model saved in 16-bit will requires approximately 2X GB of memory for loading, and ~0.5X GB memory for further inference.
Please select the appropriate size of the LLaVA model based on the capabilities of your machine.
On client Windows machines, it is recommended to run directly with full utilization of all cores:
python ./generate.py --image-path-or-url 'https://llava-vl.github.io/static/images/monalisa.jpg'
More information about arguments can be found in Arguments Info section. The expected output can be found in Sample Output section.
If you encounter some network error (which means your machine is unable to access huggingface.co) when running this example, refer to Trouble Shooting section.
For optimal performance on server, it is recommended to set several environment variables (refer to here for more information), and run the example with all the physical cores of a single socket.
E.g. on Linux,
# set BigDL-LLM env variables
source bigdl-llm-init
# e.g. for a server with 48 cores per socket
export OMP_NUM_THREADS=48
numactl -C 0-47 -m 0 python ./generate.py --image-path-or-url 'https://llava-vl.github.io/static/images/monalisa.jpg'
More information about arguments can be found in Arguments Info section. The expected output can be found in Sample Output section.
In the example, several arguments can be passed to satisfy your requirements:
--repo-id-or-model-path REPO_ID_OR_MODEL_PATH
: argument defining the huggingface repo id for the LLaVA model (e.g.liuhaotian/llava-v1.5-13b
) to be downloaded, or the path to the huggingface checkpoint folder. It is default to be'liuhaotian/llava-v1.5-13b'
.--image-path-or-url IMAGE_PATH_OR_URL
: argument defining the input image that the chat will focus on. It is required.--n-predict N_PREDICT
: argument defining the max number of tokens to predict. It is default to be512
.
USER: 你知道这幅画是谁画的吗?
ASSISTANT: 这幅画是由著名的文艺复兴画家达芬奇(Leonardo da Vinci)画的。该画是他的代表作之一,是出自意大利佛罗伦萨的博物馆。画中的女子被认为是一位不为人知的模特,而且画作可能还有一个人物底版,这可能使得这幅画的价值更高。
The sample input image is:
If you encounter the following output, it means your machine has some trouble accessing huggingface.co.
requests.exceptions.SSLError: (MaxRetryError("HTTPSConnectionPool(host='huggingface.co', port=443): Max retries exceeded with url: /openai/clip-vit-large-patch14-336/resolve/main/config.json (Caused by SSLError(SSLZeroReturnError(6, 'TLS/SSL connection has been closed (EOF) (_ssl.c:1129)')))"),
You can resolve this problem with the following steps:
- Download https://huggingface.co/openai/clip-vit-large-patch14-336 on some machine that can access huggingface.co, and put it in huggingface's local cache (default to be
~/.cache/huggingface/hub
) on the machine that you are going to run this example. - Set the environment variable (
export TRANSFORMERS_OFFLINE=1
) before you run the example.