1212import tempfile
1313import unittest
1414
15- from argparse import Namespace
1615from pathlib import Path
1716
1817import executorch .codegen .tools .gen_oplist as gen_oplist
2726)
2827
2928from 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
3130from torchgen .model import (
3231 BackendIndex ,
3332 BackendMetadata ,
3736 OperatorName ,
3837)
3938from torchgen .selective_build .selector import SelectiveBuilder
39+ from torchgen .utils import FileManager
4040
4141
4242TEST_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