Skip to content

Commit 71f49c2

Browse files
committed
[Backend Tester] Seed based on test name
ghstack-source-id: 6f79864 ghstack-comment-id: 3177836622 Pull-Request: #13313
1 parent e44ac9a commit 71f49c2

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

backends/test/suite/runner.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import argparse
2+
import hashlib
23
import importlib
34
import re
45
import time
@@ -40,6 +41,16 @@
4041
}
4142

4243

44+
def _get_test_seed(test_base_name: str) -> int:
45+
# Set the seed based on the test base name to give consistent inputs between runs and backends.
46+
# Having a stable hash between runs and across machines is a plus (builtin python hash is not).
47+
# Using MD5 here because it's fast and we don't actually care about cryptographic properties.
48+
hasher = hashlib.md5()
49+
data = test_base_name.encode("utf-8")
50+
hasher.update(data)
51+
# Torch doesn't like very long seeds.
52+
return int.from_bytes(hasher.digest(), "little") % 100_000_000
53+
4354
def run_test( # noqa: C901
4455
model: torch.nn.Module,
4556
inputs: Any,
@@ -59,6 +70,8 @@ def run_test( # noqa: C901
5970
error_statistics: list[ErrorStatistics] = []
6071
extra_stats = {}
6172

73+
torch.manual_seed(_get_test_seed(test_base_name))
74+
6275
# Helper method to construct the summary.
6376
def build_result(
6477
result: TestResult, error: Exception | None = None

0 commit comments

Comments
 (0)