prithivMLmods commited on
Commit
924a003
·
verified ·
1 Parent(s): 842a737

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +188 -1
README.md CHANGED
@@ -15,4 +15,191 @@ tags:
15
  language:
16
  - en
17
  pipeline_tag: text-generation
18
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  language:
16
  - en
17
  pipeline_tag: text-generation
18
+ ---
19
+
20
+ # **Qwen-Image-2.1-PE-T2I-FP8**
21
+
22
+ > **Qwen-Image-2.1-PE-T2I-FP8** is an **FP8 dynamic-quantized** build of [Qwen/Qwen-Image-2.1-PE-T2I](https://huggingface.co/Qwen/Qwen-Image-2.1-PE-T2I), the text-to-image **prompt rewriting model** for [Qwen-Image-2.1](https://huggingface.co/Qwen/Qwen-Image-2.1). The base model is a fine-tuned **Qwen3.5-VL 9B** that turns a brief image request in any language into a detailed English prompt plus a recommended aspect ratio. This checkpoint was compressed with **[llm-compressor](https://github.com/vllm-project/llm-compressor)** using the **FP8_DYNAMIC** scheme, stored in the **compressed-tensors** format, and is intended to be served with **[vLLM](https://github.com/vllm-project/vllm)**. The **Linear** layers are quantized to **FP8** weights with dynamic per-token FP8 activations, which lowers weight memory and improves serving throughput relative to the BF16 original. The **lm_head**, **embedding layers**, **vision modules**, and **linear attention** layers are excluded and kept in their original precision. No calibration data is required, because activation scales are computed at runtime.
23
+
24
+ ## Quantization Details
25
+
26
+ | Property | Value |
27
+ |---|---|
28
+ | Base model | `Qwen/Qwen-Image-2.1-PE-T2I` |
29
+ | Architecture | Qwen3.5-VL 9B (fine-tuned) |
30
+ | Quantization tool | `llm-compressor` |
31
+ | Modifier | `QuantizationModifier` |
32
+ | Scheme | `FP8_DYNAMIC` |
33
+ | Checkpoint format | `compressed-tensors` |
34
+ | Quantized modules | `Linear` layers |
35
+ | Excluded modules | `lm_head`, `embed_tokens`, `visual`, `linear_attn` |
36
+ | Calibration data | Not required |
37
+ | Inference engine | vLLM |
38
+
39
+ ### Recipe
40
+
41
+ ```yaml
42
+ default_stage:
43
+ default_modifiers:
44
+ QuantizationModifier:
45
+ targets: [Linear]
46
+ ignore: ['re:.*lm_head', 're:.*embed_tokens$', 're:.*visual.*', 're:.*model.visual.*',
47
+ 're:.*linear_attn.*']
48
+ scheme: FP8_DYNAMIC
49
+ bypass_divisibility_checks: false
50
+ requires_calibration_data: false
51
+ ```
52
+
53
+ ### Reproduction
54
+
55
+ ```python
56
+ import torch
57
+ from transformers import AutoModelForCausalLM, AutoTokenizer
58
+ from llmcompressor import oneshot
59
+
60
+ model_id = "Qwen/Qwen-Image-2.1-PE-T2I"
61
+ save_dir = "Qwen-Image-2.1-PE-T2I-FP8"
62
+
63
+ model = AutoModelForCausalLM.from_pretrained(
64
+ model_id, dtype=torch.bfloat16, device_map="auto"
65
+ )
66
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
67
+
68
+ # recipe.yaml is the recipe shown above
69
+ oneshot(model=model, recipe="recipe.yaml")
70
+
71
+ model.save_pretrained(save_dir, save_compressed=True)
72
+ tokenizer.save_pretrained(save_dir)
73
+ ```
74
+
75
+ ## Quick Start
76
+
77
+ ### Installation
78
+
79
+ ```bash
80
+ pip install vllm openai huggingface_hub
81
+ ```
82
+
83
+ Use a recent vLLM release with support for Qwen3.5-VL and compressed-tensors FP8 checkpoints. Native FP8 compute requires a GPU with compute capability 8.9 or higher (Ada Lovelace, Hopper, Blackwell). On older GPUs vLLM falls back to weight-only FP8 kernels.
84
+
85
+ ### Serve with vLLM
86
+
87
+ ```bash
88
+ vllm serve prithivMLmods/Qwen-Image-2.1-PE-T2I-FP8 \
89
+ --max-model-len 32768
90
+ ```
91
+
92
+ Do not enable a reasoning parser, so the thinking block is returned in the response content and can be split from the JSON answer as shown below.
93
+
94
+ ### Query the Server
95
+
96
+ ```python
97
+ import json
98
+
99
+ import huggingface_hub
100
+ from openai import OpenAI
101
+
102
+ model_id = "prithivMLmods/Qwen-Image-2.1-PE-T2I-FP8"
103
+ client = OpenAI(base_url="http://localhost:8000/v1", api_key="EMPTY")
104
+
105
+ # System prompt shipped with the base model
106
+ sys_prompt_path = huggingface_hub.hf_hub_download(
107
+ "Qwen/Qwen-Image-2.1-PE-T2I", "system_prompt.txt"
108
+ )
109
+ system_prompt = open(sys_prompt_path).read().strip()
110
+
111
+ user_prompt = "一只在雨中弹吉他的柯基"
112
+
113
+ response = client.chat.completions.create(
114
+ model=model_id,
115
+ messages=[
116
+ {"role": "system", "content": system_prompt},
117
+ {"role": "user", "content": user_prompt},
118
+ ],
119
+ max_tokens=16256,
120
+ temperature=1.0,
121
+ top_p=0.95,
122
+ seed=42,
123
+ extra_body={"top_k": 20},
124
+ )
125
+
126
+ gen = response.choices[0].message.content
127
+
128
+ # Split thinking from the answer
129
+ thinking, _, answer = gen.partition("</think>")
130
+ result = json.loads(answer.strip())
131
+ print(result)
132
+ # {"rewritten_prompt": "<long detailed English prompt>", "wh_ratio": "16:9"}
133
+ ```
134
+
135
+ ### Integration with Diffusers
136
+
137
+ ```python
138
+ import torch
139
+ from diffusers import QwenImage21Pipeline
140
+
141
+ WH_RATIO_TO_SIZE = {
142
+ "1:1": (2048, 2048), "4:3": (2400, 1792), "3:4": (1792, 2400),
143
+ "3:2": (2528, 1696), "2:3": (1696, 2528), "16:9": (2752, 1536),
144
+ "9:16": (1536, 2752),
145
+ }
146
+
147
+ # `result` from the vLLM call above
148
+ prompt = result["rewritten_prompt"]
149
+ width, height = WH_RATIO_TO_SIZE.get(result["wh_ratio"], (2048, 2048))
150
+
151
+ pipe = QwenImage21Pipeline.from_pretrained(
152
+ "Qwen/Qwen-Image-2.1", torch_dtype=torch.bfloat16
153
+ ).to("cuda")
154
+
155
+ image = pipe(
156
+ prompt=prompt,
157
+ width=width, height=height,
158
+ num_inference_steps=40,
159
+ generator=torch.Generator("cuda").manual_seed(42),
160
+ ).images[0]
161
+
162
+ image.save("rewritten_t2i.png")
163
+ ```
164
+
165
+ ## Output Format
166
+
167
+ The model emits a JSON object after a `<think>` reasoning block:
168
+
169
+ ```json
170
+ {
171
+ "rewritten_prompt": "<long detailed English prompt describing the finished image>",
172
+ "wh_ratio": "16:9"
173
+ }
174
+ ```
175
+
176
+ | Field | Description |
177
+ |---|---|
178
+ | `rewritten_prompt` | Expanded English prompt to pass to the image generation model |
179
+ | `wh_ratio` | Recommended aspect ratio for rendering |
180
+
181
+ The aspect ratios used in the Diffusers example map to the following output sizes:
182
+
183
+ | `wh_ratio` | Width x Height |
184
+ |---|---|
185
+ | `1:1` | 2048 x 2048 |
186
+ | `4:3` | 2400 x 1792 |
187
+ | `3:4` | 1792 x 2400 |
188
+ | `3:2` | 2528 x 1696 |
189
+ | `2:3` | 1696 x 2528 |
190
+ | `16:9` | 2752 x 1536 |
191
+ | `9:16` | 1536 x 2752 |
192
+
193
+ ## Notes
194
+
195
+ - FP8 quantization introduces small numerical differences from the BF16 original, so rewritten prompts may differ slightly in wording. Validate output quality on your own generation workloads before replacing the base model.
196
+ - Always parse the answer with a JSON loader and handle malformed output, since sampling at `temperature=1.0` can occasionally produce invalid JSON.
197
+ - Input requests can be written in any language, and the rewritten prompt is always English.
198
+
199
+ ## License
200
+
201
+ This model inherits the license of the base model and is distributed under the [Qwen Research License Agreement](./LICENSE).
202
+
203
+ ## Acknowledgements
204
+
205
+ Base model by the Qwen team: [Qwen/Qwen-Image-2.1-PE-T2I](https://huggingface.co/Qwen/Qwen-Image-2.1-PE-T2I). Quantization with [llm-compressor](https://github.com/vllm-project/llm-compressor). Serving with [vLLM](https://github.com/vllm-project/vllm). See the [Qwen-Image-2.1 GitHub repo](https://github.com/QwenLM/Qwen-Image-2.1) and [blog](https://qwen.ai/blog?id=qwen-image-2.1) for details on the full pipeline.