1 //===- TensorSpecTest.cpp - test for TensorSpec ---------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Analysis/TensorSpec.h"
10 #include "llvm/Support/JSON.h"
11 #include "llvm/Support/Path.h"
12 #include "llvm/Support/SourceMgr.h"
13 #include "llvm/Testing/Support/SupportHelpers.h"
14 #include "gtest/gtest.h"
18 extern const char *TestMainArgv0
;
20 TEST(TensorSpecTest
, JSONParsing
) {
21 auto Value
= json::parse(
22 R
"({"name
": "tensor_name
",
29 std::optional
<TensorSpec
> Spec
= getTensorSpecFromJSON(Ctx
, *Value
);
31 EXPECT_EQ(*Spec
, TensorSpec::createSpec
<int32_t>("tensor_name", {1, 4}, 2));
34 TEST(TensorSpecTest
, JSONParsingInvalidTensorType
) {
35 auto Value
= json::parse(
37 {"name
": "tensor_name
",
39 "type
": "no such type
",
45 auto Spec
= getTensorSpecFromJSON(Ctx
, *Value
);
49 TEST(TensorSpecTest
, TensorSpecSizesAndTypes
) {
50 auto Spec1D
= TensorSpec::createSpec
<int16_t>("Hi1", {1});
51 auto Spec2D
= TensorSpec::createSpec
<int16_t>("Hi2", {1, 1});
52 auto Spec1DLarge
= TensorSpec::createSpec
<float>("Hi3", {10});
53 auto Spec3DLarge
= TensorSpec::createSpec
<float>("Hi3", {2, 4, 10});
54 EXPECT_TRUE(Spec1D
.isElementType
<int16_t>());
55 EXPECT_FALSE(Spec3DLarge
.isElementType
<double>());
56 EXPECT_EQ(Spec1D
.getElementCount(), 1U);
57 EXPECT_EQ(Spec2D
.getElementCount(), 1U);
58 EXPECT_EQ(Spec1DLarge
.getElementCount(), 10U);
59 EXPECT_EQ(Spec3DLarge
.getElementCount(), 80U);
60 EXPECT_EQ(Spec3DLarge
.getElementByteSize(), sizeof(float));
61 EXPECT_EQ(Spec1D
.getElementByteSize(), sizeof(int16_t));
64 TEST(TensorSpecTest
, PrintValueForDebug
) {
65 std::vector
<int32_t> Values
{1, 3};
66 EXPECT_EQ(tensorValueToString(reinterpret_cast<const char *>(Values
.data()),
67 TensorSpec::createSpec
<int32_t>("name", {2})),