naive_buffer_wrapper_test.cc 9.8 KB
Newer Older
Y
Yan Chunwei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Copyright (c) 2019 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.

#include <gtest/gtest.h>
#include "lite/model_parser/naive_buffer/block_desc.h"
Y
Yan Chunwei 已提交
17
#include "lite/model_parser/naive_buffer/combined_params_desc.h"
Y
Yan Chunwei 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
#include "lite/model_parser/naive_buffer/op_desc.h"
#include "lite/model_parser/naive_buffer/param_desc.h"
#include "lite/model_parser/naive_buffer/program_desc.h"
#include "lite/model_parser/naive_buffer/var_desc.h"

namespace paddle {
namespace lite {
namespace naive_buffer {

TEST(NaiveBufferWrapper, OpDesc) {
  BinaryTable table0;
  proto::OpDesc pt_desc0(&table0);
  OpDesc nb_desc0(&pt_desc0);

  // Set OpDesc
  nb_desc0.SetType("mul");
  nb_desc0.SetInput("X", {"a"});
  nb_desc0.SetInput("Y", {"b"});
  nb_desc0.SetOutput("Out", {"c"});
  nb_desc0.SetAttr<int32_t>("x_num_col_dims", 0);
  nb_desc0.SetAttr<int32_t>("y_num_col_dims", 1);

  // Save model
  pt_desc0.Save();
  table0.SaveToFile("1.bf");

  // Load model
  BinaryTable table1;
  table1.LoadFromFile("1.bf");
  proto::OpDesc pt_desc1(&table1);
  pt_desc1.Load();
  OpDesc nb_desc1(&pt_desc1);

  ASSERT_EQ(nb_desc1.Type(), "mul");
  auto x = nb_desc1.Input("X");
  ASSERT_EQ(x.size(), 1);
  ASSERT_EQ(x[0], "a");
  auto y = nb_desc1.Input("Y");
  ASSERT_EQ(y.size(), 1);
  ASSERT_EQ(y[0], "b");
  auto out = nb_desc1.Output("Out");
  ASSERT_EQ(out.size(), 1);
  ASSERT_EQ(out[0], "c");
  ASSERT_TRUE(nb_desc1.HasAttr("x_num_col_dims"));
  ASSERT_EQ(nb_desc1.GetAttr<int32_t>("x_num_col_dims"), 0);
  ASSERT_EQ(nb_desc1.GetAttrType("x_num_col_dims"), OpDescAPI::AttrType::INT);
  ASSERT_TRUE(nb_desc1.HasAttr("y_num_col_dims"));
  ASSERT_EQ(nb_desc1.GetAttr<int32_t>("y_num_col_dims"), 1);
  ASSERT_EQ(nb_desc1.GetAttrType("y_num_col_dims"), OpDescAPI::AttrType::INT);
}

TEST(NaiveBufferWrapper, VarDesc) {
  BinaryTable table0;
  proto::VarDesc pt_desc0(&table0);
  VarDesc nb_desc0(&pt_desc0);

  // Set VarDesc
  nb_desc0.SetName("a");
  nb_desc0.SetPersistable(true);
  nb_desc0.SetType(VarDescAPI::VarDataType::LOD_TENSOR);

  // Save model
  pt_desc0.Save();
  table0.SaveToFile("2.bf");

  // Load model
  BinaryTable table1;
  table1.LoadFromFile("2.bf");
  proto::VarDesc pt_desc1(&table1);
  pt_desc1.Load();
  VarDesc nb_desc1(&pt_desc1);

  ASSERT_EQ(nb_desc1.Name(), "a");
  ASSERT_EQ(nb_desc1.GetType(), VarDescAPI::VarDataType::LOD_TENSOR);
  ASSERT_TRUE(nb_desc1.Persistable());
}

TEST(NaiveBufferWrapper, ParamDesc) {
  BinaryTable table0;
  proto::ParamDesc pt_desc0(&table0);
  ParamDesc nb_desc0(&pt_desc0);

  // Set ParamDesc
Y
Yan Chunwei 已提交
101
  nb_desc0.SetName("fc_w.0");
Y
Yan Chunwei 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
  nb_desc0.SetModelVersion(0);
  nb_desc0.SetTensorVersion(1);
  std::vector<std::vector<uint64_t>> lod({{1, 2, 3}, {4, 5}});
  nb_desc0.SetLoDLevel(2);
  nb_desc0.SetLoD(lod);
  std::vector<int64_t> dim({1, 2, 5});
  nb_desc0.SetDim(dim);
  nb_desc0.SetDataType(VarDescAPI::VarDataType::FP32);
  std::vector<float> data;
  for (int i = 0; i < 10; ++i) {
    data.push_back(i / 10.0);
  }
  nb_desc0.SetData(data);

  // Save model
  pt_desc0.Save();
  table0.SaveToFile("3.bf");

  // Load model
  BinaryTable table1;
  table1.LoadFromFile("3.bf");
  proto::ParamDesc pt_desc1(&table1);
  pt_desc1.Load();
  ParamDesc nb_desc1(&pt_desc1);

Y
Yan Chunwei 已提交
127
  ASSERT_EQ(nb_desc1.Name(), "fc_w.0");
Y
Yan Chunwei 已提交
128 129 130 131 132 133 134 135 136 137 138 139
  ASSERT_EQ(nb_desc1.ModelVersion(), 0);
  ASSERT_EQ(nb_desc1.TensorVersion(), 1);
  ASSERT_EQ(nb_desc1.LoDLevel(), 2);
  ASSERT_EQ(nb_desc1.LoD(), lod);
  ASSERT_EQ(nb_desc1.Dim(), dim);
  auto data1 = nb_desc1.Data<float>();
  ASSERT_EQ(data1.size(), data.size());
  for (size_t i = 0; i < data1.size(); ++i) {
    EXPECT_NEAR(data1[i], data[i], 1e-6);
  }
}

Y
Yan Chunwei 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152 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 204 205 206 207 208 209 210 211 212 213 214 215 216 217
TEST(NaiveBufferWrapper, CombinedParamsDesc) {
  BinaryTable table0;
  proto::CombinedParamsDesc pt_desc0(&table0);
  CombinedParamsDesc nb_desc0(&pt_desc0);

  // Set ParamDesc
  ParamDesc param_desc0_0(nb_desc0.AddParam());
  param_desc0_0.SetName("fc_w.0");
  param_desc0_0.SetModelVersion(0);
  param_desc0_0.SetTensorVersion(1);
  std::vector<std::vector<uint64_t>> param_desc0_0_lod({{1, 2, 3}, {4, 5}});
  param_desc0_0.SetLoDLevel(2);
  param_desc0_0.SetLoD(param_desc0_0_lod);
  std::vector<int64_t> param_desc0_0_dim({1, 2, 5});
  param_desc0_0.SetDim(param_desc0_0_dim);
  param_desc0_0.SetDataType(VarDescAPI::VarDataType::FP32);
  std::vector<float> param_desc0_0_data;
  for (int i = 0; i < 10; ++i) {
    param_desc0_0_data.push_back(i / 10.0);
  }
  param_desc0_0.SetData(param_desc0_0_data);

  ParamDesc param_desc0_1(nb_desc0.AddParam());
  param_desc0_1.SetName("fc_b.0");
  param_desc0_1.SetModelVersion(0);
  param_desc0_1.SetTensorVersion(1);
  std::vector<std::vector<uint64_t>> param_desc0_1_lod({{1}, {2, 3}, {4, 5}});
  param_desc0_1.SetLoDLevel(3);
  param_desc0_1.SetLoD(param_desc0_1_lod);
  std::vector<int64_t> param_desc0_1_dim({1, 2, 2, 5});
  param_desc0_1.SetDim(param_desc0_1_dim);
  param_desc0_1.SetDataType(VarDescAPI::VarDataType::FP32);
  std::vector<float> param_desc0_1_data;
  for (int i = 0; i < 20; ++i) {
    param_desc0_1_data.push_back((i - 10) / 10.0);
  }
  param_desc0_1.SetData(param_desc0_1_data);

  // Save model
  pt_desc0.Save();
  table0.SaveToFile("4.bf");

  // Load model
  BinaryTable table1;
  table1.LoadFromFile("4.bf");
  proto::CombinedParamsDesc pt_desc1(&table1);
  pt_desc1.Load();
  CombinedParamsDesc nb_desc1(&pt_desc1);

  ASSERT_EQ(nb_desc1.ParamsSize(), 2);

  ParamDesc param_desc1_0(nb_desc1.GetParam(0));
  ASSERT_EQ(param_desc1_0.Name(), "fc_w.0");
  ASSERT_EQ(param_desc1_0.ModelVersion(), 0);
  ASSERT_EQ(param_desc1_0.TensorVersion(), 1);
  ASSERT_EQ(param_desc1_0.LoDLevel(), 2);
  ASSERT_EQ(param_desc1_0.LoD(), param_desc0_0_lod);
  ASSERT_EQ(param_desc1_0.Dim(), param_desc0_0_dim);
  auto param_desc1_0_data = param_desc1_0.Data<float>();
  ASSERT_EQ(param_desc1_0_data.size(), param_desc0_0_data.size());
  for (size_t i = 0; i < param_desc1_0_data.size(); ++i) {
    EXPECT_NEAR(param_desc1_0_data[i], param_desc0_0_data[i], 1e-6);
  }

  ParamDesc param_desc1_1(nb_desc1.GetParam(1));
  ASSERT_EQ(param_desc1_1.Name(), "fc_b.0");
  ASSERT_EQ(param_desc1_1.ModelVersion(), 0);
  ASSERT_EQ(param_desc1_1.TensorVersion(), 1);
  ASSERT_EQ(param_desc1_1.LoDLevel(), 3);
  ASSERT_EQ(param_desc1_1.LoD(), param_desc0_1_lod);
  ASSERT_EQ(param_desc1_1.Dim(), param_desc0_1_dim);
  auto param_desc1_1_data = param_desc1_1.Data<float>();
  ASSERT_EQ(param_desc1_1_data.size(), param_desc0_1_data.size());
  for (size_t i = 0; i < param_desc1_1_data.size(); ++i) {
    EXPECT_NEAR(param_desc1_1_data[i], param_desc0_1_data[i], 1e-6);
  }
}

Y
Yan Chunwei 已提交
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
TEST(NaiveBufferWrapper, BlockDesc) {
  BinaryTable table0;
  proto::BlockDesc pt_desc0(&table0);
  BlockDesc nb_desc0(&pt_desc0);

  // Set BlockDesc
  nb_desc0.SetIdx(1);
  nb_desc0.SetParentIdx(2);
  nb_desc0.SetForwardBlockIdx(3);
  VarDesc var0_0(nb_desc0.AddVar<proto::VarDesc>());
  var0_0.SetName("a");
  var0_0.SetPersistable(true);
  var0_0.SetType(VarDescAPI::VarDataType::LOD_TENSOR);
  VarDesc var0_1(nb_desc0.AddVar<proto::VarDesc>());
  var0_1.SetName("b");
  var0_1.SetPersistable(false);
  var0_1.SetType(VarDescAPI::VarDataType::READER);
  OpDesc op0_0(nb_desc0.AddOp<proto::OpDesc>());
  op0_0.SetType("mul");
  op0_0.SetInput("X", {"a"});
  op0_0.SetInput("Y", {"b"});
  op0_0.SetOutput("Out", {"c"});
  op0_0.SetAttr<int32_t>("x_num_col_dims", 0);
  op0_0.SetAttr<int32_t>("y_num_col_dims", 1);

  // Save model
  pt_desc0.Save();
Y
Yan Chunwei 已提交
245
  table0.SaveToFile("5.bf");
Y
Yan Chunwei 已提交
246 247 248

  // Load model
  BinaryTable table1;
Y
Yan Chunwei 已提交
249
  table1.LoadFromFile("5.bf");
Y
Yan Chunwei 已提交
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
  proto::BlockDesc pt_desc1(&table1);
  pt_desc1.Load();
  BlockDesc nb_desc1(&pt_desc1);

  ASSERT_EQ(nb_desc1.Idx(), 1);
  ASSERT_EQ(nb_desc1.ParentIdx(), 2);
  ASSERT_EQ(nb_desc1.ForwardBlockIdx(), 3);

  ASSERT_EQ(nb_desc1.VarsSize(), 2);
  VarDesc var1_0(nb_desc1.GetVar<proto::VarDesc>(0));
  ASSERT_EQ(var1_0.Name(), "a");
  ASSERT_EQ(var1_0.GetType(), VarDescAPI::VarDataType::LOD_TENSOR);
  ASSERT_TRUE(var1_0.Persistable());
  VarDesc var1_1(nb_desc1.GetVar<proto::VarDesc>(1));
  ASSERT_EQ(var1_1.Name(), "b");
  ASSERT_EQ(var1_1.GetType(), VarDescAPI::VarDataType::READER);
  ASSERT_FALSE(var1_1.Persistable());

  ASSERT_EQ(nb_desc1.OpsSize(), 1);
  OpDesc op1_0(nb_desc1.GetOp<proto::OpDesc>(0));
  ASSERT_EQ(op1_0.Type(), "mul");
  auto x = op1_0.Input("X");
  ASSERT_EQ(x.size(), 1);
  ASSERT_EQ(x[0], "a");
  auto y = op1_0.Input("Y");
  ASSERT_EQ(y.size(), 1);
  ASSERT_EQ(y[0], "b");
  auto out = op1_0.Output("Out");
  ASSERT_EQ(out.size(), 1);
  ASSERT_EQ(out[0], "c");
  ASSERT_TRUE(op1_0.HasAttr("x_num_col_dims"));
  ASSERT_EQ(op1_0.GetAttr<int32_t>("x_num_col_dims"), 0);
  ASSERT_EQ(op1_0.GetAttrType("x_num_col_dims"), OpDescAPI::AttrType::INT);
  ASSERT_TRUE(op1_0.HasAttr("y_num_col_dims"));
  ASSERT_EQ(op1_0.GetAttr<int32_t>("y_num_col_dims"), 1);
  ASSERT_EQ(op1_0.GetAttrType("y_num_col_dims"), OpDescAPI::AttrType::INT);
}

TEST(NaiveBufferWrapper, ProgramDesc) {
  BinaryTable table0;
  proto::ProgramDesc pt_desc0(&table0);
  ProgramDesc nb_desc0(&pt_desc0);

  // Set ProgramDesc
  nb_desc0.SetVersion(1);
  for (int i = 0; i < 3; ++i) {
    auto* item = nb_desc0.AddBlock<proto::BlockDesc>();
  }

  // Save model
  pt_desc0.Save();
Y
Yan Chunwei 已提交
301
  table0.SaveToFile("6.bf");
Y
Yan Chunwei 已提交
302 303 304

  // Load model
  BinaryTable table1;
Y
Yan Chunwei 已提交
305
  table1.LoadFromFile("6.bf");
Y
Yan Chunwei 已提交
306 307 308 309 310 311 312 313 314 315 316
  proto::ProgramDesc pt_desc1(&table1);
  pt_desc1.Load();
  ProgramDesc nb_desc1(&pt_desc1);

  ASSERT_EQ(nb_desc1.Version(), 1);
  ASSERT_EQ(nb_desc1.BlocksSize(), 3);
}

}  // namespace naive_buffer
}  // namespace lite
}  // namespace paddle