kernel_tuning.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 TuningValue = std::variant<int64_t, double, bool, std::string>#
Stores one portable scalar tuning value.
-
using KernelCalibrationFunction = std::function<KernelTuningParameters(const KernelTuningKey&, const CpuExecutionDescriptor&, const CalibrationOptions&, CalibrationReporter&)>#
Calibrates one exact, registered kernel tuning key.
-
using KernelTuningValidationHook = std::function<void(const KernelTuningParameters&)>#
Validates kernel-specific value ranges and relationships.
A hook throws
std::invalid_argumentwhen the complete parameter set is invalid. Generic name, key, presence, and type checks run before the hook.
-
using KernelTuningQueryValidationHook = std::function<std::optional<std::string>(const KernelTuningParameters&)>#
Validates kernel-specific value ranges without throwing on invalid input.
Returns:
std::nulloptwhen the complete parameter set is valid, otherwise the validation message describing why it is invalid.
Functions
-
std::string_view TuningValueTypeName(const TuningValue &value) noexcept#
Returns the stable type name of a tuning value.
Returns: One of
"int64","double","bool", or"string".
-
KernelTuningRegistry &GetKernelTuningRegistry()#
Returns the process-wide tuning registry.
-
void RegisterKernelTuningSchema(KernelTuningSchema schema)#
Registers a schema in the process-wide tuning registry.
-
void RegisterKernelTuningProfile(const KernelTuningKey &key, platform::CpuSelector processors, KernelTuningParameters parameters, int priority = 0)#
Registers a processor-specific profile in the process-wide tuning registry.
-
void RegisterKernelCalibrationFunction(const KernelTuningKey &key, KernelCalibrationFunction function)#
Registers a trusted calibration callback in the process-wide registry.
-
std::vector<KernelCalibrationCase> MakeElementwiseCalibrationCases(int32_t element_type, size_t input_count, int64_t first_elements, int64_t maximum_elements, bool include_broadcasting)#
Creates elementwise benchmark cases for unary or binary kernels.
Binary cases include equal-shape inputs and, when requested, scalar and multidirectional broadcasting cases for every problem size.
Returns: Deterministic benchmark cases ordered by increasing problem size.
-
KernelTuningParameters CalibrateKernelBenchmark(const KernelTuningKey &key, const CpuExecutionDescriptor &execution, const CalibrationOptions &options, CalibrationReporter &reporter, const KernelCalibrationBenchmark &benchmark)#
Runs a bounded, validated crossover search shared by unary and binary kernels.
Returns: A complete parameter set with the selected integer parameter.
-
CalibrationBatchReport CalibrateRegisteredKernels(const KernelCalibrationSelection &selection = {}, const CalibrationOptions &options = {})#
Runs selected registered callbacks and atomically publishes their profiles.
Unsupported keys have a tuning schema but no callback.
only_missingskips keys with an explicitly published profile.- Throws:
std::exception – from a calibration callback or profile validation. No profile is published unless every selected callback succeeds.
-
struct CalibrationBatchReport#
- #include <kernel_tuning.h>
Reports one explicit batch calibration and its atomic publication.
Public Functions
-
inline std::span<const KernelTuningParameters> successful_profiles() const noexcept#
Returns successfully validated profiles.
Public Members
-
uint64_t published_generation = 0#
-
std::vector<KernelTuningParameters> calibrated#
-
std::vector<KernelTuningKey> skipped#
-
std::vector<KernelTuningKey> unsupported#
-
std::vector<KernelCalibrationDiagnostic> diagnostics#
-
std::vector<KernelCalibrationResourceUsage> resources#
-
inline std::span<const KernelTuningParameters> successful_profiles() const noexcept#
-
struct CalibrationInputSpec#
- #include <kernel_tuning.h>
Describes one deterministic input generated for a calibration benchmark.
-
struct CalibrationOptions#
- #include <kernel_tuning.h>
Bounds one explicit calibration request. Zero means callback-defined.
-
class CalibrationReporter#
- #include <kernel_tuning.h>
Collects diagnostics emitted by one calibration callback.
Public Functions
-
void RecordBenchmark(uint64_t memory_bytes, uint64_t duration_ns)#
Records resources consumed by one completed benchmark case.
-
inline const std::vector<std::string> &diagnostics() const noexcept#
Returns callback diagnostics in emission order.
-
inline uint64_t benchmark_cases() const noexcept#
Returns the number of benchmark cases measured by the callback.
-
inline uint64_t peak_memory_bytes() const noexcept#
Returns the largest live input/output allocation measured by the callback.
-
inline uint64_t measured_duration_ns() const noexcept#
Returns the accumulated benchmark measurement duration.
-
void RecordBenchmark(uint64_t memory_bytes, uint64_t duration_ns)#
-
struct CpuExecutionDescriptor#
- #include <kernel_tuning.h>
Describes the stable execution properties used to resolve a processor profile.
Public Functions
-
bool operator==(const CpuExecutionDescriptor&) const = default#
-
bool operator==(const CpuExecutionDescriptor&) const = default#
-
struct KernelCalibrationBenchmark#
- #include <kernel_tuning.h>
Defines a bounded crossover search for one integer tuning parameter.
Cases with the same
problem_sizeform one benchmark group. A group wins only when the candidate reachesminimum_speedupin every case.Public Members
-
KernelTuningParameters portable_parameters#
-
std::vector<KernelCalibrationCase> cases#
-
KernelCalibrationRunner reference#
-
KernelCalibrationRunner candidate#
-
int repetitions = 5#
-
int required_consecutive_wins = 2#
-
double minimum_speedup = 0.05#
-
uint64_t default_maximum_duration_ms = 250#
-
uint64_t default_maximum_memory_bytes = uint64_t{64} << 20#
-
KernelTuningParameters portable_parameters#
-
struct KernelCalibrationCase#
- #include <kernel_tuning.h>
Describes one kernel-specific benchmark case and its expected output.
-
struct KernelCalibrationDiagnostic#
- #include <kernel_tuning.h>
Associates one calibration diagnostic with its exact key.
-
struct KernelCalibrationResourceUsage#
- #include <kernel_tuning.h>
Reports resources consumed while calibrating one exact key.
Public Members
-
KernelTuningKey key#
-
uint64_t benchmark_cases = 0#
-
uint64_t peak_memory_bytes = 0#
-
uint64_t measured_duration_ns = 0#
-
KernelTuningKey key#
-
struct KernelCalibrationRunner#
- #include <kernel_tuning.h>
Configures and runs one reference or candidate kernel instance.
-
struct KernelCalibrationSelection#
- #include <kernel_tuning.h>
Filters tuning keys. Non-empty fields combine with logical AND.
Public Functions
-
bool Matches(const KernelTuningKey &key) const#
Returns whether an exact key satisfies this selection.
-
bool Matches(const KernelTuningKey &key) const#
-
struct KernelTuningKey#
- #include <kernel_tuning.h>
Identifies one tunable kernel implementation and element type.
tuning_abimust change whenever persisted parameters for the implementation become incompatible.Public Functions
-
bool operator==(const KernelTuningKey&) const = default#
-
bool operator==(const KernelTuningKey&) const = default#
-
struct KernelTuningKeyHash#
- #include <kernel_tuning.h>
Hashes every field of a :cpp:class:
KernelTuningKey.Public Functions
-
size_t operator()(const KernelTuningKey &key) const noexcept#
-
size_t operator()(const KernelTuningKey &key) const noexcept#
-
struct KernelTuningParameters#
- #include <kernel_tuning.h>
Stores the named values associated with an exact tuning key.
Public Functions
-
template<typename T>
inline const T *TryGet(std::string_view name) const noexcept# Returns a named value with its exact scalar type, or
nullptr.This query-style accessor returns
nullptrwhen the name is absent or when it exists with another scalar type.Returns: A pointer to the requested value, or
nullptr.
-
template<typename T>
-
class KernelTuningRegistry#
- #include <kernel_tuning.h>
Registers tuning schemas and atomically publishes immutable parameter sets.
Publication validates the complete batch before making any value visible.
Public Functions
-
KernelTuningRegistry()#
-
~KernelTuningRegistry()#
-
KernelTuningRegistry(const KernelTuningRegistry&) = delete#
-
KernelTuningRegistry &operator=(const KernelTuningRegistry&) = delete#
-
void RegisterSchema(KernelTuningSchema schema)#
Registers one schema and its portable defaults.
- Throws:
std::invalid_argument – if its key is already registered.
-
void RegisterProfile(const KernelTuningKey &key, platform::CpuSelector processors, KernelTuningParameters parameters, int priority = 0)#
Registers one validated processor-specific profile.
Exact vendor/family/model selectors outrank processor-list and microarchitecture selectors, which outrank instruction-set selectors. Priority only distinguishes profiles at the same specificity.
- Throws:
std::invalid_argument – for an invalid selector, parameters that do not match the schema, or a selector ambiguous with an existing profile.
-
void RegisterProfiles(std::span<const ProcessorKernelTuningProfile> profiles)#
Registers processor-specific profiles in one immutable generation.
Validation or ambiguity in any profile leaves the registry unchanged.
-
void RegisterCalibrationFunction(const KernelTuningKey &key, KernelCalibrationFunction function)#
Registers one trusted native calibration callback.
- Throws:
std::invalid_argument – when the key has no schema, the callback is empty, or another callback is already registered for the key.
-
KernelTuningRegistrySnapshot Snapshot() const noexcept#
Returns the current immutable registry generation.
-
KernelTuningRegistryAccessCounts AccessCounts() const noexcept#
Returns monotonic cold-path access counters for diagnostics and benchmarks.
Kernel execution does not touch these counters because kernels receive their immutable typed configuration before their first run.
-
void PublishProfiles(std::span<const KernelTuningParameters> profiles, std::span<const KernelTuningKey> reset_keys = {})#
Publishes a validated batch and resets selected keys to portable defaults.
Every replacement and reset key must be registered. Validation failure leaves the current generation unchanged.
-
void PublishCalibratedProfiles(std::span<const KernelTuningParameters> profiles, const CpuExecutionDescriptor &execution, std::span<const KernelTuningKey> reset_keys = {})#
Publishes profiles scoped to one exact execution descriptor.
Other execution descriptors and universal overrides remain unchanged.
-
std::vector<KernelTuningKey> RegisteredKeys() const#
Returns the registered keys.
-
std::shared_ptr<const KernelTuningSchema> FindSchema(const KernelTuningKey &key) const#
Returns the schema for a registered key, or
nullptr.
-
KernelCalibrationFunction FindCalibrationFunction(const KernelTuningKey &key) const#
Returns the callback registered for a key, or an empty function.
-
KernelTuningRegistry()#
-
struct KernelTuningRegistryAccessCounts#
- #include <kernel_tuning.h>
Counts cold-path accesses to one kernel tuning registry.
Public Functions
-
bool operator==(const KernelTuningRegistryAccessCounts&) const = default#
-
bool operator==(const KernelTuningRegistryAccessCounts&) const = default#
-
class KernelTuningRegistrySnapshot#
- #include <kernel_tuning.h>
Holds one immutable generation of resolved kernel tuning parameters.
A snapshot remains valid after later registrations or cache loads publish a newer generation.
Public Functions
-
uint64_t generation() const noexcept#
Returns the registry generation captured by this snapshot.
-
const KernelTuningParameters *Find(const KernelTuningKey &key) const noexcept#
Finds the resolved parameters for an exact tuning key.
This lookup does not select an execution-specific calibrated profile; use :cpp:func:
Resolvewhen an execution descriptor is available.Returns: The parameters, or
nullptrwhen the key is not registered.
-
bool HasPublishedProfile(const KernelTuningKey &key, const CpuExecutionDescriptor &execution) const noexcept#
Returns whether a compatible cached, calibrated, or override profile exists.
-
const KernelTuningParameters *Resolve(const KernelTuningKey &key, const CpuExecutionDescriptor &execution) const noexcept#
Resolves the highest-precedence profile matching an execution descriptor.
Explicitly published parameters take precedence over registered processor profiles, which in turn take precedence over portable defaults.
Returns: The resolved parameters, or
nullptrwhen the key is not registered.
Private Functions
Friends
- friend class KernelTuningRegistry
-
uint64_t generation() const noexcept#
-
class KernelTuningSchema#
- #include <kernel_tuning.h>
Defines one kernel’s portable defaults and validation contract.
Construction validates the key, names, and portable defaults immediately. Every subsequently validated parameter set must contain exactly the same names and scalar types, so no tunable value can exist without a compiled portable fallback.
Public Functions
-
explicit KernelTuningSchema(KernelTuningParameters portable_defaults, KernelTuningValidationHook validation_hook = {})#
-
inline const KernelTuningKey &key() const noexcept#
Returns the exact kernel key described by this schema.
-
inline const KernelTuningParameters &portable_defaults() const noexcept#
Returns the validated, hard-coded portable parameter set.
-
void Validate(const KernelTuningParameters ¶meters) const#
Validates a complete parameter set against this schema.
- Throws:
std::invalid_argument – for a mismatched key, unknown or missing name, wrong scalar type, or a kernel-specific validation failure.
-
bool TryValidate(const KernelTuningParameters ¶meters, std::string *error_message = nullptr) const#
Validates a complete parameter set without throwing on invalid input.
Returns:
truewhen the parameters satisfy the schema. Otherwise returnsfalseand stores the validation message inerror_messagewhen it is notnullptr.
Public Static Functions
-
static KernelTuningSchema WithQueryValidation(KernelTuningParameters portable_defaults, KernelTuningQueryValidationHook validation_hook)#
Creates a schema from a non-throwing validation query hook.
Returns: A schema whose validation hook returns
std::nullopton success or a diagnostic message on invalid input.
Private Functions
-
KernelTuningSchema(KernelTuningParameters portable_defaults, QueryValidationTag, KernelTuningQueryValidationHook validation_hook)#
-
std::optional<std::string> ValidationError(const KernelTuningParameters ¶meters) const#
Private Members
-
KernelTuningParameters portable_defaults_#
-
KernelTuningQueryValidationHook validation_hook_#
-
struct QueryValidationTag#
-
explicit KernelTuningSchema(KernelTuningParameters portable_defaults, KernelTuningValidationHook validation_hook = {})#
-
struct ProcessorKernelTuningProfile#
- #include <kernel_tuning.h>
Associates validated parameters with one deployment processor selector.
Public Members
-
KernelTuningParameters parameters#
-
platform::CpuSelector processors#
-
int priority = 0#
-
KernelTuningParameters parameters#
-
using TuningValue = std::variant<int64_t, double, bool, std::string>#
-
namespace runtime
-
namespace core