|
15 | 15 | import logging
|
16 | 16 | import os
|
17 | 17 | from pathlib import Path
|
| 18 | +from tempfile import TemporaryDirectory |
18 | 19 | from typing import Optional, Union
|
19 | 20 |
|
20 | 21 | import numpy as np
|
|
50 | 51 |
|
51 | 52 | from optimum.exporters import TasksManager
|
52 | 53 |
|
| 54 | +from ...exporters.openvino import main_export |
53 | 55 | from ..utils.import_utils import is_timm_available, is_timm_version
|
54 | 56 | from .modeling_base import OVBaseModel
|
55 | 57 | from .utils import _is_timm_ov_dir
|
@@ -411,6 +413,48 @@ def forward(
|
411 | 413 | )
|
412 | 414 | return BaseModelOutput(last_hidden_state=last_hidden_state)
|
413 | 415 |
|
| 416 | + @classmethod |
| 417 | + def _from_transformers( |
| 418 | + cls, |
| 419 | + model_id: str, |
| 420 | + config: PretrainedConfig, |
| 421 | + use_auth_token: Optional[Union[bool, str]] = None, |
| 422 | + revision: Optional[str] = None, |
| 423 | + force_download: bool = False, |
| 424 | + cache_dir: Optional[str] = None, |
| 425 | + subfolder: str = "", |
| 426 | + local_files_only: bool = False, |
| 427 | + task: Optional[str] = None, |
| 428 | + trust_remote_code: bool = False, |
| 429 | + load_in_8bit: Optional[bool] = None, |
| 430 | + load_in_4bit: Optional[bool] = None, |
| 431 | + **kwargs, |
| 432 | + ): |
| 433 | + save_dir = TemporaryDirectory() |
| 434 | + save_dir_path = Path(save_dir.name) |
| 435 | + |
| 436 | + # If load_in_8bit is not specified then compression_option should be set to None and will be set by default in main_export depending on the model size |
| 437 | + compression_option = "fp32" if load_in_8bit is not None else None |
| 438 | + |
| 439 | + # OVModelForFeatureExtraction works with Transformers type of models, thus even sentence-transformers models are loaded as such. |
| 440 | + main_export( |
| 441 | + model_name_or_path=model_id, |
| 442 | + output=save_dir_path, |
| 443 | + task=task or cls.export_feature, |
| 444 | + subfolder=subfolder, |
| 445 | + revision=revision, |
| 446 | + cache_dir=cache_dir, |
| 447 | + use_auth_token=use_auth_token, |
| 448 | + local_files_only=local_files_only, |
| 449 | + force_download=force_download, |
| 450 | + trust_remote_code=trust_remote_code, |
| 451 | + compression_option=compression_option, |
| 452 | + library_name="transformers", |
| 453 | + ) |
| 454 | + |
| 455 | + config.save_pretrained(save_dir_path) |
| 456 | + return cls._from_pretrained(model_id=save_dir_path, config=config, load_in_8bit=load_in_8bit, **kwargs) |
| 457 | + |
414 | 458 |
|
415 | 459 | MASKED_LM_EXAMPLE = r"""
|
416 | 460 | Example of masked language modeling using `transformers.pipelines`:
|
|
0 commit comments