Skip to content

Commit

Permalink
Fix none-contiguous bug for python api
Browse files Browse the repository at this point in the history
  • Loading branch information
jiweibo committed Dec 15, 2020
1 parent d82d59e commit 2716e17
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions paddle/fluid/pybind/inference_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,23 +69,27 @@ void BindMkldnnQuantizerConfig(py::module *m);
#endif

template <typename T>
PaddleBuf PaddleBufCreate(py::array_t<T> data) {
PaddleBuf PaddleBufCreate(
py::array_t<T, py::array::c_style | py::array::forcecast> data) {
PaddleBuf buf(data.size() * sizeof(T));
std::copy_n(static_cast<const T *>(data.data()), data.size(),
static_cast<T *>(buf.data()));
return buf;
}

template <typename T>
void PaddleBufReset(PaddleBuf &buf, py::array_t<T> data) { // NOLINT
void PaddleBufReset(
PaddleBuf &buf, // NOLINT
py::array_t<T, py::array::c_style | py::array::forcecast> data) { // NOLINT
buf.Resize(data.size() * sizeof(T));
std::copy_n(static_cast<const T *>(data.data()), data.size(),
static_cast<T *>(buf.data()));
}

template <typename T>
PaddleTensor PaddleTensorCreate(
py::array_t<T> data, const std::string name = "",
py::array_t<T, py::array::c_style | py::array::forcecast> data,
const std::string name = "",
const std::vector<std::vector<size_t>> &lod = {}, bool copy = true) {
PaddleTensor tensor;

Expand Down Expand Up @@ -137,17 +141,19 @@ py::array PaddleTensorGetData(PaddleTensor &tensor) { // NOLINT
}

template <typename T>
void ZeroCopyTensorCreate(ZeroCopyTensor &tensor, // NOLINT
py::array_t<T> data) {
void ZeroCopyTensorCreate(
ZeroCopyTensor &tensor, // NOLINT
py::array_t<T, py::array::c_style | py::array::forcecast> data) {
std::vector<int> shape;
std::copy_n(data.shape(), data.ndim(), std::back_inserter(shape));
tensor.Reshape(std::move(shape));
tensor.copy_from_cpu(static_cast<const T *>(data.data()));
}

template <typename T>
void PaddleInferTensorCreate(paddle_infer::Tensor &tensor, // NOLINT
py::array_t<T> data) {
void PaddleInferTensorCreate(
paddle_infer::Tensor &tensor, // NOLINT
py::array_t<T, py::array::c_style | py::array::forcecast> data) {
std::vector<int> shape;
std::copy_n(data.shape(), data.ndim(), std::back_inserter(shape));
tensor.Reshape(std::move(shape));
Expand Down

1 comment on commit 2716e17

@paddle-bot-old
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Congratulation! Your pull request passed all required CI. You could ask reviewer(s) to approve and merge. 🎉

Please sign in to comment.