Skip to content
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

add gemma3 support #1198

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open

add gemma3 support #1198

wants to merge 5 commits into from

Conversation

eaidova
Copy link
Collaborator

@eaidova eaidova commented Mar 13, 2025

What does this PR do?

Fixes # #1197

Text-Only

from transformers import AutoTokenizer
from optimum.intel.openvino import OVModelForCauslaLM

model_id = "google/gemma-3-1b-it"

tokenizer = AutoTokenizer.from_pretrained(model_id)
model = OVModelForCauslaLM.from_pretrained(model_id, device_map="auto")

input_ids = tokenizer("Write me a poem about Machine Learning.", return_tensors="pt")

outputs = model.generate(**input_ids, max_new_tokens=100)
text = tokenizer.batch_decode(outputs, skip_special_tokens=True)

print(text)

Image-Text

from transformers import AutoProcessor
from optimum.intel.openvino import OVModelForVisualCuasalLM

model_id = "google/gemma-3-4b-it"
model = OVModelForVisualCuasalLM.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

  • 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?

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@eaidova eaidova added the openvino-test Trigger OpenVINO slow tests label Mar 13, 2025
@eaidova eaidova force-pushed the ea/gemma3 branch 2 times, most recently from 21e720f to a27b6a9 Compare March 13, 2025 18:40
Co-authored-by: Alexander Kozlov <alexander.kozlov@intel.com>
@eaidova eaidova requested a review from AlexKoff88 March 14, 2025 07:54
library_name="transformers",
)
class Gemma3TextOpenVINOConfig(Gemma2OpenVINOConfig):
MIN_TRANSFORMERS_VERSION = version.parse("4.49.0")
Copy link
Member

Choose a reason for hiding this comment

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

shouldn't this be 4.50 since the model is only available on 4.49.0-Gemma-3 and later

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
openvino-test Trigger OpenVINO slow tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants