Skip to content

Commit fd1887f

Browse files
committed
Check openvino-nightly compatibility
Add check for installed openvino-nightly package as well. Improve compatibility messages. Check is OpenVINO Tokenizers is available only if tokenizer is exported.
1 parent 52e24b5 commit fd1887f

File tree

3 files changed

+81
-59
lines changed

3 files changed

+81
-59
lines changed

optimum/exporters/openvino/__main__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from optimum.exporters.onnx.base import OnnxConfig
2424
from optimum.exporters.onnx.constants import SDPA_ARCHS_ONNX_EXPORT_NOT_SUPPORTED
2525
from optimum.exporters.openvino.convert import export_from_model, export_tokenizer
26-
from optimum.intel.utils.import_utils import is_transformers_version
26+
from optimum.intel.utils.import_utils import is_openvino_tokenizers_available, is_transformers_version
2727
from optimum.utils.save_utils import maybe_load_preprocessors
2828

2929

@@ -319,7 +319,7 @@ class StoreAttr(object):
319319
**kwargs_shapes,
320320
)
321321

322-
if convert_tokenizer:
322+
if convert_tokenizer and is_openvino_tokenizers_available():
323323
if library_name != "diffusers":
324324
tokenizer = next(
325325
(preprocessor for preprocessor in preprocessors if isinstance(preprocessor, PreTrainedTokenizerBase)),

optimum/exporters/openvino/convert.py

-8
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
from pathlib import Path
2121
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Tuple, Union
2222

23-
from transformers import T5Tokenizer, T5TokenizerFast
2423
from transformers.utils import is_tf_available, is_torch_available
2524

2625
from openvino.runtime import PartialShape, save_model
@@ -55,9 +54,6 @@
5554
from optimum.exporters.onnx.__main__ import _get_submodels_and_onnx_configs
5655

5756

58-
UNSUPPORTED_TOKENIZER_CLASSES = (T5Tokenizer, T5TokenizerFast)
59-
60-
6157
logger = logging.getLogger(__name__)
6258

6359
if is_torch_available():
@@ -662,10 +658,6 @@ def export_tokenizer(
662658
):
663659
from optimum.intel.openvino import OV_DETOKENIZER_NAME, OV_TOKENIZER_NAME # avoid circular imports
664660

665-
if isinstance(tokenizer, UNSUPPORTED_TOKENIZER_CLASSES):
666-
logger.info(f"OpenVINO Tokenizer export for {type(tokenizer).__name__} is not supported.")
667-
return
668-
669661
try:
670662
from openvino_tokenizers import convert_tokenizer
671663
except ModuleNotFoundError:

optimum/intel/utils/import_utils.py

+79-49
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
14+
import functools
1515
import importlib.util
1616
import logging
1717
import operator as op
@@ -85,54 +85,6 @@
8585
except ImportError:
8686
_openvino_available = False
8787

88-
_openvino_tokenizers_available = importlib.util.find_spec("openvino_tokenizers") is not None and _openvino_available
89-
_openvino_tokenizers_version = "N/A"
90-
if _openvino_tokenizers_available:
91-
try:
92-
_openvino_tokenizers_version = importlib_metadata.version("openvino_tokenizers")
93-
except importlib_metadata.PackageNotFoundError:
94-
_openvino_tokenizers_available = False
95-
96-
if _openvino_tokenizers_available and _openvino_tokenizers_version != "N/A":
97-
_is_ovt_dev_version = "dev" in _openvino_tokenizers_version
98-
_ov_version = importlib_metadata.version("openvino")
99-
_is_ov_dev_version = "dev" in _ov_version
100-
if _is_ovt_dev_version:
101-
_compatible_openvino_major_version, _, _dev_date = _openvino_tokenizers_version.rsplit(".", 2)
102-
_compatible_ov_version = _compatible_openvino_major_version + "." + _dev_date
103-
_compatible_ovt_version = _ov_version.replace("dev", "0.dev")
104-
else:
105-
_compatible_ov_version = _openvino_tokenizers_version.rsplit(".", 1)[0]
106-
_compatible_ovt_version = _ov_version + ".0"
107-
108-
_openvino_tokenizers_available = _ov_version == _compatible_ov_version
109-
110-
if not _openvino_tokenizers_available:
111-
_update_ov_command = (
112-
f"pip install {'--pre' if _is_ovt_dev_version else ''} -U openvino=={_compatible_ov_version} "
113-
+ (
114-
"--extra-index-url https://storage.openvinotoolkit.org/simple/wheels/nightly"
115-
if _is_ovt_dev_version
116-
else ""
117-
)
118-
).strip()
119-
_update_ovt_command = (
120-
f"pip install {'--pre' if _is_ov_dev_version else ''} -U openvino-tokenizers=={_compatible_ovt_version} "
121-
+ (
122-
"--extra-index-url https://storage.openvinotoolkit.org/simple/wheels/nightly"
123-
if _is_ov_dev_version
124-
else ""
125-
)
126-
).strip()
127-
logger.warning(
128-
"OpenVINO Tokenizer version is not compatible with OpenVINO version. "
129-
f"Installed OpenVINO version: {_ov_version}, "
130-
f"OpenVINO Tokenizers requires {_compatible_ov_version}. "
131-
"OpenVINO Tokenizers models will not be added during export. "
132-
f"Update OpenVINO with \n{_update_ov_command}\n"
133-
f"Or update OpenVINO Tokenizers with \n{_update_ovt_command}"
134-
)
135-
13688
_nncf_available = importlib.util.find_spec("nncf") is not None
13789
_nncf_version = "N/A"
13890
if _nncf_available:
@@ -204,7 +156,85 @@ def is_openvino_available():
204156
return _openvino_available
205157

206158

159+
def _create_pip_update_command(package_name: str, compatible_version: str, is_dev_version: bool = False) -> str:
160+
return " ".join(
161+
filter(
162+
None,
163+
(
164+
"pip install",
165+
"--pre" if is_dev_version else "",
166+
"-U",
167+
f"{package_name}=={compatible_version}",
168+
"--extra-index-url https://storage.openvinotoolkit.org/simple/wheels/nightly"
169+
if is_dev_version
170+
else "",
171+
),
172+
)
173+
)
174+
175+
176+
@functools.lru_cache(1)
207177
def is_openvino_tokenizers_available():
178+
_openvino_tokenizers_available = (
179+
importlib.util.find_spec("openvino_tokenizers") is not None and _openvino_available
180+
)
181+
_openvino_tokenizers_version = "N/A"
182+
if _openvino_tokenizers_available:
183+
try:
184+
_openvino_tokenizers_version = importlib_metadata.version("openvino_tokenizers")
185+
except importlib_metadata.PackageNotFoundError:
186+
return False
187+
188+
if _openvino_available and _openvino_tokenizers_available and _openvino_tokenizers_version != "N/A":
189+
_is_ovt_dev_version = "dev" in _openvino_tokenizers_version
190+
try:
191+
_ov_version = importlib_metadata.version("openvino")
192+
except importlib_metadata.PackageNotFoundError:
193+
try:
194+
_ov_version = importlib_metadata.version("openvino-nightly")
195+
except importlib_metadata.PackageNotFoundError:
196+
_ov_version = _openvino_version
197+
198+
_is_ov_dev_version = "dev" in _ov_version
199+
if _is_ovt_dev_version:
200+
_compatible_openvino_major_version, _, _dev_date = _openvino_tokenizers_version.rsplit(".", 2)
201+
_compatible_ov_version = _compatible_openvino_major_version + "." + _dev_date
202+
_compatible_ovt_version = _ov_version.replace("dev", "0.dev")
203+
else:
204+
_compatible_openvino_major_version = _openvino_tokenizers_version.rsplit(".", 1)[0]
205+
_compatible_ov_version = _compatible_openvino_major_version
206+
if _is_ov_dev_version:
207+
_compatible_ovt_version = _ov_version.replace("dev", "0.dev")
208+
else:
209+
_compatible_ovt_version = _ov_version + ".0"
210+
211+
_update_ov_command = _create_pip_update_command(
212+
"openvino", _compatible_ov_version, is_dev_version=_is_ovt_dev_version
213+
)
214+
_update_ovt_command = _create_pip_update_command(
215+
"openvino-tokenizers", _compatible_ovt_version, is_dev_version=_is_ov_dev_version
216+
)
217+
218+
_openvino_tokenizers_available = _ov_version.startswith(_compatible_openvino_major_version)
219+
220+
if not _openvino_tokenizers_available:
221+
logger.warning(
222+
"OpenVINO Tokenizers version is not compatible with OpenVINO version. "
223+
f"Installed OpenVINO version: {_ov_version}, "
224+
f"OpenVINO Tokenizers requires {_compatible_ov_version}. "
225+
"OpenVINO Tokenizers models will not be added during export. "
226+
f"Update OpenVINO with \n{_update_ov_command}\n"
227+
f"Or update OpenVINO Tokenizers with \n{_update_ovt_command}"
228+
)
229+
elif _ov_version != _compatible_ov_version:
230+
logger.info(
231+
"OpenVINO Tokenizers version is not aligned with OpenVINO version. "
232+
f"Installed OpenVINO version: {_ov_version}, "
233+
f"OpenVINO Tokenizers version: {_compatible_ov_version}. "
234+
"If you didn't get OpenVINO Tokenizer after model conversion "
235+
f"try to align libraries versions with one of two commands:\n{_update_ov_command}\n"
236+
f"Or\n{_update_ovt_command}"
237+
)
208238
return _openvino_tokenizers_available
209239

210240

0 commit comments

Comments
 (0)