checkpoint_op_test.cc 2.6 KB
Newer Older
T
tangwei12 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* Copyright (c) 2018 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 "paddle/fluid/framework/op_registry.h"

T
tangwei12 已提交
18
USE_NO_KERNEL_OP(checkpoint_save)
T
tangwei12 已提交
19
USE_NO_KERNEL_OP(checkpoint_load)
T
tangwei12 已提交
20

T
tangwei12 已提交
21
TEST(CheckpointSaveOp, CPU) {
T
tangwei12 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
  paddle::framework::Scope scope;
  paddle::platform::CPUPlace place;

  auto var = scope.Var("test_var");
  auto tensor = var->GetMutable<paddle::framework::LoDTensor>();
  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));
  }

T
tangwei12 已提交
41 42
  scope.Var("SERIAL_NUMBER");

T
tangwei12 已提交
43
  paddle::framework::AttributeMap attrs;
T
tangwei12 已提交
44
  attrs.insert({"dir", std::string("ckpt")});
T
tangwei12 已提交
45 46

  auto save_op = paddle::framework::OpRegistry::CreateOp(
T
bug fix  
tangwei12 已提交
47
      "checkpoint_save", {{"X", {"test_var"}}}, {}, attrs);
T
tangwei12 已提交
48 49 50 51 52 53 54
  save_op->Run(scope, place);
}

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

T
bug fix  
tangwei12 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
  auto var = scope.Var("test_var");
  auto tensor = var->GetMutable<paddle::framework::LoDTensor>();
  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));
  }

  scope.Var("SERIAL_NUMBER");
T
bug fix  
tangwei12 已提交
72 73
  auto* serial_num = scope.FindVar("SERIAL_NUMBER")->GetMutable<std::string>();
  serial_num->append("0");
T
tangwei12 已提交
74 75 76

  paddle::framework::AttributeMap attrs;
  attrs.insert({"dir", std::string("ckpt")});
T
bug fix  
tangwei12 已提交
77
  attrs.insert({"Serial", std::string("SERIAL_NUMBER")});
T
tangwei12 已提交
78

79
  auto load_op = paddle::framework::OpRegistry::CreateOp(
T
bug fix  
tangwei12 已提交
80
      "checkpoint_load", {{"X", {"test_var"}}}, {{"Argv", {}}}, attrs);
81
  load_op->Run(scope, place);
T
tangwei12 已提交
82
}