已合并
run_once_fix #38394
cuiduo创建于 6月12日
run_once_fix #38394
已合并
共 7 个文件变更+22-34
| @@ -2,6 +2,7 @@ import torch | |||
| 2 | from torch.testing._internal.common_utils import run_tests, parametrize, instantiate_parametrized_tests | 2 | from torch.testing._internal.common_utils import run_tests, parametrize, instantiate_parametrized_tests |
| 3 | from testutils import TestUtils | 3 | from testutils import TestUtils |
| 4 | import torch_npu | 4 | import torch_npu |
| 5 | +import torch_npu._inductor | ||
| 5 | 6 | ||
| 6 | 7 | ||
| 7 | class TestAlias(TestUtils): | 8 | class TestAlias(TestUtils): |
| @@ -58,7 +58,6 @@ from ..npu.codegen.wrapper import NpuMlirWrapperCodeGen | |||
| 58 | from ..npu.npu_lowering import _register_npu_inductor_fallbacks | 58 | from ..npu.npu_lowering import _register_npu_inductor_fallbacks |
| 59 | from ..npu.utils import ( | 59 | from ..npu.utils import ( |
| 60 | npu_optimize_fx_graph, | 60 | npu_optimize_fx_graph, |
| 61 | - run_once, | ||
| 62 | logger, | 61 | logger, |
| 63 | ) | 62 | ) |
| 64 | from .. import config as anir_config | 63 | from .. import config as anir_config |
| @@ -9,7 +9,9 @@ from torch_npu._inductor.lowering_common import ( | |||
| 9 | ) | 9 | ) |
| 10 | from .. import config | 10 | from .. import config |
| 11 | from .utils import logger | 11 | from .utils import logger |
| 12 | -from ..npu.utils import run_once, get_anir_mode | 12 | +from ..npu.utils import get_anir_mode |
| 13 | +from torch_npu._inductor.lowering_common import run_once | ||
| 14 | + | ||
| 13 | 15 | ||
| 14 | aten = torch.ops.aten | 16 | aten = torch.ops.aten |
| 15 | tr_c10d = torch.ops.tr_c10d | 17 | tr_c10d = torch.ops.tr_c10d |
| @@ -17,27 +17,12 @@ from torch._dynamo.symbolic_convert import break_graph_if_unsupported, Instructi | |||
| 17 | from torch._dynamo.exc import Unsupported | 17 | from torch._dynamo.exc import Unsupported |
| 18 | from torch._dynamo.variables.lists import TupleVariable | 18 | from torch._dynamo.variables.lists import TupleVariable |
| 19 | from torch._dynamo.variables.nn_module import NNModuleVariable | 19 | from torch._dynamo.variables.nn_module import NNModuleVariable |
| 20 | +from torch_npu._inductor.lowering_common import run_once | ||
| 20 | 21 | ||
| 21 | 22 | ||
| 22 | aten = torch.ops.aten | 23 | aten = torch.ops.aten |
| 23 | npu = torch.ops.npu | 24 | npu = torch.ops.npu |
| 24 | 25 | ||
| 25 | - | ||
| 26 | -def run_once(f): | ||
| 27 | - """Runs a function (successfully) only once. | ||
| 28 | - The running can be reset by setting the `has_run` attribute to False | ||
| 29 | - """ | ||
| 30 | - | ||
| 31 | - def wrapper(*args, **kwargs): | ||
| 32 | - if not wrapper.has_run: | ||
| 33 | - result = f(*args, **kwargs) | ||
| 34 | - wrapper.has_run = True | ||
| 35 | - return result | ||
| 36 | - return None | ||
| 37 | - wrapper.has_run = False | ||
| 38 | - return wrapper | ||
| 39 | - | ||
| 40 | - | ||
| 41 | npu_meta_table = {} | 26 | npu_meta_table = {} |
| 42 | break_fn_table = {} | 27 | break_fn_table = {} |
| 43 | break_mapping_table = {} | 28 | break_mapping_table = {} |
| @@ -50,20 +50,6 @@ MLIR_DTYPE_MAPPING = { | |||
| 50 | "si64" : torch.int64 | 50 | "si64" : torch.int64 |
| 51 | } | 51 | } |
| 52 | 52 | ||
| 53 | -def run_once(f): | ||
| 54 | - """Runs a function (successfully) only once. | ||
| 55 | - The running can be reset by setting the `has_run` attribute to False | ||
| 56 | - """ | ||
| 57 | - | ||
| 58 | - def wrapper(*args, **kwargs): | ||
| 59 | - if not wrapper.has_run: | ||
| 60 | - result = f(*args, **kwargs) | ||
| 61 | - wrapper.has_run = True | ||
| 62 | - return result | ||
| 63 | - return None | ||
| 64 | - wrapper.has_run = False | ||
| 65 | - return wrapper | ||
| 66 | - | ||
| 67 | def get_device_info(example_inputs) -> Union[Tuple[str, int], None]: | 53 | def get_device_info(example_inputs) -> Union[Tuple[str, int], None]: |
| 68 | for inp in example_inputs: | 54 | for inp in example_inputs: |
| 69 | if isinstance(inp, torch.Tensor): | 55 | if isinstance(inp, torch.Tensor): |
| @@ -9,10 +9,9 @@ from torch._C import DispatchKey | |||
| 9 | from torch._decomp import remove_decompositions | 9 | from torch._decomp import remove_decompositions |
| 10 | from torch._prims_common.wrappers import out_wrapper | 10 | from torch._prims_common.wrappers import out_wrapper |
| 11 | import torch.nn.functional as F | 11 | import torch.nn.functional as F |
| 12 | - | ||
| 13 | from .lowering_common import add_overload | 12 | from .lowering_common import add_overload |
| 14 | from .ascend_npu_ir.ascend_npu_ir import config as anir_config | 13 | from .ascend_npu_ir.ascend_npu_ir import config as anir_config |
| 15 | -from .ascend_npu_ir.ascend_npu_ir.npu.utils import run_once | 14 | +from .lowering_common import run_once |
| 16 | 15 | ||
| 17 | aten = torch.ops.aten | 16 | aten = torch.ops.aten |
| 18 | npu = torch.ops.npu | 17 | npu = torch.ops.npu |
| @@ -17,6 +17,7 @@ from __future__ import annotations | |||
| 17 | import inspect | 17 | import inspect |
| 18 | import sympy | 18 | import sympy |
| 19 | from functools import reduce | 19 | from functools import reduce |
| 20 | +import functools | ||
| 20 | from dataclasses import dataclass | 21 | from dataclasses import dataclass |
| 21 | from typing import Any, Callable, Dict, List, Optional, Tuple, Union | 22 | from typing import Any, Callable, Dict, List, Optional, Tuple, Union |
| 22 | 23 | ||
| @@ -40,6 +41,21 @@ LOWERING_REGISTRY_ATTRS: tuple[str, ...] = ( | |||
| 40 | ) | 41 | ) |
| 41 | 42 | ||
| 42 | 43 | ||
| 44 | +def run_once(f): | ||
| 45 | + """Runs a function (successfully) only once. | ||
| 46 | + The running can be reset by setting the `has_run` attribute to False | ||
| 47 | + """ | ||
| 48 | + | ||
| 49 | + def wrapper(*args, **kwargs): | ||
| 50 | + if not wrapper.has_run: | ||
| 51 | + result = f(*args, **kwargs) | ||
| 52 | + wrapper.has_run = True | ||
| 53 | + return result | ||
| 54 | + return None | ||
| 55 | + wrapper.has_run = False | ||
| 56 | + return wrapper | ||
| 57 | + | ||
| 58 | + | ||
| 43 | def get_module_functions(module: Any) -> dict[str, Callable[..., Any]]: | 59 | def get_module_functions(module: Any) -> dict[str, Callable[..., Any]]: |
| 44 | functions: dict[str, Callable[..., Any]] = {} | 60 | functions: dict[str, Callable[..., Any]] = {} |
| 45 | for name, func in inspect.getmembers(module, inspect.isfunction): | 61 | for name, func in inspect.getmembers(module, inspect.isfunction): |