diff --git a/src/transformers/modeling_attn_mask_utils.py b/src/transformers/modeling_attn_mask_utils.py index 08288876b38f..fdb5e0dec658 100644 --- a/src/transformers/modeling_attn_mask_utils.py +++ b/src/transformers/modeling_attn_mask_utils.py @@ -17,18 +17,15 @@ and will be removed in the future. """ +import warnings from dataclasses import dataclass from typing import Union 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`." @@ -71,7 +68,7 @@ class AttentionMaskConverter: sliding_window: int def __init__(self, is_causal: bool, sliding_window: int | None = None): - logger.warning_once(DEPRECATION_MESSAGE, FutureWarning) + warnings.warn(DEPRECATION_MESSAGE, FutureWarning) self.is_causal = is_causal self.sliding_window = sliding_window @@ -172,7 +169,7 @@ def _make_causal_mask( """ Make causal mask used for bi-directional self-attention. """ - logger.warning_once(DEPRECATION_MESSAGE, FutureWarning) + warnings.warn(DEPRECATION_MESSAGE, FutureWarning) bsz, tgt_len = input_ids_shape mask = torch.full((tgt_len, tgt_len), torch.finfo(dtype).min, device=device) @@ -202,7 +199,7 @@ 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) + warnings.warn(DEPRECATION_MESSAGE, FutureWarning) bsz, src_len = mask.size() tgt_len = tgt_len if tgt_len is not None else src_len @@ -254,7 +251,7 @@ def _unmask_unattended( [0, 1, 1]]]] ``` """ - logger.warning_once(DEPRECATION_MESSAGE, FutureWarning) + warnings.warn(DEPRECATION_MESSAGE, FutureWarning) # fmt: on if expanded_mask.dtype == torch.bool: @@ -281,7 +278,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) + warnings.warn(DEPRECATION_MESSAGE, FutureWarning) _, query_length = inputs_embeds.shape[0], inputs_embeds.shape[1] key_value_length = query_length + past_key_values_length @@ -464,7 +461,7 @@ 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) + warnings.warn(DEPRECATION_MESSAGE, FutureWarning) _, key_value_length = mask.shape tgt_len = tgt_len if tgt_len is not None else key_value_length