tensor.cc 14.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* 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. */

15
// clang-format off
16
#include "paddle/phi/api/include/tensor.h"
17 18 19 20 21 22

#include <memory>
#include <utility>
#include <vector>

#include "glog/logging.h"
23

24
#include "paddle/phi/api/lib/utils/allocator.h"
25 26
#include "paddle/phi/backends/gpu/gpu_info.h"
#include "paddle/phi/core/ddim.h"
27
#include "paddle/phi/core/dense_tensor.h"
28
#include "paddle/phi/core/enforce.h"
29
#include "paddle/phi/core/selected_rows.h"
30 31
#include "paddle/phi/core/sparse_coo_tensor.h"
#include "paddle/phi/core/sparse_csr_tensor.h"
J
Jack Zhou 已提交
32
#include "paddle/phi/core/string_tensor.h"
33 34 35
#include "paddle/phi/core/tensor_base.h"
#include "paddle/phi/core/tensor_meta.h"
#include "paddle/phi/core/tensor_utils.h"
36

37
#include "paddle/fluid/platform/stream/cuda_stream.h"
38
// clang-format off
39 40 41

namespace paddle {
namespace experimental {
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
namespace detail {
static Place GetCorrectPlaceByPlaceType(const Place &place_type) {
  auto alloc_type = place_type.GetType();
  switch (alloc_type) {
    case AllocationType::CPU:
      return place_type;
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
    case AllocationType::GPU:
      return phi::Place(AllocationType::GPU,
                        phi::backends::gpu::GetCurrentDeviceId());
#endif
    default:
      PADDLE_THROW(phi::errors::Unavailable(
          "The PlaceType is a legacy design, only supports CPU and GPU, "
          "and will not support other place types in the future."));
  }
}
}  // namespace detail
60 61 62 63 64

/////// Tensor Methods ////////

/* Part 1: Construction and destruction methods */

65
Tensor::Tensor(std::shared_ptr<phi::TensorBase> tensor_impl)
66
    : impl_(std::move(tensor_impl)) {
67 68 69
  PADDLE_ENFORCE_NOT_NULL(
      impl_,
      phi::errors::InvalidArgument("TensorImpl with nullptr is not supported"));
70 71
}

72
Tensor::Tensor(const Place &place) {
73 74 75 76 77 78 79 80
  LOG_FIRST_N(WARNING, 1)
      << "The Tensor(place) constructor is deprecated since version "
         "2.3, and will be removed in version 2.4! Please use "
         "`paddle::empty/full` method to create a new "
         "Tensor instead. "
         "Reason: A legal tensor cannot be constructed only based on "
         "the `place`, and datatype, shape, layout, etc. is also "
         "required.";
81 82 83 84 85 86 87 88
  DefaultAllocator alloc(detail::GetCorrectPlaceByPlaceType(place));
  impl_ = std::move(std::make_shared<phi::DenseTensor>(
      &alloc,
      std::move(phi::DenseTensorMeta(
          phi::DataType::FLOAT32, phi::make_ddim({}), phi::DataLayout::NCHW))));
}

Tensor::Tensor(const Place &place, const std::vector<int64_t> &shape) {
89 90 91 92 93 94 95 96
  LOG_FIRST_N(WARNING, 1)
      << "The Tensor(place, shape) constructor is deprecated since "
         "version 2.3, and will be removed in version 2.4! Please use "
         "`paddle::empty/full` method to create a new "
         "Tensor instead. "
         "Reason: A legal tensor cannot be constructed only based on "
         "the `place` and `shape`, and datatype, layout, etc. is also "
         "required.";
97 98 99 100 101 102 103
  DefaultAllocator alloc(detail::GetCorrectPlaceByPlaceType(place));
  impl_ = std::move(std::make_shared<phi::DenseTensor>(
      &alloc,
      std::move(phi::DenseTensorMeta(phi::DataType::FLOAT32,
                                     phi::make_ddim({shape}),
                                     phi::DataLayout::NCHW))));
}
104

105
Tensor::Tensor(std::shared_ptr<phi::TensorBase> tensor_impl,
106 107
               const std::string &name)
    : impl_(std::move(tensor_impl)), name_(std::move(name)) {}
108

109 110 111 112 113 114
/* Part 2: Dimension, DataType and DataLayout methods */

int64_t Tensor::numel() const { return impl_->numel(); }

int64_t Tensor::size() const { return impl_->numel(); }

115
const phi::DDim &Tensor::dims() const { return impl_->dims(); }
116 117

std::vector<int64_t> Tensor::shape() const {
118 119
  auto dims = impl_->dims();
  return phi::vectorize<int64_t>(dims);
120 121 122
}

void Tensor::reshape(const std::vector<int64_t> &shape) {
123 124 125 126 127 128 129 130 131
  LOG_FIRST_N(WARNING, 1)
      << "The function of resetting the shape of the uninitialized "
         "Tensor of the `reshape` method is deprecated since version "
         "2.3, and will be removed in version 2.4, please use "
         "`paddle::empty/full` method to create a new Tensor "
         "instead. "
         "reason: `reshape` means changing the tensor shape without "
         "touching underlying data, this requires the total size of "
         "the tensor to remain constant.";
C
Chen Weihang 已提交
132
  if (is_dense_tensor()) {
133
    static_cast<phi::DenseTensor *>(impl_.get())->Resize(phi::make_ddim(shape));
134
  } else {
135
    PADDLE_THROW(phi::errors::Unimplemented(
136 137
        "Only support reshape operation on DenseTensor now."));
  }
138 139
}

140
DataType Tensor::dtype() const { return impl_->dtype(); }
141

142
DataType Tensor::type() const { return impl_->dtype(); }
143 144 145

DataLayout Tensor::layout() const { return impl_->layout(); }

C
Chen Weihang 已提交
146
bool Tensor::is_dense_tensor() const {
147
  return phi::DenseTensor::classof(impl_.get());
C
Chen Weihang 已提交
148
}
149
bool Tensor::is_selected_rows() const {
150
  return phi::SelectedRows::classof(impl_.get());
151
}
152 153 154 155 156 157
bool Tensor::is_sparse_coo_tensor() const {
  return phi::SparseCooTensor::classof(impl_.get());
}
bool Tensor::is_sparse_csr_tensor() const {
  return phi::SparseCsrTensor::classof(impl_.get());
}
J
Jack Zhou 已提交
158 159 160
bool Tensor::is_string_tensor() const {
  return phi::StringTensor::classof(impl_.get());
}
161 162
/* Part 3: Device and Backend methods */

163
const Place &Tensor::place() const {
164 165 166 167 168 169
  PADDLE_ENFORCE_NOT_NULL(
      impl_,
      phi::errors::PermissionDenied(
          "Null pointer error, the impl_ of Tensor should not be "
          "Null when calling Tensor::place()."));
  return impl_->place();
170 171
}

C
Chen Weihang 已提交
172
bool Tensor::is_cpu() const { return paddle::platform::is_cpu_place(place()); }
173

C
Chen Weihang 已提交
174
bool Tensor::is_gpu() const { return paddle::platform::is_gpu_place(place()); }
175

176
bool Tensor::is_gpu_pinned() const {
C
Chen Weihang 已提交
177
  return paddle::platform::is_cuda_pinned_place(place());
178 179
}

180 181 182 183
bool Tensor::is_custom_device() const {
  return paddle::platform::is_custom_place(place());
}

184 185 186 187
/* Part 4: Data Access methods */

template <typename T>
T *Tensor::mutable_data() {
188 189 190 191 192 193 194 195 196 197
  LOG_FIRST_N(WARNING, 1)
      << "Allocating memory through `mutable_data` method is "
         "deprecated since version 2.3, and `mutable_data` method "
         "will be removed in version 2.4! Please use "
         "`paddle::empty/full` method to create a new "
         "Tensor with allocated memory, and use data<T>() method "
         "to get the memory pointer of tensor instead. "
         "Reason: When calling `mutable_data` to allocate memory, "
         "the place, datatype, and data layout of tensor may be in "
         "an illegal state.";
C
Chen Weihang 已提交
198
  if (is_dense_tensor()) {
199 200
    return static_cast<phi::DenseTensor *>(impl_.get())
        ->mutable_data<T>(place());
201 202 203 204
  }
  return nullptr;
}

205 206 207 208 209 210 211 212
template PADDLE_API float *Tensor::mutable_data<float>();
template PADDLE_API double *Tensor::mutable_data<double>();
template PADDLE_API int64_t *Tensor::mutable_data<int64_t>();
template PADDLE_API int32_t *Tensor::mutable_data<int32_t>();
template PADDLE_API uint8_t *Tensor::mutable_data<uint8_t>();
template PADDLE_API int8_t *Tensor::mutable_data<int8_t>();
template PADDLE_API int16_t *Tensor::mutable_data<int16_t>();
template PADDLE_API bool *Tensor::mutable_data<bool>();
213 214 215 216 217 218
template PADDLE_API phi::dtype::complex<float>
    *Tensor::mutable_data<phi::dtype::complex<float>>();
template PADDLE_API phi::dtype::complex<double>
    *Tensor::mutable_data<phi::dtype::complex<double>>();
template PADDLE_API phi::dtype::float16 *
Tensor::mutable_data<phi::dtype::float16>();
219 220

template <typename T>
221
T *Tensor::mutable_data(const Place &place) {
222 223 224 225 226 227 228 229 230 231
  LOG_FIRST_N(WARNING, 1)
      << "Allocating memory through `mutable_data` method is "
         "deprecated since version 2.3, and `mutable_data` method "
         "will be removed in version 2.4! Please use "
         "`paddle::empty/full` method to create a new "
         "Tensor with allocated memory, and use data<T>() method "
         "to get the memory pointer of tensor instead. "
         "Reason: When calling `mutable_data` to allocate memory, "
         "the datatype, and data layout of tensor may be in "
         "an illegal state.";
232
  if (is_dense_tensor()) {
233
    return static_cast<phi::DenseTensor *>(impl_.get())->mutable_data<T>(place);
234 235
  }
  return nullptr;
236 237
}

238 239 240 241 242 243 244 245
template PADDLE_API float *Tensor::mutable_data<float>(const Place &place);
template PADDLE_API double *Tensor::mutable_data<double>(const Place &place);
template PADDLE_API int64_t *Tensor::mutable_data<int64_t>(const Place &place);
template PADDLE_API int32_t *Tensor::mutable_data<int32_t>(const Place &place);
template PADDLE_API uint8_t *Tensor::mutable_data<uint8_t>(const Place &place);
template PADDLE_API int8_t *Tensor::mutable_data<int8_t>(const Place &place);
template PADDLE_API int16_t *Tensor::mutable_data<int16_t>(const Place &place);
template PADDLE_API bool *Tensor::mutable_data<bool>(const Place &place);
246
template PADDLE_API phi::dtype::complex<float>
247
    *Tensor::mutable_data<phi::dtype::complex<float>>(const Place &place);
248
template PADDLE_API phi::dtype::complex<double>
249
    *Tensor::mutable_data<phi::dtype::complex<double>>(const Place &place);
250
template PADDLE_API phi::dtype::float16 *
251
Tensor::mutable_data<phi::dtype::float16>(const Place &place);
252 253 254

template <typename T>
const T *Tensor::data() const {
C
Chen Weihang 已提交
255
  if (is_dense_tensor()) {
256 257 258
    return static_cast<phi::DenseTensor *>(impl_.get())->data<T>();
  } else if (is_selected_rows()) {
    return static_cast<phi::SelectedRows *>(impl_.get())->value().data<T>();
259 260 261 262
  }
  return nullptr;
}

263 264 265 266 267 268 269 270
template PADDLE_API const float *Tensor::data<float>() const;
template PADDLE_API const double *Tensor::data<double>() const;
template PADDLE_API const int64_t *Tensor::data<int64_t>() const;
template PADDLE_API const int32_t *Tensor::data<int32_t>() const;
template PADDLE_API const uint8_t *Tensor::data<uint8_t>() const;
template PADDLE_API const int8_t *Tensor::data<int8_t>() const;
template PADDLE_API const int16_t *Tensor::data<int16_t>() const;
template PADDLE_API const bool *Tensor::data<bool>() const;
271 272 273 274 275 276 277 278
template PADDLE_API const phi::dtype::complex<float>
    *Tensor::data<phi::dtype::complex<float>>() const;
template PADDLE_API const phi::dtype::complex<double>
    *Tensor::data<phi::dtype::complex<double>>() const;
template PADDLE_API const phi::dtype::float16 *
Tensor::data<phi::dtype::float16>() const;
template PADDLE_API const phi::dtype::bfloat16 *
Tensor::data<phi::dtype::bfloat16>() const;
279 280 281

template <typename T>
T *Tensor::data() {
282
  if (is_dense_tensor()) {
283 284 285
    return static_cast<phi::DenseTensor *>(impl_.get())->data<T>();
  } else if (is_selected_rows()) {
    return static_cast<phi::SelectedRows *>(impl_.get())
286 287 288
        ->mutable_value()
        ->data<T>();
  }
289 290 291
  return nullptr;
}

292 293 294 295 296 297 298 299
template PADDLE_API float *Tensor::data<float>();
template PADDLE_API double *Tensor::data<double>();
template PADDLE_API int64_t *Tensor::data<int64_t>();
template PADDLE_API int32_t *Tensor::data<int32_t>();
template PADDLE_API uint8_t *Tensor::data<uint8_t>();
template PADDLE_API int8_t *Tensor::data<int8_t>();
template PADDLE_API int16_t *Tensor::data<int16_t>();
template PADDLE_API bool *Tensor::data<bool>();
300 301 302 303 304
template PADDLE_API phi::dtype::complex<float>
    *Tensor::data<phi::dtype::complex<float>>();
template PADDLE_API phi::dtype::complex<double>
    *Tensor::data<phi::dtype::complex<double>>();
template PADDLE_API phi::dtype::float16 *Tensor::data<phi::dtype::float16>();
305

306
// TODO(chenweihang): replace slice impl by API
307
Tensor Tensor::slice(int64_t begin_idx, int64_t end_idx) const {
C
Chen Weihang 已提交
308
  if (is_dense_tensor()) {
309 310
    return Tensor(std::make_shared<phi::DenseTensor>(
        std::move(phi::DenseTensorUtils::Slice(
311
            *(static_cast<phi::DenseTensor *>(impl_.get())),
312 313 314
            begin_idx,
            end_idx))));
  } else {
315
    PADDLE_THROW(phi::errors::Unimplemented(
316
        "Only support slice operation on DenseTensor now."));
317
  }
318 319
}

320
const std::shared_ptr<phi::TensorBase> &Tensor::impl() const { return impl_; }
321

322
void Tensor::set_impl(const std::shared_ptr<phi::TensorBase> &impl) {
323 324 325
  impl_ = impl;
}

326 327 328 329
void Tensor::set_impl(std::shared_ptr<phi::TensorBase> &&impl) {
  impl_ = std::move(impl);
}

330 331 332 333 334 335
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
gpuStream_t Tensor::stream() const {
  return platform::stream::get_current_stream(-1)->raw_stream();
}
#endif

336
/* Part 5: Status utils methods */
337 338 339

bool Tensor::defined() const { return impl_ != nullptr; }

340
bool Tensor::initialized() const { return defined() && impl_->initialized(); }
341 342

bool Tensor::is_initialized() const {
343 344 345 346
  LOG_FIRST_N(WARNING, 1)
      << "The `is_initialized` method is deprecated since version "
         "2.3, and will be removed in version 2.4! "
         "Please use `initialized` method instead.";
347
  return defined() && impl_->initialized();
348 349
}

350 351 352 353 354
void Tensor::reset() {
  impl_.reset();
  autograd_meta_.reset();
  name_ = "";
}
355

356
/* Part 6: Operator overloading */
357 358 359 360

Tensor &Tensor::operator=(const Tensor &x) & {
  impl_ = x.impl_;
  autograd_meta_ = x.autograd_meta_;
361
  name_ = x.name_;
362 363 364 365 366 367
  return *this;
}

Tensor &Tensor::operator=(Tensor &&x) & {
  impl_ = std::move(x.impl_);
  autograd_meta_ = std::move(x.autograd_meta_);
368
  name_ = std::move(x.name_);
369 370 371 372 373 374 375
  return *this;
}

AbstractAutogradMeta *Tensor::get_autograd_meta() const {
  return autograd_meta_.get();
}

376 377 378 379 380
const std::shared_ptr<AbstractAutogradMeta> &Tensor::mutable_autograd_meta()
    const {
  return autograd_meta_;
}

381 382 383 384 385
void Tensor::set_autograd_meta(
    std::shared_ptr<AbstractAutogradMeta> autograd_meta) {
  autograd_meta_ = std::move(autograd_meta);
}

386 387 388
void Tensor::bump_inplace_version() {
  if (is_dense_tensor()) {
    auto &inplace_version_counter =
389
        static_cast<phi::DenseTensor *>(impl_.get())->InplaceVersionCounter();
390 391 392 393 394 395 396 397 398 399
    inplace_version_counter.Bump();
  } else {
    PADDLE_THROW(phi::errors::Unimplemented(
        "bump_inplace_version is only supported on DenseTensor now."));
  }
}

uint32_t Tensor::current_inplace_version() {
  if (is_dense_tensor()) {
    auto &inplace_version_counter =
400
        static_cast<phi::DenseTensor *>(impl_.get())->InplaceVersionCounter();
401 402
    return inplace_version_counter.CurrentVersion();
  } else {
403 404
    LOG_FIRST_N(WARNING, 1)
        << "current_inplace_version is only supported on DenseTensor now.";
405 406 407 408
  }
  return 0;
}

409 410 411 412
void Tensor::reset_inplace_version(bool set_to_zero) {
  if (set_to_zero) {
    if (is_dense_tensor()) {
      auto &inplace_version_counter =
413
          static_cast<phi::DenseTensor *>(impl_.get())->InplaceVersionCounter();
414 415 416 417 418
      inplace_version_counter.SetInplaceVersionToZero();
    }
  }
}

419 420
}  // namespace experimental
}  // namespace paddle