Spaces:
Runtime error
Runtime error
Delete optimization.py
Browse files- optimization.py +0 -60
optimization.py
DELETED
|
@@ -1,60 +0,0 @@
|
|
| 1 |
-
"""
|
| 2 |
-
"""
|
| 3 |
-
|
| 4 |
-
from typing import Any
|
| 5 |
-
from typing import Callable
|
| 6 |
-
from typing import ParamSpec
|
| 7 |
-
|
| 8 |
-
import spaces
|
| 9 |
-
import torch
|
| 10 |
-
from torch.utils._pytree import tree_map_only
|
| 11 |
-
|
| 12 |
-
from optimization_utils import capture_component_call
|
| 13 |
-
from optimization_utils import aoti_compile
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
P = ParamSpec('P')
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
TRANSFORMER_HIDDEN_DIM = torch.export.Dim('hidden', min=4096, max=8212)
|
| 20 |
-
|
| 21 |
-
TRANSFORMER_DYNAMIC_SHAPES = {
|
| 22 |
-
'hidden_states': {1: TRANSFORMER_HIDDEN_DIM},
|
| 23 |
-
'img_ids': {0: TRANSFORMER_HIDDEN_DIM},
|
| 24 |
-
}
|
| 25 |
-
|
| 26 |
-
INDUCTOR_CONFIGS = {
|
| 27 |
-
'conv_1x1_as_mm': True,
|
| 28 |
-
'epilogue_fusion': False,
|
| 29 |
-
'coordinate_descent_tuning': True,
|
| 30 |
-
'coordinate_descent_check_all_directions': True,
|
| 31 |
-
'max_autotune': True,
|
| 32 |
-
'triton.cudagraphs': True,
|
| 33 |
-
}
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
def optimize_pipeline_(pipeline: Callable[P, Any], *args: P.args, **kwargs: P.kwargs):
|
| 37 |
-
|
| 38 |
-
@spaces.GPU(duration=1500)
|
| 39 |
-
def compile_transformer():
|
| 40 |
-
|
| 41 |
-
with capture_component_call(pipeline, 'transformer') as call:
|
| 42 |
-
pipeline(*args, **kwargs)
|
| 43 |
-
|
| 44 |
-
dynamic_shapes = tree_map_only((torch.Tensor, bool), lambda t: None, call.kwargs)
|
| 45 |
-
dynamic_shapes |= TRANSFORMER_DYNAMIC_SHAPES
|
| 46 |
-
|
| 47 |
-
pipeline.transformer.fuse_qkv_projections()
|
| 48 |
-
|
| 49 |
-
exported = torch.export.export(
|
| 50 |
-
mod=pipeline.transformer,
|
| 51 |
-
args=call.args,
|
| 52 |
-
kwargs=call.kwargs,
|
| 53 |
-
dynamic_shapes=dynamic_shapes,
|
| 54 |
-
)
|
| 55 |
-
|
| 56 |
-
return aoti_compile(exported, INDUCTOR_CONFIGS)
|
| 57 |
-
|
| 58 |
-
transformer_config = pipeline.transformer.config
|
| 59 |
-
pipeline.transformer = compile_transformer()
|
| 60 |
-
pipeline.transformer.config = transformer_config # pyright: ignore[reportAttributeAccessIssue]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|