Use CATCH prefix to avoid name conflicts with Caffe2.

Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/11780

Differential Revision: D9889925

Pulled By: gchanan

fbshipit-source-id: 5eca849c36ced00b8ae7482b7945b445a3e1687e
This commit is contained in:
Gregory Chanan
2018-09-18 07:59:41 -07:00
committed by Facebook Github Bot
parent 4ee0a78ee6
commit e00fb69b25
41 changed files with 1689 additions and 1665 deletions
+75 -75
View File
@@ -1,4 +1,4 @@
#include <catch.hpp>
#include "catch_utils.hpp"
#include <torch/nn/modules.h>
#include <torch/nn/modules/batchnorm.h>
@@ -21,7 +21,7 @@ using namespace torch::test;
using Catch::StartsWith;
TEST_CASE("Sequential/ConstructsFromSharedPointer") {
CATCH_TEST_CASE("Sequential/ConstructsFromSharedPointer") {
struct M : torch::nn::Module {
explicit M(int value_) : value(value_) {}
int value;
@@ -31,10 +31,10 @@ TEST_CASE("Sequential/ConstructsFromSharedPointer") {
};
Sequential sequential(
std::make_shared<M>(1), std::make_shared<M>(2), std::make_shared<M>(3));
REQUIRE(sequential->size() == 3);
CATCH_REQUIRE(sequential->size() == 3);
}
TEST_CASE("Sequential/ConstructsFromConcreteType") {
CATCH_TEST_CASE("Sequential/ConstructsFromConcreteType") {
struct M : torch::nn::Module {
explicit M(int value_) : value(value_) {}
int value;
@@ -44,9 +44,9 @@ TEST_CASE("Sequential/ConstructsFromConcreteType") {
};
Sequential sequential(M(1), M(2), M(3));
REQUIRE(sequential->size() == 3);
CATCH_REQUIRE(sequential->size() == 3);
}
TEST_CASE("Sequential/ConstructsFromModuleHolder") {
CATCH_TEST_CASE("Sequential/ConstructsFromModuleHolder") {
struct MImpl : torch::nn::Module {
explicit MImpl(int value_) : value(value_) {}
int forward() {
@@ -61,10 +61,10 @@ TEST_CASE("Sequential/ConstructsFromModuleHolder") {
};
Sequential sequential(M(1), M(2), M(3));
REQUIRE(sequential->size() == 3);
CATCH_REQUIRE(sequential->size() == 3);
}
TEST_CASE("Sequential/PushBackAddsAnElement") {
CATCH_TEST_CASE("Sequential/PushBackAddsAnElement") {
struct M : torch::nn::Module {
explicit M(int value_) : value(value_) {}
int forward() {
@@ -73,17 +73,17 @@ TEST_CASE("Sequential/PushBackAddsAnElement") {
int value;
};
Sequential sequential;
REQUIRE(sequential->size() == 0);
REQUIRE(sequential->is_empty());
CATCH_REQUIRE(sequential->size() == 0);
CATCH_REQUIRE(sequential->is_empty());
sequential->push_back(Linear(3, 4));
REQUIRE(sequential->size() == 1);
CATCH_REQUIRE(sequential->size() == 1);
sequential->push_back(std::make_shared<M>(1));
REQUIRE(sequential->size() == 2);
CATCH_REQUIRE(sequential->size() == 2);
sequential->push_back(M(2));
REQUIRE(sequential->size() == 3);
CATCH_REQUIRE(sequential->size() == 3);
}
TEST_CASE("Sequential/AccessWithAt") {
CATCH_TEST_CASE("Sequential/AccessWithAt") {
struct M : torch::nn::Module {
explicit M(int value_) : value(value_) {}
int forward() {
@@ -98,22 +98,22 @@ TEST_CASE("Sequential/AccessWithAt") {
for (auto& module : modules) {
sequential->push_back(module);
}
REQUIRE(sequential->size() == 3);
CATCH_REQUIRE(sequential->size() == 3);
// returns the correct module for a given index
for (size_t i = 0; i < modules.size(); ++i) {
REQUIRE(&sequential->at<M>(i) == modules[i].get());
CATCH_REQUIRE(&sequential->at<M>(i) == modules[i].get());
}
// throws for a bad index
REQUIRE_THROWS_WITH(
CATCH_REQUIRE_THROWS_WITH(
sequential->at<M>(modules.size() + 1), StartsWith("Index out of range"));
REQUIRE_THROWS_WITH(
CATCH_REQUIRE_THROWS_WITH(
sequential->at<M>(modules.size() + 1000000),
StartsWith("Index out of range"));
}
TEST_CASE("Sequential/AccessWithPtr") {
CATCH_TEST_CASE("Sequential/AccessWithPtr") {
struct M : torch::nn::Module {
explicit M(int value_) : value(value_) {}
int forward() {
@@ -128,46 +128,46 @@ TEST_CASE("Sequential/AccessWithPtr") {
for (auto& module : modules) {
sequential->push_back(module);
}
REQUIRE(sequential->size() == 3);
CATCH_REQUIRE(sequential->size() == 3);
// returns the correct module for a given index
for (size_t i = 0; i < modules.size(); ++i) {
REQUIRE(sequential->ptr(i).get() == modules[i].get());
REQUIRE(sequential[i].get() == modules[i].get());
REQUIRE(sequential->ptr<M>(i).get() == modules[i].get());
CATCH_REQUIRE(sequential->ptr(i).get() == modules[i].get());
CATCH_REQUIRE(sequential[i].get() == modules[i].get());
CATCH_REQUIRE(sequential->ptr<M>(i).get() == modules[i].get());
}
// throws for a bad index
REQUIRE_THROWS_WITH(
CATCH_REQUIRE_THROWS_WITH(
sequential->ptr(modules.size() + 1), StartsWith("Index out of range"));
REQUIRE_THROWS_WITH(
CATCH_REQUIRE_THROWS_WITH(
sequential->ptr(modules.size() + 1000000),
StartsWith("Index out of range"));
}
TEST_CASE("Sequential/CallingForwardOnEmptySequentialIsDisallowed") {
CATCH_TEST_CASE("Sequential/CallingForwardOnEmptySequentialIsDisallowed") {
Sequential empty;
REQUIRE_THROWS_WITH(
CATCH_REQUIRE_THROWS_WITH(
empty->forward<int>(),
StartsWith("Cannot call forward() on an empty Sequential"));
}
TEST_CASE("Sequential/CallingForwardChainsCorrectly") {
CATCH_TEST_CASE("Sequential/CallingForwardChainsCorrectly") {
struct MockModule : torch::nn::Module {
explicit MockModule(int value) : expected(value) {}
int expected;
int forward(int value) {
REQUIRE(value == expected);
CATCH_REQUIRE(value == expected);
return value + 1;
}
};
Sequential sequential(MockModule{1}, MockModule{2}, MockModule{3});
REQUIRE(sequential->forward<int>(1) == 4);
CATCH_REQUIRE(sequential->forward<int>(1) == 4);
}
TEST_CASE("Sequential/CallingForwardWithTheWrongReturnTypeThrows") {
CATCH_TEST_CASE("Sequential/CallingForwardWithTheWrongReturnTypeThrows") {
struct M : public torch::nn::Module {
int forward() {
return 5;
@@ -175,14 +175,14 @@ TEST_CASE("Sequential/CallingForwardWithTheWrongReturnTypeThrows") {
};
Sequential sequential(M{});
REQUIRE(sequential->forward<int>() == 5);
REQUIRE_THROWS_WITH(
CATCH_REQUIRE(sequential->forward<int>() == 5);
CATCH_REQUIRE_THROWS_WITH(
sequential->forward<float>(),
StartsWith("The type of the return value "
"is int, but you asked for type float"));
}
TEST_CASE("Sequential/TheReturnTypeOfForwardDefaultsToTensor") {
CATCH_TEST_CASE("Sequential/TheReturnTypeOfForwardDefaultsToTensor") {
struct M : public torch::nn::Module {
torch::Tensor forward(torch::Tensor v) {
return v;
@@ -191,21 +191,21 @@ TEST_CASE("Sequential/TheReturnTypeOfForwardDefaultsToTensor") {
Sequential sequential(M{});
auto variable = torch::ones({3, 3}, torch::requires_grad());
REQUIRE(sequential->forward(variable).equal(variable));
CATCH_REQUIRE(sequential->forward(variable).equal(variable));
}
TEST_CASE("Sequential/ForwardReturnsTheLastValue") {
CATCH_TEST_CASE("Sequential/ForwardReturnsTheLastValue") {
torch::manual_seed(0);
Sequential sequential(Linear(10, 3), Linear(3, 5), Linear(5, 100));
auto x = torch::randn({1000, 10}, torch::requires_grad());
auto y = sequential->forward(x);
REQUIRE(y.ndimension() == 2);
REQUIRE(y.size(0) == 1000);
REQUIRE(y.size(1) == 100);
CATCH_REQUIRE(y.ndimension() == 2);
CATCH_REQUIRE(y.size(0) == 1000);
CATCH_REQUIRE(y.size(1) == 100);
}
TEST_CASE("Sequential/SanityCheckForHoldingStandardModules") {
CATCH_TEST_CASE("Sequential/SanityCheckForHoldingStandardModules") {
Sequential sequential(
Linear(10, 3),
Conv2d(1, 2, 3),
@@ -215,7 +215,7 @@ TEST_CASE("Sequential/SanityCheckForHoldingStandardModules") {
LSTM(4, 5));
}
TEST_CASE("Sequential/ExtendPushesModulesFromOtherSequential") {
CATCH_TEST_CASE("Sequential/ExtendPushesModulesFromOtherSequential") {
struct A : torch::nn::Module {
int forward(int x) {
return x;
@@ -240,34 +240,34 @@ TEST_CASE("Sequential/ExtendPushesModulesFromOtherSequential") {
Sequential b(C{}, D{});
a->extend(*b);
REQUIRE(a->size() == 4);
REQUIRE(a[0]->as<A>());
REQUIRE(a[1]->as<B>());
REQUIRE(a[2]->as<C>());
REQUIRE(a[3]->as<D>());
CATCH_REQUIRE(a->size() == 4);
CATCH_REQUIRE(a[0]->as<A>());
CATCH_REQUIRE(a[1]->as<B>());
CATCH_REQUIRE(a[2]->as<C>());
CATCH_REQUIRE(a[3]->as<D>());
REQUIRE(b->size() == 2);
REQUIRE(b[0]->as<C>());
REQUIRE(b[1]->as<D>());
CATCH_REQUIRE(b->size() == 2);
CATCH_REQUIRE(b[0]->as<C>());
CATCH_REQUIRE(b[1]->as<D>());
std::vector<std::shared_ptr<A>> c = {std::make_shared<A>(),
std::make_shared<A>()};
b->extend(c);
REQUIRE(b->size() == 4);
REQUIRE(b[0]->as<C>());
REQUIRE(b[1]->as<D>());
REQUIRE(b[2]->as<A>());
REQUIRE(b[3]->as<A>());
CATCH_REQUIRE(b->size() == 4);
CATCH_REQUIRE(b[0]->as<C>());
CATCH_REQUIRE(b[1]->as<D>());
CATCH_REQUIRE(b[2]->as<A>());
CATCH_REQUIRE(b[3]->as<A>());
}
TEST_CASE("Sequential/HasReferenceSemantics") {
CATCH_TEST_CASE("Sequential/HasReferenceSemantics") {
Sequential first(Linear(2, 3), Linear(4, 4), Linear(4, 5));
Sequential second(first);
REQUIRE(first.get() == second.get());
REQUIRE(first->size() == second->size());
REQUIRE(std::equal(
CATCH_REQUIRE(first.get() == second.get());
CATCH_REQUIRE(first->size() == second->size());
CATCH_REQUIRE(std::equal(
first->begin(),
first->end(),
second->begin(),
@@ -276,17 +276,17 @@ TEST_CASE("Sequential/HasReferenceSemantics") {
}));
}
TEST_CASE("Sequential/IsCloneable") {
CATCH_TEST_CASE("Sequential/IsCloneable") {
Sequential sequential(Linear(3, 4), Functional(torch::relu), BatchNorm(3));
Sequential clone =
std::dynamic_pointer_cast<SequentialImpl>(sequential->clone());
REQUIRE(sequential->size() == clone->size());
CATCH_REQUIRE(sequential->size() == clone->size());
for (size_t i = 0; i < sequential->size(); ++i) {
// The modules should be the same kind (type).
REQUIRE(sequential[i]->name() == clone[i]->name());
CATCH_REQUIRE(sequential[i]->name() == clone[i]->name());
// But not pointer-equal (distinct objects).
REQUIRE(sequential[i] != clone[i]);
CATCH_REQUIRE(sequential[i] != clone[i]);
}
// Verify that the clone is deep, i.e. parameters of modules are cloned too.
@@ -295,38 +295,38 @@ TEST_CASE("Sequential/IsCloneable") {
auto params1 = sequential->parameters();
auto params2 = clone->parameters();
REQUIRE(params1.size() == params2.size());
CATCH_REQUIRE(params1.size() == params2.size());
for (auto& param : params1) {
REQUIRE(!pointer_equal(param.value, params2[param.key]));
REQUIRE(param->device() == params2[param.key].device());
REQUIRE(param->allclose(params2[param.key]));
CATCH_REQUIRE(!pointer_equal(param.value, params2[param.key]));
CATCH_REQUIRE(param->device() == params2[param.key].device());
CATCH_REQUIRE(param->allclose(params2[param.key]));
param->add_(2);
}
for (auto& param : params1) {
REQUIRE(!param->allclose(params2[param.key]));
CATCH_REQUIRE(!param->allclose(params2[param.key]));
}
}
TEST_CASE("Sequential/RegistersElementsAsSubmodules") {
CATCH_TEST_CASE("Sequential/RegistersElementsAsSubmodules") {
Sequential sequential(Linear(10, 3), Conv2d(1, 2, 3), FeatureDropout(0.5));
auto modules = sequential->modules();
REQUIRE(modules.size() == sequential->children().size());
CATCH_REQUIRE(modules.size() == sequential->children().size());
REQUIRE(modules[0]->as<Linear>());
REQUIRE(modules[1]->as<Conv2d>());
REQUIRE(modules[2]->as<FeatureDropout>());
CATCH_REQUIRE(modules[0]->as<Linear>());
CATCH_REQUIRE(modules[1]->as<Conv2d>());
CATCH_REQUIRE(modules[2]->as<FeatureDropout>());
}
TEST_CASE("Sequential/CloneToDevice", "[cuda]") {
CATCH_TEST_CASE("Sequential/CloneToDevice", "[cuda]") {
Sequential sequential(Linear(3, 4), Functional(torch::relu), BatchNorm(3));
torch::Device device(torch::kCUDA, 0);
Sequential clone =
std::dynamic_pointer_cast<SequentialImpl>(sequential->clone(device));
for (const auto& p : clone->parameters()) {
REQUIRE(p->device() == device);
CATCH_REQUIRE(p->device() == device);
}
for (const auto& b : clone->buffers()) {
REQUIRE(b->device() == device);
CATCH_REQUIRE(b->device() == device);
}
}