Backend API (experimental)#
To choose an existing backend, see Choosing a Backend. 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.
Importability. Every name in
helion.backend_api.__all__stays importable fromhelion.backend_api.register_compiler_backendis idempotent for the same class and raisesValueErrorif 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).Backendhooks. The names and signatures of theseBackendmembers (the list is alsohelion.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 itsDeviceBodyContext,device_function,argsandbody_start(the index of the first traced-body statement; the statements before it are thescalar_arg_preamblestatements, 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_extensionreturns aBackendSearchSurface(normally one module-level instance) whoseconfig_keysare the backend’s own autotuner keys and whosedebug_keysare keysnormalizeaccepts 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 trailings) forhl.register_tunable, so prefix them with the plugin’s name. A plugin key is searched whentunable_fragmentsgives it a fragment; as for built-in tunables,normalizefills in its default but does not check a given value.tunable_fragmentsmay return Helion’s built-in backend tunables and the backend’s own surface keys; declaring a gated generic key such asxcd_remapon the surface does not add it.supports_config_keyoverrides must defer tosuper()for these keys (an override that accepts a fixed list of keys must list them), orConfigSpecraises; for a key supported only on some targets,tunable_fragmentsmust return its fragment only on those targets. Registering raisesValueErrorif either set holds a generic key such asnum_warps. OfBackendSearchSurface,config_keys,debug_keysandfingerprint_extraare 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 whoserepris the same in every process and host, orNone(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) andget_interleaved_bench()(candidates compared side by side); returningNonekeeps Helion’s GPU-event timers. A returned timer is called likehelion.autotuner.benchmarking.do_bench(fn, keywordwarmupandrepin ms,return_mode,process_group_name, and for some timersl2_flush_mode) orinterleaved_bench(fns, keywordrepeat,desc,max_total_ms, …), and returns milliseconds: afloatfrom 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 customget_do_bench, candidates are timed in the autotuning process (not in a separate benchmark process) andautotune_benchmark_input_banksmust stay 1;autotune_benchmark_fn, when set, takes the place ofget_interleaved_bench.Strategy subclassing. The names and signatures of
TileStrategy.user_size,TileStrategy.codegen_grid,TileStrategy.codegen_device_loop,TileStrategy.codegen_preambleandReductionStrategy.codegen_reduction(alsohelion.backend_api.COVERED_STRATEGY_METHODS).Op objects.
reduce_opandmask_to_opare the API functions that backends attach@codegenhandlers to. Public ops are attached throughhelion.language(e.g.hl.load,hl.store,hl.subscript). Thecodegendecorator itself is covered ascodegen(original_fn, backend)used as@codegen(op, "backend_name").Sympy helper.
symint_sympy_expr(x)returns thesympy.Exprbehind atorch.SymInt; its signature (takes a SymInt, returnssympy.Expr) is covered, which is all codegen sites rely on.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 |