Spaces:
Runtime error
Runtime error
Fix AoT (#1)
Browse files- Fix AoT (35ee5b0267f6846d0c86392c55dd3bf67deb512c)
- Update app.py (152019b5b68dd7404d072590a79c42c29bd14814)
Co-authored-by: Charles Bensimon <cbensimon@users.noreply.huggingface.co>
Files changed (2)
- app.py+12-14
- optimization.py+46-29
1 unmodified line234556729 unmodified lines37383941424041424344484546474 unmodified lines525354596061626364656667685556575859606162636465666768691 unmodified lineimport subprocessimport sysimport iofrom kernels import get_kernelimport gradio as grimport numpy as npimport random29 unmodified lines return prompt_embeds
# Load modelfa3_kernel = get_kernel("kernels-community/flash-attn3", revision="fake-ops-return-probs")
repo_id = "black-forest-labs/FLUX.2-dev"
dit = Flux2Transformer2DModel.from_pretrained( repo_id, subfolder="transformer", attn_implementation=fa3_kernel, torch_dtype=torch.bfloat16)
4 unmodified lines torch_dtype=torch.bfloat16)pipe.to("cuda")# pipe.transformer.compile_repeated_blocks(dynamic=True)
#optimize_pipeline_(# pipe,# prompt_embeds=remote_text_encoder("prompt").to("cuda"),# guidance_scale=2.5,# width=1024,# height=1024,# num_inference_steps=1,#)
pipe.transformer.set_attention_backend("_flash_3_hub")
optimize_pipeline_( pipe, image=[Image.new("RGB", (1024, 1024))], prompt_embeds = remote_text_encoder("prompt").to("cuda"), guidance_scale=2.5, width=1024, height=1024, num_inference_steps=1)
@spaces.GPU(duration=180)def infer(prompt, input_images, seed=42, randomize_seed=False, width=1024, height=1024, num_inference_steps=50, guidance_scale=2.5, progress=gr.Progress(track_tqdm=True)):5 unmodified lines6789101112131413141516171817181920212223242526202122232425262727282930313233343536372 unmodified lines404142363743444546474842434950515253545556455758596049505152535455565758596061626364656667686970717273747576775 unmodified linesfrom typing import ParamSpecimport spacesimport torchfrom spaces.zero.torch.aoti import ZeroGPUCompiledModelfrom spaces.zero.torch.aoti import ZeroGPUWeightsfrom torch.utils._pytree import tree_map
P = ParamSpec('P')
TRANSFORMER_IMAGE_SEQ_LENGTH_DIM = torch.export.Dim('image_seq_length', min=64, max=16384)TRANSFORMER_TEXT_SEQ_LENGTH_DIM = torch.export.Dim('text_seq_length', min=64, max=512)TRANSFORMER_IMAGE_DIM = torch.export.Dim('image_seq_length', min=4096, max=16384) # min: 0 images, max: 3 (1024x1024) images
TRANSFORMER_DYNAMIC_SHAPES = { 'double': { 'hidden_states': { 1: TRANSFORMER_IMAGE_SEQ_LENGTH_DIM, 'hidden_states': { 1: TRANSFORMER_IMAGE_DIM, }, 'image_rotary_emb': ( {0: TRANSFORMER_IMAGE_DIM + 512}, {0: TRANSFORMER_IMAGE_DIM + 512}, ), }, 'encoder_hidden_states': { 1: TRANSFORMER_TEXT_SEQ_LENGTH_DIM, }, 'img_ids': { 1: TRANSFORMER_IMAGE_SEQ_LENGTH_DIM, }, 'txt_ids': { 1: TRANSFORMER_TEXT_SEQ_LENGTH_DIM, 'single': { 'hidden_states': { 1: TRANSFORMER_IMAGE_DIM + 512, }, 'image_rotary_emb': ( {0: TRANSFORMER_IMAGE_DIM + 512}, {0: TRANSFORMER_IMAGE_DIM + 512}, ), },}
2 unmodified lines 'epilogue_fusion': False, 'coordinate_descent_tuning': True, 'coordinate_descent_check_all_directions': True, #'max_autotune': True, #'triton.cudagraphs': True, 'max_autotune': True, 'triton.cudagraphs': True,}
def optimize_pipeline_(pipeline: Callable[P, Any], *args: P.args, **kwargs: P.kwargs):
@spaces.GPU(duration=1500) def compile_transformer(): blocks = { 'double': pipeline.transformer.transformer_blocks, 'single': pipeline.transformer.single_transformer_blocks, }
@spaces.GPU(duration=1200) def compile_block(blocks_kind: str): block = blocks[blocks_kind][0] with spaces.aoti_capture(pipeline.transformer) as call: with spaces.aoti_capture(block) as call: pipeline(*args, **kwargs)
dynamic_shapes = tree_map(lambda t: None, call.kwargs) dynamic_shapes |= TRANSFORMER_DYNAMIC_SHAPES exported = torch.export.export( mod=pipeline.transformer, args=call.args, kwargs=call.kwargs, dynamic_shapes=dynamic_shapes, )
return spaces.aoti_compile(exported, INDUCTOR_CONFIGS)
spaces.aoti_apply(compile_transformer(), pipeline.transformer) dynamic_shapes |= TRANSFORMER_DYNAMIC_SHAPES[blocks_kind]
with torch.no_grad(): exported = torch.export.export( mod=block, args=call.args, kwargs=call.kwargs, dynamic_shapes=dynamic_shapes, )
return spaces.aoti_compile(exported, INDUCTOR_CONFIGS).archive_file
for blocks_kind in ('double', 'single'): archive_file = compile_block(blocks_kind) for block in blocks[blocks_kind]: weights = ZeroGPUWeights(block.state_dict()) block.forward = ZeroGPUCompiledModel(archive_file, weights)app.py+12-14
optimization.py+46-29