Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
a6ba941
fix
vasqu Dec 12, 2025
c8d6984
fix order
vasqu Dec 12, 2025
8095a1c
style
vasqu Dec 12, 2025
fed0582
vision 3d rope get extra test for now
vasqu Dec 12, 2025
8522356
fix gpt2
vasqu Dec 12, 2025
69be66d
more gpt2 fixes
vasqu Dec 12, 2025
c35529d
let's see...
vasqu Dec 12, 2025
e5f4fdb
fix
vasqu Dec 12, 2025
021732c
test
vasqu Dec 12, 2025
d24b530
fix opt+biogpt
vasqu Dec 12, 2025
41a805c
fix
vasqu Dec 12, 2025
1cdd34d
fix
vasqu Dec 12, 2025
7a69035
fix
vasqu Dec 12, 2025
15ac740
fix opt
vasqu Dec 12, 2025
c08292b
mask exchange test
vasqu Dec 12, 2025
5ecbb66
style
vasqu Dec 12, 2025
7ce86ec
Merge branch 'main' into fix-fa-posids-tests
vasqu Dec 12, 2025
fc0b716
several small fixes
vasqu Dec 13, 2025
fe6f1e5
shouldnt be needed
vasqu Dec 13, 2025
0e5e70b
fix zamba models
vasqu Dec 13, 2025
34fff72
retrigger ci
vasqu Dec 13, 2025
676c828
force skip for now
vasqu Dec 13, 2025
6910316
this wont work, will fix step by step
vasqu Dec 15, 2025
0ac29a2
Merge branch 'main' into fix-fa-posids-tests
vasqu Dec 18, 2025
335fd8f
to git
vasqu Dec 18, 2025
423d423
another batch
vasqu Dec 18, 2025
cbc1644
fix a few models, clip related models are gonna be hard...
vasqu Dec 18, 2025
28ad3c8
another batch
vasqu Dec 18, 2025
3d71308
Merge branch 'main' into fix-fa-posids-tests
vasqu Dec 18, 2025
2ad3686
style
vasqu Dec 18, 2025
91bcb07
fix gpt2 attempt
vasqu Dec 18, 2025
d35466a
another batch + some models do not set their attn implementation? TODO
vasqu Dec 18, 2025
a698fd8
fix
vasqu Dec 18, 2025
eac4bed
last models
vasqu Dec 18, 2025
8bdb76f
Merge branch 'main' into fix-fa-posids-tests
vasqu Feb 4, 2026
ae6f2a6
style
vasqu Feb 4, 2026
5cf843a
repo fix
vasqu Feb 4, 2026
2504d17
Merge branch 'main' into fix-fa-posids-tests
vasqu Feb 4, 2026
ca653ed
check
vasqu Feb 4, 2026
f60629d
some quick fixes, error to catch wrong inits in some models
vasqu Feb 4, 2026
03c8159
small fixes
vasqu Feb 4, 2026
7988322
fixes for wrong mask pretrained model relation
vasqu Feb 5, 2026
5bea0a9
fix
vasqu Feb 5, 2026
6edbb5d
remove mask defaulting --> that's part of the prep + fixup some other…
vasqu Feb 5, 2026
4bfa416
small fixes
vasqu Feb 5, 2026
f797e63
fix last few models --> last to check recurrent gemma + repo consistency
vasqu Feb 5, 2026
09c0b8b
fixup test cleanup
vasqu Feb 6, 2026
0606ad9
Merge branch 'main' into fix-fa-posids-tests
vasqu Feb 6, 2026
77a6ee9
revert these tests
vasqu Feb 6, 2026
28f8a74
these were not necessary, they have a proper top module
vasqu Feb 6, 2026
e53bcdb
fixup kwargs
vasqu Feb 6, 2026
a61a3dc
Merge branch 'main' into fix-fa-posids-tests
vasqu Feb 6, 2026
87e56b0
remove old API
vasqu Feb 6, 2026
ca27bcc
more kwargs
vasqu Feb 6, 2026
9a32c44
let's revert this - im in a fork :D
vasqu Feb 6, 2026
1b602dc
fix
vasqu Feb 6, 2026
26c3aaa
dang
vasqu Feb 6, 2026
0a67029
revert removal and add deprecation msg
vasqu Feb 9, 2026
9f53f1c
kwargs typing
vasqu Feb 9, 2026
bef3e2b
style
vasqu Feb 9, 2026
8116e63
Merge branch 'main' into fix-fa-posids-tests
vasqu Feb 9, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
926 changes: 381 additions & 545 deletions examples/modular-transformers/modeling_test_detr.py

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just reapplied modular

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions src/transformers/modeling_attn_mask_utils.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,19 @@

import torch

from .utils import logging
from .utils.import_utils import is_torchdynamo_compiling, is_tracing


logger = logging.get_logger(__name__)


DEPRECATION_MESSAGE = (
"The attention mask API under `transformers.modeling_attn_mask_utils` (`AttentionMaskConverter`) "
"is deprecated and will be removed in Transformers v5.10. Please use the new API in `transformers.masking_utils`."
)


@dataclass
class AttentionMaskConverter:
"""
Expand Down Expand Up @@ -61,6 +71,8 @@ class AttentionMaskConverter:
sliding_window: int

def __init__(self, is_causal: bool, sliding_window: int | None = None):
logger.warning_once(DEPRECATION_MESSAGE, FutureWarning)

self.is_causal = is_causal
self.sliding_window = sliding_window

Expand Down Expand Up @@ -160,6 +172,8 @@ def _make_causal_mask(
"""
Make causal mask used for bi-directional self-attention.
"""
logger.warning_once(DEPRECATION_MESSAGE, FutureWarning)

bsz, tgt_len = input_ids_shape
mask = torch.full((tgt_len, tgt_len), torch.finfo(dtype).min, device=device)
mask_cond = torch.arange(mask.size(-1), device=device)
Expand Down Expand Up @@ -188,6 +202,8 @@ def _expand_mask(mask: torch.Tensor, dtype: torch.dtype, tgt_len: int | None = N
"""
Expands attention_mask from `[bsz, seq_len]` to `[bsz, 1, tgt_seq_len, src_seq_len]`.
"""
logger.warning_once(DEPRECATION_MESSAGE, FutureWarning)

bsz, src_len = mask.size()
tgt_len = tgt_len if tgt_len is not None else src_len

Expand Down Expand Up @@ -238,6 +254,8 @@ def _unmask_unattended(
[0, 1, 1]]]]
```
"""
logger.warning_once(DEPRECATION_MESSAGE, FutureWarning)

# fmt: on
if expanded_mask.dtype == torch.bool:
raise ValueError(
Expand All @@ -263,6 +281,7 @@ def _ignore_causal_mask_sdpa(
allowing to dispatch to the flash attention kernel (that can otherwise not be used if a custom `attn_mask` is
passed).
"""
logger.warning_once(DEPRECATION_MESSAGE, FutureWarning)

_, query_length = inputs_embeds.shape[0], inputs_embeds.shape[1]
key_value_length = query_length + past_key_values_length
Expand Down Expand Up @@ -445,6 +464,8 @@ def _prepare_4d_attention_mask_for_sdpa(mask: torch.Tensor, dtype: torch.dtype,
tgt_len (`int`):
The target length or query length the created mask shall have.
"""
logger.warning_once(DEPRECATION_MESSAGE, FutureWarning)

_, key_value_length = mask.shape
tgt_len = tgt_len if tgt_len is not None else key_value_length

Expand Down
14 changes: 7 additions & 7 deletions src/transformers/models/align/modeling_align.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ def forward(
hidden_states: torch.Tensor,
attention_mask: torch.FloatTensor | None = None,
output_attentions: bool | None = False,
**kwargs,
**kwargs: Unpack[TransformersKwargs],
) -> tuple[torch.Tensor]:
input_shape = hidden_states.shape[:-1]
hidden_shape = (*input_shape, -1, self.attention_head_size)
Expand Down Expand Up @@ -674,7 +674,7 @@ def forward(
hidden_states: torch.Tensor,
attention_mask: torch.FloatTensor | None = None,
output_attentions: bool | None = False,
**kwargs,
**kwargs: Unpack[TransformersKwargs],
) -> tuple[torch.Tensor]:
self_outputs = self.self(
hidden_states,
Expand Down Expand Up @@ -732,7 +732,7 @@ def forward(
hidden_states: torch.Tensor,
attention_mask: torch.FloatTensor | None = None,
output_attentions: bool | None = False,
**kwargs,
**kwargs: Unpack[TransformersKwargs],
) -> tuple[torch.Tensor]:
self_attention_outputs = self.attention(
hidden_states,
Expand Down Expand Up @@ -771,7 +771,7 @@ def forward(
output_attentions: bool | None = False,
output_hidden_states: bool | None = False,
return_dict: bool | None = True,
**kwargs,
**kwargs: Unpack[TransformersKwargs],
) -> tuple[torch.Tensor] | BaseModelOutput:
all_hidden_states = () if output_hidden_states else None
all_self_attentions = () if output_attentions else None
Expand Down Expand Up @@ -897,7 +897,7 @@ def forward(
output_attentions: bool | None = None,
output_hidden_states: bool | None = None,
return_dict: bool | None = None,
**kwargs,
**kwargs: Unpack[TransformersKwargs],
) -> tuple | BaseModelOutputWithPooling:
r"""
Examples:
Expand Down Expand Up @@ -1010,7 +1010,7 @@ def forward(
pixel_values: torch.FloatTensor | None = None,
output_hidden_states: bool | None = None,
return_dict: bool | None = None,
**kwargs,
**kwargs: Unpack[TransformersKwargs],
) -> tuple | BaseModelOutputWithPoolingAndNoAttention:
r"""
Examples:
Expand Down Expand Up @@ -1173,7 +1173,7 @@ def forward(
output_attentions: bool | None = None,
output_hidden_states: bool | None = None,
return_dict: bool | None = None,
**kwargs,
**kwargs: Unpack[TransformersKwargs],
) -> tuple | AlignOutput:
r"""
return_loss (`bool`, *optional*):
Expand Down
39 changes: 11 additions & 28 deletions src/transformers/models/altclip/modeling_altclip.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
from ...processing_utils import Unpack
from ...pytorch_utils import apply_chunking_to_forward
from ...utils import ModelOutput, TransformersKwargs, auto_docstring, can_return_tuple, logging, torch_int
from ...utils.generic import is_flash_attention_requested
from .configuration_altclip import AltCLIPConfig, AltCLIPTextConfig, AltCLIPVisionConfig


Expand Down Expand Up @@ -344,7 +343,7 @@ def forward(
hidden_states: torch.Tensor,
attention_mask: torch.FloatTensor | None = None,
output_attentions: bool | None = False,
**kwargs,
**kwargs: Unpack[TransformersKwargs],
) -> tuple[torch.Tensor]:
self_attention_outputs = self.attention(
hidden_states,
Expand Down Expand Up @@ -384,7 +383,7 @@ def forward(
output_attentions: bool | None = False,
output_hidden_states: bool | None = False,
return_dict: bool | None = True,
**kwargs,
**kwargs: Unpack[TransformersKwargs],
) -> tuple[torch.Tensor] | BaseModelOutput:
all_hidden_states = () if output_hidden_states else None
all_self_attentions = () if output_attentions else None
Expand Down Expand Up @@ -481,8 +480,8 @@ def forward(
self,
hidden_states: torch.Tensor,
attention_mask: torch.Tensor | None = None,
causal_attention_mask: torch.Tensor | None = None,
output_attentions: bool | None = False,
**kwargs: Unpack[TransformersKwargs],
) -> tuple[torch.Tensor, torch.Tensor | None]:
"""Input shape: Batch x Time x Channel"""

Expand All @@ -495,15 +494,6 @@ def forward(
queries = queries.view(batch_size, seq_length, self.num_heads, self.head_dim).transpose(1, 2)
keys = keys.view(batch_size, seq_length, self.num_heads, self.head_dim).transpose(1, 2)
values = values.view(batch_size, seq_length, self.num_heads, self.head_dim).transpose(1, 2)
# CLIP text model uses both `causal_attention_mask` and `attention_mask`
# in case FA2 kernel is called, `is_causal` should be inferred from `causal_attention_mask`
if not is_flash_attention_requested(self.config):
if attention_mask is not None and causal_attention_mask is not None:
attention_mask = attention_mask + causal_attention_mask
elif causal_attention_mask is not None:
attention_mask = causal_attention_mask
else:
self.is_causal = causal_attention_mask is not None

attention_interface: Callable = ALL_ATTENTION_FUNCTIONS.get_interface(
self.config._attn_implementation, eager_attention_forward
Expand All @@ -515,9 +505,9 @@ def forward(
keys,
values,
attention_mask,
is_causal=self.is_causal,
scaling=self.scale,
dropout=0.0 if not self.training else self.dropout,
**kwargs,
)

attn_output = attn_output.reshape(batch_size, seq_length, embed_dim).contiguous()
Expand Down Expand Up @@ -556,8 +546,8 @@ def forward(
self,
hidden_states: torch.Tensor,
attention_mask: torch.Tensor,
causal_attention_mask: torch.Tensor,
output_attentions: bool | None = False,
**kwargs: Unpack[TransformersKwargs],
) -> tuple[torch.FloatTensor]:
"""
Args:
Expand All @@ -575,8 +565,8 @@ def forward(
hidden_states, attn_weights = self.self_attn(
hidden_states=hidden_states,
attention_mask=attention_mask,
causal_attention_mask=causal_attention_mask,
output_attentions=output_attentions,
**kwargs,
)
hidden_states = residual + hidden_states

Expand Down Expand Up @@ -613,10 +603,10 @@ def forward(
self,
inputs_embeds,
attention_mask: torch.Tensor | None = None,
causal_attention_mask: torch.Tensor | None = None,
output_attentions: bool | None = None,
output_hidden_states: bool | None = None,
return_dict: bool | None = None,
**kwargs: Unpack[TransformersKwargs],
) -> tuple | BaseModelOutput:
r"""
Args:
Expand All @@ -630,13 +620,6 @@ def forward(
- 1 for tokens that are **not masked**,
- 0 for tokens that are **masked**.

[What are attention masks?](../glossary#attention-mask)
causal_attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*):
Causal mask for the text model. Mask values selected in `[0, 1]`:

- 1 for tokens that are **not masked**,
- 0 for tokens that are **masked**.

[What are attention masks?](../glossary#attention-mask)
output_attentions (`bool`, *optional*):
Whether or not to return the attentions tensors of all attention layers. See `attentions` under
Expand All @@ -663,8 +646,8 @@ def forward(
layer_outputs = encoder_layer(
hidden_states,
attention_mask,
causal_attention_mask,
output_attentions=output_attentions,
**kwargs,
)

hidden_states = layer_outputs[0]
Expand Down Expand Up @@ -896,7 +879,7 @@ def forward(
output_hidden_states: bool | None = None,
interpolate_pos_encoding: bool = False,
return_dict: bool | None = None,
**kwargs,
**kwargs: Unpack[TransformersKwargs],
) -> tuple | BaseModelOutputWithPooling:
r"""
Examples:
Expand Down Expand Up @@ -1070,7 +1053,7 @@ def forward(
output_attentions: bool | None = None,
return_dict: bool | None = None,
output_hidden_states: bool | None = None,
**kwargs,
**kwargs: Unpack[TransformersKwargs],
) -> tuple | BaseModelOutputWithPoolingAndProjection:
r"""
Examples:
Expand Down Expand Up @@ -1244,7 +1227,7 @@ def forward(
output_hidden_states: bool | None = None,
interpolate_pos_encoding: bool = False,
return_dict: bool | None = None,
**kwargs,
**kwargs: Unpack[TransformersKwargs],
) -> tuple | AltCLIPOutput:
r"""
return_loss (`bool`, *optional*):
Expand Down
54 changes: 12 additions & 42 deletions src/transformers/models/autoformer/modeling_autoformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,15 @@
from ... import initialization as init
from ...activations import ACT2FN
from ...cache_utils import Cache, DynamicCache, EncoderDecoderCache
from ...modeling_attn_mask_utils import (
_prepare_4d_attention_mask,
_prepare_4d_attention_mask_for_sdpa,
)
from ...masking_utils import create_bidirectional_mask
from ...modeling_layers import GradientCheckpointingLayer
from ...modeling_outputs import BaseModelOutput, ModelOutput, SampleTSPredictionOutput, Seq2SeqTSPredictionOutput
from ...modeling_utils import PreTrainedModel
from ...time_series_utils import NegativeBinomialOutput, NormalOutput, StudentTOutput
from ...utils import auto_docstring, is_torch_flex_attn_available, logging
from ...utils.generic import is_flash_attention_requested
from ...utils import auto_docstring, logging
from .configuration_autoformer import AutoformerConfig


if is_torch_flex_attn_available():
from ...integrations.flex_attention import make_flex_block_causal_mask


logger = logging.get_logger(__name__)


Expand Down Expand Up @@ -844,27 +836,6 @@ def _init_weights(self, module: nn.Module):
init.ones_(module.weight)
init.zeros_(module.bias)

# copied from transformers.models.bart.modeling_bart.BartPreTrainedModel._update_full_mask
def _update_full_mask(
self,
attention_mask: torch.Tensor | None,
inputs_embeds: torch.Tensor,
):
if attention_mask is not None:
if is_flash_attention_requested(self.config):
attention_mask = attention_mask if 0 in attention_mask else None
elif self.config._attn_implementation == "sdpa":
# [bsz, seq_len] -> [bsz, 1, tgt_seq_len, src_seq_len]
attention_mask = _prepare_4d_attention_mask_for_sdpa(attention_mask, inputs_embeds.dtype)
elif self.config._attn_implementation == "flex_attention":
if isinstance(attention_mask, torch.Tensor):
attention_mask = make_flex_block_causal_mask(attention_mask, is_causal=False)
else:
# [bsz, seq_len] -> [bsz, 1, tgt_seq_len, src_seq_len]
attention_mask = _prepare_4d_attention_mask(attention_mask, inputs_embeds.dtype)

return attention_mask


# copied from transformers.models.time_series_transformer.modeling_time_series_transformer.TimeSeriesTransformerEncoder with TimeSeriesTransformer->Autoformer,TimeSeries->Autoformer
class AutoformerEncoder(AutoformerPreTrainedModel):
Expand Down Expand Up @@ -938,9 +909,10 @@ def forward(
hidden_states = self.layernorm_embedding(hidden_states + embed_pos)
hidden_states = nn.functional.dropout(hidden_states, p=self.dropout, training=self.training)

attention_mask = self._update_full_mask(
attention_mask,
inputs_embeds,
attention_mask = create_bidirectional_mask(
config=self.config,
input_embeds=inputs_embeds,
attention_mask=attention_mask,
)

encoder_states = () if output_hidden_states else None
Expand Down Expand Up @@ -1086,8 +1058,6 @@ def forward(
)
use_cache = False

input_shape = inputs_embeds.size()[:-1]

if self.gradient_checkpointing and use_cache:
logger.warning(
"`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`..."
Expand All @@ -1097,12 +1067,12 @@ def forward(
if use_cache and past_key_values is None:
past_key_values = EncoderDecoderCache(DynamicCache(config=self.config), DynamicCache(config=self.config))

# expand encoder attention mask
if encoder_hidden_states is not None and encoder_attention_mask is not None:
# [bsz, seq_len] -> [bsz, 1, tgt_seq_len, src_seq_len]
encoder_attention_mask = _prepare_4d_attention_mask(
encoder_attention_mask, inputs_embeds.dtype, tgt_len=input_shape[-1]
)
encoder_attention_mask = create_bidirectional_mask(
config=self.config,
input_embeds=inputs_embeds,
attention_mask=encoder_attention_mask,
encoder_hidden_states=encoder_hidden_states,
)

hidden_states = self.value_embedding(inputs_embeds)
embed_pos = self.embed_positions(
Expand Down
Loading