# Backend API (experimental) To choose an existing backend, see {doc}`../backends`. This page is for authors implementing a backend. `helion.backend_api` is the interface for codegen backends that live outside the Helion repository. A backend subclasses `helion.backend_api.Backend` and imports Helion internals only from this module. ```{warning} This interface is experimental. Out-of-tree backends should pin one Helion minor version (e.g. `helion>=1.Y,<1.(Y+1)`). ``` ## What is covered `BACKEND_API_VERSION` covers exactly the following. Anything else, including other attributes of the classes listed here, may change without notice. 1. **Importability.** Every name in `helion.backend_api.__all__` stays importable from `helion.backend_api`. `register_compiler_backend` is idempotent for the same class and raises `ValueError` if a different class is registered under an already-registered name (including built-in names). The exception is a class with the same module and qualified name, as a reloaded module (`importlib.reload`) or a re-run factory defines: it replaces the earlier one (so a factory that builds two different backends under one name replaces the first silently). 2. **`Backend` hooks.** The names and signatures of these `Backend` members (the list is also `helion.backend_api.COVERED_BACKEND_HOOKS`): `name`, `dtype_str`, `acc_type`, `function_decorator`, `constexpr_type`, `default_launcher_name`, `library_imports` (the abstract members); `codegen_name`, `state`, `validate_environment`; `pre_inductor_lowering`, `inductor_op_overrides`; `customize_ast`, `pre_codegen`, `create_loop_strategy`, `create_reduction_strategy`; `program_id_expr`, `grid_index_expr`, `loop_index_expr`, `arange_expr`, `range_str`, `lane_index_expr`, `lane_offset_expr`, `thread_in_tile_mask_expr`, `scalar_load_expr`, `inline_constexpr`, `scalar_arg_preamble`, `finalize_device_body` (called once per device function after its body is generated and before variable renaming; returns the body and the kernel parameters to append. Of its `DeviceBodyContext`, `device_function`, `args` and `body_start` (the index of the first traced-body statement; the statements before it are the `scalar_arg_preamble` statements, then the device function's own preamble, e.g. persistent program-id setup) are covered); `cast_expr`, `cast_scalar_ast`; `where_expr`, `minimum_expr`, `zeros_expr`, `full_expr`, `reshape_expr`, `broadcast_to_expr`, `expands_broadcast_dims`; `reduction_expr`, `reduction_combine_expr`, `reduction_index_expr`, `reduction_index_zero_expr`, `wrap_reduction_accumulator`, `looped_reduction_thread_count`, `register_reduction_loop_config_slots`, `max_reduction_threads`, `max_reduction_loop`, `reduction_block_size_is_inlined_constexpr`, `launches_surplus_tile_threads`; `launcher_keyword_args`; `supports_config_key`, `config_spec_extension`, `tunable_fragments`, `supports_precompile`, `adjust_block_size_constraints`, `autotune`, `get_do_bench`, `get_interleaved_bench`. `config_spec_extension` returns a `BackendSearchSurface` (normally one module-level instance) whose `config_keys` are the backend's own autotuner keys and whose `debug_keys` are keys `normalize` accepts but the search does not try. Other backends reject these keys unless they list them explicitly, and while the backend is registered no kernel can use their names (or the names without a trailing `s`) for `hl.register_tunable`, so prefix them with the plugin's name. A plugin key is searched when `tunable_fragments` gives it a fragment; as for built-in tunables, `normalize` fills in its default but does not check a given value. `tunable_fragments` may return Helion's built-in backend tunables and the backend's own surface keys; declaring a gated generic key such as `xcd_remap` on the surface does not add it. `supports_config_key` overrides must defer to `super()` for these keys (an override that accepts a fixed list of keys must list them), or `ConfigSpec` raises; for a key supported only on some targets, `tunable_fragments` must return its fragment only on those targets. Registering raises `ValueError` if either set holds a generic key such as `num_warps`. Of `BackendSearchSurface`, `config_keys`, `debug_keys` and `fingerprint_extra` are covered; its other members (e.g. `fields`) are not. The surface's `fingerprint_extra(config_spec)` returns extra identity for the autotune cache (e.g. the plugin's package version): return a value whose `repr` is the same in every process and host, or `None` (the default, which leaves cache keys unchanged). A changed value makes cached configs miss, so an upgraded plugin re-tunes; it also stops older results from seeding the new search. The autotuner times candidates with `get_do_bench()` (single config) and `get_interleaved_bench()` (candidates compared side by side); returning `None` keeps Helion's GPU-event timers. A returned timer is called like `helion.autotuner.benchmarking.do_bench` (`fn`, keyword `warmup` and `rep` in ms, `return_mode`, `process_group_name`, and for some timers `l2_flush_mode`) or `interleaved_bench` (`fns`, keyword `repeat`, `desc`, `max_total_ms`, ...), and returns milliseconds: a `float` from the single-config timer, a list from the interleaved one. Accept keyword arguments you do not use, because the set Helion passes can grow. With a custom `get_do_bench`, candidates are timed in the autotuning process (not in a separate benchmark process) and `autotune_benchmark_input_banks` must stay 1; `autotune_benchmark_fn`, when set, takes the place of `get_interleaved_bench`. 3. **Strategy subclassing.** The names and signatures of `TileStrategy.user_size`, `TileStrategy.codegen_grid`, `TileStrategy.codegen_device_loop`, `TileStrategy.codegen_preamble` and `ReductionStrategy.codegen_reduction` (also `helion.backend_api.COVERED_STRATEGY_METHODS`). 4. **Op objects.** `reduce_op` and `mask_to_op` are the API functions that backends attach `@codegen` handlers to. Public ops are attached through `helion.language` (e.g. `hl.load`, `hl.store`, `hl.subscript`). The `codegen` decorator itself is covered as `codegen(original_fn, backend)` used as `@codegen(op, "backend_name")`. 5. **Sympy helper.** `symint_sympy_expr(x)` returns the `sympy.Expr` behind a `torch.SymInt`; its signature (takes a SymInt, returns `sympy.Expr`) is covered, which is all codegen sites rely on. 6. **Per-function state.** `DeviceFunction.backend_codegen_state`, a dict for codegen state shared across a backend's op handlers while one device function is generated. Use your backend's name as the key; the value is yours. ## What is not covered The contents of `exc`, the behavior of `TritonBackend` beyond being subclassable, and what `InductorOpOverrides` aliases (torch's private `OpsHandler[Any]`; only the name is covered). ## Version policy | Change | Action | |---|---| | Add an optional hook with a safe default | No bump | | Add a covered attribute or exported name | No bump | | Make a hook required | Bump | | Remove, rename, or change the signature of a covered member | Bump | | Change anything not covered | No bump | ```{eval-rst} .. py:data:: helion.backend_api.BACKEND_API_VERSION :type: int :value: 1 Version of the covered interface above. ```