1 #include <gtest/gtest.h> 3 #include <torch/types.h> 5 #include <ATen/Context.h> 6 #include <ATen/Functions.h> 7 #include <c10/core/TensorOptions.h> 15 #define REQUIRE_OPTIONS(device_, index_, type_, layout_) \ 16 ASSERT_EQ(options.device().type(), Device((device_), (index_)).type()); \ 18 options.device().index() == Device((device_), (index_)).index()); \ 19 ASSERT_EQ(options.dtype(), (type_)); \ 20 ASSERT_TRUE(options.layout() == (layout_)) 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_)) 28 TEST(TensorOptionsTest, DefaultsToTheRightValues) {
30 REQUIRE_OPTIONS(kCPU, -1, kFloat, kStrided);
33 TEST(TensorOptionsTest, ReturnsTheCorrectType) {
36 at::getType(options) == getNonVariableType(Backend::SparseCPU, kInt));
39 TEST(TensorOptionsTest, UtilityFunctionsReturnTheRightTensorOptions) {
40 auto options = dtype(kInt);
41 REQUIRE_OPTIONS(kCPU, -1, kInt, kStrided);
43 options = layout(kSparse);
44 REQUIRE_OPTIONS(kCPU, -1, kFloat, kSparse);
46 options = device({kCUDA, 1});
47 REQUIRE_OPTIONS(kCUDA, 1, kFloat, kStrided);
49 options = device_index(1);
50 REQUIRE_OPTIONS(kCUDA, 1, kFloat, kStrided);
52 options = dtype(kByte).layout(kSparse).device(kCUDA, 2).device_index(3);
53 REQUIRE_OPTIONS(kCUDA, 3, kByte, kSparse);
56 TEST(TensorOptionsTest, ConstructsWellFromCPUTypes) {
58 REQUIRE_OPTIONS(kCPU, -1, kFloat, kStrided);
61 REQUIRE_OPTIONS(kCPU, 0, kFloat, kStrided);
64 REQUIRE_OPTIONS(kCPU, 0, kFloat, kStrided);
67 REQUIRE_OPTIONS(kCPU, -1, kInt, kStrided);
69 options =
TensorOptions(getNonVariableType(Backend::SparseCPU, kFloat));
70 REQUIRE_OPTIONS(kCPU, -1, kFloat, kSparse);
72 options =
TensorOptions(getNonVariableType(Backend::SparseCPU, kByte));
73 REQUIRE_OPTIONS(kCPU, -1, kByte, kSparse);
76 TEST(TensorOptionsTest, ConstructsWellFromCPUTensors) {
77 auto options = empty(5, kDouble).
options();
78 REQUIRE_OPTIONS(kCPU, -1, kDouble, kStrided);
80 options = empty(5, getNonVariableType(Backend::SparseCPU, kByte)).
options();
81 REQUIRE_OPTIONS(kCPU, -1, kByte, kSparse);
84 TEST(TensorOptionsTest, ConstructsWellFromVariables) {
85 auto options = torch::empty(5).options();
86 REQUIRE_OPTIONS(kCPU, -1, kFloat, kStrided);
89 options = torch::empty(5, at::requires_grad()).options();
90 REQUIRE_OPTIONS(kCPU, -1, kFloat, kStrided);
91 ASSERT_FALSE(options.requires_grad());
94 TEST(DeviceTest, ParsesCorrectlyFromString) {
96 ASSERT_EQ(device,
Device(DeviceType::CPU, 0));
99 ASSERT_EQ(device,
Device(DeviceType::CPU));
101 device =
Device(
"cuda:123");
102 ASSERT_EQ(device,
Device(DeviceType::CUDA, 123));
105 ASSERT_EQ(device,
Device(DeviceType::CUDA));
107 device =
Device(
"mkldnn");
108 ASSERT_EQ(device,
Device(DeviceType::MKLDNN));
110 device =
Device(
"opengl");
111 ASSERT_EQ(device,
Device(DeviceType::OPENGL));
113 device =
Device(
"opencl");
114 ASSERT_EQ(device,
Device(DeviceType::OPENCL));
117 ASSERT_EQ(device,
Device(DeviceType::IDEEP));
120 ASSERT_EQ(device,
Device(DeviceType::HIP));
122 device =
Device(
"hip:321");
123 ASSERT_EQ(device,
Device(DeviceType::HIP, 321));
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); });
134 set_default_dtype(caffe2::TypeMeta::Make<float>());
137 set_default_dtype(caffe2::TypeMeta::Make<float>());
142 ASSERT_EQ(at::get_default_dtype(), kFloat);
143 set_default_dtype(caffe2::TypeMeta::Make<int>());
144 ASSERT_EQ(at::get_default_dtype(), kInt);
148 set_default_dtype(caffe2::TypeMeta::Make<int>());
149 ASSERT_EQ(at::get_default_dtype(), kInt);
151 ASSERT_EQ(options.
dtype(), kInt);
155 set_default_dtype(caffe2::TypeMeta::Make<int>());
157 auto tensor = torch::ones(5);
158 ASSERT_EQ(tensor.dtype(), kInt);
160 set_default_dtype(caffe2::TypeMeta::Make<double>());
162 auto tensor = torch::ones(5);
163 ASSERT_EQ(tensor.dtype(), kDouble);
166 auto tensor = torch::ones(5, kFloat);
167 ASSERT_EQ(tensor.dtype(), kFloat);
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.
Represents a a compute device on which a tensor is located.
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.