AgentNewTwo commited on
Commit
5af9119
·
1 Parent(s): 2137a28

Quantize model stack for ZeroGPU memory limits

Browse files
Files changed (3) hide show
  1. AGENT.md +4 -0
  2. README.md +2 -0
  3. app.py +19 -1
AGENT.md CHANGED
@@ -13,6 +13,10 @@ The base model, accelerated transformer, and Diffusers dependency are pinned to
13
  exact revisions. Update those pins deliberately and test the deployed Space
14
  before merging.
15
 
 
 
 
 
16
  ## Privacy boundary
17
 
18
  Image inference runs inside the Space. Do not add application code that sends
 
13
  exact revisions. Update those pins deliberately and test the deployed Space
14
  before merging.
15
 
16
+ The transformer and text encoder are dynamically quantized to FP8 before the
17
+ pipeline is moved to its runtime device. Keep that ordering: the unquantized
18
+ BF16 pipeline is too large for the ZeroGPU transfer path.
19
+
20
  ## Privacy boundary
21
 
22
  Image inference runs inside the Space. Do not add application code that sends
README.md CHANGED
@@ -23,3 +23,5 @@ Pro Realism Edit Studio is a powerful image editor powered by [Qwen-Image-Edit-2
23
  ## Reproducibility
24
 
25
  The base model, accelerated transformer, and development version of Diffusers are pinned to exact revisions. If the accelerated four-step transformer cannot load, the Space stops with a clear error rather than silently switching to an incompatible model.
 
 
 
23
  ## Reproducibility
24
 
25
  The base model, accelerated transformer, and development version of Diffusers are pinned to exact revisions. If the accelerated four-step transformer cannot load, the Space stops with a clear error rather than silently switching to an incompatible model.
26
+
27
+ The transformer and text encoder use dynamic FP8 quantization so the pipeline fits within Hugging Face ZeroGPU memory. This can introduce a small numerical difference from full BF16 inference, but avoids the accelerator failure caused by transferring an approximately 58 GB BF16 pipeline.
app.py CHANGED
@@ -5,6 +5,8 @@ import torch
5
  import spaces
6
 
7
  from PIL import Image
 
 
8
  from qwenimage.pipeline_qwenimage_edit_plus import QwenImageEditPlusPipeline
9
  from qwenimage.transformer_qwenimage import QwenImageTransformer2DModel
10
  from qwenimage.qwen_fa3_processor import QwenDoubleStreamAttnProcessorFA3
@@ -66,12 +68,28 @@ pipe = QwenImageEditPlusPipeline.from_pretrained(
66
  transformer=transformer,
67
  torch_dtype=dtype,
68
  token=hf_token,
69
- ).to(device)
70
 
71
  del transformer
72
  if torch.cuda.is_available():
73
  torch.cuda.empty_cache()
74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
  # Apply the upstream FA3 optimization, but keep boot resilient if kernel support
76
  # differs on duplicated Spaces or the public fallback transformer.
77
  try:
 
5
  import spaces
6
 
7
  from PIL import Image
8
+ from torchao.quantization import Float8DynamicActivationFloat8WeightConfig
9
+ from torchao.quantization import quantize_
10
  from qwenimage.pipeline_qwenimage_edit_plus import QwenImageEditPlusPipeline
11
  from qwenimage.transformer_qwenimage import QwenImageTransformer2DModel
12
  from qwenimage.qwen_fa3_processor import QwenDoubleStreamAttnProcessorFA3
 
68
  transformer=transformer,
69
  torch_dtype=dtype,
70
  token=hf_token,
71
+ )
72
 
73
  del transformer
74
  if torch.cuda.is_available():
75
  torch.cuda.empty_cache()
76
 
77
+ # A full BF16 pipeline is roughly 58 GB and cannot be transferred into a
78
+ # standard ZeroGPU allocation. Dynamic FP8 cuts the transformer/text-encoder
79
+ # footprint enough to fit while preserving four-step Rapid-AIO behavior.
80
+ fp8_config = Float8DynamicActivationFloat8WeightConfig()
81
+ quantize_(pipe.transformer, fp8_config)
82
+ try:
83
+ quantize_(pipe.text_encoder, fp8_config)
84
+ except Exception as exc:
85
+ print(
86
+ f"Text-encoder FP8 quantization unavailable ({type(exc).__name__}); "
87
+ "continuing with the pinned text encoder.",
88
+ flush=True,
89
+ )
90
+
91
+ pipe = pipe.to(device)
92
+
93
  # Apply the upstream FA3 optimization, but keep boot resilient if kernel support
94
  # differs on duplicated Spaces or the public fallback transformer.
95
  try: