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
+7
View File
@@ -4,6 +4,7 @@
#include <torch/nn/modules/linear.h>
#include <torch/nn/modules/rnn.h>
#include <torch/tensor.h>
#include <torch/utils.h>
using namespace torch::nn;
@@ -23,6 +24,7 @@ bool pointer_equal(torch::Tensor first, torch::Tensor second) {
}
TEST_CASE("module/training-mode") {
torch::manual_seed(0);
Linear module(3, 4);
REQUIRE(module->is_training());
SECTION("Enable eval mode") {
@@ -36,6 +38,7 @@ TEST_CASE("module/training-mode") {
}
TEST_CASE("module/zero-grad") {
torch::manual_seed(0);
Linear module(3, 4);
auto weight = torch::ones({8, 3}, torch::requires_grad());
auto loss = module->forward(weight).sum();
@@ -67,6 +70,7 @@ TEST_CASE("module/name") {
}
TEST_CASE("module/conversions", "[cuda]") {
torch::manual_seed(0);
Linear module(128, 64);
SECTION("starts as float on CPU") {
for (auto& parameter : module->parameters()) {
@@ -117,6 +121,7 @@ TEST_CASE("module/conversions", "[cuda]") {
}
TEST_CASE("module/clone") {
torch::manual_seed(0);
SECTION(
"a module that does not override clone() throws when clone() is called") {
struct UnCloneable : Module {};
@@ -232,6 +237,7 @@ TEST_CASE("module/clone") {
}
TEST_CASE("module/parameters") {
torch::manual_seed(0);
struct TestModule : Module {
TestModule() {
a = register_parameter("a", torch::zeros({2, 2}));
@@ -257,6 +263,7 @@ TEST_CASE("module/parameters") {
}
TEST_CASE("module/buffers") {
torch::manual_seed(0);
struct TestModule : Module {
TestModule() {
a = register_buffer("a", torch::zeros({2, 2}));