CoolFace
Apppublic

mombeeru/FLUX.2-dev

sourceHugging Faceupdated 10mo agoView on Hugging Face
0likes
optimization.py78 linesDownload Raw Back to root
1"""2"""3 4from typing import Any5from typing import Callable6from typing import ParamSpec7import spaces8import torch9from spaces.zero.torch.aoti import ZeroGPUCompiledModel10from spaces.zero.torch.aoti import ZeroGPUWeights11from torch.utils._pytree import tree_map12 13P = ParamSpec('P')14 15TRANSFORMER_IMAGE_DIM = torch.export.Dim('image_seq_length', min=4096, max=16384) # min: 0 images, max: 3 (1024x1024) images16 17TRANSFORMER_DYNAMIC_SHAPES = {18    'double': {19        'hidden_states': {20            1: TRANSFORMER_IMAGE_DIM,21        },22        'image_rotary_emb': (23            {0: TRANSFORMER_IMAGE_DIM + 512},24            {0: TRANSFORMER_IMAGE_DIM + 512},25        ),26    },27    'single': {28        'hidden_states': {29            1: TRANSFORMER_IMAGE_DIM + 512,30        },31        'image_rotary_emb': (32            {0: TRANSFORMER_IMAGE_DIM + 512},33            {0: TRANSFORMER_IMAGE_DIM + 512},34        ),35    },36}37 38INDUCTOR_CONFIGS = {39    'conv_1x1_as_mm': True,40    'epilogue_fusion': False,41    'coordinate_descent_tuning': True,42    'coordinate_descent_check_all_directions': True,43    'max_autotune': True,44    'triton.cudagraphs': True,45}46 47def optimize_pipeline_(pipeline: Callable[P, Any], *args: P.args, **kwargs: P.kwargs):48 49    blocks = {50        'double': pipeline.transformer.transformer_blocks,51        'single': pipeline.transformer.single_transformer_blocks,52    }53 54    @spaces.GPU(duration=1200)55    def compile_block(blocks_kind: str):56        block = blocks[blocks_kind][0]57        with spaces.aoti_capture(block) as call:58            pipeline(*args, **kwargs)59 60        dynamic_shapes = tree_map(lambda t: None, call.kwargs)61        dynamic_shapes |= TRANSFORMER_DYNAMIC_SHAPES[blocks_kind]62 63        with torch.no_grad():      64            exported = torch.export.export(65                mod=block,66                args=call.args,67                kwargs=call.kwargs,68                dynamic_shapes=dynamic_shapes,69            )70 71        return spaces.aoti_compile(exported, INDUCTOR_CONFIGS).archive_file72 73    for blocks_kind in ('double', 'single'):74        archive_file = compile_block(blocks_kind)75        for block in blocks[blocks_kind]:76            weights = ZeroGPUWeights(block.state_dict())77            block.forward = ZeroGPUCompiledModel(archive_file, weights)78