test_case.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.

Defined as empty because onnx-light does not require explicit __declspec(dllexport) or __attribute__((visibility("default"))) annotations — visibility is controlled at the shared-library level. The macro is provided so that vendored ONNX headers that decorate their declarations with ONNX_API compile without modification.

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 onnx_backend_test

Typedefs

using OpsetId = onnx_kernels::kernel::OpsetId#
using RegisterCasesFn = void (*)(std::vector<TestCase>&)#

Function pointer registering one or more :ref:TestCase entries into the caller-supplied registry. Used by Collect*TestCases dispatch tables.

using OpRegisterMap = std::unordered_map<std::string_view, RegisterCasesFn>#

Per-category dispatch table: maps an op_type to the function that registers its test cases. Built once per Collect*TestCases as a function-local static const so lookup is amortised O(1).

Functions

void InitModel(ModelProto &model, int64_t ir_version, const std::vector<OpsetId> &opset_imports, const std::string &producer_name = "backend-test")#

Initializes model with ir_version, producer_name and the given opset_imports (default ai.onnx domain when an entry’s domain is empty). Mirrors the boilerplate that opens every manually-built backend test case model so callers don’t have to repeat it.

void AppendValueInfo(ValueInfoProto &vi, const std::string &name, int32_t elem_type, const std::vector<int64_t> &shape)#

Fills vi with a tensor-typed ValueInfo (name, elem_type and the concrete dimension values from shape). Mirrors the boilerplate every manually-built graph repeats when declaring graph inputs / value_info / outputs for which a literal shape is already known (e.g. the gallery shapes used by the shape-inference cases). For Tensor-backed metadata see the FillValueInfo(const Tensor&, ValueInfoProto&) overload in simple_tensor.h.

void AppendValueInfo(ValueInfoProto &vi, const std::string &name, int32_t elem_type, const std::vector<DimSpec> &dims)#

Overload of :ref:AppendValueInfo accepting a mix of concrete (DimSpec(int64_t)), symbolic (DimSpec("name")) and unannotated (DimSpec()) dimensions. Used by the shape-inference cases to declare symbolic batch/seq/d_model/nnz dims without repeating the TypeProto::Tensor::add_shape() + add_dim() boilerplate.

void AppendValueInfo(ValueInfoProto &vi, const std::string &name, TensorProto::DataType elem_type, const std::vector<DimSpec> &dims)#

Overload of :ref:AppendValueInfo accepting a mix of concrete (DimSpec(int64_t)), symbolic (DimSpec("name")) and unannotated (DimSpec()) dimensions. Used by the shape-inference cases to declare symbolic batch/seq/d_model/nnz dims without repeating the TypeProto::Tensor::add_shape() + add_dim() boilerplate.

TypeSpec TensorTypeSpec(int32_t elem_type)#

Returns a TypeSpec describing a Tensor of elem_type with no declared shape (used e.g. for map value types).

TypeSpec TensorTypeSpec(int32_t elem_type, std::vector<int64_t> shape)#

Returns a TypeSpec describing a Tensor of elem_type whose declared shape has the given concrete dimension values (an empty shape declares a rank-0 / scalar shape).

TypeSpec SequenceTypeSpec(TypeSpec elem)#

Returns a TypeSpec describing a Sequence whose elements have type elem.

TypeSpec MapTypeSpec(int32_t key_type, TypeSpec value)#

Returns a TypeSpec describing a Map from key_type keys to value values.

void AppendValueInfo(ValueInfoProto &vi, const std::string &name, const TypeSpec &spec)#

Fills vi with name and the type described by spec.

void AppendDataSet(TestCase &tc, std::vector<Tensor> inputs, std::vector<Tensor> outputs)#

Appends a new DataSet to tc.data_sets populated with the given inputs and outputs. Saves the DataSet ds; ds.inputs.push_back(...); ds.outputs.push_back(...); tc.data_sets.emplace_back(std::move(ds)); boilerplate that every manually-built TestCase otherwise repeats.

void Expect(const NodeProto &node, const std::vector<Tensor> &inputs, const std::vector<Tensor> &outputs, const std::string &name, const std::vector<OpsetId> &opset_imports, const std::string &producer_name, std::vector<TestCase> &registry, const std::string &tag = "", const std::vector<TypeSpec> &output_types = {})#

Builds a single-node ModelProto from node and the provided typed inputs/outputs, then appends a TestCase to registry.

Mirrors onnx_light.backend.test.case.base.expect(). Only the inputs and outputs whose name is non-empty in the node are wired into the graph.

Parameters:
  • node – Single-node template; its op_type, domain and attributes are kept.

  • inputs – Concrete input tensors corresponding to the non-empty entries of node.input.

  • outputs – Concrete expected output tensors corresponding to the non-empty entries of node.output.

  • name – Unique test name (used both for TestCase.name and the graph name).

  • opset_imports – Opset imports for the generated model. If empty the caller is responsible for ensuring a default has been applied — typically pass at least DefaultOpset(since_version).

  • producer_name – Producer name written into the model.

  • registry – Output registry (appended to).

  • tag – Optional grouping tag (defaults to the node domain for non-default operator domains).

  • output_types – Optional per-output declared type specs. When non-empty it must contain one entry per output tensor; each output value-info is then declared from its TypeSpec instead of the materialized tensor type. Used to declare Sequence / Map valued outputs whose runtime representation is a plain Tensor.

Throws:

std::invalid_argument – if inputs.size() does not equal the number of non-empty entries in node.input or if outputs.size() does not equal the number of non-empty entries in node.output, or if output_types is non-empty and its size does not equal outputs.size().

void DispatchRegisterByOpType(std::vector<TestCase> &registry, const std::string &op_type, const OpRegisterMap &entries)#

Invokes the Register*Cases functions declared in entries. When op_type is empty, every entry is invoked (order is unspecified). Otherwise, only the entry whose key matches op_type (case-sensitive) is invoked; if no entry matches, no registration occurs. Used by per-category Collect*TestCases helpers to dispatch via a hash map instead of an explicit if chain or linear scan.

std::vector<TestCase> CollectTestCases(const std::string &op_type = "")#

Collects all C++-implemented backend test node cases. Each call is deterministic and independent: the result owns its ModelProtos and Tensor data.

Parameters:

op_type – Optional operator type filter. When non-empty, only test cases whose top-level graph contains a node with this op_type are returned.

Returns:

A fresh registry of test cases (Abs, Add equal-shape, Add scalar broadcast).

std::vector<TestCase> CollectTestCasesByName(const std::string &name_regex)#

Collects C++-implemented backend test node cases whose :attr:TestCase::name matches a regular expression. Uses std::regex_search semantics (substring match by default; anchor with ^...$ to require a full match).

Parameters:

name_regex – ECMAScript regular expression matched against each test case name. An empty string matches every case (equivalent to :func:CollectTestCases).

Throws:

std::regex_error – if name_regex is not a valid ECMAScript regular expression.

Returns:

The subset of cases whose name matches name_regex, in the same registration order as :func:CollectTestCases.

struct DataSet#
#include <test_case.h>

A single (inputs, expected outputs) data set associated with a TestCase.

Public Members

std::vector<Tensor> inputs#
std::vector<Tensor> outputs#
std::vector<Map> maps#

Map-typed inputs keyed by the graph input name.

struct DimSpec#
#include <test_case.h>

Describes one tensor dimension entry used to build a ValueInfoProto.

Public Functions

DimSpec() = default#
inline DimSpec(int v)#
inline DimSpec(int64_t v)#
inline DimSpec(const char *p)#
inline DimSpec(std::string p)#

Public Members

int64_t value = -1#
std::string param#
struct TestCase#
#include <test_case.h>

A backend test case mirroring onnx_light.backend.test.case.base.TestCase. It bundles a single-node ModelProto together with the expected input/ output data sets a runtime must reproduce.

The string-typed fields (name, model_name, kind, tag) are declared const and must therefore be supplied at construction time. tag is an optional, free-form label used to group families of cases (e.g. "empty_shape", "nan_inf", "inference"); it defaults to the empty string for the ordinary node cases in the default ai.onnx domain. For test cases whose underlying node belongs to a non-default operator domain (e.g. "ai.onnx.ml", "ai.onnx.preview.training"), :func:Expect defaults the tag to the node’s domain string when the caller does not provide an explicit one.

Public Functions

inline TestCase()#
inline explicit TestCase(std::string name_, std::string model_name_ = "", std::string kind_ = "node", std::string tag_ = "", double atol_ = 1e-7, double rtol_ = 1e-3)#
inline TestCase(TestCase &&other) noexcept#
TestCase(const TestCase&) = delete#
TestCase &operator=(const TestCase&) = delete#
TestCase &operator=(TestCase&&) = delete#

Public Members

const std::string name#
const std::string model_name#
const std::string kind#
const std::string tag#
double rtol = 1e-3#
double atol = 1e-7#
ModelProto model#
std::vector<DataSet> data_sets#
struct TypeSpec#
#include <test_case.h>

Describes an ONNX value type for a graph value-info, supporting the container kinds the backend test cases need: a plain Tensor, a Sequence of an element type, or a Map from a key type to a value type. Built via the factory helpers :func:TensorTypeSpec, :func:SequenceTypeSpec and :func:MapTypeSpec and consumed by :func:AppendValueInfo / :func:Expect to emit value-infos whose declared schema type differs from the materialized Tensor representation (e.g. sequence- or map-valued outputs).

Public Types

enum class Kind#

Values:

enumerator kTensor#
enumerator kSequence#
enumerator kMap#

Public Members

Kind kind = Kind::kTensor#
int32_t elem_type = 0#

For kTensor: the tensor element type. For kMap: the key type.

bool has_shape = false#

For kTensor only: whether a (possibly empty) shape is declared.

std::vector<int64_t> shape#

For kTensor only: the concrete dimension values of the shape.

std::vector<TypeSpec> children#

Nested element type. For kSequence the single sequence element type, for kMap the single map value type; empty for kTensor.