pd_utils.cc 10.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
//
// 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.
#include <string>

#include "paddle/fluid/inference/api/paddle_inference_api.h"
17
#include "paddle/fluid/inference/capi_exp/pd_utils.h"
18 19 20 21 22
#include "paddle/fluid/inference/capi_exp/utils_internal.h"
#include "paddle/fluid/platform/enforce.h"

#define DESTROY_ONE_DIM_ARRAY(type)                                           \
  void PD_OneDimArray##type##Destroy(__pd_take PD_OneDimArray##type* array) { \
23
    if (array != nullptr) {                                                   \
24 25 26 27
      delete[] array->data;                                                   \
      delete array;                                                           \
    }                                                                         \
  }
28 29 30 31 32 33 34 35 36 37
#define CONVERT_VEC_TO_ONE_DIM_ARRAY(type, Type, vec_type)      \
  __pd_give PD_OneDimArray##Type* CvtVecToOneDimArray##Type(    \
      const std::vector<vec_type>& vec) {                       \
    PD_OneDimArray##Type* array = new PD_OneDimArray##Type;     \
    array->size = vec.size();                                   \
    array->data = vec.empty() ? nullptr : new type[vec.size()]; \
    for (size_t index = 0; index < vec.size(); ++index) {       \
      array->data[index] = vec[index];                          \
    }                                                           \
    return array;                                               \
38 39 40 41 42
  }
#define CONVERT_ONE_DIM_ARRAY_TO_VEC(type, Type, vec_type)   \
  std::vector<vec_type> CvtOneDimArrayToVec##Type(           \
      __pd_keep const PD_OneDimArray##Type* array) {         \
    std::vector<vec_type> vec;                               \
43
    if (array != nullptr) {                                  \
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
      vec.resize(array->size);                               \
      for (size_t index = 0; index < array->size; ++index) { \
        vec[index] = array->data[index];                     \
      }                                                      \
    }                                                        \
    return vec;                                              \
  }

#define ONE_DIM_ARRAY_UTILS_FUNC_IMPL(type, Type, vec_type) \
  extern "C" {                                              \
  DESTROY_ONE_DIM_ARRAY(Type);                              \
  }                                                         \
  namespace paddle_infer {                                  \
  CONVERT_VEC_TO_ONE_DIM_ARRAY(type, Type, vec_type)        \
  CONVERT_ONE_DIM_ARRAY_TO_VEC(type, Type, vec_type)        \
  }

ONE_DIM_ARRAY_UTILS_FUNC_IMPL(int32_t, Int32, int)
ONE_DIM_ARRAY_UTILS_FUNC_IMPL(size_t, Size, size_t)
63
ONE_DIM_ARRAY_UTILS_FUNC_IMPL(int64_t, Int64, int64_t)
64 65 66 67 68 69 70

#undef ONE_DIM_ARRAY_UTILS_FUNC_IMPL
#undef CONVERT_ONE_DIM_ARRAY_TO_VEC
#undef CONVERT_VEC_TO_ONE_DIM_ARRAY
#undef DESTROY_ONE_DIM_ARRAY

void PD_OneDimArrayCstrDestroy(__pd_take PD_OneDimArrayCstr* array) {
71
  if (array != nullptr) {
72 73 74 75 76 77 78 79 80
    if (array->size != 0) {
      for (size_t index = 0; index < array->size; ++index) {
        delete[] array->data[index];
      }
    }
    delete[] array->data;
    delete array;
  }
}
81 82

void PD_CstrDestroy(__pd_take PD_Cstr* cstr) {
83
  if (cstr != nullptr) {
84 85 86
    if (cstr->size != 0) {
      cstr->size = 0;
      delete[] cstr->data;
87
      cstr->data = nullptr;
88 89 90 91
    }
    delete cstr;
  }
}
92 93 94 95 96 97
namespace paddle_infer {

__pd_give PD_OneDimArrayCstr* CvtVecToOneDimArrayCstr(
    const std::vector<std::string>& vec) {
  PD_OneDimArrayCstr* array = new PD_OneDimArrayCstr;
  array->size = vec.size();
98
  array->data = vec.empty() ? nullptr : new char*[vec.size()];
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
  for (size_t index = 0u; index < vec.size(); ++index) {
    array->data[index] = new char[vec[index].size() + 1];
    memcpy(array->data[index], vec[index].c_str(), vec[index].size() + 1);
  }
  return array;
}

std::vector<std::string> CvtOneDimArrayToVecCstr(
    __pd_keep const PD_OneDimArrayCstr* array) {
  std::vector<std::string> vec;
  for (size_t index = 0; index < array->size; ++index) {
    vec.emplace_back(array->data[index]);
  }
  return vec;
}

115 116 117 118
__pd_give PD_Cstr* CvtStrToCstr(const std::string& str) {
  PD_Cstr* cstr = new PD_Cstr;
  if (str.empty()) {
    cstr->size = 0;
119
    cstr->data = nullptr;
120 121 122 123 124 125 126
  } else {
    cstr->size = str.length() + 1;
    cstr->data = new char[str.length() + 1];
    memcpy(cstr->data, str.c_str(), str.length() + 1);
  }
  return cstr;
}
127 128 129 130
}  // namespace paddle_infer

#define DESTROY_TWO_DIM_ARRAY(type)                                           \
  void PD_TwoDimArray##type##Destroy(__pd_take PD_TwoDimArray##type* array) { \
131
    if (array != nullptr) {                                                   \
132 133 134 135 136 137 138 139 140
      if (array->size != 0) {                                                 \
        for (size_t index = 0; index < array->size; ++index) {                \
          PD_OneDimArray##type##Destroy(array->data[index]);                  \
        }                                                                     \
      }                                                                       \
      delete[] array->data;                                                   \
      delete array;                                                           \
    }                                                                         \
  }
141 142 143 144 145 146 147 148 149 150 151
#define CONVERT_VEC_TO_TWO_DIM_ARRAY(type, Type, vec_type)             \
  __pd_give PD_TwoDimArray##Type* CvtVecToTwoDimArray##Type(           \
      const std::vector<std::vector<vec_type>>& vec) {                 \
    PD_TwoDimArray##Type* array = new PD_TwoDimArray##Type;            \
    array->size = vec.size();                                          \
    array->data =                                                      \
        vec.empty() ? nullptr : new PD_OneDimArray##Type*[vec.size()]; \
    for (size_t index = 0; index < vec.size(); ++index) {              \
      array->data[index] = CvtVecToOneDimArray##Type(vec[index]);      \
    }                                                                  \
    return array;                                                      \
152 153 154 155 156
  }
#define CONVERT_TWO_DIM_ARRAY_TO_VEC(type, Type, vec_type)            \
  std::vector<std::vector<vec_type>> CvtTwoDimArrayToVec##Type(       \
      __pd_keep const PD_TwoDimArray##Type* array) {                  \
    std::vector<std::vector<vec_type>> vec;                           \
157
    if (array != nullptr && array->size != 0) {                       \
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
      vec.resize(array->size);                                        \
      for (size_t index = 0; index < array->size; ++index) {          \
        vec[index] = CvtOneDimArrayToVec##Type((array->data)[index]); \
      }                                                               \
    }                                                                 \
    return vec;                                                       \
  }
#define TWO_DIM_ARRAY_UTILS_FUNC_IMPL(type, Type, vec_type) \
  extern "C" {                                              \
  DESTROY_TWO_DIM_ARRAY(Type);                              \
  }                                                         \
  namespace paddle_infer {                                  \
  CONVERT_VEC_TO_TWO_DIM_ARRAY(type, Type, vec_type)        \
  CONVERT_TWO_DIM_ARRAY_TO_VEC(type, Type, vec_type)        \
  }

TWO_DIM_ARRAY_UTILS_FUNC_IMPL(size_t, Size, size_t)

#undef TWO_DIM_ARRAY_UTILS_FUNC_IMPL
#undef CONVERT_TWO_DIM_ARRAY_TO_VEC
#undef CONVERT_VEC_TO_TWO_DIM_ARRAY
#undef DESTROY_TWO_DIM_ARRAY

181 182 183 184 185
#ifdef __cplusplus
extern "C" {
#endif

void PD_IOInfoDestroy(__pd_take PD_IOInfo* io_info) {
186
  if (io_info != nullptr) {
187
    PD_CstrDestroy(io_info->name);
188
    io_info->name = nullptr;
189
    PD_OneDimArrayInt64Destroy(io_info->shape);
190
    io_info->shape = nullptr;
191 192 193 194 195
    delete io_info;
  }
}

void PD_IOInfosDestroy(__pd_take PD_IOInfos* io_infos) {
196
  if (io_infos != nullptr) {
197 198 199 200 201 202 203
    if (io_infos->size != 0) {
      for (size_t index = 0; index < io_infos->size; ++index) {
        PD_IOInfoDestroy(io_infos->io_info[index]);
      }
      io_infos->size = 0;
    }
    delete[] io_infos->io_info;
204
    io_infos->io_info = nullptr;
205 206 207 208 209 210 211 212
    delete io_infos;
  }
}

#ifdef __cplusplus
}  // extern "C"
#endif

213 214 215 216 217 218 219 220 221 222 223 224
namespace paddle_infer {

PlaceType CvtToCxxPlaceType(PD_PlaceType place_type) {
  switch (place_type) {
    case PD_PLACE_UNK:
      return PlaceType::kUNK;
    case PD_PLACE_CPU:
      return PlaceType::kCPU;
    case PD_PLACE_GPU:
      return PlaceType::kGPU;
    case PD_PLACE_XPU:
      return PlaceType::kXPU;
225 226
    case PD_PLACE_CUSTOM:
      return PlaceType::kCUSTOM;
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
    default:
      PADDLE_THROW(paddle::platform::errors::InvalidArgument(
          "Unsupport paddle place type %d.", place_type));
      return PlaceType::kUNK;
  }
}

PD_PlaceType CvtFromCxxPlaceType(PlaceType place_type) {
  switch (place_type) {
    case PlaceType::kCPU:
      return PD_PLACE_CPU;
    case PlaceType::kGPU:
      return PD_PLACE_GPU;
    case PlaceType::kXPU:
      return PD_PLACE_XPU;
242 243
    case PlaceType::kCUSTOM:
      return PD_PLACE_CUSTOM;
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
    default:
      return PD_PLACE_UNK;
  }
}

DataType CvtToCxxDatatype(PD_DataType data_type) {
  switch (data_type) {
    case PD_DATA_FLOAT32:
      return DataType::FLOAT32;
    case PD_DATA_INT64:
      return DataType::INT64;
    case PD_DATA_INT32:
      return DataType::INT32;
    case PD_DATA_UINT8:
      return DataType::UINT8;
259 260
    case PD_DATA_INT8:
      return DataType::INT8;
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
    default:
      PADDLE_THROW(paddle::platform::errors::InvalidArgument(
          "Unsupport paddle data type %d.", data_type));
      return DataType::FLOAT32;
  }
}

PD_DataType CvtFromCxxDatatype(DataType data_type) {
  switch (data_type) {
    case DataType::FLOAT32:
      return PD_DATA_FLOAT32;
    case DataType::INT64:
      return PD_DATA_INT64;
    case DataType::INT32:
      return PD_DATA_INT32;
    case DataType::UINT8:
      return PD_DATA_UINT8;
    default:
      return PD_DATA_UNK;
  }
}

}  // namespace paddle_infer