tensor_py.h 18.6 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
2

L
Luo Tao 已提交
3 4 5
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
6

L
Luo Tao 已提交
7
    http://www.apache.org/licenses/LICENSE-2.0
8

L
Luo Tao 已提交
9 10 11 12 13
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. */
14 15

#pragma once
L
Luo Tao 已提交
16
#include <Python.h>
W
wopeizl 已提交
17 18
#include <algorithm>
#include <memory>
Q
qijun 已提交
19
#include <string>
C
chengduoZH 已提交
20 21
#include <tuple>
#include <vector>
Y
Yi Wang 已提交
22 23
#include "paddle/fluid/framework/lod_tensor.h"
#include "paddle/fluid/memory/memcpy.h"
W
wopeizl 已提交
24 25
#include "paddle/fluid/operators/math/concat_and_split.h"
#include "paddle/fluid/operators/strided_memcpy.h"
Y
Yi Wang 已提交
26
#include "paddle/fluid/platform/device_context.h"
27
#include "paddle/fluid/platform/float16.h"
Q
qijun 已提交
28 29
#include "pybind11/numpy.h"
#include "pybind11/pybind11.h"
30

W
wopeizl 已提交
31 32
namespace py = pybind11;

33
namespace paddle {
34
namespace pybind {
35 36 37 38 39 40 41
namespace details {

template <bool less, size_t I, typename... ARGS>
struct CastToPyBufferImpl;

template <size_t I, typename... ARGS>
struct CastToPyBufferImpl<false, I, ARGS...> {
42
  pybind11::buffer_info operator()(const framework::Tensor &tensor) {
43
    PADDLE_THROW("This type of tensor cannot be expose to Python");
44
    return pybind11::buffer_info();
45 46 47 48 49 50
  }
};

template <size_t I, typename... ARGS>
struct CastToPyBufferImpl<true, I, ARGS...> {
  using CUR_TYPE = typename std::tuple_element<I, std::tuple<ARGS...>>::type;
51
  pybind11::buffer_info operator()(const framework::Tensor &tensor) {
Y
Yu Yang 已提交
52
    if (framework::DataTypeTrait<CUR_TYPE>::DataType == tensor.type()) {
53 54 55 56 57 58 59 60 61 62 63 64
      auto dim_vec = framework::vectorize(tensor.dims());
      std::vector<size_t> dims_outside;
      std::vector<size_t> strides;
      dims_outside.resize(dim_vec.size());
      strides.resize(dim_vec.size());

      size_t prod = 1;
      for (size_t i = dim_vec.size(); i != 0; --i) {
        dims_outside[i - 1] = (size_t)dim_vec[i - 1];
        strides[i - 1] = sizeof(CUR_TYPE) * prod;
        prod *= dims_outside[i - 1];
      }
Q
qijun 已提交
65
      framework::Tensor dst_tensor;
Y
Yu Yang 已提交
66 67
      bool is_gpu = paddle::platform::is_gpu_place(tensor.place());
      if (is_gpu) {
68 69 70
#ifdef PADDLE_WITH_CUDA
        auto *src_ptr = static_cast<const void *>(tensor.data<CUR_TYPE>());
        auto *dst_ptr = static_cast<void *>(dst_tensor.mutable_data<CUR_TYPE>(
S
sneaxiy 已提交
71
            tensor.dims(), platform::CPUPlace()));
D
dzhwinter 已提交
72

Y
Yu Yang 已提交
73 74 75
        paddle::platform::GpuMemcpySync(dst_ptr, src_ptr,
                                        sizeof(CUR_TYPE) * tensor.numel(),
                                        cudaMemcpyDeviceToHost);
76
#else
D
dzhwinter 已提交
77
        PADDLE_THROW("'CUDAPlace' is not supported in CPU only device.");
78
#endif
Y
Yu Yang 已提交
79
      } else if (paddle::platform::is_cpu_place(tensor.place())) {
Q
qijun 已提交
80 81
        dst_tensor = tensor;
      }
82

Y
Yu Yang 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
      std::string dtype = std::type_index(typeid(CUR_TYPE)) ==
                                  std::type_index(typeid(platform::float16))
                              ? std::string("e")  // np.dtype('e') == np.float16
                              : pybind11::format_descriptor<CUR_TYPE>::format();

      if (is_gpu) {
        // manually construct a py_buffer if is_gpu since gpu data is copied
        // into CPU.
        // TODO(yy): Is these following code memleak?
        Py_buffer *py_buffer =
            reinterpret_cast<Py_buffer *>(malloc(sizeof(Py_buffer)));
        py_buffer->format = strdup(dtype.c_str());
        py_buffer->itemsize = sizeof(CUR_TYPE);
        py_buffer->ndim = framework::arity(dst_tensor.dims());
        py_buffer->len = tensor.numel();
        py_buffer->strides = reinterpret_cast<Py_ssize_t *>(
            malloc(sizeof(Py_ssize_t) * strides.size()));
        for (size_t i = 0; i < strides.size(); ++i) {
          py_buffer->strides[i] = strides[i];
        }

        py_buffer->shape = reinterpret_cast<Py_ssize_t *>(
            malloc(sizeof(Py_ssize_t) * tensor.dims().size()));
Y
Yu Yang 已提交
106
        for (int i = 0; i < tensor.dims().size(); ++i) {
Y
Yu Yang 已提交
107 108 109 110 111 112 113 114 115 116 117
          py_buffer->shape[i] = tensor.dims()[i];
        }

        py_buffer->readonly = false;
        py_buffer->suboffsets = nullptr;
        py_buffer->obj = nullptr;
        py_buffer->buf =
            malloc(static_cast<size_t>(py_buffer->len * py_buffer->itemsize));
        memcpy(py_buffer->buf, dst_tensor.data<CUR_TYPE>(),
               static_cast<size_t>(py_buffer->len * py_buffer->itemsize));
        return pybind11::buffer_info(py_buffer, true);
118
      } else {
119
        return pybind11::buffer_info(
Y
Yu Yang 已提交
120
            dst_tensor.data<CUR_TYPE>(), sizeof(CUR_TYPE), dtype,
121
            (size_t)framework::arity(dst_tensor.dims()), dims_outside, strides);
122
      }
123 124 125 126 127 128
    } else {
      constexpr bool less = I + 1 < std::tuple_size<std::tuple<ARGS...>>::value;
      return CastToPyBufferImpl<less, I + 1, ARGS...>()(tensor);
    }
  }
};
129

130
}  // namespace details
131

132
inline pybind11::buffer_info CastToPyBuffer(const framework::Tensor &tensor) {
133
  auto buffer_info =
134
      details::CastToPyBufferImpl<true, 0, float, int, double, int64_t, bool,
Q
qingqing01 已提交
135
                                  uint8_t, int8_t, platform::float16>()(tensor);
136 137 138
  return buffer_info;
}

139
template <typename T>
140
T TensorGetElement(const framework::Tensor &self, size_t offset) {
Q
qingqing01 已提交
141 142
  PADDLE_ENFORCE_LT(offset, self.numel());
  T b = static_cast<T>(0);
143
  if (platform::is_cpu_place(self.place())) {
Q
qingqing01 已提交
144 145
    b = self.data<T>()[offset];
#ifdef PADDLE_WITH_CUDA
146
  } else {
Q
qingqing01 已提交
147 148 149 150 151
    const T *a = self.data<T>();
    auto p = boost::get<platform::CUDAPlace>(self.place());
    paddle::memory::Copy(platform::CPUPlace(), &b, p, a + offset, sizeof(T),
                         nullptr);
#endif
152
  }
Q
qingqing01 已提交
153
  return b;
154 155 156
}

template <typename T>
157
void TensorSetElement(framework::Tensor *self, size_t offset, T elem) {
Q
qingqing01 已提交
158 159
  PADDLE_ENFORCE_LT(offset, self->numel());
  if (platform::is_cpu_place(self->place())) {
Y
Yu Yang 已提交
160
    self->mutable_data<T>(self->place())[offset] = elem;
Q
qingqing01 已提交
161 162 163 164 165 166 167
#ifdef PADDLE_WITH_CUDA
  } else {
    auto p = boost::get<platform::CUDAPlace>(self->place());
    T *a = self->mutable_data<T>(p);
    paddle::memory::Copy(p, a + offset, platform::CPUPlace(), &elem, sizeof(T),
                         nullptr);
#endif
168
  }
169 170
}

171
template <typename T>
Q
qijun 已提交
172
void PyCPUTensorSetFromArray(
173 174 175 176
    framework::Tensor *self,
    pybind11::array_t<T, pybind11::array::c_style | pybind11::array::forcecast>
        array,
    paddle::platform::CPUPlace place) {
Q
qijun 已提交
177
  std::vector<int64_t> dims;
178
  dims.reserve(array.ndim());
S
fix bug  
sneaxiy 已提交
179
  for (decltype(array.ndim()) i = 0; i < array.ndim(); ++i) {
C
chengduoZH 已提交
180
    dims.push_back(static_cast<int>(array.shape()[i]));
181 182
  }

183 184
  self->Resize(framework::make_ddim(dims));
  auto *dst = self->mutable_data<T>(place);
185 186 187
  std::memcpy(dst, array.data(), sizeof(T) * array.size());
}

188
template <>
C
chengduoZH 已提交
189 190
// This following specialization maps uint16_t in the parameter type to
// platform::float16.
S
sneaxiy 已提交
191
inline void PyCPUTensorSetFromArray(
192 193 194 195 196
    framework::Tensor *self,
    pybind11::array_t<uint16_t,
                      pybind11::array::c_style | pybind11::array::forcecast>
        array,
    paddle::platform::CPUPlace place) {
197 198
  std::vector<int64_t> dims;
  dims.reserve(array.ndim());
S
fix bug  
sneaxiy 已提交
199
  for (decltype(array.ndim()) i = 0; i < array.ndim(); ++i) {
C
chengduoZH 已提交
200
    dims.push_back(static_cast<int>(array.shape()[i]));
201 202
  }

203 204
  self->Resize(framework::make_ddim(dims));
  auto *dst = self->mutable_data<platform::float16>(place);
205 206 207
  std::memcpy(dst, array.data(), sizeof(uint16_t) * array.size());
}

W
wopeizl 已提交
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
template <typename T, size_t D>
void _sliceCompute(const framework::Tensor *in, framework::Tensor *out,
                   const platform::CPUDeviceContext &ctx,
                   const std::vector<int> &axes,
                   const std::vector<int> &starts) {
  auto &eigen_place = *ctx.eigen_device();
  auto place = in->place();
  auto out_dims = out->dims();
  auto in_dims = in->dims();

  auto offsets = Eigen::array<int, D>();
  auto extents = Eigen::array<int, D>();
  for (size_t i = 0; i < D; ++i) {
    offsets[i] = 0;
    extents[i] = out_dims[i];
  }
  int start;
  for (size_t i = 0; i < axes.size(); ++i) {
    start = starts[i];
    if (start < 0) {
      start = (start + in_dims[axes[i]]);
    }
    start = std::max(start, 0);
    offsets[axes[i]] = start;
  }
  auto in_t =
      framework::EigenTensor<T, D, Eigen::RowMajor, Eigen::DenseIndex>::From(
          *in);
  auto out_t =
      framework::EigenTensor<T, D, Eigen::RowMajor, Eigen::DenseIndex>::From(
          *out);
  out_t.device(eigen_place) = in_t.slice(offsets, extents);
}

template <typename T>
void _concatCompute(const std::vector<paddle::framework::Tensor> &ins,
                    paddle::framework::Tensor *out,
                    const platform::CPUDeviceContext &ctx, int64_t axis) {
  if (axis == 0 && ins.size() < 10) {
    size_t output_offset = 0;
    for (auto &in : ins) {
      auto in_stride = framework::stride_numel(in.dims());
      auto out_stride = framework::stride_numel(out->dims());
      paddle::operators::StridedNumelCopyWithAxis<T>(
          ctx, axis, out->data<T>() + output_offset, out_stride, in.data<T>(),
          in_stride, in_stride[axis]);
      output_offset += in_stride[axis];
    }
  } else {
    paddle::operators::math::ConcatFunctor<platform::CPUDeviceContext, T>
        concat_functor;
    concat_functor(ctx, ins, static_cast<int>(axis), out);
  }
}

void _getSliceinfo(const framework::Tensor &self, py::object obj,
                   const int64_t dim, int64_t *pstart, int64_t *pstop,
                   int64_t *pstep, int64_t *pslicelength) {
  auto &start = *pstart;
  auto &stop = *pstop;
  auto &step = *pstep;
  auto &slicelength = *pslicelength;
  const framework::DDim &srcDDim = self.dims();
  if (dim < 0 || dim >= srcDDim.size()) {
    throw py::index_error();
  }
  if (py::isinstance<py::slice>(obj)) {
    size_t lstart, lstop, lstep, lslicelength;
    py::slice s = static_cast<py::slice>(obj);
    if (!s.compute(srcDDim[dim], &lstart, &lstop, &lstep, &lslicelength)) {
      throw py::index_error();
    }
    start = static_cast<int64_t>(lstart);
    stop = static_cast<int64_t>(lstop);
    step = static_cast<int64_t>(lstep);
    slicelength = static_cast<int64_t>(lslicelength);
  } else if (py::isinstance<py::int_>(obj)) {
    start = static_cast<int64_t>(static_cast<py::int_>(obj));
    if (std::abs(start) >= srcDDim[dim]) {
      throw py::index_error();
    }
    start = (start >= 0) ? start : srcDDim[dim] - start;
    stop = start + 1;
    step = 1;
    slicelength = 1;
  } else {
    throw py::index_error();
  }
}

inline framework::Tensor *_getTensor(const framework::Tensor &self,
                                     const framework::DDim &ddim) {
  framework::Tensor *output = new framework::Tensor();
  output->Resize(ddim);
  auto place = self.place();
  if (platform::is_cpu_place(place)) {
    output->mutable_data(boost::get<platform::CPUPlace>(place), self.type());
#ifdef PADDLE_WITH_CUDA
  } else {
    if (platform::is_cuda_pinned_place(place)) {
      output->mutable_data(boost::get<platform::CUDAPinnedPlace>(place),
                           self.type());
    } else if ((platform::is_gpu_place(place))) {
      output->mutable_data(boost::get<platform::CUDAPlace>(place), self.type());
    }
#endif
  }
  return output;
}

template <typename T>
void _sliceDapper(const framework::Tensor *in, framework::Tensor *out,
                  const platform::CPUDeviceContext &ctx,
                  const std::vector<int> &axes, const std::vector<int> &starts,
                  int size) {
  switch (size) {
    case 1:
      _sliceCompute<T, 1>(in, out, ctx, axes, starts);
      break;
    case 2:
      _sliceCompute<T, 2>(in, out, ctx, axes, starts);
      break;
    case 3:
      _sliceCompute<T, 3>(in, out, ctx, axes, starts);
      break;
    case 4:
      _sliceCompute<T, 4>(in, out, ctx, axes, starts);
      break;
    case 5:
      _sliceCompute<T, 5>(in, out, ctx, axes, starts);
      break;
    case 6:
      _sliceCompute<T, 6>(in, out, ctx, axes, starts);
      break;
    case 7:
      _sliceCompute<T, 7>(in, out, ctx, axes, starts);
      break;
    case 8:
      _sliceCompute<T, 8>(in, out, ctx, axes, starts);
      break;
    case 9:
      _sliceCompute<T, 9>(in, out, ctx, axes, starts);
      break;
    default:
      PADDLE_THROW("dim size not exepected, current is %d", size);
      break;
  }
}

template <typename T>
inline framework::Tensor *_sliceWrapper(const framework::Tensor &self,
                                        const platform::CPUDeviceContext &ctx,
                                        py::object obj, int dim, int64_t start,
                                        int64_t slicelength) {
  framework::DDim dstDDim = self.dims();
  dstDDim[dim] = static_cast<int64_t>(slicelength);
  std::vector<int> axes({dim});
  std::vector<int> starts({static_cast<int>(start)});
  framework::Tensor *output = _getTensor(self, dstDDim);
  _sliceDapper<T>(&self, output, ctx, axes, starts, dstDDim.size());
  return output;
}

template <typename T>
inline framework::Tensor *_sliceAndConcat(const framework::Tensor &self,
                                          py::object obj, int dim) {
  platform::CPUDeviceContext ctx;
  int64_t start, stop, step, slicelength;
  _getSliceinfo(self, obj, dim, &start, &stop, &step, &slicelength);
  if (step == 1 || slicelength == 1) {
    return _sliceWrapper<T>(self, ctx, obj, dim, start, slicelength);
  } else {
    std::vector<framework::Tensor> ins;
    for (auto i = 0; i < slicelength; ++i, start += step) {
      ins.emplace_back(*_sliceWrapper<T>(self, ctx, obj, dim, start, 1));
    }

    // do the concat operation
    framework::DDim dstDDim = self.dims();
    dstDDim[dim] = static_cast<int64_t>(slicelength);
    framework::Tensor *output1 = _getTensor(self, dstDDim);
    _concatCompute<T>(ins, output1, ctx, dim);
    return output1;
  }
}

inline framework::Tensor *_sliceTensor(const framework::Tensor &self,
                                       py::object obj, int dim) {
  auto src_type = self.type();
  switch (src_type) {
    case framework::proto::VarType::FP16:
      return _sliceAndConcat<paddle::platform::float16>(self, obj, dim);
    case framework::proto::VarType::FP32:
      return _sliceAndConcat<float>(self, obj, dim);
    case framework::proto::VarType::FP64:
      return _sliceAndConcat<double>(self, obj, dim);
    case framework::proto::VarType::INT32:
      return _sliceAndConcat<int>(self, obj, dim);
    case framework::proto::VarType::INT64:
      return _sliceAndConcat<int64_t>(self, obj, dim);
    case framework::proto::VarType::BOOL:
      return _sliceAndConcat<bool>(self, obj, dim);
    case framework::proto::VarType::INT16:
      return _sliceAndConcat<bool>(self, obj, dim);
    case framework::proto::VarType::UINT8:
      return _sliceAndConcat<bool>(self, obj, dim);
    default:
      PADDLE_THROW("Not support type %d", src_type);
  }
}

inline framework::Tensor *_pySliceTensor(const framework::Tensor &self,
                                         py::object obj) {
  if (py::isinstance<py::tuple>(obj)) {
    py::list l = static_cast<py::list>(obj);
    std::unique_ptr<framework::Tensor> target;
    framework::Tensor *src = const_cast<framework::Tensor *>(&self);
    for (auto i = 0; i < static_cast<int>(l.size()); ++i) {
      src = _sliceTensor(*src, l[i], i);
      if (i + 1 == static_cast<int>(l.size())) {
        return src;
      } else {
        target.reset(src);
      }
    }
    return nullptr;
  } else {
    return _sliceTensor(self, obj, 0);
  }
}

inline framework::Tensor *PySliceTensor(const framework::Tensor &self,
                                        py::object obj) {
  if (platform::is_gpu_place(self.place())) {
    std::unique_ptr<framework::Tensor> holder;
    framework::Tensor src;
    framework::TensorCopySync(self, platform::CPUPlace(), &src);
    framework::Tensor *output = _pySliceTensor(src, obj);
    holder.reset(output);
    framework::Tensor *dst = _getTensor(*output, output->dims());
    framework::TensorCopySync(*output, self.place(), dst);
    return dst;
  } else {
    return _pySliceTensor(self, obj);
  }
}

455
#ifdef PADDLE_WITH_CUDA
Q
qijun 已提交
456 457
template <typename T>
void PyCUDATensorSetFromArray(
458 459 460 461
    framework::Tensor *self,
    pybind11::array_t<T, pybind11::array::c_style | pybind11::array::forcecast>
        array,
    paddle::platform::CUDAPlace place) {
Q
qijun 已提交
462
  std::vector<int64_t> dims;
Q
qijun 已提交
463
  dims.reserve(array.ndim());
S
fix bug  
sneaxiy 已提交
464
  for (decltype(array.ndim()) i = 0; i < array.ndim(); ++i) {
C
chengduoZH 已提交
465
    dims.push_back(static_cast<int>(array.shape()[i]));
Q
qijun 已提交
466
  }
Q
qijun 已提交
467

468 469
  self->Resize(framework::make_ddim(dims));
  auto *dst = self->mutable_data<T>(place);
Y
Yu Yang 已提交
470 471
  paddle::platform::GpuMemcpySync(dst, array.data(), sizeof(T) * array.size(),
                                  cudaMemcpyHostToDevice);
472
}
473 474

template <>
C
chengduoZH 已提交
475 476
// This following specialization maps uint16_t in the parameter type to
// platform::float16.
S
sneaxiy 已提交
477
inline void PyCUDATensorSetFromArray(
478 479 480 481 482
    framework::Tensor *self,
    pybind11::array_t<uint16_t,
                      pybind11::array::c_style | pybind11::array::forcecast>
        array,
    paddle::platform::CUDAPlace place) {
483 484
  std::vector<int64_t> dims;
  dims.reserve(array.ndim());
S
fix bug  
sneaxiy 已提交
485
  for (decltype(array.ndim()) i = 0; i < array.ndim(); ++i) {
C
chengduoZH 已提交
486
    dims.push_back(static_cast<int>(array.shape()[i]));
487 488
  }

489 490
  self->Resize(framework::make_ddim(dims));
  auto *dst = self->mutable_data<platform::float16>(place);
Y
Yu Yang 已提交
491 492 493
  paddle::platform::GpuMemcpySync(dst, array.data(),
                                  sizeof(uint16_t) * array.size(),
                                  cudaMemcpyHostToDevice);
494
}
C
chengduoZH 已提交
495 496 497

template <typename T>
void PyCUDAPinnedTensorSetFromArray(
498 499 500
    framework::Tensor *self,
    pybind11::array_t<T, pybind11::array::c_style | pybind11::array::forcecast>
        array,
C
chengduoZH 已提交
501 502 503
    const paddle::platform::CUDAPinnedPlace &place) {
  std::vector<int64_t> dims;
  dims.reserve(array.ndim());
S
fix bug  
sneaxiy 已提交
504
  for (decltype(array.ndim()) i = 0; i < array.ndim(); ++i) {
C
chengduoZH 已提交
505 506 507
    dims.push_back(static_cast<int>(array.shape()[i]));
  }

508 509
  self->Resize(framework::make_ddim(dims));
  auto *dst = self->mutable_data<T>(place);
C
chengduoZH 已提交
510 511 512 513
  std::memcpy(dst, array.data(), sizeof(T) * array.size());
}

template <>
C
chengduoZH 已提交
514 515
// This following specialization maps uint16_t in the parameter type to
// platform::float16.
S
sneaxiy 已提交
516
inline void PyCUDAPinnedTensorSetFromArray(
517 518 519 520
    framework::Tensor *self,
    pybind11::array_t<uint16_t,
                      pybind11::array::c_style | pybind11::array::forcecast>
        array,
C
chengduoZH 已提交
521 522 523
    const paddle::platform::CUDAPinnedPlace &place) {
  std::vector<int64_t> dims;
  dims.reserve(array.ndim());
S
fix bug  
sneaxiy 已提交
524
  for (decltype(array.ndim()) i = 0; i < array.ndim(); ++i) {
C
chengduoZH 已提交
525 526 527
    dims.push_back(static_cast<int>(array.shape()[i]));
  }

528 529
  self->Resize(framework::make_ddim(dims));
  auto *dst = self->mutable_data<platform::float16>(place);
C
chengduoZH 已提交
530 531
  std::memcpy(dst, array.data(), sizeof(uint16_t) * array.size());
}
Q
qijun 已提交
532
#endif
533 534 535

}  // namespace pybind
}  // namespace paddle