optimum-intel 2.0.0__tar.gz → 2.2.0__tar.gz
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_intel-2.0.0/optimum_intel.egg-info → optimum_intel-2.2.0}/PKG-INFO +8 -10
- {optimum_intel-2.0.0 → optimum_intel-2.2.0}/README.md +2 -3
- {optimum_intel-2.0.0 → optimum_intel-2.2.0}/optimum/commands/export/openvino.py +2 -1
- {optimum_intel-2.0.0 → optimum_intel-2.2.0}/optimum/exporters/openvino/__main__.py +91 -14
- optimum_intel-2.2.0/optimum/exporters/openvino/_ov_ops.py +268 -0
- {optimum_intel-2.0.0 → optimum_intel-2.2.0}/optimum/exporters/openvino/base.py +4 -1
- {optimum_intel-2.0.0 → optimum_intel-2.2.0}/optimum/exporters/openvino/convert.py +461 -40
- {optimum_intel-2.0.0 → optimum_intel-2.2.0}/optimum/exporters/openvino/input_generators.py +760 -50
- {optimum_intel-2.0.0 → optimum_intel-2.2.0}/optimum/exporters/openvino/model_configs.py +2194 -432
- {optimum_intel-2.0.0 → optimum_intel-2.2.0}/optimum/exporters/openvino/model_patcher.py +4350 -2194
- {optimum_intel-2.0.0 → optimum_intel-2.2.0}/optimum/exporters/openvino/patching_utils.py +46 -56
- {optimum_intel-2.0.0 → optimum_intel-2.2.0}/optimum/exporters/openvino/stateful.py +6 -2
- {optimum_intel-2.0.0 → optimum_intel-2.2.0}/optimum/exporters/openvino/utils.py +44 -7
- optimum_intel-2.2.0/optimum/exporters/openvino/utils_annotations.py +238 -0
- {optimum_intel-2.0.0 → optimum_intel-2.2.0}/optimum/intel/__init__.py +62 -0
- {optimum_intel-2.0.0 → optimum_intel-2.2.0}/optimum/intel/openvino/__init__.py +17 -2
- {optimum_intel-2.0.0 → optimum_intel-2.2.0}/optimum/intel/openvino/configuration.py +38 -5
- {optimum_intel-2.0.0 → optimum_intel-2.2.0}/optimum/intel/openvino/modeling.py +12 -1
- {optimum_intel-2.0.0 → optimum_intel-2.2.0}/optimum/intel/openvino/modeling_base.py +6 -0
- {optimum_intel-2.0.0 → optimum_intel-2.2.0}/optimum/intel/openvino/modeling_decoder.py +58 -34
- {optimum_intel-2.0.0 → optimum_intel-2.2.0}/optimum/intel/openvino/modeling_diffusion.py +1045 -28
- optimum_intel-2.2.0/optimum/intel/openvino/modeling_funasr.py +443 -0
- {optimum_intel-2.0.0 → optimum_intel-2.2.0}/optimum/intel/openvino/modeling_seq2seq.py +95 -26
- {optimum_intel-2.0.0 → optimum_intel-2.2.0}/optimum/intel/openvino/modeling_visual_language.py +2521 -475
- {optimum_intel-2.0.0 → optimum_intel-2.2.0}/optimum/intel/openvino/quantization.py +20 -4
- {optimum_intel-2.0.0 → optimum_intel-2.2.0}/optimum/intel/openvino/utils.py +11 -1
- {optimum_intel-2.0.0 → optimum_intel-2.2.0}/optimum/intel/pipelines/accelerator_utils.py +23 -6
- {optimum_intel-2.0.0 → optimum_intel-2.2.0}/optimum/intel/pipelines/pipeline_base.py +2 -4
- {optimum_intel-2.0.0 → optimum_intel-2.2.0}/optimum/intel/utils/dummy_openvino_and_diffusers_objects.py +99 -0
- {optimum_intel-2.0.0 → optimum_intel-2.2.0}/optimum/intel/utils/import_utils.py +13 -1
- {optimum_intel-2.0.0 → optimum_intel-2.2.0}/optimum/intel/utils/modeling_utils.py +6 -0
- {optimum_intel-2.0.0 → optimum_intel-2.2.0}/optimum/intel/version.py +1 -1
- {optimum_intel-2.0.0 → optimum_intel-2.2.0/optimum_intel.egg-info}/PKG-INFO +8 -10
- {optimum_intel-2.0.0 → optimum_intel-2.2.0}/optimum_intel.egg-info/SOURCES.txt +2 -0
- {optimum_intel-2.0.0 → optimum_intel-2.2.0}/optimum_intel.egg-info/requires.txt +5 -6
- {optimum_intel-2.0.0 → optimum_intel-2.2.0}/setup.py +5 -6
- optimum_intel-2.0.0/optimum/exporters/openvino/_ov_ops.py +0 -113
- {optimum_intel-2.0.0 → optimum_intel-2.2.0}/LICENSE +0 -0
- {optimum_intel-2.0.0 → optimum_intel-2.2.0}/MANIFEST.in +0 -0
- {optimum_intel-2.0.0 → optimum_intel-2.2.0}/optimum/commands/register/register_openvino.py +0 -0
- {optimum_intel-2.0.0 → optimum_intel-2.2.0}/optimum/exporters/openvino/__init__.py +0 -0
- {optimum_intel-2.0.0 → optimum_intel-2.2.0}/optimum/exporters/openvino/_traceable_cache.py +0 -0
- {optimum_intel-2.0.0 → optimum_intel-2.2.0}/optimum/exporters/openvino/_traceable_decorator.py +0 -0
- {optimum_intel-2.0.0 → optimum_intel-2.2.0}/optimum/exporters/openvino/config.py +0 -0
- {optimum_intel-2.0.0 → optimum_intel-2.2.0}/optimum/intel/openvino/loaders.py +0 -0
- {optimum_intel-2.0.0 → optimum_intel-2.2.0}/optimum/intel/openvino/modeling_open_clip.py +0 -0
- {optimum_intel-2.0.0 → optimum_intel-2.2.0}/optimum/intel/openvino/modeling_sam.py +0 -0
- {optimum_intel-2.0.0 → optimum_intel-2.2.0}/optimum/intel/openvino/modeling_sentence_transformers.py +0 -0
- {optimum_intel-2.0.0 → optimum_intel-2.2.0}/optimum/intel/openvino/modeling_text2speech.py +0 -0
- {optimum_intel-2.0.0 → optimum_intel-2.2.0}/optimum/intel/openvino/modeling_timm.py +0 -0
- {optimum_intel-2.0.0 → optimum_intel-2.2.0}/optimum/intel/pipelines/__init__.py +0 -0
- {optimum_intel-2.0.0 → optimum_intel-2.2.0}/optimum/intel/utils/__init__.py +0 -0
- {optimum_intel-2.0.0 → optimum_intel-2.2.0}/optimum/intel/utils/constant.py +0 -0
- {optimum_intel-2.0.0 → optimum_intel-2.2.0}/optimum/intel/utils/dummy_openvino_and_nncf_objects.py +0 -0
- {optimum_intel-2.0.0 → optimum_intel-2.2.0}/optimum/intel/utils/dummy_openvino_and_sentence_transformers_objects.py +0 -0
- {optimum_intel-2.0.0 → optimum_intel-2.2.0}/optimum_intel.egg-info/dependency_links.txt +0 -0
- {optimum_intel-2.0.0 → optimum_intel-2.2.0}/optimum_intel.egg-info/entry_points.txt +0 -0
- {optimum_intel-2.0.0 → optimum_intel-2.2.0}/optimum_intel.egg-info/not-zip-safe +0 -0
- {optimum_intel-2.0.0 → optimum_intel-2.2.0}/optimum_intel.egg-info/top_level.txt +0 -0
- {optimum_intel-2.0.0 → optimum_intel-2.2.0}/pyproject.toml +0 -0
- {optimum_intel-2.0.0 → optimum_intel-2.2.0}/setup.cfg +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: optimum-intel
|
|
3
|
-
Version: 2.
|
|
3
|
+
Version: 2.2.0
|
|
4
4
|
Summary: Optimum Library is an extension of the Hugging Face Transformers library, providing a framework to integrate third-party libraries from Hardware Partners and interface with their specific functionality.
|
|
5
5
|
Home-page: https://www.intel.com
|
|
6
6
|
Author: HuggingFace Inc. Special Ops Team
|
|
@@ -20,15 +20,15 @@ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
|
20
20
|
Description-Content-Type: text/markdown
|
|
21
21
|
License-File: LICENSE
|
|
22
22
|
Requires-Dist: torch>=2.1
|
|
23
|
-
Requires-Dist:
|
|
24
|
-
Requires-Dist:
|
|
25
|
-
Requires-Dist: transformers<5.1,>=4.45
|
|
23
|
+
Requires-Dist: optimum~=2.3.0
|
|
24
|
+
Requires-Dist: transformers<5.6,>=4.51
|
|
26
25
|
Requires-Dist: setuptools
|
|
27
|
-
Requires-Dist: huggingface-hub<
|
|
28
|
-
Requires-Dist: nncf>=
|
|
26
|
+
Requires-Dist: huggingface-hub<1.22,>=0.23.2
|
|
27
|
+
Requires-Dist: nncf>=3.3.0
|
|
29
28
|
Requires-Dist: openvino>=2026.0
|
|
30
29
|
Requires-Dist: openvino-tokenizers>=2026.0
|
|
31
30
|
Requires-Dist: requests<3.0,>=2.33
|
|
31
|
+
Requires-Dist: torchvision
|
|
32
32
|
Provides-Extra: nncf
|
|
33
33
|
Requires-Dist: nncf>=2.19.0; extra == "nncf"
|
|
34
34
|
Provides-Extra: openvino
|
|
@@ -61,7 +61,6 @@ Requires-Dist: open_clip_torch>=2.26.1; extra == "tests"
|
|
|
61
61
|
Requires-Dist: peft; extra == "tests"
|
|
62
62
|
Requires-Dist: datasets<4.0.0,>=1.4.0; extra == "tests"
|
|
63
63
|
Requires-Dist: tbb; extra == "tests"
|
|
64
|
-
Requires-Dist: langchain-huggingface; extra == "tests"
|
|
65
64
|
Requires-Dist: hf_xet; extra == "tests"
|
|
66
65
|
Requires-Dist: num2words; extra == "tests"
|
|
67
66
|
Requires-Dist: vocos; extra == "tests"
|
|
@@ -101,16 +100,15 @@ Dynamic: summary
|
|
|
101
100
|
To install the latest release of 🤗 Optimum Intel with the corresponding required dependencies, you can use `pip` as follows:
|
|
102
101
|
|
|
103
102
|
```bash
|
|
104
|
-
|
|
103
|
+
pip install -U optimum-intel
|
|
105
104
|
```
|
|
106
105
|
|
|
107
106
|
Optimum Intel is a fast-moving project with regular additions of new model support, so you may want to install from source with the following command:
|
|
108
107
|
|
|
109
108
|
```bash
|
|
110
|
-
|
|
109
|
+
pip install optimum-intel@git+https://github.com/huggingface/optimum-intel.git
|
|
111
110
|
```
|
|
112
111
|
|
|
113
|
-
**Deprecation Notice:** The `extras` for `openvino` (e.g., `pip install optimum-intel[openvino,nncf]`), `nncf`, `neural-compressor`, `ipex` are **deprecated** and will be **removed in a future release**.
|
|
114
112
|
|
|
115
113
|
|
|
116
114
|
## Export:
|
|
@@ -14,16 +14,15 @@
|
|
|
14
14
|
To install the latest release of 🤗 Optimum Intel with the corresponding required dependencies, you can use `pip` as follows:
|
|
15
15
|
|
|
16
16
|
```bash
|
|
17
|
-
|
|
17
|
+
pip install -U optimum-intel
|
|
18
18
|
```
|
|
19
19
|
|
|
20
20
|
Optimum Intel is a fast-moving project with regular additions of new model support, so you may want to install from source with the following command:
|
|
21
21
|
|
|
22
22
|
```bash
|
|
23
|
-
|
|
23
|
+
pip install optimum-intel@git+https://github.com/huggingface/optimum-intel.git
|
|
24
24
|
```
|
|
25
25
|
|
|
26
|
-
**Deprecation Notice:** The `extras` for `openvino` (e.g., `pip install optimum-intel[openvino,nncf]`), `nncf`, `neural-compressor`, `ipex` are **deprecated** and will be **removed in a future release**.
|
|
27
26
|
|
|
28
27
|
|
|
29
28
|
## Export:
|
|
@@ -179,7 +179,7 @@ def parse_args_openvino(parser: "ArgumentParser"):
|
|
|
179
179
|
"dataset will be collected from model's generations. "
|
|
180
180
|
"For diffusion models it should be on of ['conceptual_captions',"
|
|
181
181
|
"'laion/220k-GPT4Vision-captions-from-LIVIS','laion/filtered-wit']. "
|
|
182
|
-
"For visual language models the dataset must be set to 'contextual'. "
|
|
182
|
+
"For visual language models the dataset must be set to 'textvqa' ('contextual' is deprecated). "
|
|
183
183
|
"Note: if none of the data-aware compression algorithms are selected and ratio parameter is omitted or "
|
|
184
184
|
"equals 1.0, the dataset argument will not have an effect on the resulting model."
|
|
185
185
|
"Note: for text generation task, datasets with English texts such as 'wikitext2','gsm8k','c4' or 'c4-new' usually "
|
|
@@ -314,6 +314,7 @@ def no_compression_parameter_provided(args):
|
|
|
314
314
|
args.lora_correction,
|
|
315
315
|
args.sensitivity_metric,
|
|
316
316
|
args.backup_precision,
|
|
317
|
+
args.group_size_fallback,
|
|
317
318
|
)
|
|
318
319
|
)
|
|
319
320
|
)
|
|
@@ -32,6 +32,7 @@ from optimum.exporters.openvino.base import OpenVINOConfig
|
|
|
32
32
|
from optimum.exporters.tasks import TasksManager
|
|
33
33
|
from optimum.intel.utils.import_utils import (
|
|
34
34
|
DIFFUSERS_IMPORT_ERROR,
|
|
35
|
+
_transformers_version,
|
|
35
36
|
is_diffusers_available,
|
|
36
37
|
is_nncf_available,
|
|
37
38
|
is_openvino_tokenizers_available,
|
|
@@ -89,6 +90,11 @@ def infer_task(
|
|
|
89
90
|
task = "zero-shot-image-classification"
|
|
90
91
|
elif library_name == "kokoro":
|
|
91
92
|
task = "text-to-audio"
|
|
93
|
+
elif library_name == "funasr":
|
|
94
|
+
# Use the with-past task so the encoder-decoder export is stateful (KV cache hidden in
|
|
95
|
+
# OpenVINO state). Without the `-with-past` suffix the decoder is exported stateless and
|
|
96
|
+
# incremental generation breaks (only the first token is correct).
|
|
97
|
+
task = "automatic-speech-recognition-with-past"
|
|
92
98
|
else:
|
|
93
99
|
try:
|
|
94
100
|
task = TasksManager._infer_task_from_model_name_or_path(
|
|
@@ -176,14 +182,55 @@ def infer_task(
|
|
|
176
182
|
return task
|
|
177
183
|
|
|
178
184
|
|
|
179
|
-
def
|
|
185
|
+
def _ensure_qwen3_omni_rope_scaling(config):
|
|
186
|
+
"""Fill in a valid ``rope_scaling`` for Qwen3-Omni-MoE sub-configs that are missing it."""
|
|
187
|
+
thinker_config = getattr(config, "thinker_config", None)
|
|
188
|
+
talker_config = getattr(config, "talker_config", None)
|
|
189
|
+
sub_configs = [
|
|
190
|
+
getattr(thinker_config, "text_config", None),
|
|
191
|
+
getattr(talker_config, "text_config", None),
|
|
192
|
+
getattr(talker_config, "code_predictor_config", None),
|
|
193
|
+
]
|
|
194
|
+
|
|
195
|
+
for sub_config in sub_configs:
|
|
196
|
+
if sub_config is None or getattr(sub_config, "rope_scaling", None) is not None:
|
|
197
|
+
continue
|
|
198
|
+
|
|
199
|
+
head_dim = getattr(sub_config, "head_dim", None)
|
|
200
|
+
if head_dim is None:
|
|
201
|
+
hidden_size = getattr(sub_config, "hidden_size", None)
|
|
202
|
+
num_heads = getattr(sub_config, "num_attention_heads", None)
|
|
203
|
+
if not hidden_size or not num_heads:
|
|
204
|
+
continue
|
|
205
|
+
head_dim = hidden_size // num_heads
|
|
206
|
+
|
|
207
|
+
# mrope splits head_dim // 2 across the (temporal, height, width) axes; distribute the
|
|
208
|
+
# remainder to the leading axis, matching how real checkpoints are laid out.
|
|
209
|
+
half = head_dim // 2
|
|
210
|
+
base, remainder = divmod(half, 3)
|
|
211
|
+
mrope_section = [base + remainder, base, base]
|
|
212
|
+
sub_config.rope_scaling = {"rope_type": "default", "type": "default", "mrope_section": mrope_section}
|
|
213
|
+
|
|
214
|
+
return config
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
# Maps config.architectures[0] to the (AutoModel, AutoModelForCausalLM) classes in
|
|
218
|
+
# model_patcher used to load custom draft models for speculative decoding.
|
|
219
|
+
_CUSTOM_DRAFT_MODEL_MAP = {
|
|
220
|
+
"LlamaForCausalLMEagle3": ("LlamaEagle3Model", "LlamaEagle3ForCausalLM"),
|
|
221
|
+
"Eagle3LlamaForCausalLM": ("LlamaEagle3Model", "LlamaEagle3ForCausalLM"),
|
|
222
|
+
"DFlashDraftModel": ("Qwen3DFlashDraftModel", "Qwen3DFlashForCausalLM"),
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
def update_config_for_custom_draft_model(config, auto_model, auto_model_for_causal_lm):
|
|
180
227
|
moduler_name = "optimum.exporters.openvino.model_patcher"
|
|
181
228
|
spec = importlib.util.find_spec(moduler_name)
|
|
182
229
|
if spec and spec.origin:
|
|
183
230
|
moduler_path = os.path.dirname(spec.origin)
|
|
184
231
|
config.auto_map = {
|
|
185
|
-
"AutoModel": moduler_path
|
|
186
|
-
"AutoModelForCausalLM": moduler_path
|
|
232
|
+
"AutoModel": f"{moduler_path}--model_patcher.{auto_model}",
|
|
233
|
+
"AutoModelForCausalLM": f"{moduler_path}--model_patcher.{auto_model_for_causal_lm}",
|
|
187
234
|
}
|
|
188
235
|
config.tie_word_embeddings = False
|
|
189
236
|
return config
|
|
@@ -353,11 +400,12 @@ def main_export(
|
|
|
353
400
|
quantization_config = getattr(config, "quantization_config", None)
|
|
354
401
|
quant_method = quantization_config.get("quant_method", None) if quantization_config else None
|
|
355
402
|
|
|
356
|
-
# update config to load
|
|
403
|
+
# update config to load custom draft models (eagle3 text-only/VLM variants, dflash)
|
|
357
404
|
archs = getattr(config, "architectures", None)
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
405
|
+
if isinstance(archs, list) and archs:
|
|
406
|
+
draft_classes = _CUSTOM_DRAFT_MODEL_MAP.get(archs[0])
|
|
407
|
+
if draft_classes is not None:
|
|
408
|
+
loading_kwargs["config"] = update_config_for_custom_draft_model(config, *draft_classes)
|
|
361
409
|
|
|
362
410
|
# mxfp4 quantized model will be dequantized to bf16
|
|
363
411
|
if quant_method == "mxfp4" and is_transformers_version(">=", "4.55"):
|
|
@@ -373,6 +421,20 @@ def main_export(
|
|
|
373
421
|
patch_qwenvl_configs()
|
|
374
422
|
|
|
375
423
|
model_type = config.model_type
|
|
424
|
+
|
|
425
|
+
if model_type == "qwen3_omni_moe":
|
|
426
|
+
if is_transformers_version("<", "5.0"):
|
|
427
|
+
raise ValueError(
|
|
428
|
+
"Exporting Qwen3-Omni-MoE models requires transformers >= 5.0, "
|
|
429
|
+
f"but found {_transformers_version}. Please upgrade transformers."
|
|
430
|
+
)
|
|
431
|
+
loading_kwargs["config"] = _ensure_qwen3_omni_rope_scaling(config)
|
|
432
|
+
|
|
433
|
+
if original_task == "auto" and model_type in {"phi4mm", "phi4_multimodal"}:
|
|
434
|
+
task = "image-text-to-text"
|
|
435
|
+
elif model_type == "qwen3_omni_moe":
|
|
436
|
+
task = "image-text-to-text"
|
|
437
|
+
|
|
376
438
|
if model_type not in TasksManager._SUPPORTED_MODEL_TYPE:
|
|
377
439
|
if custom_export_configs is None:
|
|
378
440
|
raise ValueError(
|
|
@@ -518,6 +580,10 @@ def main_export(
|
|
|
518
580
|
model = _OpenClipForZeroShotImageClassification.from_pretrained(model_name_or_path, cache_dir=cache_dir)
|
|
519
581
|
elif library_name == "kokoro":
|
|
520
582
|
model = _KokoroForTextToSpeech.from_pretrained(model_name_or_path, cache_dir=cache_dir, token=token)
|
|
583
|
+
elif library_name == "funasr":
|
|
584
|
+
from optimum.intel.openvino.modeling_funasr import _FunASRForSpeechSeq2Seq
|
|
585
|
+
|
|
586
|
+
model = _FunASRForSpeechSeq2Seq.from_pretrained(model_name_or_path, cache_dir=cache_dir, token=token)
|
|
521
587
|
else:
|
|
522
588
|
# remote code models like phi3_v internvl2, minicpmv, internvl2, nanollava, maira2 should be loaded using AutoModelForCausalLM and not AutoModelForImageTextToText
|
|
523
589
|
# TODO: use config.auto_map to load remote code models instead (for other models we can directly use config.architectures)
|
|
@@ -687,11 +753,14 @@ def _main_quantize(
|
|
|
687
753
|
token=token,
|
|
688
754
|
)
|
|
689
755
|
|
|
690
|
-
#
|
|
691
|
-
#
|
|
692
|
-
#
|
|
693
|
-
#
|
|
694
|
-
|
|
756
|
+
# Some multimodal models must always be reloaded through OVModelForVisualCausalLM, but their task
|
|
757
|
+
# would otherwise route to the wrong OV class:
|
|
758
|
+
# - Phi-4-multimodal-instruct: its model card pins pipeline_tag=automatic-speech-recognition, so the
|
|
759
|
+
# *inferred* (auto) task is wrong and would pick OVModelForSpeechSeq2Seq.
|
|
760
|
+
# - Qwen3-Omni-MoE: registers several tasks (text-to-audio, ASR, image-text-to-text); any of them,
|
|
761
|
+
# whether inferred or passed explicitly, must still load through OVModelForVisualCausalLM.
|
|
762
|
+
# AutoConfig is cheap (cached) so we always read it to decide on the redirect.
|
|
763
|
+
if library_name == "transformers":
|
|
695
764
|
config = AutoConfig.from_pretrained(
|
|
696
765
|
model_name_or_path,
|
|
697
766
|
subfolder=subfolder,
|
|
@@ -701,7 +770,10 @@ def _main_quantize(
|
|
|
701
770
|
trust_remote_code=trust_remote_code,
|
|
702
771
|
)
|
|
703
772
|
model_type = config.model_type
|
|
704
|
-
|
|
773
|
+
# phi4mm is only misrouted by task inference, so keep its redirect limited to the auto case.
|
|
774
|
+
if original_task == "auto" and model_type in ["phi4mm", "phi4_multimodal"]:
|
|
775
|
+
task = "image-text-to-text"
|
|
776
|
+
elif model_type == "qwen3_omni_moe":
|
|
705
777
|
task = "image-text-to-text"
|
|
706
778
|
|
|
707
779
|
# Step 1. Obtain the correct OpenVINO model class
|
|
@@ -777,7 +849,12 @@ def maybe_convert_tokenizers(library_name: str, output: Path, model=None, prepro
|
|
|
777
849
|
processor_chat_template = processor.chat_template
|
|
778
850
|
if tokenizer:
|
|
779
851
|
try:
|
|
780
|
-
export_tokenizer(
|
|
852
|
+
export_tokenizer(
|
|
853
|
+
tokenizer,
|
|
854
|
+
output,
|
|
855
|
+
task=task,
|
|
856
|
+
processor_chat_template=processor_chat_template,
|
|
857
|
+
)
|
|
781
858
|
except Exception as exception:
|
|
782
859
|
logger.warning(
|
|
783
860
|
"Could not load tokenizer using specified model ID or path. OpenVINO tokenizer/detokenizer "
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
# Copyright 2026 The HuggingFace Team. All rights reserved.
|
|
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
|
+
# Conversion rule for the `RecurrentAttentionCellOp` operation in a Torch graph.
|
|
16
|
+
# The `RecurrentAttentionCellOp` appears in the Torch graph as a result of replacing
|
|
17
|
+
# the `torch.nn.Module` block `RecurrentAttentionCell` via a registered
|
|
18
|
+
# `ModuleExtension` for `RecurrentAttentionCell` in the OpenVINO PyTorch frontend.
|
|
19
|
+
import numpy as np
|
|
20
|
+
|
|
21
|
+
import openvino as ov
|
|
22
|
+
import openvino.opset14 as ops
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def convert_recurrent_attention_cell(context):
|
|
26
|
+
query = context.get_input(0)
|
|
27
|
+
key = context.get_input(1)
|
|
28
|
+
value = context.get_input(2)
|
|
29
|
+
g = context.get_input(3)
|
|
30
|
+
beta = context.get_input(4)
|
|
31
|
+
last_recurrent_state_old = context.get_input(5)
|
|
32
|
+
|
|
33
|
+
value_shape = ops.shape_of(value)
|
|
34
|
+
const_zero = ops.constant(0, dtype=np.float32)
|
|
35
|
+
core_attn_out = ops.broadcast(const_zero, value_shape)
|
|
36
|
+
const_two_out = ops.constant(2, dtype=np.int32)
|
|
37
|
+
const_zero_out = ops.constant(0, dtype=np.int32)
|
|
38
|
+
seq_len = ops.gather(value_shape, const_two_out, const_zero_out)
|
|
39
|
+
|
|
40
|
+
timestep_param = ops.parameter([], np.int32, "timestep")
|
|
41
|
+
q_t_param = ops.parameter([-1, -1, 1, -1], np.float32, "q_t")
|
|
42
|
+
k_t_param = ops.parameter([-1, -1, 1, -1], np.float32, "k_t")
|
|
43
|
+
v_t_param = ops.parameter([-1, -1, 1, -1], np.float32, "v_t")
|
|
44
|
+
g_t_param = ops.parameter([-1, -1, 1], np.float32, "g_t")
|
|
45
|
+
beta_t_param = ops.parameter([-1, -1, 1], np.float32, "beta_t")
|
|
46
|
+
last_recurrent_state_t = ops.parameter([-1, -1, -1, -1], np.float32, "last_recurrent_state_t")
|
|
47
|
+
core_attn_out_t = ops.parameter([-1, -1, -1, -1], np.float32, "core_attn_out_t")
|
|
48
|
+
|
|
49
|
+
const_two = ops.constant(2, dtype=np.int32)
|
|
50
|
+
q_t = ops.squeeze(q_t_param, const_two)
|
|
51
|
+
k_t = ops.squeeze(k_t_param, const_two)
|
|
52
|
+
v_t = ops.squeeze(v_t_param, const_two)
|
|
53
|
+
const_minus_one = ops.constant(-1, dtype=np.int32)
|
|
54
|
+
g_t = ops.unsqueeze(ops.exp(g_t_param), const_minus_one)
|
|
55
|
+
beta_t = beta_t_param
|
|
56
|
+
|
|
57
|
+
last_recurrent_state_in = ops.multiply(last_recurrent_state_t, g_t)
|
|
58
|
+
const_minus_two = ops.constant(-2, dtype=np.int32)
|
|
59
|
+
kv_mem = ops.multiply(last_recurrent_state_in, ops.unsqueeze(k_t, const_minus_one))
|
|
60
|
+
kv_mem = ops.reduce_sum(kv_mem, const_minus_two, False)
|
|
61
|
+
delta = ops.multiply(ops.subtract(v_t, kv_mem), beta_t)
|
|
62
|
+
last_recurrent_state_delta = ops.multiply(
|
|
63
|
+
ops.unsqueeze(k_t, const_minus_one), ops.unsqueeze(delta, const_minus_two)
|
|
64
|
+
)
|
|
65
|
+
last_recurrent_state_in = ops.add(last_recurrent_state_in, last_recurrent_state_delta)
|
|
66
|
+
core_attn_update = ops.multiply(last_recurrent_state_in, ops.unsqueeze(q_t, const_minus_one))
|
|
67
|
+
core_attn_update = ops.reduce_sum(core_attn_update, const_minus_two, True)
|
|
68
|
+
const_zero = ops.constant(0, dtype=np.int32)
|
|
69
|
+
timestep = ops.unsqueeze(timestep_param, const_zero)
|
|
70
|
+
|
|
71
|
+
core_attn_out_res = ops.scatter_update(core_attn_out_t, timestep, core_attn_update, const_two)
|
|
72
|
+
last_recurrent_state_res = last_recurrent_state_in
|
|
73
|
+
|
|
74
|
+
body_cond = ops.constant([True], dtype=bool)
|
|
75
|
+
|
|
76
|
+
body_model = ov.Model(
|
|
77
|
+
[body_cond, last_recurrent_state_res, core_attn_out_res],
|
|
78
|
+
[
|
|
79
|
+
timestep_param,
|
|
80
|
+
q_t_param,
|
|
81
|
+
k_t_param,
|
|
82
|
+
v_t_param,
|
|
83
|
+
g_t_param,
|
|
84
|
+
beta_t_param,
|
|
85
|
+
last_recurrent_state_t,
|
|
86
|
+
core_attn_out_t,
|
|
87
|
+
],
|
|
88
|
+
"body_model",
|
|
89
|
+
)
|
|
90
|
+
|
|
91
|
+
seq_len = ops.convert(seq_len, "i32")
|
|
92
|
+
loop = ops.loop(seq_len, ops.constant(True, dtype="bool"))
|
|
93
|
+
loop.set_function(body_model)
|
|
94
|
+
|
|
95
|
+
loop.set_sliced_input(q_t_param, query, 0, 1, 1, -1, 2)
|
|
96
|
+
loop.set_sliced_input(k_t_param, key, 0, 1, 1, -1, 2)
|
|
97
|
+
loop.set_sliced_input(v_t_param, value, 0, 1, 1, -1, 2)
|
|
98
|
+
loop.set_sliced_input(g_t_param, g, 0, 1, 1, -1, 2)
|
|
99
|
+
loop.set_sliced_input(beta_t_param, beta, 0, 1, 1, -1, 2)
|
|
100
|
+
loop.set_merged_input(last_recurrent_state_t, last_recurrent_state_old, last_recurrent_state_res.output(0))
|
|
101
|
+
loop.set_merged_input(core_attn_out_t, core_attn_out.output(0), core_attn_out_res.output(0))
|
|
102
|
+
loop.set_special_body_ports([0, 0])
|
|
103
|
+
|
|
104
|
+
core_attn_out_new = loop.get_iter_value(core_attn_out_res.output(0), -1)
|
|
105
|
+
last_recurrent_state_new = loop.get_iter_value(last_recurrent_state_res.output(0), -1)
|
|
106
|
+
|
|
107
|
+
flatten_shape = ops.constant([-1], dtype=np.int32)
|
|
108
|
+
core_attn_out_new = ops.reshape(core_attn_out_new, flatten_shape, False)
|
|
109
|
+
last_recurrent_state_new = ops.reshape(last_recurrent_state_new, flatten_shape, False)
|
|
110
|
+
|
|
111
|
+
final_output = ops.concat([core_attn_out_new, last_recurrent_state_new], 0)
|
|
112
|
+
|
|
113
|
+
return [final_output.output(0)]
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
# Conversion rule for the `SelectiveSSMRecurrentCellOp` operation in a Torch graph.
|
|
117
|
+
#
|
|
118
|
+
# This generalizes the recurrent-cell-to-`ov::Loop` approach (originally introduced for the
|
|
119
|
+
# GatedDeltaNet block, see `convert_recurrent_attention_cell` above) to the Mamba2 selective
|
|
120
|
+
# state-space recurrence used by hybrid Mamba2 models such as GraniteMoeHybrid (Granite-4.0).
|
|
121
|
+
#
|
|
122
|
+
# The Mamba2 single-step recurrence follows the standard Mamba-2 discretization:
|
|
123
|
+
# state_t = state_{t-1} * dA_t + dBx_t # [B, H, P, N]
|
|
124
|
+
# y_t = reduce_sum(state_t * C_t, axis=N) # [B, H, P]
|
|
125
|
+
#
|
|
126
|
+
# All the heavy discretization tensors are computed vectorized over the whole sequence BEFORE
|
|
127
|
+
# the loop, so the loop body stays as simple as possible. This mirrors the torch reference:
|
|
128
|
+
# dA = exp(A * dt).reshape(B, T, H, 1, 1) # [B, T, H, 1, 1]
|
|
129
|
+
# dB = dt.reshape(B, T, H, 1) * B # [B, T, H, N]
|
|
130
|
+
# dBx = dB.reshape(B, T, H, 1, N) * x.reshape(B, T, H, P, 1) # [B, T, H, P, N]
|
|
131
|
+
# C = C.reshape(B, T, H, 1, N) # [B, T, H, 1, N]
|
|
132
|
+
# The loop then simply slices dA/dBx/C along dim 1 (T) and runs the two-line recurrence.
|
|
133
|
+
# Inputs are in [B, T, H, ...] layout; the skip connection `x_t * D` does not depend on the
|
|
134
|
+
# recurrent state and is added outside.
|
|
135
|
+
#
|
|
136
|
+
# The `SelectiveSSMRecurrentCellOp` appears in the Torch graph as a result of replacing the
|
|
137
|
+
# `SelectiveSSMRecurrentCell` `torch.nn.Module` via a registered `ModuleExtension` in the OpenVINO
|
|
138
|
+
# PyTorch frontend; OpenVINO then applies this conversion rule to the resulting operation.
|
|
139
|
+
def convert_recurrent_selective_ssm_cell(context):
|
|
140
|
+
# Inputs match the forward signature of `SelectiveSSMRecurrentCell`:
|
|
141
|
+
# A [H] — negative log-decay rates
|
|
142
|
+
# dt [B, T, H] — time steps
|
|
143
|
+
# B [B, T, G, N] — input matrix (G groups, expanded to H before the loop)
|
|
144
|
+
# x [B, T, H, P] — input hidden states
|
|
145
|
+
# C [B, T, G, N] — output matrix (G groups, expanded to H before the loop)
|
|
146
|
+
# recurrent_state [B, H, P, N] — initial recurrent state
|
|
147
|
+
A = context.get_input(0) # [H]
|
|
148
|
+
dt = context.get_input(1) # [B, T, H]
|
|
149
|
+
B = context.get_input(2) # [B, T, G, N]
|
|
150
|
+
x = context.get_input(3) # [B, T, H, P]
|
|
151
|
+
C = context.get_input(4) # [B, T, G, N]
|
|
152
|
+
recurrent_state = context.get_input(5) # [B, H, P, N]
|
|
153
|
+
|
|
154
|
+
const_zero_axis = ops.constant(0, dtype=np.int32)
|
|
155
|
+
const_one = ops.constant(1, dtype=np.int32)
|
|
156
|
+
const_minus_one = ops.constant(-1, dtype=np.int32)
|
|
157
|
+
const_minus_two = ops.constant(-2, dtype=np.int32)
|
|
158
|
+
|
|
159
|
+
# Compute heads_per_group = H / G from the shapes of dt and B.
|
|
160
|
+
dt_shape = ops.shape_of(dt)
|
|
161
|
+
B_shape = ops.shape_of(B)
|
|
162
|
+
const_two = ops.constant(2, dtype=np.int32)
|
|
163
|
+
num_heads = ops.gather(dt_shape, const_two, const_zero_axis) # H
|
|
164
|
+
num_groups = ops.gather(B_shape, const_two, const_zero_axis) # G
|
|
165
|
+
heads_per_group = ops.convert(ops.divide(num_heads, num_groups), "i64")
|
|
166
|
+
|
|
167
|
+
# Expand grouped B/C from [B, T, G, N] → [B, T, H, N] via repeat_interleave along dim 2:
|
|
168
|
+
# reshape to [B, T, G, 1, N] → tile [1, 1, 1, heads_per_group, 1] → reshape [B, T, H, N]
|
|
169
|
+
B_5d = ops.unsqueeze(B, ops.constant(3, dtype=np.int32)) # [B, T, G, 1, N]
|
|
170
|
+
C_5d = ops.unsqueeze(C, ops.constant(3, dtype=np.int32)) # [B, T, G, 1, N]
|
|
171
|
+
tile_shape = ops.concat(
|
|
172
|
+
[
|
|
173
|
+
ops.constant([1, 1, 1], dtype=np.int64),
|
|
174
|
+
ops.unsqueeze(heads_per_group, const_zero_axis),
|
|
175
|
+
ops.constant([1], dtype=np.int64),
|
|
176
|
+
],
|
|
177
|
+
0,
|
|
178
|
+
)
|
|
179
|
+
B_tiled = ops.tile(B_5d, tile_shape) # [B, T, G, H/G, N]
|
|
180
|
+
C_tiled = ops.tile(C_5d, tile_shape) # [B, T, G, H/G, N]
|
|
181
|
+
|
|
182
|
+
# Reshape [B, T, G, H/G, N] → [B, T, H, N]
|
|
183
|
+
x_shape = ops.shape_of(x)
|
|
184
|
+
N = ops.gather(B_shape, ops.constant(3, dtype=np.int32), const_zero_axis)
|
|
185
|
+
BC_shape = ops.concat([ops.constant([0, 0, -1], dtype=np.int64), ops.unsqueeze(N, const_zero_axis)], 0)
|
|
186
|
+
B = ops.reshape(B_tiled, BC_shape, True) # [B, T, H, N]
|
|
187
|
+
C = ops.reshape(C_tiled, BC_shape, True) # [B, T, H, N]
|
|
188
|
+
|
|
189
|
+
# Vectorized discretization over the whole sequence, before the loop (mirrors torch):
|
|
190
|
+
# dA = exp(A * dt).reshape(B, T, H, 1, 1)
|
|
191
|
+
# dB = dt.reshape(B, T, H, 1) * B
|
|
192
|
+
# dBx = dB.reshape(B, T, H, 1, N) * x.reshape(B, T, H, P, 1) → [B, T, H, P, N]
|
|
193
|
+
# C = C.reshape(B, T, H, 1, N)
|
|
194
|
+
dA = ops.exp(ops.multiply(A, dt)) # [B, T, H]
|
|
195
|
+
dA = ops.reshape(dA, ops.constant([0, 0, 0, 1, 1], dtype=np.int64), True) # [B, T, H, 1, 1]
|
|
196
|
+
dB = ops.multiply(ops.unsqueeze(dt, const_minus_one), B) # [B, T, H, N]
|
|
197
|
+
dBx = ops.multiply(
|
|
198
|
+
ops.unsqueeze(dB, const_minus_two), # [B, T, H, 1, N]
|
|
199
|
+
ops.unsqueeze(x, const_minus_one), # [B, T, H, P, 1]
|
|
200
|
+
) # [B, T, H, P, N]
|
|
201
|
+
C = ops.unsqueeze(C, const_minus_two) # [B, T, H, 1, N]
|
|
202
|
+
|
|
203
|
+
# Build the zero-initialized output accumulator with shape [B, T, H, P].
|
|
204
|
+
const_zero_f32 = ops.constant(0, dtype=np.float32)
|
|
205
|
+
output = ops.broadcast(const_zero_f32, x_shape)
|
|
206
|
+
|
|
207
|
+
# Trip count for the loop equals the sequence length (dim 1 of x).
|
|
208
|
+
seq_len = ops.gather(x_shape, const_one, const_zero_axis)
|
|
209
|
+
seq_len = ops.convert(seq_len, "i32")
|
|
210
|
+
|
|
211
|
+
# Body parameters (one timestep slice each along dim 1). All tensors are already discretized.
|
|
212
|
+
timestep_param = ops.parameter([], np.int32, "timestep")
|
|
213
|
+
dA_t_param = ops.parameter([-1, 1, -1, 1, 1], np.float32, "dA_t") # [B, 1, H, 1, 1]
|
|
214
|
+
dBx_t_param = ops.parameter([-1, 1, -1, -1, -1], np.float32, "dBx_t") # [B, 1, H, P, N]
|
|
215
|
+
C_t_param = ops.parameter([-1, 1, -1, 1, -1], np.float32, "C_t") # [B, 1, H, 1, N]
|
|
216
|
+
recurrent_state_t = ops.parameter([-1, -1, -1, -1], np.float32, "recurrent_state_t") # [B, H, P, N]
|
|
217
|
+
output_t = ops.parameter([-1, -1, -1, -1], np.float32, "output_t") # [B, T, H, P]
|
|
218
|
+
|
|
219
|
+
# Drop the singleton sequence dimension introduced by slicing.
|
|
220
|
+
dA_t = ops.squeeze(dA_t_param, const_one) # [B, H, 1, 1]
|
|
221
|
+
dBx_t = ops.squeeze(dBx_t_param, const_one) # [B, H, P, N]
|
|
222
|
+
C_t = ops.squeeze(C_t_param, const_one) # [B, H, 1, N]
|
|
223
|
+
|
|
224
|
+
# output_recurrent_state = output_recurrent_state * dA[:, t] + dBx[:, t]
|
|
225
|
+
recurrent_state_new = ops.add(ops.multiply(recurrent_state_t, dA_t), dBx_t) # [B, H, P, N]
|
|
226
|
+
|
|
227
|
+
# output[:, t] = (output_recurrent_state * C[:, t]).sum(dim=-1)
|
|
228
|
+
y_t = ops.reduce_sum(ops.multiply(recurrent_state_new, C_t), const_minus_one, False) # [B, H, P]
|
|
229
|
+
y_t = ops.unsqueeze(y_t, const_one) # [B, 1, H, P]
|
|
230
|
+
|
|
231
|
+
timestep = ops.unsqueeze(timestep_param, const_zero_axis)
|
|
232
|
+
output_res = ops.scatter_update(output_t, timestep, y_t, const_one)
|
|
233
|
+
recurrent_state_res = recurrent_state_new
|
|
234
|
+
|
|
235
|
+
body_cond = ops.constant([True], dtype=bool)
|
|
236
|
+
body_model = ov.Model(
|
|
237
|
+
[body_cond, recurrent_state_res, output_res],
|
|
238
|
+
[
|
|
239
|
+
timestep_param,
|
|
240
|
+
dA_t_param,
|
|
241
|
+
dBx_t_param,
|
|
242
|
+
C_t_param,
|
|
243
|
+
recurrent_state_t,
|
|
244
|
+
output_t,
|
|
245
|
+
],
|
|
246
|
+
"selective_ssm_body_model",
|
|
247
|
+
)
|
|
248
|
+
|
|
249
|
+
loop = ops.loop(seq_len, ops.constant(True, dtype="bool"))
|
|
250
|
+
loop.set_function(body_model)
|
|
251
|
+
|
|
252
|
+
loop.set_sliced_input(dA_t_param, dA.output(0), 0, 1, 1, -1, 1)
|
|
253
|
+
loop.set_sliced_input(dBx_t_param, dBx.output(0), 0, 1, 1, -1, 1)
|
|
254
|
+
loop.set_sliced_input(C_t_param, C.output(0), 0, 1, 1, -1, 1)
|
|
255
|
+
loop.set_merged_input(recurrent_state_t, recurrent_state, recurrent_state_res.output(0))
|
|
256
|
+
loop.set_merged_input(output_t, output.output(0), output_res.output(0))
|
|
257
|
+
loop.set_special_body_ports([0, 0])
|
|
258
|
+
|
|
259
|
+
output_new = loop.get_iter_value(output_res.output(0), -1)
|
|
260
|
+
output_recurrent_state_new = loop.get_iter_value(recurrent_state_res.output(0), -1)
|
|
261
|
+
|
|
262
|
+
flatten_shape = ops.constant([-1], dtype=np.int32)
|
|
263
|
+
output_new = ops.reshape(output_new, flatten_shape, False)
|
|
264
|
+
output_recurrent_state_new = ops.reshape(output_recurrent_state_new, flatten_shape, False)
|
|
265
|
+
|
|
266
|
+
final_output = ops.concat([output_new, output_recurrent_state_new], 0)
|
|
267
|
+
|
|
268
|
+
return [final_output.output(0)]
|
|
@@ -71,6 +71,7 @@ class OpenVINOConfig(ExporterConfig, ABC):
|
|
|
71
71
|
DEFAULT_VARIANT = "default"
|
|
72
72
|
PATCHING_SPECS: list[PatchingSpec] | None = None
|
|
73
73
|
_MODEL_PATCHER = ModelPatcher
|
|
74
|
+
MIN_TRANSFORMERS_VERSION = "4.57"
|
|
74
75
|
|
|
75
76
|
_TASK_TO_COMMON_OUTPUTS = { # noqa: RUF012
|
|
76
77
|
"audio-classification": OrderedDict({"logits": {0: "batch_size"}}),
|
|
@@ -353,7 +354,9 @@ class OpenVINOConfigWithPast(OpenVINOConfig, ABC):
|
|
|
353
354
|
and "attention_mask" in dummy_inputs
|
|
354
355
|
and self.task == "text-generation"
|
|
355
356
|
):
|
|
356
|
-
|
|
357
|
+
# VLM Eagle3 uses inputs_embeds instead of input_ids
|
|
358
|
+
main_input = dummy_inputs.get("input_ids", dummy_inputs.get("inputs_embeds"))
|
|
359
|
+
seq_len = main_input.shape[1]
|
|
357
360
|
past_seq_len = dummy_inputs["past_key_values"][0][1].shape[-2]
|
|
358
361
|
dummy_inputs["attention_mask"] = DummyInputGenerator.pad_input_on_dim(
|
|
359
362
|
dummy_inputs["attention_mask"], desired_length=past_seq_len + seq_len, dim=1
|