pytorch/test/cpp/api/tensor_options.cpp
Peter Goldsborough 825181ea9d Rewrite C++ API tests in gtest (#11953)
Summary:
This PR is a large codemod to rewrite all C++ API tests with GoogleTest (gtest) instead of Catch.

You can largely trust me to have correctly code-modded the tests, so it's not required to review every of the 2000+ changed lines. However, additional things I changed were:

1. Moved the cmake parts for these tests into their own `CMakeLists.txt` under `test/cpp/api` and calling `add_subdirectory` from `torch/CMakeLists.txt`
2. Fixing DataParallel tests which weren't being compiled because `USE_CUDA` wasn't correctly being set at all.
3. Updated README

ezyang ebetica
Pull Request resolved: https://github.com/pytorch/pytorch/pull/11953

Differential Revision: D9998883

Pulled By: goldsborough

fbshipit-source-id: affe3f320b0ca63e7e0019926a59076bb943db80
2018-09-21 21:28:16 -07:00

139 lines
4.2 KiB
C++

#include <gtest/gtest.h>
#include <torch/tensor.h>
#include <ATen/Context.h>
#include <ATen/Functions.h>
#include <ATen/OptionsGuard.h>
#include <ATen/core/TensorOptions.h>
#include <string>
#include <vector>
using namespace at;
// A macro so we don't lose location information when an assertion fails.
#define REQUIRE_OPTIONS(device_, index_, type_, layout_) \
ASSERT_EQ(options.device().type(), Device((device_), (index_)).type()); \
ASSERT_TRUE( \
options.device().index() == Device((device_), (index_)).index()); \
ASSERT_EQ(options.dtype(), (type_)); \
ASSERT_TRUE(options.layout() == (layout_))
#define REQUIRE_TENSOR_OPTIONS(device_, index_, type_, layout_) \
ASSERT_EQ(tensor.device().type(), Device((device_), (index_)).type()); \
ASSERT_EQ(tensor.device().index(), Device((device_), (index_)).index()); \
ASSERT_EQ(tensor.type().scalarType(), (type_)); \
ASSERT_TRUE(tensor.type().layout() == (layout_))
TEST(TensorOptionsTest, DefaultsToTheRightValues) {
TensorOptions options;
REQUIRE_OPTIONS(kCPU, -1, kFloat, kStrided);
}
TEST(TensorOptionsTest, ReturnsTheCorrectType) {
auto options = TensorOptions().device(kCPU).dtype(kInt).layout(kSparse);
ASSERT_TRUE(
at::getType(options) == getNonVariableType(Backend::SparseCPU, kInt));
}
TEST(TensorOptionsTest, UtilityFunctionsReturnTheRightTensorOptions) {
auto options = dtype(kInt);
REQUIRE_OPTIONS(kCPU, -1, kInt, kStrided);
options = layout(kSparse);
REQUIRE_OPTIONS(kCPU, -1, kFloat, kSparse);
options = device({kCUDA, 1});
REQUIRE_OPTIONS(kCUDA, 1, kFloat, kStrided);
options = device_index(1);
REQUIRE_OPTIONS(kCUDA, 1, kFloat, kStrided);
options = dtype(kByte).layout(kSparse).device({kCUDA, 2}).device_index(3);
REQUIRE_OPTIONS(kCUDA, 3, kByte, kSparse);
}
TEST(TensorOptionsTest, ConstructsWellFromCPUTypes) {
TensorOptions options;
REQUIRE_OPTIONS(kCPU, -1, kFloat, kStrided);
options = TensorOptions({kCPU, 0});
REQUIRE_OPTIONS(kCPU, 0, kFloat, kStrided);
options = TensorOptions(kInt);
REQUIRE_OPTIONS(kCPU, -1, kInt, kStrided);
options = TensorOptions(getNonVariableType(Backend::SparseCPU, kFloat));
REQUIRE_OPTIONS(kCPU, -1, kFloat, kSparse);
options = TensorOptions(getNonVariableType(Backend::SparseCPU, kByte));
REQUIRE_OPTIONS(kCPU, -1, kByte, kSparse);
}
TEST(TensorOptionsTest, ConstructsWellFromCPUTensors) {
auto options = empty(5, kDouble).options();
REQUIRE_OPTIONS(kCPU, -1, kDouble, kStrided);
options = empty(5, getNonVariableType(Backend::SparseCPU, kByte)).options();
REQUIRE_OPTIONS(kCPU, -1, kByte, kSparse);
}
TEST(TensorOptionsTest, ConstructsWellFromVariables) {
auto options = torch::empty(5).options();
REQUIRE_OPTIONS(kCPU, -1, kFloat, kStrided);
ASSERT_FALSE(options.requires_grad());
options = torch::empty(5, at::requires_grad()).options();
REQUIRE_OPTIONS(kCPU, -1, kFloat, kStrided);
ASSERT_FALSE(options.requires_grad());
}
TEST(TensorOptionsTest, OptionsGuard) {
Tensor tensor;
{
OptionsGuard guard(TensorOptions{});
tensor = at::empty({10});
}
REQUIRE_TENSOR_OPTIONS(kCPU, -1, kFloat, kStrided);
{
OptionsGuard guard(TensorOptions().dtype(kInt));
tensor = at::empty({10});
}
REQUIRE_TENSOR_OPTIONS(kCPU, -1, kInt, kStrided);
{
OptionsGuard guard(TensorOptions().dtype(kInt).layout(kSparse));
tensor = at::empty({10});
}
REQUIRE_TENSOR_OPTIONS(kCPU, -1, kInt, kSparse);
{
OptionsGuard guard(requires_grad(true));
tensor = torch::empty({10});
}
REQUIRE_TENSOR_OPTIONS(kCPU, -1, kFloat, kStrided);
ASSERT_TRUE(tensor.requires_grad());
}
TEST(DeviceTest, ParsesCorrectlyFromString) {
Device device("cpu:0");
ASSERT_EQ(device, Device(kCPU, 0));
device = Device("cpu");
ASSERT_EQ(device, Device(kCPU));
device = Device("cuda:123");
ASSERT_EQ(device, Device(kCUDA, 123));
device = Device("cuda");
ASSERT_EQ(device, Device(kCUDA));
std::vector<std::string> badnesses = {
"", "cud:1", "cuda:", "cpu::1", ":1", "3", "tpu:4", "??"};
for (const auto& badness : badnesses) {
ASSERT_ANY_THROW({ Device d(badness); });
}
}