test_GradientMachine.cpp 4.1 KB
Newer Older
Y
Yu Yang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

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. */

Y
Yu Yang 已提交
15
#include <gtest/gtest.h>
Y
Yu Yang 已提交
16
#include <paddle/gserver/gradientmachines/GradientMachine.h>
Y
Stash  
Yu Yang 已提交
17
#include <paddle/trainer/TrainerConfigHelper.h>
Y
Yu Yang 已提交
18 19
#include <stdlib.h>
#include <string.h>
Y
Yu Yang 已提交
20
#include <type_traits>
Y
Yu Yang 已提交
21
#include "PaddleCAPI.h"
Y
Yu Yang 已提交
22
#include "paddle/utils/ThreadLocal.h"
Y
Yu Yang 已提交
23

Y
Yu Yang 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36
static std::vector<pd_real> randomBuffer(size_t bufSize) {
  auto& eng = paddle::ThreadLocalRandomEngine::get();
  std::uniform_real_distribution<pd_real> dist(-1.0, 1.0);
  std::vector<pd_real> retv;
  retv.reserve(bufSize);
  for (size_t i = 0; i < bufSize; ++i) {
    retv.push_back(dist(eng));
  }
  return retv;
}

TEST(GradientMachine, testPredict) {
  paddle::TrainerConfigHelper config("./test_predict_network.py");
Y
Stash  
Yu Yang 已提交
37 38
  std::string buffer;
  ASSERT_TRUE(config.getModelConfig().SerializeToString(&buffer));
Y
Yu Yang 已提交
39
  PD_GradientMachine machine;
Y
Stash  
Yu Yang 已提交
40

Y
Yu Yang 已提交
41
  ASSERT_EQ(kPD_NO_ERROR,
Y
Stash  
Yu Yang 已提交
42 43
            PDGradientMachineCreateForPredict(
                &machine, &buffer[0], (int)buffer.size()));
Y
Yu Yang 已提交
44 45 46 47 48 49 50 51 52
  std::unique_ptr<paddle::GradientMachine> gm(
      paddle::GradientMachine::create(config.getModelConfig()));
  ASSERT_NE(nullptr, gm);
  gm->randParameters();
  gm->saveParameters("./");

  ASSERT_EQ(kPD_NO_ERROR,
            PDGradientMachineLoadParameterFromDisk(machine, "./"));

Y
Yu Yang 已提交
53
  PD_GradientMachine machineSlave;
Y
Yu Yang 已提交
54 55 56 57
  ASSERT_EQ(kPD_NO_ERROR,
            PDGradientMachineCreateSharedParam(
                machine, &buffer[0], (int)buffer.size(), &machineSlave));
  std::swap(machineSlave, machine);
Y
Stash  
Yu Yang 已提交
58
  paddle_arguments outArgs = paddle_arguments_create_none();
Y
Yu Yang 已提交
59

Y
Stash  
Yu Yang 已提交
60 61
  paddle_arguments inArgs = paddle_arguments_create_none();
  ASSERT_EQ(kPD_NO_ERROR, paddle_arguments_resize(inArgs, 1));
Y
Yu Yang 已提交
62
  paddle_matrix mat = paddle_matrix_create(1, 100, false);
Y
Yu Yang 已提交
63 64 65 66
  static_assert(std::is_same<pd_real, paddle::real>::value, "");

  auto data = randomBuffer(100);
  pd_real* rowPtr;
Y
Yu Yang 已提交
67
  ASSERT_EQ(kPD_NO_ERROR, paddle_matrix_get_row(mat, 0, &rowPtr));
Y
Yu Yang 已提交
68 69
  memcpy(rowPtr, data.data(), data.size() * sizeof(pd_real));

Y
Stash  
Yu Yang 已提交
70
  ASSERT_EQ(kPD_NO_ERROR, paddle_arguments_set_value(inArgs, 0, mat));
Y
Yu Yang 已提交
71 72 73 74
  ASSERT_EQ(kPD_NO_ERROR,
            PDGradientMachineForward(machine, inArgs, outArgs, false));

  uint64_t sz;
Y
Stash  
Yu Yang 已提交
75
  ASSERT_EQ(kPD_NO_ERROR, paddle_arguments_size(outArgs, &sz));
Y
Yu Yang 已提交
76 77
  ASSERT_EQ(1UL, sz);

Y
Stash  
Yu Yang 已提交
78
  ASSERT_EQ(kPD_NO_ERROR, paddle_arguments_value(outArgs, 0, mat));
Y
Yu Yang 已提交
79 80 81 82 83 84 85 86 87 88 89
  std::vector<paddle::Argument> paddleInArgs;
  std::vector<paddle::Argument> paddleOutArgs;
  paddleInArgs.resize(1);
  paddleInArgs[0].value =
      paddle::Matrix::create(data.data(), 1, 100, false, false);

  gm->forward(paddleInArgs, &paddleOutArgs, paddle::PASS_TEST);

  auto matPaddle = paddleOutArgs[0].value;

  uint64_t height, width;
Y
Yu Yang 已提交
90
  ASSERT_EQ(kPD_NO_ERROR, paddle_matrix_get_shape(mat, &height, &width));
Y
Yu Yang 已提交
91 92 93
  ASSERT_EQ(matPaddle->getHeight(), height);
  ASSERT_EQ(matPaddle->getWidth(), width);

Y
Yu Yang 已提交
94
  ASSERT_EQ(kPD_NO_ERROR, paddle_matrix_get_row(mat, 0, &rowPtr));
Y
Yu Yang 已提交
95 96 97 98
  for (size_t i = 0; i < width; ++i) {
    ASSERT_NEAR(matPaddle->getData()[i], rowPtr[i], 1e-5);
  }

Y
Yu Yang 已提交
99
  ASSERT_EQ(kPD_NO_ERROR, paddle_matrix_destroy(mat));
Y
Stash  
Yu Yang 已提交
100 101
  ASSERT_EQ(kPD_NO_ERROR, paddle_arguments_destroy(inArgs));
  ASSERT_EQ(kPD_NO_ERROR, paddle_arguments_destroy(outArgs));
Y
Yu Yang 已提交
102 103 104
  std::swap(machineSlave, machine);
  ASSERT_EQ(kPD_NO_ERROR, PDGradientMachineDestroy(machineSlave));
  ASSERT_EQ(kPD_NO_ERROR, PDGradientMachineDestroy(machine));
Y
Yu Yang 已提交
105 106 107 108 109 110 111 112 113 114 115 116
}

int main(int argc, char** argv) {
  testing::InitGoogleTest(&argc, argv);
  std::vector<char*> argvs;
  argvs.push_back(strdup("--use_gpu=false"));
  PDInit((int)argvs.size(), argvs.data());
  for (auto each : argvs) {
    free(each);
  }
  return RUN_ALL_TESTS();
}