forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTensor.h
49 lines (39 loc) · 1.13 KB
/
Tensor.h
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
#pragma once
#include <ATen/core/TensorBody.h>
#include <c10/util/Exception.h>
namespace at {
class TORCH_API OptionalTensorRef {
public:
OptionalTensorRef() {}
~OptionalTensorRef() {
ref_.unsafeReleaseTensorImpl();
}
OptionalTensorRef(const Tensor& src)
: ref_(c10::intrusive_ptr<TensorImpl>(
src.unsafeGetTensorImpl(),
c10::raw::DontIncreaseRefcount{})) {
TORCH_INTERNAL_ASSERT_DEBUG_ONLY(src.defined());
}
OptionalTensorRef(const OptionalTensorRef& rhs)
: OptionalTensorRef(rhs.ref_) {}
OptionalTensorRef& operator=(const OptionalTensorRef& rhs) {
// Need to call unsafeReleaseTensorImpl on ref_ since we are reassigning it
// (which does not call the destructor).
ref_.unsafeReleaseTensorImpl();
ref_ = Tensor(c10::intrusive_ptr<TensorImpl>(
rhs.ref_.unsafeGetTensorImpl(), c10::raw::DontIncreaseRefcount{}));
return *this;
}
bool has_value() const {
return ref_.defined();
}
const Tensor& getTensorRef() const & {
return ref_;
}
operator bool() const {
return ref_.defined();
}
private:
Tensor ref_;
};
} // namespace at