File size: 2,352 Bytes
37112ef
 
 
 
 
b51595d
37112ef
 
 
 
 
 
acde4c3
37112ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a5ebb54
37112ef
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import torch
from diffusers import (
    AutoPipelineForText2Image,
    AutoencoderKL,
    FluxControlNetModel,
    ControlNetModel,
    FluxMultiControlNetModel,
)
from diffusers.schedulers import *

from config import Config


def init_sys():
    device = "cuda" if torch.cuda.is_available() else "cpu"
    
    models = Config.IMAGES_MODELS
    
    for model in models:
        try:
            model['pipeline'] = AutoPipelineForText2Image.from_pretrained(
                model['repo_id'],
                torch_dtype=model['compute_type'],
                safety_checker=None,
                variant="fp16"
            ).to(device)
        except:
            model['pipeline'] = AutoPipelineForText2Image.from_pretrained(
                model['repo_id'],
                torch_dtype=model['compute_type'],
                safety_checker=None
            ).to(device)
        model['pipeline'].enable_model_cpu_offload()
    
    # VAE n Refiner
    flux_vae = AutoencoderKL.from_pretrained("black-forest-labs/FLUX.1-dev", subfolder="vae", torch_dtype=torch.bfloat16).to(device)
    sdxl_vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16).to(device)
    refiner = AutoPipelineForText2Image.from_pretrained("stabilityai/stable-diffusion-xl-refiner-1.0", vae=sdxl_vae, torch_dtype=torch.float16).to(device)
    
    # ControlNet
    controlnets = Config.IMAGES_CONTROLNETS
    for controlnet in controlnets:
        if controlnet['loader'] == 'flux-multi':
            controlnet['controlnet'] = FluxMultiControlNetModel([FluxControlNetModel.from_pretrained(
                controlnet['repo_id'],
                torch_dtype=controlnet['compute_type']
            ).to(device)])
        elif controlnet['loader'] == 'sdxl':
            controlnet['controlnet'] = ControlNetModel.from_pretrained(
                controlnet['repo_id'],
                torch_dtype=controlnet['compute_type']
            ).to(device)
        elif controlnet['loader'] == 'flux':
            controlnet['controlnet'] = FluxControlNetModel.from_pretrained(
                controlnet['repo_id'],
                torch_dtype=controlnet['compute_type']
            ).to(device)
    
    return device, models, flux_vae, sdxl_vae, refiner, controlnets

device, models, flux_vae, sdxl_vae, refiner, controlnets = init_sys()