Skip to content

Commit 84d5f4b

Browse files
Update granite vision docs for 3.2 model (#12105)
Signed-off-by: Alex-Brooks <[email protected]>
1 parent 438a839 commit 84d5f4b

File tree

1 file changed

+34
-27
lines changed

1 file changed

+34
-27
lines changed

examples/llava/README-granitevision.md

+34-27
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
Download the model and point your `GRANITE_MODEL` environment variable to the path.
44

55
```bash
6-
$ git clone https://huggingface.co/ibm-granite/granite-vision-3.1-2b-preview
7-
$ export GRANITE_MODEL=./granite-vision-3.1-2b-preview
6+
$ git clone https://huggingface.co/ibm-granite/granite-vision-3.2-2b
7+
$ export GRANITE_MODEL=./granite-vision-3.2-2b
88
```
99

1010

@@ -41,17 +41,26 @@ If you actually inspect the `.keys()` of the loaded tensors, you should see a lo
4141

4242

4343
### 2. Creating the Visual Component GGUF
44-
To create the GGUF for the visual components, we need to write a config for the visual encoder; make sure the config contains the correct `image_grid_pinpoints`
44+
Next, create a new directory to hold the visual components, and copy the llava.clip/projector files, as shown below.
4545

46+
```bash
47+
$ ENCODER_PATH=$PWD/visual_encoder
48+
$ mkdir $ENCODER_PATH
49+
50+
$ cp $GRANITE_MODEL/llava.clip $ENCODER_PATH/pytorch_model.bin
51+
$ cp $GRANITE_MODEL/llava.projector $ENCODER_PATH/
52+
```
53+
54+
Now, we need to write a config for the visual encoder. In order to convert the model, be sure to use the correct `image_grid_pinpoints`, as these may vary based on the model. You can find the `image_grid_pinpoints` in `$GRANITE_MODEL/config.json`.
4655

47-
Note: we refer to this file as `$VISION_CONFIG` later on.
4856
```json
4957
{
5058
"_name_or_path": "siglip-model",
5159
"architectures": [
5260
"SiglipVisionModel"
5361
],
5462
"image_grid_pinpoints": [
63+
[384,384],
5564
[384,768],
5665
[384,1152],
5766
[384,1536],
@@ -94,42 +103,32 @@ Note: we refer to this file as `$VISION_CONFIG` later on.
94103
}
95104
```
96105

97-
Create a new directory to hold the visual components, and copy the llava.clip/projector files, as well as the vision config into it.
98-
99-
```bash
100-
$ ENCODER_PATH=$PWD/visual_encoder
101-
$ mkdir $ENCODER_PATH
102-
103-
$ cp $GRANITE_MODEL/llava.clip $ENCODER_PATH/pytorch_model.bin
104-
$ cp $GRANITE_MODEL/llava.projector $ENCODER_PATH/
105-
$ cp $VISION_CONFIG $ENCODER_PATH/config.json
106-
```
107-
108-
At which point you should have something like this:
106+
At this point you should have something like this:
109107
```bash
110108
$ ls $ENCODER_PATH
111109
config.json llava.projector pytorch_model.bin
112110
```
113111

114-
Now convert the components to GGUF; Note that we also override the image mean/std dev to `[.5,.5,.5]` since we use the siglip visual encoder - in the transformers model, you can find these numbers in the [preprocessor_config.json](https://huggingface.co/ibm-granite/granite-vision-3.1-2b-preview/blob/main/preprocessor_config.json).
112+
Now convert the components to GGUF; Note that we also override the image mean/std dev to `[.5,.5,.5]` since we use the SigLIP visual encoder - in the transformers model, you can find these numbers in the `preprocessor_config.json`.
115113
```bash
116114
$ python convert_image_encoder_to_gguf.py \
117115
-m $ENCODER_PATH \
118116
--llava-projector $ENCODER_PATH/llava.projector \
119117
--output-dir $ENCODER_PATH \
120118
--clip-model-is-vision \
121119
--clip-model-is-siglip \
122-
--image-mean 0.5 0.5 0.5 --image-std 0.5 0.5 0.5
120+
--image-mean 0.5 0.5 0.5 \
121+
--image-std 0.5 0.5 0.5
123122
```
124123

125-
this will create the first GGUF file at `$ENCODER_PATH/mmproj-model-f16.gguf`; we will refer to the abs path of this file as the `$VISUAL_GGUF_PATH.`
124+
This will create the first GGUF file at `$ENCODER_PATH/mmproj-model-f16.gguf`; we will refer to the absolute path of this file as the `$VISUAL_GGUF_PATH.`
126125

127126

128127
### 3. Creating the LLM GGUF.
129128
The granite vision model contains a granite LLM as its language model. For now, the easiest way to get the GGUF for LLM is by loading the composite model in `transformers` and exporting the LLM so that it can be directly converted with the normal conversion path.
130129

131130
First, set the `LLM_EXPORT_PATH` to the path to export the `transformers` LLM to.
132-
```
131+
```bash
133132
$ export LLM_EXPORT_PATH=$PWD/granite_vision_llm
134133
```
135134

@@ -142,7 +141,7 @@ if not MODEL_PATH:
142141
raise ValueError("env var GRANITE_MODEL is unset!")
143142

144143
LLM_EXPORT_PATH = os.getenv("LLM_EXPORT_PATH")
145-
if not MODEL_PATH:
144+
if not LLM_EXPORT_PATH:
146145
raise ValueError("env var LLM_EXPORT_PATH is unset!")
147146

148147
tokenizer = transformers.AutoTokenizer.from_pretrained(MODEL_PATH)
@@ -166,18 +165,26 @@ $ python convert_hf_to_gguf.py --outfile $LLM_GGUF_PATH $LLM_EXPORT_PATH
166165
```
167166

168167

169-
### 4. Running the Model in Llama cpp
170-
Build llama cpp normally; you should have a target binary named `llama-llava-cli`, which you can pass two binaries to. Sample usage:
168+
### 4. Quantization
169+
If you want to quantize the LLM, you can do so with `llama-quantize` as you would any other LLM. For example:
170+
```bash
171+
$ ./build/bin/llama-quantize $LLM_EXPORT_PATH/granite_llm.gguf $LLM_EXPORT_PATH/granite_llm_q4_k_m.gguf Q4_K_M
172+
$ LLM_GGUF_PATH=$LLM_EXPORT_PATH/granite_llm_q4_k_m.gguf
173+
```
174+
175+
Note that currently you cannot quantize the visual encoder because granite vision models use SigLIP as the visual encoder, which has tensor dimensions that are not divisible by 32.
176+
171177

172-
Note - the test image shown below can be found [here](https://github-production-user-asset-6210df.s3.amazonaws.com/10740300/415512792-d90d5562-8844-4f34-a0a5-77f62d5a58b5.jpg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAVCODYLSA53PQK4ZA%2F20250221%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20250221T054145Z&X-Amz-Expires=300&X-Amz-Signature=86c60be490aa49ef7d53f25d6c973580a8273904fed11ed2453d0a38240ee40a&X-Amz-SignedHeaders=host).
178+
### 5. Running the Model in Llama cpp
179+
Build llama cpp normally; you should have a target binary named `llama-llava-cli`, which you can pass two binaries to. As an example, we pass the the llama.cpp banner.
173180

174181
```bash
175182
$ ./build/bin/llama-llava-cli -m $LLM_GGUF_PATH \
176183
--mmproj $VISUAL_GGUF_PATH \
177-
--image cherry_blossom.jpg \
184+
--image ./media/llama0-banner.png \
178185
-c 16384 \
179-
-p "<|system|>\nA chat between a curious user and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the user's questions.\n<|user|>\n\<image>\nWhat type of flowers are in this picture?\n<|assistant|>\n" \
186+
-p "<|system|>\nA chat between a curious user and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the user's questions.\n<|user|>\n\<image>\nWhat does the text in this image say?\n<|assistant|>\n" \
180187
--temp 0
181188
```
182189

183-
Sample response: `The flowers in the picture are cherry blossoms, which are known for their delicate pink petals and are often associated with the beauty of spring.`
190+
Sample output: `The text in the image reads "LLAMA C++ Can it run DOOM Llama?"`

0 commit comments

Comments
 (0)