Skip to content

Commit

Permalink
Set seed to 0 for all tests
Browse files Browse the repository at this point in the history
Summary:
Sets the seed via a fixture used on every test (autoUse=True). Can be overridden with --seed=78585 on command line.

ignore import

Test Plan: CI

Reviewers: #framework_ip_review_-_any_oss_or_third-party_code_use_has_been_approved, #pytorch, anthonyb

Reviewed By: #framework_ip_review_-_any_oss_or_third-party_code_use_has_been_approved, #pytorch, anthonyb

Subscribers: anthonyb

Maniphest Tasks: T71821

Differential Revision: https://phabricator.sourcevertex.net/D78595
  • Loading branch information
matthewhagraphcore authored and AnthonyBarbier committed Dec 9, 2022
1 parent 5f8c3dc commit 28f39a5
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#!/usr/bin/env python3
# Copyright (c) 2020 Graphcore Ltd. All rights reserved.

import random
import enum
import gc
import pytest
import torch
import helpers
import numpy as np
import poptorch


Expand Down Expand Up @@ -128,3 +130,22 @@ def pytest_addoption(parser):
default=False,
help=("Run some tests with a reduced "
"number of parameters"))
parser.addoption("--seed",
type=int,
default=0,
help=("Set the seed for running the tests."))


@pytest.fixture(autouse=True, scope="function")
def random_seed(pytestconfig):
"""Set the random seed for all tests in this directory. autouse=True will
use this fixture in every test. Seed can be overridden with --seed on the
command line to alter the seed for testing purposes. By default uses 0 for
all tests.
"""
seed = 0
if hasattr(pytestconfig, "seed"):
seed = pytestconfig.seed
np.random.seed(seed)
random.seed(seed)
torch.manual_seed(seed)

0 comments on commit 28f39a5

Please sign in to comment.