提交 7bb12fdb 编写于 作者: Y Yu Yang

Refactor API follow comments.

上级 5a9987a3
...@@ -21,31 +21,27 @@ using paddle::capi::cast; ...@@ -21,31 +21,27 @@ using paddle::capi::cast;
#define castIVec(v) cast<paddle::capi::CIVector>(v) #define castIVec(v) cast<paddle::capi::CIVector>(v)
extern "C" { extern "C" {
int PDArgsCreateNone(PD_Arguments* args) { PD_Arguments PDArgsCreateNone() { return new paddle::capi::CArguments(); }
auto ptr = new paddle::capi::CArguments();
*args = ptr;
return kPD_NO_ERROR;
}
int PDArgsDestroy(PD_Arguments args) { PD_Error PDArgsDestroy(PD_Arguments args) {
if (args == nullptr) return kPD_NULLPTR; if (args == nullptr) return kPD_NULLPTR;
delete castArg(args); delete castArg(args);
return kPD_NO_ERROR; return kPD_NO_ERROR;
} }
int PDArgsGetSize(PD_Arguments args, uint64_t* size) { PD_Error PDArgsGetSize(PD_Arguments args, uint64_t* size) {
if (args == nullptr || size == nullptr) return kPD_NULLPTR; if (args == nullptr || size == nullptr) return kPD_NULLPTR;
*size = castArg(args)->args.size(); *size = castArg(args)->args.size();
return kPD_NO_ERROR; return kPD_NO_ERROR;
} }
int PDArgsResize(PD_Arguments args, uint64_t size) { PD_Error PDArgsResize(PD_Arguments args, uint64_t size) {
if (args == nullptr) return kPD_NULLPTR; if (args == nullptr) return kPD_NULLPTR;
castArg(args)->args.resize(size); castArg(args)->args.resize(size);
return kPD_NO_ERROR; return kPD_NO_ERROR;
} }
int PDArgsSetValue(PD_Arguments args, uint64_t ID, PD_Matrix mat) { PD_Error PDArgsSetValue(PD_Arguments args, uint64_t ID, PD_Matrix mat) {
if (args == nullptr || mat == nullptr) return kPD_NULLPTR; if (args == nullptr || mat == nullptr) return kPD_NULLPTR;
auto m = paddle::capi::cast<paddle::capi::CMatrix>(mat); auto m = paddle::capi::cast<paddle::capi::CMatrix>(mat);
if (m->mat == nullptr) return kPD_NULLPTR; if (m->mat == nullptr) return kPD_NULLPTR;
...@@ -55,7 +51,7 @@ int PDArgsSetValue(PD_Arguments args, uint64_t ID, PD_Matrix mat) { ...@@ -55,7 +51,7 @@ int PDArgsSetValue(PD_Arguments args, uint64_t ID, PD_Matrix mat) {
return kPD_NO_ERROR; return kPD_NO_ERROR;
} }
int PDArgsGetValue(PD_Arguments args, uint64_t ID, PD_Matrix mat) { PD_Error PDArgsGetValue(PD_Arguments args, uint64_t ID, PD_Matrix mat) {
if (args == nullptr || mat == nullptr) return kPD_NULLPTR; if (args == nullptr || mat == nullptr) return kPD_NULLPTR;
auto m = paddle::capi::cast<paddle::capi::CMatrix>(mat); auto m = paddle::capi::cast<paddle::capi::CMatrix>(mat);
auto a = castArg(args); auto a = castArg(args);
...@@ -64,7 +60,7 @@ int PDArgsGetValue(PD_Arguments args, uint64_t ID, PD_Matrix mat) { ...@@ -64,7 +60,7 @@ int PDArgsGetValue(PD_Arguments args, uint64_t ID, PD_Matrix mat) {
return kPD_NO_ERROR; return kPD_NO_ERROR;
} }
int PDArgsGetIds(PD_Arguments args, uint64_t ID, PD_IVector ids) { PD_Error PDArgsGetIds(PD_Arguments args, uint64_t ID, PD_IVector ids) {
if (args == nullptr || ids == nullptr) return kPD_NULLPTR; if (args == nullptr || ids == nullptr) return kPD_NULLPTR;
auto iv = castIVec(ids); auto iv = castIVec(ids);
auto a = castArg(args); auto a = castArg(args);
...@@ -73,7 +69,7 @@ int PDArgsGetIds(PD_Arguments args, uint64_t ID, PD_IVector ids) { ...@@ -73,7 +69,7 @@ int PDArgsGetIds(PD_Arguments args, uint64_t ID, PD_IVector ids) {
return kPD_NO_ERROR; return kPD_NO_ERROR;
} }
int PDArgsSetIds(PD_Arguments args, uint64_t ID, PD_IVector ids) { PD_Error PDArgsSetIds(PD_Arguments args, uint64_t ID, PD_IVector ids) {
//! TODO(lizhao): Complete this method. //! TODO(lizhao): Complete this method.
if (args == nullptr || ids == nullptr) return kPD_NULLPTR; if (args == nullptr || ids == nullptr) return kPD_NULLPTR;
auto iv = paddle::capi::cast<paddle::capi::CIVector>(ids); auto iv = paddle::capi::cast<paddle::capi::CIVector>(ids);
...@@ -84,7 +80,7 @@ int PDArgsSetIds(PD_Arguments args, uint64_t ID, PD_IVector ids) { ...@@ -84,7 +80,7 @@ int PDArgsSetIds(PD_Arguments args, uint64_t ID, PD_IVector ids) {
return kPD_NO_ERROR; return kPD_NO_ERROR;
} }
int PDArgsSetSequenceStartPos(PD_Arguments args, PD_Error PDArgsSetSequenceStartPos(PD_Arguments args,
uint64_t ID, uint64_t ID,
PD_IVector seqPos) { PD_IVector seqPos) {
if (args == nullptr || seqPos == nullptr) return kPD_NULLPTR; if (args == nullptr || seqPos == nullptr) return kPD_NULLPTR;
...@@ -97,7 +93,7 @@ int PDArgsSetSequenceStartPos(PD_Arguments args, ...@@ -97,7 +93,7 @@ int PDArgsSetSequenceStartPos(PD_Arguments args,
return kPD_NO_ERROR; return kPD_NO_ERROR;
} }
int PDArgsSetSubSequenceStartPos(PD_Arguments args, PD_Error PDArgsSetSubSequenceStartPos(PD_Arguments args,
uint64_t ID, uint64_t ID,
PD_IVector subSeqPos) { PD_IVector subSeqPos) {
if (args == nullptr || subSeqPos == nullptr) return kPD_NULLPTR; if (args == nullptr || subSeqPos == nullptr) return kPD_NULLPTR;
...@@ -110,7 +106,7 @@ int PDArgsSetSubSequenceStartPos(PD_Arguments args, ...@@ -110,7 +106,7 @@ int PDArgsSetSubSequenceStartPos(PD_Arguments args,
return kPD_NO_ERROR; return kPD_NO_ERROR;
} }
int PDArgsGetSequenceStartPos(PD_Arguments args, PD_Error PDArgsGetSequenceStartPos(PD_Arguments args,
uint64_t ID, uint64_t ID,
PD_IVector seqPos) { PD_IVector seqPos) {
if (args == nullptr || seqPos == nullptr) return kPD_NULLPTR; if (args == nullptr || seqPos == nullptr) return kPD_NULLPTR;
...@@ -122,7 +118,7 @@ int PDArgsGetSequenceStartPos(PD_Arguments args, ...@@ -122,7 +118,7 @@ int PDArgsGetSequenceStartPos(PD_Arguments args,
return kPD_NO_ERROR; return kPD_NO_ERROR;
} }
int PDArgsGetSubSequenceStartPos(PD_Arguments args, PD_Error PDArgsGetSubSequenceStartPos(PD_Arguments args,
uint64_t ID, uint64_t ID,
PD_IVector subSeqPos) { PD_IVector subSeqPos) {
if (args == nullptr || subSeqPos == nullptr) return kPD_NULLPTR; if (args == nullptr || subSeqPos == nullptr) return kPD_NULLPTR;
......
...@@ -38,7 +38,7 @@ NeuralNetwork* newCustomNerualNetwork(const std::string& name, ...@@ -38,7 +38,7 @@ NeuralNetwork* newCustomNerualNetwork(const std::string& name,
} // namespace paddle } // namespace paddle
extern "C" { extern "C" {
int PDGradientMachineCreateForPredict(PD_GradientMachine* machine, PD_Error PDGradientMachineCreateForPredict(PD_GradientMachine* machine,
void* modelConfigProtobuf, void* modelConfigProtobuf,
int size) { int size) {
if (modelConfigProtobuf == nullptr) return kPD_NULLPTR; if (modelConfigProtobuf == nullptr) return kPD_NULLPTR;
...@@ -55,12 +55,12 @@ int PDGradientMachineCreateForPredict(PD_GradientMachine* machine, ...@@ -55,12 +55,12 @@ int PDGradientMachineCreateForPredict(PD_GradientMachine* machine,
return kPD_NO_ERROR; return kPD_NO_ERROR;
} }
int PDGradientMachineDestroy(PD_GradientMachine machine) { PD_Error PDGradientMachineDestroy(PD_GradientMachine machine) {
delete cast(machine); delete cast(machine);
return kPD_NO_ERROR; return kPD_NO_ERROR;
} }
int PDGradientMachineLoadParameterFromDisk(PD_GradientMachine machine, PD_Error PDGradientMachineLoadParameterFromDisk(PD_GradientMachine machine,
const char* path) { const char* path) {
auto m = cast(machine); auto m = cast(machine);
if (m == nullptr || path == nullptr || m->machine == nullptr) if (m == nullptr || path == nullptr || m->machine == nullptr)
...@@ -69,7 +69,7 @@ int PDGradientMachineLoadParameterFromDisk(PD_GradientMachine machine, ...@@ -69,7 +69,7 @@ int PDGradientMachineLoadParameterFromDisk(PD_GradientMachine machine,
return kPD_NO_ERROR; return kPD_NO_ERROR;
} }
int PDGradientMachineForward(PD_GradientMachine machine, PD_Error PDGradientMachineForward(PD_GradientMachine machine,
PD_Arguments inArgs, PD_Arguments inArgs,
PD_Arguments outArgs, PD_Arguments outArgs,
bool isTrain) { bool isTrain) {
...@@ -83,7 +83,7 @@ int PDGradientMachineForward(PD_GradientMachine machine, ...@@ -83,7 +83,7 @@ int PDGradientMachineForward(PD_GradientMachine machine,
return kPD_NO_ERROR; return kPD_NO_ERROR;
} }
int PDGradientMachineCreateSharedParam(PD_GradientMachine origin, PD_Error PDGradientMachineCreateSharedParam(PD_GradientMachine origin,
void* modelConfigProtobuf, void* modelConfigProtobuf,
int size, int size,
PD_GradientMachine* slave) { PD_GradientMachine* slave) {
......
...@@ -29,7 +29,7 @@ static void initPaddle(int argc, char** argv) { ...@@ -29,7 +29,7 @@ static void initPaddle(int argc, char** argv) {
} }
extern "C" { extern "C" {
int PDInit(int argc, char** argv) { PD_Error PDInit(int argc, char** argv) {
std::vector<char*> realArgv; std::vector<char*> realArgv;
realArgv.reserve(argc + 1); realArgv.reserve(argc + 1);
realArgv.push_back(strdup("")); realArgv.push_back(strdup(""));
......
...@@ -18,27 +18,22 @@ limitations under the License. */ ...@@ -18,27 +18,22 @@ limitations under the License. */
#define cast(v) paddle::capi::cast<paddle::capi::CMatrix>(v) #define cast(v) paddle::capi::cast<paddle::capi::CMatrix>(v)
extern "C" { extern "C" {
int PDMatCreate(PD_Matrix* mat, uint64_t height, uint64_t width, bool useGpu) { PD_Matrix PDMatCreate(uint64_t height, uint64_t width, bool useGpu) {
auto ptr = new paddle::capi::CMatrix(); auto ptr = new paddle::capi::CMatrix();
ptr->mat = paddle::Matrix::create(height, width, false, useGpu); ptr->mat = paddle::Matrix::create(height, width, false, useGpu);
*mat = ptr; return ptr;
return kPD_NO_ERROR;
} }
int PDMatCreateNone(PD_Matrix* mat) { PD_Matrix PDMatCreateNone() { return new paddle::capi::CMatrix(); }
auto ptr = new paddle::capi::CMatrix();
*mat = ptr;
return kPD_NO_ERROR;
}
int PDMatDestroy(PD_Matrix mat) { PD_Error PDMatDestroy(PD_Matrix mat) {
if (mat == nullptr) return kPD_NULLPTR; if (mat == nullptr) return kPD_NULLPTR;
auto ptr = cast(mat); auto ptr = cast(mat);
delete ptr; delete ptr;
return kPD_NO_ERROR; return kPD_NO_ERROR;
} }
int PDMatCopyToRow(PD_Matrix mat, uint64_t rowID, pd_real* rowArray) { PD_Error PDMatCopyToRow(PD_Matrix mat, uint64_t rowID, pd_real* rowArray) {
if (mat == nullptr) return kPD_NULLPTR; if (mat == nullptr) return kPD_NULLPTR;
auto ptr = cast(mat); auto ptr = cast(mat);
if (ptr->mat == nullptr) return kPD_NULLPTR; if (ptr->mat == nullptr) return kPD_NULLPTR;
...@@ -53,7 +48,7 @@ int PDMatCopyToRow(PD_Matrix mat, uint64_t rowID, pd_real* rowArray) { ...@@ -53,7 +48,7 @@ int PDMatCopyToRow(PD_Matrix mat, uint64_t rowID, pd_real* rowArray) {
return kPD_NO_ERROR; return kPD_NO_ERROR;
} }
int PDMatGetRow(PD_Matrix mat, uint64_t rowID, pd_real** rawRowBuffer) { PD_Error PDMatGetRow(PD_Matrix mat, uint64_t rowID, pd_real** rawRowBuffer) {
if (mat == nullptr) return kPD_NULLPTR; if (mat == nullptr) return kPD_NULLPTR;
auto ptr = cast(mat); auto ptr = cast(mat);
if (ptr->mat == nullptr) return kPD_NULLPTR; if (ptr->mat == nullptr) return kPD_NULLPTR;
...@@ -62,7 +57,7 @@ int PDMatGetRow(PD_Matrix mat, uint64_t rowID, pd_real** rawRowBuffer) { ...@@ -62,7 +57,7 @@ int PDMatGetRow(PD_Matrix mat, uint64_t rowID, pd_real** rawRowBuffer) {
return kPD_NO_ERROR; return kPD_NO_ERROR;
} }
int PDMatGetShape(PD_Matrix mat, uint64_t* height, uint64_t* width) { PD_Error PDMatGetShape(PD_Matrix mat, uint64_t* height, uint64_t* width) {
if (mat == nullptr) return kPD_NULLPTR; if (mat == nullptr) return kPD_NULLPTR;
if (height != nullptr) { if (height != nullptr) {
*height = cast(mat)->mat->getHeight(); *height = cast(mat)->mat->getHeight();
......
...@@ -49,48 +49,115 @@ typedef enum { ...@@ -49,48 +49,115 @@ typedef enum {
*/ */
typedef void* PD_IVector; typedef void* PD_IVector;
PD_API int PDIVecCreateNone(PD_IVector* ivec); /**
* @brief Create an none int vector. It just a handler and store nothing. Used
* to get output from other api.
* @return None int vector.
*/
PD_API PD_IVector PDIVecCreateNone();
/** /**
* @brief PDIVectorCreate create a paddle int vector * @brief PDIVectorCreate create a paddle int vector
* @param [out] ivec: output int vector. * @param array: input array.
* @param [in] array: input array. * @param size: input array size.
* @param [in] size: input array size. * @param copy: memory copy or just use same memory. True if copy.
* @param [in] copy: memory copy or just use same memory. True if copy. * @param useGPU: True if use GPU
* @return PD_Error * @return PD_Error
*/ */
PD_API int PDIVectorCreate(PD_IVector* ivec, PD_API PD_IVector PDIVectorCreate(int* array,
int* array,
uint64_t size, uint64_t size,
bool copy); bool copy,
bool useGPU);
PD_API int PDIVecDestroy(PD_IVector ivec); /**
* @brief PDIVecDestroy destory an int vector.
* @param ivec vector to be destoried.
* @return PD_Error
*/
PD_API PD_Error PDIVecDestroy(PD_IVector ivec);
PD_API int PDIVectorGet(PD_IVector ivec, int** buffer); /**
* @brief PDIVectorGet get raw buffer stored inside this int vector. It could be
* GPU memory if this int vector is stored in GPU.
* @param [in] ivec int vector
* @param [out] buffer the return buffer pointer.
* @return PD_Error
*/
PD_API PD_Error PDIVectorGet(PD_IVector ivec, int** buffer);
PD_API int PDIVectorResize(PD_IVector ivec, uint64_t size); /**
* @brief PDIVectorResize resize the int vector.
* @param [in] ivec: int vector
* @param [in] size: size to change
* @return PD_Error
*/
PD_API PD_Error PDIVectorResize(PD_IVector ivec, uint64_t size);
PD_API int PDIVectorGetSize(PD_IVector ivec, uint64_t* size); /**
* @brief PDIVectorGetSize get the size of int vector.
* @param [in] ivec: int vector
* @param [out] size: return size of this int vector.
* @return PD_Error
*/
PD_API PD_Error PDIVectorGetSize(PD_IVector ivec, uint64_t* size);
/** /**
* Matrix functions. Return will be a PD_Error type. * Matrix functions. Return will be a PD_Error type.
*/ */
typedef void* PD_Matrix; typedef void* PD_Matrix;
PD_API int PDMatCreate(PD_Matrix* mat, /**
uint64_t height, * @brief PDMatCreate Create a dense matrix
uint64_t width, * @param height matrix height.
bool useGpu); * @param width matrix width
* @param useGpu use GPU of not
* @return Matrix handler
*/
PD_API PD_Matrix PDMatCreate(uint64_t height, uint64_t width, bool useGpu);
PD_API int PDMatDestroy(PD_Matrix mat); /**
* @brief PDMatDestroy Destroy a matrix.
* @param mat
* @return PD_Error
*/
PD_API PD_Error PDMatDestroy(PD_Matrix mat);
PD_API int PDMatCopyToRow(PD_Matrix mat, uint64_t rowID, pd_real* rowArray); /**
* @brief PDMatCopyToRow Copy a row to matrix.
* @param mat Target Matrix
* @param rowID Index of row
* @param rowArray Row data.
* @return PD_Error
*/
PD_API PD_Error PDMatCopyToRow(PD_Matrix mat,
uint64_t rowID,
pd_real* rowArray);
PD_API int PDMatGetRow(PD_Matrix mat, uint64_t rowID, pd_real** rawRowBuffer); /**
* @brief PDMatGetRow Get raw row buffer from matrix
* @param [in] mat Target matrix
* @param [in] rowID Index of row.
* @param [out] rawRowBuffer Row Buffer
* @return PD_Error
*/
PD_API PD_Error PDMatGetRow(PD_Matrix mat,
uint64_t rowID,
pd_real** rawRowBuffer);
PD_API int PDMatCreateNone(PD_Matrix* mat); /**
* @brief PDMatCreateNone Create None Matrix
* @return
*/
PD_API PD_Matrix PDMatCreateNone();
PD_API int PDMatGetShape(PD_Matrix mat, uint64_t* height, uint64_t* width); /**
* @brief PDMatGetShape get the shape of matrix
* @param mat target matrix
* @param height The height of matrix
* @param width The width of matrix
* @return PD_Error
*/
PD_API PD_Error PDMatGetShape(PD_Matrix mat, uint64_t* height, uint64_t* width);
/** /**
* Arguments functions. Each argument means layer output. Arguments means a * Arguments functions. Each argument means layer output. Arguments means a
...@@ -98,35 +165,119 @@ PD_API int PDMatGetShape(PD_Matrix mat, uint64_t* height, uint64_t* width); ...@@ -98,35 +165,119 @@ PD_API int PDMatGetShape(PD_Matrix mat, uint64_t* height, uint64_t* width);
*/ */
typedef void* PD_Arguments; typedef void* PD_Arguments;
PD_API int PDArgsCreateNone(PD_Arguments* args); /**
* @brief PDArgsCreateNone Create a array of arguments, which size is zero.
* @return Arguemnts
*/
PD_API PD_Arguments PDArgsCreateNone();
PD_API int PDArgsDestroy(PD_Arguments args); /**
* @brief PDArgsDestroy Destroy the arguments
* @param args arguments to destroy
* @return PD_Error
*/
PD_API PD_Error PDArgsDestroy(PD_Arguments args);
PD_API int PDArgsGetSize(PD_Arguments args, uint64_t* size); /**
* @brief PDArgsGetSize Get size of arguments array
* @param [in] args arguments array
* @param [out] size array size
* @return PD_Error
*/
PD_API PD_Error PDArgsGetSize(PD_Arguments args, uint64_t* size);
PD_API int PDArgsResize(PD_Arguments args, uint64_t size); /**
* @brief PDArgsResize Resize a arguments array.
* @param args arguments array.
* @param size target size of array
* @return PD_Error
*/
PD_API PD_Error PDArgsResize(PD_Arguments args, uint64_t size);
PD_API int PDArgsSetValue(PD_Arguments args, uint64_t ID, PD_Matrix mat); /**
* @brief PDArgsSetValue Set value matrix of one argument in array, which index
* is `ID`.
* @param args arguments array
* @param ID array index
* @param mat matrix pointer
* @return PD_Error
*/
PD_API PD_Error PDArgsSetValue(PD_Arguments args, uint64_t ID, PD_Matrix mat);
PD_API int PDArgsGetValue(PD_Arguments args, uint64_t ID, PD_Matrix mat); /**
* @brief PDArgsGetValue Get value matrix of one argument in array, which index
* is `ID`.
* @param [in] args arguments array
* @param [in] ID array index
* @param [out] mat matrix pointer
* @return PD_Error
*/
PD_API PD_Error PDArgsGetValue(PD_Arguments args, uint64_t ID, PD_Matrix mat);
PD_API int PDArgsGetIds(PD_Arguments args, uint64_t ID, PD_IVector ids); /**
* @brief PDArgsGetIds Get the integer vector of one argument in array, which
* index is `ID`.
* @param args arguments array
* @param ID array index
* @param ids integer vector pointer
* @return PD_Error
*/
PD_API PD_Error PDArgsGetIds(PD_Arguments args, uint64_t ID, PD_IVector ids);
PD_API int PDArgsSetIds(PD_Arguments args, uint64_t ID, PD_IVector ids); /**
* @brief PDArgsSetIds Set the integer vector of one argument in array, which
* index is `ID`.
* @param [in] args arguments array
* @param [in] ID array index
* @param [out] ids integer vector pointer
* @return PD_Error
*/
PD_API PD_Error PDArgsSetIds(PD_Arguments args, uint64_t ID, PD_IVector ids);
PD_API int PDArgsSetSequenceStartPos(PD_Arguments args, /**
* @brief PDArgsSetSequenceStartPos Set sequence start position vector of one
* argument in array, which index is `ID`.
* @param args arguments array
* @param ID array index
* @param seqPos sequence position array.
* @return PD_Error
*/
PD_API PD_Error PDArgsSetSequenceStartPos(PD_Arguments args,
uint64_t ID, uint64_t ID,
PD_IVector seqPos); PD_IVector seqPos);
/**
PD_API int PDArgsGetSequenceStartPos(PD_Arguments args, * @brief PDArgsGetSequenceStartPos Get sequence start position vector of one
* argument in array, which index is `ID`.
* @param [in] args arguments array
* @param [in] ID array index
* @param [out] seqPos sequence position array
* @return PD_Error
*/
PD_API PD_Error PDArgsGetSequenceStartPos(PD_Arguments args,
uint64_t ID, uint64_t ID,
PD_IVector seqPos); PD_IVector seqPos);
PD_API int PDArgsSetSubSequenceStartPos(PD_Arguments args, /**
* @brief PDArgsSetSubSequenceStartPos Set sub-sequence start position vector of
* one argument in array, which index is `ID`.
* @param args arguments array
* @param ID array index
* @param subSeqPos sub-sequence start position array.
* @return PD_Error
*/
PD_API PD_Error PDArgsSetSubSequenceStartPos(PD_Arguments args,
uint64_t ID, uint64_t ID,
PD_IVector subSeqPos); PD_IVector subSeqPos);
PD_API int PDArgsGetSubSequenceStartPos(PD_Arguments args, /**
* @brief PDArgsGetSubSequenceStartPos Get sub-sequence start position vector of
* one argument in array, which index is `ID`.
* @param args arguments array
* @param ID array index
* @param subSeqPos sub-sequence start position array
* @return PD_Error
*/
PD_API PD_Error PDArgsGetSubSequenceStartPos(PD_Arguments args,
uint64_t ID, uint64_t ID,
PD_IVector subSeqPos); PD_IVector subSeqPos);
/** /**
...@@ -134,29 +285,65 @@ PD_API int PDArgsGetSubSequenceStartPos(PD_Arguments args, ...@@ -134,29 +285,65 @@ PD_API int PDArgsGetSubSequenceStartPos(PD_Arguments args,
*/ */
typedef void* PD_GradientMachine; typedef void* PD_GradientMachine;
PD_API int PDGradientMachineCreateForPredict(PD_GradientMachine* machine, /**
* @brief PDGradientMachineCreateForPredict Create a gradient machine used for
* model inference.
* @param [out] machine that used for model inference.
* @param [in] modelConfigProtobuf
* @param [in] size
* @return PD_Error
*/
PD_API PD_Error PDGradientMachineCreateForPredict(PD_GradientMachine* machine,
void* modelConfigProtobuf, void* modelConfigProtobuf,
int size); int size);
PD_API int PDGradientMachineLoadParameterFromDisk(PD_GradientMachine machine, /**
const char* path); * @brief PDGradientMachineLoadParameterFromDisk Load parameter from disk.
* @param machine Gradient Machine.
* @param path local directory path.
* @return PD_Error
*/
PD_API PD_Error PDGradientMachineLoadParameterFromDisk(
PD_GradientMachine machine, const char* path);
PD_API int PDGradientMachineForward(PD_GradientMachine machine, /**
* @brief PDGradientMachineForward Forward a gradient machine
* @param machine Gradient machine
* @param inArgs input arguments
* @param outArgs output arguments
* @param isTrain is train or not
* @return PD_Error
*/
PD_API PD_Error PDGradientMachineForward(PD_GradientMachine machine,
PD_Arguments inArgs, PD_Arguments inArgs,
PD_Arguments outArgs, PD_Arguments outArgs,
bool isTrain); bool isTrain);
PD_API int PDGradientMachineCreateSharedParam(PD_GradientMachine origin, /**
* @brief PDGradientMachineCreateSharedParam Create a gradient machine, which
* parameters are shared from another gradient machine.
* @param [in] origin gradient machine
* @param [in] modelConfigProtobuf model config protobuf
* @param [in] size of model config buffer.
* @param [out] slave gradient machine, the output value.
* @return PD_Error
*/
PD_API PD_Error PDGradientMachineCreateSharedParam(PD_GradientMachine origin,
void* modelConfigProtobuf, void* modelConfigProtobuf,
int size, int size,
PD_GradientMachine* slave); PD_GradientMachine* slave);
PD_API int PDGradientMachineDestroy(PD_GradientMachine machine); /**
* @brief PDGradientMachineDestroy Destroy a gradient machine
* @param machine that need to destroy
* @return PD_Error
*/
PD_API PD_Error PDGradientMachineDestroy(PD_GradientMachine machine);
/** /**
* Initialize Paddle. * Initialize Paddle.
*/ */
PD_API int PDInit(int argc, char** argv); PD_API PD_Error PDInit(int argc, char** argv);
#ifdef __cplusplus #ifdef __cplusplus
} }
......
...@@ -19,33 +19,26 @@ using paddle::capi::cast; ...@@ -19,33 +19,26 @@ using paddle::capi::cast;
extern "C" { extern "C" {
int PDIVecCreateNone(PD_IVector* ivec) { PD_IVector PDIVecCreateNone() { return new paddle::capi::CIVector(); }
if (ivec == nullptr) return kPD_NULLPTR;
auto ptr = new paddle::capi::CIVector();
*ivec = ptr;
return kPD_NO_ERROR;
}
int PDIVectorCreate(PD_IVector* ivec, int* array, uint64_t size, bool copy) { PD_IVector PDIVectorCreate(int* array, uint64_t size, bool copy, bool useGPU) {
if (ivec == nullptr) return kPD_NULLPTR;
auto ptr = new paddle::capi::CIVector(); auto ptr = new paddle::capi::CIVector();
if (copy) { if (copy) {
ptr->vec = paddle::IVector::create(size, false); ptr->vec = paddle::IVector::create(size, useGPU);
ptr->vec->copyFrom(array, size); ptr->vec->copyFrom(array, size);
} else { } else {
ptr->vec = paddle::IVector::create(array, size, false); ptr->vec = paddle::IVector::create(array, size, useGPU);
} }
*ivec = ptr; return ptr;
return kPD_NO_ERROR;
} }
int PDIVecDestroy(PD_IVector ivec) { PD_Error PDIVecDestroy(PD_IVector ivec) {
if (ivec == nullptr) return kPD_NULLPTR; if (ivec == nullptr) return kPD_NULLPTR;
delete cast<paddle::capi::CIVector>(ivec); delete cast<paddle::capi::CIVector>(ivec);
return kPD_NO_ERROR; return kPD_NO_ERROR;
} }
int PDIVectorGet(PD_IVector ivec, int** buffer) { PD_Error PDIVectorGet(PD_IVector ivec, int** buffer) {
if (ivec == nullptr || buffer == nullptr) return kPD_NULLPTR; if (ivec == nullptr || buffer == nullptr) return kPD_NULLPTR;
auto v = cast<paddle::capi::CIVector>(ivec); auto v = cast<paddle::capi::CIVector>(ivec);
if (v->vec == nullptr) return kPD_NULLPTR; if (v->vec == nullptr) return kPD_NULLPTR;
...@@ -53,7 +46,7 @@ int PDIVectorGet(PD_IVector ivec, int** buffer) { ...@@ -53,7 +46,7 @@ int PDIVectorGet(PD_IVector ivec, int** buffer) {
return kPD_NO_ERROR; return kPD_NO_ERROR;
} }
int PDIVectorResize(PD_IVector ivec, uint64_t size) { PD_Error PDIVectorResize(PD_IVector ivec, uint64_t size) {
if (ivec == nullptr) return kPD_NULLPTR; if (ivec == nullptr) return kPD_NULLPTR;
auto v = cast<paddle::capi::CIVector>(ivec); auto v = cast<paddle::capi::CIVector>(ivec);
if (v->vec == nullptr) return kPD_NULLPTR; if (v->vec == nullptr) return kPD_NULLPTR;
...@@ -61,7 +54,7 @@ int PDIVectorResize(PD_IVector ivec, uint64_t size) { ...@@ -61,7 +54,7 @@ int PDIVectorResize(PD_IVector ivec, uint64_t size) {
return kPD_NO_ERROR; return kPD_NO_ERROR;
} }
int PDIVectorGetSize(PD_IVector ivec, uint64_t* size) { PD_Error PDIVectorGetSize(PD_IVector ivec, uint64_t* size) {
if (ivec == nullptr) return kPD_NULLPTR; if (ivec == nullptr) return kPD_NULLPTR;
auto v = cast<paddle::capi::CIVector>(ivec); auto v = cast<paddle::capi::CIVector>(ivec);
if (v->vec == nullptr) return kPD_NULLPTR; if (v->vec == nullptr) return kPD_NULLPTR;
......
...@@ -28,8 +28,7 @@ static std::vector<pd_real> randomBuffer(size_t bufSize) { ...@@ -28,8 +28,7 @@ static std::vector<pd_real> randomBuffer(size_t bufSize) {
} }
TEST(CAPIArguments, create) { TEST(CAPIArguments, create) {
PD_Arguments args; PD_Arguments args = PDArgsCreateNone();
ASSERT_EQ(kPD_NO_ERROR, PDArgsCreateNone(&args));
uint64_t size; uint64_t size;
ASSERT_EQ(kPD_NO_ERROR, PDArgsGetSize(args, &size)); ASSERT_EQ(kPD_NO_ERROR, PDArgsGetSize(args, &size));
ASSERT_EQ(0UL, size); ASSERT_EQ(0UL, size);
...@@ -37,20 +36,17 @@ TEST(CAPIArguments, create) { ...@@ -37,20 +36,17 @@ TEST(CAPIArguments, create) {
} }
TEST(CAPIArguments, value) { TEST(CAPIArguments, value) {
PD_Arguments args; PD_Arguments args = PDArgsCreateNone();
ASSERT_EQ(kPD_NO_ERROR, PDArgsCreateNone(&args));
ASSERT_EQ(kPD_NO_ERROR, PDArgsResize(args, 1)); ASSERT_EQ(kPD_NO_ERROR, PDArgsResize(args, 1));
PD_Matrix mat; PD_Matrix mat = PDMatCreate(128, 64, false);
ASSERT_EQ(kPD_NO_ERROR, PDMatCreate(&mat, 128, 64, false));
for (size_t i = 0; i < 128; ++i) { for (size_t i = 0; i < 128; ++i) {
std::vector<pd_real> sampleBuf = randomBuffer(64); std::vector<pd_real> sampleBuf = randomBuffer(64);
PDMatCopyToRow(mat, i, sampleBuf.data()); PDMatCopyToRow(mat, i, sampleBuf.data());
} }
ASSERT_EQ(kPD_NO_ERROR, PDArgsSetValue(args, 0, mat)); ASSERT_EQ(kPD_NO_ERROR, PDArgsSetValue(args, 0, mat));
PD_Matrix val; PD_Matrix val = PDMatCreateNone();
ASSERT_EQ(kPD_NO_ERROR, PDMatCreateNone(&val));
ASSERT_EQ(kPD_NO_ERROR, PDArgsGetValue(args, 0, val)); ASSERT_EQ(kPD_NO_ERROR, PDArgsGetValue(args, 0, val));
...@@ -63,8 +59,7 @@ TEST(CAPIArguments, value) { ...@@ -63,8 +59,7 @@ TEST(CAPIArguments, value) {
ASSERT_EQ(row1, row2); ASSERT_EQ(row1, row2);
} }
PD_IVector ivec; PD_IVector ivec = PDIVecCreateNone();
ASSERT_EQ(kPD_NO_ERROR, PDIVecCreateNone(&ivec));
ASSERT_EQ(kPD_NO_ERROR, PDIVecDestroy(ivec)); ASSERT_EQ(kPD_NO_ERROR, PDIVecDestroy(ivec));
ASSERT_EQ(kPD_NO_ERROR, PDMatDestroy(val)); ASSERT_EQ(kPD_NO_ERROR, PDMatDestroy(val));
ASSERT_EQ(kPD_NO_ERROR, PDMatDestroy(mat)); ASSERT_EQ(kPD_NO_ERROR, PDMatDestroy(mat));
...@@ -72,17 +67,15 @@ TEST(CAPIArguments, value) { ...@@ -72,17 +67,15 @@ TEST(CAPIArguments, value) {
} }
TEST(CAPIArguments, ids) { TEST(CAPIArguments, ids) {
PD_Arguments args; PD_Arguments args = PDArgsCreateNone();
ASSERT_EQ(kPD_NO_ERROR, PDArgsCreateNone(&args));
ASSERT_EQ(kPD_NO_ERROR, PDArgsResize(args, 1)); ASSERT_EQ(kPD_NO_ERROR, PDArgsResize(args, 1));
PD_IVector ivec; PD_IVector ivec;
int array[3] = {1, 2, 3}; int array[3] = {1, 2, 3};
ASSERT_EQ(kPD_NO_ERROR, PDIVectorCreate(&ivec, array, 3, true)); ivec = PDIVectorCreate(array, 3, true, false);
ASSERT_EQ(kPD_NO_ERROR, PDArgsSetIds(args, 0, ivec)); ASSERT_EQ(kPD_NO_ERROR, PDArgsSetIds(args, 0, ivec));
PD_IVector val; PD_IVector val = PDIVecCreateNone();
ASSERT_EQ(kPD_NO_ERROR, PDIVecCreateNone(&val));
ASSERT_EQ(kPD_NO_ERROR, PDArgsGetIds(args, 0, val)); ASSERT_EQ(kPD_NO_ERROR, PDArgsGetIds(args, 0, val));
ASSERT_EQ(kPD_NO_ERROR, PDIVecDestroy(ivec)); ASSERT_EQ(kPD_NO_ERROR, PDIVecDestroy(ivec));
ASSERT_EQ(kPD_NO_ERROR, PDIVecDestroy(val)); ASSERT_EQ(kPD_NO_ERROR, PDIVecDestroy(val));
...@@ -91,17 +84,15 @@ TEST(CAPIArguments, ids) { ...@@ -91,17 +84,15 @@ TEST(CAPIArguments, ids) {
template <typename T1, typename T2> template <typename T1, typename T2>
void testSequenceHelper(T1 setter, T2 getter) { void testSequenceHelper(T1 setter, T2 getter) {
PD_Arguments args; PD_Arguments args = PDArgsCreateNone();
ASSERT_EQ(kPD_NO_ERROR, PDArgsCreateNone(&args));
ASSERT_EQ(kPD_NO_ERROR, PDArgsResize(args, 1)); ASSERT_EQ(kPD_NO_ERROR, PDArgsResize(args, 1));
PD_IVector ivec; PD_IVector ivec;
int array[3] = {1, 2, 3}; int array[3] = {1, 2, 3};
ASSERT_EQ(kPD_NO_ERROR, PDIVectorCreate(&ivec, array, 3, true)); ivec = PDIVectorCreate(array, 3, true, false);
ASSERT_EQ(kPD_NO_ERROR, setter(args, 0, ivec)); ASSERT_EQ(kPD_NO_ERROR, setter(args, 0, ivec));
PD_IVector val; PD_IVector val = PDIVecCreateNone();
ASSERT_EQ(kPD_NO_ERROR, PDIVecCreateNone(&val));
ASSERT_EQ(kPD_NO_ERROR, getter(args, 0, val)); ASSERT_EQ(kPD_NO_ERROR, getter(args, 0, val));
uint64_t size; uint64_t size;
ASSERT_EQ(kPD_NO_ERROR, PDIVectorGetSize(val, &size)); ASSERT_EQ(kPD_NO_ERROR, PDIVectorGetSize(val, &size));
......
...@@ -55,14 +55,11 @@ TEST(GradientMachine, testPredict) { ...@@ -55,14 +55,11 @@ TEST(GradientMachine, testPredict) {
PDGradientMachineCreateSharedParam( PDGradientMachineCreateSharedParam(
machine, &buffer[0], (int)buffer.size(), &machineSlave)); machine, &buffer[0], (int)buffer.size(), &machineSlave));
std::swap(machineSlave, machine); std::swap(machineSlave, machine);
PD_Arguments outArgs; PD_Arguments outArgs = PDArgsCreateNone();
ASSERT_EQ(kPD_NO_ERROR, PDArgsCreateNone(&outArgs));
PD_Arguments inArgs; PD_Arguments inArgs = PDArgsCreateNone();
ASSERT_EQ(kPD_NO_ERROR, PDArgsCreateNone(&inArgs));
ASSERT_EQ(kPD_NO_ERROR, PDArgsResize(inArgs, 1)); ASSERT_EQ(kPD_NO_ERROR, PDArgsResize(inArgs, 1));
PD_Matrix mat; PD_Matrix mat = PDMatCreate(1, 100, false);
ASSERT_EQ(kPD_NO_ERROR, PDMatCreate(&mat, 1, 100, false));
static_assert(std::is_same<pd_real, paddle::real>::value, ""); static_assert(std::is_same<pd_real, paddle::real>::value, "");
auto data = randomBuffer(100); auto data = randomBuffer(100);
......
...@@ -16,8 +16,7 @@ limitations under the License. */ ...@@ -16,8 +16,7 @@ limitations under the License. */
#include "gtest/gtest.h" #include "gtest/gtest.h"
TEST(CAPIMatrix, create) { TEST(CAPIMatrix, create) {
PD_Matrix mat; PD_Matrix mat = PDMatCreate(128, 32, false);
ASSERT_EQ(kPD_NO_ERROR, PDMatCreate(&mat, 128, 32, false));
std::vector<pd_real> sampleRow; std::vector<pd_real> sampleRow;
sampleRow.resize(32); sampleRow.resize(32);
for (size_t i = 0; i < sampleRow.size(); ++i) { for (size_t i = 0; i < sampleRow.size(); ++i) {
...@@ -41,7 +40,6 @@ TEST(CAPIMatrix, create) { ...@@ -41,7 +40,6 @@ TEST(CAPIMatrix, create) {
} }
TEST(CAPIMatrix, createNone) { TEST(CAPIMatrix, createNone) {
PD_Matrix mat; PD_Matrix mat = PDMatCreateNone();
ASSERT_EQ(kPD_NO_ERROR, PDMatCreateNone(&mat));
ASSERT_EQ(kPD_NO_ERROR, PDMatDestroy(mat)); ASSERT_EQ(kPD_NO_ERROR, PDMatDestroy(mat));
} }
...@@ -18,8 +18,7 @@ limitations under the License. */ ...@@ -18,8 +18,7 @@ limitations under the License. */
TEST(CAPIVector, create) { TEST(CAPIVector, create) {
PD_IVector vec; PD_IVector vec;
int array[3] = {1, 2, 3}; int array[3] = {1, 2, 3};
ASSERT_EQ(kPD_NO_ERROR, PDIVectorCreate(&vec, array, 3, true)); vec = PDIVectorCreate(array, 3, true, false);
ASSERT_EQ(kPD_NO_ERROR, PDIVectorCreate(&vec, array, 3, false));
ASSERT_EQ(kPD_NO_ERROR, PDIVectorResize(vec, 1000)); ASSERT_EQ(kPD_NO_ERROR, PDIVectorResize(vec, 1000));
uint64_t size; uint64_t size;
ASSERT_EQ(kPD_NO_ERROR, PDIVectorGetSize(vec, &size)); ASSERT_EQ(kPD_NO_ERROR, PDIVectorGetSize(vec, &size));
...@@ -27,7 +26,6 @@ TEST(CAPIVector, create) { ...@@ -27,7 +26,6 @@ TEST(CAPIVector, create) {
} }
TEST(CAPIVector, createNone) { TEST(CAPIVector, createNone) {
PD_IVector vec; PD_IVector vec = PDIVecCreateNone();
ASSERT_EQ(kPD_NO_ERROR, PDIVecCreateNone(&vec));
ASSERT_EQ(kPD_NO_ERROR, PDIVecDestroy(vec)); ASSERT_EQ(kPD_NO_ERROR, PDIVecDestroy(vec));
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册