Paddle.i 5.5 KB
Newer Older
Z
zhangjinchao01 已提交
1 2 3 4
%module(directors="1") swig_paddle
%include "std_string.i"
%{
#define SWIG_FILE_WITH_INIT
X
fix  
Xin Pan 已提交
5
#include "legacy/api/PaddleAPI.h"
Z
zhangjinchao01 已提交
6
%}
7 8

%include "exception.i"
9 10 11 12
%typemap(throws) UnsupportError %{
  SWIG_exception(SWIG_RuntimeError, $1.what());
  SWIG_fail;
%}
13

Z
zhangjinchao01 已提交
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
%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;
Y
Yu Yang 已提交
99
%rename(__len__) Parameter::getSize;
Z
zhangjinchao01 已提交
100
%rename(__call__) ParameterTraverseCallback::apply;
Y
Yu Yang 已提交
101
%rename(__repr__) Evaluator::toString;
Z
zhangjinchao01 已提交
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

%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;
145 146 147
%newobject Matrix::createDenseFromNumpy;
%newobject Matrix::createCpuDenseFromNumpy;
%newobject Matrix::createGpuDenseFromNumpy;
Z
zhangjinchao01 已提交
148 149
%newobject Vector::createZero;
%newobject Vector::create;
150
%newobject Vector::createVectorFromNumpy;
Z
zhangjinchao01 已提交
151 152 153 154
%newobject Vector::createCpuVectorFromNumpy;
%newobject Vector::createGpuVectorFromNumpy;
%newobject IVector::createZero;
%newobject IVector::create;
155 156 157
%newobject IVector::createVectorFromNumpy;
%newobject IVector::createCpuVectorFromNumpy;
%newobject IVector::createGpuVectorFromNumpy;
Z
zhangjinchao01 已提交
158
%newobject Trainer::createByCommandLine;
159
%newobject Trainer::getForwardOutput;
Z
zhangjinchao01 已提交
160 161 162 163 164 165 166 167 168 169 170 171
%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;
Y
Yu Yang 已提交
172
%newobject GradientMachine::makeEvaluator;
Z
zhangjinchao01 已提交
173 174 175 176 177 178 179
%newobject TrainerConfig::createFromTrainerConfigFile;
%newobject TrainerConfig::getModelConfig;
%newobject TrainerConfig::getOptimizationConfig;
%newobject Parameter::getBuf;
%newobject Parameter::getConfig;
%newobject ParameterOptimizer::create;
%newobject ParameterOptimizer::needSpecialTraversal;
Y
Yu Yang 已提交
180
%newobject ParameterUpdater::createLocalUpdater;
Q
qiaolongfei 已提交
181
%newobject ParameterUpdater::createRemoteUpdater;
182
%newobject ParameterUpdater::createNewRemoteUpdater;
Z
zhangjinchao01 已提交
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200

%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;
X
Xin Pan 已提交
201
%include "legacy/utils/GlobalConstants.h"
X
fix  
Xin Pan 已提交
202
%include "legacy/api/PaddleAPI.h"