-
Notifications
You must be signed in to change notification settings - Fork 126
/
Copy pathmodeling_base_seq2seq.py
321 lines (289 loc) · 13.7 KB
/
modeling_base_seq2seq.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# Copyright 2022 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
import os
from pathlib import Path
from tempfile import TemporaryDirectory
from typing import Dict, Optional, Union
import openvino
from huggingface_hub import hf_hub_download
from openvino._offline_transformations import apply_moc_transformations, compress_model_transformation
from transformers import PretrainedConfig
from transformers.file_utils import add_start_docstrings
from ...exporters.openvino import main_export
from ..utils.import_utils import is_transformers_version
from .modeling_base import OVBaseModel
from .utils import (
ONNX_DECODER_NAME,
ONNX_DECODER_WITH_PAST_NAME,
ONNX_ENCODER_NAME,
OV_DECODER_NAME,
OV_DECODER_WITH_PAST_NAME,
OV_ENCODER_NAME,
)
logger = logging.getLogger(__name__)
@add_start_docstrings(
"""
Base OVModelForSeq2SeqLM class.
""",
)
class OVBaseModelForSeq2SeqLM(OVBaseModel):
export_feature = "text2text-generation"
def __init__(
self,
encoder: openvino.runtime.Model,
decoder: openvino.runtime.Model,
decoder_with_past: openvino.runtime.Model = None,
config: PretrainedConfig = None,
device: str = "CPU",
dynamic_shapes: bool = True,
ov_config: Optional[Dict[str, str]] = None,
model_save_dir: Optional[Union[str, Path, TemporaryDirectory]] = None,
**kwargs,
):
self.config = config
self.use_cache = decoder_with_past is not None
self.model_save_dir = model_save_dir
self._device = device.upper()
self.is_dynamic = dynamic_shapes
self.ov_config = ov_config if ov_config is not None else {}
self.preprocessors = kwargs.get("preprocessors", [])
if self.is_dynamic:
encoder = self._reshape(encoder, -1, -1, is_decoder=False)
decoder = self._reshape(decoder, -1, -1)
decoder_with_past = self._reshape(decoder_with_past, -1, -1) if self.use_cache else None
self.encoder_model = encoder
self.decoder_model = decoder
self.decoder_with_past_model = decoder_with_past
if is_transformers_version("<=", "4.25.1"):
self.generation_config = None
else:
from transformers import GenerationConfig
self.generation_config = GenerationConfig.from_model_config(config) if self.can_generate() else None
def _save_pretrained(self, save_directory: Union[str, Path]):
"""
Saves the model to the OpenVINO IR format so that it can be re-loaded using the
[`~optimum.intel.openvino.modeling.OVModel.from_pretrained`] class method.
Arguments:
save_directory (`str` or `Path`):
The directory where to save the model files.
"""
src_files = [self.encoder_model, self.decoder_model]
dst_file_names = [OV_ENCODER_NAME, OV_DECODER_NAME]
if self.use_cache:
src_files.append(self.decoder_with_past_model)
dst_file_names.append(OV_DECODER_WITH_PAST_NAME)
for src_file, dst_file_name in zip(src_files, dst_file_names):
dst_path = os.path.join(save_directory, dst_file_name)
openvino.save_model(src_file, dst_path, compress_to_fp16=False)
@classmethod
def _from_pretrained(
cls,
model_id: Union[str, Path],
config: PretrainedConfig,
use_auth_token: Optional[Union[bool, str]] = None,
revision: Optional[str] = None,
force_download: bool = False,
cache_dir: Optional[str] = None,
encoder_file_name: Optional[str] = None,
decoder_file_name: Optional[str] = None,
decoder_with_past_file_name: Optional[str] = None,
local_files_only: bool = False,
use_cache: bool = True,
from_onnx: bool = False,
load_in_8bit: bool = False,
**kwargs,
):
"""
Loads a model and its configuration file from a directory or the HF Hub.
Arguments:
model_id (`str` or `Path`):
The directory from which to load the model.
Can be either:
- The model id of a pretrained model hosted inside a model repo on huggingface.co.
- The path to a directory containing the model weights.
use_auth_token (`str` or `bool`):
The token to use as HTTP bearer authorization for remote files. Needed to load models from a private
repository.
revision (`str`):
The specific model version to use. It can be a branch name, a tag name, or a commit id.
force_download (`bool`, *optional*, defaults to `False`):
Whether or not to force the (re-)download of the model weights and configuration files, overriding the
cached versions if they exist.
cache_dir (`Union[str, Path]`, *optional*):
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 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 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
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).
"""
default_encoder_file_name = ONNX_ENCODER_NAME if from_onnx else OV_ENCODER_NAME
default_decoder_file_name = ONNX_DECODER_NAME if from_onnx else OV_DECODER_NAME
default_decoder_with_past_file_name = ONNX_DECODER_WITH_PAST_NAME if from_onnx else OV_DECODER_WITH_PAST_NAME
encoder_file_name = encoder_file_name or default_encoder_file_name
decoder_file_name = decoder_file_name or default_decoder_file_name
decoder_with_past_file_name = decoder_with_past_file_name or default_decoder_with_past_file_name
decoder_with_past = None
# Load model from a local directory
if os.path.isdir(model_id):
encoder = cls.load_model(os.path.join(model_id, encoder_file_name), load_in_8bit)
decoder = cls.load_model(os.path.join(model_id, decoder_file_name), load_in_8bit)
if use_cache:
decoder_with_past = cls.load_model(os.path.join(model_id, decoder_with_past_file_name), load_in_8bit)
model_save_dir = Path(model_id)
# Load model from hub
else:
model_file_names = {"encoder": encoder_file_name, "decoder": decoder_file_name}
if use_cache:
model_file_names["decoder_with_past"] = decoder_with_past_file_name
# If not ONNX then OpenVINO IR : adds binary files
if not from_onnx:
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
model_save_dir = Path(model_cache_path).parent
encoder = cls.load_model(file_names["encoder"], load_in_8bit)
decoder = cls.load_model(file_names["decoder"], load_in_8bit)
if use_cache:
decoder_with_past = cls.load_model(file_names["decoder_with_past"], load_in_8bit)
return cls(
encoder=encoder,
decoder=decoder,
decoder_with_past=decoder_with_past,
config=config,
model_save_dir=model_save_dir,
**kwargs,
)
@classmethod
def _from_transformers(
cls,
model_id: str,
config: PretrainedConfig,
use_auth_token: Optional[Union[bool, str]] = None,
revision: Optional[str] = None,
force_download: bool = False,
cache_dir: Optional[str] = None,
subfolder: str = "",
local_files_only: bool = False,
task: Optional[str] = None,
use_cache: bool = True,
trust_remote_code: bool = False,
load_in_8bit: Optional[bool] = None,
**kwargs,
):
"""
Export a vanilla Transformers model into an ONNX model using `transformers.onnx.export_onnx`.
Arguments:
model_id (`str` or `Path`):
The directory from which to load the model.
Can be either:
- The model id of a pretrained model hosted inside a model repo on huggingface.co.
- The path to a directory containing the model weights.
save_dir (`str` or `Path`):
The directory where the exported ONNX model should be saved, defaults to
`transformers.file_utils.default_cache_path`, which is the cache directory for transformers.
use_auth_token (`str` or `bool`):
Is needed to load models from a private repository
revision (`str`):
Revision is the specific model version to use. It can be a branch name, a tag name, or a commit id
kwargs (`Dict`, *optional*):
kwargs will be passed to the model during initialization
"""
save_dir = TemporaryDirectory()
save_dir_path = Path(save_dir.name)
if task is None:
task = cls.export_feature
if use_cache:
task = task + "-with-past"
compression_option = None
if load_in_8bit is not None:
compression_option = "fp32"
main_export(
model_name_or_path=model_id,
output=save_dir_path,
task=task,
subfolder=subfolder,
revision=revision,
cache_dir=cache_dir,
use_auth_token=use_auth_token,
local_files_only=local_files_only,
force_download=force_download,
trust_remote_code=trust_remote_code,
compression_option=compression_option,
)
config.save_pretrained(save_dir_path)
return cls._from_pretrained(
model_id=save_dir_path, config=config, use_cache=use_cache, load_in_8bit=load_in_8bit, **kwargs
)
def _reshape(self, model: openvino.runtime.Model, batch_size: int, sequence_length: int, is_decoder=True):
shapes = {}
for inputs in model.inputs:
shapes[inputs] = inputs.get_partial_shape()
shapes[inputs][0] = batch_size if not is_decoder else -1
if inputs.get_any_name().startswith("past_key_values"):
shapes[inputs][2] = -1
elif is_decoder and not inputs.get_any_name().startswith("encoder"):
shapes[inputs][1] = -1
else:
shapes[inputs][1] = sequence_length
model.reshape(shapes)
return model
def reshape(self, batch_size: int, sequence_length: int):
"""
Propagates the given input shapes on the model's layers, fixing the inputs shapes of the model.
Arguments:
batch_size (`int`):
The batch size.
sequence_length (`int`):
The sequence length.
"""
logger.warning("Some part of the model's decoder do not support static shapes and will be kept dynamic.")
self.is_dynamic = True if batch_size == -1 and sequence_length == -1 else False
self.encoder_model = self._reshape(self.encoder_model, batch_size, sequence_length, is_decoder=False)
self.decoder_model = self._reshape(self.decoder_model, batch_size, sequence_length)
if self.use_cache:
self.decoder_with_past_model = self._reshape(self.decoder_with_past_model, batch_size, sequence_length)
def half(self):
"""
Converts all the model weights to FP16 for more efficient inference on GPU.
"""
apply_moc_transformations(self.encoder_model, cf=False)
apply_moc_transformations(self.decoder_model, cf=False)
compress_model_transformation(self.encoder_model)
compress_model_transformation(self.decoder_model)
if self.use_cache:
apply_moc_transformations(self.decoder_with_past_model, cf=False)
compress_model_transformation(self.decoder_with_past_model)
return self
def forward(self, *args, **kwargs):
raise NotImplementedError