PaddleCAPI.h 10.4 KB
Newer Older
Y
Yu Yang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#ifndef PADDLECAPI_H_
#define PADDLECAPI_H_
Y
Yu Yang 已提交
17 18 19
#include <stdbool.h>
#include <stdint.h>
#include "config.h"
Y
Yu Yang 已提交
20 21 22 23 24

// Since we only support linux and macos in compile, always use clang or
// gcc 4.8+. DLL_IMPORT/DLL_EXPORT is as simple as below.
#define PD_API __attribute__((visibility("default")))

Y
Yu Yang 已提交
25 26 27 28
#ifdef __cplusplus
extern "C" {
#endif

Y
Yu Yang 已提交
29 30 31 32 33 34 35 36 37 38
/**
 * Paddle C API. It will replace SWIG as Multiple Language API for model
 * training & inference. Currently it is only used in model infernece.
 *
 * NOTE: This is an experimental API, it could be changed.
 */

/**
 * Error Type for Paddle API.
 */
Y
Yu Yang 已提交
39
typedef enum {
Y
Yu Yang 已提交
40 41 42 43 44
  kPD_NO_ERROR = 0,
  kPD_NULLPTR = 1,
  kPD_OUT_OF_RANGE = 2,
  kPD_PROTOBUF_ERROR = 3,
  kPD_UNDEFINED_ERROR = -1,
Y
Yu Yang 已提交
45
} PD_Error;
Y
Yu Yang 已提交
46

Y
Yu Yang 已提交
47 48 49
/**
 * Int Vector Functions. Return will be a PD_Error type.
 */
Y
Yu Yang 已提交
50
typedef void* PD_IVector;
Y
Yu Yang 已提交
51

Y
Yu Yang 已提交
52 53 54 55 56 57
/**
 * @brief Create an none int vector. It just a handler and store nothing. Used
 *        to get output from other api.
 * @return None int vector.
 */
PD_API PD_IVector PDIVecCreateNone();
Y
Yu Yang 已提交
58

Y
Yu Yang 已提交
59 60
/**
 * @brief PDIVectorCreate create a paddle int vector
Y
Yu Yang 已提交
61 62 63 64
 * @param array: input array.
 * @param size: input array size.
 * @param copy: memory copy or just use same memory. True if copy.
 * @param useGPU: True if use GPU
Y
Yu Yang 已提交
65 66
 * @return PD_Error
 */
Y
Yu Yang 已提交
67 68 69 70
PD_API PD_IVector PDIVectorCreate(int* array,
                                  uint64_t size,
                                  bool copy,
                                  bool useGPU);
Y
Yu Yang 已提交
71

Y
Yu Yang 已提交
72 73 74 75 76 77
/**
 * @brief PDIVecDestroy destory an int vector.
 * @param ivec vector to be destoried.
 * @return PD_Error
 */
PD_API PD_Error PDIVecDestroy(PD_IVector ivec);
Y
Yu Yang 已提交
78

Y
Yu Yang 已提交
79 80 81 82 83 84 85 86
/**
 * @brief PDIVectorGet get raw buffer stored inside this int vector. It could be
 *        GPU memory if this int vector is stored in GPU.
 * @param [in] ivec int vector
 * @param [out] buffer the return buffer pointer.
 * @return PD_Error
 */
PD_API PD_Error PDIVectorGet(PD_IVector ivec, int** buffer);
Y
Yu Yang 已提交
87

Y
Yu Yang 已提交
88 89 90 91 92 93 94
/**
 * @brief PDIVectorResize resize the int vector.
 * @param [in] ivec: int vector
 * @param [in] size: size to change
 * @return PD_Error
 */
PD_API PD_Error PDIVectorResize(PD_IVector ivec, uint64_t size);
Y
Yu Yang 已提交
95

Y
Yu Yang 已提交
96 97 98 99 100 101 102
/**
 * @brief PDIVectorGetSize get the size of int vector.
 * @param [in] ivec: int vector
 * @param [out] size: return size of this int vector.
 * @return PD_Error
 */
PD_API PD_Error PDIVectorGetSize(PD_IVector ivec, uint64_t* size);
Y
Yu Yang 已提交
103

Y
Yu Yang 已提交
104 105 106
/**
 * Matrix functions. Return will be a PD_Error type.
 */
Y
Yu Yang 已提交
107 108
typedef void* PD_Matrix;

Y
Yu Yang 已提交
109 110 111 112 113 114 115 116
/**
 * @brief PDMatCreate Create a dense matrix
 * @param height matrix height.
 * @param width matrix width
 * @param useGpu use GPU of not
 * @return Matrix handler
 */
PD_API PD_Matrix PDMatCreate(uint64_t height, uint64_t width, bool useGpu);
Y
Yu Yang 已提交
117

Y
Yu Yang 已提交
118 119 120 121 122 123
/**
 * @brief PDMatDestroy Destroy a matrix.
 * @param mat
 * @return PD_Error
 */
PD_API PD_Error PDMatDestroy(PD_Matrix mat);
Y
Yu Yang 已提交
124

Y
Yu Yang 已提交
125 126 127 128 129 130 131 132 133 134
/**
 * @brief PDMatCopyToRow Copy a row to matrix.
 * @param mat Target Matrix
 * @param rowID Index of row
 * @param rowArray Row data.
 * @return PD_Error
 */
PD_API PD_Error PDMatCopyToRow(PD_Matrix mat,
                               uint64_t rowID,
                               pd_real* rowArray);
Y
Yu Yang 已提交
135

Y
Yu Yang 已提交
136 137 138 139 140 141 142 143 144 145
/**
 * @brief PDMatGetRow Get raw row buffer from matrix
 * @param [in] mat Target matrix
 * @param [in] rowID Index of row.
 * @param [out] rawRowBuffer Row Buffer
 * @return PD_Error
 */
PD_API PD_Error PDMatGetRow(PD_Matrix mat,
                            uint64_t rowID,
                            pd_real** rawRowBuffer);
Y
Yu Yang 已提交
146

Y
Yu Yang 已提交
147 148 149 150 151
/**
 * @brief PDMatCreateNone Create None Matrix
 * @return
 */
PD_API PD_Matrix PDMatCreateNone();
Y
Yu Yang 已提交
152

Y
Yu Yang 已提交
153 154 155 156 157 158 159 160
/**
 * @brief PDMatGetShape get the shape of matrix
 * @param mat target matrix
 * @param height The height of matrix
 * @param width The width of matrix
 * @return PD_Error
 */
PD_API PD_Error PDMatGetShape(PD_Matrix mat, uint64_t* height, uint64_t* width);
Y
Yu Yang 已提交
161

Y
Yu Yang 已提交
162 163 164 165
/**
 * Arguments functions. Each argument means layer output. Arguments means a
 * array of arguemnt.
 */
Y
Yu Yang 已提交
166 167
typedef void* PD_Arguments;

Y
Yu Yang 已提交
168 169 170 171 172
/**
 * @brief PDArgsCreateNone Create a array of arguments, which size is zero.
 * @return Arguemnts
 */
PD_API PD_Arguments PDArgsCreateNone();
Y
Yu Yang 已提交
173

Y
Yu Yang 已提交
174 175 176 177 178 179
/**
 * @brief PDArgsDestroy Destroy the arguments
 * @param args arguments to destroy
 * @return PD_Error
 */
PD_API PD_Error PDArgsDestroy(PD_Arguments args);
Y
Yu Yang 已提交
180

Y
Yu Yang 已提交
181 182 183 184 185 186 187
/**
 * @brief PDArgsGetSize Get size of arguments array
 * @param [in] args arguments array
 * @param [out] size array size
 * @return PD_Error
 */
PD_API PD_Error PDArgsGetSize(PD_Arguments args, uint64_t* size);
Y
Yu Yang 已提交
188

Y
Yu Yang 已提交
189 190 191 192 193 194 195
/**
 * @brief PDArgsResize Resize a arguments array.
 * @param args arguments array.
 * @param size target size of array
 * @return PD_Error
 */
PD_API PD_Error PDArgsResize(PD_Arguments args, uint64_t size);
Y
Yu Yang 已提交
196

Y
Yu Yang 已提交
197 198 199 200 201 202 203 204 205
/**
 * @brief PDArgsSetValue Set value matrix of one argument in array, which index
 *        is `ID`.
 * @param args arguments array
 * @param ID array index
 * @param mat matrix pointer
 * @return PD_Error
 */
PD_API PD_Error PDArgsSetValue(PD_Arguments args, uint64_t ID, PD_Matrix mat);
Y
Yu Yang 已提交
206

Y
Yu Yang 已提交
207 208 209 210 211 212 213 214 215
/**
 * @brief PDArgsGetValue Get value matrix of one argument in array, which index
 *        is `ID`.
 * @param [in] args arguments array
 * @param [in] ID array index
 * @param [out] mat matrix pointer
 * @return PD_Error
 */
PD_API PD_Error PDArgsGetValue(PD_Arguments args, uint64_t ID, PD_Matrix mat);
Y
Yu Yang 已提交
216

Y
Yu Yang 已提交
217 218 219 220 221 222 223 224 225
/**
 * @brief PDArgsGetIds Get the integer vector of one argument in array, which
 *        index is `ID`.
 * @param args arguments array
 * @param ID array index
 * @param ids integer vector pointer
 * @return PD_Error
 */
PD_API PD_Error PDArgsGetIds(PD_Arguments args, uint64_t ID, PD_IVector ids);
Y
Yu Yang 已提交
226

Y
Yu Yang 已提交
227 228 229 230 231 232 233 234 235
/**
 * @brief PDArgsSetIds Set the integer vector of one argument in array, which
 *        index is `ID`.
 * @param [in] args arguments array
 * @param [in] ID array index
 * @param [out] ids integer vector pointer
 * @return PD_Error
 */
PD_API PD_Error PDArgsSetIds(PD_Arguments args, uint64_t ID, PD_IVector ids);
Y
Yu Yang 已提交
236

Y
Yu Yang 已提交
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
/**
 * @brief PDArgsSetSequenceStartPos Set sequence start position vector of one
 *        argument in array, which index is `ID`.
 * @param args arguments array
 * @param ID array index
 * @param seqPos sequence position array.
 * @return PD_Error
 */
PD_API PD_Error PDArgsSetSequenceStartPos(PD_Arguments args,
                                          uint64_t ID,
                                          PD_IVector seqPos);
/**
 * @brief PDArgsGetSequenceStartPos Get sequence start position vector of one
 *        argument in array, which index is `ID`.
 * @param [in] args arguments array
 * @param [in] ID array index
 * @param [out] seqPos sequence position array
 * @return PD_Error
 */
PD_API PD_Error PDArgsGetSequenceStartPos(PD_Arguments args,
                                          uint64_t ID,
                                          PD_IVector seqPos);
Y
Yu Yang 已提交
259

Y
Yu Yang 已提交
260 261 262 263 264 265 266 267 268 269 270
/**
 * @brief PDArgsSetSubSequenceStartPos Set sub-sequence start position vector of
 *        one argument in array, which index is `ID`.
 * @param args arguments array
 * @param ID array index
 * @param subSeqPos sub-sequence start position array.
 * @return PD_Error
 */
PD_API PD_Error PDArgsSetSubSequenceStartPos(PD_Arguments args,
                                             uint64_t ID,
                                             PD_IVector subSeqPos);
Y
Yu Yang 已提交
271

Y
Yu Yang 已提交
272 273 274 275 276 277 278 279 280 281 282
/**
 * @brief PDArgsGetSubSequenceStartPos Get sub-sequence start position vector of
 *        one argument in array, which index is `ID`.
 * @param args arguments array
 * @param ID array index
 * @param subSeqPos sub-sequence start position array
 * @return PD_Error
 */
PD_API PD_Error PDArgsGetSubSequenceStartPos(PD_Arguments args,
                                             uint64_t ID,
                                             PD_IVector subSeqPos);
Y
Yu Yang 已提交
283 284 285
/**
 * @brief GradientMachine means a neural network.
 */
Y
Yu Yang 已提交
286
typedef void* PD_GradientMachine;
Y
Yu Yang 已提交
287

Y
Yu Yang 已提交
288 289 290 291 292 293 294 295 296 297 298
/**
 * @brief PDGradientMachineCreateForPredict Create a gradient machine used for
 *        model inference.
 * @param [out] machine that used for model inference.
 * @param [in] modelConfigProtobuf
 * @param [in] size
 * @return PD_Error
 */
PD_API PD_Error PDGradientMachineCreateForPredict(PD_GradientMachine* machine,
                                                  void* modelConfigProtobuf,
                                                  int size);
Y
Yu Yang 已提交
299

Y
Yu Yang 已提交
300 301 302 303 304 305 306 307
/**
 * @brief PDGradientMachineLoadParameterFromDisk Load parameter from disk.
 * @param machine Gradient Machine.
 * @param path local directory path.
 * @return PD_Error
 */
PD_API PD_Error PDGradientMachineLoadParameterFromDisk(
    PD_GradientMachine machine, const char* path);
Y
Yu Yang 已提交
308

Y
Yu Yang 已提交
309 310 311 312 313 314 315 316 317 318 319 320
/**
 * @brief PDGradientMachineForward Forward a gradient machine
 * @param machine Gradient machine
 * @param inArgs input arguments
 * @param outArgs output arguments
 * @param isTrain is train or not
 * @return PD_Error
 */
PD_API PD_Error PDGradientMachineForward(PD_GradientMachine machine,
                                         PD_Arguments inArgs,
                                         PD_Arguments outArgs,
                                         bool isTrain);
Y
Yu Yang 已提交
321

Y
Yu Yang 已提交
322 323 324 325 326 327 328 329 330 331 332 333 334
/**
 * @brief PDGradientMachineCreateSharedParam Create a gradient machine, which
 *        parameters are shared from another gradient machine.
 * @param [in] origin gradient machine
 * @param [in] modelConfigProtobuf model config protobuf
 * @param [in] size of model config buffer.
 * @param [out] slave gradient machine, the output value.
 * @return PD_Error
 */
PD_API PD_Error PDGradientMachineCreateSharedParam(PD_GradientMachine origin,
                                                   void* modelConfigProtobuf,
                                                   int size,
                                                   PD_GradientMachine* slave);
Y
Yu Yang 已提交
335

Y
Yu Yang 已提交
336 337 338 339 340 341
/**
 * @brief PDGradientMachineDestroy Destroy a gradient machine
 * @param machine that need to destroy
 * @return PD_Error
 */
PD_API PD_Error PDGradientMachineDestroy(PD_GradientMachine machine);
Y
Yu Yang 已提交
342

Y
Yu Yang 已提交
343 344 345
/**
 * Initialize Paddle.
 */
Y
Yu Yang 已提交
346
PD_API PD_Error PDInit(int argc, char** argv);
Y
Yu Yang 已提交
347

Y
Yu Yang 已提交
348 349 350
#ifdef __cplusplus
}
#endif
Y
Yu Yang 已提交
351 352

#endif  // PADDLECAPI_H_