Set random seed at the start of C++ tests (#8903)

Summary:
Sets the random seed at the start of C++ tests so that everything is super deterministic.

I made sure we only generate random values from torch instead of `std::`, so that this seed always applies. I.e. I do:

```
torch::randint(2, {2}, at::kInt64)
```

instead of

```
std::rand() % 2
```

Also got rid of the tests that test the random seeding, since it would interfere here. And the test is not useful since we just use ATen's seeding mechanism, which should work.

Fixes  #7288 #7286 #7289

ebetica ezyang
Closes https://github.com/pytorch/pytorch/pull/8903

Differential Revision: D8667269

Pulled By: goldsborough

fbshipit-source-id: a833e86e156d5e68dae8c53a4b1c433cb0608b6c
This commit is contained in:
Peter Goldsborough
2018-06-27 20:00:53 -07:00
committed by Facebook Github Bot
parent a41d433d9d
commit 03d0a70a4d
13 changed files with 194 additions and 209 deletions
+11 -34
View File
@@ -17,44 +17,20 @@ using OrderedDict = torch::detail::OrderedDict<std::string, T>;
using Catch::StartsWith;
TEST_CASE("misc") {
SECTION("no_grad") {
torch::NoGradGuard guard;
Linear model(5, 2);
auto x = torch::randn({10, 5}, torch::requires_grad());
auto y = model->forward(x);
torch::Tensor s = y.sum();
TEST_CASE("NoGrad") {
torch::manual_seed(0);
torch::NoGradGuard guard;
Linear model(5, 2);
auto x = torch::randn({10, 5}, torch::requires_grad());
auto y = model->forward(x);
torch::Tensor s = y.sum();
s.backward();
REQUIRE(!model->parameters()["weight"].grad().defined());
}
SECTION("CPU random seed") {
int size = 100;
torch::manual_seed(7);
auto x1 = torch::randn({size});
torch::manual_seed(7);
auto x2 = torch::randn({size});
auto l_inf = (x1.data() - x2.data()).abs().max().toCFloat();
REQUIRE(l_inf < 1e-10);
}
}
TEST_CASE("misc_cuda", "[cuda]") {
SECTION("CUDA random seed") {
int size = 100;
torch::manual_seed(7);
auto x1 = torch::randn({size}, torch::kCUDA);
torch::manual_seed(7);
auto x2 = torch::randn({size}, torch::kCUDA);
auto l_inf = (x1.data() - x2.data()).abs().max().toCFloat();
REQUIRE(l_inf < 1e-10);
}
s.backward();
REQUIRE(!model->parameters()["weight"].grad().defined());
}
TEST_CASE("autograd") {
torch::manual_seed(0);
auto x = torch::randn({3, 3}, torch::requires_grad());
auto y = torch::randn({3, 3});
auto z = x * y;
@@ -74,6 +50,7 @@ TEST_CASE("autograd") {
}
TEST_CASE("expanding-array") {
torch::manual_seed(0);
SECTION("successful construction") {
SECTION("initializer_list") {
torch::ExpandingArray<5> e({1, 2, 3, 4, 5});