Skip to content

Commit 8c05cf8

Browse files
authored
Fix failing codegen tests (pytorch#21720)
Differential Revision: D115455181 Pull Request resolved: pytorch#21720
1 parent 27d4962 commit 8c05cf8

2 files changed

Lines changed: 43 additions & 6 deletions

File tree

‎codegen/targets.bzl‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,17 @@ def define_common_targets():
5151
"parse.py",
5252
],
5353
base_module = "executorch.codegen",
54+
resources = {
55+
"templates/RegisterCodegenUnboxedKernels.cpp": "templates/RegisterCodegenUnboxedKernels.cpp",
56+
"templates/Functions.h": "templates/Functions.h",
57+
"templates/NativeFunctions.h": "templates/NativeFunctions.h",
58+
"templates/RegisterKernels.cpp": "templates/RegisterKernels.cpp",
59+
"templates/RegisterKernels.h": "templates/RegisterKernels.h",
60+
"templates/RegisterSchema.cpp": "templates/RegisterSchema.cpp",
61+
"templates/aten_interned_strings.h": "templates/aten_interned_strings.h",
62+
"templates/RegisterDispatchDefinitions.ini": "templates/RegisterDispatchDefinitions.ini",
63+
"templates/RegisterDispatchKeyCustomOps.cpp": "templates/RegisterDispatchKeyCustomOps.cpp",
64+
},
5465
deps = [
5566
":api",
5667
],

‎codegen/test/test_executorch_gen.py‎

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import tempfile
1313
import unittest
1414

15-
from argparse import Namespace
1615
from pathlib import Path
1716

1817
import executorch.codegen.tools.gen_oplist as gen_oplist
@@ -27,7 +26,7 @@
2726
)
2827

2928
from executorch.codegen.model import ETKernelIndex, ETKernelKey, ETParsedYaml
30-
from torchgen.gen import get_custom_build_selector, LineLoader, make_file_manager
29+
from torchgen.gen import get_custom_build_selector, LineLoader
3130
from torchgen.model import (
3231
BackendIndex,
3332
BackendMetadata,
@@ -37,6 +36,7 @@
3736
OperatorName,
3837
)
3938
from torchgen.selective_build.selector import SelectiveBuilder
39+
from torchgen.utils import FileManager
4040

4141

4242
TEST_YAML = """
@@ -502,6 +502,31 @@ def _write_codegen_inputs(self, temp_dir: str) -> tuple[str, str, str]:
502502
)
503503
return aten_yaml_path, included_ops_yaml_path, tags_yaml_path
504504

505+
def _find_template_dir(self) -> Path:
506+
for probe in self._candidate_template_dirs():
507+
if (probe / "RegisterCodegenUnboxedKernels.cpp").exists():
508+
return probe
509+
return Path(__file__).parents[1] / "templates"
510+
511+
def _candidate_template_dirs(self):
512+
candidates: list[Path] = []
513+
try:
514+
from importlib.resources import files as _files # type: ignore[import]
515+
516+
candidates.append(Path(str(_files("executorch.codegen") / "templates")))
517+
except Exception:
518+
pass
519+
try:
520+
import executorch.codegen.gen as _gen_mod # type: ignore[import]
521+
522+
candidates.append(
523+
Path(_gen_mod.__file__).parent / "templates" # type: ignore[attr-defined]
524+
)
525+
except Exception:
526+
pass
527+
candidates.append(Path(__file__).parents[1] / "templates")
528+
yield from candidates
529+
505530
def _assert_selection_generates_empty_buildable_cpp(
506531
self,
507532
temp_dir: str,
@@ -513,14 +538,15 @@ def _assert_selection_generates_empty_buildable_cpp(
513538
old_main_file = getattr(main_module, "__file__", None)
514539
main_module.__file__ = __file__
515540
try:
516-
options = Namespace(
517-
source_path=str(Path(__file__).parents[1]),
518-
install_dir=temp_dir,
541+
template_dir = self._find_template_dir()
542+
cpu_fm = FileManager(
543+
install_dir=Path(temp_dir),
544+
template_dir=template_dir,
519545
dry_run=False,
520546
)
521547
gen_unboxing(
522548
native_functions=parsed_yaml.native_functions,
523-
cpu_fm=make_file_manager(options=options),
549+
cpu_fm=cpu_fm,
524550
selector=selector,
525551
use_aten_lib=False,
526552
kernel_index=parsed_yaml.kernel_index,

0 commit comments

Comments
 (0)