xref: /llvm-project/llvm/unittests/Analysis/MLModelRunnerTest.cpp (revision 059e03476cbbb7349b75fa9cc26bc9da0d491529)
1 //===- MLModelRunnerTest.cpp - test for MLModelRunner ---------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/Analysis/MLModelRunner.h"
10 #include "llvm/Analysis/NoInferenceModelRunner.h"
11 #include "gtest/gtest.h"
12 
13 using namespace llvm;
14 
15 TEST(NoInferenceModelRunner, AccessTensors) {
16   const std::vector<TensorSpec> Inputs{
17       TensorSpec::createSpec<int64_t>("F1", {1}),
18       TensorSpec::createSpec<int64_t>("F2", {10}),
19       TensorSpec::createSpec<float>("F2", {5}),
20   };
21   LLVMContext Ctx;
22   NoInferenceModelRunner NIMR(Ctx, Inputs);
23   NIMR.getTensor<int64_t>(0)[0] = 1;
24   std::memcpy(NIMR.getTensor<int64_t>(1),
25               std::vector<int64_t>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}.data(),
26               10 * sizeof(int64_t));
27   std::memcpy(NIMR.getTensor<float>(2),
28               std::vector<float>{0.1, 0.2, 0.3, 0.4, 0.5}.data(),
29               5 * sizeof(float));
30   ASSERT_EQ(NIMR.getTensor<int64_t>(0)[0], 1);
31   ASSERT_EQ(NIMR.getTensor<int64_t>(1)[8], 9);
32   ASSERT_EQ(NIMR.getTensor<float>(2)[1], 0.2f);
33 }