Belle II Software development
test_ONNX_standalone.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8
9#include <mva/methods/ONNX.h>
10#include <framework/utilities/FileSystem.h>
11
12#include <gtest/gtest.h>
13
14using namespace Belle2::MVA::ONNX;
15
16namespace {
17 TEST(ONNXStandaloneTest, TensorIndexing)
18 {
19 auto t = Tensor<int>::make_shared({1, 2, 3, 4, 5, 6}, {2, 3});
20 EXPECT_EQ(t->at({0, 2}), 3);
21 EXPECT_EQ(t->at({1, 2}), 6);
22 t->at({1, 1}) = 42;
23 EXPECT_EQ(t->at(4), 42);
24 EXPECT_EQ(t->at({1, 1}), 42);
25 }
26 TEST(ONNXStandaloneTest, BoundsCheck)
27 {
28 auto t = Tensor<int>::make_shared({1, 3});
29 EXPECT_THROW(t->at({2, 0}), std::out_of_range);
30 }
31 TEST(ONNXStandaloneTest, SizeCheck)
32 {
33 EXPECT_THROW(Tensor<int>({1, 2, 3}, {2, 2}), std::length_error);
34 }
35 TEST(ONNXStandaloneTest, RunStandaloneModel)
36 {
37 // Testfile created with mva/examples/onnx/write_test_files.py
38 Session session(Belle2::FileSystem::findFile("mva/methods/tests/ModelForStandalone.onnx"));
39 auto input_a = Tensor<float>::make_shared({0.5309f, 0.4930f}, {1, 2});
40 auto input_b = Tensor<int64_t>::make_shared({1, 0, 1, 1, -1, 0}, {1, 2, 3});
41 auto output = Tensor<float>::make_shared({1, 2});
42 session.run({{"a", input_a}, {"b", input_b}}, {{"output", output}});
43 EXPECT_NEAR(output->at(0), -0.0614375323, 0.000000001);
44 EXPECT_NEAR(output->at(1), 0.3322576284, 0.000000001);
45 }
46}
static std::string findFile(const std::string &path, bool silent=false)
Search for given file or directory in local or central release directory, and return absolute path if...
A wrapper around Ort::Session providing model execution.
Definition ONNX.h:301
Represents an input or output tensor for an ONNX model.
Definition ONNX.h:51
static auto make_shared(std::vector< int64_t > shape)
Convenience method to create a shared pointer to a Tensor from shape.
Definition ONNX.h:145