diff --git a/tests/utils/test_attn_mask_compat.py b/tests/utils/test_attn_mask_compat.py index 231aad49de..a1959714b3 100644 --- a/tests/utils/test_attn_mask_compat.py +++ b/tests/utils/test_attn_mask_compat.py @@ -33,17 +33,16 @@ compat = _load_compat_module() def test_no_deprecation_warning_on_causal_mask(): - with warnings.catch_warnings(record=True) as caught: + with warnings.catch_warnings(record = True) as caught: warnings.simplefilter("always") - compat.AttentionMaskConverter(is_causal=True, sliding_window=3).to_causal_4d( + compat.AttentionMaskConverter(is_causal = True, sliding_window = 3).to_causal_4d( 1, 8, 8, - dtype=torch.float16, + dtype = torch.float16, ) assert not any( - issubclass(w.category, FutureWarning) - and "modeling_attn_mask_utils" in str(w.message) + issubclass(w.category, FutureWarning) and "modeling_attn_mask_utils" in str(w.message) for w in caught ) @@ -63,23 +62,23 @@ def test_causal_4d_matches_transformers(batch_size, query_length, sliding_window with warnings.catch_warnings(): warnings.simplefilter("ignore", FutureWarning) expected = legacy.AttentionMaskConverter( - is_causal=True, - sliding_window=sliding_window, + is_causal = True, + sliding_window = sliding_window, ).to_causal_4d( batch_size, query_length, key_value_length, - dtype=dtype, + dtype = dtype, ) actual = compat.AttentionMaskConverter( - is_causal=True, - sliding_window=sliding_window, + is_causal = True, + sliding_window = sliding_window, ).to_causal_4d( batch_size, query_length, key_value_length, - dtype=dtype, + dtype = dtype, ) if expected is None: @@ -97,7 +96,9 @@ def test_causal_4d_matches_transformers(batch_size, query_length, sliding_window (torch.tensor([[1, 1, 1, 0, 0], [1, 1, 1, 1, 1]]), 0), ], ) -def test_prepare_4d_causal_attention_mask_for_sdpa_matches_transformers(attention_mask, past_length): +def test_prepare_4d_causal_attention_mask_for_sdpa_matches_transformers( + attention_mask, past_length +): try: legacy = importlib.import_module("transformers.modeling_attn_mask_utils") except ImportError: @@ -105,7 +106,7 @@ def test_prepare_4d_causal_attention_mask_for_sdpa_matches_transformers(attentio batch_size = 2 if attention_mask is not None else 1 query_length = 5 - inputs_embeds = torch.zeros(batch_size, query_length, 16, dtype=torch.float32) + inputs_embeds = torch.zeros(batch_size, query_length, 16, dtype = torch.float32) with warnings.catch_warnings(): warnings.simplefilter("ignore", FutureWarning) @@ -114,7 +115,7 @@ def test_prepare_4d_causal_attention_mask_for_sdpa_matches_transformers(attentio (batch_size, query_length), inputs_embeds, past_length, - sliding_window=3, + sliding_window = 3, ) actual = compat._prepare_4d_causal_attention_mask_for_sdpa( @@ -122,7 +123,7 @@ def test_prepare_4d_causal_attention_mask_for_sdpa_matches_transformers(attentio (batch_size, query_length), inputs_embeds, past_length, - sliding_window=3, + sliding_window = 3, ) if expected is None: @@ -137,14 +138,14 @@ def test_prepare_4d_attention_mask_for_sdpa_matches_transformers(): except ImportError: pytest.skip("transformers.modeling_attn_mask_utils unavailable") - mask = torch.tensor([[1, 1, 0, 0], [1, 1, 1, 1]], dtype=torch.float32) + mask = torch.tensor([[1, 1, 0, 0], [1, 1, 1, 1]], dtype = torch.float32) dtype = torch.float32 with warnings.catch_warnings(): warnings.simplefilter("ignore", FutureWarning) - expected = legacy._prepare_4d_attention_mask_for_sdpa(mask, dtype=dtype) + expected = legacy._prepare_4d_attention_mask_for_sdpa(mask, dtype = dtype) - actual = compat._prepare_4d_attention_mask_for_sdpa(mask, dtype=dtype) + actual = compat._prepare_4d_attention_mask_for_sdpa(mask, dtype = dtype) if expected is None: assert actual is None @@ -158,7 +159,7 @@ def test_repo_has_no_direct_deprecated_imports(): for path in model_dir.glob("*.py"): if path.name == "_attn_mask_compat.py": continue - text = path.read_text(encoding="utf-8") + text = path.read_text(encoding = "utf-8") if "transformers.modeling_attn_mask_utils" in text: offenders.append(str(path.relative_to(_REPO_ROOT))) assert offenders == [] diff --git a/unsloth/models/_attn_mask_compat.py b/unsloth/models/_attn_mask_compat.py index cfc4b3e345..bba78697f5 100644 --- a/unsloth/models/_attn_mask_compat.py +++ b/unsloth/models/_attn_mask_compat.py @@ -41,7 +41,8 @@ try: # before `is_tracing` existed (only `is_torchdynamo_compiling`). from transformers.utils.import_utils import is_tracing # type: ignore[attr-defined] except ImportError: - def is_tracing(tensor=None) -> bool: # type: ignore[no-redef] + + def is_tracing(tensor = None) -> bool: # type: ignore[no-redef] """Local fallback for transformers < 4.52. Returns True only when Dynamo is actively compiling. Other tracing