Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More guarantees for minimum and multiple_of in SizeRequirements (bc) #124

Merged
merged 3 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/spandrel/__helpers/model_descriptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,18 @@ class SizeRequirements:
"""
The minimum size of the input image in pixels.

`minimum` is **NOT** guaranteed to be a multiple of `multiple_of`.
`minimum` is guaranteed to be a multiple of `multiple_of` and to be >= 0.

On initialization, if `minimum` is not a multiple of `multiple_of`, it will be rounded up to the next multiple of `multiple_of`.

Default/neutral value: `0`
"""
multiple_of: int = 1
"""
The width and height of the image must be a multiple of this value.

`multiple_of` is guaranteed to be >= 1.

Default/neutral value: `1`
"""
square: bool = False
Expand All @@ -45,6 +49,13 @@ class SizeRequirements:
Default/neutral value: `False`
"""

def __post_init__(self):
assert self.minimum >= 0, "minimum must be >= 0"
assert self.multiple_of >= 1, "multiple_of must be >= 1"

if self.minimum % self.multiple_of != 0:
self.minimum = (self.minimum // self.multiple_of + 1) * self.multiple_of

@property
def none(self) -> bool:
"""
Expand All @@ -56,7 +67,7 @@ def none(self) -> bool:

def check(self, width: int, height: int) -> bool:
"""
Checks if the given width and height satisfy the size requirements.
Returns whether the given width and height satisfy the size requirements.
"""
if width < self.minimum or height < self.minimum:
return False
Expand Down
2 changes: 1 addition & 1 deletion tests/__snapshots__/test_ESRGAN.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
output_channels=3,
purpose='SR',
scale=2,
size_requirements=SizeRequirements(minimum=2, multiple_of=4, square=False),
size_requirements=SizeRequirements(minimum=4, multiple_of=4, square=False),
supports_bfloat16=True,
supports_half=True,
tags=list([
Expand Down
16 changes: 16 additions & 0 deletions tests/test_size_req.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import pytest

from spandrel import SizeRequirements


def test_size_req_init():
a = SizeRequirements(minimum=1, multiple_of=2)
assert a.minimum == 2
assert a.multiple_of == 2

with pytest.raises(AssertionError):
SizeRequirements(minimum=-1)
with pytest.raises(AssertionError):
SizeRequirements(multiple_of=0)
with pytest.raises(AssertionError):
SizeRequirements(multiple_of=-1)