Skip to content

Commit

Permalink
reorg imports for faster startup
Browse files Browse the repository at this point in the history
  • Loading branch information
vladmandic committed Apr 7, 2024
1 parent 01b9c4d commit 19db61a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
10 changes: 7 additions & 3 deletions modules/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,17 @@
torch.__version__ = re.search(r'[\d.]+[\d]', torch.__version__).group(0)
timer.startup.record("torch")

import transformers # pylint: disable=W0611,C0411
timer.startup.record("transformers")

import onnxruntime
onnxruntime.set_default_logger_severity(3)
timer.startup.record("onnx")

from modules.onnx_impl import initialize_olive # pylint: disable=ungrouped-imports
initialize_olive()
timer.startup.record("olive")
# moved to webui.py:initialize()
# from modules.onnx_impl import initialize_olive # pylint: disable=ungrouped-imports
# initialize_olive()
# timer.startup.record("olive")

from fastapi import FastAPI # pylint: disable=W0611,C0411
import gradio # pylint: disable=W0611,C0411
Expand Down
8 changes: 4 additions & 4 deletions repositories/ldm/modules/encoders/modules.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import torch
import torch.nn as nn
import kornia
from torch.utils.checkpoint import checkpoint

from transformers import T5Tokenizer, T5EncoderModel, CLIPTokenizer, CLIPTextModel

import open_clip
from ldm.util import default, count_params, autocast


class AbstractEncoder(nn.Module):
def __init__(self):
super().__init__()
Expand Down Expand Up @@ -62,6 +58,7 @@ class FrozenT5Embedder(AbstractEncoder):
def __init__(self, version="google/t5-v1_1-large", device="cuda", max_length=77,
freeze=True): # others are google/t5-v1_1-xl and google/t5-v1_1-xxl
super().__init__()
from transformers import T5Tokenizer, T5EncoderModel
self.tokenizer = T5Tokenizer.from_pretrained(version)
self.transformer = T5EncoderModel.from_pretrained(version)
self.device = device
Expand Down Expand Up @@ -99,6 +96,7 @@ class FrozenCLIPEmbedder(AbstractEncoder):
def __init__(self, version="openai/clip-vit-large-patch14", device="cuda", max_length=77,
freeze=True, layer="last", layer_idx=None): # clip-vit-base-patch32
super().__init__()
from transformers import CLIPTokenizer, CLIPTextModel
assert layer in self.LAYERS
self.tokenizer = CLIPTokenizer.from_pretrained(version)
self.transformer = CLIPTextModel.from_pretrained(version)
Expand Down Expand Up @@ -155,6 +153,7 @@ def __init__(
self.ucg_rate = ucg_rate

def preprocess(self, x):
import kornia
# normalize to [0,1]
x = kornia.geometry.resize(x, (224, 224),
interpolation='bicubic', align_corners=True,
Expand Down Expand Up @@ -265,6 +264,7 @@ def __init__(self, arch="ViT-H-14", version="laion2b_s32b_b79k", device="cuda",
self.ucg_rate = ucg_rate

def preprocess(self, x):
import kornia
# normalize to [0,1]
x = kornia.geometry.resize(x, (224, 224),
interpolation='bicubic', align_corners=True,
Expand Down
6 changes: 6 additions & 0 deletions webui.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ def initialize():
log.debug('Initializing')
check_rollback_vae()

if shared.opts.cuda_compile_backend == "olive-ai":
from modules.onnx_impl import initialize_olive, install_olive
install_olive()
initialize_olive()
timer.startup.record("olive")

modules.sd_samplers.list_samplers()
timer.startup.record("samplers")

Expand Down

0 comments on commit 19db61a

Please sign in to comment.