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_NAMESPACE so 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_proto uses hidden visibility by default.

Namespace alias so that ONNX C++ code (and consumers such as onnxruntime) that refers to the literal onnx namespace — rather than the ONNX_NAMESPACE macro — resolves to the onnx-light namespace. The standard onnx package lives in namespace onnx; onnx-light uses onnx_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 from onnx.

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:FunctionProto definitions.

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

inline OpsetId DefaultOpset(int64_t version)#

Builds an :ref:OpsetId for the default ai.onnx domain (empty string).

class KernelBase#
#include <kernel_context.h>

Base class for every kernel.

Each concrete kernel class derives from KernelBase so it inherits ownership of the construction-time KernelContext. Derived kernels access the context through the protected ctx_ member and typically inherit KernelBase’s constructor via using KernelBase::KernelBase;, which preserves the explicit qualifier on the single-argument constructor.

ctx_ is stored by value so a kernel remains independent of the caller-owned KernelContext after construction. Backend-test builders construct short-lived contexts and kernels only while materializing reference outputs.

KernelBase is also the runtime dispatch interface: it exposes a virtual :cpp:func:Run that the runtime (:cpp:func:RunNode / :cpp:class:RuntimeSession) calls once per node. Each dispatch-registered kernel overrides :cpp:func:Run to read the node’s current inputs from rt.tensors(), invoke its own operator() 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:Run throws — 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 boilerplate KernelContext 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 of rt: 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 -1 preserves 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 &parameters)#

Validates and copies resolved parameters into this kernel’s typed, immutable configuration.

Called at most once, while a :cpp:class:RuntimeSession initializes 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 store with 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 nullptr when the kernel is used directly (e.g. in tests) without being resolved through the dispatch path.

shapes::ComputePeakMemoryNodeFn peak_memory_#
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 KernelContext argument. 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 opset field 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

OpsetId opset#

Opset against which the kernel must behave (domain + version).

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. nullptr when the owning :cpp:class:RuntimeContext has no allocator attached (the default), in which case results fall back to inline std::vector storage. 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.

const std::unordered_set<std::string> *consumable_inputs = nullptr#

Single-consumer, non-output initializers whose owner explicitly permits retirement. Borrowed from the session; null for ordinary borrowed-model sessions.

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

KernelPreparationInfo info#
std::function<KernelPreparationMemory(const RuntimeContext&)> estimate_memory#
std::function<void(RuntimeContext&)> execute#
std::function<bool()> sources_consumed#

Confirms that all consumed_inputs were replaced, including zero-new-byte cache hits.

struct KernelPreparationInfo#
#include <kernel_context.h>

Describes preparation work without exposing kernel-borrowing executable callables.

Public Members

size_t index = 0#
std::vector<std::string> source_inputs#
std::vector<std::string> consumed_inputs#

Sources replaced for every inference shape after successful preparation.

std::string representation#
std::vector<std::string> output_slots#
std::vector<size_t> dependencies#
bool awaits_runtime_tuning = false#
struct KernelPreparationMemory#
#include <kernel_context.h>

Describes peak live bytes needed by one preparation, excluding the existing store.

Public Members

size_t source_bytes = 0#
size_t temporary_bytes = 0#
size_t prepared_bytes = 0#
struct OpsetId#
#include <kernel_context.h>

Lightweight opset identifier used by the backend test library.

Mirrors the (domain, version) pair carried by OperatorSetIdProto but keeps the public API of this library independent from the proto type so test cases can be declared without touching the proto wire format.

Public Functions

OpsetId() = default#
inline OpsetId(std::string domain_, int64_t version_)#

Public Members

std::string domain#
int64_t version = 0#