optimum-rbln 0.1.12__py3-none-any.whl → 0.1.15__py3-none-any.whl
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/rbln/__init__.py +27 -13
- optimum/rbln/__version__.py +16 -1
- optimum/rbln/diffusers/__init__.py +22 -2
- optimum/rbln/diffusers/models/__init__.py +34 -3
- optimum/rbln/{transformers/generation → diffusers/models/autoencoders}/__init__.py +1 -2
- optimum/rbln/diffusers/models/{autoencoder_kl.py → autoencoders/autoencoder_kl.py} +66 -111
- optimum/rbln/diffusers/models/autoencoders/vae.py +84 -0
- optimum/rbln/diffusers/models/controlnet.py +85 -65
- optimum/rbln/diffusers/models/transformers/__init__.py +24 -0
- optimum/rbln/diffusers/models/transformers/transformer_sd3.py +203 -0
- optimum/rbln/diffusers/models/unets/__init__.py +24 -0
- optimum/rbln/diffusers/models/{unet_2d_condition.py → unets/unet_2d_condition.py} +129 -163
- optimum/rbln/diffusers/pipelines/__init__.py +60 -12
- optimum/rbln/diffusers/pipelines/controlnet/multicontrolnet.py +11 -25
- optimum/rbln/diffusers/pipelines/controlnet/pipeline_controlnet.py +9 -185
- optimum/rbln/diffusers/pipelines/controlnet/pipeline_controlnet_img2img.py +9 -190
- optimum/rbln/diffusers/pipelines/controlnet/pipeline_controlnet_sd_xl.py +9 -191
- optimum/rbln/diffusers/pipelines/controlnet/pipeline_controlnet_sd_xl_img2img.py +9 -192
- optimum/rbln/diffusers/pipelines/stable_diffusion/__init__.py +1 -0
- optimum/rbln/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion.py +4 -110
- optimum/rbln/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_img2img.py +4 -118
- optimum/rbln/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_inpaint.py +32 -0
- optimum/rbln/diffusers/pipelines/stable_diffusion_3/__init__.py +26 -0
- optimum/rbln/diffusers/pipelines/stable_diffusion_3/pipeline_stable_diffusion_3.py +32 -0
- optimum/rbln/diffusers/pipelines/stable_diffusion_3/pipeline_stable_diffusion_3_img2img.py +32 -0
- optimum/rbln/diffusers/pipelines/stable_diffusion_3/pipeline_stable_diffusion_3_inpaint.py +32 -0
- optimum/rbln/diffusers/pipelines/stable_diffusion_xl/__init__.py +1 -0
- optimum/rbln/diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl.py +18 -128
- optimum/rbln/diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl_img2img.py +18 -131
- optimum/rbln/diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl_inpaint.py +32 -0
- optimum/rbln/modeling.py +572 -0
- optimum/rbln/modeling_alias.py +1 -1
- optimum/rbln/modeling_base.py +176 -763
- optimum/rbln/modeling_diffusers.py +329 -0
- optimum/rbln/transformers/__init__.py +2 -2
- optimum/rbln/transformers/cache_utils.py +5 -9
- optimum/rbln/transformers/modeling_rope_utils.py +283 -0
- optimum/rbln/transformers/models/__init__.py +80 -31
- optimum/rbln/transformers/models/auto/auto_factory.py +117 -23
- optimum/rbln/transformers/models/auto/modeling_auto.py +37 -12
- optimum/rbln/transformers/models/bart/modeling_bart.py +3 -6
- optimum/rbln/transformers/models/bert/modeling_bert.py +3 -6
- optimum/rbln/transformers/models/clip/modeling_clip.py +8 -34
- optimum/rbln/transformers/models/decoderonly/__init__.py +0 -5
- optimum/rbln/transformers/models/decoderonly/decoderonly_architecture.py +779 -361
- optimum/rbln/transformers/models/decoderonly/modeling_decoderonly.py +83 -142
- optimum/rbln/transformers/models/dpt/modeling_dpt.py +1 -1
- optimum/rbln/transformers/models/exaone/exaone_architecture.py +64 -39
- optimum/rbln/transformers/models/exaone/modeling_exaone.py +6 -29
- optimum/rbln/transformers/models/gemma/gemma_architecture.py +31 -92
- optimum/rbln/transformers/models/gemma/modeling_gemma.py +4 -28
- optimum/rbln/transformers/models/gpt2/gpt2_architecture.py +50 -238
- optimum/rbln/transformers/models/gpt2/modeling_gpt2.py +6 -31
- optimum/rbln/transformers/models/llama/modeling_llama.py +4 -28
- optimum/rbln/transformers/models/llava_next/modeling_llava_next.py +29 -83
- optimum/rbln/transformers/models/midm/midm_architecture.py +88 -253
- optimum/rbln/transformers/models/midm/modeling_midm.py +8 -33
- optimum/rbln/transformers/models/mistral/modeling_mistral.py +4 -29
- optimum/rbln/transformers/models/phi/modeling_phi.py +5 -31
- optimum/rbln/transformers/models/phi/phi_architecture.py +61 -345
- optimum/rbln/transformers/models/qwen2/modeling_qwen2.py +5 -29
- optimum/rbln/transformers/models/seq2seq/modeling_seq2seq.py +1 -46
- optimum/rbln/transformers/models/t5/__init__.py +1 -1
- optimum/rbln/transformers/models/t5/modeling_t5.py +157 -6
- optimum/rbln/transformers/models/wav2vec2/modeling_wav2vec2.py +1 -1
- optimum/rbln/transformers/models/whisper/modeling_whisper.py +2 -2
- optimum/rbln/transformers/models/xlm_roberta/modeling_xlm_roberta.py +3 -35
- optimum/rbln/transformers/utils/rbln_quantization.py +128 -5
- optimum/rbln/utils/decorator_utils.py +59 -0
- optimum/rbln/utils/hub.py +131 -0
- optimum/rbln/utils/import_utils.py +21 -0
- optimum/rbln/utils/model_utils.py +53 -0
- optimum/rbln/utils/runtime_utils.py +5 -5
- optimum/rbln/utils/submodule.py +114 -0
- optimum/rbln/utils/timer_utils.py +2 -2
- optimum_rbln-0.1.15.dist-info/METADATA +106 -0
- optimum_rbln-0.1.15.dist-info/RECORD +110 -0
- {optimum_rbln-0.1.12.dist-info → optimum_rbln-0.1.15.dist-info}/WHEEL +1 -1
- optimum/rbln/transformers/generation/streamers.py +0 -139
- optimum/rbln/transformers/generation/utils.py +0 -397
- optimum/rbln/transformers/models/exaone/hf_hub_cached/configuration_exaone.py +0 -181
- optimum/rbln/transformers/models/exaone/hf_hub_cached/modeling_exaone.py +0 -1725
- optimum/rbln/transformers/models/midm/hf_hub_cached/configuration_midm.py +0 -22
- optimum/rbln/transformers/models/midm/hf_hub_cached/midm_bitext_tokenization.py +0 -304
- optimum/rbln/transformers/models/midm/hf_hub_cached/modeling_midm.py +0 -1469
- optimum/rbln/transformers/models/midm/hf_hub_cached/rotary_position_embedding.py +0 -98
- optimum_rbln-0.1.12.dist-info/METADATA +0 -119
- optimum_rbln-0.1.12.dist-info/RECORD +0 -103
- optimum_rbln-0.1.12.dist-info/entry_points.txt +0 -4
- {optimum_rbln-0.1.12.dist-info → optimum_rbln-0.1.15.dist-info}/licenses/LICENSE +0 -0
optimum/rbln/modeling_base.py
CHANGED
@@ -21,8 +21,8 @@
|
|
21
21
|
# copied, modified, or distributed without prior written permission
|
22
22
|
# from Rebellions Inc.
|
23
23
|
|
24
|
+
|
24
25
|
import importlib
|
25
|
-
import inspect
|
26
26
|
import logging
|
27
27
|
import os
|
28
28
|
import shutil
|
@@ -33,143 +33,31 @@ from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union
|
|
33
33
|
|
34
34
|
import rebel
|
35
35
|
import torch
|
36
|
-
import transformers
|
37
|
-
from huggingface_hub import HfApi, HfFolder, hf_hub_download
|
38
|
-
from optimum.exporters import TasksManager
|
39
|
-
from optimum.modeling_base import OptimizedModel
|
40
36
|
from transformers import (
|
41
37
|
AutoConfig,
|
42
38
|
AutoModel,
|
43
|
-
AutoModelForAudioClassification,
|
44
|
-
AutoModelForImageClassification,
|
45
|
-
AutoModelForMaskedLM,
|
46
|
-
AutoModelForQuestionAnswering,
|
47
|
-
AutoModelForSequenceClassification,
|
48
39
|
GenerationConfig,
|
49
40
|
PretrainedConfig,
|
50
41
|
)
|
51
42
|
|
52
|
-
from .modeling_config import
|
43
|
+
from .modeling_config import RBLNCompileConfig, RBLNConfig, use_rbln_config
|
44
|
+
from .utils.hub import PushToHubMixin, pull_compiled_model_from_hub, validate_files
|
53
45
|
from .utils.runtime_utils import UnavailableRuntime
|
54
46
|
from .utils.save_utils import maybe_load_preprocessors
|
47
|
+
from .utils.submodule import SubModulesMixin
|
55
48
|
|
56
49
|
|
57
50
|
if TYPE_CHECKING:
|
58
|
-
from transformers import
|
59
|
-
AutoFeatureExtractor,
|
60
|
-
AutoProcessor,
|
61
|
-
AutoTokenizer,
|
62
|
-
PreTrainedModel,
|
63
|
-
)
|
51
|
+
from transformers import PreTrainedModel
|
64
52
|
|
65
53
|
logger = logging.getLogger(__name__)
|
66
54
|
|
67
55
|
|
68
|
-
class
|
69
|
-
|
70
|
-
_rbln_submodules = [
|
71
|
-
{"name": "vision_tower"},
|
72
|
-
{"name": "language_model"},
|
73
|
-
]
|
74
|
-
"""
|
75
|
-
|
76
|
-
_rbln_submodules: List[Dict[str, Any]] = []
|
77
|
-
|
78
|
-
def __init__(
|
79
|
-
self,
|
80
|
-
*,
|
81
|
-
rbln_submodules: List["RBLNBaseModel"] = [],
|
82
|
-
**kwargs,
|
83
|
-
) -> None:
|
84
|
-
for submodule_meta, submodule in zip(self._rbln_submodules, rbln_submodules):
|
85
|
-
setattr(self, submodule_meta["name"], submodule)
|
86
|
-
|
87
|
-
@classmethod
|
88
|
-
def _export_submodules_from_model(
|
89
|
-
cls,
|
90
|
-
model: "PreTrainedModel",
|
91
|
-
model_save_dir: str,
|
92
|
-
rbln_kwargs: Dict[str, Any],
|
93
|
-
**kwargs,
|
94
|
-
) -> List["RBLNBaseModel"]:
|
95
|
-
rbln_submodules = []
|
96
|
-
for submodule in cls._rbln_submodules:
|
97
|
-
submodule_name = submodule["name"]
|
98
|
-
torch_submodule: "PreTrainedModel" = getattr(model, submodule["name"])
|
99
|
-
cls_name = torch_submodule.__class__.__name__
|
100
|
-
submodule_cls: "RBLNModel" = getattr(importlib.import_module("optimum.rbln"), f"RBLN{cls_name}")
|
101
|
-
|
102
|
-
if submodule_name in rbln_kwargs:
|
103
|
-
kwargs["rbln_config"] = rbln_kwargs[submodule_name]
|
104
|
-
|
105
|
-
rbln_submodule = submodule_cls.from_model(
|
106
|
-
model=torch_submodule,
|
107
|
-
subfolder=submodule_name,
|
108
|
-
model_save_dir=model_save_dir,
|
109
|
-
**kwargs,
|
110
|
-
)
|
111
|
-
|
112
|
-
rbln_submodules.append(rbln_submodule)
|
113
|
-
|
114
|
-
return rbln_submodules
|
115
|
-
|
116
|
-
@classmethod
|
117
|
-
def _load_submodules_from_compiled_models(
|
118
|
-
cls,
|
119
|
-
model_save_dir: str,
|
120
|
-
rbln_kwargs: Dict[str, Any],
|
121
|
-
**kwargs,
|
122
|
-
):
|
123
|
-
rbln_submodules = []
|
124
|
-
for submodule in cls._rbln_submodules:
|
125
|
-
submodule_name = submodule["name"]
|
126
|
-
|
127
|
-
if submodule_name in rbln_kwargs:
|
128
|
-
kwargs["rbln_config"] = rbln_kwargs[submodule_name]
|
129
|
-
|
130
|
-
# Get cls name for call the constructor of the rbln class
|
131
|
-
submodule_rbln_config = RBLNConfig.load(Path(model_save_dir) / submodule_name)
|
132
|
-
submodule_cls_name = submodule_rbln_config.meta["cls"]
|
133
|
-
submodule_cls: "RBLNBaseModel" = getattr(importlib.import_module("optimum.rbln"), submodule_cls_name)
|
134
|
-
|
135
|
-
config = OptimizedModel._load_config(Path(model_save_dir) / submodule_name)
|
136
|
-
rbln_submodule = submodule_cls._from_pretrained(
|
137
|
-
model_id=model_save_dir,
|
138
|
-
config=config,
|
139
|
-
subfolder=submodule_name,
|
140
|
-
**kwargs,
|
141
|
-
)
|
142
|
-
rbln_submodules.append(rbln_submodule)
|
143
|
-
return rbln_submodules
|
144
|
-
|
145
|
-
@classmethod
|
146
|
-
def _load_submodules(
|
147
|
-
cls,
|
148
|
-
model_save_dir,
|
149
|
-
rbln_kwargs,
|
150
|
-
model=None,
|
151
|
-
**kwargs,
|
152
|
-
):
|
153
|
-
# Two ways :
|
154
|
-
# 1. Compile from pytorch object
|
155
|
-
# 2. Load from compiled file
|
156
|
-
if model is not None:
|
157
|
-
return cls._export_submodules_from_model(
|
158
|
-
model=model,
|
159
|
-
model_save_dir=model_save_dir,
|
160
|
-
rbln_kwargs=rbln_kwargs,
|
161
|
-
**kwargs,
|
162
|
-
)
|
163
|
-
|
164
|
-
else:
|
165
|
-
return cls._load_submodules_from_compiled_models(
|
166
|
-
model_save_dir=model_save_dir,
|
167
|
-
rbln_kwargs=rbln_kwargs,
|
168
|
-
**kwargs,
|
169
|
-
)
|
56
|
+
class PreTrainedModel(ABC): # noqa: F811
|
57
|
+
pass
|
170
58
|
|
171
59
|
|
172
|
-
class RBLNBaseModel(
|
60
|
+
class RBLNBaseModel(SubModulesMixin, PushToHubMixin, PreTrainedModel):
|
173
61
|
"""
|
174
62
|
An abstract base class for compiling, loading, and saving neural network models from the huggingface
|
175
63
|
transformers and diffusers libraries to run on RBLN NPU devices.
|
@@ -205,30 +93,29 @@ class RBLNBaseModel(OptimizedModel, ABC, SubModulesMixin):
|
|
205
93
|
"""
|
206
94
|
|
207
95
|
model_type = "rbln_model"
|
208
|
-
auto_model_class = AutoModel
|
96
|
+
auto_model_class = AutoModel
|
97
|
+
config_class = AutoConfig
|
209
98
|
config_name = "config.json"
|
99
|
+
hf_library_name = "transformers"
|
100
|
+
_hf_class = None
|
210
101
|
|
211
102
|
def __init__(
|
212
103
|
self,
|
213
104
|
models: List[rebel.Runtime],
|
214
105
|
config: "PretrainedConfig",
|
215
106
|
rbln_config: RBLNConfig,
|
216
|
-
preprocessors: Optional[List],
|
217
107
|
model_save_dir: Optional[Union[str, Path, TemporaryDirectory]] = None,
|
218
108
|
subfolder: str = "",
|
219
109
|
rbln_compiled_models: Optional[rebel.RBLNCompiledModel] = None,
|
220
110
|
rbln_submodules: List["RBLNBaseModel"] = [],
|
221
111
|
**kwargs,
|
222
112
|
):
|
223
|
-
|
224
|
-
|
225
|
-
self.config = PretrainedConfig(**self.config)
|
226
|
-
|
113
|
+
self.model = models
|
114
|
+
self.config = config
|
227
115
|
self.rbln_config = rbln_config
|
228
|
-
self.preprocessors = [] if preprocessors is None else preprocessors
|
229
116
|
self.compiled_models = rbln_compiled_models
|
230
117
|
|
231
|
-
# Registers the
|
118
|
+
# Registers the RBLN classes into the transformers AutoModel classes to avoid warnings when creating
|
232
119
|
# a pipeline https://github.com/huggingface/transformers/blob/3d3204c025b6b5de013e07dd364208e28b4d9589/src/transformers/pipelines/base.py#L940
|
233
120
|
AutoConfig.register(self.model_type, AutoConfig)
|
234
121
|
if hasattr(self.auto_model_class, "register"):
|
@@ -247,6 +134,7 @@ class RBLNBaseModel(OptimizedModel, ABC, SubModulesMixin):
|
|
247
134
|
|
248
135
|
self.device = torch.device("cpu")
|
249
136
|
self.training = False
|
137
|
+
self.dtype = torch.float32
|
250
138
|
|
251
139
|
# FIXME :: model_save_dir is not used after initialized. (This can be used when save/load)
|
252
140
|
# This attribute is needed to keep one reference on the temporary directory, since garbage collecting it
|
@@ -264,34 +152,6 @@ class RBLNBaseModel(OptimizedModel, ABC, SubModulesMixin):
|
|
264
152
|
self.rbln_submodules = rbln_submodules
|
265
153
|
self.__post_init__(**kwargs)
|
266
154
|
|
267
|
-
def _save_pretrained(self, save_directory: Union[str, Path]):
|
268
|
-
"""
|
269
|
-
Saves a model and its configuration file to a directory, so that it can be re-loaded using the
|
270
|
-
[`~optimum.rbln.modeling_base.RBLNBaseModel.from_pretrained`] class method.
|
271
|
-
|
272
|
-
Args:
|
273
|
-
save_directory (`Union[str, Path]`):
|
274
|
-
Directory where to save the model file.
|
275
|
-
"""
|
276
|
-
real_save_dir = self.model_save_dir / self.subfolder
|
277
|
-
save_directory_path = Path(save_directory)
|
278
|
-
if os.path.exists(real_save_dir) and os.path.isdir(real_save_dir):
|
279
|
-
if save_directory_path.absolute() == real_save_dir.absolute():
|
280
|
-
raise FileExistsError(
|
281
|
-
f"Cannot save model to '{save_directory}'. "
|
282
|
-
f"This directory already exists and contains the model files."
|
283
|
-
)
|
284
|
-
shutil.copytree(real_save_dir, save_directory, dirs_exist_ok=True)
|
285
|
-
self.config.save_pretrained(save_directory)
|
286
|
-
if self.generation_config is not None:
|
287
|
-
self.generation_config.save_pretrained(save_directory)
|
288
|
-
else:
|
289
|
-
raise FileNotFoundError(
|
290
|
-
f"Unable to save the model. The model directory '{real_save_dir}' does not exist or is not accessible. "
|
291
|
-
f"Cannot save to the specified destination '{save_directory}'. "
|
292
|
-
f"Please ensure the model directory exists and you have the necessary permissions to access it."
|
293
|
-
)
|
294
|
-
|
295
155
|
@classmethod
|
296
156
|
def _load_compiled_model_dir(
|
297
157
|
cls,
|
@@ -302,50 +162,18 @@ class RBLNBaseModel(OptimizedModel, ABC, SubModulesMixin):
|
|
302
162
|
cache_dir: Optional[str] = None,
|
303
163
|
subfolder: str = "",
|
304
164
|
local_files_only: bool = False,
|
305
|
-
):
|
306
|
-
|
307
|
-
# And prepare or download cache folder from HF Hub if needed.
|
165
|
+
) -> str:
|
166
|
+
"""Load the directory containing the compiled model files."""
|
308
167
|
model_path = Path(model_id)
|
168
|
+
|
309
169
|
if model_path.is_dir():
|
310
170
|
model_path = model_path / subfolder
|
311
171
|
rbln_files = list(model_path.glob("*.rbln"))
|
312
172
|
rbln_config_filenames = list(model_path.glob("rbln_config.json"))
|
173
|
+
validate_files(rbln_files, rbln_config_filenames, f"directory {model_path}")
|
313
174
|
else:
|
314
|
-
|
315
|
-
|
316
|
-
else:
|
317
|
-
token = use_auth_token
|
318
|
-
repo_files = list(
|
319
|
-
map(
|
320
|
-
Path,
|
321
|
-
HfApi().list_repo_files(model_id, revision=revision, token=token),
|
322
|
-
)
|
323
|
-
)
|
324
|
-
|
325
|
-
pattern = "*.rbln" if subfolder == "" else f"{subfolder}/*.rbln"
|
326
|
-
rbln_files = [p for p in repo_files if p.match(pattern)]
|
327
|
-
|
328
|
-
pattern = "rbln_config.json" if subfolder == "" else f"{subfolder}/rbln_config.json"
|
329
|
-
rbln_config_filenames = [p for p in repo_files if p.match(pattern)]
|
330
|
-
|
331
|
-
if len(rbln_files) == 0:
|
332
|
-
raise FileNotFoundError(f"Could not find any rbln model file in {model_path}")
|
333
|
-
|
334
|
-
if len(rbln_config_filenames) == 0:
|
335
|
-
raise FileNotFoundError(f"Could not find `rbln_config.json` file in {model_path}")
|
336
|
-
|
337
|
-
if len(rbln_config_filenames) > 1:
|
338
|
-
raise FileExistsError(
|
339
|
-
f"Multiple rbln_config.json are not expected. but {len(rbln_config_filenames)} are found."
|
340
|
-
)
|
341
|
-
|
342
|
-
if model_path.is_dir():
|
343
|
-
model_path = str(model_path)
|
344
|
-
else:
|
345
|
-
rbln_config_filename = rbln_config_filenames[0]
|
346
|
-
rbln_config_cache_path = hf_hub_download(
|
347
|
-
repo_id=model_id,
|
348
|
-
filename=str(rbln_config_filename),
|
175
|
+
model_path = pull_compiled_model_from_hub(
|
176
|
+
model_id=model_id,
|
349
177
|
subfolder=subfolder,
|
350
178
|
use_auth_token=use_auth_token,
|
351
179
|
revision=revision,
|
@@ -353,9 +181,8 @@ class RBLNBaseModel(OptimizedModel, ABC, SubModulesMixin):
|
|
353
181
|
force_download=force_download,
|
354
182
|
local_files_only=local_files_only,
|
355
183
|
)
|
356
|
-
model_path = Path(rbln_config_cache_path).parent
|
357
184
|
|
358
|
-
return model_path
|
185
|
+
return str(model_path)
|
359
186
|
|
360
187
|
@classmethod
|
361
188
|
def _load_compiled_models(cls, model_path: str):
|
@@ -368,13 +195,14 @@ class RBLNBaseModel(OptimizedModel, ABC, SubModulesMixin):
|
|
368
195
|
def _from_pretrained(
|
369
196
|
cls,
|
370
197
|
model_id: Union[str, Path],
|
371
|
-
config: "PretrainedConfig",
|
198
|
+
config: "PretrainedConfig" = None,
|
372
199
|
use_auth_token: Optional[Union[bool, str]] = None,
|
373
200
|
revision: Optional[str] = None,
|
374
201
|
force_download: bool = False,
|
375
202
|
cache_dir: Optional[str] = None,
|
376
203
|
subfolder: str = "",
|
377
204
|
local_files_only: bool = False,
|
205
|
+
trust_remote_code: bool = False,
|
378
206
|
model_save_dir: Optional[Union[str, Path, TemporaryDirectory]] = None,
|
379
207
|
# passed from compile function
|
380
208
|
rbln_config: Optional[RBLNConfig] = None,
|
@@ -383,7 +211,6 @@ class RBLNBaseModel(OptimizedModel, ABC, SubModulesMixin):
|
|
383
211
|
**kwargs,
|
384
212
|
) -> "RBLNBaseModel":
|
385
213
|
from_export_method = isinstance(rbln_config, RBLNConfig) and rbln_compiled_models is not None
|
386
|
-
|
387
214
|
if not from_export_method:
|
388
215
|
# from compiled dir
|
389
216
|
rbln_kwargs = rbln_config or {}
|
@@ -401,6 +228,43 @@ class RBLNBaseModel(OptimizedModel, ABC, SubModulesMixin):
|
|
401
228
|
rbln_config = RBLNConfig.load(model_path_subfolder)
|
402
229
|
rbln_config.update_runtime_cfg(rbln_kwargs)
|
403
230
|
|
231
|
+
if rbln_config.meta["cls"] != cls.__name__:
|
232
|
+
raise NameError(
|
233
|
+
f"Cannot load the model. The model was originally compiled using "
|
234
|
+
f"{rbln_config.meta['cls']}, but you are trying to load it with {cls.__name__}."
|
235
|
+
"Please use the same model class that was used during compilation."
|
236
|
+
)
|
237
|
+
|
238
|
+
if config is None:
|
239
|
+
if cls.hf_library_name == "transformers":
|
240
|
+
config = AutoConfig.from_pretrained(
|
241
|
+
model_path_subfolder,
|
242
|
+
cache_dir=cache_dir,
|
243
|
+
force_download=force_download,
|
244
|
+
revision=revision,
|
245
|
+
token=use_auth_token,
|
246
|
+
trust_remote_code=trust_remote_code,
|
247
|
+
)
|
248
|
+
elif cls.hf_library_name == "diffusers":
|
249
|
+
# import here to prevent diffusers dependency
|
250
|
+
# TODO(jongho): Remove diffusers dependency if use transformers only.
|
251
|
+
from diffusers.configuration_utils import ConfigMixin
|
252
|
+
|
253
|
+
class DummyConfigMixin(ConfigMixin):
|
254
|
+
# Just to load config, We need to specify `config_name`
|
255
|
+
config_name = "config.json"
|
256
|
+
|
257
|
+
config = DummyConfigMixin.load_config(
|
258
|
+
model_id,
|
259
|
+
cache_dir=cache_dir,
|
260
|
+
force_download=force_download,
|
261
|
+
local_files_only=local_files_only,
|
262
|
+
revision=revision,
|
263
|
+
token=use_auth_token,
|
264
|
+
subfolder=subfolder,
|
265
|
+
)
|
266
|
+
config = PretrainedConfig(**config)
|
267
|
+
|
404
268
|
rbln_compiled_models = cls._load_compiled_models(model_path_subfolder)
|
405
269
|
|
406
270
|
if len(cls._rbln_submodules) > 0:
|
@@ -440,24 +304,29 @@ class RBLNBaseModel(OptimizedModel, ABC, SubModulesMixin):
|
|
440
304
|
):
|
441
305
|
if isinstance(model_save_dir, str):
|
442
306
|
model_save_dir = Path(model_save_dir)
|
443
|
-
preprocessors = maybe_load_preprocessors(model_save_dir.name, subfolder=subfolder)
|
444
307
|
|
445
308
|
# FIXME:: Should we convert it?
|
446
309
|
compiled_model_names = [cfg.compiled_model_name for cfg in rbln_config.compile_cfgs]
|
447
310
|
rbln_compiled_models = [rbln_compiled_models[cm_name] for cm_name in compiled_model_names]
|
448
311
|
|
449
312
|
# create runtimes only if `rbln_create_runtimes` is enabled
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
|
313
|
+
try:
|
314
|
+
models = (
|
315
|
+
cls._create_runtimes(rbln_compiled_models, rbln_config.device_map)
|
316
|
+
if rbln_config.create_runtimes
|
317
|
+
else UnavailableRuntime()
|
318
|
+
)
|
319
|
+
|
320
|
+
except rebel.core.exception.RBLNRuntimeError as e:
|
321
|
+
logger.warning(
|
322
|
+
f"Failed to create the runtime for the model due to a runtime error: {e.__class__.__name__} - {e}"
|
323
|
+
)
|
324
|
+
models = UnavailableRuntime()
|
455
325
|
|
456
326
|
return cls(
|
457
327
|
models,
|
458
328
|
config,
|
459
329
|
rbln_config,
|
460
|
-
preprocessors,
|
461
330
|
model_save_dir=model_save_dir,
|
462
331
|
subfolder=subfolder,
|
463
332
|
rbln_compiled_models=(None if rbln_config.optimize_host_memory else rbln_compiled_models),
|
@@ -465,8 +334,43 @@ class RBLNBaseModel(OptimizedModel, ABC, SubModulesMixin):
|
|
465
334
|
**kwargs,
|
466
335
|
)
|
467
336
|
|
468
|
-
|
469
|
-
|
337
|
+
@classmethod
|
338
|
+
@use_rbln_config
|
339
|
+
def _export(
|
340
|
+
cls,
|
341
|
+
model_id: Union[str, Path],
|
342
|
+
rbln_config: Optional[Dict[str, Any]] = None,
|
343
|
+
**kwargs,
|
344
|
+
) -> "RBLNBaseModel":
|
345
|
+
subfolder = kwargs.get("subfolder", "")
|
346
|
+
model_save_dir = kwargs.pop("model_save_dir", None)
|
347
|
+
|
348
|
+
rbln_kwargs = rbln_config
|
349
|
+
model: "PreTrainedModel" = cls.get_pytorch_model(
|
350
|
+
model_id=model_id,
|
351
|
+
rbln_kwargs=rbln_kwargs,
|
352
|
+
**kwargs,
|
353
|
+
)
|
354
|
+
preprocessors = maybe_load_preprocessors(model_id, subfolder=subfolder)
|
355
|
+
return cls.from_model(
|
356
|
+
model,
|
357
|
+
rbln_config=rbln_config,
|
358
|
+
preprocessors=preprocessors,
|
359
|
+
model_save_dir=model_save_dir,
|
360
|
+
**kwargs,
|
361
|
+
)
|
362
|
+
|
363
|
+
@classmethod
|
364
|
+
def from_pretrained(
|
365
|
+
cls,
|
366
|
+
model_id: Union[str, Path],
|
367
|
+
export: bool = False,
|
368
|
+
**kwargs,
|
369
|
+
) -> "RBLNBaseModel":
|
370
|
+
if isinstance(model_id, Path):
|
371
|
+
model_id = model_id.as_posix()
|
372
|
+
from_pretrained_method = cls._export if export else cls._from_pretrained
|
373
|
+
return from_pretrained_method(model_id=model_id, **kwargs)
|
470
374
|
|
471
375
|
@classmethod
|
472
376
|
def compile(cls, model, rbln_compile_config: Optional[RBLNCompileConfig] = None):
|
@@ -493,6 +397,24 @@ class RBLNBaseModel(OptimizedModel, ABC, SubModulesMixin):
|
|
493
397
|
rbln_config = cls._get_rbln_config(**others, rbln_kwargs=rbln_kwargs)
|
494
398
|
return rbln_config
|
495
399
|
|
400
|
+
@classmethod
|
401
|
+
@property
|
402
|
+
def hf_class(cls):
|
403
|
+
"""
|
404
|
+
Lazily loads and caches the corresponding Hugging Face model class.
|
405
|
+
Removes 'RBLN' prefix from the class name to get the original class name
|
406
|
+
(e.g., RBLNLlamaForCausalLM -> LlamaForCausalLM) and imports it from
|
407
|
+
the transformers/diffusers module.
|
408
|
+
|
409
|
+
Returns:
|
410
|
+
type: The original Hugging Face model class
|
411
|
+
"""
|
412
|
+
if cls._hf_class is None:
|
413
|
+
hf_cls_name = cls.__name__[4:]
|
414
|
+
library = importlib.import_module(cls.hf_library_name)
|
415
|
+
cls._hf_class = getattr(library, hf_cls_name, None)
|
416
|
+
return cls._hf_class
|
417
|
+
|
496
418
|
def can_generate(self):
|
497
419
|
return False
|
498
420
|
|
@@ -502,31 +424,62 @@ class RBLNBaseModel(OptimizedModel, ABC, SubModulesMixin):
|
|
502
424
|
def __call__(self, *args, **kwargs):
|
503
425
|
return self.forward(*args, **kwargs)
|
504
426
|
|
427
|
+
def __repr__(self):
|
428
|
+
return repr(self.model) + repr(self.rbln_submodules)
|
429
|
+
|
505
430
|
def __post_init__(self, **kwargs):
|
506
|
-
|
431
|
+
pass
|
507
432
|
|
508
|
-
|
509
|
-
|
433
|
+
def save_pretrained(
|
434
|
+
self,
|
435
|
+
save_directory: Union[str, Path],
|
436
|
+
push_to_hub: bool = False,
|
437
|
+
**kwargs,
|
438
|
+
):
|
510
439
|
"""
|
511
|
-
|
512
|
-
|
440
|
+
Saves a model and its configuration file to a directory, so that it can be re-loaded using the
|
441
|
+
[`~optimum.rbln.modeling_base.RBLNBaseModel.from_pretrained`] class method.
|
442
|
+
|
443
|
+
Args:
|
444
|
+
save_directory (`Union[str, Path]`):
|
445
|
+
Directory where to save the model file.
|
446
|
+
push_to_hub (`bool`, *optional*, defaults to `False`):
|
447
|
+
Whether or not to push your model to the Hugging Face model hub after saving it.
|
448
|
+
|
513
449
|
"""
|
514
|
-
|
450
|
+
if os.path.isfile(save_directory):
|
451
|
+
logger.error(f"Provided path ({save_directory}) should be a directory, not a file")
|
452
|
+
return
|
515
453
|
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
|
454
|
+
os.makedirs(save_directory, exist_ok=True)
|
455
|
+
|
456
|
+
real_save_dir = self.model_save_dir / self.subfolder
|
457
|
+
save_directory_path = Path(save_directory)
|
458
|
+
if os.path.exists(real_save_dir) and os.path.isdir(real_save_dir):
|
459
|
+
if save_directory_path.absolute() == real_save_dir.absolute():
|
460
|
+
raise FileExistsError(
|
461
|
+
f"Cannot save model to '{save_directory}'. "
|
462
|
+
f"This directory already exists and contains the model files."
|
463
|
+
)
|
464
|
+
shutil.copytree(real_save_dir, save_directory, dirs_exist_ok=True)
|
465
|
+
self.config.save_pretrained(save_directory)
|
466
|
+
if self.generation_config is not None:
|
467
|
+
self.generation_config.save_pretrained(save_directory)
|
468
|
+
else:
|
469
|
+
raise FileNotFoundError(
|
470
|
+
f"Unable to save the model. The model directory '{real_save_dir}' does not exist or is not accessible. "
|
471
|
+
f"Cannot save to the specified destination '{save_directory}'. "
|
472
|
+
f"Please ensure the model directory exists and you have the necessary permissions to access it."
|
473
|
+
)
|
474
|
+
|
475
|
+
if push_to_hub:
|
476
|
+
return super().push_to_hub(save_directory, **kwargs)
|
520
477
|
|
521
478
|
@classmethod
|
522
479
|
@abstractmethod
|
523
480
|
def _get_rbln_config(cls, **rbln_config_kwargs) -> RBLNConfig:
|
524
481
|
pass
|
525
482
|
|
526
|
-
@abstractmethod
|
527
|
-
def forward(self, *args: List[torch.Tensor], **kwargs: Dict[str, torch.Tensor]):
|
528
|
-
pass
|
529
|
-
|
530
483
|
@classmethod
|
531
484
|
@abstractmethod
|
532
485
|
def _create_runtimes(
|
@@ -555,546 +508,6 @@ class RBLNBaseModel(OptimizedModel, ABC, SubModulesMixin):
|
|
555
508
|
):
|
556
509
|
pass
|
557
510
|
|
558
|
-
@
|
559
|
-
@use_rbln_config
|
560
|
-
def _export(
|
561
|
-
cls,
|
562
|
-
model_id: Union[str, Path],
|
563
|
-
config: "PretrainedConfig", # FIXME : optimum passes config, but we ignore it.
|
564
|
-
rbln_config: Optional[Dict[str, Any]] = None,
|
565
|
-
**kwargs,
|
566
|
-
) -> "RBLNModel":
|
567
|
-
subfolder = kwargs.get("subfolder", "")
|
568
|
-
model_save_dir = kwargs.pop("model_save_dir", None)
|
569
|
-
|
570
|
-
rbln_kwargs = rbln_config
|
571
|
-
model: "PreTrainedModel" = cls.get_pytorch_model(
|
572
|
-
model_id=model_id,
|
573
|
-
rbln_kwargs=rbln_kwargs,
|
574
|
-
**kwargs,
|
575
|
-
)
|
576
|
-
preprocessors = maybe_load_preprocessors(model_id, subfolder=subfolder)
|
577
|
-
return cls.from_model(
|
578
|
-
model,
|
579
|
-
rbln_config=rbln_config,
|
580
|
-
preprocessors=preprocessors,
|
581
|
-
model_save_dir=model_save_dir,
|
582
|
-
**kwargs,
|
583
|
-
)
|
584
|
-
|
585
|
-
|
586
|
-
class RBLNModel(RBLNBaseModel):
|
587
|
-
"""
|
588
|
-
A class that inherits from RBLNBaseModel for models consisting of a single `torch.nn.Module`.
|
589
|
-
|
590
|
-
This class supports all the functionality of RBLNBaseModel, including loading and saving models using
|
591
|
-
the `from_pretrained` and `save_pretrained` methods, compiling PyTorch models for execution on RBLN NPU
|
592
|
-
devices.
|
593
|
-
|
594
|
-
Example:
|
595
|
-
```python
|
596
|
-
model = RBLNModel.from_pretrained("model_id", export=True, rbln_npu="npu_name")
|
597
|
-
outputs = model(**inputs)
|
598
|
-
```
|
599
|
-
"""
|
600
|
-
|
601
|
-
@classmethod
|
602
|
-
def update_kwargs(cls, kwargs):
|
603
|
-
"""
|
604
|
-
Update user-given kwargs to get proper pytorch model.
|
605
|
-
|
606
|
-
For example, `torchscript`=True should be set because torch.jit
|
607
|
-
does not support `transformers` output instances as module output;
|
608
|
-
"""
|
609
|
-
kwargs.update(
|
610
|
-
{
|
611
|
-
"torchscript": True,
|
612
|
-
"return_dict": False,
|
613
|
-
}
|
614
|
-
)
|
615
|
-
return kwargs
|
616
|
-
|
617
|
-
@classmethod
|
618
|
-
def get_pytorch_model(
|
619
|
-
cls,
|
620
|
-
model_id: str,
|
621
|
-
use_auth_token: Optional[Union[bool, str]] = None,
|
622
|
-
revision: Optional[str] = None,
|
623
|
-
force_download: bool = False,
|
624
|
-
cache_dir: Optional[str] = None,
|
625
|
-
subfolder: str = "",
|
626
|
-
local_files_only: bool = False,
|
627
|
-
trust_remote_code: bool = False,
|
628
|
-
# Some rbln-kwargs should be applied before loading torch module (i.e. quantized llm)
|
629
|
-
rbln_kwargs: Optional[Dict[str, Any]] = None,
|
630
|
-
**kwargs,
|
631
|
-
) -> "PreTrainedModel":
|
632
|
-
task = kwargs.pop("task", None)
|
633
|
-
if task is None:
|
634
|
-
task = TasksManager.infer_task_from_model(cls.auto_model_class)
|
635
|
-
|
636
|
-
kwargs = cls.update_kwargs(kwargs)
|
637
|
-
|
638
|
-
model = TasksManager.get_model_from_task(
|
639
|
-
task=task,
|
640
|
-
model_name_or_path=model_id,
|
641
|
-
subfolder=subfolder,
|
642
|
-
revision=revision,
|
643
|
-
framework="pt",
|
644
|
-
cache_dir=cache_dir,
|
645
|
-
use_auth_token=use_auth_token,
|
646
|
-
local_files_only=local_files_only,
|
647
|
-
force_download=force_download,
|
648
|
-
trust_remote_code=trust_remote_code,
|
649
|
-
**kwargs,
|
650
|
-
)
|
651
|
-
|
652
|
-
return model
|
653
|
-
|
654
|
-
@classmethod
|
655
|
-
def save_torch_artifacts(
|
656
|
-
cls,
|
657
|
-
model: "PreTrainedModel",
|
658
|
-
save_dir_path: Path,
|
659
|
-
subfolder: str,
|
660
|
-
rbln_config: RBLNConfig,
|
661
|
-
):
|
662
|
-
"""
|
663
|
-
If you are unavoidably running on a CPU rather than an RBLN device,
|
664
|
-
store the torch tensor, weight, etc. in this function.
|
665
|
-
"""
|
666
|
-
|
667
|
-
@classmethod
|
668
|
-
def get_compiled_model(cls, model: "PreTrainedModel", rbln_config: RBLNConfig):
|
669
|
-
model = cls.wrap_model_if_needed(model, rbln_config)
|
670
|
-
rbln_compile_config = rbln_config.compile_cfgs[0]
|
671
|
-
compiled_model = cls.compile(model, rbln_compile_config=rbln_compile_config)
|
672
|
-
return compiled_model
|
673
|
-
|
674
|
-
@classmethod
|
675
|
-
@use_rbln_config
|
676
|
-
def from_model(
|
677
|
-
cls,
|
678
|
-
model: "PreTrainedModel",
|
679
|
-
rbln_config: Dict[str, Any] = {},
|
680
|
-
model_save_dir: Optional[Union[str, Path, TemporaryDirectory]] = None,
|
681
|
-
subfolder: str = "",
|
682
|
-
**kwargs,
|
683
|
-
):
|
684
|
-
preprocessors = kwargs.pop("preprocessors", [])
|
685
|
-
rbln_kwargs = rbln_config
|
686
|
-
|
687
|
-
# Directory to save compile artifacts(.rbln) and original configs
|
688
|
-
if model_save_dir is None:
|
689
|
-
save_dir = TemporaryDirectory()
|
690
|
-
save_dir_path = Path(save_dir.name)
|
691
|
-
else:
|
692
|
-
save_dir = model_save_dir
|
693
|
-
if isinstance(save_dir, TemporaryDirectory):
|
694
|
-
save_dir_path = Path(model_save_dir.name)
|
695
|
-
else:
|
696
|
-
save_dir_path = Path(model_save_dir)
|
697
|
-
save_dir_path.mkdir(exist_ok=True)
|
698
|
-
|
699
|
-
# (Optional) Save preprocessors (tokenizer, image preprocessors, etc)
|
700
|
-
for preprocessor in preprocessors:
|
701
|
-
preprocessor.save_pretrained(save_dir_path)
|
702
|
-
|
703
|
-
# Save configs
|
704
|
-
# FIXME :: optimum passes AutoConfig. But here we ignore it.
|
705
|
-
config = model.config
|
706
|
-
if hasattr(model, "can_generate") and model.can_generate():
|
707
|
-
generation_config = model.generation_config
|
708
|
-
generation_config.save_pretrained(save_dir_path / subfolder)
|
709
|
-
if not isinstance(config, PretrainedConfig): # diffusers config
|
710
|
-
config = PretrainedConfig(**config)
|
711
|
-
config.save_pretrained(save_dir_path / subfolder)
|
712
|
-
|
713
|
-
# Get compilation arguments (e.g. input_info)
|
714
|
-
rbln_config: RBLNConfig = cls.get_rbln_config(
|
715
|
-
preprocessors=preprocessors, model_config=config, rbln_kwargs=rbln_kwargs
|
716
|
-
)
|
717
|
-
# rbln_config.update_runtime_cfg(rbln_kwargs) # This is done in get_rbln_config
|
718
|
-
|
719
|
-
compiled_model: Union[rebel.RBLNCompiledModel, Dict[str, rebel.RBLNCompiledModel]] = cls.get_compiled_model(
|
720
|
-
model, rbln_config=rbln_config
|
721
|
-
)
|
722
|
-
|
723
|
-
# Save compiled models (.rbln)
|
724
|
-
(save_dir_path / subfolder).mkdir(exist_ok=True)
|
725
|
-
if not isinstance(compiled_model, dict):
|
726
|
-
compiled_models = {DEFAULT_COMPILED_MODEL_NAME: compiled_model}
|
727
|
-
else:
|
728
|
-
compiled_models = compiled_model
|
729
|
-
for compiled_model_name, cm in compiled_models.items():
|
730
|
-
cm.save(save_dir_path / subfolder / f"{compiled_model_name}.rbln")
|
731
|
-
rbln_config.save(save_dir_path / subfolder)
|
732
|
-
|
733
|
-
# Save torch artifacts (e.g. embedding matrix if needed.)
|
734
|
-
cls.save_torch_artifacts(model, save_dir_path=save_dir_path, subfolder=subfolder, rbln_config=rbln_config)
|
735
|
-
|
736
|
-
# Load submodules
|
737
|
-
if len(cls._rbln_submodules) > 0:
|
738
|
-
rbln_submodules = cls._load_submodules(
|
739
|
-
model=model,
|
740
|
-
model_save_dir=save_dir,
|
741
|
-
rbln_kwargs=rbln_kwargs,
|
742
|
-
**kwargs,
|
743
|
-
)
|
744
|
-
else:
|
745
|
-
rbln_submodules = []
|
746
|
-
|
747
|
-
# Instantiate
|
748
|
-
return cls._from_pretrained(
|
749
|
-
model_id=save_dir_path,
|
750
|
-
config=config,
|
751
|
-
model_save_dir=save_dir,
|
752
|
-
subfolder=subfolder,
|
753
|
-
rbln_config=rbln_config,
|
754
|
-
rbln_compiled_models=compiled_models,
|
755
|
-
rbln_submodules=rbln_submodules,
|
756
|
-
**kwargs,
|
757
|
-
)
|
758
|
-
|
759
|
-
@classmethod
|
760
|
-
def _create_runtimes(
|
761
|
-
cls,
|
762
|
-
compiled_models: List[rebel.RBLNCompiledModel],
|
763
|
-
rbln_device_map: Dict[str, int],
|
764
|
-
) -> List[rebel.Runtime]:
|
765
|
-
device = rbln_device_map[DEFAULT_COMPILED_MODEL_NAME]
|
766
|
-
return [compiled_model.create_runtime(tensor_type="pt", device=device) for compiled_model in compiled_models]
|
767
|
-
|
511
|
+
@abstractmethod
|
768
512
|
def forward(self, *args: List[torch.Tensor], **kwargs: Dict[str, torch.Tensor]):
|
769
|
-
|
770
|
-
return output
|
771
|
-
|
772
|
-
|
773
|
-
class RBLNModelForQuestionAnswering(RBLNModel):
|
774
|
-
auto_model_class = AutoModelForQuestionAnswering
|
775
|
-
rbln_model_input_names = ["input_ids", "attention_mask", "token_type_ids"]
|
776
|
-
|
777
|
-
@classmethod
|
778
|
-
def _get_rbln_config(
|
779
|
-
cls,
|
780
|
-
preprocessors: Optional[Union["AutoFeatureExtractor", "AutoProcessor", "AutoTokenizer"]],
|
781
|
-
model_config: Optional["PretrainedConfig"] = None,
|
782
|
-
rbln_kwargs: Dict[str, Any] = {},
|
783
|
-
) -> RBLNConfig:
|
784
|
-
rbln_max_seq_len = rbln_kwargs.get("max_seq_len", None)
|
785
|
-
rbln_batch_size = rbln_kwargs.get("batch_size", None)
|
786
|
-
rbln_model_input_names = rbln_kwargs.get("model_input_names", None)
|
787
|
-
|
788
|
-
if rbln_max_seq_len is None:
|
789
|
-
for tokenizer in preprocessors:
|
790
|
-
if hasattr(tokenizer, "model_max_length"):
|
791
|
-
rbln_max_seq_len = tokenizer.model_max_length
|
792
|
-
break
|
793
|
-
if rbln_max_seq_len is None:
|
794
|
-
raise ValueError("`rbln_max_seq_len` should be specified!")
|
795
|
-
|
796
|
-
if rbln_batch_size is None:
|
797
|
-
rbln_batch_size = 1
|
798
|
-
|
799
|
-
if rbln_model_input_names is None:
|
800
|
-
for tokenizer in preprocessors:
|
801
|
-
if hasattr(tokenizer, "model_input_names"):
|
802
|
-
rbln_model_input_names = tokenizer.model_input_names
|
803
|
-
break
|
804
|
-
if rbln_model_input_names is None and hasattr(cls, "rbln_model_input_names"):
|
805
|
-
rbln_model_input_names = cls.rbln_model_input_names
|
806
|
-
elif rbln_model_input_names is None and hasattr(cls, "rbln_model_input_names") is False:
|
807
|
-
original_model_class = getattr(transformers, model_config.architectures[0])
|
808
|
-
input_names_order = inspect.signature(original_model_class.forward).parameters.keys()
|
809
|
-
raise ValueError(
|
810
|
-
"Specify the model input names obtained by the tokenizer via `rbln_model_input_names`, "
|
811
|
-
f"and be sure to make the order of the inputs same as QuestionAnswering forward() arguments like ({list(input_names_order)})"
|
812
|
-
)
|
813
|
-
|
814
|
-
input_info = [
|
815
|
-
(model_input_name, [rbln_batch_size, rbln_max_seq_len], "int64")
|
816
|
-
for model_input_name in rbln_model_input_names
|
817
|
-
]
|
818
|
-
|
819
|
-
rbln_compile_config = RBLNCompileConfig(input_info=input_info)
|
820
|
-
rbln_config = RBLNConfig(
|
821
|
-
rbln_cls=cls.__name__,
|
822
|
-
compile_cfgs=[rbln_compile_config],
|
823
|
-
rbln_kwargs=rbln_kwargs,
|
824
|
-
)
|
825
|
-
rbln_config.model_cfg.update({"max_seq_len": rbln_max_seq_len})
|
826
|
-
return rbln_config
|
827
|
-
|
828
|
-
|
829
|
-
class RBLNModelForImageClassification(RBLNModel):
|
830
|
-
"""
|
831
|
-
This is a generic model class that will be instantiated as one of the model classes of the library (with a image classification head) when created with the from_pretrained() class method
|
832
|
-
"""
|
833
|
-
|
834
|
-
auto_model_class = AutoModelForImageClassification
|
835
|
-
|
836
|
-
@classmethod
|
837
|
-
def _get_rbln_config(
|
838
|
-
cls,
|
839
|
-
preprocessors: Optional[Union["AutoFeatureExtractor", "AutoProcessor", "AutoTokenizer"]],
|
840
|
-
model_config: Optional["PretrainedConfig"] = None,
|
841
|
-
rbln_kwargs: Dict[str, Any] = {},
|
842
|
-
) -> RBLNConfig:
|
843
|
-
rbln_image_size = rbln_kwargs.get("image_size", None)
|
844
|
-
rbln_batch_size = rbln_kwargs.get("batch_size", None)
|
845
|
-
|
846
|
-
if rbln_image_size is None:
|
847
|
-
for processor in preprocessors:
|
848
|
-
if hasattr(processor, "size"):
|
849
|
-
if all(required_key in processor.size.keys() for required_key in ["height", "width"]):
|
850
|
-
rbln_image_size = (processor.size["height"], processor.size["width"])
|
851
|
-
elif "shortest_edge" in processor.size.keys():
|
852
|
-
rbln_image_size = (processor.size["shortest_edge"], processor.size["shortest_edge"])
|
853
|
-
elif "longest_edge" in processor.size.keys():
|
854
|
-
rbln_image_size = (processor.size["longest_edge"], processor.size["longest_edge"])
|
855
|
-
break
|
856
|
-
|
857
|
-
if rbln_image_size is None:
|
858
|
-
rbln_image_size = model_config.image_size
|
859
|
-
|
860
|
-
if rbln_image_size is None:
|
861
|
-
raise ValueError("`rbln_image_size` should be specified!")
|
862
|
-
|
863
|
-
if rbln_batch_size is None:
|
864
|
-
rbln_batch_size = 1
|
865
|
-
|
866
|
-
if isinstance(rbln_image_size, int):
|
867
|
-
rbln_image_height, rbln_image_width = rbln_image_size, rbln_image_size
|
868
|
-
elif isinstance(rbln_image_size, (list, tuple)):
|
869
|
-
rbln_image_height, rbln_image_width = rbln_image_size[0], rbln_image_size[1]
|
870
|
-
elif isinstance(rbln_image_size, dict):
|
871
|
-
rbln_image_height, rbln_image_width = rbln_image_size["height"], rbln_image_size["width"]
|
872
|
-
else:
|
873
|
-
raise ValueError(
|
874
|
-
"`rbln_image_size` should be `int` (ex. 224), `tuple` (ex. 224, 224), `dict` (ex. {'height': 224, 'width': 224}) format"
|
875
|
-
)
|
876
|
-
|
877
|
-
input_info = [
|
878
|
-
(
|
879
|
-
"pixel_values",
|
880
|
-
[rbln_batch_size, 3, rbln_image_height, rbln_image_width],
|
881
|
-
"float32",
|
882
|
-
)
|
883
|
-
]
|
884
|
-
|
885
|
-
rbln_compile_config = RBLNCompileConfig(input_info=input_info)
|
886
|
-
return RBLNConfig(rbln_cls=cls.__name__, compile_cfgs=[rbln_compile_config], rbln_kwargs=rbln_kwargs)
|
887
|
-
|
888
|
-
|
889
|
-
class RBLNModelForAudioClassification(RBLNModel):
|
890
|
-
"""
|
891
|
-
This is a generic model class that will be instantiated as one of the model classes of the library (with a audio classification head) when created with the from_pretrained() class method
|
892
|
-
This model inherits from [`RBLNModel`]. Check the superclass documentation for the generic methods the library implements for all its models.
|
893
|
-
|
894
|
-
A class to convert and run pre-trained transformers based AudioClassification models on RBLN devices.
|
895
|
-
It implements the methods to convert a pre-trained transformers AudioClassification model into a RBLN transformer model by:
|
896
|
-
- transferring the checkpoint weights of the original into an optimized RBLN graph,
|
897
|
-
- compiling the resulting graph using the RBLN compiler.
|
898
|
-
|
899
|
-
Currently, this model class only supports the 'AST' model from the transformers library. Future updates may include support for additional model types.
|
900
|
-
"""
|
901
|
-
|
902
|
-
auto_model_class = AutoModelForAudioClassification
|
903
|
-
|
904
|
-
@classmethod
|
905
|
-
def _get_rbln_config(
|
906
|
-
cls,
|
907
|
-
preprocessors: "AutoFeatureExtractor",
|
908
|
-
model_config: "PretrainedConfig",
|
909
|
-
rbln_kwargs: Dict[str, Any] = {},
|
910
|
-
) -> RBLNConfig:
|
911
|
-
rbln_batch_size = rbln_kwargs.get("batch_size", None)
|
912
|
-
rbln_max_length = rbln_kwargs.get("max_length", None)
|
913
|
-
rbln_num_mel_bins = rbln_kwargs.get("num_mel_bins", None)
|
914
|
-
|
915
|
-
if rbln_batch_size is None:
|
916
|
-
rbln_batch_size = 1
|
917
|
-
|
918
|
-
if rbln_num_mel_bins is None:
|
919
|
-
rbln_num_mel_bins = getattr(model_config, "num_mel_bins", None)
|
920
|
-
if rbln_num_mel_bins is None:
|
921
|
-
for feature_extractor in preprocessors:
|
922
|
-
if hasattr(feature_extractor, "num_mel_bins"):
|
923
|
-
rbln_num_mel_bins = feature_extractor.num_mel_bins
|
924
|
-
break
|
925
|
-
|
926
|
-
if rbln_num_mel_bins is None:
|
927
|
-
raise ValueError("`rbln_num_mel_bins` should be specified!")
|
928
|
-
|
929
|
-
if rbln_max_length is None:
|
930
|
-
rbln_max_length = getattr(model_config, "max_length", None)
|
931
|
-
for feature_extractor in preprocessors:
|
932
|
-
if hasattr(feature_extractor, "max_length"):
|
933
|
-
rbln_max_length = feature_extractor.max_length
|
934
|
-
break
|
935
|
-
|
936
|
-
if rbln_max_length is None:
|
937
|
-
raise ValueError("`rbln_max_length` should be specified!")
|
938
|
-
|
939
|
-
input_info = [
|
940
|
-
(
|
941
|
-
"input_values",
|
942
|
-
[rbln_batch_size, rbln_max_length, rbln_num_mel_bins],
|
943
|
-
"float32",
|
944
|
-
),
|
945
|
-
]
|
946
|
-
|
947
|
-
rbln_compile_config = RBLNCompileConfig(input_info=input_info)
|
948
|
-
rbln_config = RBLNConfig(
|
949
|
-
rbln_cls=cls.__name__,
|
950
|
-
compile_cfgs=[rbln_compile_config],
|
951
|
-
rbln_kwargs=rbln_kwargs,
|
952
|
-
)
|
953
|
-
rbln_config.model_cfg.update(
|
954
|
-
{
|
955
|
-
"batch_size": rbln_batch_size,
|
956
|
-
"max_length": rbln_max_length,
|
957
|
-
"num_mel_bins": rbln_num_mel_bins,
|
958
|
-
}
|
959
|
-
)
|
960
|
-
return rbln_config
|
961
|
-
|
962
|
-
|
963
|
-
class RBLNModelForSequenceClassification(RBLNModel):
|
964
|
-
"""
|
965
|
-
This is a generic model class that will be instantiated as one of the model classes of the library (with a sequence classification head) when created with the from_pretrained() class method
|
966
|
-
This model inherits from [`RBLNModel`]. Check the superclass documentation for the generic methods the library implements for all its models.
|
967
|
-
|
968
|
-
A class to convert and run pre-trained transformers based SequenceClassification models on RBLN devices.
|
969
|
-
It implements the methods to convert a pre-trained transformers SequenceClassification model into a RBLN transformer model by:
|
970
|
-
- transferring the checkpoint weights of the original into an optimized RBLN graph,
|
971
|
-
- compiling the resulting graph using the RBLN compiler.
|
972
|
-
|
973
|
-
Currently, this model class supports the 'XLMRoberta' and 'Roberta' model from the transformers library. Future updates may include support for additional model types.
|
974
|
-
"""
|
975
|
-
|
976
|
-
auto_model_class = AutoModelForSequenceClassification
|
977
|
-
|
978
|
-
@classmethod
|
979
|
-
def _get_rbln_config(
|
980
|
-
cls,
|
981
|
-
preprocessors: Optional[Union["AutoFeatureExtractor", "AutoProcessor", "AutoTokenizer"]],
|
982
|
-
model_config: Optional["PretrainedConfig"] = None,
|
983
|
-
rbln_kwargs: Dict[str, Any] = {},
|
984
|
-
) -> RBLNConfig:
|
985
|
-
rbln_max_seq_len = rbln_kwargs.get("max_seq_len", None)
|
986
|
-
rbln_model_input_names = rbln_kwargs.get("model_input_names", None)
|
987
|
-
rbln_batch_size = rbln_kwargs.get("batch_size", None)
|
988
|
-
|
989
|
-
max_position_embeddings = getattr(model_config, "n_positions", None) or getattr(
|
990
|
-
model_config, "max_position_embeddings", None
|
991
|
-
)
|
992
|
-
|
993
|
-
if rbln_max_seq_len is None:
|
994
|
-
rbln_max_seq_len = max_position_embeddings
|
995
|
-
if rbln_max_seq_len is None:
|
996
|
-
for tokenizer in preprocessors:
|
997
|
-
if hasattr(tokenizer, "model_max_length"):
|
998
|
-
rbln_max_seq_len = tokenizer.model_max_length
|
999
|
-
break
|
1000
|
-
if rbln_max_seq_len is None:
|
1001
|
-
raise ValueError("`rbln_max_seq_len` should be specified!")
|
1002
|
-
|
1003
|
-
if max_position_embeddings is not None and rbln_max_seq_len > max_position_embeddings:
|
1004
|
-
raise ValueError("`rbln_enc_max_seq_len` should be less or equal than max_position_embeddings!")
|
1005
|
-
|
1006
|
-
if rbln_model_input_names is None:
|
1007
|
-
for tokenizer in preprocessors:
|
1008
|
-
if hasattr(tokenizer, "model_input_names"):
|
1009
|
-
rbln_model_input_names = tokenizer.model_input_names
|
1010
|
-
break
|
1011
|
-
if rbln_model_input_names is None and hasattr(cls, "rbln_model_input_names"):
|
1012
|
-
rbln_model_input_names = cls.rbln_model_input_names
|
1013
|
-
elif rbln_model_input_names is None and hasattr(cls, "rbln_model_input_names") is False:
|
1014
|
-
original_model_class = getattr(transformers, model_config.architectures[0])
|
1015
|
-
input_names_order = inspect.signature(original_model_class.forward).parameters.keys()
|
1016
|
-
raise ValueError(
|
1017
|
-
"Specify the model input names obtained by the tokenizer via `rbln_model_input_names`, "
|
1018
|
-
f"and be sure to make the order of the inputs same as SequenceClassification forward() arguments like ({list(input_names_order)})"
|
1019
|
-
)
|
1020
|
-
|
1021
|
-
if rbln_batch_size is None:
|
1022
|
-
rbln_batch_size = 1
|
1023
|
-
|
1024
|
-
input_info = [
|
1025
|
-
(model_input_name, [rbln_batch_size, rbln_max_seq_len], "int64")
|
1026
|
-
for model_input_name in rbln_model_input_names
|
1027
|
-
]
|
1028
|
-
|
1029
|
-
rbln_compile_config = RBLNCompileConfig(input_info=input_info)
|
1030
|
-
rbln_config = RBLNConfig(
|
1031
|
-
rbln_cls=cls.__name__,
|
1032
|
-
compile_cfgs=[rbln_compile_config],
|
1033
|
-
rbln_kwargs=rbln_kwargs,
|
1034
|
-
)
|
1035
|
-
rbln_config.model_cfg.update({"max_seq_len": rbln_max_seq_len})
|
1036
|
-
return rbln_config
|
1037
|
-
|
1038
|
-
|
1039
|
-
class RBLNModelForMaskedLM(RBLNModel):
|
1040
|
-
auto_model_class = AutoModelForMaskedLM
|
1041
|
-
|
1042
|
-
@classmethod
|
1043
|
-
def _get_rbln_config(
|
1044
|
-
cls,
|
1045
|
-
preprocessors: Optional[Union["AutoFeatureExtractor", "AutoProcessor", "AutoTokenizer"]],
|
1046
|
-
model_config: Optional["PretrainedConfig"] = None,
|
1047
|
-
rbln_kwargs: Dict[str, Any] = {},
|
1048
|
-
) -> RBLNConfig:
|
1049
|
-
rbln_max_seq_len = rbln_kwargs.get("max_seq_len", None)
|
1050
|
-
rbln_model_input_names = rbln_kwargs.get("model_input_names", None)
|
1051
|
-
rbln_batch_size = rbln_kwargs.get("batch_size", None)
|
1052
|
-
|
1053
|
-
max_position_embeddings = getattr(model_config, "n_positions", None) or getattr(
|
1054
|
-
model_config, "max_position_embeddings", None
|
1055
|
-
)
|
1056
|
-
|
1057
|
-
if rbln_max_seq_len is None:
|
1058
|
-
rbln_max_seq_len = max_position_embeddings
|
1059
|
-
if rbln_max_seq_len is None:
|
1060
|
-
for tokenizer in preprocessors:
|
1061
|
-
if hasattr(tokenizer, "model_max_length"):
|
1062
|
-
rbln_max_seq_len = tokenizer.model_max_length
|
1063
|
-
break
|
1064
|
-
if rbln_max_seq_len is None:
|
1065
|
-
raise ValueError("`rbln_max_seq_len` should be specified!")
|
1066
|
-
|
1067
|
-
if max_position_embeddings is not None and rbln_max_seq_len > max_position_embeddings:
|
1068
|
-
raise ValueError("`rbln_enc_max_seq_len` should be less or equal than max_position_embeddings!")
|
1069
|
-
|
1070
|
-
if rbln_model_input_names is None:
|
1071
|
-
for tokenizer in preprocessors:
|
1072
|
-
if hasattr(tokenizer, "model_input_names"):
|
1073
|
-
rbln_model_input_names = tokenizer.model_input_names
|
1074
|
-
break
|
1075
|
-
if rbln_model_input_names is None and hasattr(cls, "rbln_model_input_names"):
|
1076
|
-
rbln_model_input_names = cls.rbln_model_input_names
|
1077
|
-
elif rbln_model_input_names is None and hasattr(cls, "rbln_model_input_names") is False:
|
1078
|
-
original_model_class = getattr(transformers, model_config.architectures[0])
|
1079
|
-
input_names_order = inspect.signature(original_model_class.forward).parameters.keys()
|
1080
|
-
raise ValueError(
|
1081
|
-
"Specify the model input names obtained by the tokenizer via `rbln_model_input_names`, "
|
1082
|
-
f"and be sure to make the order of the inputs same as MaskedLM forward() arguments like ({list(input_names_order)})"
|
1083
|
-
)
|
1084
|
-
|
1085
|
-
if rbln_batch_size is None:
|
1086
|
-
rbln_batch_size = 1
|
1087
|
-
|
1088
|
-
input_info = [
|
1089
|
-
(model_input_name, [rbln_batch_size, rbln_max_seq_len], "int64")
|
1090
|
-
for model_input_name in rbln_model_input_names
|
1091
|
-
]
|
1092
|
-
|
1093
|
-
rbln_compile_config = RBLNCompileConfig(input_info=input_info)
|
1094
|
-
rbln_config = RBLNConfig(
|
1095
|
-
rbln_cls=cls.__name__,
|
1096
|
-
compile_cfgs=[rbln_compile_config],
|
1097
|
-
rbln_kwargs=rbln_kwargs,
|
1098
|
-
)
|
1099
|
-
rbln_config.model_cfg.update({"max_seq_len": rbln_max_seq_len})
|
1100
|
-
return rbln_config
|
513
|
+
pass
|