test_GradientMachine.cpp 3.4 KB
Newer Older
Y
Yu Yang 已提交
1
#include <gtest/gtest.h>
Y
Yu Yang 已提交
2
#include <paddle/gserver/gradientmachines/GradientMachine.h>
Y
Stash  
Yu Yang 已提交
3
#include <paddle/trainer/TrainerConfigHelper.h>
Y
Yu Yang 已提交
4 5
#include <stdlib.h>
#include <string.h>
Y
Yu Yang 已提交
6
#include <type_traits>
Y
Yu Yang 已提交
7
#include "PaddleCAPI.h"
Y
Yu Yang 已提交
8
#include "paddle/utils/ThreadLocal.h"
Y
Yu Yang 已提交
9

Y
Yu Yang 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22
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 已提交
23 24 25 26
  std::string buffer;
  ASSERT_TRUE(config.getModelConfig().SerializeToString(&buffer));
  PD_GradiemtMachine machine;

Y
Yu Yang 已提交
27
  ASSERT_EQ(kPD_NO_ERROR,
Y
Stash  
Yu Yang 已提交
28 29
            PDGradientMachineCreateForPredict(
                &machine, &buffer[0], (int)buffer.size()));
Y
Yu Yang 已提交
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
  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, "./"));

  PD_GradiemtMachine machineSlave;
  ASSERT_EQ(kPD_NO_ERROR,
            PDGradientMachineCreateSharedParam(
                machine, &buffer[0], (int)buffer.size(), &machineSlave));
  std::swap(machineSlave, machine);
  PD_Arguments outArgs;
  ASSERT_EQ(kPD_NO_ERROR, PDArgsCreateNone(&outArgs));

  PD_Arguments inArgs;
  ASSERT_EQ(kPD_NO_ERROR, PDArgsCreateNone(&inArgs));
  ASSERT_EQ(kPD_NO_ERROR, PDArgsResize(inArgs, 1));
  PD_Matrix mat;
  ASSERT_EQ(kPD_NO_ERROR, PDMatCreate(&mat, 1, 100, false));
  static_assert(std::is_same<pd_real, paddle::real>::value, "");

  auto data = randomBuffer(100);
  pd_real* rowPtr;
  ASSERT_EQ(kPD_NO_ERROR, PDMatGetRow(mat, 0, &rowPtr));
  memcpy(rowPtr, data.data(), data.size() * sizeof(pd_real));

  ASSERT_EQ(kPD_NO_ERROR, PDArgsSetValue(inArgs, 0, mat));
  ASSERT_EQ(kPD_NO_ERROR,
            PDGradientMachineForward(machine, inArgs, outArgs, false));

  uint64_t sz;
  ASSERT_EQ(kPD_NO_ERROR, PDArgsGetSize(outArgs, &sz));
  ASSERT_EQ(1UL, sz);

  ASSERT_EQ(kPD_NO_ERROR, PDArgsGetValue(outArgs, 0, mat));
  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;
  ASSERT_EQ(kPD_NO_ERROR, PDMatGetShape(mat, &height, &width));
  ASSERT_EQ(matPaddle->getHeight(), height);
  ASSERT_EQ(matPaddle->getWidth(), width);

  ASSERT_EQ(kPD_NO_ERROR, PDMatGetRow(mat, 0, &rowPtr));
  for (size_t i = 0; i < width; ++i) {
    ASSERT_NEAR(matPaddle->getData()[i], rowPtr[i], 1e-5);
  }

  ASSERT_EQ(kPD_NO_ERROR, PDMatDestroy(mat));
  ASSERT_EQ(kPD_NO_ERROR, PDArgsDestroy(inArgs));
  ASSERT_EQ(kPD_NO_ERROR, PDArgsDestroy(outArgs));
  std::swap(machineSlave, machine);
  ASSERT_EQ(kPD_NO_ERROR, PDGradientMachineDestroy(machineSlave));
  ASSERT_EQ(kPD_NO_ERROR, PDGradientMachineDestroy(machine));
Y
Yu Yang 已提交
94 95 96 97 98 99 100 101 102 103 104 105
}

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();
}