Skip to content

Commit

Permalink
Rename openvino model file name (#93)
Browse files Browse the repository at this point in the history
* Rename openvino model file name

* Add possibility to load model using previous file names
  • Loading branch information
echarlaix authored Nov 14, 2022
1 parent c92ff66 commit 8e42197
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 31 deletions.
4 changes: 2 additions & 2 deletions optimum/intel/openvino/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@

class OVConfig(BaseConfig):

CONFIG_NAME = "ov_config.json"
FULL_CONFIGURATION_FILE = "ov_config.json"
CONFIG_NAME = "openvino_config.json"
FULL_CONFIGURATION_FILE = "openvino_config.json"

def __init__(
self,
Expand Down
2 changes: 1 addition & 1 deletion optimum/intel/openvino/modeling.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@

class OVModel(OVBaseModel):

base_model_prefix = "ov_model"
base_model_prefix = "openvino_model"
auto_model_class = AutoModel

def __init__(self, model: openvino.runtime.Model, config: transformers.PretrainedConfig = None, **kwargs):
Expand Down
47 changes: 37 additions & 10 deletions optimum/intel/openvino/modeling_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import openvino
from huggingface_hub import HfApi, hf_hub_download
from huggingface_hub.utils import EntryNotFoundError
from openvino.offline_transformations import compress_model_transformation
from openvino.runtime import Core
from optimum.modeling_base import OptimizedModel
Expand Down Expand Up @@ -154,6 +155,12 @@ def _from_pretrained(
# Load the model from local directory
if os.path.isdir(model_id):
file_name = os.path.join(model_id, file_name)
if os.path.isfile(os.path.join(model_id, "ov_model.xml")):
file_name = os.path.join(model_id, "ov_model.xml")
logger.warning(
"The file names `ov_model.xml` and `ov_model.bin` will be soon deprecated."
"Make sure to rename your file to respectively `openvino_model.xml` and `openvino_model.bin`"
)
bin_file_name = file_name.replace(".xml", ".bin") if not from_onnx else None
model = cls.load_model(file_name, bin_file_name)
kwargs["model_save_dir"] = model_id
Expand All @@ -164,17 +171,37 @@ def _from_pretrained(
if not from_onnx:
model_file_names.append(file_name.replace(".xml", ".bin"))
file_names = []
for file_name in model_file_names:
model_cache_path = hf_hub_download(
repo_id=model_id,
filename=file_name,
use_auth_token=use_auth_token,
revision=revision,
cache_dir=cache_dir,
force_download=force_download,
local_files_only=local_files_only,
try:
for file_name in model_file_names:
model_cache_path = hf_hub_download(
repo_id=model_id,
filename=file_name,
use_auth_token=use_auth_token,
revision=revision,
cache_dir=cache_dir,
force_download=force_download,
local_files_only=local_files_only,
)
file_names.append(model_cache_path)
except EntryNotFoundError:
file_names = []
model_file_names = ["ov_model.xml", "ov_model.bin"]
for file_name in model_file_names:
model_cache_path = hf_hub_download(
repo_id=model_id,
filename=file_name,
use_auth_token=use_auth_token,
revision=revision,
cache_dir=cache_dir,
force_download=force_download,
local_files_only=local_files_only,
)
file_names.append(model_cache_path)
logger.warning(
"The file names `ov_model.xml` and `ov_model.bin` will be soon deprecated."
"Make sure to rename your file to respectively `openvino_model.xml` and `openvino_model.bin`"
)
file_names.append(model_cache_path)

kwargs["model_save_dir"] = Path(model_cache_path).parent
bin_file_name = file_names[1] if not from_onnx else None
model = cls.load_model(file_names[0], bin_file_name=bin_file_name)
Expand Down
64 changes: 50 additions & 14 deletions optimum/intel/openvino/modeling_base_seq2seq.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import openvino
from huggingface_hub import HfApi, hf_hub_download
from huggingface_hub.utils import EntryNotFoundError
from openvino.offline_transformations import compress_model_transformation
from optimum.onnx.configuration import DecoderOnnxConfig, EncoderOnnxConfig
from optimum.onnx.modeling_seq2seq import _DecoderWithLMhead
Expand Down Expand Up @@ -148,14 +149,14 @@ def _from_pretrained(
The path to a directory in which a downloaded pretrained model configuration should be cached if the
standard cache should not be used.
encoder_file_name(`str`, *optional*):
The encoder model file name. Overwrites the default file name ov_encoder_model.xml and allows one to
The encoder model file name. Overwrites the default file name openvino_encoder_model.xml and allows one to
load the encoder model with a different name.
decoder_file_name(`str`, *optional*):
The decoder model file name. Overwrites the default file name ov_decoder_model.xml and allows one to
The decoder model file name. Overwrites the default file name openvino_decoder_model.xml and allows one to
load the decoder model with a different name.
decoder_with_past_file_name(`str`, *optional*):
The decoder with past key values model file name overwriting the default file name ov_decoder_with_past_model.xml,
allowing to load the decoder model with a different name.
The decoder with past key values model file name overwriting the default file name
openvino_decoder_with_past_model.xml, allowing to load the decoder model with a different name.
local_files_only(`bool`, *optional*, defaults to `False`):
Whether or not to only look at local files (i.e., do not try to download the model).
"""
Expand All @@ -173,6 +174,16 @@ def _from_pretrained(

# Load model from a local directory
if os.path.isdir(model_id):
if os.path.isfile(os.path.join(model_id, "ov_encoder_model.xml")):
encoder_file_name = "ov_encoder_model.xml"
encoder_file_name = "ov_decoder_model.xml"
encoder_file_name = "ov_decoder_with_past_model.xml"
logger.warning(
"The file names `ov_encoder_model.xml`, `ov_decoder_model.xml` and `ov_decoder_with_past_model.xml` "
"will be soon deprecated. Make sure to rename your file to respectively `openvino_encoder_model.xml`, "
"`openvino_decoder_model.xml` and `openvino_decoder_with_past_model.xml`"
)

encoder_bin_file_name = (
os.path.join(model_id, encoder_file_name.replace(".xml", ".bin")) if not from_onnx else None
)
Expand Down Expand Up @@ -203,17 +214,42 @@ def _from_pretrained(
for key in list(model_file_names.keys()):
model_file_names[key + "_bin"] = model_file_names[key].replace(".xml", ".bin")
file_names = model_file_names.copy()
for name, file_name in model_file_names.items():
model_cache_path = hf_hub_download(
repo_id=model_id,
filename=file_name,
use_auth_token=use_auth_token,
revision=revision,
cache_dir=cache_dir,
force_download=force_download,
local_files_only=local_files_only,
try:
for name, file_name in model_file_names.items():
model_cache_path = hf_hub_download(
repo_id=model_id,
filename=file_name,
use_auth_token=use_auth_token,
revision=revision,
cache_dir=cache_dir,
force_download=force_download,
local_files_only=local_files_only,
)
file_names[name] = model_cache_path
except EntryNotFoundError:
model_file_names = {"encoder": "ov_encoder_model.xml", "decoder": "ov_decoder_model.xml"}
if use_cache:
model_file_names["decoder_with_past"] = "ov_decoder_with_past_model.xml"
for key in list(model_file_names.keys()):
model_file_names[key + "_bin"] = model_file_names[key].replace(".xml", ".bin")
file_names = model_file_names.copy()
for name, file_name in model_file_names.items():
model_cache_path = hf_hub_download(
repo_id=model_id,
filename=file_name,
use_auth_token=use_auth_token,
revision=revision,
cache_dir=cache_dir,
force_download=force_download,
local_files_only=local_files_only,
)
file_names[name] = model_cache_path
logger.warning(
"The file names `ov_encoder_model.xml`, `ov_decoder_model.xml` and `ov_decoder_with_past_model.xml` "
"will be soon deprecated. Make sure to rename your file to respectively `openvino_encoder_model.xml`, "
"`openvino_decoder_model.xml` and `openvino_decoder_with_past_model.xml`"
)
file_names[name] = model_cache_path

kwargs["model_save_dir"] = Path(model_cache_path).parent
encoder = cls.load_model(file_names["encoder"], bin_file_name=file_names.pop("encoder_bin", None))
decoder = cls.load_model(file_names["decoder"], bin_file_name=file_names.pop("decoder_bin", None))
Expand Down
8 changes: 4 additions & 4 deletions optimum/intel/openvino/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
# limitations under the License.


OV_XML_FILE_NAME = "ov_model.xml"
OV_ENCODER_NAME = "ov_encoder_model.xml"
OV_DECODER_NAME = "ov_decoder_model.xml"
OV_DECODER_WITH_PAST_NAME = "ov_decoder_with_past_model.xml"
OV_XML_FILE_NAME = "openvino_model.xml"
OV_ENCODER_NAME = "openvino_encoder_model.xml"
OV_DECODER_NAME = "openvino_decoder_model.xml"
OV_DECODER_WITH_PAST_NAME = "openvino_decoder_with_past_model.xml"

ONNX_WEIGHTS_NAME = "model.onnx"
ONNX_ENCODER_NAME = "encoder_model.onnx"
Expand Down

0 comments on commit 8e42197

Please sign in to comment.