forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTensorProperties.cpp
63 lines (54 loc) · 2.02 KB
/
TensorProperties.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
#include <ATen/ATen.h>
#include <ATen/NativeFunctions.h>
#include <ATen/WrapDimUtils.h>
#include <ATen/detail/CUDAHooksInterface.h>
#include <ATen/Config.h>
namespace at {
namespace native {
bool is_same_size(const Tensor& self, const Tensor& other) {
return self.sizes().equals(other.sizes());
}
int64_t size(const Tensor& self, int64_t dim) {
// false is passed to maybe_wrap_dim so behavior is identical to array access (but with wrapping)
dim = maybe_wrap_dim(dim, self.dim(), false);
return self.sizes()[dim];
}
int64_t stride(const Tensor& self, int64_t dim) {
// false is passed to maybe_wrap_dim so behavior is identical to array access (but with wrapping)
dim = maybe_wrap_dim(dim, self.dim(), false);
return self.strides()[dim];
}
bool cudnn_is_acceptable(const Tensor& self) {
if (!globalContext().userEnabledCuDNN()) return false;
if (!self.is_cuda()) return false;
auto st = self.type().scalarType();
if (!(st == kDouble || st == kFloat || st == kHalf)) return false;
if (!detail::getCUDAHooks().compiledWithCuDNN()) return false;
// cuDNN functions like grid_sampler returns CUDNN_STATUS_BAD_PARAM on empty
// tensors. Maybe some cuDNN functions actually support empty tensors, but
// native/THNN kernels shouldn't be much slower because the output is also
// likely empty.
if (self.numel() == 0) return false;
// NB: In the old Python code, there was also a test to see if the
// cuDNN library was actually dynamically linked or not. I'm not
// sure if we can actually test this.
return true;
}
Tensor detach(const Tensor& self) {
// this just exists to give us a hook in VariableType and an entry in Declarations.yaml
AT_ERROR("detach is not implemented for Tensor");
return self;
}
Tensor & detach_(Tensor & self) {
// this just exists to give us a hook in VariableType and an entry in Declarations.yaml
AT_ERROR("detach_ is not implemented for Tensor");
return self;
}
Tensor contiguous(const Tensor & self) {
if (self.is_contiguous()) {
return self;
}
return self.clone();
}
}
}