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

Function gen_functions_declarations

codegen/gen.py:393–444  ·  view source on GitHub ↗

Generates namespace separated C++ function API inline declaration/definitions. Native functions are grouped by namespaces and the generated code is wrapped inside namespace blocks. E.g., for `custom_1::foo.out` in yaml file we will generate a C++ API as a symbol in `torch::exec

(
    *,
    native_functions: Sequence[NativeFunction],
    kernel_index: ETKernelIndex,
    selector: SelectiveBuilder,
    use_aten_lib: bool,
    custom_ops_native_functions: Sequence[NativeFunction] | None = None,
)

Source from the content-addressed store, hash-verified

391
392
393def gen_functions_declarations(
394 *,
395 native_functions: Sequence[NativeFunction],
396 kernel_index: ETKernelIndex,
397 selector: SelectiveBuilder,
398 use_aten_lib: bool,
399 custom_ops_native_functions: Sequence[NativeFunction] | None = None,
400) -> str:
401 """
402 Generates namespace separated C++ function API inline declaration/definitions.
403 Native functions are grouped by namespaces and the generated code is wrapped inside
404 namespace blocks.
405
406 E.g., for `custom_1::foo.out` in yaml file we will generate a C++ API as a symbol
407 in `torch::executor::custom_1::foo_out`. This way we avoid symbol conflict when
408 the other `custom_2::foo.out` is available.
409 """
410
411 # convert kernel index to BackendIndex. This is because we can't handle ETKernelIndex yet.
412 # TODO larryliu: evaluate if this code is still needed. If yes let it handle ETKernelIndex.
413
414 backend_index = kernel_index._to_backend_index()
415
416 ns_grouped_functions = defaultdict(list)
417 for native_function in native_functions:
418 ns_grouped_functions[native_function.namespace].append(native_function)
419 functions_declarations = ""
420 newline = "\n"
421 for namespace in ns_grouped_functions:
422 ns_helper = NamespaceHelper(
423 namespace_str=namespace,
424 entity_name="",
425 max_level=3,
426 )
427 declarations = list(
428 mapMaybe(
429 ComputeFunction(
430 static_dispatch_backend_indices=[backend_index],
431 selector=selector,
432 use_aten_lib=use_aten_lib,
433 is_custom_op=lambda f: custom_ops_native_functions is not None
434 and f in custom_ops_native_functions,
435 ),
436 ns_grouped_functions[namespace],
437 )
438 )
439 functions_declarations += f"""
440{ns_helper.prologue}
441{newline.join(declarations)}
442{ns_helper.epilogue}
443 """
444 return functions_declarations
445
446
447def get_ns_grouped_kernels(

Calls 3

ComputeFunctionClass · 0.85
_to_backend_indexMethod · 0.80
appendMethod · 0.45