Skip to content

[OpenVINO] Support Gemma 4#1688

Merged
regisss merged 235 commits intohuggingface:mainfrom
rkazants:support_gemma_4
May 1, 2026
Merged

[OpenVINO] Support Gemma 4#1688
regisss merged 235 commits intohuggingface:mainfrom
rkazants:support_gemma_4

Conversation

@rkazants
Copy link
Copy Markdown
Collaborator

@rkazants rkazants commented Apr 15, 2026

What does this PR do?

Fixes 182357

Re-created from PR #1675

Installation instructions:

pip install git+https://github.com/rkazants/optimum-intel.git@support_gemma_4
pip install --pre -U openvino openvino-tokenizers nncf --extra-index-url https://storage.openvinotoolkit.org/simple/wheels/nightly
pip install transformers==5.5.0
pip install requests torchvision requests

Exporting cmd-line:

optimum-cli export openvino -m google/gemma-4-E2B-it ov_gemma4_E2Bit --task=image-text-to-text

Inference script:

from transformers import AutoProcessor
import torch
from transformers import AutoProcessor, Gemma4ForConditionalGeneration
from optimum.intel.openvino import OVModelForVisualCausalLM

model_id = "google/gemma-4-E2B-it"
model = OVModelForVisualCausalLM.from_pretrained(model_id)

processor = AutoProcessor.from_pretrained(model_id, padding_side="left")

url = "https://media.istockphoto.com/id/1192867753/photo/cow-in-berchida-beach-siniscola.jpg?s=612x612&w=0&k=20&c=v0hjjniwsMNfJSuKWZuIn8pssmD5h5bSN1peBd1CmH4="
messages = [
    {
        "role": "system",
        "content": [
            {"type": "text", "text": "You are a helpful assistant."}
        ]
    },
    {
        "role": "user", "content": [
            {"type": "image", "url": url},
            {"type": "text", "text": "What is shown in this image?"},
        ]
    },
]
inputs = processor.apply_chat_template(
    messages,
    tokenize=True,
    return_dict=True,
    return_tensors="pt",
    add_generation_prompt=True,
)

output = model.generate(**inputs, max_new_tokens=50)
print(processor.decode(output[0, inputs.input_ids.shape[1]: ], skip_special_tokens=True))

Before submitting

  • [N/A] This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you make sure to update the documentation with your changes?
  • Did you write any new necessary tests?

Comment thread .github/workflows/test_openvino_preview_models.yml Outdated
Comment thread .github/workflows/test_openvino_preview_models.yml Outdated
popovaan and others added 4 commits April 29, 2026 15:38
Co-authored-by: Roman Kazantsev <roman.kazantsev@intel.com>
Co-authored-by: Roman Kazantsev <roman.kazantsev@intel.com>
Copy link
Copy Markdown
Collaborator

@echarlaix echarlaix left a comment

Choose a reason for hiding this comment

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

Thanks for the addition!

Comment thread optimum/intel/openvino/modeling_visual_language.py Outdated
dummy_inputs["token_type_ids"] = self.orig_export_config.DUMMY_INPUT_GENERATOR_CLASSES[
0
].random_int_tensor(token_type_ids_shape, min_value=0, max_value=2)
if "per_layer_inputs" in self.inputs:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

when will this be the case ?

Copy link
Copy Markdown
Collaborator

@popovaan popovaan Apr 30, 2026

Choose a reason for hiding this comment

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

It's the additional input of the language model for Gemma4. In original Gemma4 they are calculated inside of language model forward, but to calculate them we need input_ids, which we don't pass to language model, so we decided to move them to the separate model for the minimal API impact.

Comment thread optimum/exporters/openvino/model_configs.py Outdated
Comment thread optimum/exporters/openvino/model_patcher.py Outdated
@echarlaix echarlaix requested a review from regisss April 29, 2026 19:21
popovaan and others added 2 commits April 30, 2026 10:33
Co-authored-by: Ella Charlaix <80481427+echarlaix@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

@regisss regisss left a comment

Choose a reason for hiding this comment

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

Adding a few comments to @echarlaix's

Comment thread pyproject.toml
Comment thread tests/openvino/test_quantization.py
Comment thread .github/workflows/test_openvino_preview_models.yml
@popovaan popovaan requested review from echarlaix and regisss April 30, 2026 11:44
Comment thread optimum/intel/openvino/modeling_decoder.py
Comment thread tests/openvino/test_seq2seq.py Outdated
Comment thread optimum/exporters/openvino/model_configs.py Outdated
sbalandi pushed a commit to sbalandi/openvino.genai that referenced this pull request Apr 30, 2026
<!-- Keep your pull requests (PRs) as atomic as possible. That increases
the likelihood that an individual PR won't be stuck because of adjacent
problems, merge conflicts, or code review.
Your merged PR is going to appear in the automatically generated release
notes on GitHub. So the clearer the title the better. -->
## Description
<!-- Please include a summary of the change. Also include relevant
motivation and context. -->

Depends on: huggingface/optimum-intel#1688
optimum-intel PR depends on transformers v5 (**update**: transformers v5
support merged to optimum-intel).

### WWB Accuracy:

genai vs optimum-intel: 0.9682357
genai vs transformers: 0.94821364
optimum-intel vs transformers: 0.9387633

Fixes: openvinotoolkit#3653

Current implementation support image text inputs only. Ticket for video
support implementation: 185850

## Checklist:
- [x] This PR follows [GenAI Contributing
guidelines](https://github.com/openvinotoolkit/openvino.genai?tab=contributing-ov-file#contributing).
<!-- Always follow them. If there are deviations, explain what and why.
-->
- [x] Tests have been updated or added to cover the new code. <!--
Specify exactly which tests were added or updated. If the change isn't
maintenance related, update the tests at
https://github.com/openvinotoolkit/openvino.genai/tree/master/tests or
explain in the description why the tests don't need an update. -->
- [x] This PR fully addresses the ticket. <!--- If not, explain clearly
what is covered and what is not. If follow-up pull requests are needed,
specify in the description. -->
- [x] I have made corresponding changes to the documentation. <!-- Run
github.com/\<username>/openvino.genai/actions/workflows/deploy_gh_pages.yml
on your fork with your branch as a parameter to deploy a test version
with the updated content. Replace this comment with the link to the
built docs. If the documentation is updated in a separate PR, clearly
specify it. -->
@regisss regisss merged commit 3912188 into huggingface:main May 1, 2026
55 of 74 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants