-
-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Now displaying PRNGKey seed in failed tests.
- Loading branch information
1 parent
5d8a06e
commit 92c76e4
Showing
1 changed file
with
24 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,35 @@ | ||
import dataclasses | ||
import random | ||
import typing | ||
|
||
import jax.random as jr | ||
import pytest | ||
from jaxtyping import PRNGKeyArray | ||
|
||
|
||
typing.TESTING = True # pyright: ignore | ||
|
||
|
||
@pytest.fixture() | ||
def getkey(): | ||
def _getkey(): | ||
# Not sure what the maximum actually is but this will do | ||
return jr.PRNGKey(random.randint(0, 2**31 - 1)) | ||
# This offers reproducability -- the initial seed is printed in the repr so we can see | ||
# it when a test fails. | ||
# Note the `eq=False`, which means that `_GetKey `objects have `__eq__` and `__hash__` | ||
# based on object identity. | ||
@dataclasses.dataclass(eq=False) | ||
class _GetKey: | ||
seed: int | ||
call: int | ||
key: PRNGKeyArray | ||
|
||
def __init__(self, seed: int): | ||
self.seed = seed | ||
self.call = 0 | ||
self.key = jr.PRNGKey(seed) | ||
|
||
def __call__(self): | ||
self.call += 1 | ||
return jr.fold_in(self.key, self.call) | ||
|
||
return _getkey | ||
|
||
@pytest.fixture | ||
def getkey(): | ||
return _GetKey(random.randint(0, 2**31 - 1)) |