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

Update to spandrel 0.1.5 and enforce model tiling hint #2412

Merged
merged 2 commits into from
Dec 17, 2023
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
11 changes: 8 additions & 3 deletions backend/src/nodes/properties/outputs/pytorch_outputs.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from __future__ import annotations

from spandrel import (
ModelDescriptor,
)
from spandrel import ModelDescriptor, ModelTiling

import navi
from api import BaseOutput, OutputKind
Expand All @@ -29,6 +27,12 @@ def get_broadcast_data(self, value: ModelDescriptor):
}

def get_broadcast_type(self, value: ModelDescriptor):
tiling_map: dict[ModelTiling, str] = {
ModelTiling.SUPPORTED: "ModelTiling::Supported",
ModelTiling.DISCOURAGED: "ModelTiling::Discouraged",
ModelTiling.INTERNAL: "ModelTiling::Internal",
}

return navi.named(
"PyTorchModel",
{
Expand All @@ -38,6 +42,7 @@ def get_broadcast_type(self, value: ModelDescriptor):
"arch": navi.literal(value.architecture),
"subType": navi.literal(value.purpose),
"size": navi.literal("x".join(value.tags)),
"tiling": tiling_map[value.tiling],
},
)

Expand Down
2 changes: 1 addition & 1 deletion backend/src/packages/chaiNNer_pytorch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def get_pytorch():
Dependency(
display_name="Spandrel",
pypi_name="spandrel",
version="0.1.3",
version="0.1.5",
size_estimate=250 * KB,
),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
import numpy as np
import torch
from sanic.log import logger
from spandrel import ImageModelDescriptor
from spandrel import ImageModelDescriptor, ModelTiling

from nodes.groups import Condition, if_group
from nodes.impl.pytorch.auto_split import pytorch_auto_split
from nodes.impl.upscale.auto_split_tiles import (
NO_TILING,
TileSize,
estimate_tile_size,
parse_tile_size_input,
Expand Down Expand Up @@ -41,6 +42,10 @@ def upscale(
use_fp16 = options.use_fp16 and model.supports_half
device = options.device

if model.tiling == ModelTiling.INTERNAL:
# disable tiling if the model already does it internally
tile_size = NO_TILING

def estimate():
if "cuda" in device.type:
mem_info: tuple[int, int] = torch.cuda.mem_get_info(device) # type: ignore
Expand Down Expand Up @@ -84,20 +89,28 @@ def estimate():
inputs=[
ImageInput().with_id(1),
SrModelInput().with_id(0),
TileSizeDropdown()
.with_id(2)
.with_docs(
"Tiled upscaling is used to allow large images to be upscaled without"
" hitting memory limits.",
"This works by splitting the image into tiles (with overlap), upscaling"
" each tile individually, and seamlessly recombining them.",
"Generally it's recommended to use the largest tile size possible for"
" best performance (with the ideal scenario being no tiling at all),"
" but depending on the model and image size, this may not be possible.",
"If you are having issues with the automatic mode, you can manually"
" select a tile size. Sometimes, a manually selected tile size may be"
" faster than what the automatic mode picks.",
hint=True,
if_group(
Condition.type(
0,
"PyTorchModel { tiling: ModelTiling::Supported | ModelTiling::Discouraged } ",
if_not_connected=True,
)
)(
TileSizeDropdown()
.with_id(2)
.with_docs(
"Tiled upscaling is used to allow large images to be upscaled without"
" hitting memory limits.",
"This works by splitting the image into tiles (with overlap), upscaling"
" each tile individually, and seamlessly recombining them.",
"Generally it's recommended to use the largest tile size possible for"
" best performance (with the ideal scenario being no tiling at all),"
" but depending on the model and image size, this may not be possible.",
"If you are having issues with the automatic mode, you can manually"
" select a tile size. Sometimes, a manually selected tile size may be"
" faster than what the automatic mode picks.",
hint=True,
),
),
if_group(
Condition.type(1, "Image { channels: 4 } ")
Expand Down
2 changes: 2 additions & 0 deletions src/common/types/chainner-scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ struct PyTorchModel {
arch: string,
size: string,
subType: string,
tiling: ModelTiling,
}
enum ModelTiling { Supported, Discouraged, Internal }
joeyballentine marked this conversation as resolved.
Show resolved Hide resolved

struct NcnnBinFile { path: string }
struct NcnnParamFile { path: string }
Expand Down