已合并
run_once_fix #38394
cuiduo创建于 6月12日
run_once_fix #38394
已合并
cuiduo创建于 6月12日
7 个文件变更+22-34
@@ -2,6 +2,7 @@ import torch
2from torch.testing._internal.common_utils import run_tests, parametrize, instantiate_parametrized_tests2from torch.testing._internal.common_utils import run_tests, parametrize, instantiate_parametrized_tests
3from testutils import TestUtils3from testutils import TestUtils
4import torch_npu4import torch_npu
5+import torch_npu._inductor
5 6 
6 7 
7class TestAlias(TestUtils):8class TestAlias(TestUtils):
@@ -58,7 +58,6 @@ from ..npu.codegen.wrapper import NpuMlirWrapperCodeGen
58from ..npu.npu_lowering import _register_npu_inductor_fallbacks58from ..npu.npu_lowering import _register_npu_inductor_fallbacks
59from ..npu.utils import (59from ..npu.utils import (
60 npu_optimize_fx_graph,60 npu_optimize_fx_graph,
61- run_once,
62 logger,61 logger,
63)62)
64from .. import config as anir_config63from .. import config as anir_config
@@ -9,7 +9,9 @@ from torch_npu._inductor.lowering_common import (
9)9)
10from .. import config10from .. import config
11from .utils import logger11from .utils import logger
12-from ..npu.utils import run_once, get_anir_mode12+from ..npu.utils import get_anir_mode
13+from torch_npu._inductor.lowering_common import run_once
14+ 
13 15 
14aten = torch.ops.aten16aten = torch.ops.aten
15tr_c10d = torch.ops.tr_c10d17tr_c10d = torch.ops.tr_c10d
@@ -17,27 +17,12 @@ from torch._dynamo.symbolic_convert import break_graph_if_unsupported, Instructi
17from torch._dynamo.exc import Unsupported17from torch._dynamo.exc import Unsupported
18from torch._dynamo.variables.lists import TupleVariable18from torch._dynamo.variables.lists import TupleVariable
19from torch._dynamo.variables.nn_module import NNModuleVariable19from torch._dynamo.variables.nn_module import NNModuleVariable
20+from torch_npu._inductor.lowering_common import run_once
20 21 
21 22 
22aten = torch.ops.aten23aten = torch.ops.aten
23npu = torch.ops.npu24npu = 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- @wraps(f)
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- 
41npu_meta_table = {}26npu_meta_table = {}
42break_fn_table = {}27break_fn_table = {}
43break_mapping_table = {}28break_mapping_table = {}
@@ -50,20 +50,6 @@ MLIR_DTYPE_MAPPING = {
50 "si64" : torch.int6450 "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- @functools.wraps(f)
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- 
67def get_device_info(example_inputs) -> Union[Tuple[str, int], None]:53def 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
9from torch._decomp import remove_decompositions9from torch._decomp import remove_decompositions
10from torch._prims_common.wrappers import out_wrapper10from torch._prims_common.wrappers import out_wrapper
11import torch.nn.functional as F11import torch.nn.functional as F
12- 
13from .lowering_common import add_overload12from .lowering_common import add_overload
14from .ascend_npu_ir.ascend_npu_ir import config as anir_config13from .ascend_npu_ir.ascend_npu_ir import config as anir_config
15-from .ascend_npu_ir.ascend_npu_ir.npu.utils import run_once14+from .lowering_common import run_once
16 15 
17aten = torch.ops.aten16aten = torch.ops.aten
18npu = torch.ops.npu17npu = torch.ops.npu
@@ -17,6 +17,7 @@ from __future__ import annotations
17import inspect17import inspect
18import sympy18import sympy
19from functools import reduce19from functools import reduce
20+import functools
20from dataclasses import dataclass21from dataclasses import dataclass
21from typing import Any, Callable, Dict, List, Optional, Tuple, Union22from 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+ @functools.wraps(f)
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+ 
43def get_module_functions(module: Any) -> dict[str, Callable[..., Any]]:59def 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):