small changes (#425)

* docs: update todo

* refactor: small cleanup of tiling code
pull/426/head
Bryce Drennan 5 months ago committed by GitHub
parent d834e8b5b3
commit 616f686ed2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -31,11 +31,14 @@ autoformat: ## Run the autoformatter.
@-ruff check --config tests/ruff.toml . --fix-only
@black .
test: ## Run the tests.
@pytest
@echo -e "The tests pass! ✨ 🍰 ✨"
test-fast: ## Run the fast tests.
@pytest -m "not gputest"
@echo -e "The non-gpu tests pass! ✨ 🍰 ✨"
lint: ## Run the code linter.
@ruff check --config tests/ruff.toml .
@echo -e "No linting errors - well done! ✨ 🍰 ✨"
@ -43,6 +46,12 @@ lint: ## Run the code linter.
type-check: ## Run the type checker.
@mypy --config-file tox.ini .
check-fast: ## Run autoformatter, linter, typechecker, and fast tests
@make autoformat
@make lint
@make type-check
@make test-fast
deploy: ## Deploy the package to pypi.org
pip install twine wheel
-git tag $$(python setup.py -V)

@ -22,6 +22,17 @@
- add method to show cache size
- add method to clear model cache
- add method to clear cached items not recently used (does diffusers have one?)
- create actual documentation
- SDXL Support
#### Investigate
- Scalecrafter https://yingqinghe.github.io/scalecrafter/
- Fast diffusion with LCM Lora https://huggingface.co/latent-consistency/lcm-lora-sdv1-5/tree/main
- 3d diffusion https://huggingface.co/stabilityai/stable-zero123
- magic animate
- consistency decoder
### Old Todo
- Inference Performance Optimizations

@ -22,7 +22,6 @@ from refiners.foundationals.latent_diffusion.stable_diffusion_1.model import (
)
from torch import Tensor, nn
from torch.nn import functional as F
from torch.nn.modules.utils import _pair
from imaginairy.utils.feather_tile import rebuild_image, tile_image
from imaginairy.weight_management.conversion import cast_weights
@ -35,17 +34,17 @@ TileModeType = Literal["", "x", "y", "xy"]
def _tile_mode_conv2d_conv_forward(
self, input: torch.Tensor, weight: torch.Tensor, bias: torch.Tensor # noqa
):
if self.padding_modeX == self.padding_modeY:
self.padding_mode = self.padding_modeX
if self.padding_mode_x == self.padding_mode_y:
self.padding_mode = self.padding_mode_x
return self._orig_conv_forward(input, weight, bias)
w1 = F.pad(input, self.paddingX, mode=self.padding_modeX)
w1 = F.pad(input, self.padding_x, mode=self.padding_modeX)
del input
w2 = F.pad(w1, self.paddingY, mode=self.padding_modeY)
w2 = F.pad(w1, self.padding_y, mode=self.padding_modeY)
del w1
return F.conv2d(w2, weight, bias, self.stride, _pair(0), self.dilation, self.groups)
return F.conv2d(w2, weight, bias, self.stride, (0, 0), self.dilation, self.groups)
class TileModeMixin(nn.Module):
@ -57,34 +56,21 @@ class TileModeMixin(nn.Module):
tile_mode: One of "", "x", "y", "xy". If "x", the image will be tiled horizontally. If "y", the image will be
tiled vertically. If "xy", the image will be tiled both horizontally and vertically.
"""
tile_x = "x" in tile_mode
tile_y = "y" in tile_mode
padding_mode_x = "circular" if "x" in tile_mode else "constant"
padding_mode_y = "circular" if "y" in tile_mode else "constant"
for m in self.modules():
if isinstance(m, nn.Conv2d):
if not hasattr(m, "_orig_conv_forward"):
# patch with a function that can handle tiling in a single direction
m._initial_padding_mode = m.padding_mode # type: ignore
m._orig_conv_forward = m._conv_forward # type: ignore
m._conv_forward = _tile_mode_conv2d_conv_forward.__get__( # type: ignore
m, nn.Conv2d
)
m.padding_modeX = "circular" if tile_x else "constant" # type: ignore
m.padding_modeY = "circular" if tile_y else "constant" # type: ignore
if m.padding_modeY == m.padding_modeX:
m.padding_mode = m.padding_modeX
m.paddingX = (
m._reversed_padding_repeated_twice[0],
m._reversed_padding_repeated_twice[1],
0,
0,
) # type: ignore
m.paddingY = (
0,
0,
m._reversed_padding_repeated_twice[2],
m._reversed_padding_repeated_twice[3],
) # type: ignore
if not isinstance(m, nn.Conv2d):
continue
if not hasattr(m, "_orig_conv_forward"):
# patch with a function that can handle tiling in a single direction
m._initial_padding_mode = m.padding_mode # type: ignore
m._orig_conv_forward = m._conv_forward # type: ignore
m._conv_forward = _tile_mode_conv2d_conv_forward.__get__(m, nn.Conv2d) # type: ignore
m.padding_mode_x = padding_mode_x # type: ignore
m.padding_mode_y = padding_mode_y # type: ignore
rprt: list[int] = m._reversed_padding_repeated_twice
m.padding_x = (rprt[0], rprt[1], 0, 0) # type: ignore
m.padding_y = (0, 0, rprt[2], rprt[3]) # type: ignore
class StableDiffusion_1(TileModeMixin, RefinerStableDiffusion_1):

@ -5,11 +5,11 @@ import contextlib
_NAMED_RESOLUTIONS = {
"HD": (1280, 720),
"FHD": (1920, 1080),
"HALF-FHD": (960, 540),
"2K": (2048, 1080),
"4K": (3840, 2160),
"UHD": (3840, 2160),
"8K": (7680, 4320),
"360p": (640, 360),
"16K": (15360, 8640),
"VGA": (640, 480),
"SVGA": (800, 600),
"XGA": (1024, 768),
@ -26,6 +26,7 @@ _NAMED_RESOLUTIONS = {
"WQXGA": (2560, 1600),
"UWQHD": (3440, 1440),
"240p": (426, 240),
"360p": (640, 360),
"480p": (854, 480),
"720p": (1280, 720),
"1080p": (1920, 1080),
@ -40,8 +41,8 @@ _NAMED_RESOLUTIONS = {
"HDV": (1440, 1080),
"WQHD": (2560, 1440),
"UW-UXGA": (2560, 1080),
"UHD": (3840, 2160),
"UHD+": (5120, 2880),
"8K UHD": (7680, 4320),
"SVD": (1024, 576), # stable video diffusion
}

Loading…
Cancel
Save