optimum-rbln 0.8.2rc0__py3-none-any.whl → 0.8.3__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 +32 -9
- optimum/rbln/__version__.py +16 -3
- optimum/rbln/configuration_utils.py +20 -4
- optimum/rbln/diffusers/__init__.py +7 -0
- optimum/rbln/diffusers/configurations/models/configuration_autoencoder_kl.py +2 -2
- optimum/rbln/diffusers/configurations/models/configuration_autoencoder_kl_cosmos.py +2 -2
- optimum/rbln/diffusers/configurations/models/configuration_controlnet.py +2 -2
- optimum/rbln/diffusers/configurations/models/configuration_prior_transformer.py +2 -2
- optimum/rbln/diffusers/configurations/models/configuration_transformer_cosmos.py +2 -2
- optimum/rbln/diffusers/configurations/models/configuration_transformer_sd3.py +2 -2
- optimum/rbln/diffusers/configurations/models/configuration_unet_2d_condition.py +2 -2
- optimum/rbln/diffusers/configurations/models/configuration_vq_model.py +2 -2
- optimum/rbln/diffusers/configurations/pipelines/configuration_controlnet.py +3 -3
- optimum/rbln/diffusers/configurations/pipelines/configuration_cosmos.py +2 -2
- optimum/rbln/diffusers/configurations/pipelines/configuration_kandinsky2_2.py +4 -4
- optimum/rbln/diffusers/configurations/pipelines/configuration_stable_diffusion.py +2 -2
- optimum/rbln/diffusers/configurations/pipelines/configuration_stable_diffusion_3.py +2 -2
- optimum/rbln/diffusers/configurations/pipelines/configuration_stable_diffusion_xl.py +2 -2
- optimum/rbln/diffusers/modeling_diffusers.py +1 -1
- optimum/rbln/diffusers/models/__init__.py +3 -13
- optimum/rbln/diffusers/pipelines/__init__.py +11 -5
- optimum/rbln/diffusers/pipelines/auto_pipeline.py +237 -0
- optimum/rbln/diffusers/pipelines/cosmos/configuration_cosmos_guardrail.py +11 -6
- optimum/rbln/diffusers/pipelines/cosmos/cosmos_guardrail.py +14 -18
- optimum/rbln/diffusers/pipelines/cosmos/pipeline_cosmos_text2world.py +1 -1
- optimum/rbln/diffusers/pipelines/cosmos/pipeline_cosmos_video2world.py +1 -1
- optimum/rbln/diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2_combined.py +1 -6
- optimum/rbln/modeling.py +3 -2
- optimum/rbln/modeling_base.py +29 -4
- optimum/rbln/ops/attn.py +158 -0
- optimum/rbln/ops/flash_attn.py +166 -0
- optimum/rbln/transformers/__init__.py +24 -0
- optimum/rbln/transformers/configuration_generic.py +6 -4
- optimum/rbln/transformers/modeling_generic.py +13 -8
- optimum/rbln/transformers/modeling_outputs.py +37 -0
- optimum/rbln/transformers/models/__init__.py +31 -16
- optimum/rbln/transformers/models/auto/__init__.py +2 -0
- optimum/rbln/transformers/models/auto/modeling_auto.py +14 -0
- optimum/rbln/transformers/models/bart/bart_architecture.py +1 -3
- optimum/rbln/transformers/models/bart/configuration_bart.py +2 -0
- optimum/rbln/transformers/models/bert/bert_architecture.py +16 -0
- optimum/rbln/transformers/models/bert/modeling_bert.py +8 -4
- optimum/rbln/transformers/models/blip_2/configuration_blip_2.py +2 -2
- optimum/rbln/transformers/models/blip_2/modeling_blip_2.py +7 -6
- optimum/rbln/transformers/models/clip/configuration_clip.py +3 -3
- optimum/rbln/transformers/models/colpali/colpali_architecture.py +1 -4
- optimum/rbln/transformers/models/colpali/configuration_colpali.py +2 -2
- optimum/rbln/transformers/models/colpali/modeling_colpali.py +2 -10
- optimum/rbln/transformers/models/decoderonly/configuration_decoderonly.py +43 -174
- optimum/rbln/transformers/models/decoderonly/decoderonly_architecture.py +101 -91
- optimum/rbln/transformers/models/decoderonly/decoderonly_runtime_utils.py +450 -0
- optimum/rbln/transformers/models/decoderonly/generation_decoderonly.py +88 -0
- optimum/rbln/transformers/models/decoderonly/modeling_decoderonly.py +296 -986
- 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 +25 -0
- optimum/rbln/transformers/models/gemma/gemma_architecture.py +1 -4
- optimum/rbln/transformers/models/gemma/modeling_gemma.py +9 -0
- optimum/rbln/transformers/models/gemma3/configuration_gemma3.py +3 -3
- optimum/rbln/transformers/models/gemma3/gemma3_runtime_utils.py +217 -0
- optimum/rbln/transformers/models/gemma3/modeling_gemma3.py +25 -251
- optimum/rbln/transformers/models/gpt2/modeling_gpt2.py +2 -0
- optimum/rbln/transformers/models/grounding_dino/__init__.py +10 -0
- optimum/rbln/transformers/models/grounding_dino/configuration_grounding_dino.py +86 -0
- optimum/rbln/transformers/models/grounding_dino/grounding_dino_architecture.py +507 -0
- optimum/rbln/transformers/models/grounding_dino/modeling_grounding_dino.py +1032 -0
- optimum/rbln/transformers/models/idefics3/configuration_idefics3.py +2 -2
- optimum/rbln/transformers/models/idefics3/modeling_idefics3.py +3 -9
- optimum/rbln/transformers/models/llama/modeling_llama.py +12 -3
- optimum/rbln/transformers/models/llava/configuration_llava.py +2 -2
- optimum/rbln/transformers/models/llava/modeling_llava.py +53 -14
- optimum/rbln/transformers/models/llava_next/configuration_llava_next.py +2 -2
- optimum/rbln/transformers/models/llava_next/modeling_llava_next.py +6 -16
- optimum/rbln/transformers/models/opt/modeling_opt.py +2 -30
- optimum/rbln/transformers/models/pegasus/configuration_pegasus.py +4 -0
- optimum/rbln/transformers/models/pegasus/modeling_pegasus.py +2 -0
- optimum/rbln/transformers/models/pegasus/pegasus_architecture.py +1 -3
- optimum/rbln/transformers/models/pixtral/configuration_pixtral.py +2 -2
- optimum/rbln/transformers/models/pixtral/modeling_pixtral.py +1 -4
- optimum/rbln/transformers/models/qwen2_5_vl/configuration_qwen2_5_vl.py +3 -3
- optimum/rbln/transformers/models/qwen2_5_vl/modeling_qwen2_5_vl.py +6 -15
- optimum/rbln/transformers/models/qwen2_5_vl/qwen2_5_vl_architecture.py +4 -7
- optimum/rbln/transformers/models/qwen3/modeling_qwen3.py +77 -3
- optimum/rbln/transformers/models/qwen3/qwen3_architecture.py +1 -4
- optimum/rbln/transformers/models/seq2seq/configuration_seq2seq.py +19 -2
- optimum/rbln/transformers/models/seq2seq/modeling_seq2seq.py +20 -1
- optimum/rbln/transformers/models/siglip/__init__.py +2 -6
- optimum/rbln/transformers/models/siglip/modeling_siglip.py +2 -2
- 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 +341 -0
- optimum/rbln/transformers/models/t5/configuration_t5.py +2 -0
- optimum/rbln/transformers/models/t5/t5_architecture.py +8 -1
- optimum/rbln/transformers/models/time_series_transformer/configuration_time_series_transformer.py +2 -2
- optimum/rbln/transformers/models/time_series_transformer/modeling_time_series_transformer.py +4 -14
- optimum/rbln/transformers/models/whisper/configuration_whisper.py +10 -2
- optimum/rbln/transformers/models/whisper/modeling_whisper.py +20 -1
- optimum/rbln/transformers/models/xlm_roberta/__init__.py +2 -8
- optimum/rbln/transformers/utils/rbln_quantization.py +365 -65
- optimum/rbln/utils/runtime_utils.py +3 -3
- optimum/rbln/utils/submodule.py +10 -4
- {optimum_rbln-0.8.2rc0.dist-info → optimum_rbln-0.8.3.dist-info}/METADATA +1 -1
- {optimum_rbln-0.8.2rc0.dist-info → optimum_rbln-0.8.3.dist-info}/RECORD +105 -89
- {optimum_rbln-0.8.2rc0.dist-info → optimum_rbln-0.8.3.dist-info}/WHEEL +0 -0
- {optimum_rbln-0.8.2rc0.dist-info → optimum_rbln-0.8.3.dist-info}/licenses/LICENSE +0 -0
|
@@ -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_depth_anything import RBLNDepthAnythingForDepthEstimationConfig
|
|
16
|
+
from .modeling_depth_anything import RBLNDepthAnythingForDepthEstimation
|
|
@@ -0,0 +1,24 @@
|
|
|
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 RBLNModelForDepthEstimationConfig
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class RBLNDepthAnythingForDepthEstimationConfig(RBLNModelForDepthEstimationConfig):
|
|
19
|
+
"""
|
|
20
|
+
Configuration class for DepthAnythingForDepthEstimation.
|
|
21
|
+
|
|
22
|
+
This configuration class stores the configuration parameters specific to
|
|
23
|
+
RBLN-optimized Depth Anything V2 Small models for depth estimation tasks.
|
|
24
|
+
"""
|
|
@@ -0,0 +1,25 @@
|
|
|
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
|
+
|
|
16
|
+
from ...modeling_generic import RBLNModelForDepthEstimation
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class RBLNDepthAnythingForDepthEstimation(RBLNModelForDepthEstimation):
|
|
20
|
+
"""
|
|
21
|
+
RBLN optimized DepthAnythingForDepthEstimation model for depth estimation tasks.
|
|
22
|
+
|
|
23
|
+
This class provides hardware-accelerated inference for Depth Anything V2
|
|
24
|
+
models on RBLN devices, providing the most capable monocular depth estimation (MDE) model.
|
|
25
|
+
"""
|
|
@@ -13,10 +13,7 @@
|
|
|
13
13
|
# limitations under the License.
|
|
14
14
|
|
|
15
15
|
|
|
16
|
-
from ...models.decoderonly.decoderonly_architecture import
|
|
17
|
-
DecoderOnlyModel,
|
|
18
|
-
DecoderOnlyWrapper,
|
|
19
|
-
)
|
|
16
|
+
from ...models.decoderonly.decoderonly_architecture import DecoderOnlyModel, DecoderOnlyWrapper
|
|
20
17
|
|
|
21
18
|
|
|
22
19
|
class GemmaWrapper(DecoderOnlyWrapper):
|
|
@@ -90,6 +90,15 @@ class RBLNGemmaModel(RBLNDecoderOnlyModel):
|
|
|
90
90
|
|
|
91
91
|
A class to convert and run pre-trained transformers based GemmaModel model on RBLN devices.
|
|
92
92
|
It implements the methods to convert a pre-trained transformers GemmaModel model into a RBLN transformer model by:
|
|
93
|
+
|
|
94
|
+
- transferring the checkpoint weights of the original into an optimized RBLN graph,
|
|
95
|
+
- compiling the resulting graph using the RBLN compiler.
|
|
96
|
+
|
|
97
|
+
**Configuration:**
|
|
98
|
+
This model uses [`RBLNGemmaModelConfig`] for configuration. When calling methods like `from_pretrained` or `from_model`,
|
|
99
|
+
the `rbln_config` parameter should be an instance of [`RBLNGemmaModelConfig`] or a dictionary conforming to its structure.
|
|
100
|
+
|
|
101
|
+
See the [`RBLNGemmaModelConfig`] class for all available configuration options.
|
|
93
102
|
"""
|
|
94
103
|
|
|
95
104
|
_decoder_wrapper_cls = GemmaWrapper
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
12
|
# See the License for the specific language governing permissions and
|
|
13
13
|
# limitations under the License.
|
|
14
|
-
from typing import Any,
|
|
14
|
+
from typing import Any, Optional
|
|
15
15
|
|
|
16
16
|
from ....configuration_utils import RBLNModelConfig
|
|
17
17
|
from ..decoderonly.configuration_decoderonly import RBLNDecoderOnlyModelForCausalLMConfig
|
|
@@ -25,7 +25,7 @@ class RBLNGemma3ForCausalLMConfig(RBLNDecoderOnlyModelForCausalLMConfig):
|
|
|
25
25
|
use_attention_mask: Optional[bool] = None,
|
|
26
26
|
prefill_chunk_size: Optional[int] = None,
|
|
27
27
|
image_prefill_chunk_size: Optional[int] = None,
|
|
28
|
-
**kwargs:
|
|
28
|
+
**kwargs: Any,
|
|
29
29
|
):
|
|
30
30
|
# use_attention_mask and use_position_ids are always True for Gemma3
|
|
31
31
|
use_attention_mask = use_attention_mask or True
|
|
@@ -57,7 +57,7 @@ class RBLNGemma3ForConditionalGenerationConfig(RBLNModelConfig):
|
|
|
57
57
|
batch_size: Optional[int] = None,
|
|
58
58
|
vision_tower: Optional[RBLNModelConfig] = None,
|
|
59
59
|
language_model: Optional[RBLNModelConfig] = None,
|
|
60
|
-
**kwargs:
|
|
60
|
+
**kwargs: Any,
|
|
61
61
|
):
|
|
62
62
|
"""
|
|
63
63
|
Args:
|
|
@@ -0,0 +1,217 @@
|
|
|
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
|
+
from typing import Optional
|
|
15
|
+
|
|
16
|
+
import rebel
|
|
17
|
+
import torch
|
|
18
|
+
|
|
19
|
+
from ...modeling_outputs import RBLNDecoderOnlyOutput, RBLNGemma3ForCausalLMOutput
|
|
20
|
+
from ..decoderonly.modeling_decoderonly import RBLNRuntimeModel
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class RBLNGemma3RuntimeModel(RBLNRuntimeModel):
|
|
24
|
+
def __init__(self, *args, image_prefill: Optional[rebel.Runtime] = None, **kwargs):
|
|
25
|
+
super().__init__(*args, **kwargs)
|
|
26
|
+
self.image_prefill = image_prefill # FIXME(taehoon)
|
|
27
|
+
self.prefill = self.runtime if self.phase == "prefill" else None # FIXME
|
|
28
|
+
self.decode = self.runtime if self.phase == "decode" else None
|
|
29
|
+
|
|
30
|
+
def _prepare_prefill_inputs(self, *args, **kwargs):
|
|
31
|
+
(
|
|
32
|
+
inputs,
|
|
33
|
+
cache_position,
|
|
34
|
+
chunked_attention_mask,
|
|
35
|
+
position_ids,
|
|
36
|
+
position_embed,
|
|
37
|
+
padded_cache_lengths,
|
|
38
|
+
query_length,
|
|
39
|
+
token_type_ids,
|
|
40
|
+
) = super()._prepare_prefill_inputs(*args, **kwargs)
|
|
41
|
+
|
|
42
|
+
# chunked_attention_mask shape
|
|
43
|
+
chunked_attention_mask = torch.zeros(1, chunked_attention_mask.shape[-1], dtype=torch.float32)
|
|
44
|
+
|
|
45
|
+
# In case of Gemma3ForConditionalGeneration, the loop counter may not be a prefill_chunk_size,
|
|
46
|
+
# so we cannot guarantee that the last chunk starts at a position that is a multiple of prefill_chunk_size.
|
|
47
|
+
if self.rbln_config.use_image_prefill:
|
|
48
|
+
padding_size = self.rbln_config.image_prefill_chunk_size
|
|
49
|
+
inputs = torch.nn.functional.pad(inputs, (0, 0, 0, padding_size))
|
|
50
|
+
cache_position = torch.nn.functional.pad(cache_position, (0, padding_size))
|
|
51
|
+
position_ids = torch.nn.functional.pad(position_ids, (0, padding_size))
|
|
52
|
+
token_type_ids = torch.nn.functional.pad(token_type_ids, (0, padding_size), value=-1)
|
|
53
|
+
|
|
54
|
+
return (
|
|
55
|
+
inputs,
|
|
56
|
+
cache_position,
|
|
57
|
+
chunked_attention_mask,
|
|
58
|
+
position_ids,
|
|
59
|
+
position_embed,
|
|
60
|
+
padded_cache_lengths,
|
|
61
|
+
query_length,
|
|
62
|
+
token_type_ids,
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
def prefill_forward(
|
|
66
|
+
self,
|
|
67
|
+
inputs: torch.Tensor,
|
|
68
|
+
cache_position: torch.Tensor = None,
|
|
69
|
+
attention_mask: Optional[torch.Tensor] = None,
|
|
70
|
+
batch_idx: int = None,
|
|
71
|
+
block_tables: torch.Tensor = None,
|
|
72
|
+
is_external_block_tables: bool = None,
|
|
73
|
+
position_embed: Optional[torch.Tensor] = None,
|
|
74
|
+
token_type_ids: Optional[torch.Tensor] = None,
|
|
75
|
+
local_block_tables: Optional[torch.Tensor] = None,
|
|
76
|
+
) -> torch.FloatTensor:
|
|
77
|
+
"""
|
|
78
|
+
Performs chunked prefill for efficient KV-cache updates and memory optimization.
|
|
79
|
+
Instead of processing the entire sequence at once, the input is divided into chunks of size `prefill_chunk_size`,
|
|
80
|
+
and each chunk is processed sequentially. This allows for better memory utilization and compatibility with continuous batching.
|
|
81
|
+
"""
|
|
82
|
+
(
|
|
83
|
+
inputs,
|
|
84
|
+
cache_position,
|
|
85
|
+
chunked_attention_mask,
|
|
86
|
+
position_ids,
|
|
87
|
+
position_embed,
|
|
88
|
+
padded_cache_lengths,
|
|
89
|
+
query_length,
|
|
90
|
+
token_type_ids,
|
|
91
|
+
) = self._prepare_prefill_inputs(
|
|
92
|
+
inputs, cache_position, attention_mask, position_embed, token_type_ids=token_type_ids
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
step = 0
|
|
96
|
+
while step < query_length:
|
|
97
|
+
if self.rbln_config.use_image_prefill:
|
|
98
|
+
# Check if the prefill chunk is an image prefill
|
|
99
|
+
is_image_prefill = torch.all(
|
|
100
|
+
token_type_ids[:, step : step + self.rbln_config.image_prefill_chunk_size] == 1
|
|
101
|
+
)
|
|
102
|
+
# Check if the prefill chunk is a text prefill which have image_tokens in it.
|
|
103
|
+
is_text_prefill_with_image_tokens = not is_image_prefill and torch.any(
|
|
104
|
+
token_type_ids[:, step : step + self.rbln_config.prefill_chunk_size] == 1
|
|
105
|
+
)
|
|
106
|
+
else:
|
|
107
|
+
is_image_prefill, is_text_prefill_with_image_tokens = False, False
|
|
108
|
+
|
|
109
|
+
# Check if the prefill chunk is the last chunk
|
|
110
|
+
is_last_chunk = step + self.rbln_config.prefill_chunk_size >= query_length
|
|
111
|
+
|
|
112
|
+
input_chunk = inputs[:, step : step + self.rbln_config.prefill_chunk_size]
|
|
113
|
+
cache_pos_chunk = (
|
|
114
|
+
cache_position[:, step : step + self.rbln_config.prefill_chunk_size] + padded_cache_lengths
|
|
115
|
+
)
|
|
116
|
+
position_ids_chunk = position_ids[:, step : step + self.rbln_config.prefill_chunk_size]
|
|
117
|
+
|
|
118
|
+
# if text_prefill end with image_tokens, we only treat the text part.
|
|
119
|
+
num_processed_tokens = self.rbln_config.prefill_chunk_size
|
|
120
|
+
current_padded_cache_lengths = 0
|
|
121
|
+
if is_text_prefill_with_image_tokens:
|
|
122
|
+
first_image_token_idx = torch.where(
|
|
123
|
+
token_type_ids[:, step : step + self.rbln_config.prefill_chunk_size] == 1
|
|
124
|
+
)[1][0]
|
|
125
|
+
num_processed_tokens = first_image_token_idx.item()
|
|
126
|
+
current_padded_cache_lengths = self.rbln_config.prefill_chunk_size - num_processed_tokens
|
|
127
|
+
if is_last_chunk:
|
|
128
|
+
num_processed_tokens = query_length - step
|
|
129
|
+
|
|
130
|
+
chunked_attention_mask[
|
|
131
|
+
:, step + padded_cache_lengths : step + num_processed_tokens + padded_cache_lengths
|
|
132
|
+
] = 1
|
|
133
|
+
query_position = torch.tensor(num_processed_tokens - 1, dtype=torch.int16)
|
|
134
|
+
|
|
135
|
+
if is_image_prefill:
|
|
136
|
+
logits = self.image_prefill(
|
|
137
|
+
input_chunk,
|
|
138
|
+
cache_pos_chunk,
|
|
139
|
+
block_tables,
|
|
140
|
+
local_block_tables,
|
|
141
|
+
query_position,
|
|
142
|
+
chunked_attention_mask,
|
|
143
|
+
position_ids_chunk,
|
|
144
|
+
)
|
|
145
|
+
else:
|
|
146
|
+
logits = self.prefill(
|
|
147
|
+
input_chunk,
|
|
148
|
+
cache_pos_chunk,
|
|
149
|
+
block_tables,
|
|
150
|
+
local_block_tables,
|
|
151
|
+
query_position,
|
|
152
|
+
chunked_attention_mask,
|
|
153
|
+
position_ids_chunk,
|
|
154
|
+
)
|
|
155
|
+
|
|
156
|
+
padded_cache_lengths += current_padded_cache_lengths
|
|
157
|
+
step += num_processed_tokens
|
|
158
|
+
|
|
159
|
+
if not is_external_block_tables:
|
|
160
|
+
self.dec_attn_mask[batch_idx : batch_idx + 1] = chunked_attention_mask
|
|
161
|
+
|
|
162
|
+
return RBLNGemma3ForCausalLMOutput(
|
|
163
|
+
logits=logits, padded_cache_lengths=padded_cache_lengths, attention_mask=chunked_attention_mask
|
|
164
|
+
)
|
|
165
|
+
|
|
166
|
+
def decode_forward(
|
|
167
|
+
self,
|
|
168
|
+
inputs: torch.Tensor,
|
|
169
|
+
cache_position: torch.Tensor = None,
|
|
170
|
+
block_tables: torch.Tensor = None,
|
|
171
|
+
is_external_block_tables: bool = None,
|
|
172
|
+
attention_mask: Optional[torch.Tensor] = None,
|
|
173
|
+
position_embed: Optional[torch.Tensor] = None,
|
|
174
|
+
position_ids: Optional[torch.Tensor] = None,
|
|
175
|
+
local_block_tables: Optional[torch.Tensor] = None,
|
|
176
|
+
) -> torch.FloatTensor:
|
|
177
|
+
batch_size = inputs.shape[0]
|
|
178
|
+
if batch_size != self.batch_size:
|
|
179
|
+
raise RuntimeError(
|
|
180
|
+
f"Batch size mismatch: got {batch_size}, expected {self.batch_size} (compiled batch size)."
|
|
181
|
+
)
|
|
182
|
+
|
|
183
|
+
if batch_size != cache_position.shape[0]:
|
|
184
|
+
raise RuntimeError(f"Cache position size mismatch: got {cache_position.shape[0]}, expected {batch_size}.")
|
|
185
|
+
|
|
186
|
+
# FIXME(taehoon): how to handle pos_attn_mask with external block tables
|
|
187
|
+
if is_external_block_tables:
|
|
188
|
+
if attention_mask is None:
|
|
189
|
+
raise ValueError("attention_mask should be provided with external block tables.")
|
|
190
|
+
if local_block_tables is None:
|
|
191
|
+
raise ValueError("local_block_tables should be provided with external block tables.")
|
|
192
|
+
else:
|
|
193
|
+
local_block_tables = (
|
|
194
|
+
local_block_tables
|
|
195
|
+
if local_block_tables is not None
|
|
196
|
+
else torch.arange(0, self.batch_size, dtype=torch.int16).view(self.batch_size, -1)
|
|
197
|
+
)
|
|
198
|
+
if self.rbln_config.use_attention_mask and attention_mask is None:
|
|
199
|
+
for b_idx in range(batch_size):
|
|
200
|
+
decoding_step = cache_position[b_idx].item()
|
|
201
|
+
if not (0 <= decoding_step < self.dec_attn_mask.shape[-1]):
|
|
202
|
+
raise ValueError(
|
|
203
|
+
f"Decoding step {decoding_step} out of bounds for attention mask with shape {self.dec_attn_mask.shape}."
|
|
204
|
+
)
|
|
205
|
+
self.dec_attn_mask[b_idx, decoding_step] = 1
|
|
206
|
+
|
|
207
|
+
attention_mask = self.dec_attn_mask
|
|
208
|
+
|
|
209
|
+
if self.batch_size < block_tables.shape[0]:
|
|
210
|
+
block_tables = block_tables[: self.batch_size]
|
|
211
|
+
|
|
212
|
+
if attention_mask is not None and self.batch_size < attention_mask.shape[0]:
|
|
213
|
+
attention_mask = attention_mask[: self.batch_size]
|
|
214
|
+
|
|
215
|
+
logits = self.decode(inputs, cache_position, block_tables, local_block_tables, attention_mask, position_ids)
|
|
216
|
+
|
|
217
|
+
return RBLNDecoderOnlyOutput(logits=logits)
|