File size: 5,678 Bytes
7298b2b
3b9ff18
 
 
 
 
 
 
 
 
 
 
 
 
7298b2b
 
 
3b9ff18
7298b2b
3b9ff18
7298b2b
 
 
3b9ff18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7298b2b
 
3b9ff18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
593eefd
3b9ff18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
---
license: apache-2.0
base_model: HuggingFaceTB/SmolVLM-Instruct
tags:
- vision
- image-text-to-text
- multimodal
- quantized
- gptq
- 4-bit
- llm-compressor
language:
- en
pipeline_tag: image-text-to-text
library_name: transformers
---

# SmolVLM-Instruct-GPTQ-4bit

This is a 4-bit GPTQ quantized version of [HuggingFaceTB/SmolVLM-Instruct](https://huggingface.co/HuggingFaceTB/SmolVLM-Instruct), a 2.2B parameter vision-language model.

## Model Details

- **Base Model**: HuggingFaceTB/SmolVLM-Instruct
- **Quantization Method**: GPTQ W4A16 (4-bit weights, 16-bit activations)
- **Quantization Tool**: [llm-compressor](https://github.com/vllm-project/llm-compressor)
- **Model Size**: 1.97 GB (55% reduction from 4.4 GB)
- **Architecture**: Idefics3 (vision encoder + Llama-3.2 text decoder)

### What's Quantized**Quantized to 4-bit**:
- Text decoder (24 LlamaDecoderLayer blocks)
- All attention projections (q_proj, k_proj, v_proj, o_proj)
- All MLP layers (gate_proj, up_proj, down_proj)
- Total: 168 linear layers

**Preserved at full precision**:
- Vision encoder/tower (SigLIP)
- Vision-text connector
- Language model head
- All layer norms and biases

## Usage

### Requirements

```bash
pip install transformers torch pillow
```

### Basic Usage

```python
from transformers import Idefics3ForConditionalGeneration, AutoProcessor
from PIL import Image
import requests

# Load model and processor
model = Idefics3ForConditionalGeneration.from_pretrained(
    "ronantakizawa/SmolVLM-Instruct-GPTQ-4bit",
    device_map="auto",
    torch_dtype="auto"
)
processor = AutoProcessor.from_pretrained("ronantakizawa/SmolVLM-Instruct-GPTQ-4bit")

# Load an image
url = "https://huggingface.co/spaces/merve/chatml-llava/resolve/main/bee.jpg"
image = Image.open(requests.get(url, stream=True).raw)

# Create prompt
messages = [
    {
        "role": "user",
        "content": [
            {"type": "image"},
            {"type": "text", "text": "Describe this image in detail."}
        ]
    }
]

# Generate
prompt = processor.apply_chat_template(messages, add_generation_prompt=True)
inputs = processor(text=prompt, images=[image], return_tensors="pt").to(model.device)

generated_ids = model.generate(**inputs, max_new_tokens=500)
generated_texts = processor.batch_decode(
    generated_ids,
    skip_special_tokens=True,
)

print(generated_texts[0])
```

### Using with vLLM (Production Deployment)

```bash
pip install vllm

python -m vllm.entrypoints.openai.api_server \
    --model ronantakizawa/SmolVLM-Instruct-GPTQ-4bit \
    --quantization gptq \
    --dtype auto
```

Then use the OpenAI-compatible API:

```python
from openai import OpenAI

client = OpenAI(
    base_url="http://localhost:8000/v1",
    api_key="dummy"
)

response = client.chat.completions.create(
    model="ronantakizawa/SmolVLM-Instruct-GPTQ-4bit",
    messages=[
        {
            "role": "user",
            "content": [
                {"type": "image_url", "image_url": {"url": "https://example.com/image.jpg"}},
                {"type": "text", "text": "What's in this image?"}
            ]
        }
    ]
)

print(response.choices[0].message.content)
```

## Quantization Details

### Training Data
- **Calibration Dataset**: lmms-lab/flickr30k
- **Calibration Samples**: 256 images
- **Sequence Length**: 2048 tokens

### Quantization Parameters
```python
GPTQModifier(
    targets="Linear",
    scheme="W4A16",
    ignore=[
        "re:.*lm_head",
        "re:.*vision_model.*",
        "re:.*connector.*",
        "re:.*vision_tower.*"
    ]
)
```

### Sequential Targets
- Target layers: `LlamaDecoderLayer`
- Pipeline: Sequential (layer-by-layer calibration)

## Performance

| Metric | Value |
|--------|-------|
| **Original Size** | 4.4 GB |
| **Quantized Size** | 1.97 GB |
| **Compression Ratio** | 2.23x (55% reduction) |
| **GPU Memory (inference)** | ~2-3 GB |
| **Vision Quality** | Preserved (no degradation) |
| **Text Quality** | Under 1% quality degradation in DocVQA |

### Inference Speed
- Similar or slightly faster than fp16 due to reduced memory bandwidth
- Ideal for deployment on consumer GPUs (RTX 3090, 4090, etc.)

## Limitations

1. **Slight quality degradation**: 4-bit quantization introduces minor quality loss in text generation
2. **GPTQ-specific**: Requires GPTQ-compatible inference engines (vLLM, transformers)
3. **Vision tower not quantized**: Vision encoder remains at full precision to preserve image understanding

## Technical Notes

This model was quantized using custom patches to llm-compressor to support the idefics3 architecture:
- Fixed meta tensor materialization issues in sequential pipeline
- Enabled GPTQ quantization for vision-language models
- Patches available at: [ronantakizawa/llm-compressor](https://github.com/ronantakizawa/llm-compressor)

## Citation

If you use this model, please cite the original SmolVLM work:

```bibtex
@misc{smolvlm2024,
  title={SmolVLM: Small Vision-Language Model},
  author={HuggingFace Team},
  year={2024},
  publisher={HuggingFace},
  url={https://huggingface.co/HuggingFaceTB/SmolVLM-Instruct}
}
```

And the quantization tool:

```bibtex
@software{llmcompressor2024,
  title={LLM Compressor},
  author={Neural Magic},
  year={2024},
  url={https://github.com/vllm-project/llm-compressor}
}
```

## License

This model inherits the Apache 2.0 license from the base model.

## Acknowledgments

- Base model: [HuggingFaceTB/SmolVLM-Instruct](https://huggingface.co/HuggingFaceTB/SmolVLM-Instruct)
- Quantization: [llm-compressor](https://github.com/vllm-project/llm-compressor)
- Calibration data: [lmms-lab/flickr30k](https://huggingface.co/datasets/lmms-lab/flickr30k)