Skip to content

Commit dc51ee2

Browse files
committed
enable t5 in SD3 pipe testing and flux img2img and inpaint
1 parent 595246d commit dc51ee2

File tree

5 files changed

+123
-41
lines changed

5 files changed

+123
-41
lines changed

optimum/intel/__init__.py

+4
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@
106106
"OVLatentConsistencyModelPipeline",
107107
"OVLatentConsistencyModelImg2ImgPipeline",
108108
"OVFluxPipeline",
109+
"OVFluxImg2ImgPipeline",
110+
"OVFluxInpaintPipeline",
109111
"OVPipelineForImage2Image",
110112
"OVPipelineForText2Image",
111113
"OVPipelineForInpainting",
@@ -126,6 +128,8 @@
126128
"OVLatentConsistencyModelPipeline",
127129
"OVLatentConsistencyModelImg2ImgPipeline",
128130
"OVFluxPipeline",
131+
"OVFluxImg2ImgPipeline",
132+
"OVFluxInpaintPipeline",
129133
"OVPipelineForImage2Image",
130134
"OVPipelineForText2Image",
131135
"OVPipelineForInpainting",

optimum/intel/openvino/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@
8282
if is_diffusers_available():
8383
from .modeling_diffusion import (
8484
OVDiffusionPipeline,
85+
OVFluxImg2ImgPipeline,
86+
OVFluxInpaintPipeline,
8587
OVFluxPipeline,
8688
OVLatentConsistencyModelImg2ImgPipeline,
8789
OVLatentConsistencyModelPipeline,

optimum/intel/openvino/modeling_diffusion.py

+23-3
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,13 @@
9595
FluxPipeline = StableDiffusionPipeline
9696

9797

98+
if is_diffusers_version(">=", "0.31.0"):
99+
from diffusers import FluxImg2ImgPipeline, FluxInpaintPipeline
100+
else:
101+
FluxImg2ImgPipeline = StableDiffusionImg2ImgPipeline
102+
FluxInpaintPipeline = StableDiffusionInpaintPipeline
103+
104+
98105
DIFFUSION_MODEL_TRANSFORMER_SUBFOLDER = "transformer"
99106
DIFFUSION_MODEL_TEXT_ENCODER_3_SUBFOLDER = "text_encoder_3"
100107

@@ -887,9 +894,6 @@ def compile(self):
887894
def _load_config(cls, config_name_or_path: Union[str, os.PathLike], **kwargs):
888895
return cls.load_config(config_name_or_path, **kwargs)
889896

890-
def _save_config(self, save_directory):
891-
self.save_config(save_directory)
892-
893897
@property
894898
def components(self) -> Dict[str, Any]:
895899
components = {
@@ -1447,6 +1451,18 @@ class OVFluxPipeline(OVDiffusionPipeline, OVTextualInversionLoaderMixin, FluxPip
14471451
auto_model_class = FluxPipeline
14481452

14491453

1454+
class OVFluxImg2ImgPipeline(OVDiffusionPipeline, OVTextualInversionLoaderMixin, FluxImg2ImgPipeline):
1455+
main_input_name = "prompt"
1456+
export_feature = "image-to-image"
1457+
auto_model_class = FluxImg2ImgPipeline
1458+
1459+
1460+
class OVFluxInpaintPipeline(OVDiffusionPipeline, OVTextualInversionLoaderMixin, FluxInpaintPipeline):
1461+
main_input_name = "prompt"
1462+
export_feature = "inpainting"
1463+
auto_model_class = FluxInpaintPipeline
1464+
1465+
14501466
SUPPORTED_OV_PIPELINES = [
14511467
OVStableDiffusionPipeline,
14521468
OVStableDiffusionImg2ImgPipeline,
@@ -1510,6 +1526,10 @@ def _get_ov_class(pipeline_class_name: str, throw_error_if_not_exist: bool = Tru
15101526
OV_INPAINT_PIPELINES_MAPPING["stable-diffusion-3"] = OVStableDiffusion3InpaintPipeline
15111527
OV_TEXT2IMAGE_PIPELINES_MAPPING["flux"] = OVFluxPipeline
15121528

1529+
if is_diffusers_version(">=", "0.31.0"):
1530+
SUPPORTED_OV_PIPELINES.extend([OVFluxImg2ImgPipeline, OVFluxInpaintPipeline])
1531+
OV_INPAINT_PIPELINES_MAPPING["flux"] = OVFluxInpaintPipeline
1532+
OV_IMAGE2IMAGE_PIPELINES_MAPPING["flux"] = OVFluxImg2ImgPipeline
15131533

15141534
SUPPORTED_OV_PIPELINES_MAPPINGS = [
15151535
OV_TEXT2IMAGE_PIPELINES_MAPPING,

optimum/intel/utils/dummy_openvino_and_diffusers_objects.py

+22
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,25 @@ def __init__(self, *args, **kwargs):
189189
@classmethod
190190
def from_pretrained(cls, *args, **kwargs):
191191
requires_backends(cls, ["openvino", "diffusers"])
192+
193+
194+
class OVFluxImg2ImgPipeline(metaclass=DummyObject):
195+
_backends = ["openvino", "diffusers"]
196+
197+
def __init__(self, *args, **kwargs):
198+
requires_backends(self, ["openvino", "diffusers"])
199+
200+
@classmethod
201+
def from_pretrained(cls, *args, **kwargs):
202+
requires_backends(cls, ["openvino", "diffusers"])
203+
204+
205+
class OVFluxInpaintPipeline(metaclass=DummyObject):
206+
_backends = ["openvino", "diffusers"]
207+
208+
def __init__(self, *args, **kwargs):
209+
requires_backends(self, ["openvino", "diffusers"])
210+
211+
@classmethod
212+
def from_pretrained(cls, *args, **kwargs):
213+
requires_backends(cls, ["openvino", "diffusers"])

tests/openvino/test_diffusion.py

+72-38
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import json
1516
import unittest
1617
from pathlib import Path
1718

@@ -134,8 +135,8 @@ def test_compare_to_diffusers_pipeline(self, model_arch: str):
134135
height, width, batch_size = 128, 128, 1
135136
inputs = self.generate_inputs(height=height, width=width, batch_size=batch_size)
136137

137-
ov_pipeline = self.OVMODEL_CLASS.from_pretrained(MODEL_NAMES[model_arch], text_encoder_3=None)
138-
diffusers_pipeline = self.AUTOMODEL_CLASS.from_pretrained(MODEL_NAMES[model_arch], text_encoder_3=None)
138+
ov_pipeline = self.OVMODEL_CLASS.from_pretrained(MODEL_NAMES[model_arch])
139+
diffusers_pipeline = self.AUTOMODEL_CLASS.from_pretrained(MODEL_NAMES[model_arch])
139140

140141
for output_type in ["latent", "np", "pt"]:
141142
inputs["output_type"] = output_type
@@ -330,6 +331,15 @@ def test_load_and_save_pipeline_with_safety_checker(self):
330331
]:
331332
subdir_path = Path(tmpdirname) / subdir
332333
self.assertTrue(subdir_path.is_dir())
334+
# check that config contains original model classes
335+
pipeline_config = Path(tmpdirname) / "model_index.json"
336+
self.assertTrue(pipeline_config.exists())
337+
with pipeline_config.open("r") as f:
338+
config = json.load(f)
339+
for key in ["unet", "vae", "text_encoder"]:
340+
model_lib, model_class = config[key]
341+
self.assertTrue(model_lib in ["diffusers", "transformers"])
342+
self.assertFalse(model_class.startswith("OV"))
333343
loaded_pipeline = self.OVMODEL_CLASS.from_pretrained(tmpdirname)
334344
self.assertTrue(loaded_pipeline.safety_checker is not None)
335345
self.assertIsInstance(loaded_pipeline.safety_checker, StableDiffusionSafetyChecker)
@@ -398,6 +408,7 @@ class OVPipelineForImage2ImageTest(unittest.TestCase):
398408
SUPPORTED_ARCHITECTURES = ["stable-diffusion", "stable-diffusion-xl", "latent-consistency"]
399409
if is_transformers_version(">=", "4.40.0"):
400410
SUPPORTED_ARCHITECTURES.append("stable-diffusion-3")
411+
SUPPORTED_ARCHITECTURES.append("flux")
401412

402413
AUTOMODEL_CLASS = AutoPipelineForImage2Image
403414
OVMODEL_CLASS = OVPipelineForImage2Image
@@ -410,6 +421,8 @@ def generate_inputs(self, height=128, width=128, batch_size=1, channel=3, input_
410421
inputs["image"] = _generate_images(
411422
height=height, width=width, batch_size=batch_size, channel=channel, input_type=input_type
412423
)
424+
inputs["height"] = height
425+
inputs["width"] = width
413426

414427
inputs["strength"] = 0.75
415428

@@ -490,29 +503,35 @@ def test_shape(self, model_arch: str):
490503
elif output_type == "pt":
491504
self.assertEqual(outputs.shape, (batch_size, 3, height, width))
492505
else:
493-
out_channels = (
494-
pipeline.unet.config.out_channels
495-
if pipeline.unet is not None
496-
else pipeline.transformer.config.out_channels
497-
)
498-
self.assertEqual(
499-
outputs.shape,
500-
(
501-
batch_size,
502-
out_channels,
503-
height // pipeline.vae_scale_factor,
504-
width // pipeline.vae_scale_factor,
505-
),
506-
)
506+
if model_arch != "flux":
507+
out_channels = (
508+
pipeline.unet.config.out_channels
509+
if pipeline.unet is not None
510+
else pipeline.transformer.config.out_channels
511+
)
512+
self.assertEqual(
513+
outputs.shape,
514+
(
515+
batch_size,
516+
out_channels,
517+
height // pipeline.vae_scale_factor,
518+
width // pipeline.vae_scale_factor,
519+
),
520+
)
521+
else:
522+
packed_height = height // pipeline.vae_scale_factor
523+
packed_width = width // pipeline.vae_scale_factor
524+
channels = pipeline.transformer.config.in_channels
525+
self.assertEqual(outputs.shape, (batch_size, packed_height * packed_width, channels))
507526

508527
@parameterized.expand(SUPPORTED_ARCHITECTURES)
509528
@require_diffusers
510529
def test_compare_to_diffusers_pipeline(self, model_arch: str):
511530
height, width, batch_size = 128, 128, 1
512531
inputs = self.generate_inputs(height=height, width=width, batch_size=batch_size)
513532

514-
diffusers_pipeline = self.AUTOMODEL_CLASS.from_pretrained(MODEL_NAMES[model_arch], text_encoder_3=None)
515-
ov_pipeline = self.OVMODEL_CLASS.from_pretrained(MODEL_NAMES[model_arch], text_encoder_3=None)
533+
diffusers_pipeline = self.AUTOMODEL_CLASS.from_pretrained(MODEL_NAMES[model_arch])
534+
ov_pipeline = self.OVMODEL_CLASS.from_pretrained(MODEL_NAMES[model_arch])
516535

517536
for output_type in ["latent", "np", "pt"]:
518537
print(output_type)
@@ -586,9 +605,13 @@ def test_height_width_properties(self, model_arch: str):
586605

587606
self.assertFalse(ov_pipeline.is_dynamic)
588607
expected_batch = batch_size * num_images_per_prompt
589-
if ov_pipeline.unet is None or "timestep_cond" not in {
590-
inputs.get_any_name() for inputs in ov_pipeline.unet.model.inputs
591-
}:
608+
if (
609+
ov_pipeline.unet is not None
610+
and "timestep_cond" not in {inputs.get_any_name() for inputs in ov_pipeline.unet.model.inputs}
611+
) or (
612+
ov_pipeline.transformer is not None
613+
and "txt_ids" not in {inputs.get_any_name() for inputs in ov_pipeline.transformer.model.inputs}
614+
):
592615
expected_batch *= 2
593616
self.assertEqual(ov_pipeline.batch_size, expected_batch)
594617
self.assertEqual(ov_pipeline.height, height)
@@ -624,6 +647,7 @@ class OVPipelineForInpaintingTest(unittest.TestCase):
624647

625648
if is_transformers_version(">=", "4.40.0"):
626649
SUPPORTED_ARCHITECTURES.append("stable-diffusion-3")
650+
SUPPORTED_ARCHITECTURES.append("flux")
627651

628652
AUTOMODEL_CLASS = AutoPipelineForInpainting
629653
OVMODEL_CLASS = OVPipelineForInpainting
@@ -721,20 +745,26 @@ def test_shape(self, model_arch: str):
721745
elif output_type == "pt":
722746
self.assertEqual(outputs.shape, (batch_size, 3, height, width))
723747
else:
724-
out_channels = (
725-
pipeline.unet.config.out_channels
726-
if pipeline.unet is not None
727-
else pipeline.transformer.config.out_channels
728-
)
729-
self.assertEqual(
730-
outputs.shape,
731-
(
732-
batch_size,
733-
out_channels,
734-
height // pipeline.vae_scale_factor,
735-
width // pipeline.vae_scale_factor,
736-
),
737-
)
748+
if model_arch != "flux":
749+
out_channels = (
750+
pipeline.unet.config.out_channels
751+
if pipeline.unet is not None
752+
else pipeline.transformer.config.out_channels
753+
)
754+
self.assertEqual(
755+
outputs.shape,
756+
(
757+
batch_size,
758+
out_channels,
759+
height // pipeline.vae_scale_factor,
760+
width // pipeline.vae_scale_factor,
761+
),
762+
)
763+
else:
764+
packed_height = height // pipeline.vae_scale_factor
765+
packed_width = width // pipeline.vae_scale_factor
766+
channels = pipeline.transformer.config.in_channels
767+
self.assertEqual(outputs.shape, (batch_size, packed_height * packed_width, channels))
738768

739769
@parameterized.expand(SUPPORTED_ARCHITECTURES)
740770
@require_diffusers
@@ -816,9 +846,13 @@ def test_height_width_properties(self, model_arch: str):
816846

817847
self.assertFalse(ov_pipeline.is_dynamic)
818848
expected_batch = batch_size * num_images_per_prompt
819-
if ov_pipeline.unet is None or "timestep_cond" not in {
820-
inputs.get_any_name() for inputs in ov_pipeline.unet.model.inputs
821-
}:
849+
if (
850+
ov_pipeline.unet is not None
851+
and "timestep_cond" not in {inputs.get_any_name() for inputs in ov_pipeline.unet.model.inputs}
852+
) or (
853+
ov_pipeline.transformer is not None
854+
and "txt_ids" not in {inputs.get_any_name() for inputs in ov_pipeline.transformer.model.inputs}
855+
):
822856
expected_batch *= 2
823857
self.assertEqual(
824858
ov_pipeline.batch_size,

0 commit comments

Comments
 (0)