optimum-rbln 0.9.3.post1__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.
Potentially problematic release.
This version of optimum-rbln might be problematic. Click here for more details.
- optimum/rbln/__init__.py +505 -0
- optimum/rbln/__version__.py +34 -0
- optimum/rbln/cli.py +660 -0
- optimum/rbln/configuration_utils.py +968 -0
- optimum/rbln/diffusers/__init__.py +198 -0
- optimum/rbln/diffusers/configurations/__init__.py +37 -0
- optimum/rbln/diffusers/configurations/models/__init__.py +10 -0
- optimum/rbln/diffusers/configurations/models/configuration_autoencoder_kl.py +73 -0
- optimum/rbln/diffusers/configurations/models/configuration_autoencoder_kl_cosmos.py +84 -0
- optimum/rbln/diffusers/configurations/models/configuration_autoencoder_kl_temporal_decoder.py +67 -0
- optimum/rbln/diffusers/configurations/models/configuration_controlnet.py +64 -0
- optimum/rbln/diffusers/configurations/models/configuration_prior_transformer.py +59 -0
- optimum/rbln/diffusers/configurations/models/configuration_transformer_cosmos.py +78 -0
- optimum/rbln/diffusers/configurations/models/configuration_transformer_sd3.py +63 -0
- optimum/rbln/diffusers/configurations/models/configuration_unet_2d_condition.py +81 -0
- optimum/rbln/diffusers/configurations/models/configuration_unet_spatio_temporal_condition.py +59 -0
- optimum/rbln/diffusers/configurations/models/configuration_vq_model.py +74 -0
- optimum/rbln/diffusers/configurations/pipelines/__init__.py +34 -0
- optimum/rbln/diffusers/configurations/pipelines/configuration_controlnet.py +316 -0
- optimum/rbln/diffusers/configurations/pipelines/configuration_cosmos.py +117 -0
- optimum/rbln/diffusers/configurations/pipelines/configuration_kandinsky2_2.py +363 -0
- optimum/rbln/diffusers/configurations/pipelines/configuration_stable_diffusion.py +156 -0
- optimum/rbln/diffusers/configurations/pipelines/configuration_stable_diffusion_3.py +176 -0
- optimum/rbln/diffusers/configurations/pipelines/configuration_stable_diffusion_xl.py +159 -0
- optimum/rbln/diffusers/configurations/pipelines/configuration_stable_video_diffusion.py +114 -0
- optimum/rbln/diffusers/modeling_diffusers.py +451 -0
- optimum/rbln/diffusers/models/__init__.py +64 -0
- optimum/rbln/diffusers/models/autoencoders/__init__.py +18 -0
- optimum/rbln/diffusers/models/autoencoders/autoencoder_kl.py +255 -0
- optimum/rbln/diffusers/models/autoencoders/autoencoder_kl_cosmos.py +245 -0
- optimum/rbln/diffusers/models/autoencoders/autoencoder_kl_temporal_decoder.py +275 -0
- optimum/rbln/diffusers/models/autoencoders/vae.py +178 -0
- optimum/rbln/diffusers/models/autoencoders/vq_model.py +211 -0
- optimum/rbln/diffusers/models/controlnet.py +281 -0
- optimum/rbln/diffusers/models/transformers/__init__.py +17 -0
- optimum/rbln/diffusers/models/transformers/prior_transformer.py +160 -0
- optimum/rbln/diffusers/models/transformers/transformer_cosmos.py +344 -0
- optimum/rbln/diffusers/models/transformers/transformer_sd3.py +191 -0
- optimum/rbln/diffusers/models/unets/__init__.py +16 -0
- optimum/rbln/diffusers/models/unets/unet_2d_condition.py +408 -0
- optimum/rbln/diffusers/models/unets/unet_spatio_temporal_condition.py +201 -0
- optimum/rbln/diffusers/pipelines/__init__.py +113 -0
- optimum/rbln/diffusers/pipelines/auto_pipeline.py +307 -0
- optimum/rbln/diffusers/pipelines/controlnet/__init__.py +19 -0
- optimum/rbln/diffusers/pipelines/controlnet/multicontrolnet.py +139 -0
- optimum/rbln/diffusers/pipelines/controlnet/pipeline_controlnet.py +669 -0
- optimum/rbln/diffusers/pipelines/controlnet/pipeline_controlnet_img2img.py +640 -0
- optimum/rbln/diffusers/pipelines/controlnet/pipeline_controlnet_sd_xl.py +825 -0
- optimum/rbln/diffusers/pipelines/controlnet/pipeline_controlnet_sd_xl_img2img.py +837 -0
- optimum/rbln/diffusers/pipelines/cosmos/__init__.py +17 -0
- optimum/rbln/diffusers/pipelines/cosmos/configuration_cosmos_guardrail.py +113 -0
- optimum/rbln/diffusers/pipelines/cosmos/cosmos_guardrail.py +425 -0
- optimum/rbln/diffusers/pipelines/cosmos/pipeline_cosmos_text2world.py +128 -0
- optimum/rbln/diffusers/pipelines/cosmos/pipeline_cosmos_video2world.py +128 -0
- optimum/rbln/diffusers/pipelines/kandinsky2_2/__init__.py +23 -0
- optimum/rbln/diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2.py +34 -0
- optimum/rbln/diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2_combined.py +207 -0
- optimum/rbln/diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2_img2img.py +34 -0
- optimum/rbln/diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2_inpaint.py +34 -0
- optimum/rbln/diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2_prior.py +31 -0
- optimum/rbln/diffusers/pipelines/stable_diffusion/__init__.py +17 -0
- optimum/rbln/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion.py +32 -0
- optimum/rbln/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_img2img.py +31 -0
- optimum/rbln/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_inpaint.py +31 -0
- optimum/rbln/diffusers/pipelines/stable_diffusion_3/__init__.py +17 -0
- optimum/rbln/diffusers/pipelines/stable_diffusion_3/pipeline_stable_diffusion_3.py +31 -0
- optimum/rbln/diffusers/pipelines/stable_diffusion_3/pipeline_stable_diffusion_3_img2img.py +31 -0
- optimum/rbln/diffusers/pipelines/stable_diffusion_3/pipeline_stable_diffusion_3_inpaint.py +31 -0
- optimum/rbln/diffusers/pipelines/stable_diffusion_xl/__init__.py +17 -0
- optimum/rbln/diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl.py +31 -0
- optimum/rbln/diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl_img2img.py +31 -0
- optimum/rbln/diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl_inpaint.py +31 -0
- optimum/rbln/diffusers/pipelines/stable_video_diffusion/__init__.py +15 -0
- optimum/rbln/diffusers/pipelines/stable_video_diffusion/pipeline_stable_video_diffusion.py +46 -0
- optimum/rbln/modeling.py +364 -0
- optimum/rbln/modeling_base.py +637 -0
- optimum/rbln/ops/__init__.py +19 -0
- optimum/rbln/ops/attn.py +455 -0
- optimum/rbln/ops/flash_attn.py +350 -0
- optimum/rbln/ops/kv_cache_update.py +29 -0
- optimum/rbln/ops/linear.py +32 -0
- optimum/rbln/ops/sliding_window_attn.py +111 -0
- optimum/rbln/transformers/__init__.py +340 -0
- optimum/rbln/transformers/configuration_generic.py +120 -0
- optimum/rbln/transformers/modeling_attention_utils.py +385 -0
- optimum/rbln/transformers/modeling_generic.py +280 -0
- optimum/rbln/transformers/modeling_outputs.py +37 -0
- optimum/rbln/transformers/modeling_rope_utils.py +314 -0
- optimum/rbln/transformers/models/__init__.py +343 -0
- optimum/rbln/transformers/models/audio_spectrogram_transformer/__init__.py +17 -0
- optimum/rbln/transformers/models/audio_spectrogram_transformer/configuration_audio_spectrogram_transformer.py +47 -0
- optimum/rbln/transformers/models/audio_spectrogram_transformer/modeling_audio_spectrogram_transformer.py +91 -0
- optimum/rbln/transformers/models/auto/__init__.py +31 -0
- optimum/rbln/transformers/models/auto/auto_factory.py +267 -0
- optimum/rbln/transformers/models/auto/modeling_auto.py +162 -0
- optimum/rbln/transformers/models/bart/__init__.py +17 -0
- optimum/rbln/transformers/models/bart/bart_architecture.py +163 -0
- optimum/rbln/transformers/models/bart/configuration_bart.py +36 -0
- optimum/rbln/transformers/models/bart/modeling_bart.py +86 -0
- optimum/rbln/transformers/models/bert/__init__.py +16 -0
- optimum/rbln/transformers/models/bert/bert_architecture.py +16 -0
- optimum/rbln/transformers/models/bert/configuration_bert.py +46 -0
- optimum/rbln/transformers/models/bert/modeling_bert.py +148 -0
- optimum/rbln/transformers/models/blip_2/__init__.py +20 -0
- optimum/rbln/transformers/models/blip_2/configuration_blip_2.py +115 -0
- optimum/rbln/transformers/models/blip_2/modeling_blip_2.py +526 -0
- optimum/rbln/transformers/models/clip/__init__.py +26 -0
- optimum/rbln/transformers/models/clip/configuration_clip.py +103 -0
- optimum/rbln/transformers/models/clip/modeling_clip.py +384 -0
- optimum/rbln/transformers/models/colpali/__init__.py +2 -0
- optimum/rbln/transformers/models/colpali/colpali_architecture.py +218 -0
- optimum/rbln/transformers/models/colpali/configuration_colpali.py +84 -0
- optimum/rbln/transformers/models/colpali/modeling_colpali.py +361 -0
- optimum/rbln/transformers/models/colqwen2/__init__.py +2 -0
- optimum/rbln/transformers/models/colqwen2/colqwen2_architecture.py +233 -0
- optimum/rbln/transformers/models/colqwen2/configuration_colqwen2.py +74 -0
- optimum/rbln/transformers/models/colqwen2/modeling_colqwen2.py +446 -0
- optimum/rbln/transformers/models/decoderonly/__init__.py +27 -0
- optimum/rbln/transformers/models/decoderonly/configuration_decoderonly.py +300 -0
- optimum/rbln/transformers/models/decoderonly/configuration_lora.py +411 -0
- optimum/rbln/transformers/models/decoderonly/decoderonly_architecture.py +1224 -0
- optimum/rbln/transformers/models/decoderonly/decoderonly_runtime_utils.py +508 -0
- optimum/rbln/transformers/models/decoderonly/generation_decoderonly.py +119 -0
- optimum/rbln/transformers/models/decoderonly/lora_architecture.py +204 -0
- optimum/rbln/transformers/models/decoderonly/modeling_decoderonly.py +823 -0
- optimum/rbln/transformers/models/depth_anything/__init__.py +16 -0
- optimum/rbln/transformers/models/depth_anything/configuration_depth_anything.py +24 -0
- optimum/rbln/transformers/models/depth_anything/modeling_depth_anything.py +42 -0
- optimum/rbln/transformers/models/distilbert/__init__.py +19 -0
- optimum/rbln/transformers/models/distilbert/configuration_distilbert.py +24 -0
- optimum/rbln/transformers/models/distilbert/modeling_distilbert.py +51 -0
- optimum/rbln/transformers/models/dpt/__init__.py +16 -0
- optimum/rbln/transformers/models/dpt/configuration_dpt.py +24 -0
- optimum/rbln/transformers/models/dpt/modeling_dpt.py +42 -0
- optimum/rbln/transformers/models/exaone/__init__.py +24 -0
- optimum/rbln/transformers/models/exaone/configuration_exaone.py +42 -0
- optimum/rbln/transformers/models/exaone/exaone_architecture.py +77 -0
- optimum/rbln/transformers/models/exaone/modeling_exaone.py +145 -0
- optimum/rbln/transformers/models/gemma/__init__.py +16 -0
- optimum/rbln/transformers/models/gemma/configuration_gemma.py +50 -0
- optimum/rbln/transformers/models/gemma/gemma_architecture.py +27 -0
- optimum/rbln/transformers/models/gemma/modeling_gemma.py +104 -0
- optimum/rbln/transformers/models/gemma3/__init__.py +16 -0
- optimum/rbln/transformers/models/gemma3/configuration_gemma3.py +109 -0
- optimum/rbln/transformers/models/gemma3/gemma3_architecture.py +170 -0
- optimum/rbln/transformers/models/gemma3/gemma3_runtime_utils.py +245 -0
- optimum/rbln/transformers/models/gemma3/modeling_gemma3.py +611 -0
- optimum/rbln/transformers/models/gpt2/__init__.py +16 -0
- optimum/rbln/transformers/models/gpt2/configuration_gpt2.py +50 -0
- optimum/rbln/transformers/models/gpt2/gpt2_architecture.py +93 -0
- optimum/rbln/transformers/models/gpt2/modeling_gpt2.py +55 -0
- optimum/rbln/transformers/models/grounding_dino/__init__.py +10 -0
- optimum/rbln/transformers/models/grounding_dino/configuration_grounding_dino.py +92 -0
- optimum/rbln/transformers/models/grounding_dino/grounding_dino_architecture.py +599 -0
- optimum/rbln/transformers/models/grounding_dino/modeling_grounding_dino.py +1048 -0
- optimum/rbln/transformers/models/idefics3/__init__.py +16 -0
- optimum/rbln/transformers/models/idefics3/configuration_idefics3.py +89 -0
- optimum/rbln/transformers/models/idefics3/modeling_idefics3.py +497 -0
- optimum/rbln/transformers/models/llama/__init__.py +16 -0
- optimum/rbln/transformers/models/llama/configuration_llama.py +50 -0
- optimum/rbln/transformers/models/llama/llama_architecture.py +19 -0
- optimum/rbln/transformers/models/llama/modeling_llama.py +104 -0
- optimum/rbln/transformers/models/llava/__init__.py +16 -0
- optimum/rbln/transformers/models/llava/configuration_llava.py +72 -0
- optimum/rbln/transformers/models/llava/modeling_llava.py +490 -0
- optimum/rbln/transformers/models/llava_next/__init__.py +16 -0
- optimum/rbln/transformers/models/llava_next/configuration_llava_next.py +69 -0
- optimum/rbln/transformers/models/llava_next/modeling_llava_next.py +493 -0
- optimum/rbln/transformers/models/midm/__init__.py +24 -0
- optimum/rbln/transformers/models/midm/configuration_midm.py +42 -0
- optimum/rbln/transformers/models/midm/midm_architecture.py +144 -0
- optimum/rbln/transformers/models/midm/modeling_midm.py +144 -0
- optimum/rbln/transformers/models/mistral/__init__.py +16 -0
- optimum/rbln/transformers/models/mistral/configuration_mistral.py +50 -0
- optimum/rbln/transformers/models/mistral/mistral_architecture.py +19 -0
- optimum/rbln/transformers/models/mistral/modeling_mistral.py +115 -0
- optimum/rbln/transformers/models/opt/__init__.py +16 -0
- optimum/rbln/transformers/models/opt/configuration_opt.py +29 -0
- optimum/rbln/transformers/models/opt/modeling_opt.py +102 -0
- optimum/rbln/transformers/models/opt/opt_architecture.py +74 -0
- optimum/rbln/transformers/models/pegasus/__init__.py +17 -0
- optimum/rbln/transformers/models/pegasus/configuration_pegasus.py +38 -0
- optimum/rbln/transformers/models/pegasus/modeling_pegasus.py +71 -0
- optimum/rbln/transformers/models/pegasus/pegasus_architecture.py +161 -0
- optimum/rbln/transformers/models/phi/__init__.py +16 -0
- optimum/rbln/transformers/models/phi/configuration_phi.py +50 -0
- optimum/rbln/transformers/models/phi/modeling_phi.py +92 -0
- optimum/rbln/transformers/models/phi/phi_architecture.py +115 -0
- optimum/rbln/transformers/models/pixtral/__init__.py +16 -0
- optimum/rbln/transformers/models/pixtral/configuration_pixtral.py +43 -0
- optimum/rbln/transformers/models/pixtral/modeling_pixtral.py +322 -0
- optimum/rbln/transformers/models/pixtral/pixtral_architecture.py +73 -0
- optimum/rbln/transformers/models/qwen2/__init__.py +16 -0
- optimum/rbln/transformers/models/qwen2/configuration_qwen2.py +50 -0
- optimum/rbln/transformers/models/qwen2/modeling_qwen2.py +123 -0
- optimum/rbln/transformers/models/qwen2/qwen2_architecture.py +19 -0
- optimum/rbln/transformers/models/qwen2_5_vl/__init__.py +19 -0
- optimum/rbln/transformers/models/qwen2_5_vl/configuration_qwen2_5_vl.py +111 -0
- optimum/rbln/transformers/models/qwen2_5_vl/modeling_qwen2_5_vl.py +636 -0
- optimum/rbln/transformers/models/qwen2_5_vl/qwen2_5_vl_architecture.py +220 -0
- optimum/rbln/transformers/models/qwen2_vl/__init__.py +19 -0
- optimum/rbln/transformers/models/qwen2_vl/configuration_qwen2_vl.py +88 -0
- optimum/rbln/transformers/models/qwen2_vl/modeling_qwen2_vl.py +513 -0
- optimum/rbln/transformers/models/qwen2_vl/qwen2_vl_architecture.py +165 -0
- optimum/rbln/transformers/models/qwen3/__init__.py +16 -0
- optimum/rbln/transformers/models/qwen3/configuration_qwen3.py +71 -0
- optimum/rbln/transformers/models/qwen3/modeling_qwen3.py +133 -0
- optimum/rbln/transformers/models/qwen3/qwen3_architecture.py +31 -0
- optimum/rbln/transformers/models/resnet/__init__.py +23 -0
- optimum/rbln/transformers/models/resnet/configuration_resnet.py +42 -0
- optimum/rbln/transformers/models/resnet/modeling_resnet.py +99 -0
- optimum/rbln/transformers/models/roberta/__init__.py +24 -0
- optimum/rbln/transformers/models/roberta/configuration_roberta.py +33 -0
- optimum/rbln/transformers/models/roberta/modeling_roberta.py +72 -0
- optimum/rbln/transformers/models/seq2seq/__init__.py +16 -0
- optimum/rbln/transformers/models/seq2seq/configuration_seq2seq.py +71 -0
- optimum/rbln/transformers/models/seq2seq/modeling_seq2seq.py +477 -0
- optimum/rbln/transformers/models/seq2seq/seq2seq_architecture.py +527 -0
- optimum/rbln/transformers/models/siglip/__init__.py +16 -0
- optimum/rbln/transformers/models/siglip/configuration_siglip.py +76 -0
- optimum/rbln/transformers/models/siglip/modeling_siglip.py +199 -0
- optimum/rbln/transformers/models/swin/__init__.py +16 -0
- optimum/rbln/transformers/models/swin/configuration_swin.py +42 -0
- optimum/rbln/transformers/models/swin/modeling_swin.py +354 -0
- optimum/rbln/transformers/models/t5/__init__.py +17 -0
- optimum/rbln/transformers/models/t5/configuration_t5.py +36 -0
- optimum/rbln/transformers/models/t5/modeling_t5.py +130 -0
- optimum/rbln/transformers/models/t5/t5_architecture.py +264 -0
- optimum/rbln/transformers/models/time_series_transformer/__init__.py +26 -0
- optimum/rbln/transformers/models/time_series_transformer/configuration_time_series_transformer.py +41 -0
- optimum/rbln/transformers/models/time_series_transformer/modeling_time_series_transformer.py +435 -0
- optimum/rbln/transformers/models/time_series_transformer/time_series_transformers_architecture.py +337 -0
- optimum/rbln/transformers/models/vit/__init__.py +19 -0
- optimum/rbln/transformers/models/vit/configuration_vit.py +24 -0
- optimum/rbln/transformers/models/vit/modeling_vit.py +44 -0
- optimum/rbln/transformers/models/wav2vec2/__init__.py +16 -0
- optimum/rbln/transformers/models/wav2vec2/configuration_wav2vec2.py +38 -0
- optimum/rbln/transformers/models/wav2vec2/modeling_wav2vec2.py +104 -0
- optimum/rbln/transformers/models/whisper/__init__.py +17 -0
- optimum/rbln/transformers/models/whisper/configuration_whisper.py +72 -0
- optimum/rbln/transformers/models/whisper/generation_whisper.py +159 -0
- optimum/rbln/transformers/models/whisper/modeling_whisper.py +475 -0
- optimum/rbln/transformers/models/whisper/whisper_architecture.py +349 -0
- optimum/rbln/transformers/models/xlm_roberta/__init__.py +24 -0
- optimum/rbln/transformers/models/xlm_roberta/configuration_xlm_roberta.py +32 -0
- optimum/rbln/transformers/models/xlm_roberta/modeling_xlm_roberta.py +82 -0
- optimum/rbln/transformers/utils/__init__.py +0 -0
- optimum/rbln/transformers/utils/rbln_quantization.py +589 -0
- optimum/rbln/transformers/utils/rbln_runtime_wrapper.py +79 -0
- optimum/rbln/utils/__init__.py +16 -0
- optimum/rbln/utils/decorator_utils.py +86 -0
- optimum/rbln/utils/deprecation.py +213 -0
- optimum/rbln/utils/hub.py +94 -0
- optimum/rbln/utils/import_utils.py +170 -0
- optimum/rbln/utils/logging.py +110 -0
- optimum/rbln/utils/model_utils.py +63 -0
- optimum/rbln/utils/runtime_utils.py +249 -0
- optimum/rbln/utils/save_utils.py +102 -0
- optimum/rbln/utils/submodule.py +152 -0
- optimum_rbln-0.9.3.post1.dist-info/METADATA +124 -0
- optimum_rbln-0.9.3.post1.dist-info/RECORD +264 -0
- optimum_rbln-0.9.3.post1.dist-info/WHEEL +4 -0
- optimum_rbln-0.9.3.post1.dist-info/entry_points.txt +2 -0
- optimum_rbln-0.9.3.post1.dist-info/licenses/LICENSE +201 -0
|
@@ -0,0 +1,199 @@
|
|
|
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 typing import TYPE_CHECKING, Any, Optional, Tuple, Union
|
|
16
|
+
|
|
17
|
+
import torch
|
|
18
|
+
from transformers import SiglipVisionConfig, SiglipVisionModel
|
|
19
|
+
from transformers.modeling_outputs import BaseModelOutputWithPooling
|
|
20
|
+
|
|
21
|
+
from ....configuration_utils import RBLNCompileConfig
|
|
22
|
+
from ....modeling import RBLNModel
|
|
23
|
+
from ....utils.logging import get_logger
|
|
24
|
+
from .configuration_siglip import RBLNSiglipVisionModelConfig
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
logger = get_logger(__name__)
|
|
28
|
+
|
|
29
|
+
if TYPE_CHECKING:
|
|
30
|
+
from transformers import AutoFeatureExtractor, AutoProcessor, AutoTokenizer, PreTrainedModel
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class _SiglipVisionModel(torch.nn.Module):
|
|
34
|
+
def __init__(
|
|
35
|
+
self,
|
|
36
|
+
model: SiglipVisionModel,
|
|
37
|
+
interpolate_pos_encoding: bool,
|
|
38
|
+
output_hidden_states: bool,
|
|
39
|
+
output_attentions: bool,
|
|
40
|
+
):
|
|
41
|
+
super().__init__()
|
|
42
|
+
self.vision_model = model.vision_model
|
|
43
|
+
self.interpolate_pos_encoding = interpolate_pos_encoding
|
|
44
|
+
self.output_hidden_states = output_hidden_states
|
|
45
|
+
self.output_attentions = output_attentions
|
|
46
|
+
|
|
47
|
+
def forward(self, inp):
|
|
48
|
+
enc_out = self.vision_model(
|
|
49
|
+
inp,
|
|
50
|
+
output_hidden_states=self.output_hidden_states,
|
|
51
|
+
return_dict=False,
|
|
52
|
+
interpolate_pos_encoding=self.interpolate_pos_encoding,
|
|
53
|
+
output_attentions=self.output_attentions,
|
|
54
|
+
)
|
|
55
|
+
return tuple(x for x in enc_out if x is not None)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
class RBLNSiglipVisionModel(RBLNModel):
|
|
59
|
+
"""
|
|
60
|
+
RBLN optimized SigLIP vision model.
|
|
61
|
+
|
|
62
|
+
This class provides hardware-accelerated inference for SigLIP vision models
|
|
63
|
+
on RBLN devices, supporting image encoding for multimodal vision-language tasks.
|
|
64
|
+
"""
|
|
65
|
+
|
|
66
|
+
_tp_support = False
|
|
67
|
+
|
|
68
|
+
@classmethod
|
|
69
|
+
def _wrap_model_if_needed(
|
|
70
|
+
cls, model: torch.nn.Module, rbln_config: RBLNSiglipVisionModelConfig
|
|
71
|
+
) -> torch.nn.Module:
|
|
72
|
+
wrapper_cfg = {
|
|
73
|
+
"interpolate_pos_encoding": rbln_config.interpolate_pos_encoding,
|
|
74
|
+
"output_hidden_states": rbln_config.output_hidden_states,
|
|
75
|
+
"output_attentions": rbln_config.output_attentions,
|
|
76
|
+
}
|
|
77
|
+
return _SiglipVisionModel(model, **wrapper_cfg).eval()
|
|
78
|
+
|
|
79
|
+
@classmethod
|
|
80
|
+
def _update_rbln_config(
|
|
81
|
+
cls,
|
|
82
|
+
preprocessors: Union["AutoFeatureExtractor", "AutoProcessor", "AutoTokenizer"],
|
|
83
|
+
model: Optional["PreTrainedModel"] = None,
|
|
84
|
+
model_config: "SiglipVisionConfig" = None,
|
|
85
|
+
rbln_config: Optional[RBLNSiglipVisionModelConfig] = None,
|
|
86
|
+
) -> RBLNSiglipVisionModelConfig:
|
|
87
|
+
if rbln_config.image_size is None:
|
|
88
|
+
rbln_config.image_size = getattr(model_config, "image_size", None)
|
|
89
|
+
|
|
90
|
+
if isinstance(rbln_config.image_size, int):
|
|
91
|
+
rbln_config.image_size = (rbln_config.image_size, rbln_config.image_size)
|
|
92
|
+
if rbln_config.image_size is None:
|
|
93
|
+
raise ValueError("`rbln_image_size` should be specified!")
|
|
94
|
+
|
|
95
|
+
if rbln_config.output_attentions is None:
|
|
96
|
+
rbln_config.output_attentions = getattr(model_config, "output_attentions", False)
|
|
97
|
+
if rbln_config.output_hidden_states is None:
|
|
98
|
+
rbln_config.output_hidden_states = getattr(model_config, "output_hidden_states", False)
|
|
99
|
+
|
|
100
|
+
rbln_compile_config = RBLNCompileConfig(
|
|
101
|
+
input_info=[
|
|
102
|
+
(
|
|
103
|
+
"pixel_values",
|
|
104
|
+
[
|
|
105
|
+
rbln_config.batch_size,
|
|
106
|
+
3,
|
|
107
|
+
rbln_config.image_height,
|
|
108
|
+
rbln_config.image_width,
|
|
109
|
+
],
|
|
110
|
+
"float32",
|
|
111
|
+
)
|
|
112
|
+
]
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
rbln_config.set_compile_cfgs([rbln_compile_config])
|
|
116
|
+
return rbln_config
|
|
117
|
+
|
|
118
|
+
def forward(
|
|
119
|
+
self,
|
|
120
|
+
pixel_values: torch.Tensor,
|
|
121
|
+
return_dict: bool = None,
|
|
122
|
+
output_attentions: bool = None,
|
|
123
|
+
output_hidden_states: bool = None,
|
|
124
|
+
interpolate_pos_encoding: bool = False,
|
|
125
|
+
**kwargs: Any,
|
|
126
|
+
) -> Union[Tuple, BaseModelOutputWithPooling]:
|
|
127
|
+
"""
|
|
128
|
+
Forward pass for the RBLN-optimized SigLIP vision model.
|
|
129
|
+
|
|
130
|
+
Args:
|
|
131
|
+
pixel_values (torch.FloatTensor of shape (batch_size, num_channels, image_size, image_size), optional): The tensors corresponding to the input images. Pixel values can be obtained using ViTImageProcessor. See ViTImageProcessor.call() for details (processor_class uses ViTImageProcessor for processing images).
|
|
132
|
+
return_dict (bool, optional): Whether or not to return a ModelOutput instead of a plain tuple.
|
|
133
|
+
output_attentions (bool, optional): Whether or not to return the attentions tensors of all attention layers. See attentions under returned tensors for more detail.
|
|
134
|
+
output_hidden_states (bool, optional): Whether or not to return the hidden states of all layers. See hidden_states under returned tensors for more detail.
|
|
135
|
+
interpolate_pos_encoding (bool, defaults to False): Whether to interpolate the pre-trained position encodings.
|
|
136
|
+
|
|
137
|
+
Returns:
|
|
138
|
+
The model outputs. If return_dict=False is passed, returns a tuple of tensors. Otherwise, returns a BaseModelOutputWithPooling object.
|
|
139
|
+
"""
|
|
140
|
+
|
|
141
|
+
output_attentions = output_attentions if output_attentions is not None else self.rbln_config.output_attentions
|
|
142
|
+
output_hidden_states = (
|
|
143
|
+
output_hidden_states if output_hidden_states is not None else self.rbln_config.output_hidden_states
|
|
144
|
+
)
|
|
145
|
+
|
|
146
|
+
if output_attentions != self.rbln_config.output_attentions:
|
|
147
|
+
raise ValueError(
|
|
148
|
+
f"Variable output_attentions {output_attentions} is not equal to rbln_config.output_attentions {self.rbln_config.output_attentions} "
|
|
149
|
+
f"Please compile again with the correct argument."
|
|
150
|
+
)
|
|
151
|
+
|
|
152
|
+
if output_hidden_states != self.rbln_config.output_hidden_states:
|
|
153
|
+
raise ValueError(
|
|
154
|
+
f"Variable output_hidden_states {output_hidden_states} is not equal to rbln_config.output_hidden_states {self.rbln_config.output_hidden_states} "
|
|
155
|
+
f"Please compile again with the correct argument."
|
|
156
|
+
)
|
|
157
|
+
|
|
158
|
+
if interpolate_pos_encoding != self.rbln_config.interpolate_pos_encoding:
|
|
159
|
+
raise ValueError(
|
|
160
|
+
f"Variable interpolate_pos_encoding {interpolate_pos_encoding} is not equal to rbln_config.interpolate_pos_encoding {self.rbln_config.interpolate_pos_encoding} "
|
|
161
|
+
f"Please compile again with the correct argument."
|
|
162
|
+
)
|
|
163
|
+
|
|
164
|
+
output = super().forward(pixel_values, return_dict=return_dict, **kwargs)
|
|
165
|
+
return output
|
|
166
|
+
|
|
167
|
+
def _prepare_output(self, output, return_dict):
|
|
168
|
+
# Prepare model output based on return_dict flag.
|
|
169
|
+
# This method can be overridden by subclasses to provide task-specific output handling.
|
|
170
|
+
|
|
171
|
+
if not return_dict:
|
|
172
|
+
return (output,) if not isinstance(output, (tuple, list)) else output
|
|
173
|
+
else:
|
|
174
|
+
last_hidden_state = output.pop(0) if isinstance(output, (tuple, list)) else output
|
|
175
|
+
vision_config = self.config.vision_config if hasattr(self.config, "vision_config") else self.config
|
|
176
|
+
pooler_output = output.pop(0) if getattr(vision_config, "vision_use_head", True) else None
|
|
177
|
+
|
|
178
|
+
if self.rbln_config.output_hidden_states:
|
|
179
|
+
hidden_states = ()
|
|
180
|
+
num_hidden_layers = vision_config.num_hidden_layers
|
|
181
|
+
for _ in range(num_hidden_layers + 1):
|
|
182
|
+
hidden_states += (output.pop(0),)
|
|
183
|
+
else:
|
|
184
|
+
hidden_states = None
|
|
185
|
+
|
|
186
|
+
if self.rbln_config.output_attentions:
|
|
187
|
+
attentions = ()
|
|
188
|
+
num_hidden_layers = vision_config.num_hidden_layers
|
|
189
|
+
for _ in range(num_hidden_layers):
|
|
190
|
+
attentions += (output.pop(0),)
|
|
191
|
+
else:
|
|
192
|
+
attentions = None
|
|
193
|
+
|
|
194
|
+
return BaseModelOutputWithPooling(
|
|
195
|
+
last_hidden_state=last_hidden_state,
|
|
196
|
+
pooler_output=pooler_output,
|
|
197
|
+
hidden_states=hidden_states,
|
|
198
|
+
attentions=attentions,
|
|
199
|
+
)
|
|
@@ -0,0 +1,16 @@
|
|
|
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_swin import RBLNSwinBackboneConfig
|
|
16
|
+
from .modeling_swin import RBLNSwinBackbone
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
2
|
+
# you may not use this file except in compliance with the License.
|
|
3
|
+
# You may obtain a copy of the License at:
|
|
4
|
+
|
|
5
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
|
|
7
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
8
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
9
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
10
|
+
# See the License for the specific language governing permissions and
|
|
11
|
+
# limitations under the License.
|
|
12
|
+
|
|
13
|
+
from typing import Any, Optional, Tuple, Union
|
|
14
|
+
|
|
15
|
+
from ...configuration_generic import RBLNModelForImageClassificationConfig
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class RBLNSwinBackboneConfig(RBLNModelForImageClassificationConfig):
|
|
19
|
+
def __init__(
|
|
20
|
+
self,
|
|
21
|
+
image_size: Optional[Union[int, Tuple[int, int]]] = None,
|
|
22
|
+
batch_size: Optional[int] = None,
|
|
23
|
+
output_hidden_states: Optional[bool] = None,
|
|
24
|
+
output_attentions: Optional[bool] = None,
|
|
25
|
+
**kwargs: Any,
|
|
26
|
+
):
|
|
27
|
+
"""
|
|
28
|
+
Args:
|
|
29
|
+
batch_size (Optional[int]): The batch size for text processing. Defaults to 1.
|
|
30
|
+
kwargs: Additional arguments passed to the parent RBLNModelConfig.
|
|
31
|
+
|
|
32
|
+
Raises:
|
|
33
|
+
ValueError: If batch_size is not a positive integer.
|
|
34
|
+
"""
|
|
35
|
+
super().__init__(**kwargs)
|
|
36
|
+
self.batch_size = batch_size or 1
|
|
37
|
+
if not isinstance(self.batch_size, int) or self.batch_size < 0:
|
|
38
|
+
raise ValueError(f"batch_size must be a positive integer, got {self.batch_size}")
|
|
39
|
+
|
|
40
|
+
self.image_size = image_size
|
|
41
|
+
self.output_hidden_states = output_hidden_states
|
|
42
|
+
self.output_attentions = output_attentions
|
|
@@ -0,0 +1,354 @@
|
|
|
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
|
+
import types
|
|
16
|
+
from typing import TYPE_CHECKING, Optional, Tuple, Union
|
|
17
|
+
|
|
18
|
+
import torch
|
|
19
|
+
import torch.nn.functional as F
|
|
20
|
+
from transformers import SwinConfig
|
|
21
|
+
from transformers.models.swin.modeling_swin import BackboneOutput
|
|
22
|
+
|
|
23
|
+
from ....configuration_utils import RBLNCompileConfig, RBLNModelConfig
|
|
24
|
+
from ....modeling import RBLNModel
|
|
25
|
+
from ....utils.logging import get_logger
|
|
26
|
+
from .configuration_swin import RBLNSwinBackboneConfig
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
logger = get_logger(__name__)
|
|
30
|
+
|
|
31
|
+
if TYPE_CHECKING:
|
|
32
|
+
from transformers import (
|
|
33
|
+
AutoFeatureExtractor,
|
|
34
|
+
AutoProcessor,
|
|
35
|
+
AutoTokenizer,
|
|
36
|
+
PreTrainedModel,
|
|
37
|
+
SwinBackbone,
|
|
38
|
+
SwinEncoder,
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def window_partition(input_feature, window_size):
|
|
43
|
+
"""
|
|
44
|
+
Partitions the given input into windows.
|
|
45
|
+
"""
|
|
46
|
+
batch_size, height, width, num_channels = input_feature.shape
|
|
47
|
+
input_feature = input_feature.view(
|
|
48
|
+
batch_size, height // window_size, window_size, width // window_size, window_size, num_channels
|
|
49
|
+
)
|
|
50
|
+
windows = input_feature.permute(0, 1, 3, 2, 4, 5).contiguous().view(-1, window_size, window_size, num_channels)
|
|
51
|
+
return windows
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def get_attn_mask(self, height, width, dtype, device):
|
|
55
|
+
if self.shift_size > 0:
|
|
56
|
+
# calculate attention mask for SW-MSA
|
|
57
|
+
img_mask = torch.zeros((1, height, width, 1), dtype=dtype, device=device)
|
|
58
|
+
height_slices = (
|
|
59
|
+
slice(0, -self.window_size),
|
|
60
|
+
slice(-self.window_size, -self.shift_size),
|
|
61
|
+
slice(-self.shift_size, None),
|
|
62
|
+
)
|
|
63
|
+
width_slices = (
|
|
64
|
+
slice(0, -self.window_size),
|
|
65
|
+
slice(-self.window_size, -self.shift_size),
|
|
66
|
+
slice(-self.shift_size, None),
|
|
67
|
+
)
|
|
68
|
+
count = torch.zeros(1)
|
|
69
|
+
for height_slice in height_slices:
|
|
70
|
+
for width_slice in width_slices:
|
|
71
|
+
img_mask[:, height_slice, width_slice, :] = count
|
|
72
|
+
count += 1
|
|
73
|
+
|
|
74
|
+
mask_windows = window_partition(img_mask, self.window_size)
|
|
75
|
+
mask_windows = mask_windows.view(-1, self.window_size * self.window_size)
|
|
76
|
+
attn_mask = mask_windows.unsqueeze(1) - mask_windows.unsqueeze(2)
|
|
77
|
+
attn_mask = attn_mask.masked_fill(attn_mask != 0, float(-100.0)).masked_fill(attn_mask == 0, float(0.0))
|
|
78
|
+
else:
|
|
79
|
+
attn_mask = None
|
|
80
|
+
return attn_mask
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
class _SwinEncoder(torch.nn.Module):
|
|
84
|
+
def __init__(self, model: "SwinEncoder"):
|
|
85
|
+
super().__init__()
|
|
86
|
+
self.layers = model.layers
|
|
87
|
+
|
|
88
|
+
def forward(
|
|
89
|
+
self,
|
|
90
|
+
hidden_states: torch.Tensor,
|
|
91
|
+
input_dimensions: Tuple[int, int],
|
|
92
|
+
head_mask: Optional[torch.FloatTensor] = None,
|
|
93
|
+
output_attentions: Optional[bool] = False,
|
|
94
|
+
output_hidden_states: Optional[bool] = False,
|
|
95
|
+
output_hidden_states_before_downsampling: Optional[bool] = False,
|
|
96
|
+
always_partition: Optional[bool] = False,
|
|
97
|
+
return_dict: Optional[bool] = True,
|
|
98
|
+
):
|
|
99
|
+
all_hidden_states = () if output_hidden_states else None
|
|
100
|
+
all_reshaped_hidden_states = () if output_hidden_states else None
|
|
101
|
+
all_self_attentions = () if output_attentions else None
|
|
102
|
+
|
|
103
|
+
if output_hidden_states:
|
|
104
|
+
batch_size, _, hidden_size = hidden_states.shape
|
|
105
|
+
# rearrange b (h w) c -> b c h w
|
|
106
|
+
reshaped_hidden_state = hidden_states.view(batch_size, *input_dimensions, hidden_size)
|
|
107
|
+
reshaped_hidden_state = reshaped_hidden_state.permute(0, 3, 1, 2)
|
|
108
|
+
all_hidden_states += (hidden_states,)
|
|
109
|
+
all_reshaped_hidden_states += (reshaped_hidden_state,)
|
|
110
|
+
|
|
111
|
+
for i, layer_module in enumerate(self.layers):
|
|
112
|
+
layer_head_mask = head_mask[i] if head_mask is not None else None
|
|
113
|
+
|
|
114
|
+
layer_outputs = layer_module(
|
|
115
|
+
hidden_states, input_dimensions, layer_head_mask, output_attentions, always_partition
|
|
116
|
+
)
|
|
117
|
+
|
|
118
|
+
hidden_states = layer_outputs[0]
|
|
119
|
+
hidden_states_before_downsampling = layer_outputs[1]
|
|
120
|
+
output_dimensions = layer_outputs[2]
|
|
121
|
+
|
|
122
|
+
input_dimensions = (output_dimensions[-2], output_dimensions[-1])
|
|
123
|
+
|
|
124
|
+
if output_hidden_states and output_hidden_states_before_downsampling:
|
|
125
|
+
batch_size, _, hidden_size = hidden_states_before_downsampling.shape
|
|
126
|
+
# rearrange b (h w) c -> b c h w
|
|
127
|
+
# here we use the original (not downsampled) height and width
|
|
128
|
+
reshaped_hidden_state = hidden_states_before_downsampling.view(
|
|
129
|
+
batch_size, *(output_dimensions[0], output_dimensions[1]), hidden_size
|
|
130
|
+
)
|
|
131
|
+
reshaped_hidden_state = reshaped_hidden_state.permute(0, 3, 1, 2)
|
|
132
|
+
all_hidden_states += (hidden_states_before_downsampling,)
|
|
133
|
+
all_reshaped_hidden_states += (reshaped_hidden_state,)
|
|
134
|
+
elif output_hidden_states and not output_hidden_states_before_downsampling:
|
|
135
|
+
batch_size, _, hidden_size = hidden_states.shape
|
|
136
|
+
# rearrange b (h w) c -> b c h w
|
|
137
|
+
reshaped_hidden_state = hidden_states.view(batch_size, *input_dimensions, hidden_size)
|
|
138
|
+
reshaped_hidden_state = reshaped_hidden_state.permute(0, 3, 1, 2)
|
|
139
|
+
all_hidden_states += (hidden_states,)
|
|
140
|
+
all_reshaped_hidden_states += (reshaped_hidden_state,)
|
|
141
|
+
|
|
142
|
+
if output_attentions:
|
|
143
|
+
all_self_attentions += layer_outputs[3:]
|
|
144
|
+
|
|
145
|
+
return tuple(
|
|
146
|
+
v
|
|
147
|
+
for v in [hidden_states, all_hidden_states, all_self_attentions, all_reshaped_hidden_states]
|
|
148
|
+
if v is not None
|
|
149
|
+
)
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
class _SwinBackbone(torch.nn.Module):
|
|
153
|
+
def __init__(self, model: "SwinBackbone", output_hidden_states: bool, output_attentions: bool):
|
|
154
|
+
super().__init__()
|
|
155
|
+
self.model = model
|
|
156
|
+
self.embeddings = model.embeddings
|
|
157
|
+
self.encoder = model.encoder
|
|
158
|
+
self.stage_names = model.stage_names
|
|
159
|
+
self.out_features = model.out_features
|
|
160
|
+
self.hidden_states_norms = model.hidden_states_norms
|
|
161
|
+
self.output_hidden_states = output_hidden_states
|
|
162
|
+
self.output_attentions = output_attentions
|
|
163
|
+
|
|
164
|
+
def forward(
|
|
165
|
+
self,
|
|
166
|
+
pixel_values: torch.Tensor,
|
|
167
|
+
):
|
|
168
|
+
embedding_output, input_dimensions = self.embeddings(pixel_values)
|
|
169
|
+
outputs = _SwinEncoder(self.encoder)(
|
|
170
|
+
embedding_output,
|
|
171
|
+
input_dimensions,
|
|
172
|
+
head_mask=None,
|
|
173
|
+
output_attentions=self.output_attentions,
|
|
174
|
+
output_hidden_states=True,
|
|
175
|
+
output_hidden_states_before_downsampling=True,
|
|
176
|
+
always_partition=True,
|
|
177
|
+
return_dict=False,
|
|
178
|
+
)
|
|
179
|
+
|
|
180
|
+
hidden_states = outputs[-1]
|
|
181
|
+
|
|
182
|
+
feature_maps = ()
|
|
183
|
+
for stage, hidden_state in zip(self.stage_names, hidden_states):
|
|
184
|
+
if stage in self.out_features:
|
|
185
|
+
batch_size, num_channels, height, width = hidden_state.shape
|
|
186
|
+
hidden_state = hidden_state.permute(0, 2, 3, 1).contiguous()
|
|
187
|
+
hidden_state = hidden_state.view(batch_size, height * width, num_channels)
|
|
188
|
+
hidden_state = self.hidden_states_norms[stage](hidden_state)
|
|
189
|
+
hidden_state = hidden_state.view(batch_size, height, width, num_channels)
|
|
190
|
+
hidden_state = hidden_state.permute(0, 3, 1, 2).contiguous()
|
|
191
|
+
feature_maps += (hidden_state,)
|
|
192
|
+
|
|
193
|
+
output = (feature_maps,)
|
|
194
|
+
|
|
195
|
+
if self.output_hidden_states:
|
|
196
|
+
output += (outputs[1],)
|
|
197
|
+
|
|
198
|
+
if self.output_attentions:
|
|
199
|
+
output += (outputs[2],)
|
|
200
|
+
|
|
201
|
+
return output
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
class RBLNSwinBackbone(RBLNModel):
|
|
205
|
+
@classmethod
|
|
206
|
+
def _wrap_model_if_needed(cls, model: torch.nn.Module, rbln_config: RBLNSwinBackboneConfig) -> torch.nn.Module:
|
|
207
|
+
for layer in model.encoder.layers:
|
|
208
|
+
for block in layer.blocks:
|
|
209
|
+
block.get_attn_mask = types.MethodType(get_attn_mask, block)
|
|
210
|
+
|
|
211
|
+
wrapper_cfg = {
|
|
212
|
+
"output_hidden_states": rbln_config.output_hidden_states,
|
|
213
|
+
"output_attentions": rbln_config.output_attentions,
|
|
214
|
+
}
|
|
215
|
+
return _SwinBackbone(model, **wrapper_cfg).eval()
|
|
216
|
+
|
|
217
|
+
@classmethod
|
|
218
|
+
def _update_submodule_config(
|
|
219
|
+
cls,
|
|
220
|
+
model: "PreTrainedModel",
|
|
221
|
+
rbln_config: RBLNModelConfig,
|
|
222
|
+
preprocessors: Optional[Union["AutoFeatureExtractor", "AutoProcessor", "AutoTokenizer"]],
|
|
223
|
+
):
|
|
224
|
+
for processor in preprocessors:
|
|
225
|
+
if rbln_config.image_size is None and hasattr(processor, "image_processor"):
|
|
226
|
+
if "height" in processor.image_processor.size and "width" in processor.image_processor.size:
|
|
227
|
+
rbln_config.image_size = (
|
|
228
|
+
processor.image_processor.size["height"],
|
|
229
|
+
processor.image_processor.size["width"],
|
|
230
|
+
)
|
|
231
|
+
elif (
|
|
232
|
+
"longest_edge" in processor.image_processor.size
|
|
233
|
+
and "shortest_edge" in processor.image_processor.size
|
|
234
|
+
):
|
|
235
|
+
rbln_config.image_size = processor.image_processor.size["longest_edge"]
|
|
236
|
+
elif "shortest_edge" in processor.image_processor.size:
|
|
237
|
+
rbln_config.image_size = processor.image_processor.size["shortest_edge"]
|
|
238
|
+
break
|
|
239
|
+
|
|
240
|
+
return rbln_config
|
|
241
|
+
|
|
242
|
+
@classmethod
|
|
243
|
+
def _update_rbln_config(
|
|
244
|
+
cls,
|
|
245
|
+
preprocessors: Union["AutoFeatureExtractor", "AutoProcessor", "AutoTokenizer"],
|
|
246
|
+
model: Optional["PreTrainedModel"] = None,
|
|
247
|
+
model_config: "SwinConfig" = None,
|
|
248
|
+
rbln_config: Optional[RBLNSwinBackboneConfig] = None,
|
|
249
|
+
) -> RBLNSwinBackboneConfig:
|
|
250
|
+
if rbln_config.image_size is None:
|
|
251
|
+
for processor in preprocessors:
|
|
252
|
+
if hasattr(processor, "size"):
|
|
253
|
+
if all(required_key in processor.size.keys() for required_key in ["height", "width"]):
|
|
254
|
+
rbln_config.image_size = (processor.size["height"], processor.size["width"])
|
|
255
|
+
break
|
|
256
|
+
|
|
257
|
+
input_info = [
|
|
258
|
+
(
|
|
259
|
+
"pixel_values",
|
|
260
|
+
[
|
|
261
|
+
rbln_config.batch_size,
|
|
262
|
+
3,
|
|
263
|
+
rbln_config.image_height,
|
|
264
|
+
rbln_config.image_width,
|
|
265
|
+
],
|
|
266
|
+
"float32",
|
|
267
|
+
),
|
|
268
|
+
]
|
|
269
|
+
|
|
270
|
+
rbln_config.set_compile_cfgs([RBLNCompileConfig(input_info=input_info)])
|
|
271
|
+
return rbln_config
|
|
272
|
+
|
|
273
|
+
def forward(
|
|
274
|
+
self,
|
|
275
|
+
pixel_values: Optional[torch.FloatTensor] = None,
|
|
276
|
+
return_dict: bool = True,
|
|
277
|
+
output_attentions: bool = None,
|
|
278
|
+
output_hidden_states: bool = None,
|
|
279
|
+
**kwargs,
|
|
280
|
+
) -> Union[Tuple, BackboneOutput]:
|
|
281
|
+
"""
|
|
282
|
+
Forward pass for the RBLN-optimized Swin backbone model.
|
|
283
|
+
|
|
284
|
+
Args:
|
|
285
|
+
pixel_values (torch.FloatTensor of shape (batch_size, num_channels, image_size, image_size), optional): The tensors corresponding to the input images. Pixel values can be obtained using ViTImageProcessor. See ViTImageProcessor.call() for details (processor_class uses ViTImageProcessor for processing images).
|
|
286
|
+
return_dict (bool, optional): Whether or not to return a ModelOutput instead of a plain tuple.
|
|
287
|
+
output_attentions (bool, optional): Whether or not to return the attentions tensors of all attention layers. See attentions under returned tensors for more detail.
|
|
288
|
+
output_hidden_states (bool, optional): Whether or not to return the hidden states of all layers. See hidden_states under returned tensors for more detail.
|
|
289
|
+
|
|
290
|
+
Returns:
|
|
291
|
+
The model outputs. If return_dict=False is passed, returns a tuple of tensors. Otherwise, returns a BackboneOutput object.
|
|
292
|
+
"""
|
|
293
|
+
|
|
294
|
+
if len(kwargs) > 0 and any(value is not None for value in kwargs.values()):
|
|
295
|
+
logger.warning(
|
|
296
|
+
f"Currently, optimum-rbln does not support kwargs {kwargs.keys()} for {self.__class__.__name__}."
|
|
297
|
+
)
|
|
298
|
+
|
|
299
|
+
output_attentions = output_attentions if output_attentions is not None else self.rbln_config.output_attentions
|
|
300
|
+
output_hidden_states = (
|
|
301
|
+
output_hidden_states if output_hidden_states is not None else self.rbln_config.output_hidden_states
|
|
302
|
+
)
|
|
303
|
+
|
|
304
|
+
if output_attentions != self.rbln_config.output_attentions:
|
|
305
|
+
raise ValueError(
|
|
306
|
+
f"Variable output_attentions {output_attentions} is not equal to rbln_config.output_attentions {self.rbln_config.output_attentions} "
|
|
307
|
+
f"Please compile again with the correct argument."
|
|
308
|
+
)
|
|
309
|
+
|
|
310
|
+
if output_hidden_states != self.rbln_config.output_hidden_states:
|
|
311
|
+
raise ValueError(
|
|
312
|
+
f"Variable output_hidden_states {output_hidden_states} is not equal to rbln_config.output_hidden_states {self.rbln_config.output_hidden_states} "
|
|
313
|
+
f"Please compile again with the correct argument."
|
|
314
|
+
)
|
|
315
|
+
|
|
316
|
+
_, _, original_h, original_w = pixel_values.shape
|
|
317
|
+
if original_h > self.rbln_config.image_height or original_w > self.rbln_config.image_width:
|
|
318
|
+
raise ValueError(
|
|
319
|
+
f"Input image size ({original_h}x{original_w}) exceeds the configured maximum size"
|
|
320
|
+
f" ({self.rbln_config.image_height}x{self.rbln_config.image_width})."
|
|
321
|
+
)
|
|
322
|
+
|
|
323
|
+
pad_h = self.rbln_config.image_height - original_h
|
|
324
|
+
pad_w = self.rbln_config.image_width - original_w
|
|
325
|
+
padded_pixel_values = F.pad(pixel_values, (0, pad_w, 0, pad_h))
|
|
326
|
+
|
|
327
|
+
output = self.model[0](padded_pixel_values)
|
|
328
|
+
|
|
329
|
+
feature_maps = ()
|
|
330
|
+
for i in range(len(self.config.out_features)):
|
|
331
|
+
feature_maps += (output.pop(0),)
|
|
332
|
+
|
|
333
|
+
if self.rbln_config.output_hidden_states:
|
|
334
|
+
hidden_states = ()
|
|
335
|
+
for i in range(len(self.config.stage_names)):
|
|
336
|
+
hidden_states += (output.pop(0),)
|
|
337
|
+
else:
|
|
338
|
+
hidden_states = None
|
|
339
|
+
|
|
340
|
+
if self.rbln_config.output_attentions:
|
|
341
|
+
attentions = ()
|
|
342
|
+
for i in range(len(self.config.depths)):
|
|
343
|
+
attentions += (output.pop(0),)
|
|
344
|
+
else:
|
|
345
|
+
attentions = None
|
|
346
|
+
|
|
347
|
+
if not return_dict:
|
|
348
|
+
return tuple(item for item in (feature_maps, hidden_states, attentions) if item is not None)
|
|
349
|
+
else:
|
|
350
|
+
return BackboneOutput(
|
|
351
|
+
feature_maps=feature_maps,
|
|
352
|
+
hidden_states=hidden_states,
|
|
353
|
+
attentions=attentions,
|
|
354
|
+
)
|
|
@@ -0,0 +1,17 @@
|
|
|
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 ....ops import paged_add_softmax_attn_decode
|
|
16
|
+
from .configuration_t5 import RBLNT5EncoderModelConfig, RBLNT5ForConditionalGenerationConfig
|
|
17
|
+
from .modeling_t5 import RBLNT5EncoderModel, RBLNT5ForConditionalGeneration
|
|
@@ -0,0 +1,36 @@
|
|
|
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 RBLNTransformerEncoderForFeatureExtractionConfig
|
|
16
|
+
from ..seq2seq import RBLNModelForSeq2SeqLMConfig
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class RBLNT5EncoderModelConfig(RBLNTransformerEncoderForFeatureExtractionConfig):
|
|
20
|
+
"""
|
|
21
|
+
Configuration class for RBLNT5EncoderModel.
|
|
22
|
+
|
|
23
|
+
This configuration class stores the configuration parameters specific to
|
|
24
|
+
RBLN-optimized T5 encoder models for feature extraction tasks.
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class RBLNT5ForConditionalGenerationConfig(RBLNModelForSeq2SeqLMConfig):
|
|
29
|
+
"""
|
|
30
|
+
Configuration class for RBLNT5ForConditionalGeneration.
|
|
31
|
+
|
|
32
|
+
This configuration class stores the configuration parameters specific to
|
|
33
|
+
RBLN-optimized T5 models for conditional text generation tasks.
|
|
34
|
+
"""
|
|
35
|
+
|
|
36
|
+
support_paged_attention = False
|