kernel_dispatch_table.h#
Per-(domain, op_type) dispatch table used by :cpp:func:RunNode / :cpp:class:RuntimeSession to resolve each NodeProto to a matching kernel factory.
The generic dispatch mechanism (this file, :cpp:class:RuntimeContext, :cpp:func:RunNode, …) lives in onnx_core so it has no dependency on any particular set of operator kernels. The concrete kernel implementations for every standard ONNX operator remain in onnx_kernels and register themselves here via :cpp:func:RegisterKernelFn instead of being hard-coded in this table, which keeps the onnx_core -> onnx_kernels dependency direction from ever being introduced.
-
namespace onnx_light
Alias that makes onnx-light headers compatible with code that references
ONNX_LIGHT_NAMESPACE(the macro used in the standard onnx package).Set to
ONNX_LIGHT_NAMESPACEso both names resolve to the same namespace.Symbol-visibility attribute for the public onnx-light C++ API.
Maps the upstream compatibility macro to onnx-light’s explicit proto ABI annotation. This keeps declarations from vendored ONNX headers visible when
lib_onnx_protouses hidden visibility by default.Namespace alias so that ONNX C++ code (and consumers such as onnxruntime) that refers to the literal
onnxnamespace — rather than theONNX_NAMESPACEmacro — resolves to the onnx-light namespace. The standard onnx package lives innamespace onnx; onnx-light usesonnx_light(via ONNX_LIGHT_NAMESPACE), so this alias keeps onnx-light a true drop-in. It is only introduced when the onnx-light namespace differs fromonnx.-
namespace core
-
namespace runtime
Typedefs
-
using SequenceMapPackFn = std::function<Sequences(RuntimeContext &rt, const Sequence &input_sequence, const std::vector<Tensors> &body_outputs_per_iter)>#
Signature of the
SequenceMapoutput-packing callback: given the input sequence (whose length sets the iteration count) and the per-iteration body outputs (body_outputs_per_iter[k][i]is body outputkat iterationi), returns theMassembled output sequences. Registered byonnx_kernels(kernel::SequenceMap) so that :cpp:func:RunNode’sSequenceMaporchestration (which must live inonnx_coresince it recursively drives a :cpp:class:RuntimeSession) never has to include anonnx_kernelsheader directly.
Functions
-
NodeKernelFn MakeCustomKernelFactory(CustomKernelFn fn)#
Adapts a callback to a factory without copying or serializing the node. Each resolution copies the callable into a fresh session-owned KernelBase.
-
NodeKernelFn MakeCustomKernelFactory(ContextualCustomKernelFn fn)#
-
template<typename Kernel, typename ExecutionScope>
std::unique_ptr<KernelBase> MakeSessionKernel(const NodeProto &node, const KernelContext &kernel_context)# Builds a backend kernel adapted to the active runtime session.
Kernelsupplies the operator implementation andExecutionScopeinstalls the backend-specific executor bridge while the kernel runs.Returns: The ready-to-run kernel owned through :cpp:class:
KernelBase.
-
const std::unordered_map<std::string, NodeKernelFn> &KernelDispatchTable()#
Returns the
(normalised_domain, op_type) -> NodeKernelFnfactory dispatch table. Empty until kernel libraries (e.g.onnx_kernels) populate it via :cpp:func:RegisterKernelFn.
-
bool RegisterKernelFn(const std::string &domain, const std::string &op_type, symbolic::Device device, NodeKernelFn fn, bool overwrite = true)#
Registers (or replaces) the kernel factory for the identifier (
domain,op_type,device) in the shared :cpp:func:KernelDispatchTable.Use an empty string for
domainto denote the default ONNX domain (normalised to :cpp:var:kDefaultOnnxDomain).deviceis part of the identifier so that a distinct kernel can be registered per device; :cpp:enumerator:symbolic::Device::kCPU(and :cpp:enumerator:symbolic::Device::kUndefined) denote the default host entry. Intended to be called once per operator during static initialization by kernel libraries that must not be linked intoonnx_core(e.g.onnx_kernels); seeonnx_kernels::RegisterKernelFunctions.When
overwriteistrue(the default) an existing entry for the same identifier is replaced. Passfalseto register the factory only if no kernel is registered for that identifier yet; this lets bulk built-in registration (onnx_kernels::RegisterKernelFunctions) run without clobbering an override that a downstream library (e.g.onnx-light-cpu) installed earlier, so a custom kernel wins regardless of the order in which the two registrations happen. Explicit per-operator overrides keep the defaultoverwrite = trueso they always take precedence over the built-ins.Replacement affects future resolutions only. Existing sessions retain their kernels. Registry mutation must not overlap resolution on any thread; separate sessions may run concurrently if their kernels do not share mutable state. Concurrent runs on the same session are not supported.
- Parameters:
domain – The operator domain (
""or"ai.onnx"for standard ONNX).op_type – The ONNX operator type name (e.g.
"Abs").device – The device the kernel runs on (e.g. :cpp:enumerator:
symbolic::Device::kCPU).fn – The factory implementing kernel construction for this operator.
overwrite – Whether to replace an existing entry (
true, default) or keep it and ignorefnwhen one already exists (false).
- Returns:
truewhenfnwas stored,falsewhen an existing entry was kept becauseoverwritewasfalse.
-
bool RegisterKernelFn(const std::string &domain, const std::string &op_type, symbolic::Device device, KernelRegistration registration, bool overwrite = true)#
Registers a factory and its matching estimator together. A refused registration changes neither registry. Success replaces global shape-only accounting and retains the estimator on each future resolved kernel. An empty estimator, including through a bare factory-only registration, selects zero additional memory. Factories produced by MakeKernelFactory retain their associated estimator, including when copied from KernelDispatchTable and registered again. Existing sessions retain their resolved implementation and accounting. Standalone shape-estimator registration is independent and should not be used to replace accounting for an implementation registered through this contract.
-
const CustomKernelMap &GlobalCustomKernels()#
Returns the process-wide (global) custom-kernel registry consulted by :cpp:func:
RunNode/ :cpp:class:RuntimeSessionduring kernel resolution.Global custom kernels complement the per-:cpp:class:
RuntimeContextregistry (:cpp:func:RuntimeContext::RegisterCustomKernel): they let a caller install a kernel once and have every :cpp:class:RuntimeContextpick it up, rather than registering it on each context separately. Resolution precedence is: model-local functions, built-in control-flow operators, per-context custom kernels, global custom kernels, then the built-in :cpp:func:KernelDispatchTable. A per-context registration for the same(domain, op_type)therefore overrides the global one.Keys are the canonical
"<domain>:<op_type>"pair (the default ONNX domain — the emptyNodeProto::domain()— is normalised to :cpp:var:kDefaultOnnxDomain).Like the built-in :cpp:func:
KernelDispatchTable, this registry is a plain process-wide singleton and is not synchronised: register / unregister global kernels before starting concurrent :cpp:func:RunNode/ :cpp:class:RuntimeSession::Runcalls (e.g. during start-up), not while other threads are resolving nodes.
-
void RegisterGlobalCustomKernel(const std::string &domain, const std::string &op_type, CustomKernelFn fn)#
Registers (or replaces) a process-wide (global) custom kernel for (
domain,op_type) in :cpp:func:GlobalCustomKernels. The empty domain is normalised to :cpp:var:kDefaultOnnxDomain. Unlike :cpp:func:RegisterKernelFn(which registers a per-device kernel factory),fnkeeps the simple “run the whole node now” contract of :cpp:type:CustomKernelFn. Registration converts it to a :cpp:type:NodeKernelFn; each resolution creates a new session-owned adapter retaining the callable and a non-owning pointer to the node.
-
void RegisterGlobalCustomKernel(const std::string &domain, const std::string &op_type, ContextualCustomKernelFn fn)#
-
void RegisterGlobalCustomKernelFactory(const std::string &domain, const std::string &op_type, NodeKernelFn fn)#
Registers a factory in the global custom override registry. This registry retains the precedence of the callback convenience API, but stores only factories and uses the same session-owned lifecycle as RegisterKernelFn.
-
bool UnregisterGlobalCustomKernel(const std::string &domain, const std::string &op_type)#
Removes the process-wide custom kernel registered for (
domain,op_type). The empty domain is normalised to :cpp:var:kDefaultOnnxDomain. Returnstruewhen an entry was removed,falseotherwise.
-
void ClearGlobalCustomKernels()#
Removes every process-wide custom kernel registration from :cpp:func:
GlobalCustomKernels.
-
const SequenceMapPackFn &GetSequenceMapPackFn()#
Returns the currently registered
SequenceMapoutput-packing callback, or an emptystd::functionif none has been registered yet (see :cpp:func:RegisterSequenceMapPackFn).
-
void RegisterSequenceMapPackFn(SequenceMapPackFn fn)#
Registers the
SequenceMapoutput-packing callback (see :cpp:type:SequenceMapPackFn). Called once byonnx_kernels::RegisterKernelFunctions.
-
template<typename Kernel, typename ExecutionScope>
class SessionKernel : public Kernel# - #include <kernel_dispatch_table.h>
Adapts a backend kernel to the lifetime of a :cpp:class:
RuntimeSession.ExecutionScoperemains backend-defined: constructing it from the active :cpp:class:RuntimeContextinstalls any backend-specific executor bridge for the duration of one kernel invocation. Keeping that policy outsideonnx_corelets downstream libraries reuse the session factory without introducing a dependency fromonnx_coreto a concrete backend.Public Functions
-
inline SessionKernel(const NodeProto &node, const KernelContext &kernel_context)#
-
inline void Run(RuntimeContext &rt) override#
-
inline SessionKernel(const NodeProto &node, const KernelContext &kernel_context)#
-
using SequenceMapPackFn = std::function<Sequences(RuntimeContext &rt, const Sequence &input_sequence, const std::vector<Tensors> &body_outputs_per_iter)>#
-
namespace runtime
-
namespace core