Skip to content

Commit d6a0c67

Browse files
authored
feat: add initial qwen2.5-vl model and test (#2971)
* feat: support qwen2.5 vl model * fix: bump support models doc * feat: check before rope type adjustment and small refactors * fix: add transformer overlay for processor support * fix: vendor processor and config from transformers * fix: refactor/simplify conditionals
1 parent a744866 commit d6a0c67

File tree

15 files changed

+1282
-17
lines changed

15 files changed

+1282
-17
lines changed

docs/source/supported_models.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Text Generation Inference enables serving optimized models. The following sectio
2727
- [StarCoder 2](https://huggingface.co/bigcode/starcoder2-15b-instruct-v0.1)
2828
- [Qwen 2](https://huggingface.co/collections/Qwen/qwen2-6659360b33528ced941e557f)
2929
- [Qwen 2 VL](https://huggingface.co/collections/Qwen/qwen2-vl-66cee7455501d7126940800d)
30+
- [Qwen 2.5 VL](https://huggingface.co/collections/Qwen/qwen25-66e81a666513e518adb90d9e)
3031
- [Opt](https://huggingface.co/facebook/opt-6.7b)
3132
- [T5](https://huggingface.co/google/flan-t5-xxl)
3233
- [Galactica](https://huggingface.co/facebook/galactica-120b)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"choices": [
3+
{
4+
"finish_reason": "stop",
5+
"index": 0,
6+
"logprobs": null,
7+
"message": {
8+
"content": "The image showcases the Statue of Liberty, a colossal bronze statue located in New York Harbor, a heritage building in the United States. The statue has a majestic presence, with one arm raised towards the sun and the other hitched on her hip. It sits atop a keeper's walkway, observed from the water. Surrounding the statue is a lush green meadow, where picnic spots, walkways, and a visitor desk can be found. In front of the statue, a large marina can accommodate fourteen different kinds of boats. In the backdrop stands the Empire State Building, marking the crowded skyscrapers of New York City.",
9+
"name": null,
10+
"role": "assistant",
11+
"tool_calls": null
12+
},
13+
"usage": null
14+
}
15+
],
16+
"created": 1738342753,
17+
"id": "",
18+
"model": "Qwen/Qwen2.5-VL-3B-Instruct",
19+
"object": "chat.completion",
20+
"system_fingerprint": "3.0.2-dev0-native",
21+
"usage": {
22+
"completion_tokens": 128,
23+
"prompt_tokens": 8736,
24+
"total_tokens": 8864
25+
}
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"choices": [
3+
{
4+
"finish_reason": "stop",
5+
"index": 0,
6+
"logprobs": null,
7+
"message": {
8+
"content": "The image shows a whimsical scene set in what appears to be a fast-food restaurant. Dominating the foreground is a large, green, inflatable dinosaur with realistic textures, giving it a Jurassic Park-like appearance. The dinosaur is wearing a red Adult Swim logo hat, adding a humorous touch to its appearance.\n\nSurrounding the dinosaur are various food items typically found in a fast-food restaurant, including French fries in a plastic cup, a hamburger on a plate, and a beverage in another cup. The hamburger is detailed with lettuce, tomato, and other typical fast-food ingredients.\n\nAccompanying the dinosaur is a realistic-looking owl perched on the table, which adds to the surreal and playful atmosphere of the scene. The background features the interior of the restaurant with neon signs and other typical decor elements, enhancing the overall theme of a fun and fantastical fast-food experience.\n\nOverall, the image is a playful and imaginative blend of a standard fast-food setting with an unexpected and amusing twist provided by the dinosaur and owl characters.",
9+
"name": null,
10+
"role": "assistant",
11+
"tool_calls": null
12+
},
13+
"usage": null
14+
}
15+
],
16+
"created": 1738343775,
17+
"id": "",
18+
"model": "Qwen/Qwen2.5-VL-3B-Instruct",
19+
"object": "chat.completion",
20+
"system_fingerprint": "3.0.2-dev0-native",
21+
"usage": {
22+
"completion_tokens": 206,
23+
"prompt_tokens": 5375,
24+
"total_tokens": 5581
25+
}
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"choices": [
3+
{
4+
"finish_reason": "stop",
5+
"index": 0,
6+
"logprobs": null,
7+
"message": {
8+
"content": "The image depicts an anthropomorphic rabbit character wearing an intricate space suit, which includes a helmet with a starry face pattern and multiple suitors. The rabbit's ears are significantly large and upright, and it has a hitchhiker-like star antennas on its chest. The background is a reddish-orange, rocky landscape, suggesting a Martian environment. The suit has various buttons, a red button on the chest, and a reflective or illuminated dome on the head. The overall color scheme is dominated by shades of red, orange, and gray, giving a sense of a rugged, otherworldly setting.",
9+
"name": null,
10+
"role": "assistant",
11+
"tool_calls": null
12+
},
13+
"usage": null
14+
}
15+
],
16+
"created": 1738342872,
17+
"id": "",
18+
"model": "Qwen/Qwen2.5-VL-3B-Instruct",
19+
"object": "chat.completion",
20+
"system_fingerprint": "3.0.2-dev0-native",
21+
"usage": {
22+
"completion_tokens": 121,
23+
"prompt_tokens": 1363,
24+
"total_tokens": 1484
25+
}
26+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"choices": [
3+
{
4+
"delta": {
5+
"content": "",
6+
"role": "assistant",
7+
"tool_calls": null
8+
},
9+
"finish_reason": "stop",
10+
"index": 0,
11+
"logprobs": null
12+
}
13+
],
14+
"created": 1738343559,
15+
"id": "",
16+
"model": "Qwen/Qwen2.5-VL-3B-Instruct",
17+
"object": "chat.completion.chunk",
18+
"system_fingerprint": "3.0.2-dev0-native",
19+
"usage": null
20+
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import pytest
2+
3+
4+
@pytest.fixture(scope="module")
5+
def flash_qwen2_5_vl_handle(launcher):
6+
with launcher("Qwen/Qwen2.5-VL-3B-Instruct") as handle:
7+
yield handle
8+
9+
10+
@pytest.fixture(scope="module")
11+
async def flash_qwen2_5(flash_qwen2_5_vl_handle):
12+
await flash_qwen2_5_vl_handle.health(300)
13+
return flash_qwen2_5_vl_handle.client
14+
15+
16+
@pytest.mark.private
17+
async def test_flash_qwen2_5_vl_simple(flash_qwen2_5, response_snapshot):
18+
response = await flash_qwen2_5.chat(
19+
seed=42,
20+
messages=[
21+
{
22+
"role": "user",
23+
"content": [
24+
{
25+
"type": "image_url",
26+
"image_url": {
27+
"url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/rabbit.png"
28+
},
29+
},
30+
{"type": "text", "text": "Describe the image"},
31+
],
32+
},
33+
],
34+
)
35+
36+
assert (
37+
response.choices[0].message.content
38+
== "The image depicts an anthropomorphic rabbit character wearing an intricate space suit, which includes a helmet with a starry face pattern and multiple suitors. The rabbit's ears are significantly large and upright, and it has a hitchhiker-like star antennas on its chest. The background is a reddish-orange, rocky landscape, suggesting a Martian environment. The suit has various buttons, a red button on the chest, and a reflective or illuminated dome on the head. The overall color scheme is dominated by shades of red, orange, and gray, giving a sense of a rugged, otherworldly setting."
39+
)
40+
41+
assert response == response_snapshot
42+
43+
44+
@pytest.mark.private
45+
async def test_flash_qwen2_5_vl_simple_streaming(flash_qwen2_5, response_snapshot):
46+
responses = await flash_qwen2_5.chat(
47+
seed=42,
48+
messages=[
49+
{
50+
"role": "user",
51+
"content": [
52+
{
53+
"type": "image_url",
54+
"image_url": {
55+
"url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/rabbit.png"
56+
},
57+
},
58+
{"type": "text", "text": "Describe the image"},
59+
],
60+
},
61+
],
62+
stream=True,
63+
)
64+
65+
count = 0
66+
generated = ""
67+
last_response = None
68+
async for response in responses:
69+
count += 1
70+
generated += response.choices[0].delta.content
71+
last_response = response
72+
73+
assert (
74+
generated
75+
== "The image depicts an anthropomorphic rabbit character wearing an intricate space suit, which includes a helmet with a starry face pattern and multiple suitors. The rabbit's ears are significantly large and upright, and it has a hitchhiker-like star antennas on its chest. The background is a reddish-orange, rocky landscape, suggesting a Martian environment. The suit has various buttons, a red button on the chest, and a reflective or illuminated dome on the head. The overall color scheme is dominated by shades of red, orange, and gray, giving a sense of a rugged, otherworldly setting."
76+
)
77+
assert count == 121
78+
assert last_response == response_snapshot
79+
80+
81+
@pytest.mark.private
82+
async def test_flash_qwen2_5_vl_bay(flash_qwen2_5, response_snapshot):
83+
response = await flash_qwen2_5.chat(
84+
seed=42,
85+
messages=[
86+
{
87+
"role": "user",
88+
"content": [
89+
{
90+
"type": "image_url",
91+
"image_url": {
92+
"url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg"
93+
},
94+
},
95+
{"type": "text", "text": "Describe the image"},
96+
],
97+
},
98+
],
99+
)
100+
assert response == response_snapshot
101+
102+
103+
@pytest.mark.private
104+
async def test_flash_qwen2_5_vl_inpaint(flash_qwen2_5, response_snapshot):
105+
response = await flash_qwen2_5.chat(
106+
seed=42,
107+
messages=[
108+
{
109+
"role": "user",
110+
"content": [
111+
{
112+
"type": "image_url",
113+
"image_url": {
114+
"url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/autopipeline-inpaint.png"
115+
},
116+
},
117+
{"type": "text", "text": "Describe the image"},
118+
],
119+
},
120+
],
121+
)
122+
assert response == response_snapshot

launcher/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2053,7 +2053,7 @@ fn main() -> Result<(), LauncherError> {
20532053
// this is a short term temporary fix to enable vlms to avoid rejecting images
20542054
let default_optimal = match config {
20552055
Some(ref config) => match config.model_type.as_deref() {
2056-
Some("qwen2_vl") => 10_000,
2056+
Some("qwen2_vl") | Some("qwen2_5_vl") => 10_000,
20572057
_ => 4096,
20582058
},
20592059
None => 4096,

nix/overlay.nix

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@ final: prev: {
1313
(
1414
python-self: python-super: with python-self; {
1515
# Python package override example:
16-
# transformers = python-super.transformers.overrideAttrs (
17-
# _: _: {
18-
# src = final.fetchFromGitHub {
19-
# owner = "huggingface";
20-
# repo = "transformers";
21-
# rev = "2bd4d5897dc73e8b172832070a6f9e567a0df017";
22-
# hash = "sha256-JOIpKH9ssDEfI2Tf15e0iPKtThJwQ9GxMvRAnm+M2Pg=";
23-
# };
24-
# }
25-
# );
16+
transformers = python-super.transformers.overrideAttrs (
17+
_: _: {
18+
src = final.fetchFromGitHub {
19+
owner = "huggingface";
20+
repo = "transformers";
21+
rev = "8d73a38606bc342b370afe1f42718b4828d95aaa";
22+
hash = "sha256-MxroG6CWqrcmRS+eFt7Ej87TDOInN15aRPBUcaycKTI=";
23+
};
24+
}
25+
);
2626
}
2727
)
2828
];

router/src/config.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,43 @@ impl Qwen2Vl {
184184
}
185185
}
186186

187+
#[derive(Clone, Debug, Serialize, Deserialize)]
188+
#[serde(rename_all = "snake_case")]
189+
pub struct Qwen2_5VlVisionConfig {
190+
pub(crate) depth: usize,
191+
pub(crate) hidden_act: String,
192+
pub(crate) hidden_size: usize,
193+
pub(crate) intermediate_size: usize,
194+
pub(crate) num_heads: usize,
195+
pub(crate) in_chans: usize,
196+
pub(crate) out_hidden_size: usize,
197+
pub(crate) patch_size: usize,
198+
pub(crate) spatial_merge_size: usize,
199+
pub(crate) spatial_patch_size: usize,
200+
pub(crate) window_size: usize,
201+
pub(crate) fullatt_block_indexes: Vec<usize>,
202+
pub(crate) tokens_per_second: usize,
203+
pub(crate) temporal_patch_size: usize,
204+
}
205+
206+
#[derive(Clone, Debug, Serialize, Deserialize)]
207+
#[serde(rename_all = "snake_case")]
208+
pub struct Qwen2_5Vl {
209+
pub(crate) vision_config: Qwen2_5VlVisionConfig,
210+
}
211+
212+
impl Qwen2_5Vl {
213+
pub fn get_number_of_features(&self, height: usize, width: usize) -> usize {
214+
let num_pixels = height * width;
215+
num_pixels / self.vision_config.patch_size.pow(2)
216+
}
217+
}
218+
187219
#[derive(Clone, Debug, Serialize, Deserialize)]
188220
#[serde(tag = "model_type")]
189221
#[serde(rename_all = "snake_case")]
190222
pub enum Config {
223+
Qwen2_5Vl(Qwen2_5Vl),
191224
Qwen2Vl(Qwen2Vl),
192225
LlavaNext(LlavaNext),
193226
ClipVisionModel(ClipVisionModel),

router/src/validation.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,10 @@ fn image_tokens(
690690
"<|vision_start|>{:?}<|vision_end|>",
691691
"<|image_pad|>".repeat(config.get_number_of_features(height, width))
692692
),
693+
Qwen2_5Vl(config) => format!(
694+
"<|vision_start|>{:?}<|vision_end|>",
695+
"<|image_pad|>".repeat(config.get_number_of_features(height, width))
696+
),
693697
_ => unimplemented!("Images tokens are not supported for this model configuration"),
694698
}
695699
}
@@ -718,7 +722,7 @@ fn prepare_input<T: TokenizerTrait>(
718722
let (tokenizer_query, input_chunks) = match config {
719723
Some(
720724
config @ (Idefics | Mllama | Idefics2(_) | Idefics3(_) | Paligemma(_) | LlavaNext(_)
721-
| Qwen2Vl(_)),
725+
| Qwen2Vl(_) | Qwen2_5Vl(_)),
722726
) => {
723727
let mut input_chunks = Vec::new();
724728
let mut tokenizer_query = String::with_capacity(inputs.len());

0 commit comments

Comments
 (0)