optimum-rbln 0.1.12__py3-none-any.whl → 0.1.15__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- optimum/rbln/__init__.py +27 -13
- optimum/rbln/__version__.py +16 -1
- optimum/rbln/diffusers/__init__.py +22 -2
- optimum/rbln/diffusers/models/__init__.py +34 -3
- optimum/rbln/{transformers/generation → diffusers/models/autoencoders}/__init__.py +1 -2
- optimum/rbln/diffusers/models/{autoencoder_kl.py → autoencoders/autoencoder_kl.py} +66 -111
- optimum/rbln/diffusers/models/autoencoders/vae.py +84 -0
- optimum/rbln/diffusers/models/controlnet.py +85 -65
- optimum/rbln/diffusers/models/transformers/__init__.py +24 -0
- optimum/rbln/diffusers/models/transformers/transformer_sd3.py +203 -0
- optimum/rbln/diffusers/models/unets/__init__.py +24 -0
- optimum/rbln/diffusers/models/{unet_2d_condition.py → unets/unet_2d_condition.py} +129 -163
- optimum/rbln/diffusers/pipelines/__init__.py +60 -12
- optimum/rbln/diffusers/pipelines/controlnet/multicontrolnet.py +11 -25
- optimum/rbln/diffusers/pipelines/controlnet/pipeline_controlnet.py +9 -185
- optimum/rbln/diffusers/pipelines/controlnet/pipeline_controlnet_img2img.py +9 -190
- optimum/rbln/diffusers/pipelines/controlnet/pipeline_controlnet_sd_xl.py +9 -191
- optimum/rbln/diffusers/pipelines/controlnet/pipeline_controlnet_sd_xl_img2img.py +9 -192
- optimum/rbln/diffusers/pipelines/stable_diffusion/__init__.py +1 -0
- optimum/rbln/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion.py +4 -110
- optimum/rbln/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_img2img.py +4 -118
- optimum/rbln/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_inpaint.py +32 -0
- optimum/rbln/diffusers/pipelines/stable_diffusion_3/__init__.py +26 -0
- optimum/rbln/diffusers/pipelines/stable_diffusion_3/pipeline_stable_diffusion_3.py +32 -0
- optimum/rbln/diffusers/pipelines/stable_diffusion_3/pipeline_stable_diffusion_3_img2img.py +32 -0
- optimum/rbln/diffusers/pipelines/stable_diffusion_3/pipeline_stable_diffusion_3_inpaint.py +32 -0
- optimum/rbln/diffusers/pipelines/stable_diffusion_xl/__init__.py +1 -0
- optimum/rbln/diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl.py +18 -128
- optimum/rbln/diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl_img2img.py +18 -131
- optimum/rbln/diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl_inpaint.py +32 -0
- optimum/rbln/modeling.py +572 -0
- optimum/rbln/modeling_alias.py +1 -1
- optimum/rbln/modeling_base.py +176 -763
- optimum/rbln/modeling_diffusers.py +329 -0
- optimum/rbln/transformers/__init__.py +2 -2
- optimum/rbln/transformers/cache_utils.py +5 -9
- optimum/rbln/transformers/modeling_rope_utils.py +283 -0
- optimum/rbln/transformers/models/__init__.py +80 -31
- optimum/rbln/transformers/models/auto/auto_factory.py +117 -23
- optimum/rbln/transformers/models/auto/modeling_auto.py +37 -12
- optimum/rbln/transformers/models/bart/modeling_bart.py +3 -6
- optimum/rbln/transformers/models/bert/modeling_bert.py +3 -6
- optimum/rbln/transformers/models/clip/modeling_clip.py +8 -34
- optimum/rbln/transformers/models/decoderonly/__init__.py +0 -5
- optimum/rbln/transformers/models/decoderonly/decoderonly_architecture.py +779 -361
- optimum/rbln/transformers/models/decoderonly/modeling_decoderonly.py +83 -142
- optimum/rbln/transformers/models/dpt/modeling_dpt.py +1 -1
- optimum/rbln/transformers/models/exaone/exaone_architecture.py +64 -39
- optimum/rbln/transformers/models/exaone/modeling_exaone.py +6 -29
- optimum/rbln/transformers/models/gemma/gemma_architecture.py +31 -92
- optimum/rbln/transformers/models/gemma/modeling_gemma.py +4 -28
- optimum/rbln/transformers/models/gpt2/gpt2_architecture.py +50 -238
- optimum/rbln/transformers/models/gpt2/modeling_gpt2.py +6 -31
- optimum/rbln/transformers/models/llama/modeling_llama.py +4 -28
- optimum/rbln/transformers/models/llava_next/modeling_llava_next.py +29 -83
- optimum/rbln/transformers/models/midm/midm_architecture.py +88 -253
- optimum/rbln/transformers/models/midm/modeling_midm.py +8 -33
- optimum/rbln/transformers/models/mistral/modeling_mistral.py +4 -29
- optimum/rbln/transformers/models/phi/modeling_phi.py +5 -31
- optimum/rbln/transformers/models/phi/phi_architecture.py +61 -345
- optimum/rbln/transformers/models/qwen2/modeling_qwen2.py +5 -29
- optimum/rbln/transformers/models/seq2seq/modeling_seq2seq.py +1 -46
- optimum/rbln/transformers/models/t5/__init__.py +1 -1
- optimum/rbln/transformers/models/t5/modeling_t5.py +157 -6
- optimum/rbln/transformers/models/wav2vec2/modeling_wav2vec2.py +1 -1
- optimum/rbln/transformers/models/whisper/modeling_whisper.py +2 -2
- optimum/rbln/transformers/models/xlm_roberta/modeling_xlm_roberta.py +3 -35
- optimum/rbln/transformers/utils/rbln_quantization.py +128 -5
- optimum/rbln/utils/decorator_utils.py +59 -0
- optimum/rbln/utils/hub.py +131 -0
- optimum/rbln/utils/import_utils.py +21 -0
- optimum/rbln/utils/model_utils.py +53 -0
- optimum/rbln/utils/runtime_utils.py +5 -5
- optimum/rbln/utils/submodule.py +114 -0
- optimum/rbln/utils/timer_utils.py +2 -2
- optimum_rbln-0.1.15.dist-info/METADATA +106 -0
- optimum_rbln-0.1.15.dist-info/RECORD +110 -0
- {optimum_rbln-0.1.12.dist-info → optimum_rbln-0.1.15.dist-info}/WHEEL +1 -1
- optimum/rbln/transformers/generation/streamers.py +0 -139
- optimum/rbln/transformers/generation/utils.py +0 -397
- optimum/rbln/transformers/models/exaone/hf_hub_cached/configuration_exaone.py +0 -181
- optimum/rbln/transformers/models/exaone/hf_hub_cached/modeling_exaone.py +0 -1725
- optimum/rbln/transformers/models/midm/hf_hub_cached/configuration_midm.py +0 -22
- optimum/rbln/transformers/models/midm/hf_hub_cached/midm_bitext_tokenization.py +0 -304
- optimum/rbln/transformers/models/midm/hf_hub_cached/modeling_midm.py +0 -1469
- optimum/rbln/transformers/models/midm/hf_hub_cached/rotary_position_embedding.py +0 -98
- optimum_rbln-0.1.12.dist-info/METADATA +0 -119
- optimum_rbln-0.1.12.dist-info/RECORD +0 -103
- optimum_rbln-0.1.12.dist-info/entry_points.txt +0 -4
- {optimum_rbln-0.1.12.dist-info → optimum_rbln-0.1.15.dist-info}/licenses/LICENSE +0 -0
@@ -26,209 +26,24 @@ from typing import Any, Callable, Dict, List, Optional, Tuple, Union
|
|
26
26
|
|
27
27
|
import torch
|
28
28
|
import torch.nn.functional as F
|
29
|
-
from diffusers import
|
29
|
+
from diffusers import StableDiffusionXLControlNetImg2ImgPipeline
|
30
30
|
from diffusers.image_processor import PipelineImageInput
|
31
|
-
from diffusers.models.unets.unet_2d_condition import UNet2DConditionModel
|
32
31
|
from diffusers.pipelines.stable_diffusion_xl.pipeline_output import StableDiffusionXLPipelineOutput
|
33
32
|
from diffusers.utils import deprecate, logging
|
34
33
|
from diffusers.utils.torch_utils import is_compiled_module
|
35
|
-
from transformers import CLIPTextModel
|
36
34
|
|
37
|
-
from ....
|
38
|
-
from ....
|
39
|
-
from
|
40
|
-
from ...models import RBLNAutoencoderKL, RBLNControlNetModel, RBLNUNet2DConditionModel
|
35
|
+
from ....modeling_diffusers import RBLNDiffusionMixin
|
36
|
+
from ....utils.decorator_utils import remove_compile_time_kwargs
|
37
|
+
from ...models import RBLNControlNetModel
|
41
38
|
from ...pipelines.controlnet.multicontrolnet import RBLNMultiControlNetModel
|
42
39
|
|
43
40
|
|
44
41
|
logger = logging.get_logger(__name__)
|
45
42
|
|
46
43
|
|
47
|
-
class RBLNStableDiffusionXLControlNetImg2ImgPipeline(StableDiffusionXLControlNetImg2ImgPipeline):
|
48
|
-
|
49
|
-
|
50
|
-
def from_pretrained(cls, model_id, **kwargs):
|
51
|
-
"""
|
52
|
-
Pipeline for image-to-image generation using Stable Diffusion XL with ControlNet.
|
53
|
-
|
54
|
-
This model inherits from [`StableDiffusionXLControlNetImg2ImgPipeline`]. Check the superclass documentation for the generic methods
|
55
|
-
implemented for all pipelines (downloading, saving, running on a particular device, etc.).
|
56
|
-
|
57
|
-
It implements the methods to convert a pre-trained Stable Diffusion XL Controlnet pipeline into a RBLNStableDiffusionXLControlNetImg2Img pipeline by:
|
58
|
-
- transferring the checkpoint weights of the original into an optimized RBLN graph,
|
59
|
-
- compiling the resulting graph using the RBLN compiler.
|
60
|
-
|
61
|
-
Args:
|
62
|
-
model_id (`Union[str, Path]`):
|
63
|
-
Can be either:
|
64
|
-
- A string, the *model id* of a pretrained model hosted inside a model repo on huggingface.co.
|
65
|
-
- A path to a *directory* containing a model saved using [`~OptimizedModel.save_pretrained`],
|
66
|
-
"""
|
67
|
-
export = kwargs.pop("export", None)
|
68
|
-
vae = kwargs.pop("vae", None)
|
69
|
-
unet = kwargs.pop("unet", None)
|
70
|
-
text_encoder = kwargs.pop("text_encoder", None)
|
71
|
-
text_encoder_2 = kwargs.pop("text_encoder_2", None)
|
72
|
-
controlnet = kwargs.pop("controlnet", None)
|
73
|
-
model_save_dir = kwargs.pop("model_save_dir", None)
|
74
|
-
|
75
|
-
rbln_config = kwargs.pop("rbln_config", None)
|
76
|
-
rbln_config = {} if rbln_config is None else rbln_config
|
77
|
-
|
78
|
-
device = rbln_config.get("device", None)
|
79
|
-
device_map = rbln_config.get("device_map", None)
|
80
|
-
create_runtimes = rbln_config.get("create_runtimes", None)
|
81
|
-
optimize_host_memory = rbln_config.get("optimize_host_memory", None)
|
82
|
-
|
83
|
-
kwargs_dict = {
|
84
|
-
"pretrained_model_name_or_path": model_id,
|
85
|
-
**kwargs,
|
86
|
-
}
|
87
|
-
|
88
|
-
kwargs_dict.update(
|
89
|
-
{
|
90
|
-
**({"vae": vae} if vae is not None and isinstance(vae, AutoencoderKL) else {}),
|
91
|
-
**({"unet": unet} if unet is not None and isinstance(unet, UNet2DConditionModel) else {}),
|
92
|
-
**(
|
93
|
-
{"text_encoder": text_encoder}
|
94
|
-
if text_encoder is not None and isinstance(text_encoder, CLIPTextModel)
|
95
|
-
else {}
|
96
|
-
),
|
97
|
-
**(
|
98
|
-
{"controlnet": controlnet}
|
99
|
-
if controlnet is not None
|
100
|
-
and (
|
101
|
-
isinstance(controlnet, ControlNetModel)
|
102
|
-
or all(isinstance(c, ControlNetModel) for c in controlnet)
|
103
|
-
)
|
104
|
-
else {}
|
105
|
-
),
|
106
|
-
}
|
107
|
-
)
|
108
|
-
|
109
|
-
with ContextRblnConfig(
|
110
|
-
device=device,
|
111
|
-
device_map=device_map,
|
112
|
-
create_runtimes=create_runtimes,
|
113
|
-
optimze_host_mem=optimize_host_memory,
|
114
|
-
):
|
115
|
-
model = super().from_pretrained(**{k: v for k, v in kwargs_dict.items() if v is not None})
|
116
|
-
|
117
|
-
if export is None or export is False:
|
118
|
-
return model
|
119
|
-
|
120
|
-
do_classifier_free_guidance = (
|
121
|
-
rbln_config.pop("guidance_scale", 5.0) > 1.0 and model.unet.config.time_cond_proj_dim is None
|
122
|
-
)
|
123
|
-
|
124
|
-
if not isinstance(vae, RBLNAutoencoderKL):
|
125
|
-
vae = RBLNAutoencoderKL.from_pretrained(
|
126
|
-
model_id=model_id,
|
127
|
-
subfolder="vae",
|
128
|
-
export=True,
|
129
|
-
model_save_dir=model_save_dir,
|
130
|
-
rbln_unet_sample_size=model.unet.config.sample_size,
|
131
|
-
rbln_use_encode=True,
|
132
|
-
rbln_vae_scale_factor=model.vae_scale_factor,
|
133
|
-
rbln_config={**rbln_config},
|
134
|
-
)
|
135
|
-
|
136
|
-
if not isinstance(text_encoder, RBLNCLIPTextModel):
|
137
|
-
text_encoder = RBLNCLIPTextModel.from_pretrained(
|
138
|
-
model_id=model_id,
|
139
|
-
subfolder="text_encoder",
|
140
|
-
export=True,
|
141
|
-
model_save_dir=model_save_dir,
|
142
|
-
rbln_config={**rbln_config},
|
143
|
-
)
|
144
|
-
|
145
|
-
if not isinstance(text_encoder_2, RBLNCLIPTextModel):
|
146
|
-
text_encoder_2 = RBLNCLIPTextModelWithProjection.from_pretrained(
|
147
|
-
model_id=model_id,
|
148
|
-
subfolder="text_encoder_2",
|
149
|
-
export=True,
|
150
|
-
model_save_dir=model_save_dir,
|
151
|
-
rbln_config={**rbln_config},
|
152
|
-
)
|
153
|
-
|
154
|
-
batch_size = rbln_config.pop("batch_size", 1)
|
155
|
-
unet_batch_size = batch_size * 2 if do_classifier_free_guidance else batch_size
|
156
|
-
|
157
|
-
if not isinstance(unet, RBLNUNet2DConditionModel):
|
158
|
-
unet = RBLNUNet2DConditionModel.from_pretrained(
|
159
|
-
model_id=model_id,
|
160
|
-
subfolder="unet",
|
161
|
-
export=True,
|
162
|
-
model_save_dir=model_save_dir,
|
163
|
-
rbln_max_seq_len=model.text_encoder.config.max_position_embeddings,
|
164
|
-
rbln_text_model_hidden_size=model.text_encoder_2.config.hidden_size,
|
165
|
-
rbln_batch_size=unet_batch_size,
|
166
|
-
rbln_use_encode=True,
|
167
|
-
rbln_vae_scale_factor=model.vae_scale_factor,
|
168
|
-
rbln_is_controlnet=True if "controlnet" in model.config.keys() else False,
|
169
|
-
rbln_config={**rbln_config},
|
170
|
-
)
|
171
|
-
|
172
|
-
if not isinstance(controlnet, (RBLNControlNetModel, RBLNMultiControlNetModel)):
|
173
|
-
if isinstance(controlnet, (list, tuple)):
|
174
|
-
multicontrolnet = []
|
175
|
-
for i, cid in enumerate(controlnet):
|
176
|
-
subfolder_name = "controlnet" if i == 0 else f"controlnet_{i}"
|
177
|
-
multicontrolnet.append(
|
178
|
-
RBLNControlNetModel.from_model(
|
179
|
-
model=cid,
|
180
|
-
subfolder=subfolder_name,
|
181
|
-
model_save_dir=model_save_dir,
|
182
|
-
rbln_batch_size=unet_batch_size,
|
183
|
-
rbln_text_model_hidden_size=model.text_encoder_2.config.hidden_size,
|
184
|
-
rbln_vae_scale_factor=model.vae_scale_factor,
|
185
|
-
rbln_config={**rbln_config},
|
186
|
-
)
|
187
|
-
)
|
188
|
-
controlnet = RBLNMultiControlNetModel(multicontrolnet, config=controlnet[0].config)
|
189
|
-
controlnet_dict = ("optimum.rbln", "RBLNMultiControlNetModel")
|
190
|
-
else:
|
191
|
-
controlnet = RBLNControlNetModel.from_model(
|
192
|
-
model=controlnet,
|
193
|
-
subfolder="controlnet",
|
194
|
-
model_save_dir=model_save_dir,
|
195
|
-
rbln_batch_size=unet_batch_size,
|
196
|
-
rbln_text_model_hidden_size=model.text_encoder_2.config.hidden_size,
|
197
|
-
rbln_vae_scale_factor=model.vae_scale_factor,
|
198
|
-
rbln_config={**rbln_config},
|
199
|
-
)
|
200
|
-
controlnet_dict = ("optimum.rbln", "RBLNControlNetModel")
|
201
|
-
|
202
|
-
if model_save_dir is not None:
|
203
|
-
# To skip saving original pytorch modules
|
204
|
-
del (model.vae, model.text_encoder, model.unet, model.controlnet)
|
205
|
-
|
206
|
-
# Direct calling of `save_pretrained` causes config.unet = (None, None).
|
207
|
-
# So config must be saved again, later.
|
208
|
-
model.save_pretrained(model_save_dir)
|
209
|
-
|
210
|
-
# replace modules
|
211
|
-
model.vae = vae
|
212
|
-
model.text_encoder = text_encoder
|
213
|
-
model.unet = unet
|
214
|
-
model.text_encoder_2 = text_encoder_2
|
215
|
-
model.controlnet = controlnet
|
216
|
-
|
217
|
-
# update config to be able to load from file
|
218
|
-
update_dict = {
|
219
|
-
"vae": ("optimum.rbln", "RBLNAutoencoderKL"),
|
220
|
-
"text_encoder": ("optimum.rbln", "RBLNCLIPTextModel"),
|
221
|
-
"unet": ("optimum.rbln", "RBLNUNet2DConditionModel"),
|
222
|
-
"text_encoder_2": ("optimum.rbln", "RBLNCLIPTextModelWithProjection"),
|
223
|
-
"controlnet": controlnet_dict,
|
224
|
-
}
|
225
|
-
model.register_to_config(**update_dict)
|
226
|
-
|
227
|
-
if model_save_dir is not None:
|
228
|
-
# overwrite to replace incorrect config
|
229
|
-
model.save_config(model_save_dir)
|
230
|
-
|
231
|
-
return model
|
44
|
+
class RBLNStableDiffusionXLControlNetImg2ImgPipeline(RBLNDiffusionMixin, StableDiffusionXLControlNetImg2ImgPipeline):
|
45
|
+
original_class = StableDiffusionXLControlNetImg2ImgPipeline
|
46
|
+
_submodules = ["text_encoder", "text_encoder_2", "unet", "vae", "controlnet"]
|
232
47
|
|
233
48
|
def check_inputs(
|
234
49
|
self,
|
@@ -432,6 +247,7 @@ class RBLNStableDiffusionXLControlNetImg2ImgPipeline(StableDiffusionXLControlNet
|
|
432
247
|
)
|
433
248
|
|
434
249
|
@torch.no_grad()
|
250
|
+
@remove_compile_time_kwargs
|
435
251
|
def __call__(
|
436
252
|
self,
|
437
253
|
prompt: Union[str, List[str]] = None,
|
@@ -718,6 +534,7 @@ class RBLNStableDiffusionXLControlNetImg2ImgPipeline(StableDiffusionXLControlNet
|
|
718
534
|
text_encoder_lora_scale = (
|
719
535
|
self.cross_attention_kwargs.get("scale", None) if self.cross_attention_kwargs is not None else None
|
720
536
|
)
|
537
|
+
|
721
538
|
(
|
722
539
|
prompt_embeds,
|
723
540
|
negative_prompt_embeds,
|
@@ -24,115 +24,9 @@
|
|
24
24
|
|
25
25
|
from diffusers import StableDiffusionPipeline
|
26
26
|
|
27
|
-
from ....
|
28
|
-
from ....transformers import RBLNCLIPTextModel
|
29
|
-
from ....utils.runtime_utils import ContextRblnConfig
|
30
|
-
from ...models import RBLNAutoencoderKL, RBLNUNet2DConditionModel
|
27
|
+
from ....modeling_diffusers import RBLNDiffusionMixin
|
31
28
|
|
32
29
|
|
33
|
-
class RBLNStableDiffusionPipeline(StableDiffusionPipeline):
|
34
|
-
|
35
|
-
|
36
|
-
def from_pretrained(cls, model_id, **kwargs):
|
37
|
-
"""
|
38
|
-
Pipeline for text-to-image generation using Stable Diffusion.
|
39
|
-
|
40
|
-
This model inherits from [`StableDiffusionPipeline`]. Check the superclass documentation for the generic methods
|
41
|
-
implemented for all pipelines (downloading, saving, running on a particular device, etc.).
|
42
|
-
|
43
|
-
It implements the methods to convert a pre-trained Stable Diffusion pipeline into a RBLNStableDiffusion pipeline by:
|
44
|
-
- transferring the checkpoint weights of the original into an optimized RBLN graph,
|
45
|
-
- compiling the resulting graph using the RBLN compiler.
|
46
|
-
|
47
|
-
Args:
|
48
|
-
model_id (`Union[str, Path]`):
|
49
|
-
Can be either:
|
50
|
-
- A string, the *model id* of a pretrained model hosted inside a model repo on huggingface.co.
|
51
|
-
- A path to a *directory* containing a model saved using [`~OptimizedModel.save_pretrained`],
|
52
|
-
"""
|
53
|
-
export = kwargs.pop("export", None)
|
54
|
-
model_save_dir = kwargs.pop("model_save_dir", None)
|
55
|
-
rbln_config = kwargs.pop("rbln_config", None)
|
56
|
-
rbln_config = {} if rbln_config is None else rbln_config
|
57
|
-
|
58
|
-
device = rbln_config.get("device", None)
|
59
|
-
device_map = rbln_config.get("device_map", None)
|
60
|
-
create_runtimes = rbln_config.get("create_runtimes", None)
|
61
|
-
optimize_host_memory = rbln_config.get("optimize_host_memory", None)
|
62
|
-
|
63
|
-
with ContextRblnConfig(
|
64
|
-
device=device,
|
65
|
-
device_map=device_map,
|
66
|
-
create_runtimes=create_runtimes,
|
67
|
-
optimze_host_mem=optimize_host_memory,
|
68
|
-
):
|
69
|
-
model = super().from_pretrained(pretrained_model_name_or_path=model_id, **kwargs)
|
70
|
-
|
71
|
-
if export is None or export is False:
|
72
|
-
return model
|
73
|
-
|
74
|
-
do_classifier_free_guidance = (
|
75
|
-
rbln_config.pop("guidance_scale", 5.0) > 1.0 and model.unet.config.time_cond_proj_dim is None
|
76
|
-
)
|
77
|
-
|
78
|
-
vae = RBLNAutoencoderKL.from_pretrained(
|
79
|
-
model_id=model_id,
|
80
|
-
subfolder="vae",
|
81
|
-
export=True,
|
82
|
-
model_save_dir=model_save_dir,
|
83
|
-
rbln_unet_sample_size=model.unet.config.sample_size,
|
84
|
-
rbln_use_encode=False,
|
85
|
-
rbln_config={**rbln_config},
|
86
|
-
)
|
87
|
-
text_encoder = RBLNCLIPTextModel.from_pretrained(
|
88
|
-
model_id=model_id,
|
89
|
-
subfolder="text_encoder",
|
90
|
-
export=True,
|
91
|
-
model_save_dir=model_save_dir,
|
92
|
-
rbln_config={**rbln_config},
|
93
|
-
)
|
94
|
-
|
95
|
-
batch_size = rbln_config.pop("batch_size", 1)
|
96
|
-
unet_batch_size = batch_size * 2 if do_classifier_free_guidance else batch_size
|
97
|
-
|
98
|
-
unet = RBLNUNet2DConditionModel.from_pretrained(
|
99
|
-
model_id=model_id,
|
100
|
-
subfolder="unet",
|
101
|
-
export=True,
|
102
|
-
model_save_dir=model_save_dir,
|
103
|
-
rbln_max_seq_len=text_encoder.config.max_position_embeddings,
|
104
|
-
rbln_batch_size=unet_batch_size,
|
105
|
-
rbln_use_encode=False,
|
106
|
-
rbln_is_controlnet=True if "controlnet" in model.config.keys() else False,
|
107
|
-
rbln_config={**rbln_config},
|
108
|
-
)
|
109
|
-
|
110
|
-
if model_save_dir is not None:
|
111
|
-
# To skip saving original pytorch modules
|
112
|
-
del (model.vae, model.text_encoder, model.unet)
|
113
|
-
|
114
|
-
# Direct calling of `save_pretrained` causes config.unet = (None, None).
|
115
|
-
# So config must be saved again, later.
|
116
|
-
model.save_pretrained(model_save_dir)
|
117
|
-
|
118
|
-
# replace modules
|
119
|
-
model.vae = vae
|
120
|
-
model.text_encoder = text_encoder
|
121
|
-
model.unet = unet
|
122
|
-
|
123
|
-
# update config to be able to load from file.
|
124
|
-
update_dict = {
|
125
|
-
"vae": ("optimum.rbln", "RBLNAutoencoderKL"),
|
126
|
-
"text_encoder": ("optimum.rbln", "RBLNCLIPTextModel"),
|
127
|
-
"unet": ("optimum.rbln", "RBLNUNet2DConditionModel"),
|
128
|
-
}
|
129
|
-
model.register_to_config(**update_dict)
|
130
|
-
|
131
|
-
if model_save_dir is not None:
|
132
|
-
# overwrite to replace incorrect config
|
133
|
-
model.save_config(model_save_dir)
|
134
|
-
|
135
|
-
if optimize_host_memory is False:
|
136
|
-
model.compiled_models = [vae.compiled_models[0], text_encoder.compiled_models[0], unet.compiled_models[0]]
|
137
|
-
|
138
|
-
return model
|
30
|
+
class RBLNStableDiffusionPipeline(RBLNDiffusionMixin, StableDiffusionPipeline):
|
31
|
+
original_class = StableDiffusionPipeline
|
32
|
+
_submodules = ["text_encoder", "unet", "vae"]
|
@@ -24,123 +24,9 @@
|
|
24
24
|
|
25
25
|
from diffusers import StableDiffusionImg2ImgPipeline
|
26
26
|
|
27
|
-
from ....
|
28
|
-
from ....transformers import RBLNCLIPTextModel
|
29
|
-
from ....utils.runtime_utils import ContextRblnConfig
|
30
|
-
from ...models import RBLNAutoencoderKL, RBLNUNet2DConditionModel
|
27
|
+
from ....modeling_diffusers import RBLNDiffusionMixin
|
31
28
|
|
32
29
|
|
33
|
-
class RBLNStableDiffusionImg2ImgPipeline(StableDiffusionImg2ImgPipeline):
|
34
|
-
|
35
|
-
|
36
|
-
def from_pretrained(cls, model_id, **kwargs):
|
37
|
-
"""
|
38
|
-
Pipeline for image-to-image generation using Stable Diffusion.
|
39
|
-
|
40
|
-
This model inherits from [`StableDiffusionPipeline`]. Check the superclass documentation for the generic methods
|
41
|
-
implemented for all pipelines (downloading, saving, running on a particular device, etc.).
|
42
|
-
|
43
|
-
It implements the methods to convert a pre-trained Stable Diffusion pipeline into a RBLNStableDiffusion pipeline by:
|
44
|
-
- transferring the checkpoint weights of the original into an optimized RBLN graph,
|
45
|
-
- compiling the resulting graph using the RBLN compiler.
|
46
|
-
|
47
|
-
Args:
|
48
|
-
model_id (`Union[str, Path]`):
|
49
|
-
Can be either:
|
50
|
-
- A string, the *model id* of a pretrained model hosted inside a model repo on huggingface.co.
|
51
|
-
- A path to a *directory* containing a model saved using [`~OptimizedModel.save_pretrained`],
|
52
|
-
"""
|
53
|
-
export = kwargs.pop("export", None)
|
54
|
-
model_save_dir = kwargs.pop("model_save_dir", None)
|
55
|
-
rbln_config = kwargs.pop("rbln_config", None)
|
56
|
-
rbln_config = {} if rbln_config is None else rbln_config
|
57
|
-
|
58
|
-
device = rbln_config.get("device", None)
|
59
|
-
device_map = rbln_config.get("device_map", None)
|
60
|
-
create_runtimes = rbln_config.get("create_runtimes", None)
|
61
|
-
optimize_host_memory = rbln_config.get("optimize_host_memory", None)
|
62
|
-
|
63
|
-
with ContextRblnConfig(
|
64
|
-
device=device,
|
65
|
-
device_map=device_map,
|
66
|
-
create_runtimes=create_runtimes,
|
67
|
-
optimze_host_mem=optimize_host_memory,
|
68
|
-
):
|
69
|
-
model = super().from_pretrained(pretrained_model_name_or_path=model_id, **kwargs)
|
70
|
-
|
71
|
-
if export is None or export is False:
|
72
|
-
return model
|
73
|
-
|
74
|
-
do_classifier_free_guidance = (
|
75
|
-
rbln_config.pop("guidance_scale", 5.0) > 1.0 and model.unet.config.time_cond_proj_dim is None
|
76
|
-
)
|
77
|
-
|
78
|
-
# compile model, create runtime
|
79
|
-
vae = RBLNAutoencoderKL.from_pretrained(
|
80
|
-
model_id=model_id,
|
81
|
-
subfolder="vae",
|
82
|
-
export=True,
|
83
|
-
model_save_dir=model_save_dir,
|
84
|
-
rbln_unet_sample_size=model.unet.config.sample_size,
|
85
|
-
rbln_use_encode=True,
|
86
|
-
rbln_vae_scale_factor=model.vae_scale_factor,
|
87
|
-
rbln_config={**rbln_config},
|
88
|
-
)
|
89
|
-
text_encoder = RBLNCLIPTextModel.from_pretrained(
|
90
|
-
model_id=model_id,
|
91
|
-
subfolder="text_encoder",
|
92
|
-
export=True,
|
93
|
-
model_save_dir=model_save_dir,
|
94
|
-
rbln_config={**rbln_config},
|
95
|
-
)
|
96
|
-
|
97
|
-
batch_size = rbln_config.pop("batch_size", 1)
|
98
|
-
unet_batch_size = batch_size * 2 if do_classifier_free_guidance else batch_size
|
99
|
-
|
100
|
-
unet = RBLNUNet2DConditionModel.from_pretrained(
|
101
|
-
model_id=model_id,
|
102
|
-
subfolder="unet",
|
103
|
-
export=True,
|
104
|
-
model_save_dir=model_save_dir,
|
105
|
-
rbln_max_seq_len=text_encoder.config.max_position_embeddings,
|
106
|
-
rbln_batch_size=unet_batch_size,
|
107
|
-
rbln_use_encode=True,
|
108
|
-
rbln_vae_scale_factor=model.vae_scale_factor,
|
109
|
-
rbln_is_controlnet=True if "controlnet" in model.config.keys() else False,
|
110
|
-
rbln_config={**rbln_config},
|
111
|
-
)
|
112
|
-
|
113
|
-
if model_save_dir is not None:
|
114
|
-
# To skip saving original pytorch modules
|
115
|
-
del (model.vae, model.text_encoder, model.unet)
|
116
|
-
|
117
|
-
# Direct calling of `save_pretrained` causes config.unet = (None, None).
|
118
|
-
# So config must be saved again, later.
|
119
|
-
model.save_pretrained(model_save_dir)
|
120
|
-
|
121
|
-
# replace modules
|
122
|
-
model.vae = vae
|
123
|
-
model.text_encoder = text_encoder
|
124
|
-
model.unet = unet
|
125
|
-
|
126
|
-
# update config to be able to load from file.
|
127
|
-
update_dict = {
|
128
|
-
"vae": ("optimum.rbln", "RBLNAutoencoderKL"),
|
129
|
-
"text_encoder": ("optimum.rbln", "RBLNCLIPTextModel"),
|
130
|
-
"unet": ("optimum.rbln", "RBLNUNet2DConditionModel"),
|
131
|
-
}
|
132
|
-
model.register_to_config(**update_dict)
|
133
|
-
|
134
|
-
if model_save_dir is not None:
|
135
|
-
# overwrite to replace incorrect config
|
136
|
-
model.save_config(model_save_dir)
|
137
|
-
|
138
|
-
if optimize_host_memory is False:
|
139
|
-
model.compiled_models = [
|
140
|
-
vae.compiled_models[0],
|
141
|
-
vae.compiled_models[1],
|
142
|
-
text_encoder.compiled_models[0],
|
143
|
-
unet.compiled_models[0],
|
144
|
-
]
|
145
|
-
|
146
|
-
return model
|
30
|
+
class RBLNStableDiffusionImg2ImgPipeline(RBLNDiffusionMixin, StableDiffusionImg2ImgPipeline):
|
31
|
+
original_class = StableDiffusionImg2ImgPipeline
|
32
|
+
_submodules = ["text_encoder", "unet", "vae"]
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# Copyright 2024 Rebellions Inc.
|
2
|
+
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at:
|
6
|
+
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|
15
|
+
# Portions of this software are licensed under the Apache License,
|
16
|
+
# Version 2.0. See the NOTICE file distributed with this work for
|
17
|
+
# additional information regarding copyright ownership.
|
18
|
+
|
19
|
+
# All other portions of this software, including proprietary code,
|
20
|
+
# are the intellectual property of Rebellions Inc. and may not be
|
21
|
+
# copied, modified, or distributed without prior written permission
|
22
|
+
# from Rebellions Inc.
|
23
|
+
"""RBLNStableDiffusionInpaintPipeline class for inference of diffusion models on rbln devices."""
|
24
|
+
|
25
|
+
from diffusers import StableDiffusionInpaintPipeline
|
26
|
+
|
27
|
+
from ....modeling_diffusers import RBLNDiffusionMixin
|
28
|
+
|
29
|
+
|
30
|
+
class RBLNStableDiffusionInpaintPipeline(RBLNDiffusionMixin, StableDiffusionInpaintPipeline):
|
31
|
+
original_class = StableDiffusionInpaintPipeline
|
32
|
+
_submodules = ["text_encoder", "unet", "vae"]
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# Copyright 2024 Rebellions Inc.
|
2
|
+
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at:
|
6
|
+
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|
15
|
+
# Portions of this software are licensed under the Apache License,
|
16
|
+
# Version 2.0. See the NOTICE file distributed with this work for
|
17
|
+
# additional information regarding copyright ownership.
|
18
|
+
|
19
|
+
# All other portions of this software, including proprietary code,
|
20
|
+
# are the intellectual property of Rebellions Inc. and may not be
|
21
|
+
# copied, modified, or distributed without prior written permission
|
22
|
+
# from Rebellions Inc.
|
23
|
+
|
24
|
+
from .pipeline_stable_diffusion_3 import RBLNStableDiffusion3Pipeline
|
25
|
+
from .pipeline_stable_diffusion_3_img2img import RBLNStableDiffusion3Img2ImgPipeline
|
26
|
+
from .pipeline_stable_diffusion_3_inpaint import RBLNStableDiffusion3InpaintPipeline
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# Copyright 2024 Rebellions Inc.
|
2
|
+
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at:
|
6
|
+
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|
15
|
+
# Portions of this software are licensed under the Apache License,
|
16
|
+
# Version 2.0. See the NOTICE file distributed with this work for
|
17
|
+
# additional information regarding copyright ownership.
|
18
|
+
|
19
|
+
# All other portions of this software, including proprietary code,
|
20
|
+
# are the intellectual property of Rebellions Inc. and may not be
|
21
|
+
# copied, modified, or distributed without prior written permission
|
22
|
+
# from Rebellions Inc.
|
23
|
+
"""RBLNStableDiffusion3Pipeline class for inference of diffusion models on rbln devices."""
|
24
|
+
|
25
|
+
from diffusers import StableDiffusion3Pipeline
|
26
|
+
|
27
|
+
from ....modeling_diffusers import RBLNDiffusionMixin
|
28
|
+
|
29
|
+
|
30
|
+
class RBLNStableDiffusion3Pipeline(RBLNDiffusionMixin, StableDiffusion3Pipeline):
|
31
|
+
original_class = StableDiffusion3Pipeline
|
32
|
+
_submodules = ["transformer", "text_encoder_3", "text_encoder", "text_encoder_2", "vae"]
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# Copyright 2024 Rebellions Inc.
|
2
|
+
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at:
|
6
|
+
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|
15
|
+
# Portions of this software are licensed under the Apache License,
|
16
|
+
# Version 2.0. See the NOTICE file distributed with this work for
|
17
|
+
# additional information regarding copyright ownership.
|
18
|
+
|
19
|
+
# All other portions of this software, including proprietary code,
|
20
|
+
# are the intellectual property of Rebellions Inc. and may not be
|
21
|
+
# copied, modified, or distributed without prior written permission
|
22
|
+
# from Rebellions Inc.
|
23
|
+
"""RBLNStableDiffusion3Img2ImgPipeline class for inference of diffusion models on rbln devices."""
|
24
|
+
|
25
|
+
from diffusers import StableDiffusion3Img2ImgPipeline
|
26
|
+
|
27
|
+
from ....modeling_diffusers import RBLNDiffusionMixin
|
28
|
+
|
29
|
+
|
30
|
+
class RBLNStableDiffusion3Img2ImgPipeline(RBLNDiffusionMixin, StableDiffusion3Img2ImgPipeline):
|
31
|
+
original_class = StableDiffusion3Img2ImgPipeline
|
32
|
+
_submodules = ["transformer", "text_encoder_3", "text_encoder", "text_encoder_2", "vae"]
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# Copyright 2024 Rebellions Inc.
|
2
|
+
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at:
|
6
|
+
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|
15
|
+
# Portions of this software are licensed under the Apache License,
|
16
|
+
# Version 2.0. See the NOTICE file distributed with this work for
|
17
|
+
# additional information regarding copyright ownership.
|
18
|
+
|
19
|
+
# All other portions of this software, including proprietary code,
|
20
|
+
# are the intellectual property of Rebellions Inc. and may not be
|
21
|
+
# copied, modified, or distributed without prior written permission
|
22
|
+
# from Rebellions Inc.
|
23
|
+
"""RBLNStableDiffusion3InpaintPipeline class for inference of diffusion models on rbln devices."""
|
24
|
+
|
25
|
+
from diffusers import StableDiffusion3InpaintPipeline
|
26
|
+
|
27
|
+
from ....modeling_diffusers import RBLNDiffusionMixin
|
28
|
+
|
29
|
+
|
30
|
+
class RBLNStableDiffusion3InpaintPipeline(RBLNDiffusionMixin, StableDiffusion3InpaintPipeline):
|
31
|
+
original_class = StableDiffusion3InpaintPipeline
|
32
|
+
_submodules = ["transformer", "text_encoder_3", "text_encoder", "text_encoder_2", "vae"]
|