Skip to content

Commit 470bbcf

Browse files
committed
Add example
1 parent 9c1c19b commit 470bbcf

25 files changed

+550
-19
lines changed

paddle/capi/Matrix.cpp

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ paddle_error paddle_matrix_destroy(paddle_matrix mat) {
3939

4040
paddle_error paddle_matrix_set_row(paddle_matrix mat,
4141
uint64_t rowID,
42-
pd_real* rowArray) {
42+
paddle_real* rowArray) {
4343
if (mat == nullptr) return kPD_NULLPTR;
4444
auto ptr = cast(mat);
4545
if (ptr->mat == nullptr) return kPD_NULLPTR;
@@ -56,7 +56,7 @@ paddle_error paddle_matrix_set_row(paddle_matrix mat,
5656

5757
paddle_error paddle_matrix_get_row(paddle_matrix mat,
5858
uint64_t rowID,
59-
pd_real** rawRowBuffer) {
59+
paddle_real** rawRowBuffer) {
6060
if (mat == nullptr) return kPD_NULLPTR;
6161
auto ptr = cast(mat);
6262
if (ptr->mat == nullptr) return kPD_NULLPTR;
@@ -78,3 +78,46 @@ paddle_error paddle_matrix_get_shape(paddle_matrix mat,
7878
return kPD_NO_ERROR;
7979
}
8080
}
81+
82+
paddle_matrix paddle_matrix_create_sparse(
83+
uint64_t height, uint64_t width, uint64_t nnz, bool isBinary, bool useGpu) {
84+
auto ptr = new paddle::capi::CMatrix();
85+
ptr->mat = paddle::Matrix::createSparseMatrix(
86+
height,
87+
width,
88+
nnz,
89+
isBinary ? paddle::NO_VALUE : paddle::FLOAT_VALUE,
90+
paddle::SPARSE_CSR,
91+
false,
92+
useGpu);
93+
return ptr;
94+
}
95+
96+
paddle_error paddle_matrix_sparse_copy_from(paddle_matrix mat,
97+
int* rowArray,
98+
uint64_t rowSize,
99+
int* colArray,
100+
uint64_t colSize,
101+
float* valueArray,
102+
uint64_t valueSize) {
103+
if (mat == nullptr) return kPD_NULLPTR;
104+
auto ptr = cast(mat);
105+
if (rowArray == nullptr || colArray == nullptr ||
106+
(valueSize != 0 && valueArray == nullptr) || ptr->mat == nullptr) {
107+
return kPD_NULLPTR;
108+
}
109+
if (auto sparseMat = dynamic_cast<paddle::CpuSparseMatrix*>(ptr->mat.get())) {
110+
std::vector<int> row(rowSize);
111+
row.assign(rowArray, rowArray + rowSize);
112+
std::vector<int> col(colSize);
113+
col.assign(colArray, colArray + colSize);
114+
std::vector<paddle_real> val(valueSize);
115+
if (valueSize) {
116+
val.assign(valueArray, valueArray + valueSize);
117+
}
118+
sparseMat->copyFrom(row, col, val);
119+
return kPD_NO_ERROR;
120+
} else {
121+
return kPD_NOT_SUPPORTED;
122+
}
123+
}

paddle/capi/config.h.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef __PADDLE_PADDLE_CAPI_CONFIG_H_INCLUDED__
22
#define __PADDLE_PADDLE_CAPI_CONFIG_H_INCLUDED__
33

4-
typedef @PADDLE_FLOAT_TYPE@ pd_real;
4+
typedef @PADDLE_FLOAT_TYPE@ paddle_real;
55

66
// Since we only support linux and macos in compile, always use clang or
77
// gcc 4.8+. DLL_IMPORT/DLL_EXPORT is as simple as below.

paddle/capi/error.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ typedef enum {
2323
kPD_NULLPTR = 1,
2424
kPD_OUT_OF_RANGE = 2,
2525
kPD_PROTOBUF_ERROR = 3,
26+
kPD_NOT_SUPPORTED = 4,
2627
kPD_UNDEFINED_ERROR = -1,
2728
} paddle_error;
2829

paddle/capi/examples/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.bin
2+
build-*
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#ifndef __CAPI_EXAMPLE_COMMON_H__
2+
#define __CAPI_EXAMPLE_COMMON_H__
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
6+
#define CHECK(stmt) \
7+
do { \
8+
paddle_error __err__ = stmt; \
9+
if (__err__ != kPD_NO_ERROR) { \
10+
fprintf(stderr, "Invoke paddle error %d \n" #stmt, __err__); \
11+
exit(__err__); \
12+
} \
13+
} while (0)
14+
15+
void* read_config(const char* filename, long* size) {
16+
FILE* file = fopen(filename, "r");
17+
if (file == NULL) return NULL;
18+
fseek(file, 0L, SEEK_END);
19+
*size = ftell(file);
20+
fseek(file, 0L, SEEK_SET);
21+
void* buf = malloc(*size);
22+
fread(buf, 1, *size, file);
23+
fclose(file);
24+
return buf;
25+
}
26+
#endif
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
project(dense)
2+
cmake_minimum_required(VERSION 2.8)
3+
aux_source_directory(. SRC_LIST)
4+
add_executable(${PROJECT_NAME} ${SRC_LIST})
5+
set_property(TARGET ${PROJECT_NAME} PROPERTY C_STANDARD 99)
6+
target_link_libraries(${PROJECT_NAME} -lpaddle_capi_shared)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash
2+
python -m paddle.utils.dump_config trainer_config.py '' --binary > trainer_config.bin

paddle/capi/examples/dense/main.c

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include <paddle/capi.h>
2+
#include <time.h>
3+
#include "../common/common.h"
4+
5+
#define CONFIG_BIN "./trainer_config.bin"
6+
7+
int main() {
8+
// Initalize Paddle
9+
char* argv[] = {"--use_gpu=False"};
10+
CHECK(paddle_init(1, (char**)argv));
11+
12+
// Reading config binary file. It is generated by `convert_protobin.sh`
13+
long size;
14+
void* buf = read_config(CONFIG_BIN, &size);
15+
16+
// Create a gradient machine for inference.
17+
paddle_gradient_machine machine;
18+
CHECK(paddle_gradient_machine_create_for_inference(&machine, buf, (int)size));
19+
CHECK(paddle_gradient_machine_randomize_param(machine));
20+
21+
// Loading parameter. Uncomment the following line and change the directory.
22+
// CHECK(paddle_gradient_machine_load_parameter_from_disk(machine,
23+
// "./some_where_to_params"));
24+
paddle_arguments in_args = paddle_arguments_create_none();
25+
26+
// There is only one input of this network.
27+
CHECK(paddle_arguments_resize(in_args, 1));
28+
29+
// Create input matrix.
30+
paddle_matrix mat = paddle_matrix_create(/* sample_num */ 1,
31+
/* size */ 784,
32+
/* useGPU */ false);
33+
srand(time(0));
34+
paddle_real* array;
35+
36+
// Get First row.
37+
CHECK(paddle_matrix_get_row(mat, 0, &array));
38+
39+
for (int i = 0; i < 784; ++i) {
40+
array[i] = rand() / ((float)RAND_MAX);
41+
}
42+
43+
CHECK(paddle_arguments_set_value(in_args, 0, mat));
44+
45+
paddle_arguments out_args = paddle_arguments_create_none();
46+
CHECK(paddle_gradient_machine_forward(machine,
47+
in_args,
48+
out_args,
49+
/* isTrain */ false));
50+
paddle_matrix prob = paddle_matrix_create_none();
51+
52+
CHECK(paddle_arguments_value(out_args, 0, prob));
53+
54+
CHECK(paddle_matrix_get_row(prob, 0, &array));
55+
56+
printf("Prob: ");
57+
for (int i = 0; i < 10; ++i) {
58+
printf("%.2f ", array[i]);
59+
}
60+
printf("\n");
61+
62+
return 0;
63+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from paddle.trainer_config_helpers import *
2+
3+
img = data_layer(name='pixel', size=784)
4+
5+
hidden = fc_layer(
6+
input=img,
7+
size=200,
8+
param_attr=ParamAttr(name='hidden.w'),
9+
bias_attr=ParamAttr(name='hidden.b'))
10+
11+
prob = fc_layer(
12+
input=hidden,
13+
size=10,
14+
act=SoftmaxActivation(),
15+
param_attr=ParamAttr(name='prob.w'),
16+
bias_attr=ParamAttr(name='prob.b'))
17+
18+
outputs(prob)
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# This file is used to ignore files which are generated
2+
# ----------------------------------------------------------------------------
3+
4+
*~
5+
*.autosave
6+
*.a
7+
*.core
8+
*.moc
9+
*.o
10+
*.obj
11+
*.orig
12+
*.rej
13+
*.so
14+
*.so.*
15+
*_pch.h.cpp
16+
*_resource.rc
17+
*.qm
18+
.#*
19+
*.*#
20+
core
21+
!core/
22+
tags
23+
.DS_Store
24+
.directory
25+
*.debug
26+
Makefile*
27+
*.prl
28+
*.app
29+
moc_*.cpp
30+
ui_*.h
31+
qrc_*.cpp
32+
Thumbs.db
33+
*.res
34+
*.rc
35+
/.qmake.cache
36+
/.qmake.stash
37+
38+
# qtcreator generated files
39+
*.pro.user*
40+
41+
# xemacs temporary files
42+
*.flc
43+
44+
# Vim temporary files
45+
.*.swp
46+
47+
# Visual Studio generated files
48+
*.ib_pdb_index
49+
*.idb
50+
*.ilk
51+
*.pdb
52+
*.sln
53+
*.suo
54+
*.vcproj
55+
*vcproj.*.*.user
56+
*.ncb
57+
*.sdf
58+
*.opensdf
59+
*.vcxproj
60+
*vcxproj.*
61+
62+
# MinGW generated files
63+
*.Debug
64+
*.Release
65+
66+
# Python byte code
67+
*.pyc
68+
69+
# Binaries
70+
# --------
71+
*.dll
72+
*.exe
73+

0 commit comments

Comments
 (0)