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 line234567829 unmodified lines38394041424344454647484950514 unmodified lines565758596061626364656667686970711 unmodified lineimport subprocessimport sysimport iofrom kernels import get_kernelimport gradio as grimport numpy as npimport random29 unmodified linesreturn 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 linestorch_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,#)@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)):1 unmodified line23456729 unmodified lines37383940414243444546474 unmodified lines5253545556575859606162636465666768691 unmodified lineimport subprocessimport sysimport ioimport gradio as grimport numpy as npimport random29 unmodified linesreturn prompt_embeds# Load modelrepo_id = "black-forest-labs/FLUX.2-dev"dit = Flux2Transformer2DModel.from_pretrained(repo_id,subfolder="transformer",torch_dtype=torch.bfloat16)4 unmodified linestorch_dtype=torch.bfloat16)pipe.to("cuda")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 lines67891011121314151617181920212223242526272829302 unmodified lines333435363738394041424344454647484950515253545556575859605 unmodified linesfrom typing import ParamSpecimport spacesimport torchfrom torch.utils._pytree import tree_mapP = 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_DYNAMIC_SHAPES = {'hidden_states': {1: TRANSFORMER_IMAGE_SEQ_LENGTH_DIM,},'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,},}2 unmodified lines'epilogue_fusion': False,'coordinate_descent_tuning': True,'coordinate_descent_check_all_directions': 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():with spaces.aoti_capture(pipeline.transformer) as call:pipeline(*args, **kwargs)dynamic_shapes = tree_map(lambda t: None, call.kwargs)dynamic_shapes |= TRANSFORMER_DYNAMIC_SHAPESexported = 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)5 unmodified lines6789101112131415161718192021222324252627282930313233343536372 unmodified lines40414243444546474849505152535455565758596061626364656667686970717273747576775 unmodified linesfrom typing import ParamSpecimport spacesimport torchfrom spaces.zero.torch.aoti import ZeroGPUCompiledModelfrom spaces.zero.torch.aoti import ZeroGPUWeightsfrom torch.utils._pytree import tree_mapP = ParamSpec('P')TRANSFORMER_IMAGE_DIM = torch.export.Dim('image_seq_length', min=4096, max=16384) # min: 0 images, max: 3 (1024x1024) imagesTRANSFORMER_DYNAMIC_SHAPES = {'double': {'hidden_states': {1: TRANSFORMER_IMAGE_DIM,},'image_rotary_emb': ({0: TRANSFORMER_IMAGE_DIM + 512},{0: TRANSFORMER_IMAGE_DIM + 512},),},'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,}def optimize_pipeline_(pipeline: Callable[P, Any], *args: P.args, **kwargs: P.kwargs):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(block) as call:pipeline(*args, **kwargs)dynamic_shapes = tree_map(lambda t: None, call.kwargs)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_filefor 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