Caffe2 - C++ API
A deep learning, cross platform ML framework
tensor_options.cpp
1 #include <gtest/gtest.h>
2 
3 #include <torch/types.h>
4 
5 #include <ATen/Context.h>
6 #include <ATen/Functions.h>
7 #include <c10/core/TensorOptions.h>
8 
9 #include <string>
10 #include <vector>
11 
12 using namespace at;
13 
14 // A macro so we don't lose location information when an assertion fails.
15 #define REQUIRE_OPTIONS(device_, index_, type_, layout_) \
16  ASSERT_EQ(options.device().type(), Device((device_), (index_)).type()); \
17  ASSERT_TRUE( \
18  options.device().index() == Device((device_), (index_)).index()); \
19  ASSERT_EQ(options.dtype(), (type_)); \
20  ASSERT_TRUE(options.layout() == (layout_))
21 
22 #define REQUIRE_TENSOR_OPTIONS(device_, index_, type_, layout_) \
23  ASSERT_EQ(tensor.device().type(), Device((device_), (index_)).type()); \
24  ASSERT_EQ(tensor.device().index(), Device((device_), (index_)).index()); \
25  ASSERT_EQ(tensor.scalar_type(), (type_)); \
26  ASSERT_TRUE(tensor.type().layout() == (layout_))
27 
28 TEST(TensorOptionsTest, DefaultsToTheRightValues) {
29  TensorOptions options;
30  REQUIRE_OPTIONS(kCPU, -1, kFloat, kStrided);
31 }
32 
33 TEST(TensorOptionsTest, ReturnsTheCorrectType) {
34  auto options = TensorOptions().device(kCPU).dtype(kInt).layout(kSparse);
35  ASSERT_TRUE(
36  at::getType(options) == getNonVariableType(Backend::SparseCPU, kInt));
37 }
38 
39 TEST(TensorOptionsTest, UtilityFunctionsReturnTheRightTensorOptions) {
40  auto options = dtype(kInt);
41  REQUIRE_OPTIONS(kCPU, -1, kInt, kStrided);
42 
43  options = layout(kSparse);
44  REQUIRE_OPTIONS(kCPU, -1, kFloat, kSparse);
45 
46  options = device({kCUDA, 1});
47  REQUIRE_OPTIONS(kCUDA, 1, kFloat, kStrided);
48 
49  options = device_index(1);
50  REQUIRE_OPTIONS(kCUDA, 1, kFloat, kStrided);
51 
52  options = dtype(kByte).layout(kSparse).device(kCUDA, 2).device_index(3);
53  REQUIRE_OPTIONS(kCUDA, 3, kByte, kSparse);
54 }
55 
56 TEST(TensorOptionsTest, ConstructsWellFromCPUTypes) {
57  TensorOptions options;
58  REQUIRE_OPTIONS(kCPU, -1, kFloat, kStrided);
59 
60  options = TensorOptions({kCPU, 0});
61  REQUIRE_OPTIONS(kCPU, 0, kFloat, kStrided);
62 
63  options = TensorOptions("cpu:0");
64  REQUIRE_OPTIONS(kCPU, 0, kFloat, kStrided);
65 
66  options = TensorOptions(kInt);
67  REQUIRE_OPTIONS(kCPU, -1, kInt, kStrided);
68 
69  options = TensorOptions(getNonVariableType(Backend::SparseCPU, kFloat));
70  REQUIRE_OPTIONS(kCPU, -1, kFloat, kSparse);
71 
72  options = TensorOptions(getNonVariableType(Backend::SparseCPU, kByte));
73  REQUIRE_OPTIONS(kCPU, -1, kByte, kSparse);
74 }
75 
76 TEST(TensorOptionsTest, ConstructsWellFromCPUTensors) {
77  auto options = empty(5, kDouble).options();
78  REQUIRE_OPTIONS(kCPU, -1, kDouble, kStrided);
79 
80  options = empty(5, getNonVariableType(Backend::SparseCPU, kByte)).options();
81  REQUIRE_OPTIONS(kCPU, -1, kByte, kSparse);
82 }
83 
84 TEST(TensorOptionsTest, ConstructsWellFromVariables) {
85  auto options = torch::empty(5).options();
86  REQUIRE_OPTIONS(kCPU, -1, kFloat, kStrided);
87  ASSERT_FALSE(options.requires_grad());
88 
89  options = torch::empty(5, at::requires_grad()).options();
90  REQUIRE_OPTIONS(kCPU, -1, kFloat, kStrided);
91  ASSERT_FALSE(options.requires_grad());
92 }
93 
94 TEST(DeviceTest, ParsesCorrectlyFromString) {
95  Device device("cpu:0");
96  ASSERT_EQ(device, Device(DeviceType::CPU, 0));
97 
98  device = Device("cpu");
99  ASSERT_EQ(device, Device(DeviceType::CPU));
100 
101  device = Device("cuda:123");
102  ASSERT_EQ(device, Device(DeviceType::CUDA, 123));
103 
104  device = Device("cuda");
105  ASSERT_EQ(device, Device(DeviceType::CUDA));
106 
107  device = Device("mkldnn");
108  ASSERT_EQ(device, Device(DeviceType::MKLDNN));
109 
110  device = Device("opengl");
111  ASSERT_EQ(device, Device(DeviceType::OPENGL));
112 
113  device = Device("opencl");
114  ASSERT_EQ(device, Device(DeviceType::OPENCL));
115 
116  device = Device("ideep");
117  ASSERT_EQ(device, Device(DeviceType::IDEEP));
118 
119  device = Device("hip");
120  ASSERT_EQ(device, Device(DeviceType::HIP));
121 
122  device = Device("hip:321");
123  ASSERT_EQ(device, Device(DeviceType::HIP, 321));
124 
125  std::vector<std::string> badnesses = {
126  "", "cud:1", "cuda:", "cpu::1", ":1", "3", "tpu:4", "??"};
127  for (const auto& badness : badnesses) {
128  ASSERT_ANY_THROW({ Device d(badness); });
129  }
130 }
131 
132 struct DefaultDtypeTest : ::testing::Test {
133  DefaultDtypeTest() {
134  set_default_dtype(caffe2::TypeMeta::Make<float>());
135  }
136  ~DefaultDtypeTest() override {
137  set_default_dtype(caffe2::TypeMeta::Make<float>());
138  }
139 };
140 
141 TEST_F(DefaultDtypeTest, CanSetAndGetDefaultDtype) {
142  ASSERT_EQ(at::get_default_dtype(), kFloat);
143  set_default_dtype(caffe2::TypeMeta::Make<int>());
144  ASSERT_EQ(at::get_default_dtype(), kInt);
145 }
146 
147 TEST_F(DefaultDtypeTest, NewTensorOptionsHasCorrectDefault) {
148  set_default_dtype(caffe2::TypeMeta::Make<int>());
149  ASSERT_EQ(at::get_default_dtype(), kInt);
150  TensorOptions options;
151  ASSERT_EQ(options.dtype(), kInt);
152 }
153 
154 TEST_F(DefaultDtypeTest, NewTensorsHaveCorrectDefaultDtype) {
155  set_default_dtype(caffe2::TypeMeta::Make<int>());
156  {
157  auto tensor = torch::ones(5);
158  ASSERT_EQ(tensor.dtype(), kInt);
159  }
160  set_default_dtype(caffe2::TypeMeta::Make<double>());
161  {
162  auto tensor = torch::ones(5);
163  ASSERT_EQ(tensor.dtype(), kDouble);
164  }
165  {
166  auto tensor = torch::ones(5, kFloat);
167  ASSERT_EQ(tensor.dtype(), kFloat);
168  }
169 }
C10_NODISCARD TensorOptions device(c10::optional< Device > device) const noexcept
Return a copy of TensorOptions with device set to the given one, or cleared if device is nullopt...
TensorOptions options() const
Returns the TensorOptions corresponding to this Tensor.
Definition: TensorMethods.h:42
Represents a a compute device on which a tensor is located.
Definition: Device.h:30
C10_NODISCARD TensorOptions dtype(c10::optional< caffe2::TypeMeta > dtype) const noexcept
Return a copy of TensorOptions with dtype set to the given one.
Flush-To-Zero and Denormals-Are-Zero mode.
C10_NODISCARD TensorOptions layout(c10::optional< Layout > layout) const noexcept
Sets the layout of the TensorOptions.
C10_NODISCARD TensorOptions requires_grad(c10::optional< bool > requires_grad) const noexcept
Sets the requires_grad property of the TensorOptions.