Spaces:
Running
on
Zero
Running
on
Zero
Delete optimization_utils.py
Browse files- optimization_utils.py +0 -96
optimization_utils.py
DELETED
|
@@ -1,96 +0,0 @@
|
|
| 1 |
-
"""
|
| 2 |
-
"""
|
| 3 |
-
import contextlib
|
| 4 |
-
from contextvars import ContextVar
|
| 5 |
-
from io import BytesIO
|
| 6 |
-
from typing import Any
|
| 7 |
-
from typing import cast
|
| 8 |
-
from unittest.mock import patch
|
| 9 |
-
|
| 10 |
-
import torch
|
| 11 |
-
from torch._inductor.package.package import package_aoti
|
| 12 |
-
from torch.export.pt2_archive._package import AOTICompiledModel
|
| 13 |
-
from torch.export.pt2_archive._package_weights import TensorProperties
|
| 14 |
-
from torch.export.pt2_archive._package_weights import Weights
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
INDUCTOR_CONFIGS_OVERRIDES = {
|
| 18 |
-
'aot_inductor.package_constants_in_so': False,
|
| 19 |
-
'aot_inductor.package_constants_on_disk': True,
|
| 20 |
-
'aot_inductor.package': True,
|
| 21 |
-
}
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
class ZeroGPUCompiledModel:
|
| 25 |
-
def __init__(self, archive_file: torch.types.FileLike, weights: Weights, cuda: bool = False):
|
| 26 |
-
self.archive_file = archive_file
|
| 27 |
-
self.weights = weights
|
| 28 |
-
if cuda:
|
| 29 |
-
self.weights_to_cuda_()
|
| 30 |
-
self.compiled_model: ContextVar[AOTICompiledModel | None] = ContextVar('compiled_model', default=None)
|
| 31 |
-
def weights_to_cuda_(self):
|
| 32 |
-
for name in self.weights:
|
| 33 |
-
tensor, properties = self.weights.get_weight(name)
|
| 34 |
-
self.weights[name] = (tensor.to('cuda'), properties)
|
| 35 |
-
def __call__(self, *args, **kwargs):
|
| 36 |
-
if (compiled_model := self.compiled_model.get()) is None:
|
| 37 |
-
constants_map = {name: value[0] for name, value in self.weights.items()}
|
| 38 |
-
compiled_model = cast(AOTICompiledModel, torch._inductor.aoti_load_package(self.archive_file))
|
| 39 |
-
compiled_model.load_constants(constants_map, check_full_update=True, user_managed=True)
|
| 40 |
-
self.compiled_model.set(compiled_model)
|
| 41 |
-
return compiled_model(*args, **kwargs)
|
| 42 |
-
def __reduce__(self):
|
| 43 |
-
weight_dict: dict[str, tuple[torch.Tensor, TensorProperties]] = {}
|
| 44 |
-
for name in self.weights:
|
| 45 |
-
tensor, properties = self.weights.get_weight(name)
|
| 46 |
-
tensor_ = torch.empty_like(tensor, device='cpu').pin_memory()
|
| 47 |
-
weight_dict[name] = (tensor_.copy_(tensor).detach().share_memory_(), properties)
|
| 48 |
-
return ZeroGPUCompiledModel, (self.archive_file, Weights(weight_dict), True)
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
def aoti_compile(
|
| 52 |
-
exported_program: torch.export.ExportedProgram,
|
| 53 |
-
inductor_configs: dict[str, Any] | None = None,
|
| 54 |
-
):
|
| 55 |
-
inductor_configs = (inductor_configs or {}) | INDUCTOR_CONFIGS_OVERRIDES
|
| 56 |
-
gm = cast(torch.fx.GraphModule, exported_program.module())
|
| 57 |
-
assert exported_program.example_inputs is not None
|
| 58 |
-
args, kwargs = exported_program.example_inputs
|
| 59 |
-
artifacts = torch._inductor.aot_compile(gm, args, kwargs, options=inductor_configs)
|
| 60 |
-
archive_file = BytesIO()
|
| 61 |
-
files: list[str | Weights] = [file for file in artifacts if isinstance(file, str)]
|
| 62 |
-
package_aoti(archive_file, files)
|
| 63 |
-
weights, = (artifact for artifact in artifacts if isinstance(artifact, Weights))
|
| 64 |
-
return ZeroGPUCompiledModel(archive_file, weights)
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
@contextlib.contextmanager
|
| 68 |
-
def capture_component_call(
|
| 69 |
-
pipeline: Any,
|
| 70 |
-
component_name: str,
|
| 71 |
-
component_method='forward',
|
| 72 |
-
):
|
| 73 |
-
|
| 74 |
-
class CapturedCallException(Exception):
|
| 75 |
-
def __init__(self, *args, **kwargs):
|
| 76 |
-
super().__init__()
|
| 77 |
-
self.args = args
|
| 78 |
-
self.kwargs = kwargs
|
| 79 |
-
|
| 80 |
-
class CapturedCall:
|
| 81 |
-
def __init__(self):
|
| 82 |
-
self.args: tuple[Any, ...] = ()
|
| 83 |
-
self.kwargs: dict[str, Any] = {}
|
| 84 |
-
|
| 85 |
-
component = getattr(pipeline, component_name)
|
| 86 |
-
captured_call = CapturedCall()
|
| 87 |
-
|
| 88 |
-
def capture_call(*args, **kwargs):
|
| 89 |
-
raise CapturedCallException(*args, **kwargs)
|
| 90 |
-
|
| 91 |
-
with patch.object(component, component_method, new=capture_call):
|
| 92 |
-
try:
|
| 93 |
-
yield captured_call
|
| 94 |
-
except CapturedCallException as e:
|
| 95 |
-
captured_call.args = e.args
|
| 96 |
-
captured_call.kwargs = e.kwargs
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|