Skip to content

Commit aa6e252

Browse files
committed
Doing C-API
1 parent fbb1b0e commit aa6e252

15 files changed

+169
-160
lines changed

paddle/capi/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ target_link_libraries(paddle_capi
3535
${CBLAS_LIBS}
3636
${ZLIB_LIBRARIES}
3737
${INTERAL_LIBS}
38-
${CMAKE_DL_LIBS})
38+
${CMAKE_DL_LIBS}
39+
${PYTHON_LIBRARIES})
3940

4041

4142
set(PADDLE_CAPI_INC_PATH

paddle/capi/GradientMachine.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include "PaddleCAPI.h"
2+
#include "PaddleCAPIPrivate.h"
3+
#include "paddle/gserver/gradientmachines/NeuralNetwork.h"
4+
5+
#define cast(v) paddle::capi::cast<paddle::capi::CGradientMachine>(v)
6+
7+
enum GradientMatchineCreateMode {
8+
CREATE_MODE_NORMAL = 0,
9+
CREATE_MODE_TESTING = 4
10+
};
11+
12+
namespace paddle {
13+
14+
class MyNeuralNetwork : public NeuralNetwork {
15+
public:
16+
MyNeuralNetwork(const std::string& name, NeuralNetwork* network)
17+
: NeuralNetwork(name, network) {}
18+
};
19+
20+
NeuralNetwork* newCustomNerualNetwork(const std::string& name,
21+
NeuralNetwork* network) {
22+
return new MyNeuralNetwork(name, network);
23+
}
24+
}
25+
26+
extern "C" {
27+
int PDGradientMachineCreateForPredict(PD_GradiemtMachine* machine,
28+
void* modelConfigProtobuf,
29+
int size) {
30+
if (modelConfigProtobuf == nullptr) return PD_NULLPTR;
31+
paddle::ModelConfig config;
32+
if (!config.ParseFromArray(modelConfigProtobuf, size) ||
33+
!config.IsInitialized()) {
34+
return PD_PROTOBUF_ERROR;
35+
}
36+
37+
auto ptr = new paddle::capi::CGradientMachine();
38+
ptr->machine.reset(paddle::GradientMachine::create(
39+
config, CREATE_MODE_TESTING, {paddle::PARAMETER_VALUE}));
40+
*machine = ptr;
41+
return PD_NO_ERROR;
42+
}
43+
44+
int PDGradientMachineDestroy(PD_GradiemtMachine machine) {
45+
delete cast(machine);
46+
return PD_NO_ERROR;
47+
}
48+
}

paddle/capi/Main.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <fenv.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#include <vector>
5+
#include "PaddleCAPI.h"
6+
#include "PaddleCAPIPrivate.h"
7+
#include "paddle/trainer/TrainerConfigHelper.h"
8+
#include "paddle/utils/Excepts.h"
9+
#include "paddle/utils/PythonUtil.h"
10+
11+
static void initPaddle(int argc, char** argv) {
12+
paddle::initMain(argc, argv);
13+
paddle::initPython(argc, argv);
14+
feenableexcept(FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW);
15+
}
16+
17+
extern "C" {
18+
int PDInit(int argc, char** argv) {
19+
std::vector<char*> realArgv;
20+
realArgv.reserve(argc + 1);
21+
realArgv.push_back(strdup(""));
22+
for (int i = 0; i < argc; ++i) {
23+
realArgv.push_back(argv[i]);
24+
}
25+
initPaddle(argc + 1, realArgv.data());
26+
free(realArgv[0]);
27+
return PD_NO_ERROR;
28+
}
29+
30+
int PDParseTrainerConfigFromFile(char* filename,
31+
void** modelConfigProtobuf,
32+
int* size) {
33+
if (filename == nullptr || modelConfigProtobuf == nullptr || size == nullptr)
34+
return PD_NULLPTR;
35+
paddle::TrainerConfigHelper conf(filename);
36+
if (!conf.getConfig().IsInitialized()) return PD_PROTOBUF_ERROR;
37+
*size = conf.getConfig().ByteSize();
38+
*modelConfigProtobuf = malloc(*size);
39+
if (!conf.getConfig().SerializeToArray(*modelConfigProtobuf, *size))
40+
return PD_PROTOBUF_ERROR;
41+
return PD_NO_ERROR;
42+
}
43+
}

paddle/capi/PaddleCAPI.h

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77
extern "C" {
88
#endif
99

10-
#define PD_NO_ERROR 0
11-
#define PD_NULLPTR 1
12-
#define PD_OUT_OF_RANGE 2
13-
#define PD_UNDEFINED_ERROR -1
10+
typedef enum {
11+
PD_NO_ERROR = 0,
12+
PD_NULLPTR = 1,
13+
PD_OUT_OF_RANGE = 2,
14+
PD_PROTOBUF_ERROR = 3,
15+
PD_UNDEFINED_ERROR = -1,
16+
} PD_Error;
1417

1518
typedef void* PD_Vector;
1619

@@ -48,6 +51,20 @@ int PDArgsSetValue(PD_Arguments args, uint64_t ID, PD_Matrix mat);
4851

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

54+
typedef void* PD_GradiemtMachine;
55+
56+
int PDGradientMachineCreateForPredict(PD_GradiemtMachine* machine,
57+
void* modelConfigProtobuf,
58+
int size);
59+
60+
int PDGradientMachineDestroy(PD_GradiemtMachine machine);
61+
62+
int PDInit(int argc, char** argv);
63+
64+
int PDParseTrainerConfigFromFile(char* filename,
65+
void** modelConfigProtobuf,
66+
int* size);
67+
5168
#ifdef __cplusplus
5269
}
5370
#endif

paddle/capi/PaddleCAPIPrivate.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "PaddleCAPI.h"
2+
#include "paddle/gserver/gradientmachines/GradientMachine.h"
23
#include "paddle/math/Matrix.h"
34
#include "paddle/math/Vector.h"
45
#include "paddle/parameter/Argument.h"
@@ -19,6 +20,10 @@ struct CArguments {
1920
std::vector<paddle::Argument> args;
2021
};
2122

23+
struct CGradientMachine {
24+
paddle::GradientMachinePtr machine;
25+
};
26+
2227
template <typename T>
2328
inline T* cast(void* ptr) {
2429
return reinterpret_cast<T*>(ptr);

paddle/capi/tests/CMakeLists.txt

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,36 @@
1-
function(add_capi_unittest TARGET_NAME)
1+
function(add_capi_unittest_without_exec TARGET_NAME)
2+
set(with_test_main ON)
3+
set(sources)
4+
foreach(source_file ${ARGN})
5+
if (${source_file} STREQUAL "NO_MAIN")
6+
set(with_test_main OFF)
7+
else()
8+
list(APPEND sources ${source_file})
9+
endif()
10+
endforeach()
11+
212
add_executable(
313
${TARGET_NAME}
4-
${ARGN})
14+
${sources})
15+
16+
517
target_link_libraries(
618
${TARGET_NAME}
719
paddle_capi
8-
paddle_test_main
920
${GTEST_LIBRARIES})
21+
22+
if (with_test_main)
23+
target_link_libraries(
24+
${TARGET_NAME} paddle_test_main)
25+
endif()
1026
target_include_directories(${TARGET_NAME} PUBLIC ${PADDLE_CAPI_INC_PATH})
11-
add_test(NAME ${TARGET_NAME} COMMAND ${TARGET_NAME})
1227
endfunction()
1328

29+
function(add_capi_unittest TARGET_NAME)
30+
add_capi_unittest_without_exec(${TARGET_NAME} ${ARGN})
31+
add_test(NAME ${TARGET_NAME} COMMAND ${TARGET_NAME})
32+
endfunction()
1433
add_capi_unittest(capi_test_mats test_Vector.cpp
1534
test_Matrix.cpp test_Arguments.cpp)
35+
36+
add_capi_unittest(capi_test_gradientMachine NO_MAIN test_GradientMachine.cpp)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <gtest/gtest.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#include "PaddleCAPI.h"
5+
6+
TEST(GradientMachine, load) {
7+
void* buf;
8+
int size;
9+
ASSERT_EQ(
10+
PD_NO_ERROR,
11+
PDParseTrainerConfigFromFile(strdup("./vgg_16_cifar.py"), &buf, &size));
12+
free(buf);
13+
}
14+
15+
int main(int argc, char** argv) {
16+
testing::InitGoogleTest(&argc, argv);
17+
std::vector<char*> argvs;
18+
argvs.push_back(strdup("--use_gpu=false"));
19+
PDInit((int)argvs.size(), argvs.data());
20+
for (auto each : argvs) {
21+
free(each);
22+
}
23+
return RUN_ALL_TESTS();
24+
}

paddle/capi/tests/test_Init.cpp

Whitespace-only changes.

paddle/capi/tests/vgg_16_cifar.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../demo/image_classification/vgg_16_cifar.py

paddle/utils/ForceLink.h

Lines changed: 0 additions & 46 deletions
This file was deleted.

0 commit comments

Comments
 (0)