forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tensor.cpp
166 lines (136 loc) · 4.64 KB
/
Tensor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <ATen/core/Tensor.h>
#include <ATen/core/Formatting.h>
#include <ATen/core/VariableHooksInterface.h>
#include <ATen/core/LegacyTypeDispatch.h>
#include <ATen/FunctionalTensorWrapper.h>
#ifndef AT_PER_OPERATOR_HEADERS
#include <ATen/MethodOperators.h>
#else
#include <ATen/ops/contiguous_ops.h>
#include <ATen/ops/fill_ops.h>
#include <ATen/ops/to_ops.h>
#include <ATen/ops/zero_ops.h>
#endif
#include <iostream>
namespace at {
const TensorBase& get_tensor_base(const Tensor &t) {
return t;
}
TensorBase TensorBase::__dispatch_contiguous(c10::MemoryFormat memory_format) const {
OptionalTensorRef self(*this);
return at::_ops::contiguous::call(*self, memory_format);
}
const TensorBase& TensorBase::fill_(const c10::Scalar &fill_value) const {
Tensor self(*this);
at::_ops::fill__Scalar::call(self, fill_value);
return *this;
}
const TensorBase& TensorBase::zero_() const {
Tensor self(*this);
at::_ops::zero_::call(self);
return *this;
}
TensorBase TensorBase::to(
at::TensorOptions options,
bool non_blocking,
bool copy,
c10::optional<at::MemoryFormat> memory_format) const {
Tensor self(*this);
return at::_ops::to_dtype_layout::call(
self, optTypeMetaToScalarType(options.dtype_opt()),
options.layout_opt(), options.device_opt(),
options.pinned_memory_opt(), non_blocking, copy, memory_format);
}
void TensorBase::enforce_invariants() {
if (impl_.get() == nullptr) {
throw std::runtime_error("TensorImpl with nullptr is not supported");
}
// Following line throws if the method is not a POD data type or is not
// supported by ATen
scalar_type();
if (defined()) {
TORCH_INTERNAL_ASSERT(
impl_->dtype_initialized(),
"Partially-initialized tensor not supported by Tensor");
TORCH_INTERNAL_ASSERT(
!impl_->is_sparse(),
"Sparse Tensors are supported by Tensor, but invariant checking isn't implemented. Please file a bug.");
TORCH_INTERNAL_ASSERT(
!impl_->has_storage() || impl_->is_meta() || impl_->storage_initialized(),
"Partially-initialized tensor not supported by Tensor");
}
}
void TensorBase::print() const {
if (defined()) {
std::cerr << "[" << toString() << " " << sizes() << "]" << std::endl;
} else {
std::cerr << "[UndefinedTensor]" << std::endl;
}
}
std::string TensorBase::toString() const {
std::string base_str;
if (scalar_type() == ScalarType::Undefined) {
base_str = "UndefinedType";
} else {
base_str = std::string(at::toString(options().computeDispatchKey())) + at::toString(scalar_type()) + "Type";
}
return base_str;
}
TensorBase TensorBase::variable_data() const {
return impl::GetVariableHooks()->variable_data(*this);
}
TensorBase TensorBase::tensor_data() const {
return impl::GetVariableHooks()->tensor_data(*this);
}
bool TensorBase::is_leaf() const {
return impl::GetVariableHooks()->is_leaf(*this);
}
int64_t TensorBase::output_nr() const {
return impl::GetVariableHooks()->output_nr(*this);
}
void TensorBase::set_data(const TensorBase & new_data) const {
impl::GetVariableHooks()->set_data(*this, new_data);
}
TensorBase TensorBase::data() const {
return impl::GetVariableHooks()->data(*this);
}
int64_t TensorBase::_version() const {
return impl::GetVariableHooks()->_version(*this);
}
void TensorBase::retain_grad() const {
impl::GetVariableHooks()->retain_grad(*this);
}
bool TensorBase::retains_grad() const {
return impl::GetVariableHooks()->retains_grad(*this);
}
void Tensor::_backward(TensorList inputs,
const c10::optional<Tensor>& gradient,
c10::optional<bool> keep_graph,
bool create_graph) const {
return impl::GetVariableHooks()->_backward(*this, inputs, gradient, keep_graph, create_graph);
}
const TensorBase& TensorBase::requires_grad_(bool _requires_grad) const {
impl::GetVariableHooks()->requires_grad_(*this, _requires_grad);
return *this;
}
// View Methods
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bool TensorBase::is_view() const {
return impl::GetVariableHooks()->is_view(*this);
}
const TensorBase& TensorBase::_base() const {
return impl::GetVariableHooks()->base(*this);
}
const std::string& TensorBase::name() const {
return impl::GetVariableHooks()->name(*this);
}
const std::shared_ptr<torch::autograd::Node>& TensorBase::grad_fn() const {
return impl::GetVariableHooks()->grad_fn(*this);
}
void TensorBase::remove_hook(unsigned pos) const {
impl::GetVariableHooks()->remove_hook(*this, pos);
}
unsigned TensorBase::_register_hook(std::function<TensorBase(const TensorBase&)> hook) const {
return impl::GetVariableHooks()->_register_hook(*this, std::move(hook));
}
} // namespace at