kernel_context.h#
-
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 OpsetImports = std::unordered_map<std::string, int64_t>#
Immutable opset imports owned by a model or function session.
-
using FunctionMap = std::unordered_map<std::string, const FunctionProto*>#
Name-keyed map of model-local :cpp:type:
FunctionProtodefinitions.Keys are the canonical
"<domain>:<op_type>:<overload>"triple with the default ONNX domain normalized to"ai.onnx". Values are non-owning pointers into a caller-owned model.
Functions
-
class KernelBase#
- #include <kernel_context.h>
Base class for every kernel.
Each concrete kernel class derives from
KernelBaseso it inherits ownership of the construction-timeKernelContext. Derived kernels access the context through the protectedctx_member and typically inheritKernelBase’s constructor viausing KernelBase::KernelBase;, which preserves theexplicitqualifier on the single-argument constructor.ctx_is stored by value so a kernel remains independent of the caller-ownedKernelContextafter construction. Backend-test builders construct short-lived contexts and kernels only while materializing reference outputs.KernelBaseis also the runtime dispatch interface: it exposes a virtual :cpp:func:Runthat the runtime (:cpp:func:RunNode/ :cpp:class:RuntimeSession) calls once per node. Each dispatch-registered kernel overrides :cpp:func:Runto read the node’s current inputs fromrt.tensors(), invoke its ownoperator()and store the produced outputs back. The node the kernel runs for is attached once (during kernel resolution) via :cpp:func:set_node; the owning graph / execution plan outlives the kernel, so the stored pointer stays valid for the kernel’s life. The default :cpp:func:Runthrows — only kernels registered in the dispatch table (and the control-flow / custom kernels) override it.Centralizing the context member here keeps every kernel class consistent, makes it trivial to extend the construction-time interface (e.g. by adding new fields to
KernelContext), and avoids a repeated boilerplateKernelContext ctx_;member in each kernel.Subclassed by onnx_light::core::runtime::If, onnx_light::core::runtime::Loop, onnx_light::core::runtime::Scan, onnx_light::onnx_kernels::tuning::ParallelTunableKernel
Public Functions
-
explicit KernelBase(const KernelContext &ctx)#
-
KernelBase(const KernelBase &other)#
-
KernelBase(KernelBase &&other) noexcept#
-
KernelBase &operator=(const KernelBase&) = default#
-
KernelBase &operator=(KernelBase&&) noexcept = default#
-
virtual ~KernelBase()#
-
inline void set_node(const NodeProto &node)#
Attaches the node this kernel runs for. Called once at kernel-resolution time; the node outlives the kernel.
-
inline void set_peak_memory(shapes::ComputePeakMemoryNodeFn fn)#
Retains implementation-specific accounting independently of subsequent registrations.
-
shapes::PeakMemoryEstimate ComputePeakMemory(symbolic::Device device, const std::vector<symbolic::SymShape> &input_shapes) const#
Returns this implementation’s additional memory, not the current global estimate.
-
virtual void Run(RuntimeContext &rt)#
Runs the kernel for its node (see :cpp:func:
set_node) against the current state ofrt: reads the node’s current inputs, computes and writes the outputs back. Safe to call repeatedly. The default throws; every dispatch-registered kernel overrides it.
-
virtual KernelTuningKey TuningKey(int32_t element_type) const#
Returns the tuning key for this implementation and
element_type.The default returns an undefined key, indicating that the kernel has no tuning schema. Tunable kernels override this together with :cpp:func:
Configure.
-
virtual int32_t TuningInputIndex() const noexcept#
Returns the node input whose element type selects the tuning key.
The default
-1preserves generic first-available-input selection. Kernels whose control input has a different type override this with the zero-based index of the data input that determines their implementation.
-
virtual void Configure(const KernelTuningParameters ¶meters)#
Validates and copies resolved parameters into this kernel’s typed, immutable configuration.
Called at most once, while a :cpp:class:
RuntimeSessioninitializes the kernel and before its first :cpp:func:Run.
-
virtual bool HasPreparations(const std::unordered_set<std::string> &immutable_inputs) const#
Returns whether this node consumes an immutable input it can prepare.
-
virtual void DeclarePreparations(const std::unordered_set<std::string> &immutable_inputs, std::vector<KernelPreparation> &preparations)#
Appends declarations for immutable inputs without reading their payloads.
The default does nothing. Kernels override this hook when they can transform immutable values into a session-owned execution format. Nested graph and model-local function sessions share
storewith their root session.
Public Static Functions
-
static uint64_t ConstructionCountForTesting()#
Returns the number of kernel instances constructed in this process.
-
static int64_t LiveInstanceCountForTesting()#
Returns the number of kernel instances currently alive in this process.
Protected Attributes
-
KernelContext ctx_#
-
const NodeProto *node_ = nullptr#
The node this kernel was built for, or
nullptrwhen the kernel is used directly (e.g. in tests) without being resolved through the dispatch path.
-
shapes::ComputePeakMemoryNodeFn peak_memory_#
-
explicit KernelBase(const KernelContext &ctx)#
-
struct KernelContext#
- #include <kernel_context.h>
Construction-time context passed to backend test kernel classes.
Kernels are implemented as classes whose constructor takes a single
KernelContextargument. The context bundles the opset against which the kernel must behave so the same kernel class can specialize its computation (or perform opset-specific validation) without changing its call sites.Only the
opsetfield is exposed today; new construction-time inputs (for example a device descriptor or an allocator) can be added later as additional fields without breaking existing kernel classes.Public Functions
-
KernelContext() = default#
-
inline explicit KernelContext(OpsetId opset_, RawBufferAllocator *allocator_ = nullptr, const OpsetImports *opset_imports_ = nullptr, const FunctionMap *model_functions_ = nullptr)#
Public Members
-
RawBufferAllocator *allocator = nullptr#
Allocator kernels must use to acquire the raw byte storage of the tensors they return, so no result buffer is allocated outside the runtime context.
nullptrwhen the owning :cpp:class:RuntimeContexthas no allocator attached (the default), in which case results fall back to inlinestd::vectorstorage. Initialized from the allocator supplied to :cpp:class:RuntimeContext.
-
const OpsetImports *opset_imports = nullptr#
Effective model/function imports. Borrowed from the owning session.
-
const FunctionMap *model_functions = nullptr#
Effective model-local functions. Borrowed from the owning model session.
-
KernelContext() = default#
-
struct KernelPreparation#
- #include <kernel_context.h>
Declares immutable work without accessing source payloads.
Callables borrow their kernel and model, which outlive the session’s work. Dependencies are preceding indices in the enclosing session’s declaration list. Execution must publish complete outputs only after successful computation.
Public Members
-
std::function<KernelPreparationMemory(const RuntimeContext&)> estimate_memory#
-
std::function<void(RuntimeContext&)> execute#
-
std::function<KernelPreparationMemory(const RuntimeContext&)> estimate_memory#
-
struct KernelPreparationInfo#
- #include <kernel_context.h>
Describes preparation work without exposing kernel-borrowing executable callables.
-
struct KernelPreparationMemory#
- #include <kernel_context.h>
Describes peak live bytes needed by one preparation, excluding the existing store.
-
struct OpsetId#
- #include <kernel_context.h>
Lightweight opset identifier used by the backend test library.
Mirrors the (domain, version) pair carried by
OperatorSetIdProtobut keeps the public API of this library independent from the proto type so test cases can be declared without touching the proto wire format.
-
using OpsetImports = std::unordered_map<std::string, int64_t>#
-
namespace runtime
-
namespace core