save_load_combine_op_test.cc 10.5 KB
Newer Older
1
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

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 <iostream>
#include <string>
#include <vector>
#include "gtest/gtest.h"
Y
Yi Wang 已提交
19
#include "paddle/fluid/framework/op_registry.h"
20
#include "paddle/fluid/platform/float16.h"
21 22 23 24

USE_NO_KERNEL_OP(save_combine);
USE_NO_KERNEL_OP(load_combine);

25 26 27 28 29 30
template <typename T, typename U>
T* CreateForSaveCombineOp(int x, int y, const std::vector<int>& lod_info,
                          std::string var_name,
                          const paddle::platform::CPUPlace& place,
                          paddle::framework::Scope* scope,
                          paddle::framework::LoD* expect_lod) {
31
  auto var = scope->Var(var_name);
32 33
  auto tensor = var->GetMutable<paddle::framework::LoDTensor>();
  tensor->Resize({x, y});
34
  expect_lod->resize(1);
35
  for (size_t i = 0; i < lod_info.size(); i++) {
36
    (*expect_lod)[0].push_back(lod_info[i]);
37
  }
38
  tensor->set_lod(*expect_lod);
39
  T* expect = tensor->mutable_data<T>(place);
40
  for (int64_t i = 0; i < tensor->numel(); ++i) {
41 42
    expect[i] = static_cast<T>(
        static_cast<U>(i));  // For FP16, we intend to do float(float16(i))
43 44 45 46 47
  }
  return expect;
}

paddle::framework::LoDTensor* GeneratePlaceholderBeforeLoad(
48 49
    const std::string out_var_name, paddle::framework::Scope* scope) {
  auto load_var = scope->Var(out_var_name);
50 51 52 53
  auto target = load_var->GetMutable<paddle::framework::LoDTensor>();
  return target;
}

54 55 56 57 58
template <typename T>
T* GetValuesAfterLoadCombineOp(paddle::framework::LoDTensor* target,
                               const paddle::framework::Scope& scope,
                               paddle::framework::LoD* actual_lod) {
  T* actual = target->data<T>();
59
  *actual_lod = target->lod();
60 61 62
  return actual;
}

63 64 65 66 67
template <typename T, typename U>
void CheckValues(T* expect, U* actual, const paddle::framework::LoD& expect_lod,
                 const paddle::framework::LoD& actual_lod, const int& numel) {
  for (int i = 0; i < numel; ++i) {
    EXPECT_EQ(expect[i], static_cast<T>(actual[i]));
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
  }
  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]);
    }
  }
}

// Here, we create 4 LoDTensors and use save_combine_op to first save these
// in a single file. Then, we use load_combine_op to load these sequentially
TEST(SaveLoadCombineOp, CPU) {
  paddle::framework::Scope scope;
  paddle::platform::CPUPlace place;

  std::vector<int> lod1 = {0, 1, 2, 3, 10};
  int numel1 = 100;
  paddle::framework::LoD expect_lod1;
86 87
  int* expect1 = CreateForSaveCombineOp<int, int>(10, 10, lod1, "test_var1",
                                                  place, &scope, &expect_lod1);
88 89 90 91

  std::vector<int> lod2 = {0, 2, 5, 10};
  int numel2 = 200;
  paddle::framework::LoD expect_lod2;
92 93
  int* expect2 = CreateForSaveCombineOp<int, int>(10, 20, lod2, "test_var2",
                                                  place, &scope, &expect_lod2);
94 95 96 97

  std::vector<int> lod3 = {0, 2, 3, 20};
  int numel3 = 4000;
  paddle::framework::LoD expect_lod3;
98 99
  int* expect3 = CreateForSaveCombineOp<int, int>(20, 200, lod3, "test_var3",
                                                  place, &scope, &expect_lod3);
100 101 102 103

  std::vector<int> lod4 = {0, 1, 20};
  int numel4 = 1000;
  paddle::framework::LoD expect_lod4;
104 105
  int* expect4 = CreateForSaveCombineOp<int, int>(20, 50, lod4, "test_var4",
                                                  place, &scope, &expect_lod4);
106 107 108 109 110 111 112 113 114 115 116 117 118

  // Set attributes
  std::string filename = "check_tensor.ls";
  paddle::framework::AttributeMap attrs;
  attrs.insert({"file_path", std::string(filename)});

  // Run the save_combine_op
  auto save_combine_op = paddle::framework::OpRegistry::CreateOp(
      "save_combine",
      {{"X", {"test_var1", "test_var2", "test_var3", "test_var4"}}}, {}, attrs);
  save_combine_op->Run(scope, place);

  // Set up output vars
119 120 121 122
  auto target1 = GeneratePlaceholderBeforeLoad("out_var1", &scope);
  auto target2 = GeneratePlaceholderBeforeLoad("out_var2", &scope);
  auto target3 = GeneratePlaceholderBeforeLoad("out_var3", &scope);
  auto target4 = GeneratePlaceholderBeforeLoad("out_var4", &scope);
123 124 125 126 127 128 129 130

  // Run the load_combine_op
  auto load_combine_op = paddle::framework::OpRegistry::CreateOp(
      "load_combine", {},
      {{"Out", {"out_var1", "out_var2", "out_var3", "out_var4"}}}, attrs);
  load_combine_op->Run(scope, place);

  paddle::framework::LoD actual_lod1, actual_lod2, actual_lod3, actual_lod4;
131 132 133 134 135 136 137 138 139 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
  int* actual1 = GetValuesAfterLoadCombineOp<int>(target1, scope, &actual_lod1);
  int* actual2 = GetValuesAfterLoadCombineOp<int>(target2, scope, &actual_lod2);
  int* actual3 = GetValuesAfterLoadCombineOp<int>(target3, scope, &actual_lod3);
  int* actual4 = GetValuesAfterLoadCombineOp<int>(target4, scope, &actual_lod4);

  CheckValues<int, int>(expect1, actual1, expect_lod1, actual_lod1, numel1);
  CheckValues<int, int>(expect2, actual2, expect_lod2, actual_lod2, numel2);
  CheckValues<int, int>(expect3, actual3, expect_lod3, actual_lod3, numel3);
  CheckValues<int, int>(expect4, actual4, expect_lod4, actual_lod4, numel4);
}

// FP16 version of SaveLoadCombineOp Test
TEST(SaveLoadCombineFP16Op, CPU) {
  paddle::framework::Scope scope;
  paddle::platform::CPUPlace place;

  std::vector<int> lod1 = {0, 1, 2, 3, 10};
  int numel1 = 100;
  paddle::framework::LoD expect_lod1;
  float* expect1 = CreateForSaveCombineOp<float, paddle::platform::float16>(
      10, 10, lod1, "test_var1", place, &scope, &expect_lod1);

  std::vector<int> lod2 = {0, 2, 5, 10};
  int numel2 = 200;
  paddle::framework::LoD expect_lod2;
  float* expect2 = CreateForSaveCombineOp<float, paddle::platform::float16>(
      10, 20, lod2, "test_var2", place, &scope, &expect_lod2);

  std::vector<int> lod3 = {0, 20};
  int numel3 = 4000;
  paddle::framework::LoD expect_lod3;
  float* expect3 = CreateForSaveCombineOp<float, paddle::platform::float16>(
      20, 200, lod3, "test_var3", place, &scope, &expect_lod3);

  std::vector<int> lod4 = {0, 1, 20};
  int numel4 = 1000;
  paddle::framework::LoD expect_lod4;
  float* expect4 = CreateForSaveCombineOp<float, paddle::platform::float16>(
      20, 50, lod4, "test_var4", place, &scope, &expect_lod4);

  // Set attributes
  std::string filename = "check_tensor_fp16.ls";
  paddle::framework::AttributeMap attrs;
  attrs.insert({"file_path", std::string(filename)});
  attrs.insert({"save_as_fp16", true});

  // Run the save_combine_op
  auto save_combine_op = paddle::framework::OpRegistry::CreateOp(
      "save_combine",
      {{"X", {"test_var1", "test_var2", "test_var3", "test_var4"}}}, {}, attrs);
  save_combine_op->Run(scope, place);

  // Set up output vars
  auto target1 = GeneratePlaceholderBeforeLoad("out_var1", &scope);
  auto target2 = GeneratePlaceholderBeforeLoad("out_var2", &scope);
  auto target3 = GeneratePlaceholderBeforeLoad("out_var3", &scope);
  auto target4 = GeneratePlaceholderBeforeLoad("out_var4", &scope);

  // Run the load_combine_op
  auto load_combine_op = paddle::framework::OpRegistry::CreateOp(
      "load_combine", {},
      {{"Out", {"out_var1", "out_var2", "out_var3", "out_var4"}}}, attrs);
  load_combine_op->Run(scope, place);

  paddle::framework::LoD actual_lod1, actual_lod2, actual_lod3, actual_lod4;
  paddle::platform::float16* actual1 =
      GetValuesAfterLoadCombineOp<paddle::platform::float16>(target1, scope,
                                                             &actual_lod1);
  paddle::platform::float16* actual2 =
      GetValuesAfterLoadCombineOp<paddle::platform::float16>(target2, scope,
                                                             &actual_lod2);
  paddle::platform::float16* actual3 =
      GetValuesAfterLoadCombineOp<paddle::platform::float16>(target3, scope,
                                                             &actual_lod3);
  paddle::platform::float16* actual4 =
      GetValuesAfterLoadCombineOp<paddle::platform::float16>(target4, scope,
                                                             &actual_lod4);

  CheckValues<float, paddle::platform::float16>(expect1, actual1, expect_lod1,
                                                actual_lod1, numel1);
  CheckValues<float, paddle::platform::float16>(expect2, actual2, expect_lod2,
                                                actual_lod2, numel2);
  CheckValues<float, paddle::platform::float16>(expect3, actual3, expect_lod3,
                                                actual_lod3, numel3);
  CheckValues<float, paddle::platform::float16>(expect4, actual4, expect_lod4,
                                                actual_lod4, numel4);
217 218 219 220 221 222 223 224 225
}

// Test with original SaveLoadTest
TEST(SaveLoadTestWithCombineOp, CPU) {
  paddle::framework::Scope scope;
  paddle::platform::CPUPlace place;

  auto var = scope.Var("test_var");
  auto tensor = var->GetMutable<paddle::framework::LoDTensor>();
226
  tensor->Resize({3, 4000});
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
  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);
  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("check_t.save")});

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

  auto load_var = scope.Var("out_var");
  auto target = load_var->GetMutable<paddle::framework::LoDTensor>();
  auto load_op = paddle::framework::OpRegistry::CreateOp(
      "load_combine", {}, {{"Out", {"out_var"}}}, attrs);
  load_op->Run(scope, place);
  int* actual = target->data<int>();
  for (int64_t i = 0; i < tensor->numel(); ++i) {
    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]);
    }
  }
}