1
from __future__ import annotations
6
from collections import Counter, defaultdict, namedtuple
7
from pathlib import Path
8
from typing import Sequence
12
import torchgen.api.dispatcher as dispatcher
13
import torchgen.dest as dest
14
from torchgen.api.types import DispatcherSignature
15
from torchgen.code_template import CodeTemplate
16
from torchgen.context import native_function_manager
17
from torchgen.gen import get_grouped_native_functions, parse_native_yaml
18
from torchgen.model import (
26
from torchgen.selective_build.selector import SelectiveBuilder
27
from torchgen.utils import concatMap, context, FileManager, NamespaceHelper, Target
28
from torchgen.yaml_utils import YamlLoader
31
# Parses the external backend's yaml, and adds a new BackendIndex for the backend's dispatch key.
32
# Returns a Tuple of (backend_key, autograd_key, cpp_namespace, updated BackendIndex mapping)
33
ParsedExternalYaml = namedtuple(
35
["backend_key", "autograd_key", "class_name", "cpp_namespace", "backend_indices"],
39
def parse_backend_yaml(
40
backend_yaml_path: str,
41
grouped_native_functions: Sequence[NativeFunction | NativeFunctionsGroup],
42
backend_indices: dict[DispatchKey, BackendIndex],
43
) -> ParsedExternalYaml:
44
native_functions_map: dict[OperatorName, NativeFunction] = {
47
lambda f: [f] if isinstance(f, NativeFunction) else list(f.functions()),
48
grouped_native_functions,
52
with open(backend_yaml_path) as f:
53
yaml_values = yaml.load(f, Loader=YamlLoader)
54
assert isinstance(yaml_values, dict)
69
backend = yaml_values.pop("backend", None)
70
assert backend is not None, 'You must provide a value for "backend"'
72
class_name = yaml_values.pop("class_name", None)
74
cpp_namespace = yaml_values.pop("cpp_namespace", None)
75
assert cpp_namespace is not None, 'You must provide a value for "cpp_namespace"'
77
# Mostly just defaulting to false to stick with LazyTensor convention.
78
use_out_as_primary = yaml_values.pop("use_out_as_primary", False)
80
use_out_as_primary, bool
81
), f"You must provide either True or False for use_out_as_primary. Provided: {use_out_as_primary}"
83
use_device_guard = yaml_values.pop("device_guard", False)
85
use_device_guard, bool
86
), f"You must provide either True or False for device_guard. Provided: {use_device_guard}"
88
supported = yaml_values.pop("supported", [])
90
supported = [] # Allow an empty list of supported ops
93
), f'expected "supported" to be a list, but got: {supported} (of type {type(supported)})'
95
symint = yaml_values.pop("symint", [])
97
symint = [] # Allow an empty list of symint ops
100
), f'expected "symint" to be a list, but got: {supported} (of type {type(supported)})'
101
symint_set = set(symint)
103
supported_autograd = yaml_values.pop("autograd", [])
105
supported_autograd, list
106
), f'expected "autograd" to be a list, but got: {supported_autograd}'
108
# full_codegen is ignored by parse_backend_yaml, and re-parsed in gen_lazy_tensor.py
109
full_codegen = yaml_values.pop("full_codegen", [])
110
supported.extend(full_codegen)
112
# non_native is ignored by parse_backend_yaml, and re-parsed in gen_lazy_tensor.py
113
yaml_values.pop("non_native", {})
115
# ir_gen is ignored by parse_backend_yaml, and re-parsed in gen_lazy_tensor.py
116
yaml_values.pop("ir_gen", {})
119
len(yaml_values.keys()) == 0
120
), f'{backend_yaml_path} contains unexpected keys: {", ".join(yaml_values.keys())}. \
121
Only the following keys are supported: {", ".join(valid_keys)}'
123
def create_backend_index(
124
backend_ops: list[str],
125
symint_ops: set[str],
126
dispatch_key: DispatchKey,
128
use_out_as_primary: bool,
129
use_device_guard: bool,
131
metadata: dict[OperatorName, BackendMetadata] = {}
132
for op in backend_ops:
133
op_name = OperatorName.parse(op)
135
op_name in native_functions_map
136
), f"Found an invalid operator name: {op_name}"
137
# See Note [External Backends Follow Dispatcher API]
138
kernel_name = dispatcher.name(native_functions_map[op_name].func)
140
kernel_name += "_symint"
141
# TODO: allow structured external backends later.
143
kernel=kernel_name, structured=False, cpp_namespace=cpp_namespace
145
metadata[op_name] = m
147
dispatch_key=dispatch_key,
148
use_out_as_primary=use_out_as_primary,
150
device_guard=use_device_guard,
154
backend_key: DispatchKey | None = None
155
if len(supported) > 0:
157
lambda: f'The provided value for "backend" must be a valid DispatchKey, but got {backend}.'
159
backend_key = DispatchKey.parse(backend)
161
backend_idx = create_backend_index(
165
use_out_as_primary=use_out_as_primary,
166
use_device_guard=use_device_guard,
168
assert backend_key not in backend_indices
169
backend_indices[backend_key] = backend_idx
171
autograd_key: DispatchKey | None = None
172
if len(supported_autograd) > 0:
174
lambda: f'The "autograd" key was specified, which indicates that you would like to override \
175
the behavior of autograd for some operators on your backend. However "Autograd{backend}" is not a valid DispatchKey.'
177
autograd_key = DispatchKey.parse(f"Autograd{backend}")
179
autograd_idx = create_backend_index(
183
use_out_as_primary=use_out_as_primary,
184
use_device_guard=use_device_guard,
186
assert autograd_key not in backend_indices
187
backend_indices[autograd_key] = autograd_idx
189
for g in grouped_native_functions:
190
if isinstance(g, NativeFunction):
193
if backend_key is None
196
for m in [backend_indices[backend_key].get_kernel(g)]
202
if autograd_key is None
205
for m in [backend_indices[autograd_key].get_kernel(g)]
212
if backend_key is None
216
backend_indices[backend_key].get_kernel(f)
217
for f in g.functions()
224
if autograd_key is None
228
backend_indices[autograd_key].get_kernel(f)
229
for f in g.functions()
235
forward_kernels = [f for f in forward_kernels if f is not None]
236
backward_kernels = [f for f in backward_kernels if f is not None]
238
len(forward_kernels) == 0 or len(backward_kernels) == 0
239
), f'Currently, all variants of an op must either be registered to a backend key, or to a backend\'s \
240
autograd key. They cannot be mix and matched. If this is something you need, feel free to create an issue! \
241
{forward_kernels[0].kernel} is listed under "supported", but {backward_kernels[0].kernel} is listed under "autograd".'
243
return ParsedExternalYaml(
244
backend_key, autograd_key, class_name, cpp_namespace, backend_indices
248
def error_on_missing_kernels(
249
native_functions: Sequence[NativeFunction],
250
backend_indices: dict[DispatchKey, BackendIndex],
251
backend_key: DispatchKey,
252
autograd_key: DispatchKey | None,
254
kernel_defn_file_path: str,
255
full_codegen: list[OperatorName] | None = None,
258
with open(kernel_defn_file_path) as f:
259
backend_defns = f.read()
261
raise AssertionError(
262
f"Unable to read from the specified impl_path file: {kernel_defn_file_path}"
265
if full_codegen is None:
268
indices = [backend_indices[backend_key].index] + (
269
[] if autograd_key is None else [backend_indices[autograd_key].index]
271
# Quick mapping from each OperatorName used by the external backend
272
# to its backend kernel name
273
expected_backend_op_names: dict[OperatorName, str] = dict(
277
(op_name, metadata.kernel) for op_name, metadata in index.items()
283
expected_backend_native_funcs: list[NativeFunction] = [
285
for f in native_functions
286
if f.func.name in expected_backend_op_names.keys()
287
and f.func.name not in full_codegen
289
expected_backend_kernel_name_counts: dict[str, list[NativeFunction]] = defaultdict(
292
for native_f in expected_backend_native_funcs:
293
expected_backend_kernel_name_counts[
294
expected_backend_op_names[native_f.func.name]
297
# This just looks for lines containing "foo(", and assumes that the kernel foo has been implemented.
298
# It might cause false negatives (we won't catch all cases), but that's ok - if we catch a missing kernel
299
# here, then we get a nicer error message. If we miss it, you get a linker error.
300
kernel_defn_regex = rf"(.*){class_name}::\s*([\w\d]*)\("
301
actual_backend_kernel_name_counts = Counter(
302
# A bit unwieldy (this could probably be moved into regex),
303
# but we don't want to include kernel names that come from function calls,
304
# like "return torch_xla::XLANativeFunctions::empty_strided_symint(...)".
305
# Easy check is to ignore any lines with colons before the class name.
308
for (x, y) in re.findall(kernel_defn_regex, backend_defns)
309
if not x.endswith(":")
313
missing_kernels_err_msg = ""
314
for expected_name, funcs in expected_backend_kernel_name_counts.items():
315
expected_overload_count = len(funcs)
316
actual_overload_count = actual_backend_kernel_name_counts[expected_name]
317
if expected_overload_count != actual_overload_count:
319
def create_decl(f: NativeFunction) -> str:
320
with native_function_manager(f):
321
return DispatcherSignature.from_schema(f.func).decl()
323
expected_schemas_str = "\n".join([create_decl(f) for f in funcs])
324
missing_kernels_err_msg += f"""
325
{class_name} is missing a kernel definition for {expected_name}. We found {actual_overload_count} kernel(s) with that name,
326
but expected {expected_overload_count} kernel(s). The expected function schemas for the missing operator are:
327
{expected_schemas_str}
330
assert missing_kernels_err_msg == "", missing_kernels_err_msg
334
parser = argparse.ArgumentParser(description="Generate backend stub files")
339
help="path to source yaml file containing operator external definitions",
341
parser.add_argument("-o", "--output-dir", "--output_dir", help="output directory")
343
"--dry-run", "--dry_run", type=bool, default=False, help="output directory"
350
help="path to the source C++ file containing kernel definitions",
352
options = parser.parse_args()
354
run(options.source_yaml, options.output_dir, options.dry_run, options.impl_path)
357
def gen_dispatchkey_nativefunc_headers(
361
backend_indices: dict[DispatchKey, BackendIndex],
362
grouped_native_functions: Sequence[NativeFunction | NativeFunctionsGroup],
363
backend_dispatch_key: DispatchKey,
364
autograd_dispatch_key: DispatchKey | None,
365
backend_name: str = "",
367
assert class_name is not None
368
generated_comment = (
369
"Autogenerated file by gen_backend_stubs.py. Do not edit directly!"
372
# Convert to a set first to remove duplicate kernel names.
373
# Backends are allowed to repeat kernel names; only generate the declaration once!
374
# Sort for deterministic output.
375
backend_declarations = sorted(
378
lambda f: dest.compute_native_function_declaration(
379
f, backend_indices[backend_dispatch_key]
381
grouped_native_functions,
385
autograd_declarations = sorted(
389
if autograd_dispatch_key is None
390
else dest.compute_native_function_declaration(
391
f, backend_indices[autograd_dispatch_key]
393
grouped_native_functions,
398
ns_helper = NamespaceHelper(cpp_namespace)
399
fm.write_with_template(
400
f"{backend_dispatch_key}NativeFunctions.h",
401
"DispatchKeyNativeFunctions.h",
403
"generated_comment": generated_comment,
404
"namespace_prologue": ns_helper.prologue,
405
"class_name": class_name,
406
"namespace_epilogue": ns_helper.epilogue,
407
"dispatch_declarations": backend_declarations + autograd_declarations,
408
"BackendName": backend_name,
409
"DispatchKey": backend_dispatch_key,
414
def gen_dispatcher_registrations(
418
backend_indices: dict[DispatchKey, BackendIndex],
419
grouped_native_functions: Sequence[NativeFunction | NativeFunctionsGroup],
420
backend_dispatch_key: DispatchKey,
421
dispatch_key: DispatchKey,
422
selector: SelectiveBuilder,
423
# build_in_tree is true for lazy TS backend and affects include paths, not used for external backends
424
build_in_tree: bool = False,
425
per_operator_headers: bool = False,
426
backend_name: str = "",
427
eager_registration: bool = True,
430
f"{output_dir}/{backend_dispatch_key}NativeFunctions.h",
433
external_backend_headers_str = "\n".join(f"#include <{h}>" for h in headers)
435
external_backend_headers_str = "\n".join(f'#include "{h}"' for h in headers)
437
assert class_name is not None
438
backend_index = backend_indices[dispatch_key]
440
dispatch_registrations_body = list(
442
dest.RegisterDispatchKey(
448
class_method_name=f"{class_name}",
449
skip_dispatcher_op_registration=False,
451
grouped_native_functions,
455
ns_helper = NamespaceHelper(namespace_str="at")
456
deferred_dispatch_registrations = ""
457
static_init_dispatch_registrations = ""
458
if eager_registration:
459
static_template = CodeTemplate(
461
TORCH_LIBRARY_IMPL(aten, $dispatch_key, m) {
462
$dispatch_registrations_body
465
static_init_dispatch_registrations = static_template.substitute(
466
dispatch_key=dispatch_key,
467
dispatch_registrations_body=dispatch_registrations_body,
470
deferred_template = CodeTemplate(
472
TORCH_API void Register${backend_name}${dispatch_key}NativeFunctions();
473
TORCH_API void Register${backend_name}${dispatch_key}NativeFunctions() {
474
static auto m = MAKE_TORCH_LIBRARY_IMPL(aten, $dispatch_key);
475
$dispatch_registrations_body
478
deferred_dispatch_registrations = deferred_template.substitute(
479
backend_name=backend_name,
480
dispatch_key=dispatch_key,
481
dispatch_registrations_body=dispatch_registrations_body,
484
fm.write_with_template(
485
f"Register{dispatch_key}.cpp",
486
"RegisterDispatchKey.cpp",
488
"extra_cuda_headers": "",
489
"external_backend_headers": external_backend_headers_str,
490
"ops_headers": "#include <ATen/Functions.h>"
491
if not per_operator_headers
493
"DispatchKey": dispatch_key,
494
"dispatch_namespace": dispatch_key.lower(),
495
"dispatch_headers": dest.gen_registration_headers(
496
backend_index, per_operator_headers=per_operator_headers, rocm=False
498
"dispatch_definitions": fm.substitute_with_template(
499
"RegisterDispatchDefinitions.ini",
501
"ns_prologue": ns_helper.prologue,
502
"ns_epilogue": ns_helper.epilogue,
503
"static_init_dispatch_registrations": static_init_dispatch_registrations,
504
"deferred_dispatch_registrations": deferred_dispatch_registrations,
505
"dispatch_helpers": dest.gen_registration_helpers(backend_index),
506
"dispatch_namespace": dispatch_key.lower(),
507
"dispatch_namespaced_definitions": "",
508
"dispatch_anonymous_definitions": list(
510
dest.RegisterDispatchKey(
512
Target.ANONYMOUS_DEFINITION,
516
class_method_name=f"{class_name}",
517
skip_dispatcher_op_registration=False,
519
grouped_native_functions,
529
source_yaml: str, output_dir: str, dry_run: bool, impl_path: str | None = None
531
# Assumes that this file lives at PYTORCH_ROOT/torchgen/gen_backend_stubs.py
532
pytorch_root = Path(__file__).parent.parent.absolute()
533
template_dir = os.path.join(pytorch_root, "aten/src/ATen/templates")
535
def make_file_manager(install_dir: str) -> FileManager:
537
install_dir=install_dir, template_dir=template_dir, dry_run=dry_run
540
fm = make_file_manager(output_dir)
542
native_yaml_path = os.path.join(
543
pytorch_root, "aten/src/ATen/native/native_functions.yaml"
545
tags_yaml_path = os.path.join(pytorch_root, "aten/src/ATen/native/tags.yaml")
546
parsed_yaml = parse_native_yaml(native_yaml_path, tags_yaml_path)
547
native_functions, backend_indices = (
548
parsed_yaml.native_functions,
549
parsed_yaml.backend_indices,
551
grouped_native_functions = get_grouped_native_functions(native_functions)
552
parsed_backend_yaml = parse_backend_yaml(
553
source_yaml, grouped_native_functions, backend_indices
555
backend_key = parsed_backend_yaml.backend_key
556
autograd_key = parsed_backend_yaml.autograd_key
557
cpp_namespace = parsed_backend_yaml.cpp_namespace
558
class_name = parsed_backend_yaml.class_name
559
backend_indices = parsed_backend_yaml.backend_indices
561
selector = SelectiveBuilder.get_nop_selector()
563
if backend_key is None:
564
# This could be useful if a backend wants to quickly set up a noop yaml file but doesn't have any kernels ready yet.
567
if class_name is None:
568
# class_name is an optional argument to backend yaml file.
569
# if specified it allows an external backend to override
570
# the name of the class that all generated kernel definitions live under.
571
# if not specified, its value is given as native_function_class_name.
572
class_name = backend_indices[backend_key].native_function_class_name()
573
assert class_name is not None
575
if impl_path is not None:
576
error_on_missing_kernels(
585
gen_dispatchkey_nativefunc_headers(
590
grouped_native_functions,
595
for dispatch_key in (
596
[backend_key] if autograd_key is None else [backend_key, autograd_key]
598
gen_dispatcher_registrations(
603
grouped_native_functions,
610
if __name__ == "__main__":