1/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7    http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15
16#include "tensorflow/compiler/xla/service/cpu/external_constant_pool.h"
17#include "tensorflow/compiler/xla/layout_util.h"
18#include "tensorflow/core/platform/test.h"
19
20namespace xla {
21namespace cpu {
22namespace {
23class ExternalConstantPoolTest : public ::testing::Test {};
24
25template <typename T>
26T GetFromBuffer(const uint8* buffer, int64 index) {
27  T result;
28  std::memcpy(&result, buffer + index * sizeof(T), sizeof(T));
29  return result;
30}
31
32TEST(ExternalConstantPoolTest, Basic) {
33  ExternalConstantPool constant_pool;
34  EXPECT_EQ(constant_pool.Find("name-0"), nullptr);
35  const auto literal = Literal::CreateR2({{1, 2}, {3, 4}});
36  constant_pool.Insert("name-0", *literal, 4);
37  const uint8* constant = constant_pool.Find("name-0");
38  ASSERT_NE(constant, nullptr);
39
40  EXPECT_EQ(GetFromBuffer<int32>(constant, 0), 1);
41  EXPECT_EQ(GetFromBuffer<int32>(constant, 1), 2);
42  EXPECT_EQ(GetFromBuffer<int32>(constant, 2), 3);
43  EXPECT_EQ(GetFromBuffer<int32>(constant, 3), 4);
44
45  EXPECT_EQ(constant_pool.Find("name-1"), nullptr);
46}
47
48TEST(ExternalConstantPoolTest, RowMinorLayout) {
49  ExternalConstantPool constant_pool;
50  EXPECT_EQ(constant_pool.Find("name-0"), nullptr);
51  const auto literal = Literal::CreateR2WithLayout(
52      {{1, 2}, {3, 4}}, LayoutUtil::MakeLayout({0, 1}));
53  constant_pool.Insert("name-0", *literal, 4);
54  const uint8* constant = constant_pool.Find("name-0");
55  ASSERT_NE(constant, nullptr);
56
57  EXPECT_EQ(GetFromBuffer<int32>(constant, 0), 1);
58  EXPECT_EQ(GetFromBuffer<int32>(constant, 1), 3);
59  EXPECT_EQ(GetFromBuffer<int32>(constant, 2), 2);
60  EXPECT_EQ(GetFromBuffer<int32>(constant, 3), 4);
61}
62
63TEST(ExternalConstantPoolTest, Alignment) {
64  ExternalConstantPool constant_pool;
65  EXPECT_EQ(constant_pool.Find("name-0"), nullptr);
66
67  for (int i = 0; i < 8; i++) {
68    int64 alignment = 1 << i;
69    string name = tensorflow::strings::StrCat("name-", i);
70
71    const auto literal = Literal::CreateR2({{1, 2}, {3, 4}});
72    constant_pool.Insert(name, *literal, alignment);
73
74    const uint8* constant = constant_pool.Find(name);
75    ASSERT_NE(constant, nullptr);
76    EXPECT_EQ(reinterpret_cast<intptr_t>(constant) % alignment, 0);
77  }
78}
79
80}  // namespace
81}  // namespace cpu
82}  // namespace xla
83