optimum-rbln 0.8.1a0__py3-none-any.whl → 0.8.1a2__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 +2 -0
- optimum/rbln/__version__.py +2 -2
- optimum/rbln/configuration_utils.py +53 -33
- optimum/rbln/diffusers/configurations/models/configuration_autoencoder_kl.py +9 -2
- optimum/rbln/diffusers/configurations/models/configuration_controlnet.py +4 -2
- optimum/rbln/diffusers/configurations/models/configuration_prior_transformer.py +9 -2
- optimum/rbln/diffusers/configurations/models/configuration_transformer_sd3.py +4 -2
- optimum/rbln/diffusers/configurations/models/configuration_unet_2d_condition.py +9 -2
- optimum/rbln/diffusers/configurations/models/configuration_vq_model.py +9 -2
- optimum/rbln/diffusers/configurations/pipelines/configuration_controlnet.py +33 -9
- optimum/rbln/diffusers/configurations/pipelines/configuration_kandinsky2_2.py +30 -12
- optimum/rbln/diffusers/configurations/pipelines/configuration_stable_diffusion.py +22 -6
- optimum/rbln/diffusers/configurations/pipelines/configuration_stable_diffusion_3.py +16 -6
- optimum/rbln/diffusers/configurations/pipelines/configuration_stable_diffusion_xl.py +16 -6
- optimum/rbln/diffusers/modeling_diffusers.py +16 -26
- optimum/rbln/diffusers/models/autoencoders/autoencoder_kl.py +11 -0
- optimum/rbln/diffusers/models/autoencoders/vae.py +1 -8
- optimum/rbln/diffusers/models/autoencoders/vq_model.py +11 -0
- optimum/rbln/diffusers/models/controlnet.py +13 -7
- optimum/rbln/diffusers/models/transformers/prior_transformer.py +10 -0
- optimum/rbln/diffusers/models/transformers/transformer_sd3.py +2 -0
- optimum/rbln/diffusers/models/unets/unet_2d_condition.py +7 -0
- optimum/rbln/diffusers/pipelines/controlnet/multicontrolnet.py +1 -4
- optimum/rbln/diffusers/pipelines/controlnet/pipeline_controlnet.py +7 -0
- optimum/rbln/diffusers/pipelines/controlnet/pipeline_controlnet_img2img.py +7 -0
- optimum/rbln/diffusers/pipelines/controlnet/pipeline_controlnet_sd_xl.py +7 -0
- optimum/rbln/diffusers/pipelines/controlnet/pipeline_controlnet_sd_xl_img2img.py +7 -0
- optimum/rbln/diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2.py +7 -0
- optimum/rbln/diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2_combined.py +48 -27
- optimum/rbln/diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2_img2img.py +7 -0
- optimum/rbln/diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2_inpaint.py +7 -0
- optimum/rbln/diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2_prior.py +7 -0
- optimum/rbln/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion.py +7 -0
- optimum/rbln/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_img2img.py +7 -0
- optimum/rbln/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_inpaint.py +7 -0
- optimum/rbln/diffusers/pipelines/stable_diffusion_3/pipeline_stable_diffusion_3.py +7 -0
- optimum/rbln/diffusers/pipelines/stable_diffusion_3/pipeline_stable_diffusion_3_img2img.py +7 -0
- optimum/rbln/diffusers/pipelines/stable_diffusion_3/pipeline_stable_diffusion_3_inpaint.py +7 -0
- optimum/rbln/diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl.py +7 -0
- optimum/rbln/diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl_img2img.py +7 -0
- optimum/rbln/diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl_inpaint.py +7 -0
- optimum/rbln/modeling.py +33 -35
- optimum/rbln/modeling_base.py +45 -107
- optimum/rbln/transformers/__init__.py +39 -47
- optimum/rbln/transformers/configuration_generic.py +16 -13
- optimum/rbln/transformers/modeling_generic.py +18 -19
- optimum/rbln/transformers/modeling_rope_utils.py +5 -2
- optimum/rbln/transformers/models/__init__.py +46 -4
- optimum/rbln/transformers/models/audio_spectrogram_transformer/__init__.py +17 -0
- optimum/rbln/transformers/models/audio_spectrogram_transformer/configuration_audio_spectrogram_transformer.py +21 -0
- optimum/rbln/transformers/models/audio_spectrogram_transformer/modeling_audio_spectrogram_transformer.py +28 -0
- optimum/rbln/transformers/models/auto/auto_factory.py +35 -12
- optimum/rbln/transformers/models/bart/bart_architecture.py +14 -1
- optimum/rbln/transformers/models/blip_2/modeling_blip_2.py +35 -4
- optimum/rbln/transformers/models/clip/configuration_clip.py +3 -3
- optimum/rbln/transformers/models/clip/modeling_clip.py +11 -12
- optimum/rbln/transformers/models/decoderonly/configuration_decoderonly.py +111 -14
- optimum/rbln/transformers/models/decoderonly/decoderonly_architecture.py +102 -35
- optimum/rbln/transformers/models/decoderonly/modeling_decoderonly.py +229 -175
- optimum/rbln/transformers/models/distilbert/__init__.py +19 -0
- optimum/rbln/transformers/models/distilbert/configuration_distilbert.py +19 -0
- optimum/rbln/transformers/models/distilbert/modeling_distilbert.py +19 -0
- optimum/rbln/transformers/models/exaone/configuration_exaone.py +24 -1
- optimum/rbln/transformers/models/exaone/exaone_architecture.py +5 -1
- optimum/rbln/transformers/models/exaone/modeling_exaone.py +66 -5
- optimum/rbln/transformers/models/gemma/configuration_gemma.py +24 -1
- optimum/rbln/transformers/models/gemma/gemma_architecture.py +5 -1
- optimum/rbln/transformers/models/gemma/modeling_gemma.py +49 -0
- optimum/rbln/transformers/models/gemma3/configuration_gemma3.py +3 -3
- optimum/rbln/transformers/models/gemma3/gemma3_architecture.py +18 -250
- optimum/rbln/transformers/models/gemma3/modeling_gemma3.py +106 -236
- optimum/rbln/transformers/models/gpt2/configuration_gpt2.py +4 -1
- optimum/rbln/transformers/models/gpt2/gpt2_architecture.py +6 -1
- optimum/rbln/transformers/models/idefics3/configuration_idefics3.py +12 -2
- optimum/rbln/transformers/models/idefics3/modeling_idefics3.py +41 -4
- optimum/rbln/transformers/models/llama/configuration_llama.py +24 -1
- optimum/rbln/transformers/models/llama/modeling_llama.py +49 -0
- optimum/rbln/transformers/models/llava_next/configuration_llava_next.py +2 -2
- optimum/rbln/transformers/models/llava_next/modeling_llava_next.py +32 -4
- optimum/rbln/transformers/models/midm/configuration_midm.py +24 -1
- optimum/rbln/transformers/models/midm/midm_architecture.py +6 -1
- optimum/rbln/transformers/models/midm/modeling_midm.py +66 -5
- optimum/rbln/transformers/models/mistral/configuration_mistral.py +24 -1
- optimum/rbln/transformers/models/mistral/modeling_mistral.py +62 -4
- optimum/rbln/transformers/models/opt/configuration_opt.py +4 -1
- optimum/rbln/transformers/models/opt/modeling_opt.py +10 -0
- optimum/rbln/transformers/models/opt/opt_architecture.py +7 -1
- optimum/rbln/transformers/models/phi/configuration_phi.py +24 -1
- optimum/rbln/transformers/models/phi/modeling_phi.py +49 -0
- optimum/rbln/transformers/models/phi/phi_architecture.py +1 -1
- optimum/rbln/transformers/models/qwen2/configuration_qwen2.py +24 -1
- optimum/rbln/transformers/models/qwen2/modeling_qwen2.py +67 -4
- optimum/rbln/transformers/models/qwen2_5_vl/configuration_qwen2_5_vl.py +15 -3
- optimum/rbln/transformers/models/qwen2_5_vl/modeling_qwen2_5_vl.py +58 -27
- optimum/rbln/transformers/models/qwen2_5_vl/qwen2_5_vl_architecture.py +47 -2
- optimum/rbln/transformers/models/resnet/__init__.py +23 -0
- optimum/rbln/transformers/models/resnet/configuration_resnet.py +20 -0
- optimum/rbln/transformers/models/resnet/modeling_resnet.py +22 -0
- optimum/rbln/transformers/models/roberta/__init__.py +24 -0
- optimum/rbln/transformers/{configuration_alias.py → models/roberta/configuration_roberta.py} +4 -30
- optimum/rbln/transformers/{modeling_alias.py → models/roberta/modeling_roberta.py} +2 -32
- optimum/rbln/transformers/models/seq2seq/__init__.py +1 -1
- optimum/rbln/transformers/models/seq2seq/{configuration_seq2seq2.py → configuration_seq2seq.py} +2 -2
- optimum/rbln/transformers/models/seq2seq/modeling_seq2seq.py +1 -1
- optimum/rbln/transformers/models/seq2seq/seq2seq_architecture.py +41 -3
- optimum/rbln/transformers/models/siglip/configuration_siglip.py +3 -0
- optimum/rbln/transformers/models/siglip/modeling_siglip.py +62 -21
- optimum/rbln/transformers/models/t5/modeling_t5.py +46 -4
- optimum/rbln/transformers/models/t5/t5_architecture.py +5 -1
- optimum/rbln/transformers/models/{time_series_transformers → time_series_transformer}/__init__.py +1 -1
- optimum/rbln/transformers/models/{time_series_transformers → time_series_transformer}/configuration_time_series_transformer.py +2 -2
- optimum/rbln/transformers/models/{time_series_transformers/modeling_time_series_transformers.py → time_series_transformer/modeling_time_series_transformer.py} +14 -9
- optimum/rbln/transformers/models/vit/__init__.py +19 -0
- optimum/rbln/transformers/models/vit/configuration_vit.py +19 -0
- optimum/rbln/transformers/models/vit/modeling_vit.py +19 -0
- optimum/rbln/transformers/models/wav2vec2/__init__.py +1 -1
- optimum/rbln/transformers/models/wav2vec2/modeling_wav2vec2.py +1 -1
- optimum/rbln/transformers/models/whisper/configuration_whisper.py +3 -1
- optimum/rbln/transformers/models/whisper/modeling_whisper.py +35 -15
- optimum/rbln/transformers/models/xlm_roberta/__init__.py +16 -2
- optimum/rbln/transformers/models/xlm_roberta/configuration_xlm_roberta.py +15 -2
- optimum/rbln/transformers/models/xlm_roberta/modeling_xlm_roberta.py +12 -3
- optimum/rbln/utils/model_utils.py +20 -0
- optimum/rbln/utils/submodule.py +6 -8
- {optimum_rbln-0.8.1a0.dist-info → optimum_rbln-0.8.1a2.dist-info}/METADATA +2 -2
- {optimum_rbln-0.8.1a0.dist-info → optimum_rbln-0.8.1a2.dist-info}/RECORD +130 -117
- /optimum/rbln/transformers/models/{time_series_transformers → time_series_transformer}/time_series_transformers_architecture.py +0 -0
- /optimum/rbln/transformers/models/wav2vec2/{configuration_wav2vec.py → configuration_wav2vec2.py} +0 -0
- {optimum_rbln-0.8.1a0.dist-info → optimum_rbln-0.8.1a2.dist-info}/WHEEL +0 -0
- {optimum_rbln-0.8.1a0.dist-info → optimum_rbln-0.8.1a2.dist-info}/licenses/LICENSE +0 -0
@@ -12,12 +12,11 @@
|
|
12
12
|
# See the License for the specific language governing permissions and
|
13
13
|
# limitations under the License.
|
14
14
|
|
15
|
-
from typing import TYPE_CHECKING, Optional, Tuple, Union
|
15
|
+
from typing import TYPE_CHECKING, Any, Dict, Optional, Tuple, Union
|
16
16
|
|
17
17
|
import torch
|
18
18
|
from transformers import SiglipVisionConfig, SiglipVisionModel
|
19
19
|
from transformers.modeling_outputs import BaseModelOutputWithPooling
|
20
|
-
from transformers.models.siglip.modeling_siglip import SiglipVisionModelOutput
|
21
20
|
|
22
21
|
from ....configuration_utils import RBLNCompileConfig
|
23
22
|
from ....modeling import RBLNModel
|
@@ -34,11 +33,18 @@ if TYPE_CHECKING:
|
|
34
33
|
|
35
34
|
|
36
35
|
class _SiglipVisionModel(torch.nn.Module):
|
37
|
-
def __init__(
|
36
|
+
def __init__(
|
37
|
+
self,
|
38
|
+
model: SiglipVisionModel,
|
39
|
+
interpolate_pos_encoding: bool,
|
40
|
+
output_hidden_states: bool,
|
41
|
+
output_attentions: bool,
|
42
|
+
):
|
38
43
|
super().__init__()
|
39
44
|
self.vision_model = model.vision_model
|
40
45
|
self.interpolate_pos_encoding = interpolate_pos_encoding
|
41
46
|
self.output_hidden_states = output_hidden_states
|
47
|
+
self.output_attentions = output_attentions
|
42
48
|
|
43
49
|
def forward(self, inp):
|
44
50
|
enc_out = self.vision_model(
|
@@ -46,6 +52,7 @@ class _SiglipVisionModel(torch.nn.Module):
|
|
46
52
|
output_hidden_states=self.output_hidden_states,
|
47
53
|
return_dict=False,
|
48
54
|
interpolate_pos_encoding=self.interpolate_pos_encoding,
|
55
|
+
output_attentions=self.output_attentions,
|
49
56
|
)
|
50
57
|
return tuple(x for x in enc_out if x is not None)
|
51
58
|
|
@@ -56,6 +63,7 @@ class RBLNSiglipVisionModel(RBLNModel):
|
|
56
63
|
wrapper_cfg = {
|
57
64
|
"interpolate_pos_encoding": rbln_config.interpolate_pos_encoding,
|
58
65
|
"output_hidden_states": rbln_config.output_hidden_states,
|
66
|
+
"output_attentions": rbln_config.output_attentions,
|
59
67
|
}
|
60
68
|
return _SiglipVisionModel(model, **wrapper_cfg).eval()
|
61
69
|
|
@@ -81,8 +89,10 @@ class RBLNSiglipVisionModel(RBLNModel):
|
|
81
89
|
if rbln_config.image_size is None:
|
82
90
|
raise ValueError("`rbln_image_size` should be specified!")
|
83
91
|
|
92
|
+
if rbln_config.output_attentions is None:
|
93
|
+
rbln_config.output_attentions = getattr(model_config, "output_attentions", False)
|
84
94
|
if rbln_config.output_hidden_states is None:
|
85
|
-
rbln_config.output_hidden_states = model_config
|
95
|
+
rbln_config.output_hidden_states = getattr(model_config, "output_hidden_states", False)
|
86
96
|
|
87
97
|
rbln_compile_config = RBLNCompileConfig(
|
88
98
|
input_info=[
|
@@ -104,43 +114,74 @@ class RBLNSiglipVisionModel(RBLNModel):
|
|
104
114
|
|
105
115
|
def forward(
|
106
116
|
self,
|
107
|
-
pixel_values:
|
117
|
+
pixel_values: torch.Tensor,
|
108
118
|
return_dict: bool = None,
|
119
|
+
output_attentions: bool = None,
|
120
|
+
output_hidden_states: bool = None,
|
109
121
|
interpolate_pos_encoding: bool = False,
|
110
|
-
**kwargs,
|
111
|
-
) -> Union[Tuple,
|
112
|
-
if len(kwargs) > 0 and any(kwargs.values()):
|
113
|
-
logger.warning(
|
122
|
+
**kwargs: Dict[str, Any],
|
123
|
+
) -> Union[Tuple, BaseModelOutputWithPooling]:
|
124
|
+
if len(kwargs) > 0 and any(value is not None for value in kwargs.values()):
|
125
|
+
logger.warning(
|
126
|
+
f"Currently, optimum-rbln does not support kwargs {kwargs.keys()} for {self.__class__.__name__}."
|
127
|
+
)
|
128
|
+
|
129
|
+
output_attentions = output_attentions if output_attentions is not None else self.rbln_config.output_attentions
|
130
|
+
output_hidden_states = (
|
131
|
+
output_hidden_states if output_hidden_states is not None else self.rbln_config.output_hidden_states
|
132
|
+
)
|
133
|
+
|
134
|
+
if output_attentions != self.rbln_config.output_attentions:
|
135
|
+
raise ValueError(
|
136
|
+
f"Variable output_attentions {output_attentions} is not equal to rbln_config.output_attentions {self.rbln_config.output_attentions} "
|
137
|
+
f"Please compile again with the correct argument."
|
138
|
+
)
|
139
|
+
|
140
|
+
if output_hidden_states != self.rbln_config.output_hidden_states:
|
141
|
+
raise ValueError(
|
142
|
+
f"Variable output_hidden_states {output_hidden_states} is not equal to rbln_config.output_hidden_states {self.rbln_config.output_hidden_states} "
|
143
|
+
f"Please compile again with the correct argument."
|
144
|
+
)
|
114
145
|
|
115
146
|
if interpolate_pos_encoding != self.rbln_config.interpolate_pos_encoding:
|
116
147
|
raise ValueError(
|
117
|
-
f"Variable interpolate_pos_encoding {interpolate_pos_encoding} is not equal to rbln_config.interpolate_pos_encoding {self.rbln_config.interpolate_pos_encoding}"
|
148
|
+
f"Variable interpolate_pos_encoding {interpolate_pos_encoding} is not equal to rbln_config.interpolate_pos_encoding {self.rbln_config.interpolate_pos_encoding} "
|
118
149
|
f"Please compile again with the correct argument."
|
119
150
|
)
|
151
|
+
|
120
152
|
output = super().forward(pixel_values, return_dict=return_dict)
|
121
153
|
return output
|
122
154
|
|
123
155
|
def _prepare_output(self, output, return_dict):
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
"""
|
156
|
+
# Prepare model output based on return_dict flag.
|
157
|
+
# This method can be overridden by subclasses to provide task-specific output handling.
|
158
|
+
|
128
159
|
if not return_dict:
|
129
160
|
return (output,) if not isinstance(output, (tuple, list)) else output
|
130
161
|
else:
|
131
|
-
last_hidden_state = (
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
)
|
136
|
-
pooler_output = output[1] if self.rbln_config.interpolate_pos_encoding else None
|
162
|
+
last_hidden_state = output.pop(0) if isinstance(output, (tuple, list)) else output
|
163
|
+
vision_config = self.config.vision_config if hasattr(self.config, "vision_config") else self.config
|
164
|
+
pooler_output = output.pop(0) if getattr(vision_config, "vision_use_head", True) else None
|
165
|
+
|
137
166
|
if self.rbln_config.output_hidden_states:
|
138
|
-
hidden_states = (
|
167
|
+
hidden_states = ()
|
168
|
+
num_hidden_layers = vision_config.num_hidden_layers
|
169
|
+
for _ in range(num_hidden_layers + 1):
|
170
|
+
hidden_states += (output.pop(0),)
|
139
171
|
else:
|
140
172
|
hidden_states = None
|
141
173
|
|
174
|
+
if self.rbln_config.output_attentions:
|
175
|
+
attentions = ()
|
176
|
+
num_hidden_layers = vision_config.num_hidden_layers
|
177
|
+
for _ in range(num_hidden_layers):
|
178
|
+
attentions += (output.pop(0),)
|
179
|
+
else:
|
180
|
+
attentions = None
|
181
|
+
|
142
182
|
return BaseModelOutputWithPooling(
|
143
183
|
last_hidden_state=last_hidden_state,
|
144
184
|
pooler_output=pooler_output,
|
145
185
|
hidden_states=hidden_states,
|
186
|
+
attentions=attentions,
|
146
187
|
)
|
@@ -41,6 +41,28 @@ class T5EncoderWrapper(torch.nn.Module):
|
|
41
41
|
|
42
42
|
|
43
43
|
class RBLNT5EncoderModel(RBLNTransformerEncoderForFeatureExtraction):
|
44
|
+
"""
|
45
|
+
The T5 Model transformer with an encoder-only architecture for feature extraction.
|
46
|
+
This model inherits from [`RBLNTransformerEncoderForFeatureExtraction`]. Check the superclass documentation for the generic methods the library implements for all its models.
|
47
|
+
|
48
|
+
Important Note:
|
49
|
+
This model supports various sizes of the T5EncoderModel. For optimal performance, it is highly recommended to adjust the tensor parallelism setting
|
50
|
+
based on the model size. Please refer to the [Optimum RBLN Overview](../../../optimum_rbln.md) for guidance on choosing the appropriate tensor parallelism size for your model.
|
51
|
+
|
52
|
+
Examples:
|
53
|
+
```python
|
54
|
+
from optimum.rbln import RBLNT5EncoderModel
|
55
|
+
|
56
|
+
model = RBLNT5EncoderModel.from_pretrained(
|
57
|
+
"sentence-transformers/sentence-t5-xxl",
|
58
|
+
export=True,
|
59
|
+
rbln_tensor_parallel_size=4,
|
60
|
+
)
|
61
|
+
|
62
|
+
model.save_pretrained("compiled-sentence-t5-xxl")
|
63
|
+
```
|
64
|
+
"""
|
65
|
+
|
44
66
|
auto_model_class = AutoModelForTextEncoding
|
45
67
|
rbln_model_input_names = ["input_ids", "attention_mask"]
|
46
68
|
|
@@ -50,10 +72,7 @@ class RBLNT5EncoderModel(RBLNTransformerEncoderForFeatureExtraction):
|
|
50
72
|
|
51
73
|
@classmethod
|
52
74
|
def update_rbln_config_using_pipe(
|
53
|
-
cls,
|
54
|
-
pipe: "RBLNDiffusionMixin",
|
55
|
-
rbln_config: "RBLNDiffusionMixinConfig",
|
56
|
-
submodule_name: str,
|
75
|
+
cls, pipe: "RBLNDiffusionMixin", rbln_config: "RBLNDiffusionMixinConfig", submodule_name: str
|
57
76
|
) -> "RBLNDiffusionMixinConfig":
|
58
77
|
submodule_config = getattr(rbln_config, submodule_name)
|
59
78
|
submodule_config.max_seq_len = rbln_config.max_seq_len or 256
|
@@ -62,6 +81,29 @@ class RBLNT5EncoderModel(RBLNTransformerEncoderForFeatureExtraction):
|
|
62
81
|
|
63
82
|
|
64
83
|
class RBLNT5ForConditionalGeneration(RBLNModelForSeq2SeqLM):
|
84
|
+
"""
|
85
|
+
The T5 Model transformer with a language modeling head for conditional generation.
|
86
|
+
This model inherits from [`RBLNModelForSeq2SeqLM`]. Check the superclass documentation for the generic methods the library implements for all its models.
|
87
|
+
|
88
|
+
Important Note:
|
89
|
+
This model supports various sizes of the T5ForConditionalGeneration. For optimal performance, it is highly recommended to adjust the tensor parallelism setting
|
90
|
+
based on the model size. Please refer to the [Optimum RBLN Overview](../../../optimum_rbln.md) for guidance on choosing the appropriate tensor parallelism size for your model.
|
91
|
+
|
92
|
+
|
93
|
+
Examples:
|
94
|
+
```python
|
95
|
+
from optimum.rbln import RBLNT5ForConditionalGeneration
|
96
|
+
|
97
|
+
model = RBLNT5ForConditionalGeneration.from_pretrained(
|
98
|
+
"google-t5/t5-11b",
|
99
|
+
export=True,
|
100
|
+
rbln_tensor_parallel_size=4,
|
101
|
+
)
|
102
|
+
|
103
|
+
model.save_pretrained("compiled-sentence-t5-xxl")
|
104
|
+
```
|
105
|
+
"""
|
106
|
+
|
65
107
|
support_causal_attn = False
|
66
108
|
|
67
109
|
@classmethod
|
@@ -136,10 +136,14 @@ class T5Decoder(Seq2SeqDecoder):
|
|
136
136
|
|
137
137
|
|
138
138
|
class T5Block(Seq2SeqDecoderLayer):
|
139
|
+
def __init__(self, decoder_layer, self_attn):
|
140
|
+
super().__init__(decoder_layer, self_attn, cross_attn=None)
|
141
|
+
self.__post_init__()
|
142
|
+
|
139
143
|
def __post_init__(self):
|
140
144
|
self.self_attn_layer_norm = self._original_mod.layer[0].layer_norm
|
141
145
|
self.encoder_attn_layer_norm = self._original_mod.layer[1].layer_norm
|
142
|
-
self.
|
146
|
+
self.cross_attn = T5CrossAttention(self._original_mod.layer[1].EncDecAttention)
|
143
147
|
self.ff_layer = self._original_mod.layer[2]
|
144
148
|
|
145
149
|
def pre_self_attn_layer_norm(self, hidden_states):
|
optimum/rbln/transformers/models/{time_series_transformers → time_series_transformer}/__init__.py
RENAMED
@@ -23,4 +23,4 @@
|
|
23
23
|
|
24
24
|
from ....ops import paged_add_softmax_attn_decode, rbln_cache_update
|
25
25
|
from .configuration_time_series_transformer import RBLNTimeSeriesTransformerForPredictionConfig
|
26
|
-
from .
|
26
|
+
from .modeling_time_series_transformer import RBLNTimeSeriesTransformerForPrediction
|
@@ -1,4 +1,4 @@
|
|
1
|
-
from typing import Optional
|
1
|
+
from typing import Any, Dict, Optional
|
2
2
|
|
3
3
|
from ....configuration_utils import RBLNModelConfig
|
4
4
|
|
@@ -10,7 +10,7 @@ class RBLNTimeSeriesTransformerForPredictionConfig(RBLNModelConfig):
|
|
10
10
|
enc_max_seq_len: Optional[int] = None,
|
11
11
|
dec_max_seq_len: Optional[int] = None,
|
12
12
|
num_parallel_samples: Optional[int] = None,
|
13
|
-
**kwargs,
|
13
|
+
**kwargs: Dict[str, Any],
|
14
14
|
):
|
15
15
|
"""
|
16
16
|
Args:
|
@@ -120,6 +120,17 @@ class RBLNSeq2SeqTSDecoderOutput(ModelOutput):
|
|
120
120
|
|
121
121
|
|
122
122
|
class RBLNTimeSeriesTransformerForPrediction(RBLNModel):
|
123
|
+
"""
|
124
|
+
The Time Series Transformer Model with a distribution head on top for time-series forecasting. e.g., for datasets like M4, NN5, or other time series forecasting benchmarks.
|
125
|
+
This model inherits from [`RBLNModel`]. Check the superclass documentation for the generic methods the library implements for all its models.
|
126
|
+
|
127
|
+
A class to convert and run pre-trained transformer-based `TimeSeriesTransformerForPrediction` models on RBLN devices.
|
128
|
+
It implements the methods to convert a pre-trained transformers `TimeSeriesTransformerForPrediction` model into a RBLN transformer model by:
|
129
|
+
|
130
|
+
- transferring the checkpoint weights of the original into an optimized RBLN graph,
|
131
|
+
- compiling the resulting graph using the RBLN Compiler.
|
132
|
+
"""
|
133
|
+
|
123
134
|
auto_model_class = None
|
124
135
|
main_input_name = "inputs_embeds"
|
125
136
|
|
@@ -144,11 +155,6 @@ class RBLNTimeSeriesTransformerForPrediction(RBLNModel):
|
|
144
155
|
)
|
145
156
|
|
146
157
|
def __getattr__(self, __name: str) -> Any:
|
147
|
-
"""This is the key method to implement RBLN-TimeSeriesTransformersForPrediction.
|
148
|
-
Returns:
|
149
|
-
Any: TimeSeriesTransformersForPrediction's corresponding method
|
150
|
-
"""
|
151
|
-
|
152
158
|
def redirect(func):
|
153
159
|
return lambda *pargs, **kwargs: func(self, *pargs, **kwargs)
|
154
160
|
|
@@ -211,10 +217,9 @@ class RBLNTimeSeriesTransformerForPrediction(RBLNModel):
|
|
211
217
|
subfolder: str,
|
212
218
|
rbln_config: RBLNTimeSeriesTransformerForPredictionConfig,
|
213
219
|
):
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
"""
|
220
|
+
# If you are unavoidably running on a CPU rather than an RBLN device,
|
221
|
+
# store the torch tensor, weight, etc. in this function.
|
222
|
+
|
218
223
|
save_dict = {}
|
219
224
|
save_dict["embedder"] = model.model.embedder.state_dict()
|
220
225
|
torch.save(save_dict, save_dir_path / subfolder / "torch_artifacts.pth")
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# Copyright 2025 Rebellions Inc. All rights reserved.
|
2
|
+
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at:
|
6
|
+
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|
15
|
+
from .configuration_vit import RBLNViTForImageClassificationConfig
|
16
|
+
from .modeling_vit import RBLNViTForImageClassification
|
17
|
+
|
18
|
+
|
19
|
+
__all__ = ["RBLNViTForImageClassificationConfig", "RBLNViTForImageClassification"]
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# Copyright 2025 Rebellions Inc. All rights reserved.
|
2
|
+
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at:
|
6
|
+
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|
15
|
+
from ...configuration_generic import RBLNModelForImageClassificationConfig
|
16
|
+
|
17
|
+
|
18
|
+
class RBLNViTForImageClassificationConfig(RBLNModelForImageClassificationConfig):
|
19
|
+
""
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# Copyright 2025 Rebellions Inc. All rights reserved.
|
2
|
+
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at:
|
6
|
+
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|
15
|
+
from ...modeling_generic import RBLNModelForImageClassification
|
16
|
+
|
17
|
+
|
18
|
+
class RBLNViTForImageClassification(RBLNModelForImageClassification):
|
19
|
+
""
|
@@ -12,5 +12,5 @@
|
|
12
12
|
# See the License for the specific language governing permissions and
|
13
13
|
# limitations under the License.
|
14
14
|
|
15
|
-
from .
|
15
|
+
from .configuration_wav2vec2 import RBLNWav2Vec2ForCTCConfig
|
16
16
|
from .modeling_wav2vec2 import RBLNWav2Vec2ForCTC
|
@@ -17,7 +17,7 @@ import torch
|
|
17
17
|
from transformers import AutoModelForMaskedLM, Wav2Vec2ForCTC
|
18
18
|
|
19
19
|
from ...modeling_generic import RBLNModelForMaskedLM
|
20
|
-
from .
|
20
|
+
from .configuration_wav2vec2 import RBLNWav2Vec2ForCTCConfig
|
21
21
|
|
22
22
|
|
23
23
|
class _Wav2Vec2(torch.nn.Module):
|
@@ -12,6 +12,8 @@
|
|
12
12
|
# See the License for the specific language governing permissions and
|
13
13
|
# limitations under the License.
|
14
14
|
|
15
|
+
from typing import Any, Dict
|
16
|
+
|
15
17
|
import rebel
|
16
18
|
|
17
19
|
from ....configuration_utils import RBLNModelConfig
|
@@ -29,7 +31,7 @@ class RBLNWhisperForConditionalGenerationConfig(RBLNModelConfig):
|
|
29
31
|
use_attention_mask: bool = None,
|
30
32
|
enc_max_seq_len: int = None,
|
31
33
|
dec_max_seq_len: int = None,
|
32
|
-
**kwargs,
|
34
|
+
**kwargs: Dict[str, Any],
|
33
35
|
):
|
34
36
|
"""
|
35
37
|
Args:
|
@@ -104,13 +104,44 @@ class RBLNRuntimeDecoder(RBLNPytorchRuntime):
|
|
104
104
|
|
105
105
|
class RBLNWhisperForConditionalGeneration(RBLNModel, RBLNWhisperGenerationMixin):
|
106
106
|
"""
|
107
|
-
|
108
|
-
This model inherits from [`RBLNDecoderOnlyModelForCausalLM`]. Check the superclass documentation for the generic methods the library implements for all its models.
|
107
|
+
Whisper model for speech recognition and transcription optimized for RBLN NPU.
|
109
108
|
|
110
|
-
|
111
|
-
|
109
|
+
This model inherits from [`RBLNModel`]. It implements the methods to convert and run
|
110
|
+
pre-trained transformers based Whisper model on RBLN devices by:
|
112
111
|
- transferring the checkpoint weights of the original into an optimized RBLN graph,
|
113
112
|
- compiling the resulting graph using the RBLN compiler.
|
113
|
+
|
114
|
+
Example (Short form):
|
115
|
+
```python
|
116
|
+
import torch
|
117
|
+
from transformers import AutoProcessor
|
118
|
+
from datasets import load_dataset
|
119
|
+
from optimum.rbln import RBLNWhisperForConditionalGeneration
|
120
|
+
|
121
|
+
# Load processor and dataset
|
122
|
+
model_id = "openai/whisper-tiny"
|
123
|
+
processor = AutoProcessor.from_pretrained(model_id)
|
124
|
+
ds = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation")
|
125
|
+
|
126
|
+
# Prepare input features
|
127
|
+
input_features = processor(
|
128
|
+
ds[0]["audio"]["array"],
|
129
|
+
sampling_rate=ds[0]["audio"]["sampling_rate"],
|
130
|
+
return_tensors="pt"
|
131
|
+
).input_features
|
132
|
+
|
133
|
+
# Load and compile model (or load pre-compiled model)
|
134
|
+
model = RBLNWhisperForConditionalGeneration.from_pretrained(
|
135
|
+
model_id=model_id,
|
136
|
+
export=True,
|
137
|
+
rbln_batch_size=1
|
138
|
+
)
|
139
|
+
|
140
|
+
# Generate transcription
|
141
|
+
outputs = model.generate(input_features=input_features, return_timestamps=True)
|
142
|
+
transcription = processor.batch_decode(outputs, skip_special_tokens=True)[0]
|
143
|
+
print(f"Transcription: {transcription}")
|
144
|
+
```
|
114
145
|
"""
|
115
146
|
|
116
147
|
auto_model_class = AutoModelForSpeechSeq2Seq
|
@@ -153,11 +184,6 @@ class RBLNWhisperForConditionalGeneration(RBLNModel, RBLNWhisperGenerationMixin)
|
|
153
184
|
return self.decoder
|
154
185
|
|
155
186
|
def __getattr__(self, __name: str) -> Any:
|
156
|
-
"""This is the key method to implement RBLN-Whisper.
|
157
|
-
Returns:
|
158
|
-
Any: Whisper's corresponding method
|
159
|
-
"""
|
160
|
-
|
161
187
|
def redirect(func):
|
162
188
|
return lambda *pargs, **kwargs: func(self, *pargs, **kwargs)
|
163
189
|
|
@@ -331,12 +357,6 @@ class RBLNWhisperForConditionalGeneration(RBLNModel, RBLNWhisperGenerationMixin)
|
|
331
357
|
attention_mask: Optional[torch.Tensor] = None, # need for support transformers>=4.45.0
|
332
358
|
**kwargs,
|
333
359
|
):
|
334
|
-
"""
|
335
|
-
whisper don't use attention_mask,
|
336
|
-
attention_mask (`torch.Tensor`)`, *optional*):
|
337
|
-
Whisper does not support masking of the `input_features`, this argument is preserved for compatibility,
|
338
|
-
but it is not used. By default the silence in the input log mel spectrogram are ignored.
|
339
|
-
"""
|
340
360
|
return {
|
341
361
|
"input_ids": input_ids,
|
342
362
|
"cache_position": cache_position,
|
@@ -12,5 +12,19 @@
|
|
12
12
|
# See the License for the specific language governing permissions and
|
13
13
|
# limitations under the License.
|
14
14
|
|
15
|
-
from .configuration_xlm_roberta import
|
16
|
-
|
15
|
+
from .configuration_xlm_roberta import (
|
16
|
+
RBLNXLMRobertaForSequenceClassificationConfig,
|
17
|
+
RBLNXLMRobertaModelConfig,
|
18
|
+
)
|
19
|
+
from .modeling_xlm_roberta import (
|
20
|
+
RBLNXLMRobertaForSequenceClassification,
|
21
|
+
RBLNXLMRobertaModel,
|
22
|
+
)
|
23
|
+
|
24
|
+
|
25
|
+
__all__ = [
|
26
|
+
"RBLNXLMRobertaModelConfig",
|
27
|
+
"RBLNXLMRobertaForSequenceClassificationConfig",
|
28
|
+
"RBLNXLMRobertaModel",
|
29
|
+
"RBLNXLMRobertaForSequenceClassification",
|
30
|
+
]
|
@@ -12,8 +12,21 @@
|
|
12
12
|
# See the License for the specific language governing permissions and
|
13
13
|
# limitations under the License.
|
14
14
|
|
15
|
-
from ...configuration_generic import
|
15
|
+
from ...configuration_generic import (
|
16
|
+
RBLNModelForSequenceClassificationConfig,
|
17
|
+
RBLNTransformerEncoderForFeatureExtractionConfig,
|
18
|
+
)
|
16
19
|
|
17
20
|
|
18
21
|
class RBLNXLMRobertaModelConfig(RBLNTransformerEncoderForFeatureExtractionConfig):
|
19
|
-
|
22
|
+
"""
|
23
|
+
Configuration class for XLM-RoBERTa model.
|
24
|
+
Inherits from RBLNTransformerEncoderForFeatureExtractionConfig with no additional parameters.
|
25
|
+
"""
|
26
|
+
|
27
|
+
|
28
|
+
class RBLNXLMRobertaForSequenceClassificationConfig(RBLNModelForSequenceClassificationConfig):
|
29
|
+
"""
|
30
|
+
Configuration class for XLM-RoBERTa sequence classification model.
|
31
|
+
Inherits from RBLNModelForSequenceClassificationConfig with no additional parameters.
|
32
|
+
"""
|
@@ -12,9 +12,18 @@
|
|
12
12
|
# See the License for the specific language governing permissions and
|
13
13
|
# limitations under the License.
|
14
14
|
|
15
|
-
|
16
|
-
from ...modeling_generic import RBLNTransformerEncoderForFeatureExtraction
|
15
|
+
from ...modeling_generic import RBLNModelForSequenceClassification, RBLNTransformerEncoderForFeatureExtraction
|
17
16
|
|
18
17
|
|
19
18
|
class RBLNXLMRobertaModel(RBLNTransformerEncoderForFeatureExtraction):
|
20
|
-
|
19
|
+
"""
|
20
|
+
XLM-RoBERTa base model optimized for RBLN NPU.
|
21
|
+
"""
|
22
|
+
|
23
|
+
|
24
|
+
class RBLNXLMRobertaForSequenceClassification(RBLNModelForSequenceClassification):
|
25
|
+
"""
|
26
|
+
XLM-RoBERTa model for sequence classification tasks optimized for RBLN NPU.
|
27
|
+
"""
|
28
|
+
|
29
|
+
rbln_model_input_names = ["input_ids", "attention_mask"]
|
@@ -12,10 +12,20 @@
|
|
12
12
|
# See the License for the specific language governing permissions and
|
13
13
|
# limitations under the License.
|
14
14
|
|
15
|
+
import importlib
|
16
|
+
from typing import TYPE_CHECKING, Type
|
17
|
+
|
18
|
+
|
19
|
+
if TYPE_CHECKING:
|
20
|
+
from ..modeling import RBLNModel
|
21
|
+
|
15
22
|
# Prefix used for RBLN model class names
|
16
23
|
RBLN_PREFIX = "RBLN"
|
17
24
|
|
18
25
|
|
26
|
+
MODEL_MAPPING = {}
|
27
|
+
|
28
|
+
|
19
29
|
def convert_hf_to_rbln_model_name(hf_model_name: str):
|
20
30
|
"""
|
21
31
|
Convert HuggingFace model name to RBLN model name.
|
@@ -41,3 +51,13 @@ def convert_rbln_to_hf_model_name(rbln_model_name: str):
|
|
41
51
|
"""
|
42
52
|
|
43
53
|
return rbln_model_name.removeprefix(RBLN_PREFIX)
|
54
|
+
|
55
|
+
|
56
|
+
def get_rbln_model_cls(cls_name: str) -> Type["RBLNModel"]:
|
57
|
+
cls = getattr(importlib.import_module("optimum.rbln"), cls_name, None)
|
58
|
+
if cls is None:
|
59
|
+
if cls_name in MODEL_MAPPING:
|
60
|
+
cls = MODEL_MAPPING[cls_name]
|
61
|
+
else:
|
62
|
+
raise AttributeError(f"RBLNModel for {cls_name} not found.")
|
63
|
+
return cls
|
optimum/rbln/utils/submodule.py
CHANGED
@@ -12,19 +12,19 @@
|
|
12
12
|
# See the License for the specific language governing permissions and
|
13
13
|
# limitations under the License.
|
14
14
|
|
15
|
-
import importlib
|
16
15
|
from pathlib import Path
|
17
16
|
from typing import TYPE_CHECKING, Any, Dict, List, Type
|
18
17
|
|
19
18
|
from transformers import PretrainedConfig
|
20
19
|
|
21
20
|
from ..configuration_utils import RBLNModelConfig
|
21
|
+
from ..utils.model_utils import get_rbln_model_cls
|
22
22
|
|
23
23
|
|
24
24
|
if TYPE_CHECKING:
|
25
25
|
from transformers import PreTrainedModel
|
26
26
|
|
27
|
-
from ..
|
27
|
+
from ..modeling import RBLNModel
|
28
28
|
|
29
29
|
|
30
30
|
class SubModulesMixin:
|
@@ -37,7 +37,7 @@ class SubModulesMixin:
|
|
37
37
|
|
38
38
|
_rbln_submodules: List[Dict[str, Any]] = []
|
39
39
|
|
40
|
-
def __init__(self, *, rbln_submodules: List["
|
40
|
+
def __init__(self, *, rbln_submodules: List["RBLNModel"] = [], **kwargs) -> None:
|
41
41
|
for submodule_meta, submodule in zip(self._rbln_submodules, rbln_submodules):
|
42
42
|
setattr(self, submodule_meta["name"], submodule)
|
43
43
|
|
@@ -48,7 +48,7 @@ class SubModulesMixin:
|
|
48
48
|
@classmethod
|
49
49
|
def _export_submodules_from_model(
|
50
50
|
cls, model: "PreTrainedModel", model_save_dir: str, rbln_config: RBLNModelConfig, **kwargs
|
51
|
-
) -> List["
|
51
|
+
) -> List["RBLNModel"]:
|
52
52
|
rbln_submodules = []
|
53
53
|
submodule_prefix = getattr(cls, "_rbln_submodule_prefix", None)
|
54
54
|
|
@@ -61,7 +61,7 @@ class SubModulesMixin:
|
|
61
61
|
torch_submodule: PreTrainedModel = getattr(model, submodule_name)
|
62
62
|
|
63
63
|
cls_name = torch_submodule.__class__.__name__
|
64
|
-
submodule_cls: Type["
|
64
|
+
submodule_cls: Type["RBLNModel"] = get_rbln_model_cls(f"RBLN{cls_name}")
|
65
65
|
submodule_rbln_config = getattr(rbln_config, submodule_name) or {}
|
66
66
|
|
67
67
|
if isinstance(submodule_rbln_config, dict):
|
@@ -95,9 +95,7 @@ class SubModulesMixin:
|
|
95
95
|
submodule_rbln_config = getattr(rbln_config, submodule_name)
|
96
96
|
|
97
97
|
# RBLNModelConfig -> RBLNModel
|
98
|
-
submodule_cls
|
99
|
-
importlib.import_module("optimum.rbln"), submodule_rbln_config.rbln_model_cls_name
|
100
|
-
)
|
98
|
+
submodule_cls = get_rbln_model_cls(submodule_rbln_config.rbln_model_cls_name)
|
101
99
|
|
102
100
|
json_file_path = Path(model_save_dir) / submodule_name / "config.json"
|
103
101
|
config = PretrainedConfig.from_json_file(json_file_path)
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.4
|
2
2
|
Name: optimum-rbln
|
3
|
-
Version: 0.8.
|
3
|
+
Version: 0.8.1a2
|
4
4
|
Summary: Optimum RBLN is the interface between the HuggingFace Transformers and Diffusers libraries and RBLN accelerators. It provides a set of tools enabling easy model loading and inference on single and multiple rbln device settings for different downstream tasks.
|
5
5
|
Project-URL: Homepage, https://rebellions.ai
|
6
6
|
Project-URL: Documentation, https://docs.rbln.ai
|
@@ -28,7 +28,7 @@ Requires-Dist: packaging>=24.1
|
|
28
28
|
Requires-Dist: torch==2.6.0
|
29
29
|
Requires-Dist: torchaudio<=2.6.0
|
30
30
|
Requires-Dist: torchvision<=0.21.0
|
31
|
-
Requires-Dist: transformers==4.
|
31
|
+
Requires-Dist: transformers==4.51.3
|
32
32
|
Description-Content-Type: text/markdown
|
33
33
|
|
34
34
|
|