Skip to content

[OpenVINO]Add Kontext support #1374

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
2 changes: 2 additions & 0 deletions optimum/intel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
"OVFluxImg2ImgPipeline",
"OVFluxInpaintPipeline",
"OVFluxFillPipeline",
"OVFluxKontextPipeline",
"OVSanaPipeline",
"OVPipelineForImage2Image",
"OVPipelineForText2Image",
Expand All @@ -150,6 +151,7 @@
"OVFluxImg2ImgPipeline",
"OVFluxInpaintPipeline",
"OVFluxFillPipeline",
"OVFluxKontextPipeline",
"OVSanaPipeline",
"OVPipelineForImage2Image",
"OVPipelineForText2Image",
Expand Down
1 change: 1 addition & 0 deletions optimum/intel/openvino/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
OVFluxFillPipeline,
OVFluxImg2ImgPipeline,
OVFluxInpaintPipeline,
OVFluxKontextPipeline,
OVFluxPipeline,
OVLatentConsistencyModelImg2ImgPipeline,
OVLatentConsistencyModelPipeline,
Expand Down
16 changes: 16 additions & 0 deletions optimum/intel/openvino/modeling_diffusion.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@
else:
SanaSprintPipeline = object

if is_diffusers_version(">", "0.34.0"):
from diffusers import FluxKontextPipeline
else:
FluxKontextPipeline = object


DIFFUSION_MODEL_TRANSFORMER_SUBFOLDER = "transformer"
DIFFUSION_MODEL_TEXT_ENCODER_3_SUBFOLDER = "text_encoder_3"
Expand Down Expand Up @@ -1666,6 +1671,12 @@ class OVFluxFillPipeline(OVDiffusionPipeline, OVTextualInversionLoaderMixin, Flu
auto_model_class = FluxFillPipeline


class OVFluxKontextPipeline(OVDiffusionPipeline, OVTextualInversionLoaderMixin, FluxKontextPipeline):
main_input_name = "image"
export_feature = "image-to-image"
auto_model_class = FluxKontextPipeline


class OVSanaPipeline(OVDiffusionPipeline, OVTextualInversionLoaderMixin, SanaPipeline):
main_input_name = "prompt"
export_feature = "text-to-image"
Expand Down Expand Up @@ -1769,6 +1780,11 @@ def _get_ov_class(pipeline_class_name: str, throw_error_if_not_exist: bool = Tru
SUPPORTED_OV_PIPELINES.append(OVSanaSprintPipeline)
OV_TEXT2IMAGE_PIPELINES_MAPPING["sana-sprint"] = OVSanaSprintPipeline


if is_diffusers_version(">", "0.34.0"):
SUPPORTED_OV_PIPELINES.extend([OVFluxKontextPipeline])
OV_IMAGE2IMAGE_PIPELINES_MAPPING["flux-kontext"] = OVFluxKontextPipeline

SUPPORTED_OV_PIPELINES_MAPPINGS = [
OV_TEXT2IMAGE_PIPELINES_MAPPING,
OV_IMAGE2IMAGE_PIPELINES_MAPPING,
Expand Down
11 changes: 11 additions & 0 deletions optimum/intel/utils/dummy_openvino_and_diffusers_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,17 @@ def from_pretrained(cls, *args, **kwargs):
requires_backends(cls, ["openvino", "diffusers"])


class OVFluxKontextPipeline(metaclass=DummyObject):
_backends = ["openvino", "diffusers"]

def __init__(self, *args, **kwargs):
requires_backends(self, ["openvino", "diffusers"])

@classmethod
def from_pretrained(cls, *args, **kwargs):
requires_backends(cls, ["openvino", "diffusers"])


class OVSanaPipeline(metaclass=DummyObject):
_backends = ["openvino", "diffusers"]

Expand Down
1 change: 1 addition & 0 deletions tests/openvino/utils_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
"flaubert": "hf-internal-testing/tiny-random-flaubert",
"flux": "katuni4ka/tiny-random-flux",
"flux-fill": "katuni4ka/tiny-random-flux-fill",
"flux-kontext": "snake7gun/flux-kontext-random",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe adding this here is not enough for the test for this model to be run. Could you please add changes to test_diffusion.py, test_exporters_cli.py and test_quantization.py similar to how it was done in this PR for example #1106?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @IlyasMoutawwakil my patch depends on the fix for diffusers lib, could you help to check ?
huggingface/diffusers#11875

Copy link
Member

@IlyasMoutawwakil IlyasMoutawwakil Jul 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you don't need to wait for diffusers to do an entire release to test this. You can do the same as sana by using DiffusionPipeline instead of AutoPipelineForxxx https://github.com/huggingface/optimum-intel/blob/main/tests/openvino/test_diffusion.py#L146 for now

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you don't need to wait for diffusers to do an entire release to test this. You can do the same as sana by using DiffusionPipeline instead of AutoPipelineForxxx https://github.com/huggingface/optimum-intel/blob/main/tests/openvino/test_diffusion.py#L146 for now

yes, but how about in CLI export test case ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

@openvino-dev-samples openvino-dev-samples Jul 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cli export test should pass as it uses the correct class name https://github.com/huggingface/optimum/blob/ec756fdd00543d3a93458fafbcdf5abbb17b61ba/optimum/exporters/tasks.py#L2159 🙂

  1. CLI export should not work without my patch on diffusers
  2. another question is if there is a method to update the version of diffusers in pytest?
    E ImportError: cannot import name 'FluxKontextPipeline' from 'diffusers'

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. no it should work without it. your patch is only rleevant to the AutoPipelineForxxx class, not the DiffusionPipeline class.
  2. update it to what version ? I just checked the latest diffusers and this model is not even released in diffusers yet https://github.com/huggingface/diffusers/blob/v0.34.0/src/diffusers/pipelines/flux/pipeline_flux_kontext.py
    what's exactly the motive for adding this model anyway ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So your suggestion is adding the model after 0.34.0 version of diffuser released, am i right ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or install diffusers from source.. for testing sake.
but bear in mind that python packages can't be released with a "git+https...." dependency, so we will have to wait for a diffusers release if you want this in the next optimum-intel release.

"gpt_bigcode": "hf-internal-testing/tiny-random-GPTBigCodeModel",
"gpt2": "hf-internal-testing/tiny-random-gpt2",
"gpt_neo": "hf-internal-testing/tiny-random-GPTNeoModel",
Expand Down