Skip to content

Commit

Permalink
add unittest for prediction
Browse files Browse the repository at this point in the history
  • Loading branch information
reyoung committed Jan 5, 2017
1 parent 106620e commit fdb64ac
Show file tree
Hide file tree
Showing 16 changed files with 304 additions and 156 deletions.
52 changes: 32 additions & 20 deletions paddle/capi/Arguments.cpp
Original file line number Diff line number Diff line change
@@ -1,49 +1,61 @@
#include "PaddleCAPI.h"
#include "PaddleCAPIPrivate.h"

#define cast(v) paddle::capi::cast<paddle::capi::CArguments>(v)
using paddle::capi::cast;

#define castArg(v) cast<paddle::capi::CArguments>(v)
#define castIVec(v) cast<paddle::capi::CIVector>(v)

extern "C" {
int PDArgsCreateNone(PD_Arguments* args) {
auto ptr = new paddle::capi::CArguments();
*args = ptr;
return PD_NO_ERROR;
return kPD_NO_ERROR;
}

int PDArgsDestroy(PD_Arguments args) {
if (args == nullptr) return PD_NULLPTR;
delete cast(args);
return PD_NO_ERROR;
if (args == nullptr) return kPD_NULLPTR;
delete castArg(args);
return kPD_NO_ERROR;
}

int PDArgsGetSize(PD_Arguments args, uint64_t* size) {
if (args == nullptr || size == nullptr) return PD_NULLPTR;
*size = cast(args)->args.size();
return PD_NO_ERROR;
if (args == nullptr || size == nullptr) return kPD_NULLPTR;
*size = castArg(args)->args.size();
return kPD_NO_ERROR;
}

int PDArgsResize(PD_Arguments args, uint64_t size) {
if (args == nullptr) return PD_NULLPTR;
cast(args)->args.resize(size);
return PD_NO_ERROR;
if (args == nullptr) return kPD_NULLPTR;
castArg(args)->args.resize(size);
return kPD_NO_ERROR;
}

int PDArgsSetValue(PD_Arguments args, uint64_t ID, PD_Matrix mat) {
if (args == nullptr || mat == nullptr) return PD_NULLPTR;
if (args == nullptr || mat == nullptr) return kPD_NULLPTR;
auto m = paddle::capi::cast<paddle::capi::CMatrix>(mat);
if (m->mat == nullptr) return PD_NULLPTR;
auto a = cast(args);
if (ID >= a->args.size()) return PD_OUT_OF_RANGE;
if (m->mat == nullptr) return kPD_NULLPTR;
auto a = castArg(args);
if (ID >= a->args.size()) return kPD_OUT_OF_RANGE;
a->args[ID].value = m->mat;
return PD_NO_ERROR;
return kPD_NO_ERROR;
}

int PDArgsGetValue(PD_Arguments args, uint64_t ID, PD_Matrix mat) {
if (args == nullptr || mat == nullptr) return PD_NULLPTR;
if (args == nullptr || mat == nullptr) return kPD_NULLPTR;
auto m = paddle::capi::cast<paddle::capi::CMatrix>(mat);
auto a = cast(args);
if (ID >= a->args.size()) return PD_OUT_OF_RANGE;
auto a = castArg(args);
if (ID >= a->args.size()) return kPD_OUT_OF_RANGE;
m->mat = a->args[ID].value;
return PD_NO_ERROR;
return kPD_NO_ERROR;
}

int PDArgsGetIds(PD_Arguments args, uint64_t ID, PD_IVector ids) {
if (args == nullptr || ids == nullptr) return kPD_NULLPTR;
auto iv = castIVec(ids);
auto a = castArg(args);
if (ID >= a->args.size()) return kPD_OUT_OF_RANGE;
iv->vec = a->args[ID].ids;
return kPD_NO_ERROR;
}
}
25 changes: 1 addition & 24 deletions paddle/capi/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,11 @@ set(CAPI_PRIVATE_HEADER
PaddleCAPIPrivate.h)
file(GLOB CAPI_SOURCES *.cpp)

add_library(paddle_capi SHARED ${CAPI_SOURCES})
add_library(paddle_capi STATIC ${CAPI_SOURCES})

target_include_directories(paddle_capi PUBLIC ${CMAKE_CURRENT_BINARY_DIR})
add_dependencies(paddle_capi gen_proto_cpp)

target_link_libraries(paddle_capi
"-Wl,-force_load"
paddle_gserver
"-Wl,-force_load"
paddle_function
paddle_pserver
paddle_trainer_lib
paddle_network
paddle_math
paddle_utils
paddle_parameter
paddle_proto
paddle_cuda
${PROTOBUF_LIBRARY}
${LIBGLOG_LIBRARY}
gflags
${CMAKE_THREAD_LIBS_INIT}
${CBLAS_LIBS}
${ZLIB_LIBRARIES}
${INTERAL_LIBS}
${CMAKE_DL_LIBS}
${PYTHON_LIBRARIES})

set(PADDLE_CAPI_INC_PATH
${CMAKE_CURRENT_BINARY_DIR}
${CMAKE_CURRENT_SOURCE_DIR})
Expand Down
62 changes: 58 additions & 4 deletions paddle/capi/GradientMachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,76 @@ extern "C" {
int PDGradientMachineCreateForPredict(PD_GradiemtMachine* machine,
void* modelConfigProtobuf,
int size) {
if (modelConfigProtobuf == nullptr) return PD_NULLPTR;
if (modelConfigProtobuf == nullptr) return kPD_NULLPTR;
paddle::ModelConfig config;
if (!config.ParseFromArray(modelConfigProtobuf, size) ||
!config.IsInitialized()) {
return PD_PROTOBUF_ERROR;
return kPD_PROTOBUF_ERROR;
}

auto ptr = new paddle::capi::CGradientMachine();
ptr->machine.reset(paddle::GradientMachine::create(
config, CREATE_MODE_TESTING, {paddle::PARAMETER_VALUE}));
*machine = ptr;
return PD_NO_ERROR;
return kPD_NO_ERROR;
}

int PDGradientMachineDestroy(PD_GradiemtMachine machine) {
delete cast(machine);
return PD_NO_ERROR;
return kPD_NO_ERROR;
}

int PDGradientMachineLoadParameterFromDisk(PD_GradiemtMachine machine,
const char* path) {
auto m = cast(machine);
if (m == nullptr || path == nullptr || m->machine == nullptr)
return kPD_NULLPTR;
m->machine->loadParameters(path);
return kPD_NO_ERROR;
}

int PDGradientMachineForward(PD_GradiemtMachine machine,
PD_Arguments inArgs,
PD_Arguments outArgs,
bool isTrain) {
auto m = cast(machine);
auto in = paddle::capi::cast<paddle::capi::CArguments>(inArgs);
auto out = paddle::capi::cast<paddle::capi::CArguments>(outArgs);
if (m == nullptr || in == nullptr || out == nullptr || m->machine == nullptr)
return kPD_NULLPTR;
m->machine->forward(
in->args, &out->args, isTrain ? paddle::PASS_TRAIN : paddle::PASS_TEST);
return kPD_NO_ERROR;
}

int PDGradientMachineCreateSharedParam(PD_GradiemtMachine origin,
void* modelConfigProtobuf,
int size,
PD_GradiemtMachine* slave) {
auto o = cast(origin);
if (origin == nullptr || slave == nullptr || o->machine == nullptr) {
return kPD_NULLPTR;
}
paddle::ModelConfig config;
if (!config.ParseFromArray(modelConfigProtobuf, size) ||
!config.IsInitialized()) {
return kPD_PROTOBUF_ERROR;
}

std::unique_ptr<paddle::capi::CGradientMachine> ptr(
new paddle::capi::CGradientMachine());
auto nn = paddle::NeuralNetwork::create(config);
nn->init(config,
[&o](int paramId, paddle::Parameter* param) {
auto p = o->machine->getParameters()[paramId];
param->enableSharedType(paddle::PARAMETER_VALUE,
p->getBuf(paddle::PARAMETER_VALUE));

},
{paddle::PARAMETER_VALUE},
false);
ptr->machine.reset(nn);
*slave = ptr.release();
return kPD_NO_ERROR;
}
}
2 changes: 1 addition & 1 deletion paddle/capi/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ int PDInit(int argc, char** argv) {
}
initPaddle(argc + 1, realArgv.data());
free(realArgv[0]);
return PD_NO_ERROR;
return kPD_NO_ERROR;
}
}
28 changes: 14 additions & 14 deletions paddle/capi/Matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,54 +8,54 @@ int PDMatCreate(PD_Matrix* mat, uint64_t height, uint64_t width, bool useGpu) {
auto ptr = new paddle::capi::CMatrix();
ptr->mat = paddle::Matrix::create(height, width, false, useGpu);
*mat = ptr;
return PD_NO_ERROR;
return kPD_NO_ERROR;
}

int PDMatCreateNone(PD_Matrix* mat) {
auto ptr = new paddle::capi::CMatrix();
*mat = ptr;
return PD_NO_ERROR;
return kPD_NO_ERROR;
}

int PDMatDestroy(PD_Matrix mat) {
if (mat == nullptr) return PD_NULLPTR;
if (mat == nullptr) return kPD_NULLPTR;
auto ptr = cast(mat);
delete ptr;
return PD_NO_ERROR;
return kPD_NO_ERROR;
}

int PDMatCopyToRow(PD_Matrix mat, uint64_t rowID, pd_real* rowArray) {
if (mat == nullptr) return PD_NULLPTR;
if (mat == nullptr) return kPD_NULLPTR;
auto ptr = cast(mat);
if (ptr->mat == nullptr) return PD_NULLPTR;
if (rowID >= ptr->mat->getHeight()) return PD_OUT_OF_RANGE;
if (ptr->mat == nullptr) return kPD_NULLPTR;
if (rowID >= ptr->mat->getHeight()) return kPD_OUT_OF_RANGE;
paddle::real* buf = ptr->mat->getRowBuf(rowID);
size_t width = ptr->mat->getWidth();
#ifndef PADDLE_ONLY_CPU
hl_memcpy(buf, rowArray, sizeof(paddle::real) * width);
#else
std::copy(rowArray, rowArray + width, buf);
#endif
return PD_NO_ERROR;
return kPD_NO_ERROR;
}

int PDMatGetRow(PD_Matrix mat, uint64_t rowID, pd_real** rawRowBuffer) {
if (mat == nullptr) return PD_NULLPTR;
if (mat == nullptr) return kPD_NULLPTR;
auto ptr = cast(mat);
if (ptr->mat == nullptr) return PD_NULLPTR;
if (rowID >= ptr->mat->getHeight()) return PD_OUT_OF_RANGE;
if (ptr->mat == nullptr) return kPD_NULLPTR;
if (rowID >= ptr->mat->getHeight()) return kPD_OUT_OF_RANGE;
*rawRowBuffer = ptr->mat->getRowBuf(rowID);
return PD_NO_ERROR;
return kPD_NO_ERROR;
}

int PDMatGetShape(PD_Matrix mat, uint64_t* height, uint64_t* width) {
if (mat == nullptr) return PD_NULLPTR;
if (mat == nullptr) return kPD_NULLPTR;
if (height != nullptr) {
*height = cast(mat)->mat->getHeight();
}
if (width != nullptr) {
*width = cast(mat)->mat->getWidth();
}
return PD_NO_ERROR;
return kPD_NO_ERROR;
}
}
33 changes: 24 additions & 9 deletions paddle/capi/PaddleCAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ extern "C" {
#endif

typedef enum {
PD_NO_ERROR = 0,
PD_NULLPTR = 1,
PD_OUT_OF_RANGE = 2,
PD_PROTOBUF_ERROR = 3,
PD_UNDEFINED_ERROR = -1,
kPD_NO_ERROR = 0,
kPD_NULLPTR = 1,
kPD_OUT_OF_RANGE = 2,
kPD_PROTOBUF_ERROR = 3,
kPD_UNDEFINED_ERROR = -1,
} PD_Error;

typedef void* PD_Vector;
typedef void* PD_IVector;

int PDVecCreate(PD_Vector* vec, uint64_t size, bool useGpu);
int PDIVecCreateNone(PD_IVector* ivec);

int PDVecDestroy(PD_Vector vec);
int PDIVecDestroy(PD_IVector ivec);

int PDVecIsSparse(PD_Vector vec, bool* isSparse);
int PDIVectorGet(PD_IVector ivec, int** buffer);

typedef void* PD_Matrix;

Expand Down Expand Up @@ -51,12 +51,27 @@ int PDArgsSetValue(PD_Arguments args, uint64_t ID, PD_Matrix mat);

int PDArgsGetValue(PD_Arguments args, uint64_t ID, PD_Matrix mat);

int PDArgsGetIds(PD_Arguments args, uint64_t ID, PD_IVector ids);

typedef void* PD_GradiemtMachine;

int PDGradientMachineCreateForPredict(PD_GradiemtMachine* machine,
void* modelConfigProtobuf,
int size);

int PDGradientMachineLoadParameterFromDisk(PD_GradiemtMachine machine,
const char* path);

int PDGradientMachineForward(PD_GradiemtMachine machine,
PD_Arguments inArgs,
PD_Arguments outArgs,
bool isTrain);

int PDGradientMachineCreateSharedParam(PD_GradiemtMachine origin,
void* modelConfigProtobuf,
int size,
PD_GradiemtMachine* slave);

int PDGradientMachineDestroy(PD_GradiemtMachine machine);

int PDInit(int argc, char** argv);
Expand Down
24 changes: 22 additions & 2 deletions paddle/capi/PaddleCAPIPrivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,40 @@
namespace paddle {
namespace capi {

struct CVector {
VectorPtr vec;
enum CType { kIVECTOR = 0, kMATRIX, kARGUMENTS, kGRADIENT_MACHINE };

#define STRUCT_HEADER CType type;

struct CHeader {
STRUCT_HEADER
};

struct CIVector {
STRUCT_HEADER
IVectorPtr vec;

CIVector() : type(kIVECTOR) {}
};

struct CMatrix {
STRUCT_HEADER
MatrixPtr mat;

CMatrix() : type(kMATRIX) {}
};

struct CArguments {
STRUCT_HEADER
std::vector<paddle::Argument> args;

CArguments() : type(kARGUMENTS) {}
};

struct CGradientMachine {
STRUCT_HEADER
paddle::GradientMachinePtr machine;

CGradientMachine() : type(kGRADIENT_MACHINE) {}
};

template <typename T>
Expand Down
Loading

0 comments on commit fdb64ac

Please sign in to comment.