3 #include <ATen/core/Tensor.h> 4 #include <c10/core/Scalar.h> 5 #include <c10/macros/Macros.h> 6 #include <ATen/core/SparseTensorRef.h> 7 #include <ATen/core/Type.h> 8 #include <c10/core/TensorOptions.h> 12 inline Tensor Tensor::toType(
const Type & t,
bool non_blocking)
const {
15 return t.copy(*
this, non_blocking);
18 inline Tensor Tensor::cpu()
const {
19 return toType(type().cpu());
22 inline Tensor Tensor::cuda()
const {
23 return toType(type().cuda());
26 inline Tensor Tensor::hip()
const {
27 return toType(type().hip());
30 inline Tensor & Tensor::copy_(
const Tensor & src,
bool non_blocking) {
31 return type().copy_(*
this, src, non_blocking);
34 inline Tensor Tensor::toType(ScalarType t)
const {
35 return toType(type().toScalarType(t));
38 inline Tensor Tensor::toBackend(Backend b)
const {
39 return toType(type().toBackend(b));
53 type().backward(*
this, std::move(gradient), keep_graph, create_graph);
56 inline void Tensor::set_data(
Tensor new_data) {
57 type().set_data(*
this, new_data);
61 ${tensor_method_definitions}
68 return impl_->
dtype();
72 return impl_->layout();
76 return impl_->device();
81 return impl_->get_device();
85 return self.get_device();
90 return impl_->is_cuda();
94 return self.is_cuda();
99 return impl_->is_hip();
103 return self.is_hip();
108 return impl_->is_sparse();
112 return self.is_sparse();
115 #define DEFINE_CAST(T, name, _) \ 117 inline T* Tensor::data() const { \ 119 scalar_type() == ScalarType::name, \ 120 "expected scalar type ", \ 123 c10::toString(scalar_type())); \ 124 return static_cast<T*>(this->data_ptr()); \ 127 AT_FORALL_SCALAR_TYPES_WITH_COMPLEX_EXCEPT_COMPLEX_HALF(DEFINE_CAST)
130 #define DEFINE_ITEM(T, name, _) \ 132 inline T Tensor::item() const { \ 133 return item().to##name(); \ 136 AT_FORALL_SCALAR_TYPES_WITH_COMPLEX_EXCEPT_COMPLEX_HALF(DEFINE_ITEM)
void backward(c10::optional< Tensor > gradient=c10::nullopt, bool keep_graph=false, bool create_graph=false)
Computes the gradient of current tensor w.r.t. graph leaves.
bool is_variable() const
True if a tensor is a variable.
bool is_hip() const
Returns if a Tensor has HIP backend.
TensorOptions options() const
Returns the TensorOptions corresponding to this Tensor.
const caffe2::TypeMeta & dtype() const
Returns the TypeMeta of a tensor, which describes what data type it is (e.g., int, float, ...)
int64_t get_device() const
Returns a Tensor's device index.
Layout layout() const noexcept
Returns a Tensor's layout. Defined in Type.h.
caffe2::TypeMeta dtype() const noexcept
Returns a Tensor's dtype (TypeMeta). Defined in TensorMethods.h.
bool is_variable() const noexcept
Returns true if the Tensor is actually a torch::autograd::Variable.
bool is_cuda() const
Returns if a Tensor has CUDA backend.
Device device() const
Returns a Tensor's device.
TensorOptions(T &&device)
A class to encapsulate construction axes of an Tensor.
bool is_sparse() const
Returns if a Tensor has sparse backend.
Flush-To-Zero and Denormals-Are-Zero mode.