1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
%module(directors="1") swig_paddle
%include "std_string.i"
%{
#define SWIG_FILE_WITH_INIT
#include "api/PaddleAPI.h"
%}
%include "std_vector.i"
%include "std_pair.i"
#ifdef SWIGPYTHON
%include "numpy.i"
#endif
%init %{
#ifdef SWIGPYTHON
import_array();
#endif
%}
namespace std {
%template(vector_int) vector<int>;
%template(vector_uint) vector<unsigned int>;
%template(vector_float) vector<float>;
%template(vector_string) vector<string>;
%template(vector_vec_star) vector<Vector*>;
}
#ifdef SWIGPYTHON
%typemap(in) (int argc, char** argv) {
int i = 0;
if (!PyList_Check($input)) {
PyErr_SetString(PyExc_ValueError, "Expecting a list");
return NULL;
}
$1 = PyList_Size($input);
$2 = (char **) malloc(($1+1)*sizeof(char *));
for (i = 0; i < $1; i++) {
PyObject *s = PyList_GetItem($input,i);
if (!PyString_Check(s)) {
free($2);
PyErr_SetString(PyExc_ValueError, "List items must be strings");
return NULL;
}
$2[i] = PyString_AsString(s);
}
$2[i] = 0;
}
%typemap(freearg) (int argc, char** argv) {
if ($2) free($2);
}
%typemap(out) FloatArray {
$result = PyList_New($1.length);
for (size_t i=0; i<$1.length; ++i) {
PyList_SetItem($result, i, PyFloat_FromDouble($1.buf[i]));
}
if($1.needFree) {
delete [] $1.buf;
}
}
%typemap(out) IntArray {
$result = PyList_New($1.length);
for (size_t i=0; i<$1.length; ++i) {
PyList_SetItem($result, i, PyInt_FromLong($1.buf[i]));
}
if ($1.needFree) {
delete [] $1.buf;
}
}
%typemap(out) IntWithFloatArray {
$result = PyList_New($1.length);
for (size_t i=0; i<$1.length; ++i) {
PyList_SetItem($result, i, PyTuple_Pack(2,
PyInt_FromLong($1.idxBuf[i]),
PyFloat_FromDouble($1.valBuf[i])
));
}
if ($1.needFree) {
delete [] $1.idxBuf;
delete [] $1.valBuf;
}
}
%rename(__getitem__) IVector::get;
%rename(__setitem__) IVector::set;
%rename(__len__) IVector::getSize;
%rename(__getitem__) Vector::get;
%rename(__setitem__) Vector::set;
%rename(__len__) Vector::getSize;
%rename(__call__) ParameterTraverseCallback::apply;
%apply (float* INPLACE_ARRAY2, int DIM1, int DIM2) {
(float* data, int dim1, int dim2)
}
%apply (float** ARGOUTVIEW_ARRAY2, int* DIM1, int* DIM2) {
(float** view_data, int* dim1, int* dim2)
}
%apply (float** ARGOUTVIEWM_ARRAY2, int* DIM1, int* DIM2) {
(float** view_m_data, int* dim1, int* dim2)
}
%apply (int** ARGOUTVIEWM_ARRAY1, int* DIM1) {
(int** view_m_data, int* dim1)
}
%apply (int* INPLACE_ARRAY1, int DIM1) {
(int* data, int dim)
}
%apply (int** ARGOUTVIEW_ARRAY1, int* DIM1) {
(int** view_data, int* dim1)
}
%apply (float* INPLACE_ARRAY1, int DIM1) {
(float* data, int dim)
}
%apply (float** ARGOUTVIEW_ARRAY1, int* DIM1) {
(float** view_data, int* dim1)
}
%apply (float** ARGOUTVIEWM_ARRAY1, int* DIM1) {
(float** view_m_data, int* dim1)
}
#endif
// The below functions internally create object by "new", so it should use
// use SWIG to handle gc. There are hints for SWIG to handle GC.
%newobject Matrix::createZero;
%newobject Matrix::createSparse;
%newobject Matrix::createDense;
%newobject Vector::createZero;
%newobject Vector::create;
%newobject Vector::createCpuVectorFromNumpy;
%newobject Vector::createGpuVectorFromNumpy;
%newobject IVector::createZero;
%newobject IVector::create;
%newobject Trainer::createByCommandLine;
%newobject Trainer::getNetworkOutput;
%newobject Trainer::getLayerOutput;
%newobject Arguments::getSlotValue;
%newobject Arguments::getSlotIds;
%newobject Arguments::getSlotIn;
%newobject Arguments::getSlotSequenceStartPositions;
%newobject Arguments::getSlotSequenceDim;
%newobject Arguments::createArguments;
%newobject GradientMachine::createByConfigProtoStr;
%newobject GradientMachine::createByModelConfig;
%newobject GradientMachine::asSequenceGenerator;
%newobject GradientMachine::getParameter;
%newobject GradientMachine::getLayerOutput;
%newobject TrainerConfig::createFromTrainerConfigFile;
%newobject TrainerConfig::getModelConfig;
%newobject TrainerConfig::getOptimizationConfig;
%newobject Parameter::getBuf;
%newobject Parameter::getConfig;
%newobject ParameterOptimizer::create;
%newobject ParameterOptimizer::needSpecialTraversal;
%feature("director") UpdateCallback;
%feature("autodoc", 1); // To generate method stub, for code hint in ide
// Ignore many private class, and method cannot be handled by swig.
%ignore MatrixPrivate;
%ignore TrainerPrivate;
%ignore IVector::operator[];
%ignore ArgumentsPrivate;
%ignore GradientMachinePrivate;
%ignore TrainerConfigPrivate;
%ignore ModelConfigPrivate;
%ignore ParameterPrivate;
%ignore SequenceGeneratorPrivate;
%ignore VectorPrivate;
%ignore ParameterConfigPrivate;
%ignore OptimizationConfigPrivate;
%ignore ParameterTraverseCallbackPrivate;
%include "utils/GlobalConstants.h"
%include "api/PaddleAPI.h"