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 NodeKernelFn = std::function<std::unique_ptr<KernelBase>(const NodeProto &node, RuntimeContext &rt)>#
Factory signature registered in :cpp:func:
KernelDispatchTablefor every(domain, op_type). Called once per node (during kernel resolution / initialization, e.g. by :cpp:func:RuntimeSession::Runor by :cpp:func:RunNode): validates the node’s input/output counts, constructs the concrete kernel object and attaches the node via :cpp:func:KernelBase::set_node, returning a :cpp:class:KernelBase. Must NOT perform any computation itself — all per-run computation belongs in the returned kernel’s :cpp:func:KernelBase::Run.
-
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
-
template<typename Kernel, typename ExecutionScope>
std::unique_ptr<KernelBase> MakeSessionKernel(const NodeProto &node, RuntimeContext &rt)# 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.- 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.
-
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.
-
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, RuntimeContext &rt)#
-
inline void Run(RuntimeContext &rt) override#
-
inline SessionKernel(const NodeProto &node, RuntimeContext &rt)#
-
using NodeKernelFn = std::function<std::unique_ptr<KernelBase>(const NodeProto &node, RuntimeContext &rt)>#
-
namespace runtime
-
namespace core