MCPcopy Create free account
hub / github.com/pytorch/executorch / gen_custom_ops_registration

Function gen_custom_ops_registration

codegen/api/custom_ops.py:85–151  ·  view source on GitHub ↗

Generate custom ops registration code for dest.RegisterDispatchKey. :param native_functions: a sequence of `NativeFunction` :param selector: for selective build. :param kernel_index: kernels for all the ops. :param rocm: bool for dest.RegisterDispatchKey. :return: generated

(
    *,
    native_functions: Sequence[NativeFunction],
    selector: SelectiveBuilder,
    kernel_index: ETKernelIndex,
    rocm: bool,
)

Source from the content-addressed store, hash-verified

83
84
85def gen_custom_ops_registration(
86 *,
87 native_functions: Sequence[NativeFunction],
88 selector: SelectiveBuilder,
89 kernel_index: ETKernelIndex,
90 rocm: bool,
91) -> tuple[str, str]:
92 """
93 Generate custom ops registration code for dest.RegisterDispatchKey.
94
95 :param native_functions: a sequence of `NativeFunction`
96 :param selector: for selective build.
97 :param kernel_index: kernels for all the ops.
98 :param rocm: bool for dest.RegisterDispatchKey.
99 :return: generated C++ code to register custom operators into PyTorch
100 """
101
102 # convert kernel index to BackendIndex. This is because we can't handle ETKernelIndex yet.
103 # TODO larryliu: evaluate if this code is still needed. If yes let it handle ETKernelIndex.
104
105 dispatch_key = DispatchKey.CPU
106 backend_index = kernel_index._to_backend_index()
107 static_init_dispatch_registrations = ""
108 ns_grouped_native_functions: dict[str, list[NativeFunction]] = defaultdict(list)
109 for native_function in native_functions:
110 ns_grouped_native_functions[native_function.namespace].append(native_function)
111
112 for namespace, functions in ns_grouped_native_functions.items():
113 if len(functions) == 0:
114 continue
115 dispatch_registrations_body = "\n".join(
116 list(
117 concatMap(
118 dest.RegisterDispatchKey(
119 backend_index,
120 Target.REGISTRATION,
121 selector,
122 rocm=rocm,
123 symint=False,
124 class_method_name=None,
125 skip_dispatcher_op_registration=False,
126 ),
127 functions,
128 )
129 )
130 )
131 static_init_dispatch_registrations += f"""
132TORCH_LIBRARY_IMPL({namespace}, {dispatch_key}, m) {{
133{dispatch_registrations_body}
134}}"""
135 anonymous_definition = "\n".join(
136 list(
137 concatMap(
138 dest.RegisterDispatchKey(
139 backend_index,
140 Target.ANONYMOUS_DEFINITION,
141 selector,
142 rocm=rocm,

Callers 1

gen_custom_opsFunction · 0.85

Calls 3

_to_backend_indexMethod · 0.80
itemsMethod · 0.80
appendMethod · 0.45

Tested by

no test coverage detected