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.

Files changed (105) hide show
  1. optimum/rbln/__init__.py +32 -9
  2. optimum/rbln/__version__.py +16 -3
  3. optimum/rbln/configuration_utils.py +20 -4
  4. optimum/rbln/diffusers/__init__.py +7 -0
  5. optimum/rbln/diffusers/configurations/models/configuration_autoencoder_kl.py +2 -2
  6. optimum/rbln/diffusers/configurations/models/configuration_autoencoder_kl_cosmos.py +2 -2
  7. optimum/rbln/diffusers/configurations/models/configuration_controlnet.py +2 -2
  8. optimum/rbln/diffusers/configurations/models/configuration_prior_transformer.py +2 -2
  9. optimum/rbln/diffusers/configurations/models/configuration_transformer_cosmos.py +2 -2
  10. optimum/rbln/diffusers/configurations/models/configuration_transformer_sd3.py +2 -2
  11. optimum/rbln/diffusers/configurations/models/configuration_unet_2d_condition.py +2 -2
  12. optimum/rbln/diffusers/configurations/models/configuration_vq_model.py +2 -2
  13. optimum/rbln/diffusers/configurations/pipelines/configuration_controlnet.py +3 -3
  14. optimum/rbln/diffusers/configurations/pipelines/configuration_cosmos.py +2 -2
  15. optimum/rbln/diffusers/configurations/pipelines/configuration_kandinsky2_2.py +4 -4
  16. optimum/rbln/diffusers/configurations/pipelines/configuration_stable_diffusion.py +2 -2
  17. optimum/rbln/diffusers/configurations/pipelines/configuration_stable_diffusion_3.py +2 -2
  18. optimum/rbln/diffusers/configurations/pipelines/configuration_stable_diffusion_xl.py +2 -2
  19. optimum/rbln/diffusers/modeling_diffusers.py +1 -1
  20. optimum/rbln/diffusers/models/__init__.py +3 -13
  21. optimum/rbln/diffusers/pipelines/__init__.py +11 -5
  22. optimum/rbln/diffusers/pipelines/auto_pipeline.py +237 -0
  23. optimum/rbln/diffusers/pipelines/cosmos/configuration_cosmos_guardrail.py +11 -6
  24. optimum/rbln/diffusers/pipelines/cosmos/cosmos_guardrail.py +14 -18
  25. optimum/rbln/diffusers/pipelines/cosmos/pipeline_cosmos_text2world.py +1 -1
  26. optimum/rbln/diffusers/pipelines/cosmos/pipeline_cosmos_video2world.py +1 -1
  27. optimum/rbln/diffusers/pipelines/kandinsky2_2/pipeline_kandinsky2_2_combined.py +1 -6
  28. optimum/rbln/modeling.py +3 -2
  29. optimum/rbln/modeling_base.py +29 -4
  30. optimum/rbln/ops/attn.py +158 -0
  31. optimum/rbln/ops/flash_attn.py +166 -0
  32. optimum/rbln/transformers/__init__.py +24 -0
  33. optimum/rbln/transformers/configuration_generic.py +6 -4
  34. optimum/rbln/transformers/modeling_generic.py +13 -8
  35. optimum/rbln/transformers/modeling_outputs.py +37 -0
  36. optimum/rbln/transformers/models/__init__.py +31 -16
  37. optimum/rbln/transformers/models/auto/__init__.py +2 -0
  38. optimum/rbln/transformers/models/auto/modeling_auto.py +14 -0
  39. optimum/rbln/transformers/models/bart/bart_architecture.py +1 -3
  40. optimum/rbln/transformers/models/bart/configuration_bart.py +2 -0
  41. optimum/rbln/transformers/models/bert/bert_architecture.py +16 -0
  42. optimum/rbln/transformers/models/bert/modeling_bert.py +8 -4
  43. optimum/rbln/transformers/models/blip_2/configuration_blip_2.py +2 -2
  44. optimum/rbln/transformers/models/blip_2/modeling_blip_2.py +7 -6
  45. optimum/rbln/transformers/models/clip/configuration_clip.py +3 -3
  46. optimum/rbln/transformers/models/colpali/colpali_architecture.py +1 -4
  47. optimum/rbln/transformers/models/colpali/configuration_colpali.py +2 -2
  48. optimum/rbln/transformers/models/colpali/modeling_colpali.py +2 -10
  49. optimum/rbln/transformers/models/decoderonly/configuration_decoderonly.py +43 -174
  50. optimum/rbln/transformers/models/decoderonly/decoderonly_architecture.py +101 -91
  51. optimum/rbln/transformers/models/decoderonly/decoderonly_runtime_utils.py +450 -0
  52. optimum/rbln/transformers/models/decoderonly/generation_decoderonly.py +88 -0
  53. optimum/rbln/transformers/models/decoderonly/modeling_decoderonly.py +296 -986
  54. optimum/rbln/transformers/models/depth_anything/__init__.py +16 -0
  55. optimum/rbln/transformers/models/depth_anything/configuration_depth_anything.py +24 -0
  56. optimum/rbln/transformers/models/depth_anything/modeling_depth_anything.py +25 -0
  57. optimum/rbln/transformers/models/gemma/gemma_architecture.py +1 -4
  58. optimum/rbln/transformers/models/gemma/modeling_gemma.py +9 -0
  59. optimum/rbln/transformers/models/gemma3/configuration_gemma3.py +3 -3
  60. optimum/rbln/transformers/models/gemma3/gemma3_runtime_utils.py +217 -0
  61. optimum/rbln/transformers/models/gemma3/modeling_gemma3.py +25 -251
  62. optimum/rbln/transformers/models/gpt2/modeling_gpt2.py +2 -0
  63. optimum/rbln/transformers/models/grounding_dino/__init__.py +10 -0
  64. optimum/rbln/transformers/models/grounding_dino/configuration_grounding_dino.py +86 -0
  65. optimum/rbln/transformers/models/grounding_dino/grounding_dino_architecture.py +507 -0
  66. optimum/rbln/transformers/models/grounding_dino/modeling_grounding_dino.py +1032 -0
  67. optimum/rbln/transformers/models/idefics3/configuration_idefics3.py +2 -2
  68. optimum/rbln/transformers/models/idefics3/modeling_idefics3.py +3 -9
  69. optimum/rbln/transformers/models/llama/modeling_llama.py +12 -3
  70. optimum/rbln/transformers/models/llava/configuration_llava.py +2 -2
  71. optimum/rbln/transformers/models/llava/modeling_llava.py +53 -14
  72. optimum/rbln/transformers/models/llava_next/configuration_llava_next.py +2 -2
  73. optimum/rbln/transformers/models/llava_next/modeling_llava_next.py +6 -16
  74. optimum/rbln/transformers/models/opt/modeling_opt.py +2 -30
  75. optimum/rbln/transformers/models/pegasus/configuration_pegasus.py +4 -0
  76. optimum/rbln/transformers/models/pegasus/modeling_pegasus.py +2 -0
  77. optimum/rbln/transformers/models/pegasus/pegasus_architecture.py +1 -3
  78. optimum/rbln/transformers/models/pixtral/configuration_pixtral.py +2 -2
  79. optimum/rbln/transformers/models/pixtral/modeling_pixtral.py +1 -4
  80. optimum/rbln/transformers/models/qwen2_5_vl/configuration_qwen2_5_vl.py +3 -3
  81. optimum/rbln/transformers/models/qwen2_5_vl/modeling_qwen2_5_vl.py +6 -15
  82. optimum/rbln/transformers/models/qwen2_5_vl/qwen2_5_vl_architecture.py +4 -7
  83. optimum/rbln/transformers/models/qwen3/modeling_qwen3.py +77 -3
  84. optimum/rbln/transformers/models/qwen3/qwen3_architecture.py +1 -4
  85. optimum/rbln/transformers/models/seq2seq/configuration_seq2seq.py +19 -2
  86. optimum/rbln/transformers/models/seq2seq/modeling_seq2seq.py +20 -1
  87. optimum/rbln/transformers/models/siglip/__init__.py +2 -6
  88. optimum/rbln/transformers/models/siglip/modeling_siglip.py +2 -2
  89. optimum/rbln/transformers/models/swin/__init__.py +16 -0
  90. optimum/rbln/transformers/models/swin/configuration_swin.py +42 -0
  91. optimum/rbln/transformers/models/swin/modeling_swin.py +341 -0
  92. optimum/rbln/transformers/models/t5/configuration_t5.py +2 -0
  93. optimum/rbln/transformers/models/t5/t5_architecture.py +8 -1
  94. optimum/rbln/transformers/models/time_series_transformer/configuration_time_series_transformer.py +2 -2
  95. optimum/rbln/transformers/models/time_series_transformer/modeling_time_series_transformer.py +4 -14
  96. optimum/rbln/transformers/models/whisper/configuration_whisper.py +10 -2
  97. optimum/rbln/transformers/models/whisper/modeling_whisper.py +20 -1
  98. optimum/rbln/transformers/models/xlm_roberta/__init__.py +2 -8
  99. optimum/rbln/transformers/utils/rbln_quantization.py +365 -65
  100. optimum/rbln/utils/runtime_utils.py +3 -3
  101. optimum/rbln/utils/submodule.py +10 -4
  102. {optimum_rbln-0.8.2rc0.dist-info → optimum_rbln-0.8.3.dist-info}/METADATA +1 -1
  103. {optimum_rbln-0.8.2rc0.dist-info → optimum_rbln-0.8.3.dist-info}/RECORD +105 -89
  104. {optimum_rbln-0.8.2rc0.dist-info → optimum_rbln-0.8.3.dist-info}/WHEEL +0 -0
  105. {optimum_rbln-0.8.2rc0.dist-info → optimum_rbln-0.8.3.dist-info}/licenses/LICENSE +0 -0
@@ -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, Dict, List, Optional, Union
14
+ from typing import Any, List, Optional, Union
15
15
 
16
16
  from ....configuration_utils import RBLNModelConfig
17
17
 
@@ -50,7 +50,7 @@ class RBLNColPaliForRetrievalConfig(RBLNModelConfig):
50
50
  max_seq_lens: Union[int, List[int]] = None,
51
51
  output_hidden_states: Optional[bool] = None,
52
52
  vision_tower: Optional[RBLNModelConfig] = None,
53
- **kwargs: Dict[str, Any],
53
+ **kwargs: Any,
54
54
  ):
55
55
  """
56
56
  Args:
@@ -17,10 +17,7 @@ from pathlib import Path
17
17
  from typing import TYPE_CHECKING, Any, Optional, Union
18
18
 
19
19
  import torch
20
- from transformers import (
21
- PretrainedConfig,
22
- PreTrainedModel,
23
- )
20
+ from transformers import PretrainedConfig, PreTrainedModel
24
21
  from transformers.modeling_outputs import BaseModelOutputWithPooling
25
22
  from transformers.modeling_utils import no_init_weights
26
23
  from transformers.models.colpali.modeling_colpali import ColPaliForRetrievalOutput
@@ -32,12 +29,7 @@ from .colpali_architecture import RBLNColPaliForRetrievalWrapper
32
29
 
33
30
 
34
31
  if TYPE_CHECKING:
35
- from transformers import (
36
- AutoFeatureExtractor,
37
- AutoProcessor,
38
- AutoTokenizer,
39
- PretrainedConfig,
40
- )
32
+ from transformers import AutoFeatureExtractor, AutoProcessor, AutoTokenizer, PretrainedConfig
41
33
 
42
34
 
43
35
  class LoopVisionTower:
@@ -34,6 +34,9 @@ class RBLNDecoderOnlyModelConfig(RBLNModelConfig):
34
34
  KV cache management, and batching for inference.
35
35
  """
36
36
 
37
+ _default_phases = ["prefill"]
38
+ _default_logits_to_keep = 0
39
+
37
40
  def __init__(
38
41
  self,
39
42
  batch_size: Optional[int] = None,
@@ -47,9 +50,12 @@ class RBLNDecoderOnlyModelConfig(RBLNModelConfig):
47
50
  quantization: Optional[Union[Dict[str, Any], RBLNQuantizationConfig]] = None,
48
51
  prefill_chunk_size: Optional[int] = None,
49
52
  kvcache_num_blocks: Optional[int] = None,
53
+ decoder_batch_sizes: Optional[List[int]] = None,
50
54
  cache_impl: Optional[CacheImplType] = None,
51
55
  sliding_window: Optional[int] = None,
52
56
  sliding_window_layers: Optional[List[int]] = None,
57
+ phases: Optional[List[PhaseType]] = None,
58
+ logits_to_keep: Optional[int] = None,
53
59
  **kwargs,
54
60
  ):
55
61
  """
@@ -80,6 +86,12 @@ class RBLNDecoderOnlyModelConfig(RBLNModelConfig):
80
86
  kvcache_num_blocks (Optional[int]): The total number of blocks to allocate for the
81
87
  PagedAttention KV cache. See the "KV Cache Number of Blocks (`kvcache_num_blocks`)"
82
88
  section below for details.
89
+ decoder_batch_sizes (Optional[List[int]]): A list of batch sizes for which separate decoder models will be compiled.
90
+ This allows the model to handle varying batch sizes efficiently during generation. If not specified,
91
+ defaults to a list containing only the model's main batch size. When specifying multiple batch sizes:
92
+ 1) All values must be less than or equal to the main batch size.
93
+ 2) The list will be sorted in descending order (larger batch sizes first).
94
+ 3) If using multiple decoders, at least one batch size should match the main batch size.
83
95
  cache_impl (Optional[CacheImplType]): Specifies the KV cache implementation strategy. Defaults to "static".
84
96
  - "static": Uses a fixed-size global KV cache for all layers, suitable for standard attention patterns.
85
97
  - "sliding_window": Implements a sliding window KV cache, where each layer maintains a local cache of recent tokens.
@@ -88,6 +100,10 @@ class RBLNDecoderOnlyModelConfig(RBLNModelConfig):
88
100
  you must specify the `sliding_window` size and optionally `sliding_window_layers` for hybrid mode.
89
101
  sliding_window (Optional[int]): The size of the sliding window. Defaults to None.
90
102
  sliding_window_layers (Optional[List[int]]): The layers to use for the sliding window used in the hybrid model. Defaults to None.
103
+ phases (Optional[List[PhaseType]]): The phases to compile the model for. Defaults to ["prefill"] if DecoderOnlyModel is used,
104
+ ["prefill", "decode"] if DecoderOnlyModelForCausalLM is used.
105
+ logits_to_keep (Optional[int]): The number of logits to keep for the decoder. If set to 0, the decoder will keep all logits.
106
+ Defaults to 0 if DecoderOnlyModel is used, 1 if DecoderOnlyModelForCausalLM is used.
91
107
  **kwargs: Additional arguments passed to the parent RBLNModelConfig.
92
108
 
93
109
  Raises:
@@ -156,8 +172,6 @@ class RBLNDecoderOnlyModelConfig(RBLNModelConfig):
156
172
  self.batch_size = batch_size or 1
157
173
  if not isinstance(self.batch_size, int) or self.batch_size < 0:
158
174
  raise ValueError(f"batch_size must be a positive integer, got {self.batch_size}")
159
- if self.batch_size > 1:
160
- raise NotImplementedError("Batch size > 1 is not supported for RBLNDecoderOnlyModel.")
161
175
 
162
176
  self.max_seq_len = max_seq_len
163
177
  self.use_inputs_embeds = use_inputs_embeds or False
@@ -183,178 +197,12 @@ class RBLNDecoderOnlyModelConfig(RBLNModelConfig):
183
197
  self.sliding_window = sliding_window
184
198
  self.sliding_window_layers = sliding_window_layers or []
185
199
 
186
- @property
187
- def use_global_attention(self):
188
- return self.cache_impl in ["static", "hybrid"]
189
-
190
- @property
191
- def use_local_attention(self):
192
- return self.cache_impl in ["sliding_window", "hybrid"]
193
-
194
-
195
- class RBLNDecoderOnlyModelForCausalLMConfig(RBLNDecoderOnlyModelConfig):
196
- """
197
- Configuration class for RBLN decoder-only models for Causal Language Modeling.
198
-
199
- This class extends RBLNModelConfig with parameters specific to decoder-only transformer
200
- architectures optimized for RBLN devices. It controls aspects like attention implementation,
201
- KV cache management, and batching for inference.
202
- """
203
-
204
- def __init__(
205
- self,
206
- batch_size: Optional[int] = None,
207
- max_seq_len: Optional[int] = None,
208
- use_inputs_embeds: Optional[bool] = None,
209
- use_attention_mask: Optional[bool] = None,
210
- use_position_ids: Optional[bool] = None,
211
- attn_impl: Optional[str] = None,
212
- kvcache_partition_len: Optional[int] = None,
213
- kvcache_block_size: Optional[int] = None,
214
- quantization: Optional[Union[Dict[str, Any], RBLNQuantizationConfig]] = None,
215
- prefill_chunk_size: Optional[int] = None,
216
- kvcache_num_blocks: Optional[int] = None,
217
- decoder_batch_sizes: Optional[List[int]] = None,
218
- cache_impl: Optional[CacheImplType] = None,
219
- sliding_window: Optional[int] = None,
220
- sliding_window_layers: Optional[List[int]] = None,
221
- phases: Optional[List[PhaseType]] = None,
222
- **kwargs,
223
- ):
224
- """
225
- Args:
226
- batch_size (Optional[int]): The batch size for inference. Defaults to 1.
227
- max_seq_len (Optional[int]): The maximum sequence length supported by the model.
228
- If not provided, it attempts to infer from the model's configuration
229
- (`max_position_embeddings` or `n_positions`). Must be specified if not available
230
- in the model config.
231
- use_inputs_embeds (Optional[bool]): Whether to use input embeddings (`inputs_embeds`)
232
- directly instead of `input_ids`. Defaults to False. Requires the model to be
233
- compiled with this option enabled.
234
- use_attention_mask (Optional[bool]): Whether the model requires attention masks during
235
- inference. This is typically determined based on the target device and model
236
- architecture. Defaults are often set automatically based on the model and RBLN NPU.
237
- use_position_ids (Optional[bool]): Whether to use position IDs. Defaults to False.
238
- attn_impl (Optional[str]): Specifies the attention implementation to use.
239
- See the "Attention Implementation (`attn_impl`)" section below for details.
240
- kvcache_partition_len (Optional[int]): Defines the partition length for the KV cache
241
- when using "flash_attn". See the "KV Cache Partition Length (`kvcache_partition_len`)"
242
- section below for details.
243
- kvcache_block_size (Optional[int]): Sets the size (in number of tokens) of each block
244
- in the PagedAttention KV cache. See the "KV Cache Block Size (`kvcache_block_size`)"
245
- section below for details.
246
- quantization (Optional[Dict[str, Any]]): Configuration dictionary for applying model
247
- quantization. Specifies format, etc.
248
- prefill_chunk_size (Optional[int]): The chunk size used during the prefill phase for
249
- processing input sequences. Defaults to 128. Must be a positive integer
250
- divisible by 64. Affects prefill performance and memory usage.
251
- kvcache_num_blocks (Optional[int]): The total number of blocks to allocate for the
252
- PagedAttention KV cache. See the "KV Cache Number of Blocks (`kvcache_num_blocks`)"
253
- section below for details.
254
- decoder_batch_sizes (Optional[List[int]]): A list of batch sizes for which separate decoder models will be compiled.
255
- This allows the model to handle varying batch sizes efficiently during generation. If not specified,
256
- defaults to a list containing only the model's main batch size. When specifying multiple batch sizes:
257
- 1) All values must be less than or equal to the main batch size.
258
- 2) The list will be sorted in descending order (larger batch sizes first).
259
- 3) If using multiple decoders, at least one batch size should match the main batch size.
260
- cache_impl (Optional[CacheImplType]): Specifies the KV cache implementation strategy. Defaults to "static".
261
- - "static": Uses a fixed-size global KV cache for all layers, suitable for standard attention patterns.
262
- - "sliding_window": Implements a sliding window KV cache, where each layer maintains a local cache of recent tokens.
263
- - "hybrid": Combines both static and sliding window approaches, allowing different layers to use different cache strategies.
264
- The choice affects memory usage and attention patterns. When using "sliding_window" or "hybrid",
265
- you must specify the `sliding_window` size and optionally `sliding_window_layers` for hybrid mode.
266
- sliding_window (Optional[int]): The size of the sliding window. Defaults to None.
267
- sliding_window_layers (Optional[List[int]]): The layers to use for the sliding window used in the hybrid model. Defaults to None.
268
- phases (Optional[List[PhaseType]]): The phases to compile the model for. Defaults to None.
269
- **kwargs: Additional arguments passed to the parent RBLNModelConfig.
270
-
271
- Raises:
272
- ValueError: If `batch_size` is not a positive integer.
273
- ValueError: If `prefill_chunk_size` is not a positive integer divisible by 64.
274
- ValueError: If `max_seq_len` cannot be determined and is required.
275
- ValueError: If attention parameter constraints are violated (e.g., `max_seq_len` vs
276
- `kvcache_partition_len` for flash attention).
277
-
278
-
279
- Attention Implementation:
280
- `attn_impl` determines the underlying attention mechanism used by the model.
281
-
282
- - **`"eager"`** (Default if `kvcache_partition_len` is not set): Uses the standard PyTorch
283
- attention implementation. Suitable for sequences up to a certain limit (e.g., 32,768 tokens).
284
- - **`"flash_attn"`**: Utilizes an optimized Flash Attention implementation, beneficial for
285
- longer sequences and potentially faster execution. Requires `max_seq_len` to be at least
286
- 8,192. If `kvcache_partition_len` is specified, `attn_impl` automatically defaults
287
- to `"flash_attn"`. When using `"flash_attn"`, `kvcache_block_size` must equal
288
- `kvcache_partition_len`.
289
-
290
- The choice impacts performance and memory usage, especially for long sequences.
291
- Constraints related to `max_seq_len` and `kvcache_partition_len` apply when using
292
- `"flash_attn"`.
293
-
294
-
295
- KV Cache Partition Length:
296
- `kvcache_partition_len` is relevant **only** when `attn_impl` is `"flash_attn"`.
297
-
298
- - It defines the length (number of tokens) of each partition within the Key-Value (KV) cache.
299
- - Must be between 4,096 and 32,768 (inclusive).
300
- - When using `"flash_attn"`, `max_seq_len` must be a multiple of `kvcache_partition_len`
301
- and at least twice its value (`max_seq_len >= 2 * kvcache_partition_len`).
302
- - If `attn_impl` is `"flash_attn"` and `kvcache_partition_len` is `None`, it defaults to
303
- 16,384.
304
-
305
-
306
- KV Cache Number of Blocks:
307
- `kvcache_num_blocks` controls the total number of memory blocks allocated for the PagedAttention KV cache.
308
- Each block holds `kvcache_block_size` tokens of Key and Value states.
309
-
310
- - **Automatic Estimation (Default)**: If `kvcache_num_blocks` is `None`, the system estimates
311
- the maximum number of blocks that can fit into the available RBLN device memory. This
312
- calculation considers the model size (kernel memory), required buffer memory, the number
313
- of layers and heads, `kvcache_block_size`, tensor parallelism, and available RBLN NPU DRAM.
314
- This aims to maximize cache capacity for potentially better performance with long sequences
315
- or larger batches without manual tuning.
316
- - **Manual Setting**: You can explicitly set the number of blocks. This provides finer control
317
- but requires careful consideration of memory limits. Setting it too high may lead to
318
- compilation errors if it exceeds available memory. The system will issue warnings if your
319
- setting exceeds the estimated maximum.
320
- - **Performance Impact**: A larger number of blocks reduces the likelihood of cache eviction,
321
- which is beneficial for tasks involving many long sequences or large batch sizes, enabling
322
- higher throughput. However, allocating more blocks consumes more memory.
323
- - **Minimum Requirement**: The system requires a minimum number of blocks to function,
324
- calculated based on `max_seq_len`, `kvcache_block_size`, and `batch_size`. The number of
325
- allocated blocks must be sufficient to hold at least one full sequence length per item
326
- in the batch concurrently. The system will log warnings or raise errors if constraints
327
- are violated (e.g., if `kvcache_num_blocks` is less than `batch_size` when using Flash Attention).
328
-
329
- The optimal value depends on the specific model, task, hardware, and desired trade-off
330
- between performance and memory usage. The automatic estimation provides a robust starting point.
331
- """
332
-
333
- super().__init__(
334
- max_seq_len=max_seq_len,
335
- use_inputs_embeds=use_inputs_embeds,
336
- use_attention_mask=use_attention_mask,
337
- use_position_ids=use_position_ids,
338
- attn_impl=attn_impl,
339
- kvcache_partition_len=kvcache_partition_len,
340
- kvcache_block_size=kvcache_block_size,
341
- quantization=quantization,
342
- prefill_chunk_size=prefill_chunk_size,
343
- kvcache_num_blocks=kvcache_num_blocks,
344
- cache_impl=cache_impl,
345
- sliding_window=sliding_window,
346
- sliding_window_layers=sliding_window_layers,
347
- **kwargs,
348
- )
349
-
350
- # override batch_size for causal lm
351
- self.batch_size = batch_size or 1
352
- if not isinstance(self.batch_size, int) or self.batch_size < 0:
353
- raise ValueError(f"batch_size must be a positive integer, got {self.batch_size}")
354
-
355
200
  if phases is not None:
356
201
  self.validate_phases_type(phases)
357
- self.phases = phases or ["prefill", "decode"]
202
+ self.phases = phases or self._default_phases
203
+ self.logits_to_keep = logits_to_keep or self._default_logits_to_keep
204
+ if self.logits_to_keep is not None and self.logits_to_keep > 1:
205
+ raise NotImplementedError("`logits_to_keep` > 1 is currently not supported for RBLN models.")
358
206
 
359
207
  if "decode" in self.phases:
360
208
  self.decoder_batch_sizes = decoder_batch_sizes
@@ -384,9 +232,30 @@ class RBLNDecoderOnlyModelForCausalLMConfig(RBLNDecoderOnlyModelConfig):
384
232
  raise ValueError(f"All elements in `phases` must be of type `PhaseType`({get_args(PhaseType)}).")
385
233
 
386
234
  @property
387
- def use_multiple_decoder(self):
235
+ def use_global_attention(self) -> bool:
236
+ return self.cache_impl in ["static", "hybrid"]
237
+
238
+ @property
239
+ def use_local_attention(self) -> bool:
240
+ return self.cache_impl in ["sliding_window", "hybrid"]
241
+
242
+ @property
243
+ def use_multiple_decoder(self) -> bool:
388
244
  return isinstance(self.decoder_batch_sizes, list) and len(self.decoder_batch_sizes) > 1
389
245
 
390
246
  @property
391
- def can_generate(self):
247
+ def can_generate(self) -> bool:
392
248
  return "decode" in self.phases
249
+
250
+
251
+ class RBLNDecoderOnlyModelForCausalLMConfig(RBLNDecoderOnlyModelConfig):
252
+ """
253
+ Configuration class for RBLN decoder-only models for Causal Language Modeling.
254
+
255
+ This class extends RBLNModelConfig with parameters specific to decoder-only transformer
256
+ architectures optimized for RBLN devices. It controls aspects like attention implementation,
257
+ KV cache management, and batching for inference.
258
+ """
259
+
260
+ _default_phases = ["prefill", "decode"]
261
+ _default_logits_to_keep = 1