refiners/tests/fluxion/test_module.py
Pierre Chapuis 471ef91d1c make __getattr__ on Module return object, not Any
PyTorch chose to make it Any because they expect its users' code
to be "highly dynamic": https://github.com/pytorch/pytorch/pull/104321

It is not the case for us, in Refiners having untyped code
goes contrary to one of our core principles.

Note that there is currently an open PR in PyTorch to
return `Module | Tensor`, but in practice this is not always
correct either: https://github.com/pytorch/pytorch/pull/115074

I also moved Residuals-related code from SD1 to latent_diffusion
because SDXL should not depend on SD1.
2024-02-06 11:32:18 +01:00

31 lines
914 B
Python

import refiners.fluxion.layers as fl
def test_module_get_path() -> None:
chain = fl.Chain(
fl.Sum(
fl.Linear(1, 1),
fl.Linear(1, 1),
),
fl.Sum(),
)
sum_1 = chain.layer("Sum_1", fl.Sum)
linear_2 = sum_1.layer("Linear_2", fl.Linear)
assert linear_2.get_path(parent=sum_1) == "Chain.Sum_1.Linear_2"
assert linear_2.get_path(parent=sum_1, top=sum_1) == "Sum.Linear_2"
assert sum_1.get_path() == "Chain.Sum_1"
def test_module_basic_attributes() -> None:
class MyModule(fl.Module):
def __init__(self, spam: int = 0, foo: list[str | int] = ["bar", "qux", 42]) -> None:
self.spam = spam
self.foo = foo
self.chunky = "bacon"
m = MyModule(spam=3995)
assert str(m) == "MyModule(spam=3995)"
assert m.basic_attributes() == {"chunky": "bacon", "foo": ["bar", "qux", 42], "spam": 3995}