Skip to content
10 changes: 5 additions & 5 deletions docs/source/en/modular_transformers.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ class Olmo2Attention(OlmoAttention):
hidden_states: torch.Tensor,
position_embeddings: tuple[torch.Tensor, torch.Tensor],
attention_mask: Optional[torch.Tensor],
past_key_value: Optional[Cache] = None,
past_key_values: Optional[Cache] = None,
cache_position: Optional[torch.LongTensor] = None,
**kwargs,
) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]:
Expand All @@ -236,10 +236,10 @@ class Olmo2Attention(OlmoAttention):
cos, sin = position_embeddings
query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin)

if past_key_value is not None:
if past_key_values is not None:
# sin and cos are specific to RoPE models; cache_position needed for the static cache
cache_kwargs = {"sin": sin, "cos": cos, "cache_position": cache_position}
key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx, cache_kwargs)
key_states, value_states = past_key_values.update(key_states, value_states, self.layer_idx, cache_kwargs)

attention_interface: Callable = eager_attention_forward
if self.config._attn_implementation != "eager":
Expand Down Expand Up @@ -290,7 +290,7 @@ class Olmo2DecoderLayer(OlmoDecoderLayer):
hidden_states: torch.Tensor,
attention_mask: Optional[torch.Tensor] = None,
position_ids: Optional[torch.LongTensor] = None,
past_key_value: Optional[Cache] = None,
past_key_values: Optional[Cache] = None,
output_attentions: Optional[bool] = False,
use_cache: Optional[bool] = False,
cache_position: Optional[torch.LongTensor] = None,
Expand All @@ -304,7 +304,7 @@ class Olmo2DecoderLayer(OlmoDecoderLayer):
hidden_states=hidden_states,
attention_mask=attention_mask,
position_ids=position_ids,
past_key_value=past_key_value,
past_key_values=past_key_values,
output_attentions=output_attentions,
use_cache=use_cache,
cache_position=cache_position,
Expand Down
3 changes: 3 additions & 0 deletions examples/modular-transformers/modeling_add_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import torch
from torch import nn

from ...utils.deprecation import deprecate_kwarg


def rotate_half(x):
"""Rotates half the hidden dims of the input."""
Expand Down Expand Up @@ -62,5 +64,6 @@ class TestAttention(nn.Module):
def __init__(self):
pass

@deprecate_kwarg("past_key_value", new_name="past_key_values", version="4.58")
def forward(self) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]:
_ = apply_rotary_pos_emb(1, 1, 1, 1)
292 changes: 154 additions & 138 deletions examples/modular-transformers/modeling_dummy_bert.py

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions examples/modular-transformers/modeling_global_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from ...cache_utils import Cache
from ...processing_utils import Unpack
from ...utils import TransformersKwargs
from ...utils.deprecation import deprecate_kwarg
from .configuration_global_indexing import GlobalIndexingConfig


Expand Down Expand Up @@ -125,12 +126,13 @@ def __init__(self, config: GlobalIndexingConfig, layer_idx: int):
config.num_attention_heads * self.head_dim, config.hidden_size, bias=config.attention_bias
)

@deprecate_kwarg("past_key_value", new_name="past_key_values", version="4.58")
def forward(
self,
hidden_states: torch.Tensor,
position_embeddings: tuple[torch.Tensor, torch.Tensor],
attention_mask: Optional[torch.Tensor],
past_key_value: Optional[Cache] = None,
past_key_values: Optional[Cache] = None,
cache_position: Optional[torch.LongTensor] = None,
**kwargs: Unpack[TransformersKwargs],
) -> tuple[torch.Tensor, torch.Tensor]:
Expand All @@ -144,10 +146,10 @@ def forward(
cos, sin = position_embeddings
query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin)

if past_key_value is not None:
if past_key_values is not None:
# sin and cos are specific to RoPE models; cache_position needed for the static cache
cache_kwargs = {"sin": sin, "cos": cos, "cache_position": cache_position}
key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx, cache_kwargs)
key_states, value_states = past_key_values.update(key_states, value_states, self.layer_idx, cache_kwargs)

attention_interface: Callable = eager_attention_forward
if self.config._attn_implementation != "eager":
Expand Down
4 changes: 2 additions & 2 deletions examples/modular-transformers/modeling_multimodal2.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ def forward(

@auto_docstring
class Multimodal2VisionPreTrainedModel(PreTrainedModel):
config_class = Multimodal2Config
config: Multimodal2Config
base_model_prefix = "multimodal2_vision"
supports_gradient_checkpointing = True
_supports_sdpa = True
Expand All @@ -512,7 +512,7 @@ def _init_weights(self, module):

@add_start_docstrings("New doc", MULTIMODAL2_VISION_START_DOCSTRING)
class Multimodal2VisionModel(Multimodal2VisionPreTrainedModel):
config_class = Multimodal2VisionConfig
config: Multimodal2VisionConfig
main_input_name = "pixel_values"
_no_split_modules = ["Multimodal2VisionEncoderLayer"]

Expand Down
Loading