コード例 #1
0
 def assertUfuncErrorInline(self, yaml_str: str, expect: str) -> None:
     # parse a single structured group out of the yaml to g
     es = yaml.load(yaml_str, Loader=LineLoader)
     parsed_yaml = parse_native_yaml_struct(es)
     native_functions, backend_indices = parsed_yaml.native_functions, parsed_yaml.backend_indices
     grouped_native_functions = gen.get_grouped_native_functions(
         native_functions)
     assert len(grouped_native_functions) == 1
     g = grouped_native_functions[0]
     assert isinstance(g, NativeFunctionsGroup)
     assert g.out.ufunc_inner_loop
     # this is not ufunc codegen per se, but it does some basic sanity tests for
     # ufunc generation
     gen.compute_meta_function_declaration(g)
     dest.compute_native_function_declaration(
         g, backend_indices[DispatchKey.CPU])
     dest.compute_native_function_declaration(
         g, backend_indices[DispatchKey.CUDA])
     try:
         # the real kahuna
         dest.compute_ufunc_cpu(g)
         dest.compute_ufunc_cpu_kernel(g)
         dest.compute_ufunc_cuda(g)
     except AssertionError as e:
         # hack to strip out the context
         msg, _ = str(e).split('  in ', 2)
         self.assertExpectedInline('\n'.join(textwrap.wrap(msg)),
                                   expect,
                                   skip=1)
         return
     self.fail(msg="Did not raise when expected to")
コード例 #2
0
def run(source_yaml: str,
        output_dir: str,
        dry_run: bool,
        impl_path: Optional[str] = None) -> None:

    # Assumes that this file lives at PYTORCH_ROOT/tools/codegen/gen_backend_stubs.py
    pytorch_root = pathlib.Path(__file__).parent.parent.parent.absolute()
    template_dir = os.path.join(pytorch_root, "aten/src/ATen/templates")

    def make_file_manager(install_dir: str) -> FileManager:
        return FileManager(install_dir=install_dir,
                           template_dir=template_dir,
                           dry_run=dry_run)

    fm = make_file_manager(output_dir)

    native_yaml_path = os.path.join(
        pytorch_root, 'aten/src/ATen/native/native_functions.yaml')
    parsed_yaml = parse_native_yaml(native_yaml_path)
    native_functions, backend_indices = parsed_yaml.native_functions, parsed_yaml.backend_indices
    grouped_native_functions = get_grouped_native_functions(native_functions)
    parsed_backend_yaml = parse_backend_yaml(source_yaml,
                                             grouped_native_functions,
                                             backend_indices)
    backend_key = parsed_backend_yaml.backend_key
    autograd_key = parsed_backend_yaml.autograd_key
    cpp_namespace = parsed_backend_yaml.cpp_namespace
    class_name = parsed_backend_yaml.class_name
    backend_indices = parsed_backend_yaml.backend_indices

    selector = SelectiveBuilder.get_nop_selector()

    if backend_key is None:
        # This could be useful if a backend wants to quickly set up a noop yaml file but doesn't have any kernels ready yet.
        return

    if class_name is None:
        # class_name is an optional argument to backend yaml file.
        # if specified it allows an external backend to override
        # the name of the class that all generated kernel definitions live under.
        # if not specified, its value is given as native_function_class_name.
        class_name = backend_indices[backend_key].native_function_class_name()
    assert class_name is not None

    if impl_path is not None:
        error_on_missing_kernels(native_functions, backend_indices,
                                 backend_key, autograd_key, class_name,
                                 impl_path)

    gen_dispatchkey_nativefunc_headers(fm, class_name, cpp_namespace,
                                       backend_indices,
                                       grouped_native_functions, backend_key,
                                       autograd_key)

    for dispatch_key in [backend_key] if autograd_key is None else [
            backend_key, autograd_key
    ]:
        gen_dispatcher_registrations(fm, output_dir, class_name, cpp_namespace,
                                     backend_indices, grouped_native_functions,
                                     backend_key, dispatch_key, selector)
コード例 #3
0
def main() -> None:
    parser = argparse.ArgumentParser(description='Generate backend stub files')
    parser.add_argument(
        '-s',
        '--source_yaml',
        help='path to source yaml file containing operator external definitions')
    parser.add_argument(
        '-o', '--output_dir', help='output directory')
    parser.add_argument(
        '--dry_run', type=bool, default=False, help='output directory')
    options = parser.parse_args()

    # Assumes that this file lives at PYTORCH_ROOT/tools/codegen/gen_backend_stubs.py
    pytorch_root = pathlib.Path(__file__).parent.parent.parent.absolute()
    template_dir = os.path.join(pytorch_root, "aten/src/ATen/templates")

    def make_file_manager(install_dir: str) -> FileManager:
        return FileManager(install_dir=install_dir, template_dir=template_dir, dry_run=options.dry_run)

    fm = make_file_manager(options.output_dir)

    native_yaml_path = os.path.join(pytorch_root, 'aten/src/ATen/native/native_functions.yaml')
    grouped_native_functions = get_grouped_native_functions(native_yaml_path)
    cpp_namespace, external_backend_functions = parse_backend_yaml(options.source_yaml, grouped_native_functions)

    native_functions = parse_native_yaml(native_yaml_path)

    selector = SelectiveBuilder.get_nop_selector()


    generated_comment = 'Autogenerated file by gen_backend_stubs.py. Do not edit directly!'
    fm.write('aten_xla_type.h', lambda: {
        'generated_comment': generated_comment,
        'cpp_namespace': cpp_namespace,
        'dispatch_xla_declarations': list(concatMap(dest.compute_native_function_declaration, external_backend_functions)),
    })

    fm.write('aten_xla_type_default.h', lambda: {
        'generated_comment': generated_comment,
        'cpp_namespace': cpp_namespace,
        'dispatch_aten_fallback_declarations': list(concatMap(
            dest.GenExternalAtenFallback(Target.NAMESPACED_DECLARATION), external_backend_functions
        )),
    })

    fm.write('aten_xla_type_default.cpp', lambda: {
        'generated_comment': generated_comment,
        'cpp_namespace': cpp_namespace,
        # TODO: after cpu fallbacks are moved to a boxed kernel,
        # merge registrations / definitions into RegisterDispatchKey
        'dispatch_aten_fallback_definitions': list(concatMap(
            dest.GenExternalAtenFallback(Target.NAMESPACED_DEFINITION), external_backend_functions
        )),
        'dispatch_registrations': list(concatMap(
            dest.GenExternalAtenFallback(Target.REGISTRATION), [e for e in external_backend_functions if not e.is_autograd_kernel]
        )),
        'dispatch_autograd_registrations': list(concatMap(
            dest.GenExternalAtenFallback(Target.REGISTRATION), [e for e in external_backend_functions if e.is_autograd_kernel]
        )),
    })
コード例 #4
0
def run(source_yaml: str, output_dir: str, dry_run: bool,
        impl_path: Optional[str]) -> None:

    # Assumes that this file lives at PYTORCH_ROOT/tools/codegen/gen_backend_stubs.py
    pytorch_root = pathlib.Path(__file__).parent.parent.parent.absolute()
    template_dir = os.path.join(pytorch_root, "aten/src/ATen/templates")

    def make_file_manager(install_dir: str) -> FileManager:
        return FileManager(install_dir=install_dir,
                           template_dir=template_dir,
                           dry_run=dry_run)

    fm = make_file_manager(output_dir)

    native_yaml_path = os.path.join(
        pytorch_root, 'aten/src/ATen/native/native_functions.yaml')
    parsed_yaml = parse_native_yaml(native_yaml_path)
    native_functions, backend_indices = parsed_yaml.native_functions, parsed_yaml.backend_indices
    grouped_native_functions = get_grouped_native_functions(native_functions)
    parsed_backend_yaml = parse_backend_yaml(source_yaml,
                                             grouped_native_functions,
                                             backend_indices)
    backend_key = parsed_backend_yaml.backend_key
    autograd_key = parsed_backend_yaml.autograd_key
    cpp_namespace = parsed_backend_yaml.cpp_namespace
    backend_indices = parsed_backend_yaml.backend_indices

    selector = SelectiveBuilder.get_nop_selector()

    assert backend_key is not None
    class_name = backend_indices[backend_key].native_function_class_name()

    if impl_path is not None:
        error_on_missing_kernels(native_functions, backend_indices,
                                 backend_key, autograd_key, impl_path)

        gen_dispatchkey_nativefunc_headers(fm, class_name, cpp_namespace,
                                           backend_indices,
                                           grouped_native_functions,
                                           backend_key, autograd_key)

        for dispatch_key in [backend_key] if autograd_key is None else [
                backend_key, autograd_key
        ]:
            gen_dispatcher_registrations(fm, output_dir, cpp_namespace,
                                         backend_indices,
                                         grouped_native_functions, backend_key,
                                         dispatch_key, selector)
コード例 #5
0
def main() -> None:
    parser = argparse.ArgumentParser(description='Generate ATen source files')
    parser.add_argument(
        '-s',
        '--source-path',
        help='path to source directory for ATen',
        default='aten/src/ATen')
    parser.add_argument(
        '-p',
        '--generated-ops-cpp-path',
        help='path to directory to generate op dispatcher .cpp file',
        default='torch/csrc/jit/runtime/static/generated_ops.cpp')
    parser.add_argument(
        '-t',
        '--generated-ops-test-cpp-path',
        help='path to directory to generate op dispatcher .cpp file',
        default='benchmarks/static_runtime/test_generated_ops.cc')
    options = parser.parse_args()
    native_yaml_path = os.path.join(options.source_path, 'native/native_functions.yaml')
    parsed_yaml = gen.parse_native_yaml(native_yaml_path)
    native_functions, backend_indices = parsed_yaml.native_functions, parsed_yaml.backend_indices
    grouped_native_functions = gen.get_grouped_native_functions(native_functions)
    structured_native_functions = [g for g in grouped_native_functions
                                   if isinstance(g, NativeFunctionsGroup)]
    supported_function_groups = group_functions_by_op_name(structured_native_functions)

    gen_out_variant_dispatcher = gen_structured.GenOutVariantDispatcher()
    result = [gen_out_variant_dispatcher(groups) for groups in supported_function_groups]

    gen_out_variant_dispatcher_test_case = gen_structured.GenOutVariantDispatcherTestCase()
    test_result = [gen_out_variant_dispatcher_test_case(groups) for groups in supported_function_groups]

    write_cpp(result, options.generated_ops_cpp_path)
    write_test_cpp(test_result, options.generated_ops_test_cpp_path)

    print("total grouped native ops: %d" % len(grouped_native_functions))
    print("structured grouped native ops: %d" % len(structured_native_functions))
    supported_grouped_functions = sum([len(groups) for groups in supported_function_groups])
    print("generated grouped native ops: %d" % supported_grouped_functions)
コード例 #6
0
def run(source_yaml: str, output_dir: str, dry_run: bool,
        impl_path: Optional[str], gen_ts_lowerings: bool, node_base: str,
        node_base_hdr: Optional[str], tensor_class: str,
        tensor_class_hdr: str) -> None:

    # Assumes that this file lives at PYTORCH_ROOT/tools/codegen/gen_backend_stubs.py
    pytorch_root = pathlib.Path(__file__).parent.parent.parent.absolute()
    template_dir = os.path.join(pytorch_root, "aten/src/ATen/templates")

    def make_file_manager(install_dir: str) -> FileManager:
        return FileManager(install_dir=install_dir,
                           template_dir=template_dir,
                           dry_run=dry_run)

    fm = make_file_manager(output_dir)

    native_yaml_path = os.path.join(
        pytorch_root, 'aten/src/ATen/native/native_functions.yaml')
    parsed_yaml = parse_native_yaml(native_yaml_path)
    native_functions, backend_indices = parsed_yaml.native_functions, parsed_yaml.backend_indices
    grouped_native_functions = get_grouped_native_functions(native_functions)

    def sort_native_function(
            f: Union[NativeFunctionsGroup, NativeFunction]) -> str:
        """
        We sort the native function because of the note in concat_map_codegen.
        TODO(alanwaketan): Remove this sorting hack once all ops are grouped properly.
        """
        func = f.functional.func if isinstance(
            f, NativeFunctionsGroup) else f.func
        return str(func.name.name)

    grouped_native_functions = sorted(grouped_native_functions,
                                      key=sort_native_function)
    parsed_backend_yaml = parse_backend_yaml(source_yaml,
                                             grouped_native_functions,
                                             backend_indices)
    backend_key = parsed_backend_yaml.backend_key
    autograd_key = parsed_backend_yaml.autograd_key
    cpp_namespace = parsed_backend_yaml.cpp_namespace
    backend_indices = parsed_backend_yaml.backend_indices
    full_codegen = parse_full_codegen_ops(source_yaml,
                                          grouped_native_functions)

    def concat_map_codegen(
            func: Callable[[NativeFunction], Sequence[str]],
            xs: Iterable[Union[NativeFunctionsGroup, NativeFunction]],
            *,
            codegenInplaceVariant: bool = False) -> Iterator[str]:
        """
        We code-gen for the functional variant, which is all we need for IR classes/lowerings/shape inferences, but we
        only code-gen additional entries for the inplace variant for the native functions.
        Note: If xs is not sorted, there may be an edge case when generating IR classes. Considering relu and relu_, if
        we encounter relu_ before relu. we will then generate an IR class with op = at::aten::relu_ for both relu and
        relu_ which will cause problems for relu.
        TODO(alanwaketan): Once all ops are grouped properly, we should no longer need this hack.
        """
        generated = set()

        def gen_key(func: FunctionSchema) -> Tuple[str, str]:
            # we want to generate unique entries for overloads of functional variants,
            # but not for inplace variants unless explicitly told `codegenInplaceVariant`
            return (func.name.name.base, func.name.overload_name)

        for x in xs:
            f = x.functional if isinstance(x, NativeFunctionsGroup) else x
            # For the 'or'd terms:
            # 1. codegenInplaceVariant means we can generate the in-place variant corresponding items.
            # 2. not f.func.name.name.inplace means the op is not a in-place variant, so we can generate the item.
            # 3. f.func.name.name.base not in generated means even for in-place ops we still need to generate the item
            # as if they were the functional variants for one time.
            if f.func.name in full_codegen and \
               (codegenInplaceVariant or not f.func.name.name.inplace or gen_key(f.func) not in generated):
                generated.add(gen_key(f.func))
                for r in func(f):
                    yield r

    selector = SelectiveBuilder.get_nop_selector()

    assert backend_key is not None
    class_name = backend_indices[backend_key].native_function_class_name()

    if impl_path is not None:
        error_on_missing_kernels(native_functions, backend_indices,
                                 backend_key, autograd_key, impl_path,
                                 full_codegen)

    assert class_name is not None

    # Generate nativefunction declarations
    gen_dispatchkey_nativefunc_headers(fm, class_name, cpp_namespace,
                                       backend_indices,
                                       grouped_native_functions, backend_key,
                                       autograd_key)

    # Generate Dispatcher registrations which hook up the nativefunctions
    for dispatch_key in [backend_key] if autograd_key is None else [
            backend_key, autograd_key
    ]:
        gen_dispatcher_registrations(fm, output_dir, cpp_namespace,
                                     backend_indices, grouped_native_functions,
                                     backend_key, dispatch_key, selector)

    # Generate native function impls that build IR nodes
    fm.write_with_template(
        f'{backend_key}NativeFunctions.cpp',
        'DispatchKeyNativeFunctions.cpp',
        lambda: {
            'includes': [
                f'#include <{path}>' for path in [
                    tensor_class_hdr,
                    "ATen/MetaFunctions.h",
                    "torch/csrc/lazy/core/metrics.h",
                    "torch/csrc/lazy/core/shape.h",
                    "lazy_tensor_core/csrc/aten_ltc_bridge.h",
                    "lazy_tensor_core/csrc/lazy_graph_executor.h",
                    f"{output_dir}/{backend_key}NativeFunctions.h",
                    f"{output_dir}/{backend_key}LazyIr.h",
                    f"{output_dir}/{backend_key}ShapeInference.h",
                ]
            ],
            'native_functions_include':
            '',
            'backend_namespace':
            'torch_lazy_tensors',  # this is wrong
            'native_function_definitions':
            list(
                concat_map_codegen(dest.GenLazyNativeFuncDefinition(
                    f'{backend_key}NativeFunctions', backend_indices[
                        backend_key], tensor_class),
                                   grouped_native_functions,
                                   codegenInplaceVariant=True)),
        })
    # Generate headers for shape/dtype funcs for non-meta kernels
    fm.write_with_template(
        f'{backend_key}ShapeInference.h', 'ShapeInference.h', lambda: {
            'lazy_ir_sysinc': [
                f'#include <{path}>' for path in [
                    "ATen/Tensor.h",
                    "c10/core/ScalarType.h",
                    "c10/util/Optional.h",
                    "torch/csrc/lazy/core/ir.h",
                    "torch/csrc/lazy/core/shape.h",
                    "vector",
                ]
            ],
            'lazy_ir_inc': [],
            'DispatchKey':
            backend_key,
            'dispatch_namespace':
            backend_key.lower(),
            'func_declarations':
            list(
                concat_map_codegen(
                    dest.GenLazyShapeInferenceDefinition(
                        backend_indices[backend_key], tensor_class),
                    grouped_native_functions)),
        })
    # Generate IR node classes
    fm.write_with_template(
        f'{backend_key}LazyIr.h', 'LazyIr.h', lambda: {
            'lazy_ir_sysinc': [
                f'#include <{path}>' for path in [
                    "ATen/core/Formatting.h",
                    "c10/core/ScalarType.h",
                    "c10/util/Optional.h",
                    "torch/csrc/lazy/core/hash.h",
                    "torch/csrc/lazy/core/ir.h",
                    "vector",
                ]
            ],
            'lazy_ir_inc': [
                f'#include "{path}"' for path in
                [node_base_hdr if node_base_hdr is not None else None]
                if path is not None
            ],
            'external_backend_headers':
            f'#include "{output_dir}/{backend_key}NativeFunctions.h"',
            'namespaced_headers':
            '',
            'DispatchKey':
            backend_key,
            'dispatch_namespace':
            backend_key.lower(),
            'ir_declarations':
            list(
                concat_map_codegen(
                    dest.LazyIR(backend_indices[backend_key], node_base),
                    grouped_native_functions)),
        })
コード例 #7
0
def run(source_yaml: str, output_dir: str, dry_run: bool,
        impl_path: Optional[str]) -> None:

    # Assumes that this file lives at PYTORCH_ROOT/tools/codegen/gen_backend_stubs.py
    pytorch_root = pathlib.Path(__file__).parent.parent.parent.absolute()
    template_dir = os.path.join(pytorch_root, "aten/src/ATen/templates")

    def make_file_manager(install_dir: str) -> FileManager:
        return FileManager(install_dir=install_dir,
                           template_dir=template_dir,
                           dry_run=dry_run)

    fm = make_file_manager(output_dir)

    native_yaml_path = os.path.join(
        pytorch_root, 'aten/src/ATen/native/native_functions.yaml')
    parsed_yaml = parse_native_yaml(native_yaml_path)
    native_functions, backend_indices = parsed_yaml.native_functions, parsed_yaml.backend_indices
    grouped_native_functions = get_grouped_native_functions(native_functions)
    parsed_backend_yaml = parse_backend_yaml(source_yaml,
                                             grouped_native_functions,
                                             backend_indices)
    backend_key = parsed_backend_yaml.backend_key
    autograd_key = parsed_backend_yaml.autograd_key
    cpp_namespace = parsed_backend_yaml.cpp_namespace
    backend_indices = parsed_backend_yaml.backend_indices

    selector = SelectiveBuilder.get_nop_selector()

    # TODO: handle cases when yaml contains zero ops properly in a later PR.
    if backend_key is not None and autograd_key is not None:
        backend_dispatch_key: DispatchKey = backend_key
        autograd_dispatch_key: DispatchKey = autograd_key
        class_name = backend_indices[
            backend_dispatch_key].native_function_class_name()

        if impl_path is not None:
            error_on_missing_kernels(native_functions, backend_indices,
                                     backend_key, autograd_key, impl_path)

        assert class_name is not None
        generated_comment = 'Autogenerated file by gen_backend_stubs.py. Do not edit directly!'
        fm.write_with_template(
            f'{backend_dispatch_key}NativeFunctions.h',
            'DispatchKeyNativeFunctions.h',
            lambda: {
                'generated_comment':
                generated_comment,
                'cpp_namespace':
                cpp_namespace,
                'class_name':
                class_name,
                # Convert to a set first to remove duplicate kernel names.
                # Backends are allowed to repeat kernel names; only generate the declaration once!
                'dispatch_declarations':
                list(
                    set(
                        concatMap(
                            lambda f: dest.compute_native_function_declaration(
                                f, backend_indices[backend_dispatch_key]),
                            grouped_native_functions))) +
                list(
                    set(
                        concatMap(
                            lambda f: dest.compute_native_function_declaration(
                                f, backend_indices[autograd_dispatch_key]),
                            grouped_native_functions))),
            })

        for dispatch_key in [backend_dispatch_key, autograd_dispatch_key]:
            fm.write_with_template(
                f'Register{dispatch_key}.cpp', 'RegisterDispatchKey.cpp',
                lambda: {
                    'extra_cuda_headers':
                    '',
                    'external_backend_headers':
                    f'#include "{output_dir}/{backend_key}NativeFunctions.h"',
                    'namespaced_headers':
                    '',
                    'DispatchKey':
                    dispatch_key,
                    'dispatch_namespace':
                    dispatch_key.lower(),
                    'dispatch_helpers':
                    dest.gen_registration_helpers(backend_indices[dispatch_key]
                                                  ),
                    'dispatch_namespaced_definitions':
                    list(
                        concatMap(
                            dest.RegisterDispatchKey(
                                backend_indices[dispatch_key],
                                Target.NAMESPACED_DEFINITION,
                                selector,
                                rocm=False,
                                cpp_namespace=cpp_namespace,
                                class_method_name=
                                f'{backend_dispatch_key}NativeFunctions'),
                            grouped_native_functions)),
                    'dispatch_anonymous_definitions':
                    list(
                        concatMap(
                            dest.RegisterDispatchKey(
                                backend_indices[dispatch_key],
                                Target.ANONYMOUS_DEFINITION,
                                selector,
                                rocm=False,
                                cpp_namespace=cpp_namespace,
                                class_method_name=
                                f'{backend_dispatch_key}NativeFunctions'),
                            grouped_native_functions)),
                    'dispatch_registrations':
                    list(
                        concatMap(
                            dest.RegisterDispatchKey(
                                backend_indices[dispatch_key],
                                Target.REGISTRATION,
                                selector,
                                rocm=False,
                                cpp_namespace=cpp_namespace,
                                class_method_name=
                                f'{backend_dispatch_key}NativeFunctions'),
                            grouped_native_functions)),
                })
コード例 #8
0
ファイル: gen_lazy_tensor.py プロジェクト: xkszltl/pytorch
def run_gen_lazy_tensor(
        aten_path: str,
        source_yaml: str,
        output_dir: str,
        dry_run: bool,
        impl_path: Optional[str],
        node_base: str = default_args.node_base,
        node_base_hdr: Optional[str] = default_args.node_base_hdr,
        tensor_class: str = default_args.tensor_class,
        tensor_class_hdr: str = default_args.tensor_class_hdr,
        shape_inference_hdr: str = default_args.shape_inference_hdr,
        lazy_ir_cls: Type[LazyIR] = default_args.lazy_ir_cls,
        # build_in_tree is true for TS backend and affects include paths
        build_in_tree: bool = False,
        # per_operator_headers changes whether ATen/Functions.h or individual operator headers are used
        # it must match how ATen was built
        per_operator_headers: bool = False,
        backend_name: str = default_args.backend_name,
        gen_forced_fallback_code: bool = False) -> None:

    template_dir = os.path.join(aten_path, "templates")

    def make_file_manager(install_dir: str) -> FileManager:
        return FileManager(install_dir=install_dir,
                           template_dir=template_dir,
                           dry_run=dry_run)

    fm = make_file_manager(output_dir)

    native_yaml_path = os.path.join(aten_path, 'native/native_functions.yaml')
    parsed_yaml = parse_native_yaml(native_yaml_path)
    native_functions, backend_indices = parsed_yaml.native_functions, parsed_yaml.backend_indices
    grouped_native_functions = get_grouped_native_functions(native_functions)

    def sort_native_function(
            f: Union[NativeFunctionsGroup, NativeFunction]) -> str:
        """
        We sort the native function because of the note in concat_map_codegen.
        TODO(alanwaketan): Remove this sorting hack once all ops are grouped properly.
        """
        func = f.functional.func if isinstance(
            f, NativeFunctionsGroup) else f.func
        return str(func.name.name)

    grouped_native_functions = sorted(grouped_native_functions,
                                      key=sort_native_function)
    parsed_backend_yaml = parse_backend_yaml(source_yaml,
                                             grouped_native_functions,
                                             backend_indices)
    backend_key = parsed_backend_yaml.backend_key
    autograd_key = parsed_backend_yaml.autograd_key
    cpp_namespace = parsed_backend_yaml.cpp_namespace
    backend_indices = parsed_backend_yaml.backend_indices
    full_codegen = parse_full_codegen_ops(source_yaml,
                                          grouped_native_functions)

    def concat_map_codegen(
            func: Callable[[NativeFunction], Sequence[str]],
            xs: Iterable[Union[NativeFunctionsGroup, NativeFunction]],
            *,
            codegenInplaceVariant: bool = False) -> Iterator[str]:
        """
        We code-gen for the functional variant, which is all we need for IR classes/lowerings/shape inferences, but we
        only code-gen additional entries for the inplace variant for the native functions.
        Note: If xs is not sorted, there may be an edge case when generating IR classes. Considering relu and relu_, if
        we encounter relu_ before relu. we will then generate an IR class with op = at::aten::relu_ for both relu and
        relu_ which will cause problems for relu.
        TODO(alanwaketan): Once all ops are grouped properly, we should no longer need this hack.
        """
        generated = set()

        def gen_key(func: FunctionSchema) -> Tuple[str, str]:
            # we want to generate unique entries for overloads of functional variants,
            # but not for inplace variants unless explicitly told `codegenInplaceVariant`
            return (func.name.name.base, func.name.overload_name)

        for x in xs:
            f = x.functional if isinstance(x, NativeFunctionsGroup) else x
            # For the 'or'd terms:
            # 1. codegenInplaceVariant means we can generate the in-place variant corresponding items.
            # 2. not f.func.name.name.inplace means the op is not a in-place variant, so we can generate the item.
            # 3. f.func.name.name.base not in generated means even for in-place ops we still need to generate the item
            # as if they were the functional variants for one time.
            if f.func.name in full_codegen and \
               (codegenInplaceVariant or not f.func.name.name.inplace or gen_key(f.func) not in generated):
                generated.add(gen_key(f.func))
                for r in func(f):
                    yield r

    selector = SelectiveBuilder.get_nop_selector()

    assert backend_key is not None
    class_name = backend_indices[backend_key].native_function_class_name()

    if impl_path is not None:
        error_on_missing_kernels(native_functions, backend_indices,
                                 backend_key, autograd_key, class_name,
                                 impl_path, full_codegen)
    """ Validate Shape Inference Definitions

    Generated lazy native functions all perform shape inference, by first using a meta:: kernel
    if available for that op, and otherwise using a 'compute_shape_{op}' function instead.  The generator
    knows the call signature for compute_shape_{op} becuase it matches the nativefunction (and meta::) signature,
    so it just has to check whether the op is structured and generate a call for one or the other.  It's up to the dev
    to supply the missing compute_shape_{op} function, but the codegen at least warns you about this and provides
    the expected signature which can be copy-pasted into shape_inference.h.

    compute_shape_{op} functions are handwritten and should be replaced over time as ops get ported
    to structured kernels.

    See torch/csrc/lazy/core/shape_inference.cpp #READ THIS! for more information.
    """
    if shape_inference_hdr is not None:
        expected_shape_infr_decls = list(
            concat_map_codegen(dest.GenLazyShapeInferenceDefinition(
                backend_indices[backend_key], tensor_class),
                               grouped_native_functions,
                               codegenInplaceVariant=True))

        validate_shape_inference_header(shape_inference_hdr,
                                        expected_shape_infr_decls)
    assert class_name is not None

    # Generate nativefunction declarations
    # Note, eager registrations is set to False for the lazy TS backend as another LTC backend
    # may want to register their own lazy kernels instead of registering the TS ones.
    # The registration will lazily happen when init_ts_backend is called.
    gen_dispatchkey_nativefunc_headers(fm, class_name, cpp_namespace,
                                       backend_indices,
                                       grouped_native_functions, backend_key,
                                       autograd_key, backend_name)

    # Generate Dispatcher registrations which hook up the nativefunctions
    for dispatch_key in [backend_key] if autograd_key is None else [
            backend_key, autograd_key
    ]:
        gen_dispatcher_registrations(fm,
                                     output_dir,
                                     class_name,
                                     cpp_namespace,
                                     backend_indices,
                                     grouped_native_functions,
                                     backend_key,
                                     dispatch_key,
                                     selector,
                                     build_in_tree=build_in_tree,
                                     per_operator_headers=per_operator_headers,
                                     backend_name=backend_name,
                                     eager_registration=False)

    # Generate native function impls that build IR nodes
    ns_helper = NamespaceHelper(cpp_namespace)
    fm.write_with_template(
        f'{backend_key}NativeFunctions.cpp', 'DispatchKeyNativeFunctions.cpp',
        lambda: {
            'includes': [
                f'#include <{path}>' for path in [
                    tensor_class_hdr,
                    shape_inference_hdr,
                    "ATen/Functions.h",
                    "ATen/MetaFunctions.h",
                    "ATen/Operators.h",
                    "ATen/native/CPUFallback.h",
                    "torch/csrc/lazy/core/lazy_graph_executor.h",
                    "torch/csrc/lazy/core/metrics.h",
                    "torch/csrc/lazy/core/shape.h",
                    f"{output_dir}/{backend_key}NativeFunctions.h",
                    f"{output_dir}/LazyIr.h",
                ] + (["torch/csrc/lazy/ts_backend/ts_eager_fallback.h"]
                     if gen_forced_fallback_code else [])
            ],
            'native_functions_include':
            '',
            'namespace_prologue':
            ns_helper.prologue,
            'namespace_epilogue':
            ns_helper.epilogue,
            'native_function_definitions':
            list(
                concat_map_codegen(dest.GenLazyNativeFuncDefinition(
                    f'{backend_key}NativeFunctions', backend_indices[
                        backend_key], tensor_class, gen_forced_fallback_code),
                                   grouped_native_functions,
                                   codegenInplaceVariant=True)),
        })
    # Generate IR node classes
    fm.write_with_template(
        'LazyIr.h', 'LazyIr.h', lambda: {
            'lazy_ir_sysinc': [
                f'#include <{path}>' for path in [
                    "ATen/core/Formatting.h",
                    "c10/core/ScalarType.h",
                    "c10/util/Optional.h",
                    "torch/csrc/lazy/core/hash.h",
                    "torch/csrc/lazy/core/ir.h",
                    "torch/csrc/lazy/core/shape.h",
                    "vector",
                ]
            ],
            'lazy_ir_inc': [
                f'#include "{path}"' for path in
                [node_base_hdr if node_base_hdr is not None else None]
                if path is not None
            ],
            'ir_declarations':
            list(
                concat_map_codegen(
                    lazy_ir_cls(backend_indices[backend_key], node_base),
                    grouped_native_functions)),
            'namespace_prologue':
            ns_helper.prologue,
            'namespace_epilogue':
            ns_helper.epilogue,
        })
コード例 #9
0
def run(source_yaml: str, output_dir: str, dry_run: bool) -> None:

    # Assumes that this file lives at PYTORCH_ROOT/tools/codegen/gen_backend_stubs.py
    pytorch_root = pathlib.Path(__file__).parent.parent.parent.absolute()
    template_dir = os.path.join(pytorch_root, "aten/src/ATen/templates")

    def make_file_manager(install_dir: str) -> FileManager:
        return FileManager(install_dir=install_dir,
                           template_dir=template_dir,
                           dry_run=dry_run)

    fm = make_file_manager(output_dir)

    native_yaml_path = os.path.join(
        pytorch_root, 'aten/src/ATen/native/native_functions.yaml')
    parsed_yaml = parse_native_yaml(native_yaml_path)
    native_functions, backend_indices = parsed_yaml.native_functions, parsed_yaml.backend_indices
    grouped_native_functions = get_grouped_native_functions(native_functions)
    parsed_backend_yaml = parse_backend_yaml(source_yaml,
                                             grouped_native_functions,
                                             backend_indices)
    backend_key = parsed_backend_yaml.backend_key
    autograd_key = parsed_backend_yaml.autograd_key
    cpp_namespace = parsed_backend_yaml.cpp_namespace
    backend_indices = parsed_backend_yaml.backend_indices

    selector = SelectiveBuilder.get_nop_selector()

    # TODO: handle cases when yaml contains zero ops properly in a later PR.
    if backend_key is not None and autograd_key is not None:
        backend_dispatch_key: DispatchKey = backend_key
        autograd_dispatch_key: DispatchKey = autograd_key
        generated_comment = 'Autogenerated file by gen_backend_stubs.py. Do not edit directly!'
        fm.write(
            'aten_xla_type.h',
            lambda: {
                'generated_comment':
                generated_comment,
                'cpp_namespace':
                cpp_namespace,
                # Convert to a set first to remove duplicate kernel names.
                # Backends are allowed to repeat kernel names; only generate the declaration once!
                'dispatch_xla_declarations':
                list(
                    set(
                        concatMap(
                            lambda f: dest.compute_native_function_declaration(
                                f, backend_indices[backend_dispatch_key]),
                            grouped_native_functions))) +
                list(
                    set(
                        concatMap(
                            lambda f: dest.compute_native_function_declaration(
                                f, backend_indices[autograd_dispatch_key]),
                            grouped_native_functions))),
            })

        external_backend_headers = '''\
#include <tensorflow/compiler/xla/xla_client/debug_macros.h>
#include <tensorflow/compiler/xla/xla_client/metrics.h>
#include <tensorflow/compiler/xla/xla_client/tf_logging.h>
#include <torch_xla/csrc/function_call_tracker.h>
#include <torch_xla/csrc/aten_xla_type.h>
#include <torch_xla/csrc/aten_xla_type_default.h>'''

        for dispatch_key in [backend_dispatch_key, autograd_dispatch_key]:
            fm.write_with_template(
                f'Register{dispatch_key}.cpp', 'RegisterDispatchKey.cpp',
                lambda: {
                    'extra_cuda_headers':
                    '',
                    'legacy_th_headers':
                    '',
                    'external_backend_headers':
                    external_backend_headers,
                    'DispatchKey':
                    dispatch_key,
                    'dispatch_namespace':
                    dispatch_key.lower(),
                    'dispatch_namespaced_definitions':
                    list(
                        concatMap(
                            dest.RegisterDispatchKey(
                                backend_indices[dispatch_key],
                                Target.NAMESPACED_DEFINITION,
                                selector,
                                rocm=False,
                                cpp_namespace=cpp_namespace),
                            grouped_native_functions)),
                    'dispatch_anonymous_definitions':
                    list(
                        concatMap(
                            dest.RegisterDispatchKey(
                                backend_indices[dispatch_key],
                                Target.ANONYMOUS_DEFINITION,
                                selector,
                                rocm=False,
                                cpp_namespace=cpp_namespace),
                            grouped_native_functions)),
                    'dispatch_registrations':
                    list(
                        concatMap(
                            dest.RegisterDispatchKey(
                                backend_indices[dispatch_key],
                                Target.REGISTRATION,
                                selector,
                                rocm=False,
                                cpp_namespace=cpp_namespace),
                            grouped_native_functions)),
                })

        fm.write(
            'aten_xla_type_default.h', lambda: {
                'generated_comment':
                generated_comment,
                'cpp_namespace':
                cpp_namespace,
                'dispatch_aten_fallback_declarations':
                list(
                    concatMap(
                        dest.GenExternalAtenFallback(
                            Target.NAMESPACED_DECLARATION, backend_indices[
                                backend_dispatch_key]),
                        grouped_native_functions)),
            })

        fm.write(
            'aten_xla_type_default.cpp',
            lambda: {
                'generated_comment':
                generated_comment,
                'cpp_namespace':
                cpp_namespace,
                # TODO: after cpu fallbacks are moved to a boxed kernel,
                # merge registrations / definitions into RegisterDispatchKey
                'dispatch_aten_fallback_definitions':
                list(
                    concatMap(
                        dest.GenExternalAtenFallback(
                            Target.NAMESPACED_DEFINITION, backend_indices[
                                backend_dispatch_key]),
                        grouped_native_functions)),
                'dispatch_registrations':
                list(
                    concatMap(
                        dest.GenExternalAtenFallback(
                            Target.REGISTRATION, backend_indices[
                                backend_dispatch_key]),
                        grouped_native_functions)),
            })