Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ExaTrkXTensorConversionTests.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file ExaTrkXTensorConversionTests.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2022 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
9 #include <boost/test/unit_test.hpp>
10 
12 
13 #include <iostream>
14 
15 #include <torch/torch.h>
16 
17 BOOST_AUTO_TEST_CASE(test_vector_tensor_conversion_int_2cols) {
18  std::vector<int64_t> start_vec = {
19  // clang-format off
20  0, 1,
21  1, 2,
22  2, 3,
23  3, 4
24  // clang-format on
25  };
26 
27  auto tensor = Acts::detail::vectorToTensor2D(start_vec, 2).clone();
28 
29  BOOST_CHECK(tensor.options().dtype() == torch::kInt64);
30  BOOST_CHECK(tensor.sizes().size() == 2);
31  BOOST_CHECK(tensor.size(0) == 4);
32  BOOST_CHECK(tensor.size(1) == 2);
33 
34  BOOST_CHECK(tensor[0][0].item<int64_t>() == 0);
35  BOOST_CHECK(tensor[0][1].item<int64_t>() == 1);
36 
37  BOOST_CHECK(tensor[1][0].item<int64_t>() == 1);
38  BOOST_CHECK(tensor[1][1].item<int64_t>() == 2);
39 
40  BOOST_CHECK(tensor[2][0].item<int64_t>() == 2);
41  BOOST_CHECK(tensor[2][1].item<int64_t>() == 3);
42 
43  BOOST_CHECK(tensor[3][0].item<int64_t>() == 3);
44  BOOST_CHECK(tensor[3][1].item<int64_t>() == 4);
45 
46  auto test_vec = Acts::detail::tensor2DToVector<int64_t>(tensor);
47 
48  BOOST_CHECK(test_vec == start_vec);
49 }
50 
51 BOOST_AUTO_TEST_CASE(test_vector_tensor_conversion_float_3cols) {
52  std::vector<float> start_vec = {
53  // clang-format off
54  0.f, 0.f, 0.f,
55  1.f, 1.f, 1.f,
56  2.f, 2.f, 2.f,
57  3.f, 3.f, 3.f
58  // clang-format on
59  };
60 
61  auto tensor = Acts::detail::vectorToTensor2D(start_vec, 3).clone();
62 
63  BOOST_CHECK(tensor.options().dtype() == torch::kFloat32);
64  BOOST_CHECK(tensor.sizes().size() == 2);
65  BOOST_CHECK(tensor.size(0) == 4);
66  BOOST_CHECK(tensor.size(1) == 3);
67 
68  for (auto i : {0, 1, 2, 3}) {
69  BOOST_CHECK(tensor[i][0].item<int64_t>() == static_cast<float>(i));
70  BOOST_CHECK(tensor[i][1].item<int64_t>() == static_cast<float>(i));
71  BOOST_CHECK(tensor[i][2].item<int64_t>() == static_cast<float>(i));
72  }
73 
74  auto test_vec = Acts::detail::tensor2DToVector<float>(tensor);
75 
76  BOOST_CHECK(test_vec == start_vec);
77 }
78 
79 BOOST_AUTO_TEST_CASE(test_slicing) {
80  std::vector<float> start_vec = {
81  // clang-format off
82  0.f, 4.f, 0.f,
83  1.f, 5.f, 1.f,
84  2.f, 6.f, 2.f,
85  3.f, 7.f, 3.f
86  // clang-format on
87  };
88 
89  auto tensor = Acts::detail::vectorToTensor2D(start_vec, 3).clone();
90 
91  using namespace torch::indexing;
92  tensor = tensor.index({Slice{}, Slice{0, None, 2}});
93 
94  BOOST_CHECK(tensor.size(0) == 4);
95  BOOST_CHECK(tensor.size(1) == 2);
96 
97  const std::vector<float> ref_vec = {
98  // clang-format off
99  0.f, 0.f,
100  1.f, 1.f,
101  2.f, 2.f,
102  3.f, 3.f,
103  // clang-format on
104  };
105 
106  const auto test_vec = Acts::detail::tensor2DToVector<float>(tensor);
107 
108  BOOST_CHECK(test_vec == ref_vec);
109 }