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

Optimize for inference when using call api #162

Merged
merged 2 commits into from
Feb 19, 2024
Merged
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
6 changes: 5 additions & 1 deletion src/spandrel/__helpers/model_descriptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@ def __init__(
def purpose(self) -> Literal["SR", "FaceSR", "Restoration"]:
return self._purpose

@torch.inference_mode()
def __call__(self, image: Tensor) -> Tensor:
"""
Takes a single image tensor as input and returns a single image tensor as output.
Expand All @@ -466,11 +467,14 @@ def __call__(self, image: Tensor) -> Tensor:
# satisfy size requirements
did_pad, image = pad_tensor(image, self.size_requirements)

# Optimize for inference
self.model.eval()

# call model
output = self._call_fn(self.model, image)
assert isinstance(
output, Tensor
), f"Expected {type(self.model).__name__} model to returns a tensor, but got {type(output)}"
), f"Expected {type(self.model).__name__} model to return a tensor, but got {type(output)}"

# guarantee range
output = output.clamp_(0, 1)
Expand Down
Loading