-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0aed709
commit a2ee0b8
Showing
2 changed files
with
86 additions
and
11 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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
from io import StringIO | ||
|
||
import jax.numpy as jnp | ||
from flax import nnx | ||
from pytest import CaptureFixture | ||
from rich.console import Console | ||
|
||
from tjax import JaxRealArray, print_generic | ||
from tjax.dataclasses import DataClassModule, field | ||
|
||
from .test_display import verify | ||
|
||
|
||
def test_module_display(capsys: CaptureFixture[str], | ||
console: Console) -> None: | ||
class C(nnx.Module): | ||
def __init__(self) -> None: | ||
super().__init__() | ||
self.x = jnp.zeros(4) | ||
self.y = 'abc' | ||
self.a = nnx.Variable(jnp.zeros(4)) | ||
|
||
c = C() | ||
print_generic(c=c, console=console, immediate=True) | ||
assert isinstance(console.file, StringIO) | ||
captured = console.file.getvalue() | ||
verify(captured, | ||
""" | ||
c=C[flax-module] | ||
├── x=Jax Array (4,) float64 | ||
│ └── 0.0000 │ 0.0000 │ 0.0000 │ 0.0000 | ||
├── y="abc" | ||
└── a=Variable | ||
└── value=Jax Array (4,) float64 | ||
└── 0.0000 │ 0.0000 │ 0.0000 │ 0.0000 | ||
""") | ||
|
||
|
||
def test_dataclass_module_display(capsys: CaptureFixture[str], | ||
console: Console) -> None: | ||
class C(DataClassModule): | ||
x: JaxRealArray | ||
y: str = field(static=True) | ||
|
||
def __post_init__(self, rngs: nnx.Rngs) -> None: | ||
if hasattr(super(), '__post_init__'): | ||
super().__post_init__(rngs) | ||
self.a = nnx.Variable(jnp.zeros(4)) | ||
|
||
c = C(jnp.zeros(4), 'abc', rngs=nnx.Rngs()) | ||
print_generic(c=c, console=console) | ||
assert isinstance(console.file, StringIO) | ||
captured = console.file.getvalue() | ||
verify(captured, | ||
""" | ||
c=C[dataclass,flax-module] | ||
├── a=Variable | ||
│ └── value=Jax Array (4,) float64 | ||
│ └── 0.0000 │ 0.0000 │ 0.0000 │ 0.0000 | ||
├── x=Jax Array (4,) float64 | ||
│ └── 0.0000 │ 0.0000 │ 0.0000 │ 0.0000 | ||
└── y="abc" | ||
""") |
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