tensor.h 18.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* 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. */

#pragma once

#include <functional>
#include <memory>
#include <utility>
20
#include <vector>
21

22 23 24 25
#ifdef PADDLE_WITH_CUDA
#include <cuda_runtime.h>
using gpuStream_t = cudaStream_t;
#endif
26

27 28 29 30 31
#ifdef PADDLE_WITH_HIP
#include <hip/hip_runtime.h>
using gpuStream_t = hipStream_t;
#endif

32
#include "paddle/phi/api/include/dll_decl.h"
33
#include "paddle/phi/common/data_type.h"
34
#include "paddle/phi/common/int_array.h"
35 36
#include "paddle/phi/common/layout.h"
#include "paddle/phi/common/place.h"
37
#include "paddle/phi/common/scalar.h"
38 39

namespace phi {
40
class DenseTensor;
41
}  // namespace phi
42

43
namespace phi {
44
class TensorBase;
45
class DDim;
46
}  // namespace phi
47 48

namespace paddle {
49 50 51
// TODO(chenweihang): Remove the experimental namespace for Scalar and IntArray
using Scalar = experimental::Scalar;
using IntArray = experimental::IntArray;
52 53 54 55 56 57 58 59 60

class AbstractAutogradMeta {
 public:
  // No AbstractAutogradMeta should be created
  virtual ~AbstractAutogradMeta() {}
};

/**
 * Tensor is the API description of the basic data structure in the
61
 * [ "Paddle HIgh reusability operator (phi)" Library ].
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
 *
 * It is not limited to a simple n-dimensional array.
 * It contains a smart pointer to `TensorImpl`. The data description contained
 * in Tensor is defined by TensorImpl. Tensor only defines the interface for
 * computation.
 *
 * Note: Tensor can be NULL state, Tensor is meaningful only when the
 * TensorImpl to which it is pointed is not empty.
 *
 * Note: For the consistency of C++ API self, and the consistency between C++
 * API and Python API, all member methods of Tensor are named with lowercase
 * letters and underscores.
 *
 * Note: Tensor cannot be inherited. The heterogeneous Tensor implementation
 * can be achieved by inheriting the underlying TensorBase.
 *
78 79
 * Note: This Tensor API is suitable for training,inference and custom
 * operators.
80 81
 */

82
class PADDLE_API Tensor final {
83
 public:
84 85
  /* Part 1: Construction and destruction methods */

86 87 88 89 90 91 92 93
  /**
   * @brief Construct a new Tensor object
   */
  Tensor() = default;

  /**
   * @brief Construct a new Tensor object by copy
   */
94
  Tensor(const Tensor&) = default;
95 96 97 98

  /**
   * @brief Construct a new Tensor object by move
   */
99 100 101
  Tensor(Tensor&&) = default;

  /**
102 103 104 105
   * @brief Construct a new Tensor object by a TensorBase pointer
   *
   * @param tensor_impl
   */
106
  explicit Tensor(std::shared_ptr<phi::TensorBase> tensor_impl);
107 108 109

  /**
   * @brief Construct a new Tensor object on the target place.
110 111
   *
   * This is a deprecated method and may be removed in the future!!!
112 113
   *
   * @param place
114
   */
115
  explicit Tensor(const Place& place);
116 117 118 119

  /**
   * @brief Construct a new Tensor object on the target place
   * with specified shape.
120 121
   *
   * This is a deprecated method and may be removed in the future!!!
122 123 124 125
   *
   * @param place
   * @param shape
   */
126
  Tensor(const Place& place, const std::vector<int64_t>& shape);
127

128 129 130 131 132
  /**
   * @brief Construct a new Tensor object by a TensorBase pointer and name
   *
   * @param tensor_impl
   */
133
  Tensor(std::shared_ptr<phi::TensorBase> tensor_impl, const std::string& name);
134

J
Jiabin Yang 已提交
135
  /**
136
   * @brief Construct a new Tensor object with name
J
Jiabin Yang 已提交
137
   *
138 139 140
   * @note Internal method, used to adapt original execution mechanism and
   * debug analysis in the development of new dygraph. It may be removed in
   * the future.
141 142
   * */
  explicit Tensor(const std::string& name) : name_(name) {}
J
Jiabin Yang 已提交
143

144
  /* Part 2: Dimension, DataType and DataLayout methods */
145 146 147 148 149 150 151 152 153 154

  /**
   * @brief Return the number of elements of Tensor.
   *
   * @return int64_t
   */
  int64_t numel() const;

  /**
   * @brief Get the size of current tensor.
155
   *
156 157 158 159 160 161 162
   * The compatible method of `Tensor::numel()`.
   * This is a deprecated method and may be removed in the future!
   *
   * @return int64_t
   */
  int64_t size() const;

163
  /**
164 165
   * @brief Return the dimensions of Tensor.
   *
166
   * @return phi::DDim
167
   */
168
  const phi::DDim& dims() const;
169 170

  /**
171
   * @brief Return the shape (dimensions) of Tensor.
172
   *
173 174 175 176 177 178 179 180 181
   * The compatible method of `Tensor::dims()`.
   * This is a deprecated method and may be removed in the future!
   *
   * @return std::vector<int64_t>
   */
  std::vector<int64_t> shape() const;

  /**
   * @brief Reset the shape of the tensor.
182
   * @note: This method means Reset the shape of the tensor,
183
   * and must be called before calling mutable_data() or
184
   * copy_to(const Place& place), this is not a standard definition of
185
   * reshape behavior, so we will deprecated this feature in the future.
186 187 188 189 190 191 192 193 194
   *
   * @param shape
   */
  void reshape(const std::vector<int64_t>& shape);

  /**
   * @brief Return the data type of Tensor.
   *
   * @return DataType
195
   */
196
  DataType dtype() const;
197 198

  /**
199
   * @brief Return the data type of Tensor.
200
   *
201 202 203 204
   * The compatible method of `Tensor::dtype()`.
   * This is a deprecated method and may be removed in the future!
   *
   * @return DataType
205
   */
206
  DataType type() const;
207 208

  /**
209 210 211
   * @brief Return the layout of Tensor.
   *
   * @return DataLayout
212
   */
213
  phi::DataLayout layout() const;
214

C
Chen Weihang 已提交
215 216 217
  /**
   * @brief Determine whether tensor is DenseTensor
   *
张春乔 已提交
218
   * @return bool
C
Chen Weihang 已提交
219 220 221
   */
  bool is_dense_tensor() const;

L
LiYuRio 已提交
222 223 224 225 226 227 228
  /**
   * @brief Determine whether tensor is DistTensor
   *
   * @return bool
   */
  bool is_dist_tensor() const;

229 230 231
  /**
   * @brief Determine whether tensor is SelectedRows
   *
张春乔 已提交
232
   * @return bool
233 234 235
   */
  bool is_selected_rows() const;

236 237 238
  /**
   * @brief Determine whether tensor is SparseCooTensor
   *
张春乔 已提交
239
   * @return bool
240 241 242 243 244 245
   */
  bool is_sparse_coo_tensor() const;

  /**
   * @brief Determine whether tensor is SparseCsrTensor
   *
张春乔 已提交
246
   * @return bool
247 248 249
   */
  bool is_sparse_csr_tensor() const;

J
Jack Zhou 已提交
250 251 252
  /**
   * @brief Determine whether tensor is StringTensor
   *
张春乔 已提交
253
   * @return bool
J
Jack Zhou 已提交
254 255 256
   */
  bool is_string_tensor() const;

257
  /* Part 3: Device and Backend methods */
258

259
  /**
260 261
   * @brief Return the place (device) of Tensor.
   *
262
   * @return Place
263
   */
264
  const Place& place() const;
265 266

  /**
267 268
   * @brief Determine whether the tensor device is CPU
   *
张春乔 已提交
269
   * @return bool
270
   */
271 272 273
  bool is_cpu() const;

  /**
274
   * @brief Determine whether the tensor device is GPU
275
   *
张春乔 已提交
276
   * @return bool
277
   */
278 279 280 281 282
  bool is_gpu() const;

  /**
   * @brief Determine whether the tensor device is GPU_PINNED
   *
张春乔 已提交
283
   * @return bool
284 285
   */
  bool is_gpu_pinned() const;
286

C
Chen Weihang 已提交
287 288 289
  /**
   * @brief Determine whether the tensor device is XPU
   *
张春乔 已提交
290
   * @return bool
C
Chen Weihang 已提交
291 292 293
   */
  bool is_xpu() const;

294 295 296
  /**
   * @brief Determine whether the tensor device is CustomDevice
   *
张春乔 已提交
297
   * @return bool
298 299 300
   */
  bool is_custom_device() const;

301
  /* Part 4: Data Access methods */
302 303 304

  /**
   * @brief Get the memory pointer in CPU or GPU with specific data type.
305
   * It's usually used to get the output data pointer, same as the T* data().
306 307 308 309 310 311 312
   *
   * @tparam T
   * @return T*
   */
  template <typename T>
  T* mutable_data();

313
  /**
314
   * @brief Get the memory pointer in CPU or GPU with specific data type.
315
   *
316 317 318 319 320 321 322 323
   * It's usually used to get the output data pointer.
   * This is a deprecated method and may be removed in the future!
   *
   * @tparam T
   * @param place
   * @return T*
   */
  template <typename T>
324
  T* mutable_data(const Place& place);
325 326 327 328 329 330 331 332 333 334 335 336 337

  /**
   * @brief Get the const memory pointer directly.
   * It's usually used to get the output data pointer.
   *
   * @tparam T
   * @return T*
   */
  template <typename T>
  const T* data() const;

  /**
   * @brief Get the memory pointer directly.
338
   * It's usually used to get the mutable output data pointer.
339 340 341 342 343 344 345
   *
   * @tparam T
   * @return T*
   */
  template <typename T>
  T* data();

346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
  /**
   * @brief Get the const memory pointer directly.
   * It's usually used to get the output data pointer.
   *
   * @tparam T
   * @return T*
   */
  const void* data() const;

  /**
   * @brief Get the memory pointer directly.
   * It's usually used to get the mutable output data pointer.
   *
   * @tparam T
   * @return T*
   */
  void* data();

364 365 366 367 368 369 370 371 372 373 374 375
  /**
   * @brief Return a sub-tensor of the given tensor.
   * It is usually used to extract a sub-tensor (which supports
   * modifying the data of the original tensor) to perform further
   * operations.
   *
   * @param begin_idx The index of the start row (inclusive) to slice.
   *                  The index number begins from 0.
   * @param end_idx The index of the end row (exclusive) to slice.
   *                 The index number begins from begin_idx + 1.
   * @return Tensor
   */
376
  Tensor slice(int64_t begin_idx, int64_t end_idx) const;
377 378

  /**
379
   * @brief Return the implementation of current Tensor.
380
   *
381
   * @return std::shared_ptr<phi::TensorBase>
382
   */
383
  const std::shared_ptr<phi::TensorBase>& impl() const;
384 385

  /**
386
   * @brief Set the implementation of current Tensor.
387 388 389
   *
   * @param impl
   */
390
  void set_impl(const std::shared_ptr<phi::TensorBase>& impl);
391

392
  /**
393
   * @brief Set the implementation of current Tensor.
394 395 396 397 398
   *
   * @param impl
   */
  void set_impl(std::shared_ptr<phi::TensorBase>&& impl);

399 400 401 402 403 404 405 406 407 408
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
  /**
   * @brief Get the stream where the tensor is currently located
   * This is a deprecated method and may be removed in the future!
   *
   * @return gpuStream_t
   */
  gpuStream_t stream() const;
#endif

409 410 411
  /**
   * @brief Return the name of Tensor.
   * @note Used to adapt original execution mechanism and debug analysis
412
   * in the development of new dygraph.
413 414 415
   *
   * @return const std::string&
   */
416
  const std::string& name() const;
417 418 419 420

  /**
   * @brief Set name of Tensor.
   * @note Used to adapt original execution mechanism and debug analysis
421
   * in the development of new dygraph.
422 423 424
   *
   * @param const std::string& name
   */
425
  void set_name(const std::string& name);
426

427
  /* Part 5: Data Transform methods */
428 429
  /* Alert!!!!: All copy method can only deep copy impl, autograd info only be
   * copied */
430
  /* out of phi */
431 432
  /**
   * @brief Copy the current Tensor data to the specified device
433
   * and return the new Tensor. It's usually used to set the input tensor data.
434 435 436 437
   * @note The Tensor's `copy_to` method is deprecated since version 2.3, and
   * will be removed in version 2.4, please use `copy_to` method without
   * template argument instead.
   * reason: copying a Tensor to another device does not need to specify the
438
   * data type template argument
439 440
   *
   * @tparam T
441
   * @param target_place The target place of which the tensor will copy to.
442
   * @return Tensor
443
   */
444
  template <typename T>
445
  Tensor copy_to(const Place& target_place) const;
446 447

  /**
448 449
   * @brief Transfer the current Tensor to the specified device and return.
   *
450 451
   * @param place The target place of which the tensor will copy to.
   * @param blocking Should we copy this in sync way.
452 453
   * @return Tensor
   */
454
  Tensor copy_to(const Place& place, bool blocking) const;
455 456

  /**
457 458
   * @brief Transfer the source Tensor to current Tensor.
   *
459 460
   * @param src The source Tensor to be copied.
   * @param blocking Should we copy this in sync way.
461 462
   * @return void
   */
463 464
  void copy_(const Tensor& src, const Place& target_place, bool blocking);

465
  /**
466 467 468 469
   * @brief Cast datatype from one to another
   *
   * @param target_type
   * @return Tensor
470
   */
471
  Tensor cast(DataType target_type) const;
472

473
  /* Part 6: Status utils methods */
474

475 476 477
  /**
   * @brief Determine whether it is a meaningful Tensor
   *
张春乔 已提交
478
   * @return bool
479 480 481 482 483 484
   */
  bool defined() const;

  /**
   * @brief Determine whether Tensor is initialized.
   *
张春乔 已提交
485
   * @return bool
486 487 488 489 490 491 492
   */
  bool initialized() const;

  /**
   * @brief Determine whether Tensor is initialized.
   * This is a deprecated method and may be removed in the future!
   *
张春乔 已提交
493
   * @return bool
494 495
   */
  bool is_initialized() const;
496 497

  /**
498
   * @brief Reset the Tensor implementation
499
   */
500 501 502
  void reset();

  /* Part 7: Operator overloading */
503 504

  /**
505 506 507 508
   * @brief Assignment operator
   *
   * @param x
   * @return Tensor&
509
   */
510
  Tensor& operator=(const Tensor& x) &;
511 512

  /**
513 514 515 516
   * @brief Move assignment operator
   *
   * @param x
   * @return Tensor&
517
   */
518
  Tensor& operator=(Tensor&& x) &;
519

520 521 522 523 524 525 526 527 528 529
  /**
   * @brief Tensor operants
   *
   * @param other
   * @return Tensor
   */
  Tensor operator+(const Tensor& other) const;
  Tensor operator-(const Tensor& other) const;
  Tensor operator*(const Tensor& other) const;
  Tensor operator/(const Tensor& other) const;
530 531 532 533
  Tensor operator+(const Scalar& other) const;
  Tensor operator-(const Scalar& other) const;
  Tensor operator*(const Scalar& other) const;
  Tensor operator/(const Scalar& other) const;
534 535 536 537 538 539
  Tensor operator<(const Tensor& other) const;
  Tensor operator<=(const Tensor& other) const;
  Tensor operator==(const Tensor& other) const;
  Tensor operator!=(const Tensor& other) const;
  Tensor operator>(const Tensor& other) const;
  Tensor operator>=(const Tensor& other) const;
540
  Tensor operator-() const;
541 542 543 544 545
  Tensor operator~() const;
  Tensor operator&(const Tensor& other) const;
  Tensor operator|(const Tensor& other) const;
  Tensor operator^(const Tensor& other) const;

546
  /* Part 8: Autograd methods */
547

548
  /**
549
   * @brief Get the autograd meta object pointer
550 551 552 553
   *
   * @return AbstractAutogradMeta*
   */
  AbstractAutogradMeta* get_autograd_meta() const;
554 555 556 557 558 559

  /**
   * @brief Get the shared pointer of autograd meta object
   *
   * @return std::shared_ptr<AbstractAutogradMeta>&
   */
560
  const std::shared_ptr<AbstractAutogradMeta>& mutable_autograd_meta() const;
561

562 563 564 565 566 567
  /**
   * @brief Set the autograd meta object
   *
   * @param autograd_meta
   */
  void set_autograd_meta(std::shared_ptr<AbstractAutogradMeta> autograd_meta);
568

569 570 571 572 573 574 575 576 577 578 579 580 581 582
  /* Part 9: Inplace methods */

  /**
   * @brief Increase inplace version
   */
  void bump_inplace_version();

  /**
   * @brief Get current inplace version
   *
   * @return uint32_t
   */
  uint32_t current_inplace_version();

583 584 585 586 587
  /**
   * @brief Reset inplace version
   */
  void reset_inplace_version(bool set_to_zero = false);

588
  /* Part 10: Auto generated Tensor methods */
589

590
  /* Part 11: Methods of converting underlying TensorType to each other
591 592 593 594
   */
  /**
   * @brief Convert DenseTensor or SparseCsrTensor to SparseCooTensor
   *
595
   * @param sparse_dim The number of sparse dimensions
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
   * @return Tensor
   */
  Tensor to_sparse_coo(const int64_t sparse_dim) const;

  /**
   * @brief Convert DenseTensor or SparseCooTensor to SparseCsrTensor
   *
   * @return Tensor
   */
  Tensor to_sparse_csr() const;

  /**
   * @brief Convert SparseCooTensor or SparseCsrTensor to DenseTensor
   *
   * @return Tensor
   */
  Tensor to_dense() const;

614 615 616 617 618 619 620 621
 private:
  /**
   * [ Why use abstract TensorImpl interface here? ]
   *
   * We hope that the data structure at the API level of the framework can be
   * unified to Tensor, but Tensor itself is heterogeneous.
   *
   * Tensor can generally be represented by void* and size_t, place.
张春乔 已提交
622
   * This is suitable for most scenarios including CPU, GPU, HIP, etc.,
623 624 625 626 627 628 629 630 631 632
   * but there are a few cases where this definition cannot be described,
   * such as the Tensor representation in third-party lib such as Metal,
   * OpenCL, etc., as well as some special Tensor implementations, including
   * Tensor containing only one Scalar value, or Tensor representing String,
   * etc.
   *
   * Therefore, we hope to use a unified interface to shield the underlying
   * heterogeneous Tensor implementation, so that the API level can be unified
   * to one `Tensor`.
   */
H
hong 已提交
633
  std::shared_ptr<phi::TensorBase> impl_{nullptr};
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648

  /**
   * [ Why need abstract AbstractAutogradMeta here? ]
   *
   * Dynamic graphs need to hold backward information
   *
   * [ Why AutogradMeta not in TensorImpl? ]
   *
   * 1. AutogradMeta is only used in dynamic graph, It is execution-related
   *    information, not Tensor data description-related information.
   * 2. Kernel calculation does not require AutogradMeta.
   */
  std::shared_ptr<AbstractAutogradMeta> autograd_meta_{nullptr};

  /**
649
   * Tensor name: used to adapt original execution mechanism and debug analysis
650
   * in the development of new dygraph.
651
   */
652
  std::string name_{""};
653

654
 public:
655 656 657 658 659 660
  // Tensor C++ APIs
  // Example: Tensor add(const Tensor& other) const;
  Tensor add(const Tensor& y) const;
  Tensor divide(const Tensor& y) const;
  Tensor multiply(const Tensor& y) const;
  Tensor subtract(const Tensor& y) const;
661 662 663 664
  Tensor add(const Scalar& y) const;
  Tensor divide(const Scalar& y) const;
  Tensor multiply(const Scalar& y) const;
  Tensor subtract(const Scalar& y) const;
665 666 667 668 669 670
  Tensor less_equal(const Tensor& y) const;
  Tensor less_than(const Tensor& y) const;
  Tensor equal(const Tensor& y) const;
  Tensor not_equal(const Tensor& y) const;
  Tensor greater_equal(const Tensor& y) const;
  Tensor greater_than(const Tensor& y) const;
671 672 673 674
  Tensor bitwise_and(const Tensor& y) const;
  Tensor bitwise_or(const Tensor& y) const;
  Tensor bitwise_xor(const Tensor& y) const;
  Tensor bitwise_not() const;
675 676
  Tensor pow(const Tensor& y) const;
  Tensor pow(const Scalar& y) const;
677

678 679 680 681
  Tensor exp() const;
  Tensor floor() const;
  Tensor gather_nd(const Tensor& index) const;
  Tensor log() const;
682 683
  Tensor roll(const IntArray& shifts = {},
              const std::vector<int64_t>& axis = {}) const;
684 685
  Tensor scatter(const Tensor& index,
                 const Tensor& updates,
686
                 bool overwrite = true) const;
687 688 689 690 691
  Tensor scatter_nd_add(const Tensor& index, const Tensor& updates) const;
  Tensor abs() const;
  Tensor assign() const;
  Tensor elementwise_pow(const Tensor& y) const;
  Tensor expand(const IntArray& shape) const;
692 693 694 695
  Tensor matmul(const Tensor& y,
                bool transpose_x = false,
                bool transpose_y = false) const;
  Tensor max(const IntArray& axis = {}, bool keepdim = false) const;
696 697
  Tensor maximum(const Tensor& y) const;
  Tensor minimum(const Tensor& y) const;
698 699 700 701 702 703 704
  Tensor scale(const Scalar& scale = 1.0,
               float bias = 0.0,
               bool bias_after_scale = true) const;
  Tensor sum(const IntArray& axis = {},
             DataType dtype = DataType::UNDEFINED,
             bool keepdim = false) const;
  Tensor tile(const IntArray& repeat_times = {}) const;
705 706
};

707 708 709 710 711 712 713 714
PADDLE_API Tensor operator+(const Scalar& x, const Tensor& y);

PADDLE_API Tensor operator-(const Scalar& x, const Tensor& y);

PADDLE_API Tensor operator*(const Scalar& x, const Tensor& y);

PADDLE_API Tensor operator/(const Scalar& x, const Tensor& y);

715
}  // namespace paddle