cinn_cache_key_test.cc 7.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

15 16
#include "paddle/fluid/framework/paddle2cinn/cinn_cache_key.h"

17 18 19 20 21 22 23
#include <map>
#include <unordered_set>

#include "gtest/gtest.h"
#include "paddle/fluid/framework/ir/graph.h"
#include "paddle/fluid/framework/lod_tensor.h"
#include "paddle/fluid/framework/program_desc.h"
24
#include "paddle/phi/core/ddim.h"
25 26 27 28 29

namespace paddle {
namespace framework {
namespace paddle2cinn {

J
jiangcheng 已提交
30 31
TEST(CinnCacheKeyTest, TestAsUnorderedKeyByStructure) {
  std::unordered_set<CinnCacheKeyByStructure, CinnCacheKey::Hash> test_set;
32 33 34 35 36 37 38 39 40 41

  ProgramDesc empty_program;
  ir::Graph empty_graph(empty_program);

  ProgramDesc program;
  auto *global_block = program.MutableBlock(0);
  auto *x = global_block->Var("X");
  x->SetType(proto::VarType::LOD_TENSOR);
  ir::Graph graph(program);

42
  DataType fp32 = DataType::FLOAT32;
43
  phi::DenseTensor tensor;
44
  tensor.set_type(fp32);
45
  tensor.Resize({1, 2, 3});
46 47
  const phi::DenseTensor *tensor_pointer = &tensor;
  std::map<std::string, const phi::DenseTensor *> feed_tensors = {
48 49
      {"X", tensor_pointer}};

50
  DDim ddim = phi::make_ddim({1, 2, 3});
51
  std::map<std::string, DDim> feed_shapes = {{"X", ddim}};
52
  std::map<std::string, DataType> feed_dtypes = {{"X", fp32}};
53

J
jiangcheng 已提交
54
  CinnCacheKeyByStructure cache_key0(empty_graph, feed_tensors, "x86");
55 56
  CinnCacheKeyByStructure cache_key1(
      empty_graph, feed_shapes, feed_dtypes, "x86");
57 58
  EXPECT_EQ(cache_key0, cache_key1);

59 60
  CinnCacheKeyByStructure cache_key2(graph, feed_shapes, feed_dtypes, "x86");
  CinnCacheKeyByStructure cache_key3(graph, feed_shapes, feed_dtypes, "nvgpu");
J
jiangcheng 已提交
61
  CinnCacheKeyByStructure cache_key4(graph, feed_tensors, "nvgpu");
62
  EXPECT_NE(cache_key2, cache_key3);
63 64
  EXPECT_EQ(cache_key3, cache_key4);

J
jiangcheng 已提交
65
  CinnCacheKeyByStructure cache_key5(
66
      empty_graph, std::map<std::string, const phi::DenseTensor *>(), "unk");
67 68 69 70
  CinnCacheKeyByStructure cache_key6(empty_graph,
                                     std::map<std::string, DDim>(),
                                     std::map<std::string, DataType>(),
                                     "unk");
71 72 73 74 75 76 77 78 79 80 81
  EXPECT_EQ(cache_key5, cache_key6);

  EXPECT_NE(cache_key1, cache_key3);
  EXPECT_NE(cache_key4, cache_key2);

  EXPECT_NE(cache_key3, cache_key5);
  EXPECT_NE(cache_key6, cache_key4);

  EXPECT_NE(cache_key5, cache_key1);
  EXPECT_NE(cache_key2, cache_key6);

82
  test_set.insert(cache_key0);
83 84 85 86 87 88 89
  test_set.insert(cache_key1);
  test_set.insert(cache_key3);
  test_set.insert(cache_key4);
  test_set.insert(cache_key5);
  test_set.insert(cache_key6);
  EXPECT_EQ(test_set.size(), 3U);

90
  auto iter = test_set.find(cache_key0);
91 92 93
  EXPECT_NE(iter, test_set.end());
  test_set.erase(iter);
  EXPECT_EQ(test_set.size(), 2U);
94
  EXPECT_EQ(test_set.find(cache_key1), test_set.end());
95 96 97 98 99 100 101 102 103 104 105 106 107 108

  iter = test_set.find(cache_key3);
  EXPECT_NE(iter, test_set.end());
  test_set.erase(iter);
  EXPECT_EQ(test_set.size(), 1U);
  EXPECT_EQ(test_set.find(cache_key4), test_set.end());

  iter = test_set.find(cache_key5);
  EXPECT_NE(iter, test_set.end());
  test_set.erase(iter);
  EXPECT_EQ(test_set.size(), 0U);
  EXPECT_EQ(test_set.find(cache_key6), test_set.end());
}

J
jiangcheng 已提交
109 110 111 112 113 114 115 116 117 118 119 120
TEST(CinnCacheKeyTest, TestAsUnorderedKeyByAddress) {
  std::unordered_set<CinnCacheKeyByAddress, CinnCacheKey::Hash> test_set;

  ProgramDesc empty_program;
  ir::Graph empty_graph(empty_program);

  ProgramDesc program;
  auto *global_block = program.MutableBlock(0);
  auto *x = global_block->Var("X");
  x->SetType(proto::VarType::LOD_TENSOR);
  ir::Graph graph(program);

121
  DataType fp32 = DataType::FLOAT32;
122
  phi::DenseTensor tensor;
J
jiangcheng 已提交
123
  tensor.Resize({1, 2, 3});
124 125
  const phi::DenseTensor *tensor_pointer = &tensor;
  std::map<std::string, const phi::DenseTensor *> feed_tensors = {
J
jiangcheng 已提交
126 127
      {"X", tensor_pointer}};

128
  DDim ddim = phi::make_ddim({1, 2, 3});
J
jiangcheng 已提交
129
  std::map<std::string, DDim> feed_shapes = {{"X", ddim}};
130 131
  std::map<std::string, DataType> feed_dtypes = {{"X", fp32}};
  std::map<std::string, DataType> new_dtypes = {{"X", DataType::FLOAT64}};
J
jiangcheng 已提交
132 133

  CinnCacheKeyByAddress cache_key0(empty_graph, feed_tensors, "x86");
134 135
  CinnCacheKeyByAddress cache_key1(
      empty_graph, feed_shapes, feed_dtypes, "x86");
J
jiangcheng 已提交
136 137
  EXPECT_EQ(cache_key0, cache_key1);

138 139 140 141 142
  CinnCacheKeyByAddress cache_key7(empty_graph, feed_shapes, new_dtypes, "x86");
  EXPECT_NE(cache_key1, cache_key7);

  CinnCacheKeyByAddress cache_key2(graph, feed_shapes, feed_dtypes, "x86");
  CinnCacheKeyByAddress cache_key3(graph, feed_shapes, feed_dtypes, "nvgpu");
J
jiangcheng 已提交
143 144 145 146 147
  CinnCacheKeyByAddress cache_key4(graph, feed_tensors, "nvgpu");
  EXPECT_NE(cache_key2, cache_key3);
  EXPECT_EQ(cache_key3, cache_key4);

  CinnCacheKeyByAddress cache_key5(
148
      empty_graph, std::map<std::string, const phi::DenseTensor *>(), "unk");
149 150 151 152
  CinnCacheKeyByAddress cache_key6(empty_graph,
                                   std::map<std::string, DDim>(),
                                   std::map<std::string, DataType>(),
                                   "unk");
J
jiangcheng 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
  EXPECT_EQ(cache_key5, cache_key6);

  EXPECT_NE(cache_key1, cache_key3);
  EXPECT_NE(cache_key4, cache_key2);

  EXPECT_NE(cache_key3, cache_key5);
  EXPECT_NE(cache_key6, cache_key4);

  EXPECT_NE(cache_key5, cache_key1);
  EXPECT_NE(cache_key2, cache_key6);

  test_set.insert(cache_key0);
  test_set.insert(cache_key1);
  test_set.insert(cache_key3);
  test_set.insert(cache_key4);
  test_set.insert(cache_key5);
  test_set.insert(cache_key6);
  EXPECT_EQ(test_set.size(), 3U);

  auto iter = test_set.find(cache_key0);
  EXPECT_NE(iter, test_set.end());
  test_set.erase(iter);
  EXPECT_EQ(test_set.size(), 2U);
  EXPECT_EQ(test_set.find(cache_key1), test_set.end());

  iter = test_set.find(cache_key3);
  EXPECT_NE(iter, test_set.end());
  test_set.erase(iter);
  EXPECT_EQ(test_set.size(), 1U);
  EXPECT_EQ(test_set.find(cache_key4), test_set.end());

  iter = test_set.find(cache_key5);
  EXPECT_NE(iter, test_set.end());
  test_set.erase(iter);
  EXPECT_EQ(test_set.size(), 0U);
  EXPECT_EQ(test_set.find(cache_key6), test_set.end());
}

TEST(CinnCacheKeyTest, TestSameGraph) {
  ProgramDesc program1;
  auto *global_block1 = program1.MutableBlock(0);
  auto *x1 = global_block1->Var("X");
  x1->SetType(proto::VarType::LOD_TENSOR);
  ir::Graph graph1(program1);

  ProgramDesc program2;
  auto *global_block2 = program2.MutableBlock(0);
  auto *x2 = global_block2->Var("X");
  x2->SetType(proto::VarType::LOD_TENSOR);
  ir::Graph graph2(program2);

204
  DataType fp32 = DataType::FLOAT32;
205
  phi::DenseTensor tensor;
206
  tensor.set_type(fp32);
J
jiangcheng 已提交
207
  tensor.Resize({1, 2, 3});
208 209
  const phi::DenseTensor *tensor_pointer = &tensor;
  std::map<std::string, const phi::DenseTensor *> feed_tensors = {
J
jiangcheng 已提交
210 211 212 213 214 215 216 217 218 219 220
      {"X", tensor_pointer}};

  CinnCacheKeyByAddress cache_key_by_address1(graph1, feed_tensors, "x86");
  CinnCacheKeyByAddress cache_key_by_address2(graph2, feed_tensors, "x86");
  EXPECT_NE(cache_key_by_address1, cache_key_by_address2);

  CinnCacheKeyByStructure cache_key_by_struct1(graph1, feed_tensors, "x86");
  CinnCacheKeyByStructure cache_key_by_struct2(graph2, feed_tensors, "x86");
  EXPECT_EQ(cache_key_by_struct1, cache_key_by_struct2);
}

221 222 223
}  // namespace paddle2cinn
}  // namespace framework
}  // namespace paddle