From 8ffaea76ba2cceb4947f6b44095da7342e857ce8 Mon Sep 17 00:00:00 2001 From: Disty0 Date: Mon, 31 Jul 2023 14:39:32 +0300 Subject: [PATCH] Add Diffusers model and VAE variant loading option --- html/locale_en.json | 5 ++++- html/locale_ko.json | 4 +++- modules/sd_models.py | 9 +++++++-- modules/sd_vae.py | 9 +++++++-- modules/shared.py | 2 ++ 5 files changed, 23 insertions(+), 6 deletions(-) diff --git a/html/locale_en.json b/html/locale_en.json index 6a13e9cd4..0d2a6cc20 100644 --- a/html/locale_en.json +++ b/html/locale_en.json @@ -572,7 +572,10 @@ {"id":"","label":"Enable model CPU offload","localized":"","hint":"Transferring of entire models to the GPU, negligible impact on inference time while still providing some memory savings. Use with Enable Attention slicing for additional memory savings"}, {"id":"","label":"Enable VAE slicing","localized":"","hint":"Decodes batch latents one image at a time with limited VRAM. Small performance boost in VAE decode on multi-image batches. Use with Enable Attention slicing"}, {"id":"","label":"Enable VAE tiling","localized":"","hint":"Divide large images into overlapping tiles with limited VRAM. Might result in a minor increase in processing time. Use with Enable Attention Slicing"}, - {"id":"","label":"Enable attention slicing","localized":"","hint":"Performs attention computation in steps instead of all at once. 10% slower inference times. Greatly reduces memory usage. Best used, period"} + {"id":"","label":"Enable attention slicing","localized":"","hint":"Performs attention computation in steps instead of all at once. 10% slower inference times. Greatly reduces memory usage. Best used, period"}, + {"id":"","label":"Diffusers model loading variant","localized":"","hint":""}, + {"id":"","label":"Diffusers VAE loading variant","localized":"","hint":""} + ], "scripts": [ {"id":"","label":"Script","localized":"","hint":""}, diff --git a/html/locale_ko.json b/html/locale_ko.json index b2a2807d0..f91f7759c 100644 --- a/html/locale_ko.json +++ b/html/locale_ko.json @@ -572,7 +572,9 @@ {"id":"","label":"Enable model CPU offload","localized":"모델 CPU 오프로드 활성화","hint":"전체 모델을 GPU로 옮긴다. 여전히 GPU 메모리 사용률을 약간 낮춰주지만 생성 속도에는 무시할 수 있는 정도의 영향을 준다. 추가적인 메모리 절약을 위해서는 어텐션 슬라이싱과 함께 사용한다."}, {"id":"","label":"Enable VAE slicing","localized":"VAE 슬라이싱 활성화","hint":"Decodes batch latents one image at a time with limited VRAM. 여러 이미지를 다룰 때 VAE 해독 시 작은 성능 향상이 있다. 어텐션 슬라이싱과 함께 사용한다."}, {"id":"","label":"Enable VAE tiling","localized":"VAE 타일링 활성화","hint":"Divide large images into overlapping tiles with limited VRAM. 생성 시간이 약간 늘어날 수 있다. 어텐션 슬라이싱과 함께 사용한다."}, - {"id":"","label":"Enable attention slicing","localized":"어텐션 슬라이싱 활성화","hint":"Performs attention computation in steps instead of all at once. 생성이 10% 느려지지만 메모리 사용률을 매우 크게 줄여준다."} + {"id":"","label":"Enable attention slicing","localized":"어텐션 슬라이싱 활성화","hint":"Performs attention computation in steps instead of all at once. 생성이 10% 느려지지만 메모리 사용률을 매우 크게 줄여준다."}, + {"id":"","label":"Diffusers model loading variant","localized":"","hint":""}, + {"id":"","label":"Diffusers VAE loading variant","localized":"","hint":""} ], "scripts": [ {"id":"","label":"Script","localized":"스크립트","hint":""}, diff --git a/modules/sd_models.py b/modules/sd_models.py index cc1f92cb4..72dacf3bf 100644 --- a/modules/sd_models.py +++ b/modules/sd_models.py @@ -548,8 +548,13 @@ def load_diffuser(checkpoint_info=None, already_loaded_state_dict=None, timer=No "load_connected_pipeline": True # always load end-to-end / connected pipelines # "use_safetensors": True, # TODO(PVP) - we can't enable this for all checkpoints just yet } - if devices.dtype == torch.float16: - diffusers_load_config['variant'] = 'fp16' + if shared.opts.diffusers_model_load_variant == 'default': + if devices.dtype == torch.float16: + diffusers_load_config['variant'] = 'fp16' + elif shared.opts.diffusers_model_load_variant == 'fp32': + pass + else: + diffusers_load_config['variant'] = shared.opts.diffusers_model_load_variant if shared.opts.data.get('sd_model_checkpoint', '') == 'model.ckpt' or shared.opts.data.get('sd_model_checkpoint', '') == '': shared.opts.data['sd_model_checkpoint'] = "runwayml/stable-diffusion-v1-5" diff --git a/modules/sd_vae.py b/modules/sd_vae.py index acab7cfda..e87af749f 100644 --- a/modules/sd_vae.py +++ b/modules/sd_vae.py @@ -181,8 +181,13 @@ def load_vae_diffusers(_model, vae_file=None, vae_source="from unknown source"): "torch_dtype": devices.dtype_vae, "use_safetensors": True, } - if devices.dtype_vae == torch.float16: - diffusers_load_config['variant'] = 'fp16' + if shared.opts.diffusers_vae_load_variant == 'default': + if devices.dtype_vae == torch.float16: + diffusers_load_config['variant'] = 'fp16' + elif shared.opts.diffusers_vae_load_variant == 'fp32': + pass + else: + diffusers_load_config['variant'] = shared.opts.diffusers_vae_load_variant if shared.opts.diffusers_vae_upcast != 'default': diffusers_load_config['force_upcast'] = True if shared.opts.diffusers_vae_upcast == 'true' else False diff --git a/modules/shared.py b/modules/shared.py index 711a77802..596058796 100644 --- a/modules/shared.py +++ b/modules/shared.py @@ -407,6 +407,8 @@ options_templates.update(options_section(('diffusers', "Diffusers Settings"), { "diffusers_vae_slicing": OptionInfo(True, "Enable VAE slicing"), "diffusers_vae_tiling": OptionInfo(False, "Enable VAE tiling"), "diffusers_attention_slicing": OptionInfo(False, "Enable attention slicing"), + "diffusers_model_load_variant": OptionInfo("default", "Diffusers model loading variant", gr.Radio, lambda: {"choices": ['default', 'fp32', 'fp16']}), + "diffusers_vae_load_variant": OptionInfo("default", "Diffusers VAE loading variant", gr.Radio, lambda: {"choices": ['default', 'fp32', 'fp16']}), # "diffusers_force_zeros": OptionInfo(False, "Force zeros for prompts when empty"), # "diffusers_aesthetics_score": OptionInfo(6.0, "Require aesthetic score", gr.Slider, {"minimum": 0, "maximum": 10, "step": 0.1}), }))