test_case.h#
Defines
-
ONNX_LIGHT_BACKEND_TEST_LOCAL#
-
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 backend_test
Typedefs
-
using RegisterCasesFn = void (*)(std::vector<TestCase>&)#
Function pointer registering one or more :ref:
TestCaseentries into the caller-suppliedregistry. Used byCollect*TestCasesdispatch tables.
-
using OpRegisterMap = std::unordered_map<std::string_view, RegisterCasesFn>#
Per-category dispatch table: maps an
op_typeto the function that registers its test cases. Built once perCollect*TestCasesas a function-localstatic constso lookup is amortised O(1).
-
using RegisterCasesModeFn = void (*)(std::vector<TestCase>&, TestMode)#
Mode-aware variant of :ref:
RegisterCasesFn. In addition to the outputregistryit receives a :ref:TestModeselecting standard (TEST) or benchmark-sized (BENCHMARK) case generation.
-
using OpRegisterModeMap = std::unordered_map<std::string_view, RegisterCasesModeFn>#
Mode-aware variant of :ref:
OpRegisterMapwhose values are :ref:RegisterCasesModeFn.
Enums
-
enum class TestMode#
Selects how a
Register*Cases/Collect*helper generates its cases.TEST(the default) produces the standard correctness cases with small, fixed inputs. The generated cases are byte-for-byte unchanged from before this mode existed.BENCHMARKproduces cases whose inputs are enlarged so a single kernel evaluation processes enough elements to run long enough (~0.1 s) to be timed reliably. The exact sizes are hand-tuned per operator.
Values:
-
enumerator TEST#
-
enumerator BENCHMARK#
Functions
-
void InitModel(ModelProto &model, int64_t ir_version, const std::vector<OpsetId> &opset_imports, const std::string &producer_name = "backend-test")#
Initializes
modelwithir_version,producer_nameand the givenopset_imports(default ai.onnx domain when an entry’sdomainis 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
viwith a tensor-typed ValueInfo (name,elem_typeand the concrete dimension values fromshape). 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 theFillValueInfo(const Tensor&, ValueInfoProto&)overload insimple_tensor.h.
-
void AppendValueInfo(ValueInfoProto &vi, const std::string &name, int32_t elem_type, const std::vector<DimSpec> &dims)#
Overload of :ref:
AppendValueInfoaccepting a mix of concrete (DimSpec(int64_t)), symbolic (DimSpec("name")) and unannotated (DimSpec()) dimensions. Used by the shape-inference cases to declare symbolicbatch/seq/d_model/nnzdims without repeating theTypeProto::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:
AppendValueInfoaccepting a mix of concrete (DimSpec(int64_t)), symbolic (DimSpec("name")) and unannotated (DimSpec()) dimensions. Used by the shape-inference cases to declare symbolicbatch/seq/d_model/nnzdims without repeating theTypeProto::Tensor::add_shape()+add_dim()boilerplate.
-
TypeSpec TensorTypeSpec(int32_t elem_type)#
Returns a
TypeSpecdescribing aTensorofelem_typewith no declared shape (used e.g. for map value types).
-
TypeSpec TensorTypeSpec(int32_t elem_type, std::vector<int64_t> shape)#
Returns a
TypeSpecdescribing aTensorofelem_typewhose declared shape has the given concrete dimension values (an emptyshapedeclares a rank-0 / scalar shape).
-
TypeSpec SequenceTypeSpec(TypeSpec elem)#
Returns a
TypeSpecdescribing aSequencewhose elements have typeelem.
-
TypeSpec MapTypeSpec(int32_t key_type, TypeSpec value)#
Returns a
TypeSpecdescribing aMapfromkey_typekeys tovaluevalues.
-
void AppendValueInfo(ValueInfoProto &vi, const std::string &name, const TypeSpec &spec)#
Fills
viwithnameand the type described byspec.
-
void AppendDataSet(TestCase &tc, Tensors inputs, Tensors outputs)#
Appends a new
DataSettotc.data_sets()populated with the giveninputsandoutputs. Saves theDataSet 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 DispatchRegisterByOpType(std::vector<TestCase> ®istry, const std::string &op_type, const OpRegisterMap &entries)#
Invokes the
Register*Casesfunctions declared inentries. Whenop_typeis empty, every entry is invoked (order is unspecified). Otherwise, only the entry whose key matchesop_type(case-sensitive) is invoked; if no entry matches, no registration occurs. Used by per-categoryCollect*TestCaseshelpers to dispatch via a hash map instead of an explicitifchain or linear scan.
-
void DispatchRegisterByOpType(std::vector<TestCase> ®istry, const std::string &op_type, const OpRegisterModeMap &entries, TestMode mode)#
Mode-aware overload of :ref:
DispatchRegisterByOpType. Forwardsmodeto each invoked :ref:RegisterCasesModeFnso a category can generate either the standard (TestMode::TEST) or benchmark-sized (TestMode::BENCHMARK) cases.
-
std::vector<TestCase> CollectTestCases(const std::string &op_type = "", bool include_big = false, TestMode mode = TestMode::TEST)#
Collects all C++-implemented backend test node cases. Each call is deterministic and independent: the result owns its
ModelProtos andTensordata.Iterates the collector functions registered via :func:
RegisterTestCasesCollector(typically everyCollect*TestCasescategory inlib_onnx_backend_test).- Parameters:
op_type – Optional operator type filter. When non-empty, only test cases whose top-level graph contains a node with this
op_typeare returned.include_big – When
false(the default), test cases whose name contains the substring"_big_"are excluded from the result. Passtrueto include them; the big models are intentionally opt-in because they carry large weight tensors that make exhaustive test loops slow.mode – When :cpp:enumerator:
TestMode::BENCHMARK, categories that support it emit benchmark-sized cases (large inputs) instead of the standard correctness cases. Defaults to :cpp:enumerator:TestMode::TEST.
- Returns:
A fresh registry of test cases (Abs, Add equal-shape, Add scalar broadcast).
-
std::vector<TestCase> CollectTestCasesByName(const std::string &name_regex, bool include_big = false, TestMode mode = TestMode::TEST)#
Collects C++-implemented backend test node cases whose :attr:
TestCase::namematches a regular expression. Usesstd::regex_searchsemantics (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).include_big – When
false(the default), test cases whose name contains"_big_"are excluded before the regex filter is applied. Passtrueto include them.mode – Forwarded to :func:
CollectTestCases; selects standard or benchmark-sized case generation.
- Throws:
std::regex_error – if
name_regexis not a valid ECMAScript regular expression.- Returns:
The subset of cases whose
namematchesname_regex, in the same registration order as :func:CollectTestCases.
-
std::vector<TestCase> GetTestCaseByName(const std::string &name, bool include_big = false, TestMode mode = TestMode::TEST)#
Returns the single C++-implemented backend test case whose :attr:
TestCase::nameequalsnameexactly, or an empty optional if no such case exists.Unlike :func:
CollectTestCasesByNamethis performs a plain string comparison instead of compiling astd::regexand avoids copying all non-matching cases. It is therefore the most efficient way to retrieve a single known case by name.- Parameters:
name – The exact test case name to look up (e.g.
"test_cc_abs").include_big – When
false(the default), a case whose name contains"_big_"is excluded even if it matchesname.mode – Forwarded to :func:
CollectTestCases; selects standard or benchmark-sized case generation.
- Returns:
A vector containing at most one :class:
TestCase. An empty vector signals that no case with the requested name was found.
-
struct BuiltCase#
- #include <test_case.h>
Product of a lazily-built :ref:
TestCase: the single-nodeModelPrototogether with its input/output data sets. ATestCasestores a builder returning this so that constructing the (potentially large) model and running the kernel that computes the expected outputs is deferred until a consumer actually needs them. Collecting a large family of cases (in particular theBENCHMARKcases whose inputs contain millions of elements) therefore stays cheap.
-
struct DataSet#
- #include <test_case.h>
A single (inputs, expected outputs) data set associated with a TestCase.
-
struct DimSpec#
- #include <test_case.h>
Describes one tensor dimension entry used to build a ValueInfoProto.
DimSpec(int64_t v)(v >= 0): concretedim_value.DimSpec("name")/DimSpec(std::string): symbolicdim_param.DimSpec(): unannotated dim (neitherdim_valuenordim_param).
-
struct TestCase#
- #include <test_case.h>
A backend test case mirroring
onnx_light.backend.test.case.base.TestCase. It bundles a single-nodeModelPrototogether with the expected input/ output data sets a runtime must reproduce.The model is not stored directly. Every case built through :func:
Expect(both the correctnessTESTcases and theBENCHMARKcases) is lazy: it carries abuildclosure that produces the :ref:BuiltCase— theModelProtoand itsdata_sets— on first access via :func:model/ :func:data_sets/ :func:Materialize. A handful of manually-assembled cases (control-flow, sequence, …) are instead eager: they populate the model cache with :func:emplace_model/ :func:set_modeland append their data sets directly, sobuildis left unset and :func:Materializeis a no-op. Every case recordsdeclared_input_element_counts/declared_output_element_countsso its sizing can be checked without running the (potentially expensive) builder.The string-typed fields (
name,model_name,kind,tag) are declaredconstand must therefore be supplied at construction time.tagis 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 defaultai.onnxdomain. For test cases whose underlying node belongs to a non-default operator domain (e.g."ai.onnx.ml","ai.onnx.preview.training"), :func:Expectdefaults 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 ModelProto &emplace_model()#
Creates (if needed) and returns the mutable model cache. Used by eager case builders that populate the
ModelProtoin place. Clears any previously-built cache.
-
inline void set_model(ModelProto model)#
Stores an already-built model into the cache.
-
inline bool materialized() const#
Returns whether the case has already been materialized (its model cache exists). Introspection helper that does not trigger materialization.
-
inline bool is_lazy() const#
Returns whether the case is lazy (carries a
buildclosure). Does not trigger materialization.
-
inline void Materialize()#
Runs the
buildclosure once (if the case is lazy and not yet built), materializing the model cache anddata_sets. No-op for eager cases and for already-materialized cases.
-
inline ModelProto &model()#
Lazily builds (once) and returns the model.
-
inline const ModelProto &model() const#
Const overload. Materializes the case (model and data sets) on first access via the same builder, so
data_setsis consistent in const contexts as well.
Public Members
-
double rtol = 1e-3#
-
double atol = 1e-7#
-
std::function<BuiltCase()> build#
Optional builder producing the model + data sets on demand. When set the case is lazy:
data_setsstarts empty and the model is unbuilt until :func:Materialize/ :func:model/ :func:data_setsruns the builder once.
Private Functions
-
inline void EnsureMaterialized() const#
-
inline TestCase()#
-
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, aSequenceof an element type, or aMapfrom a key type to a value type. Built via the factory helpers :func:TensorTypeSpec, :func:SequenceTypeSpecand :func:MapTypeSpecand consumed by :func:AppendValueInfo/ :func:Expectto emit value-infos whose declared schema type differs from the materializedTensorrepresentation (e.g. sequence- or map-valued outputs).
-
using RegisterCasesFn = void (*)(std::vector<TestCase>&)#
-
namespace backend_test
-
namespace core