Paddle.swig 5.4 KB
Newer Older
Z
zhangjinchao01 已提交
1 2 3 4 5 6
%module(directors="1") swig_paddle
%include "std_string.i"
%{
#define SWIG_FILE_WITH_INIT
#include "api/PaddleAPI.h"   
%}
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 99
%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;
Y
Yu Yang 已提交
100
%rename(__repr__) Evaluator::toString;
Z
zhangjinchao01 已提交
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

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

%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"
Y
Yu Yang 已提交
199
%include "api/PaddleAPI.h"