Skip to content

Commit

Permalink
add ras-sd35 experimental
Browse files Browse the repository at this point in the history
Signed-off-by: Vladimir Mandic <[email protected]>
  • Loading branch information
vladmandic committed Feb 18, 2025
1 parent 041f0bb commit 6cf445d
Show file tree
Hide file tree
Showing 18 changed files with 777 additions and 17 deletions.
3 changes: 2 additions & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,21 @@ ignore-paths=/usr/lib/.*$,
modules/k-diffusion,
modules/ldsr,
modules/meissonic,
modules/mod,
modules/omnigen,
modules/onnx_impl,
modules/pag,
modules/pixelsmith,
modules/prompt_parser_xhinker.py,
modules/pulid/eva_clip,
modules/ras,
modules/rife,
modules/schedulers,
modules/taesd,
modules/teacache,
modules/todo,
modules/unipc,
modules/xadapter,
modules/mod,
repositories,
extensions-builtin/Lora,
extensions-builtin/sd-webui-agent-scheduler,
Expand Down
3 changes: 2 additions & 1 deletion .ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ exclude = [
"modules/k-diffusion",
"modules/ldsr",
"modules/meissonic",
"modules/mod",
"modules/omnigen",
"modules/pag",
"modules/pixelsmith",
"modules/postprocess/aurasr_arch.py",
"modules/prompt_parser_xhinker.py",
"modules/pulid/eva_clip",
"modules/ras",
"modules/rife",
"modules/schedulers",
"modules/segmoe",
Expand All @@ -31,7 +33,6 @@ exclude = [
"modules/todo",
"modules/unipc",
"modules/xadapter",
"modules/mod",
"repositories",
"extensions-builtin/Lora",
"extensions-builtin/sd-extension-chainner/nodes",
Expand Down
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,16 @@ We're back with another update with nearly 100 commits!
result is model smaller than *Flux.1-Dev*, but with similar capabilities
fully supports offloading and on-the-fly quantization
simply select from *networks -> models -> reference*
- **Pipelines**
- **Functions**
- [Mixture-of-Diffusers](https://huggingface.co/posts/elismasilva/251775641926329)
Regional tiling type of a solution for SDXL models
select from *scripts -> mixture of diffusers*
- [Automatic Color Inpaint]
Automatically creates mask based on selected color and triggers inpaint
simply select in *scripts -> automatic color inpaint* when in img2img mode
- [RAS: Region-Adaptive Sampling](https://github.com/microsoft/RAS) *experimental*
Speeds up SD3.5 models by sampling only regions of interest
Enable in *settings -> pipeline modifiers -> ras*
- **Interrogate/Captioning**
- Redesigned captioning UI
split from Process tab into separate tab
Expand Down
2 changes: 1 addition & 1 deletion modules/interrogate/deepseek.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
vl_chat_processor = None


class fake_attrdict(object):
class fake_attrdict():
class AttrDict(dict): # dot notation access to dictionary attributes
__getattr__ = dict.get
__setattr__ = dict.__setitem__
Expand Down
2 changes: 1 addition & 1 deletion modules/interrogate/interrogate.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def interrogate(image):
elif shared.opts.interrogate_default_type == 'VLM':
shared.log.info(f'Interrogate: type={shared.opts.interrogate_default_type} vlm="{shared.opts.interrogate_vlm_model}" prompt="{shared.opts.interrogate_vlm_prompt}"')
from modules.interrogate import vqa
prompt = vqa.interrogate(image=image, model_name=shared.opts.interrogate_vlm_model, question=shared.opts.interrogate_vlm_prompt)
prompt = vqa.interrogate(image=image, model_name=shared.opts.interrogate_vlm_model, question=shared.opts.interrogate_vlm_prompt, prompt=None)
shared.log.debug(f'Interrogate: time={time.time()-t0:.2f} answer="{prompt}"')
return prompt
else:
Expand Down
2 changes: 1 addition & 1 deletion modules/interrogate/joytag.py
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,7 @@ def __init__(self, config: str):
ty, line = line.split(":") if ":" in line else (line, "")
options = line.split(",")
options = [o.split("=") for o in options] if line else []
options = {k: v for k, v in options} # noqa: C416
options = {k: v for k, v in options} # pylint: disable=unnecessary-comprehension # noqa: C416
if ty == 'conv':
layers.append(nn.Conv2d(
in_channels=channels,
Expand Down
10 changes: 5 additions & 5 deletions modules/interrogate/openclip.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,11 +239,11 @@ def close(self):

def update_interrogate_params():
if ci is not None:
ci.caption_max_length=shared.opts.interrogate_clip_max_length,
ci.chunk_size=shared.opts.interrogate_clip_chunk_size,
ci.flavor_intermediate_count=shared.opts.interrogate_clip_flavor_count,
ci.clip_offload=shared.opts.interrogate_offload,
ci.caption_offload=shared.opts.interrogate_offload,
ci.caption_max_length=shared.opts.interrogate_clip_max_length
ci.chunk_size=shared.opts.interrogate_clip_chunk_size
ci.flavor_intermediate_count=shared.opts.interrogate_clip_flavor_count
ci.clip_offload=shared.opts.interrogate_offload
ci.caption_offload=shared.opts.interrogate_offload


def get_clip_models():
Expand Down
1 change: 0 additions & 1 deletion modules/masking.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,6 @@ def run_mask(input_image: Image.Image, input_mask: Image.Image = None, return_ty
debug(f'Mask args legacy: blur={mask_blur} padding={mask_padding}')
if invert is not None:
opts.invert = invert
print('HERE', opts.mask_blur, mask_blur)
if mask_blur is not None: # compatibility with old img2img values which uses px values
opts.mask_blur = round(4 * mask_blur / size, 3)
if mask_padding is not None: # compatibility with old img2img values which uses px values
Expand Down
4 changes: 2 additions & 2 deletions modules/postprocess/dcc.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import numpy as np


def DetectDirect(A, type, k, T):
if type == 1:
def DetectDirect(A, dcc_type, k, T):
if dcc_type == 1:
# 45 degree diagonal direction
t1 = abs(A[2,0]-A[0,2])
t2 = abs(A[4,0]-A[2,2])+abs(A[2,2]-A[0,4])
Expand Down
4 changes: 3 additions & 1 deletion modules/processing_diffusers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import torch
import torchvision.transforms.functional as TF
from PIL import Image
from modules import shared, devices, processing, sd_models, errors, sd_hijack_hypertile, processing_vae, sd_models_compile, hidiffusion, timer, modelstats, extra_networks
from modules import shared, devices, processing, sd_models, errors, sd_hijack_hypertile, processing_vae, sd_models_compile, hidiffusion, timer, modelstats, extra_networks, ras
from modules.processing_helpers import resize_hires, calculate_base_steps, calculate_hires_steps, calculate_refiner_steps, save_intermediate, update_sampler, is_txt2img, is_refiner_enabled, get_job_name
from modules.processing_args import set_pipeline_args
from modules.onnx_impl import preprocess_pipeline as preprocess_onnx_pipeline, check_parameters_changed as olive_check_parameters_changed
Expand Down Expand Up @@ -93,6 +93,7 @@ def process_base(p: processing.StableDiffusionProcessing):
sd_models.move_model(shared.sd_model.transformer, devices.device)
extra_networks.activate(p, exclude=['text_encoder', 'text_encoder_2', 'text_encoder_3'])
hidiffusion.apply(p, shared.sd_model_type)
ras.apply(shared.sd_model, p)
timer.process.record('move')
if hasattr(shared.sd_model, 'tgate') and getattr(p, 'gate_step', -1) > 0:
base_args['gate_step'] = p.gate_step
Expand All @@ -106,6 +107,7 @@ def process_base(p: processing.StableDiffusionProcessing):
if hasattr(output, 'images'):
shared.history.add(output.images, info=processing.create_infotext(p), ops=p.ops)
timer.process.record('pipeline')
ras.unapply(shared.sd_model)
hidiffusion.unapply()
sd_models_compile.openvino_post_compile(op="base") # only executes on compiled vino models
sd_models_compile.check_deepcache(enable=False)
Expand Down
2 changes: 1 addition & 1 deletion modules/prompt_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ def multiply_range(start_position, multiplier):
# log.trace(f'Prompt: text="{text[m.start():m.end()]}" section="{section}" weight="{weight}"')
if len(section) == 0:
continue
elif section.startswith('\\'):
if section.startswith('\\'):
if len(res) > 0 and text[m.start()-1] != ' ':
res[-1][0] += section[1:] # append literal character to the last section
else:
Expand Down
36 changes: 36 additions & 0 deletions modules/ras/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# source <https://github.com/Trojaner/RAS_Simplified>
# original: <https://github.com/microsoft/RAS>

from modules import shared, processing


def apply(pipe, p: processing.StableDiffusionProcessing):
if shared.sd_model_type != "sd3" or not shared.opts.ras_enable:
return
from .ras_manager import MANAGER
from .ras_scheduler import RASFlowMatchEulerDiscreteScheduler
from .ras_attention import RASJointAttnProcessor2_0
from .ras_forward import ras_forward
scheduler = RASFlowMatchEulerDiscreteScheduler.from_config(pipe.scheduler.config)
pipe.scheduler = scheduler
MANAGER.num_steps = p.steps
MANAGER.scheduler_end_step = p.steps
MANAGER.width = p.width
MANAGER.height = p.height
MANAGER.error_reset_steps = [int(1*p.steps/3), int(2*p.steps/3)]
shared.log.info(f'RAS: scheduler={pipe.scheduler.__class__.__name__} {str(MANAGER)}')
MANAGER.reset_cache()
MANAGER.generate_skip_token_list()
pipe.transformer.old_forward = pipe.transformer.forward
pipe.transformer.forward = ras_forward.__get__(pipe.transformer, pipe.transformer.__class__) # pylint: disable=no-value-for-parameter
for block in pipe.transformer.transformer_blocks:
block.attn.set_processor(RASJointAttnProcessor2_0())


def unapply(pipe):
if hasattr(pipe, 'transformer') and hasattr(pipe.transformer, "old_forward"):
from diffusers.models.attention_processor import JointAttnProcessor2_0
pipe.transformer.forward = pipe.transformer.old_forward
del pipe.transformer.old_forward
for block in pipe.transformer.transformer_blocks:
block.attn.set_processor(JointAttnProcessor2_0())
Loading

0 comments on commit 6cf445d

Please sign in to comment.