提交 c5eac0ab 编写于 作者: Y Yu Yang

Rename API

上级 0afd5c30
......@@ -12,8 +12,8 @@ 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 "PaddleCAPI.h"
#include "PaddleCAPIPrivate.h"
#include "arguments.h"
using paddle::capi::cast;
......@@ -92,50 +92,26 @@ paddle_error paddle_arguments_set_ids(paddle_arguments args,
paddle_error paddle_arguments_set_sequence_start_pos(paddle_arguments args,
uint64_t ID,
uint32_t nestedLevel,
paddle_ivector seqPos) {
if (args == nullptr || seqPos == nullptr) return kPD_NULLPTR;
auto iv = paddle::capi::cast<paddle::capi::CIVector>(seqPos);
if (iv->vec == nullptr) return kPD_NULLPTR;
auto a = castArg(args);
if (ID >= a->args.size()) return kPD_OUT_OF_RANGE;
a->args[ID].sequenceStartPositions =
std::make_shared<paddle::ICpuGpuVector>(iv->vec);
return kPD_NO_ERROR;
}
paddle_error paddle_arguments_set_sub_sequence_start_pos(
paddle_arguments args, uint64_t ID, paddle_ivector subSeqPos) {
if (args == nullptr || subSeqPos == nullptr) return kPD_NULLPTR;
auto iv = paddle::capi::cast<paddle::capi::CIVector>(subSeqPos);
if (iv->vec == nullptr) return kPD_NULLPTR;
auto a = castArg(args);
if (ID >= a->args.size()) return kPD_OUT_OF_RANGE;
a->args[ID].subSequenceStartPositions =
std::make_shared<paddle::ICpuGpuVector>(iv->vec);
return kPD_NO_ERROR;
return a->accessSeqPos(ID, nestedLevel, [&iv](paddle::ICpuGpuVectorPtr& ptr) {
ptr = std::make_shared<paddle::ICpuGpuVector>(iv->vec);
});
}
paddle_error paddle_arguments_sequence_start_pos(paddle_arguments args,
uint64_t ID,
uint32_t nestedLevel,
paddle_ivector seqPos) {
if (args == nullptr || seqPos == nullptr) return kPD_NULLPTR;
auto iv = castIVec(seqPos);
auto a = castArg(args);
if (ID >= a->args.size()) return kPD_OUT_OF_RANGE;
paddle::Argument& arg = a->args[ID];
iv->vec = arg.sequenceStartPositions->getMutableVector(false);
return kPD_NO_ERROR;
}
paddle_error paddle_arguments_sub_sequence_start_pos(paddle_arguments args,
uint64_t ID,
paddle_ivector subSeqPos) {
if (args == nullptr || subSeqPos == nullptr) return kPD_NULLPTR;
auto iv = castIVec(subSeqPos);
auto iv = paddle::capi::cast<paddle::capi::CIVector>(seqPos);
auto a = castArg(args);
if (ID >= a->args.size()) return kPD_OUT_OF_RANGE;
paddle::Argument& arg = a->args[ID];
iv->vec = arg.subSequenceStartPositions->getMutableVector(false);
return kPD_NO_ERROR;
return a->accessSeqPos(ID, nestedLevel, [&iv](paddle::ICpuGpuVectorPtr& ptr) {
iv->vec = ptr->getMutableVector(false);
});
}
}
......@@ -29,7 +29,7 @@ static void initPaddle(int argc, char** argv) {
}
extern "C" {
paddle_error PDInit(int argc, char** argv) {
paddle_error paddle_init(int argc, char** argv) {
std::vector<char*> realArgv;
realArgv.reserve(argc + 1);
realArgv.push_back(strdup(""));
......
......@@ -14,16 +14,6 @@ limitations under the License. */
#ifndef PADDLECAPI_H_
#define PADDLECAPI_H_
#include <stdbool.h>
#include <stdint.h>
#include "config.h"
#include "error.h"
#include "matrix.h"
#include "vector.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* Paddle C API. It will replace SWIG as Multiple Language API for model
......@@ -31,203 +21,12 @@ extern "C" {
*
* NOTE: This is an experimental API, it could be changed.
*/
/**
* Arguments functions. Each argument means layer output. Arguments means a
* array of arguemnt.
*/
typedef void* paddle_arguments;
/**
* @brief paddle_arguments_create_none Create a array of arguments, which size
* is zero.
* @return Arguemnts
*/
PD_API paddle_arguments paddle_arguments_create_none();
/**
* @brief paddle_arguments_destroy Destroy the arguments
* @param args arguments to destroy
* @return paddle_error
*/
PD_API paddle_error paddle_arguments_destroy(paddle_arguments args);
/**
* @brief PDArgsGetSize Get size of arguments array
* @param [in] args arguments array
* @param [out] size array size
* @return paddle_error
*/
PD_API paddle_error paddle_arguments_size(paddle_arguments args,
uint64_t* size);
/**
* @brief PDArgsResize Resize a arguments array.
* @param args arguments array.
* @param size target size of array
* @return paddle_error
*/
PD_API paddle_error paddle_arguments_resize(paddle_arguments args,
uint64_t size);
/**
* @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 paddle_error
*/
PD_API paddle_error paddle_arguments_set_value(paddle_arguments args,
uint64_t ID,
paddle_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 paddle_error
*/
PD_API paddle_error paddle_arguments_value(paddle_arguments args,
uint64_t ID,
paddle_matrix mat);
/**
* @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 paddle_error
*/
PD_API paddle_error paddle_arguments_ids(paddle_arguments args,
uint64_t ID,
paddle_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 paddle_error
*/
PD_API paddle_error paddle_arguments_set_ids(paddle_arguments args,
uint64_t ID,
paddle_ivector ids);
/**
* @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 paddle_error
*/
PD_API paddle_error paddle_arguments_set_sequence_start_pos(
paddle_arguments args, uint64_t ID, paddle_ivector seqPos);
/**
* @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 paddle_error
*/
PD_API paddle_error paddle_arguments_sequence_start_pos(paddle_arguments args,
uint64_t ID,
paddle_ivector seqPos);
/**
* @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 paddle_error
*/
PD_API paddle_error paddle_arguments_set_sub_sequence_start_pos(
paddle_arguments args, uint64_t ID, paddle_ivector subSeqPos);
/**
* @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 paddle_error
*/
PD_API paddle_error paddle_arguments_sub_sequence_start_pos(
paddle_arguments args, uint64_t ID, paddle_ivector subSeqPos);
/**
* @brief GradientMachine means a neural network.
*/
typedef void* PD_GradientMachine;
/**
* @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 paddle_error
*/
PD_API paddle_error PDGradientMachineCreateForPredict(
PD_GradientMachine* machine, void* modelConfigProtobuf, int size);
/**
* @brief PDGradientMachineLoadParameterFromDisk Load parameter from disk.
* @param machine Gradient Machine.
* @param path local directory path.
* @return paddle_error
*/
PD_API paddle_error PDGradientMachineLoadParameterFromDisk(
PD_GradientMachine machine, const char* path);
/**
* @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 paddle_error
*/
PD_API paddle_error PDGradientMachineForward(PD_GradientMachine machine,
paddle_arguments inArgs,
paddle_arguments outArgs,
bool isTrain);
/**
* @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 paddle_error
*/
PD_API paddle_error
PDGradientMachineCreateSharedParam(PD_GradientMachine origin,
void* modelConfigProtobuf,
int size,
PD_GradientMachine* slave);
/**
* @brief PDGradientMachineDestroy Destroy a gradient machine
* @param machine that need to destroy
* @return paddle_error
*/
PD_API paddle_error PDGradientMachineDestroy(PD_GradientMachine machine);
/**
* Initialize Paddle.
*/
PD_API paddle_error PDInit(int argc, char** argv);
#ifdef __cplusplus
}
#endif
#include "arguments.h"
#include "config.h"
#include "error.h"
#include "gradient_machine.h"
#include "main.h"
#include "matrix.h"
#include "vector.h"
#endif // PADDLECAPI_H_
......@@ -49,6 +49,22 @@ struct CArguments {
std::vector<paddle::Argument> args;
CArguments() : type(kARGUMENTS) {}
template <typename T>
paddle_error accessSeqPos(uint64_t ID, uint32_t nestedLevel, T callback) {
if (ID >= args.size()) return kPD_OUT_OF_RANGE;
switch (nestedLevel) {
case 0:
callback(args[ID].sequenceStartPositions);
break;
case 1:
callback(args[ID].subSequenceStartPositions);
break;
default:
return kPD_OUT_OF_RANGE;
}
return kPD_NO_ERROR;
}
};
struct CGradientMachine {
......
#ifndef __PADDLE_CAPI_ARGUMENTS_H__
#define __PADDLE_CAPI_ARGUMENTS_H__
#include <stdint.h>
#include "config.h"
#include "error.h"
#include "matrix.h"
#include "vector.h"
/**
* Arguments functions. Each argument means layer output. Arguments means a
* array of arguemnt.
*/
typedef void* paddle_arguments;
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief paddle_arguments_create_none Create a array of arguments, which size
* is zero.
* @return Arguemnts
*/
PD_API paddle_arguments paddle_arguments_create_none();
/**
* @brief paddle_arguments_destroy Destroy the arguments
* @param args arguments to destroy
* @return paddle_error
*/
PD_API paddle_error paddle_arguments_destroy(paddle_arguments args);
/**
* @brief PDArgsGetSize Get size of arguments array
* @param [in] args arguments array
* @param [out] size array size
* @return paddle_error
*/
PD_API paddle_error paddle_arguments_size(paddle_arguments args,
uint64_t* size);
/**
* @brief PDArgsResize Resize a arguments array.
* @param args arguments array.
* @param size target size of array
* @return paddle_error
*/
PD_API paddle_error paddle_arguments_resize(paddle_arguments args,
uint64_t size);
/**
* @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 paddle_error
*/
PD_API paddle_error paddle_arguments_set_value(paddle_arguments args,
uint64_t ID,
paddle_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 paddle_error
*/
PD_API paddle_error paddle_arguments_value(paddle_arguments args,
uint64_t ID,
paddle_matrix mat);
/**
* @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 paddle_error
*/
PD_API paddle_error paddle_arguments_ids(paddle_arguments args,
uint64_t ID,
paddle_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 paddle_error
*/
PD_API paddle_error paddle_arguments_set_ids(paddle_arguments args,
uint64_t ID,
paddle_ivector ids);
/**
* @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 paddle_error
*/
PD_API paddle_error
paddle_arguments_set_sequence_start_pos(paddle_arguments args,
uint64_t ID,
uint32_t nestedLevel,
paddle_ivector seqPos);
/**
* @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 paddle_error
*/
PD_API paddle_error paddle_arguments_sequence_start_pos(paddle_arguments args,
uint64_t ID,
uint32_t nestedLevel,
paddle_ivector seqPos);
#ifdef __cplusplus
}
#endif
#endif
/* 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. */
#ifndef __PADDLE_CAPI_ERROR_H__
#define __PADDLE_CAPI_ERROR_H__
......
......@@ -38,9 +38,8 @@ NeuralNetwork* newCustomNerualNetwork(const std::string& name,
} // namespace paddle
extern "C" {
paddle_error PDGradientMachineCreateForPredict(PD_GradientMachine* machine,
void* modelConfigProtobuf,
int size) {
paddle_error paddle_gradient_machine_create_for_inference(
paddle_gradient_machine* machine, void* modelConfigProtobuf, int size) {
if (modelConfigProtobuf == nullptr) return kPD_NULLPTR;
paddle::ModelConfig config;
if (!config.ParseFromArray(modelConfigProtobuf, size) ||
......@@ -55,13 +54,13 @@ paddle_error PDGradientMachineCreateForPredict(PD_GradientMachine* machine,
return kPD_NO_ERROR;
}
paddle_error PDGradientMachineDestroy(PD_GradientMachine machine) {
paddle_error paddle_gradient_machine_destroy(paddle_gradient_machine machine) {
delete cast(machine);
return kPD_NO_ERROR;
}
paddle_error PDGradientMachineLoadParameterFromDisk(PD_GradientMachine machine,
const char* path) {
paddle_error paddle_gradient_machine_load_parameter_from_disk(
paddle_gradient_machine machine, const char* path) {
auto m = cast(machine);
if (m == nullptr || path == nullptr || m->machine == nullptr)
return kPD_NULLPTR;
......@@ -69,10 +68,10 @@ paddle_error PDGradientMachineLoadParameterFromDisk(PD_GradientMachine machine,
return kPD_NO_ERROR;
}
paddle_error PDGradientMachineForward(PD_GradientMachine machine,
paddle_arguments inArgs,
paddle_arguments outArgs,
bool isTrain) {
paddle_error paddle_gradient_machine_forward(paddle_gradient_machine machine,
paddle_arguments inArgs,
paddle_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);
......@@ -83,10 +82,11 @@ paddle_error PDGradientMachineForward(PD_GradientMachine machine,
return kPD_NO_ERROR;
}
paddle_error PDGradientMachineCreateSharedParam(PD_GradientMachine origin,
void* modelConfigProtobuf,
int size,
PD_GradientMachine* slave) {
paddle_error paddle_gradient_machine_create_shared_param(
paddle_gradient_machine origin,
void* modelConfigProtobuf,
int size,
paddle_gradient_machine* slave) {
auto o = cast(origin);
if (origin == nullptr || slave == nullptr || o->machine == nullptr) {
return kPD_NULLPTR;
......
#ifndef __PADDLE_CAPI_GRADIENT_MACHINE_H__
#define __PADDLE_CAPI_GRADIENT_MACHINE_H__
#include "arguments.h"
#include "config.h"
#include "error.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief GradientMachine means a neural network.
*/
typedef void* paddle_gradient_machine;
/**
* @brief Create a gradient machine used for model inference.
* @param [out] machine that used for model inference.
* @param [in] modelConfigProtobuf
* @param [in] size
* @return paddle_error
*/
PD_API paddle_error paddle_gradient_machine_create_for_inference(
paddle_gradient_machine* machine, void* modelConfigProtobuf, int size);
/**
* @brief Load parameter from disk.
* @param machine Gradient Machine.
* @param path local directory path.
* @return paddle_error
*/
PD_API paddle_error paddle_gradient_machine_load_parameter_from_disk(
paddle_gradient_machine machine, const char* path);
/**
* @brief Forward a gradient machine
* @param machine Gradient machine
* @param inArgs input arguments
* @param outArgs output arguments
* @param isTrain is train or not
* @return paddle_error
*/
PD_API paddle_error
paddle_gradient_machine_forward(paddle_gradient_machine machine,
paddle_arguments inArgs,
paddle_arguments outArgs,
bool isTrain);
/**
* @brief 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 paddle_error
*/
PD_API paddle_error
paddle_gradient_machine_create_shared_param(paddle_gradient_machine origin,
void* modelConfigProtobuf,
int size,
paddle_gradient_machine* slave);
/**
* @brief Destroy a gradient machine
* @param machine that need to destroy
* @return paddle_error
*/
PD_API paddle_error
paddle_gradient_machine_destroy(paddle_gradient_machine machine);
#ifdef __cplusplus
}
#endif
#endif
#ifndef __PADDLE_CAPI_MAIN_H__
#define __PADDLE_CAPI_MAIN_H__
#include "config.h"
#include "error.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* Initialize Paddle.
*/
PD_API paddle_error paddle_init(int argc, char** argv);
#ifdef __cplusplus
}
#endif
#endif
......@@ -12,6 +12,7 @@ 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 <functional>
#include "PaddleCAPI.h"
#include "gtest/gtest.h"
#include "paddle/utils/ThreadLocal.h"
......@@ -109,8 +110,19 @@ void testSequenceHelper(T1 setter, T2 getter) {
}
TEST(CAPIArguments, Sequence) {
testSequenceHelper(paddle_arguments_set_sequence_start_pos,
paddle_arguments_sequence_start_pos);
testSequenceHelper(paddle_arguments_set_sub_sequence_start_pos,
paddle_arguments_sub_sequence_start_pos);
auto testSequence = [](uint32_t nestedLevel) {
testSequenceHelper(std::bind(paddle_arguments_set_sequence_start_pos,
std::placeholders::_1,
std::placeholders::_2,
nestedLevel,
std::placeholders::_3),
std::bind(paddle_arguments_sequence_start_pos,
std::placeholders::_1,
std::placeholders::_2,
nestedLevel,
std::placeholders::_3));
};
for (uint32_t i = 0; i < 2; ++i) { // test seq and sub-seq.
testSequence(i);
}
}
......@@ -36,10 +36,10 @@ TEST(GradientMachine, testPredict) {
paddle::TrainerConfigHelper config("./test_predict_network.py");
std::string buffer;
ASSERT_TRUE(config.getModelConfig().SerializeToString(&buffer));
PD_GradientMachine machine;
paddle_gradient_machine machine;
ASSERT_EQ(kPD_NO_ERROR,
PDGradientMachineCreateForPredict(
paddle_gradient_machine_create_for_inference(
&machine, &buffer[0], (int)buffer.size()));
std::unique_ptr<paddle::GradientMachine> gm(
paddle::GradientMachine::create(config.getModelConfig()));
......@@ -48,11 +48,11 @@ TEST(GradientMachine, testPredict) {
gm->saveParameters("./");
ASSERT_EQ(kPD_NO_ERROR,
PDGradientMachineLoadParameterFromDisk(machine, "./"));
paddle_gradient_machine_load_parameter_from_disk(machine, "./"));
PD_GradientMachine machineSlave;
paddle_gradient_machine machineSlave;
ASSERT_EQ(kPD_NO_ERROR,
PDGradientMachineCreateSharedParam(
paddle_gradient_machine_create_shared_param(
machine, &buffer[0], (int)buffer.size(), &machineSlave));
std::swap(machineSlave, machine);
paddle_arguments outArgs = paddle_arguments_create_none();
......@@ -69,7 +69,7 @@ TEST(GradientMachine, testPredict) {
ASSERT_EQ(kPD_NO_ERROR, paddle_arguments_set_value(inArgs, 0, mat));
ASSERT_EQ(kPD_NO_ERROR,
PDGradientMachineForward(machine, inArgs, outArgs, false));
paddle_gradient_machine_forward(machine, inArgs, outArgs, false));
uint64_t sz;
ASSERT_EQ(kPD_NO_ERROR, paddle_arguments_size(outArgs, &sz));
......@@ -100,15 +100,15 @@ TEST(GradientMachine, testPredict) {
ASSERT_EQ(kPD_NO_ERROR, paddle_arguments_destroy(inArgs));
ASSERT_EQ(kPD_NO_ERROR, paddle_arguments_destroy(outArgs));
std::swap(machineSlave, machine);
ASSERT_EQ(kPD_NO_ERROR, PDGradientMachineDestroy(machineSlave));
ASSERT_EQ(kPD_NO_ERROR, PDGradientMachineDestroy(machine));
ASSERT_EQ(kPD_NO_ERROR, paddle_gradient_machine_destroy(machineSlave));
ASSERT_EQ(kPD_NO_ERROR, paddle_gradient_machine_destroy(machine));
}
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());
paddle_init((int)argvs.size(), argvs.data());
for (auto each : argvs) {
free(each);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册