save_load_op_test.cc 6.7 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Y
Yu Yang 已提交
2

L
Luo Tao 已提交
3 4 5
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
Y
Yu Yang 已提交
6

L
Luo Tao 已提交
7
    http://www.apache.org/licenses/LICENSE-2.0
Y
Yu Yang 已提交
8

L
Luo Tao 已提交
9 10 11 12 13
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. */
Y
Yu Yang 已提交
14 15

#include "gtest/gtest.h"
Y
Yi Wang 已提交
16
#include "paddle/fluid/framework/op_registry.h"
K
Kexin Zhao 已提交
17
#include "paddle/fluid/platform/float16.h"
18
#include "paddle/phi/core/kernel_registry.h"
Y
Yu Yang 已提交
19

20 21 22 23
USE_OP_ITSELF(save);
PD_DECLARE_KERNEL(save, CPU, ALL_LAYOUT);
PD_DECLARE_KERNEL(save_sr, CPU, ALL_LAYOUT);
PD_DECLARE_KERNEL(cast, CPU, ALL_LAYOUT);
24 25 26
USE_OP_ITSELF(load);
PD_DECLARE_KERNEL(load, CPU, ALL_LAYOUT);
PD_DECLARE_KERNEL(load_sr, CPU, ALL_LAYOUT);
Y
Yu Yang 已提交
27 28 29 30

TEST(SaveLoadOp, CPU) {
  paddle::framework::Scope scope;
  paddle::platform::CPUPlace place;
D
dzhwinter 已提交
31

Y
Yu Yang 已提交
32
  auto var = scope.Var("test_var");
33
  auto tensor = var->GetMutable<phi::DenseTensor>();
34
  tensor->Resize({3, 10});
Y
Yu Yang 已提交
35 36 37 38 39 40 41 42 43
  paddle::framework::LoD expect_lod;
  expect_lod.resize(1);
  expect_lod[0].push_back(0);
  expect_lod[0].push_back(1);
  expect_lod[0].push_back(2);
  expect_lod[0].push_back(3);

  tensor->set_lod(expect_lod);
  int* expect = tensor->mutable_data<int>(place);
44
  for (int64_t i = 0; i < tensor->numel(); ++i) {
Y
Yu Yang 已提交
45 46 47 48 49 50 51
    expect[i] = static_cast<int>(i);
  }
  paddle::framework::AttributeMap attrs;
  attrs.insert({"file_path", std::string("tensor.save")});

  auto save_op = paddle::framework::OpRegistry::CreateOp(
      "save", {{"X", {"test_var"}}}, {}, attrs);
D
dzhwinter 已提交
52
  save_op->Run(scope, place);
Y
Yu Yang 已提交
53 54

  auto load_var = scope.Var("out_var");
55
  auto target = load_var->GetMutable<phi::DenseTensor>();
Y
Yu Yang 已提交
56 57
  auto load_op = paddle::framework::OpRegistry::CreateOp(
      "load", {}, {{"Out", {"out_var"}}}, attrs);
D
dzhwinter 已提交
58
  load_op->Run(scope, place);
Y
Yu Yang 已提交
59
  int* actual = target->data<int>();
60
  for (int64_t i = 0; i < tensor->numel(); ++i) {
Y
Yu Yang 已提交
61 62 63 64 65 66 67 68 69
    EXPECT_EQ(expect[i], actual[i]);
  }
  auto& actual_lod = target->lod();
  EXPECT_EQ(expect_lod.size(), actual_lod.size());
  for (size_t i = 0; i < expect_lod.size(); ++i) {
    for (size_t j = 0; j < expect_lod[i].size(); ++j) {
      EXPECT_EQ(expect_lod[i][j], actual_lod[i][j]);
    }
  }
70
}
K
Kexin Zhao 已提交
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 101 102 103 104 105 106 107 108
TEST(SaveLoadOpSelectedRows, CPU) {
  paddle::framework::Scope scope;
  paddle::platform::CPUPlace place;

  auto var = scope.Var("test_var_sr");
  auto selected_rows = var->GetMutable<phi::SelectedRows>();
  selected_rows->set_height(3);
  selected_rows->set_rows({0, 1, 2});
  auto* tensor = selected_rows->mutable_value();
  tensor->Resize({3, 10});
  int* expect = tensor->mutable_data<int>(place);
  for (int64_t i = 0; i < tensor->numel(); ++i) {
    expect[i] = static_cast<int>(i);
  }
  paddle::framework::AttributeMap attrs;
  attrs.insert({"file_path", std::string("selected_rows.save")});

  auto save_op = paddle::framework::OpRegistry::CreateOp(
      "save", {{"X", {"test_var_sr"}}}, {}, attrs);
  save_op->Run(scope, place);

  auto load_var = scope.Var("out_var_sr");
  auto target = load_var->GetMutable<phi::SelectedRows>();
  auto load_op = paddle::framework::OpRegistry::CreateOp(
      "load", {}, {{"Out", {"out_var_sr"}}}, attrs);
  load_op->Run(scope, place);
  const int* actual = target->value().data<int>();
  for (int64_t i = 0; i < tensor->numel(); ++i) {
    EXPECT_EQ(expect[i], actual[i]);
  }
  EXPECT_EQ(target->height(), 3);
  auto& rows = target->rows();
  for (size_t i = 0; i < rows.size(); ++i) {
    EXPECT_EQ(rows[i], static_cast<int64_t>(i));
  }
}

K
Kexin Zhao 已提交
109
TEST(SaveFP16Op, CPU) {
K
Kexin Zhao 已提交
110 111 112 113
  paddle::framework::Scope scope;
  paddle::platform::CPUPlace place;

  auto var = scope.Var("test_var");
114
  auto tensor = var->GetMutable<phi::DenseTensor>();
K
Kexin Zhao 已提交
115
  tensor->Resize({3, 10});
116 117 118 119 120 121
  paddle::framework::LoD expect_lod;
  expect_lod.resize(1);
  expect_lod[0].push_back(0);
  expect_lod[0].push_back(1);
  expect_lod[0].push_back(2);
  expect_lod[0].push_back(3);
K
Kexin Zhao 已提交
122

123
  tensor->set_lod(expect_lod);
K
Kexin Zhao 已提交
124 125
  float* expect = tensor->mutable_data<float>(place);
  for (int64_t i = 0; i < tensor->numel(); ++i) {
K
Kexin Zhao 已提交
126
    expect[i] = static_cast<float>(paddle::platform::float16(i));
K
Kexin Zhao 已提交
127 128 129 130 131 132 133 134 135 136 137
  }

  paddle::framework::AttributeMap attrs;
  attrs.insert({"file_path", std::string("tensor.save")});
  attrs.insert({"save_as_fp16", true});

  auto save_op = paddle::framework::OpRegistry::CreateOp(
      "save", {{"X", {"test_var"}}}, {}, attrs);
  save_op->Run(scope, place);

  auto load_var = scope.Var("out_var");
138
  auto target = load_var->GetMutable<phi::DenseTensor>();
K
Kexin Zhao 已提交
139 140 141 142 143 144 145
  auto load_op = paddle::framework::OpRegistry::CreateOp(
      "load", {}, {{"Out", {"out_var"}}}, attrs);
  load_op->Run(scope, place);
  paddle::platform::float16* actual = target->data<paddle::platform::float16>();
  for (int64_t i = 0; i < tensor->numel(); ++i) {
    EXPECT_EQ(expect[i], static_cast<float>(actual[i]));
  }
146 147 148 149 150 151 152
  auto& actual_lod = target->lod();
  EXPECT_EQ(expect_lod.size(), actual_lod.size());
  for (size_t i = 0; i < expect_lod.size(); ++i) {
    for (size_t j = 0; j < expect_lod[i].size(); ++j) {
      EXPECT_EQ(expect_lod[i][j], actual_lod[i][j]);
    }
  }
K
Kexin Zhao 已提交
153
}
K
Kexin Zhao 已提交
154 155 156 157 158 159

TEST(LoadFP16Op, CPU) {
  paddle::framework::Scope scope;
  paddle::platform::CPUPlace place;

  auto var = scope.Var("test_var");
160
  auto tensor = var->GetMutable<phi::DenseTensor>();
K
Kexin Zhao 已提交
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
  tensor->Resize({3, 10});

  paddle::framework::LoD expect_lod;
  expect_lod.resize(1);
  expect_lod[0].push_back(0);
  expect_lod[0].push_back(1);
  expect_lod[0].push_back(2);
  expect_lod[0].push_back(3);

  tensor->set_lod(expect_lod);
  float* expect = tensor->mutable_data<float>(place);
  for (int64_t i = 0; i < tensor->numel(); ++i) {
    expect[i] = static_cast<float>(paddle::platform::float16(i));
  }

  paddle::framework::AttributeMap attrs;
  attrs.insert({"file_path", std::string("tensor.save")});
  attrs.insert({"load_as_fp16", true});

  auto save_op = paddle::framework::OpRegistry::CreateOp(
      "save", {{"X", {"test_var"}}}, {}, attrs);
  save_op->Run(scope, place);

  auto load_var = scope.Var("out_var");
185
  load_var->GetMutable<phi::DenseTensor>();
K
Kexin Zhao 已提交
186 187 188 189
  auto load_op = paddle::framework::OpRegistry::CreateOp(
      "load", {}, {{"Out", {"out_var"}}}, attrs);
  load_op->Run(scope, place);

190
  auto target = load_var->Get<phi::DenseTensor>();
K
Kexin Zhao 已提交
191
  paddle::platform::float16* actual = target.data<paddle::platform::float16>();
K
Kexin Zhao 已提交
192 193 194 195
  for (int64_t i = 0; i < tensor->numel(); ++i) {
    EXPECT_EQ(expect[i], static_cast<float>(actual[i]));
  }

K
Kexin Zhao 已提交
196
  auto& actual_lod = target.lod();
K
Kexin Zhao 已提交
197 198 199 200 201 202 203
  EXPECT_EQ(expect_lod.size(), actual_lod.size());
  for (size_t i = 0; i < expect_lod.size(); ++i) {
    for (size_t j = 0; j < expect_lod[i].size(); ++j) {
      EXPECT_EQ(expect_lod[i][j], actual_lod[i][j]);
    }
  }
}