moose Claude Opus 4.5 commited on
Commit ·
bca4977
1
Parent(s): 07e1710
Fix device mismatch during AOT compilation warmup
Browse filesMove text encoder offloading inside optimize_pipeline_() to happen
AFTER the warmup pass but BEFORE torch.export. This ensures:
1. Warmup run has all components on GPU (avoids CPU/CUDA mismatch)
2. torch.export has reduced memory (text encoder on CPU)
3. Text encoder returns to GPU after compilation
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Files changed (2)
- app.py+1-9
- optimization.py+17-2
1234118 unmodified lines123124125126127128129130131132133134135136137138import gcimport gradio as grimport numpy as npimport random118 unmodified linespipe.transformer.set_attn_processor(QwenDoubleStreamAttnProcessorFA3())# --- Ahead-of-time compilation ---# Temporarily offload text encoder to CPU to free ~16GB during memory-intensive torch.exportpipe.text_encoder.to('cpu')gc.collect()torch.cuda.empty_cache()optimize_pipeline_(pipe, image=[Image.new("RGB", (1024, 1024)), Image.new("RGB", (1024, 1024))], prompt="prompt")# Move text encoder back to GPU for inferencepipe.text_encoder.to(device)# --- UI Constants and Helpers ---MAX_SEED = np.iinfo(np.int32).max123118 unmodified lines122123124125126127128129130import gradio as grimport numpy as npimport random118 unmodified linespipe.transformer.set_attn_processor(QwenDoubleStreamAttnProcessorFA3())# --- Ahead-of-time compilation ---# Note: optimize_pipeline_ handles text encoder offloading internally to save memory during torch.exportoptimize_pipeline_(pipe, image=[Image.new("RGB", (1024, 1024)), Image.new("RGB", (1024, 1024))], prompt="prompt")# --- UI Constants and Helpers ---MAX_SEED = np.iinfo(np.int32).max
12345643 unmodified lines50515253545556575859606162631 unmodified line656667686970""""""from typing import Anyfrom typing import Callablefrom typing import ParamSpec43 unmodified lines@spaces.GPU(duration=1500)def compile_transformer():with spaces.aoti_capture(pipeline.transformer) as call:pipeline(*args, **kwargs)dynamic_shapes = tree_map(lambda t: None, call.kwargs)dynamic_shapes |= TRANSFORMER_DYNAMIC_SHAPES# quantize_(pipeline.transformer, Float8DynamicActivationFloat8WeightConfig())exported = torch.export.export(mod=pipeline.transformer,args=call.args,1 unmodified linedynamic_shapes=dynamic_shapes,)return spaces.aoti_compile(exported, INDUCTOR_CONFIGS)spaces.aoti_apply(compile_transformer(), pipeline.transformer)1234567843 unmodified lines525354555657585960616263646566676869707172731 unmodified line7576777879808182838485"""AOT compilation optimization for Qwen-Image-Edit pipeline."""import gcfrom typing import Anyfrom typing import Callablefrom typing import ParamSpec43 unmodified lines@spaces.GPU(duration=1500)def compile_transformer():# Run warmup pass to capture transformer inputs (needs all components on GPU)with spaces.aoti_capture(pipeline.transformer) as call:pipeline(*args, **kwargs)# Offload text encoder to CPU to free ~16GB during memory-intensive torch.export# This happens AFTER warmup but BEFORE export to avoid device mismatchtext_encoder_device = next(pipeline.text_encoder.parameters()).devicepipeline.text_encoder.to('cpu')gc.collect()torch.cuda.empty_cache()dynamic_shapes = tree_map(lambda t: None, call.kwargs)dynamic_shapes |= TRANSFORMER_DYNAMIC_SHAPES# quantize_(pipeline.transformer, Float8DynamicActivationFloat8WeightConfig())exported = torch.export.export(mod=pipeline.transformer,args=call.args,1 unmodified linedynamic_shapes=dynamic_shapes,)compiled = spaces.aoti_compile(exported, INDUCTOR_CONFIGS)# Move text encoder back to original devicepipeline.text_encoder.to(text_encoder_device)return compiledspaces.aoti_apply(compile_transformer(), pipeline.transformer)
app.py+1-9
optimization.py+17-2