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
This commit is contained in:
Peter Goldsborough
2018-09-21 21:12:37 -07:00
committed by Facebook Github Bot
parent d0db23e95a
commit 825181ea9d
27 changed files with 2154 additions and 2182 deletions
+74 -77
View File
@@ -1,4 +1,4 @@
#include "catch_utils.hpp"
#include <gtest/gtest.h>
#include <torch/csrc/autograd/functions/comm.h>
#include <torch/nn/module.h>
@@ -7,110 +7,110 @@
#include <torch/nn/pimpl.h>
#include <torch/tensor.h>
#include <test/cpp/api/support.h>
#include <iostream>
#include <memory>
#include <utility>
#include <vector>
using Catch::StartsWith;
using namespace torch::autograd;
using namespace torch::nn;
#ifdef USE_CUDA
struct ParallelTest : torch::test::SeedingFixture {};
CATCH_TEST_CASE("Parallel/DifferentiableScatter", "[multi-cuda]") {
TEST_F(ParallelTest, DifferentiableScatter_MultiCUDA) {
Scatter scatter(
{torch::Device(torch::kCUDA, 0), torch::Device(torch::kCUDA, 1)});
auto input = torch::ones(10, torch::requires_grad(true));
auto output = scatter.apply({input});
CATCH_REQUIRE(output.size() == 2);
CATCH_REQUIRE(output[0].size(0) == 5);
CATCH_REQUIRE(output[1].size(0) == 5);
ASSERT_EQ(output.size(), 2);
ASSERT_EQ(output[0].size(0), 5);
ASSERT_EQ(output[1].size(0), 5);
CATCH_REQUIRE(torch::cat({output[0].to(torch::kCPU), output[1].to(torch::kCPU)})
.allclose(input));
ASSERT_TRUE(torch::cat({output[0].to(torch::kCPU), output[1].to(torch::kCPU)})
.allclose(input));
auto sum = output[0].to({torch::kCUDA, 1}) + output[1];
torch::Tensor sum = output[0].to({torch::kCUDA, 1}) + output[1];
sum.backward();
CATCH_REQUIRE(input.grad().defined());
CATCH_REQUIRE(input.grad().device().is_cpu());
CATCH_REQUIRE(input.grad().sum().toCInt() == 10);
ASSERT_TRUE(input.grad().defined());
ASSERT_TRUE(input.grad().device().is_cpu());
ASSERT_EQ(input.grad().sum().toCInt(), 10);
}
CATCH_TEST_CASE("Parallel/DifferentiableGather", "[multi-cuda]") {
TEST_F(ParallelTest, DifferentiableGather_MultiCUDA) {
Gather gather(torch::Device(torch::kCUDA, 1));
auto a = torch::ones(5, torch::requires_grad(true).device({torch::kCUDA, 0}));
auto b = torch::ones(5, torch::requires_grad(true).device({torch::kCUDA, 1}));
auto outputs = gather.apply({a, b});
CATCH_REQUIRE(outputs.size() == 1);
auto& output = outputs.front();
ASSERT_EQ(outputs.size(), 1);
torch::Tensor output = outputs.front();
CATCH_REQUIRE(output.size(0) == 10);
CATCH_REQUIRE(output.device() == torch::Device(torch::kCUDA, 1));
ASSERT_EQ(output.size(0), 10);
ASSERT_EQ(output.device(), torch::Device(torch::kCUDA, 1));
auto chunks = output.chunk(2);
CATCH_REQUIRE(chunks[0].to({torch::kCUDA, 0}).allclose(a));
CATCH_REQUIRE(chunks[1].allclose(b));
ASSERT_TRUE(chunks[0].to({torch::kCUDA, 0}).allclose(a));
ASSERT_TRUE(chunks[1].allclose(b));
output.backward();
CATCH_REQUIRE(a.grad().defined());
CATCH_REQUIRE(a.grad().device() == torch::Device(torch::kCUDA, 0));
CATCH_REQUIRE(a.grad().sum().toCInt() == 5);
ASSERT_TRUE(a.grad().defined());
ASSERT_EQ(a.grad().device(), torch::Device(torch::kCUDA, 0));
ASSERT_EQ(a.grad().sum().toCInt(), 5);
CATCH_REQUIRE(b.grad().defined());
CATCH_REQUIRE(b.grad().device() == torch::Device(torch::kCUDA, 1));
CATCH_REQUIRE(b.grad().sum().toCInt() == 5);
ASSERT_TRUE(b.grad().defined());
ASSERT_EQ(b.grad().device(), torch::Device(torch::kCUDA, 1));
ASSERT_EQ(b.grad().sum().toCInt(), 5);
}
CATCH_TEST_CASE("Parallel/Replicate", "[multi-cuda]") {
TEST_F(ParallelTest, Replicate_MultiCUDA) {
Linear linear(3, 4);
auto replicas = parallel::replicate(
linear, {torch::Device(torch::kCUDA, 0), torch::Device(torch::kCUDA, 1)});
CATCH_REQUIRE(replicas.size() == 2);
ASSERT_EQ(replicas.size(), 2);
auto original_parameters = linear->parameters();
auto replica1_parameters = replicas[0]->parameters();
for (auto& parameter : replica1_parameters) {
CATCH_REQUIRE(parameter->device() == torch::Device(torch::kCUDA, 0));
ASSERT_EQ(parameter->device(), torch::Device(torch::kCUDA, 0));
}
replicas[0]->to(torch::kCPU);
CATCH_REQUIRE(replica1_parameters.size() == original_parameters.size());
ASSERT_EQ(replica1_parameters.size(), original_parameters.size());
for (size_t i = 0; i < original_parameters.size(); ++i) {
CATCH_REQUIRE(replica1_parameters[i]->allclose(*original_parameters[i]));
CATCH_REQUIRE(
replica1_parameters[i].data<float>() !=
original_parameters[i].data<float>());
ASSERT_TRUE(replica1_parameters[i]->allclose(*original_parameters[i]));
ASSERT_TRUE(
replica1_parameters[i]->data<float>() !=
original_parameters[i]->data<float>());
}
auto replica2_parameters = replicas[1]->parameters();
for (auto& parameter : replica2_parameters) {
CATCH_REQUIRE(parameter->device() == torch::Device(torch::kCUDA, 1));
ASSERT_EQ(parameter->device(), torch::Device(torch::kCUDA, 1));
}
replicas[1]->to(torch::kCPU);
CATCH_REQUIRE(replica2_parameters.size() == original_parameters.size());
ASSERT_EQ(replica2_parameters.size(), original_parameters.size());
for (size_t i = 0; i < original_parameters.size(); ++i) {
CATCH_REQUIRE(replica2_parameters[i]->allclose(*original_parameters[i]));
CATCH_REQUIRE(
replica2_parameters[i].data<float>() !=
original_parameters[i].data<float>());
ASSERT_TRUE(replica2_parameters[i]->allclose(*original_parameters[i]));
ASSERT_TRUE(
replica2_parameters[i]->data<float>() !=
original_parameters[i]->data<float>());
}
}
CATCH_TEST_CASE("Parallel/ParallelApply", "[multi-cuda]") {
TEST_F(ParallelTest, ParallelApply_MultiCUDA) {
Linear a(3, 4);
Linear b(std::static_pointer_cast<LinearImpl>(a->clone()));
Linear b(std::dynamic_pointer_cast<LinearImpl>(a->clone()));
b->to({torch::kCUDA, 0});
Linear c(std::static_pointer_cast<LinearImpl>(a->clone()));
Linear c(std::dynamic_pointer_cast<LinearImpl>(a->clone()));
c->to({torch::kCUDA, 1});
std::vector<Linear> modules = {a, b, c};
@@ -121,17 +121,17 @@ CATCH_TEST_CASE("Parallel/ParallelApply", "[multi-cuda]") {
auto outputs = parallel::parallel_apply(modules, inputs);
CATCH_REQUIRE(outputs.size() == 3);
CATCH_REQUIRE(outputs[0].device().is_cpu());
ASSERT_EQ(outputs.size(), 3);
ASSERT_TRUE(outputs[0].device().is_cpu());
CATCH_REQUIRE(outputs[1].device() == torch::Device(torch::kCUDA, 0));
CATCH_REQUIRE(outputs[1].to(torch::kCPU).allclose(outputs[0]));
ASSERT_EQ(outputs[1].device(), torch::Device(torch::kCUDA, 0));
ASSERT_TRUE(outputs[1].to(torch::kCPU).allclose(outputs[0]));
CATCH_REQUIRE(outputs[2].device() == torch::Device(torch::kCUDA, 1));
CATCH_REQUIRE(outputs[2].to(torch::kCPU).allclose(outputs[0]));
ASSERT_EQ(outputs[2].device(), torch::Device(torch::kCUDA, 1));
ASSERT_TRUE(outputs[2].to(torch::kCPU).allclose(outputs[0]));
}
CATCH_TEST_CASE("Parallel/ParallelApplyWithDifferentOutputDevice", "[multi-cuda]") {
TEST_F(ParallelTest, ParallelApplyWithDifferentOutputDevice_MultiCUDA) {
struct M : torch::nn::Module {
torch::Tensor forward(torch::Tensor input) {
return torch::ones({5}, torch::dtype(torch::kInt32));
@@ -147,17 +147,17 @@ CATCH_TEST_CASE("Parallel/ParallelApplyWithDifferentOutputDevice", "[multi-cuda]
auto outputs = parallel::parallel_apply(modules, inputs, devices);
CATCH_REQUIRE(outputs.size() == 3);
CATCH_REQUIRE(outputs[0].device().is_cuda());
CATCH_REQUIRE(outputs[0].device() == torch::Device(torch::kCUDA, 1));
ASSERT_EQ(outputs.size(), 3);
ASSERT_TRUE(outputs[0].device().is_cuda());
ASSERT_EQ(outputs[0].device(), torch::Device(torch::kCUDA, 1));
CATCH_REQUIRE(outputs[1].device().is_cuda());
CATCH_REQUIRE(outputs[1].device() == torch::Device(torch::kCUDA, 0));
ASSERT_TRUE(outputs[1].device().is_cuda());
ASSERT_EQ(outputs[1].device(), torch::Device(torch::kCUDA, 0));
CATCH_REQUIRE(outputs[2].device().is_cpu());
ASSERT_TRUE(outputs[2].device().is_cpu());
}
CATCH_TEST_CASE("Parallel/ParallelApplyRethrowsException", "[multi-cuda]") {
TEST_F(ParallelTest, ParallelApplyRethrowsException_MultiCUDA) {
struct M : torch::nn::Cloneable<M> {
void reset() override {}
torch::Tensor forward(torch::Tensor input) {
@@ -167,13 +167,12 @@ CATCH_TEST_CASE("Parallel/ParallelApplyRethrowsException", "[multi-cuda]") {
auto m = std::make_shared<M>();
auto input = torch::ones({10, 3});
CATCH_REQUIRE_THROWS_WITH(
parallel::data_parallel(m, input), StartsWith("Badness!"));
ASSERT_THROWS_WITH(parallel::data_parallel(m, input), "Badness!");
}
CATCH_TEST_CASE(
"Parallel/DataParallelPlacesTheOutputOnTheRequestedDevice",
"[multi-cuda]") {
TEST_F(
ParallelTest,
DataParallelPlacesTheOutputOnTheRequestedDevice_MultiCUDA) {
struct M : torch::nn::Cloneable<M> {
void reset() override {}
torch::Tensor forward(torch::Tensor input) {
@@ -192,9 +191,9 @@ CATCH_TEST_CASE(
input,
/*devices=*/at::nullopt,
/*output_device=*/torch::Device(torch::kCUDA, 1));
CATCH_REQUIRE(output.defined());
CATCH_REQUIRE(output.device().is_cuda());
CATCH_REQUIRE(output.device().index() == 1);
ASSERT_TRUE(output.defined());
ASSERT_TRUE(output.device().is_cuda());
ASSERT_EQ(output.device().index(), 1);
}
{
// Verify for the single-device case (where we don't scatter/gather).
@@ -203,16 +202,16 @@ CATCH_TEST_CASE(
input,
/*devices=*/std::vector<torch::Device>{torch::Device(torch::kCUDA, 0)},
/*output_device=*/torch::Device(torch::kCUDA, 1));
CATCH_REQUIRE(m->intermediate_tensor.defined());
CATCH_REQUIRE(m->intermediate_tensor.device().is_cuda());
CATCH_REQUIRE(m->intermediate_tensor.device().index() == 0);
CATCH_REQUIRE(output.defined());
CATCH_REQUIRE(output.device().is_cuda());
CATCH_REQUIRE(output.device().index() == 1);
ASSERT_TRUE(m->intermediate_tensor.defined());
ASSERT_TRUE(m->intermediate_tensor.device().is_cuda());
ASSERT_EQ(m->intermediate_tensor.device().index(), 0);
ASSERT_TRUE(output.defined());
ASSERT_TRUE(output.device().is_cuda());
ASSERT_EQ(output.device().index(), 1);
}
}
CATCH_TEST_CASE("Parallel/DataParallelUsesAllAvailableCUDADevices", "[cuda]") {
TEST_F(ParallelTest, DataParallelUsesAllAvailableCUDADevices_CUDA) {
struct M : torch::nn::Cloneable<M> {
void reset() override {}
torch::Tensor forward(torch::Tensor input) {
@@ -225,10 +224,8 @@ CATCH_TEST_CASE("Parallel/DataParallelUsesAllAvailableCUDADevices", "[cuda]") {
auto output = parallel::data_parallel(m, input);
const auto device_count = torch::cuda::device_count();
CATCH_REQUIRE(output.numel() == device_count);
ASSERT_EQ(output.numel(), device_count);
for (size_t i = 0; i < device_count; ++i) {
CATCH_REQUIRE(output[i].toCInt() == i);
ASSERT_EQ(output[i].toCInt(), i);
}
}
#endif