unsloth/tests/utils
Taranum Wasu 881cea038e fix(attn-mask-compat): preserve JIT/FX tracing detection in fallback
Codex review follow-up on PR #6880 (P2). The previous fallback
(``def is_tracing(tensor=None): return is_torchdynamo_compiling()``)
was strictly less conservative than what upstream
``transformers==4.51.3`` did inline before ``is_tracing`` was added
to ``transformers.utils.import_utils``. The pre-4.52 upstream
expression was:

    is_tracing = torch.jit.is_tracing() or isinstance(
        inputs_embeds, torch.fx.Proxy
    ) or is_torchdynamo_compiling()

The previous fallback only consulted Dynamo. That meant callers
tracing or exporting under transformers 4.51.x would silently hit
the data-dependent ``torch.all(attention_mask == 1)`` branch in
``_ignore_causal_mask_sdpa`` (and the equivalent in
``_prepare_4d_attention_mask_for_sdpa``) — failing on proxy control
flow or baking the wrong SDPA causal-mask path.

The local fallback now mirrors the legacy upstream expression:

- ``torch.jit.is_tracing()`` for ``torch.jit.trace`` /
  ``torch.jit.script`` flows.
- ``isinstance(tensor, torch.fx.Proxy)`` for ``symbolic_trace`` and
  ``torch.export`` paths that don't go through Dynamo.
- ``is_torchdynamo_compiling()`` for ``torch.compile`` and
  ``torch._dynamo`` paths.

The CUDA stream capture, FakeTensor, and JAX (torchax) checks the
modern ``is_tracing`` does are out of scope: those need newer
``import_utils`` helpers, and the conservative dynamo fallback is
the right choice when those helpers aren't available. This matches
the behavior of the upstream ``is_tracing`` helpers that landed in
4.52 in the first place — those were new additions, not behaviour
that pre-existed in 4.51.x.

Tests:
- ``test_import_falls_back_when_is_tracing_missing`` now exercises
  all three branches (dynamo idle → False; ``torch.fx.Proxy`` arg →
  True; patched ``torch.jit.is_tracing()`` → True).
- All 26 tests in ``tests/utils/test_attn_mask_compat.py`` pass.
- ``ruff check`` and ``ruff format`` clean on both files.

Signed-off-by: Taranum Wasu <taranumwasu@Taranums-MacBook-Pro.local>
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-08 03:59:07 +05:30
..
__init__.py Revert "[FIX] Vllm guided decoding params (#3662)" 2025-12-01 05:43:45 -08:00
aime_eval.md reroute merge logic language models + comprehensive tests + eval kits (#2673) 2025-06-02 20:32:57 -07:00
aime_eval.py Reduce and tighten comments and docstrings across the test suite (#6429) 2026-06-18 01:07:09 -07:00
cleanup_utils.py Reduce and tighten comments and docstrings across the test suite (#6429) 2026-06-18 01:07:09 -07:00
data_utils.py Reduce and tighten comments and docstrings across the test suite (#6429) 2026-06-18 01:07:09 -07:00
generate_dataset_with_none.py Reduce and tighten comments and docstrings across the test suite (#6429) 2026-06-18 01:07:09 -07:00
hf_utils.py Reduce and tighten comments and docstrings across the test suite (#6429) 2026-06-18 01:07:09 -07:00
ocr_eval.md Fix Typos in Documentation and Comments (#2721) 2025-06-17 04:34:51 -07:00
ocr_eval.py Reduce and tighten comments and docstrings across the test suite (#6429) 2026-06-18 01:07:09 -07:00
os_utils.py Reduce and tighten comments and docstrings across the test suite (#6429) 2026-06-18 01:07:09 -07:00
perplexity_eval.md reroute merge logic language models + comprehensive tests + eval kits (#2673) 2025-06-02 20:32:57 -07:00
perplexity_eval.py Reduce and tighten comments and docstrings across the test suite (#6429) 2026-06-18 01:07:09 -07:00
run_none_detect_tests.py Reduce and tighten comments and docstrings across the test suite (#6429) 2026-06-18 01:07:09 -07:00
test_attention_dispatch_dora_dtype.py Fix FlashAttention fp32 crash with DoRA (use_dora=True) (#6526) 2026-06-23 01:29:19 -07:00
test_attention_masks.py Formatting: ruff line-length 100, kwarg-spacing passes, drop blank after short local imports (#6079) 2026-06-08 04:24:13 -07:00
test_attn_mask_compat.py fix(attn-mask-compat): preserve JIT/FX tracing detection in fallback 2026-07-08 03:59:07 +05:30
test_batched_leftpad_generation_gpu.py Reduce and tighten comments and docstrings across the test suite (#6429) 2026-06-18 01:07:09 -07:00
test_packing.py Reduce and tighten comments and docstrings across the test suite (#6429) 2026-06-18 01:07:09 -07:00
test_prepare_inputs_leftpad.py Reduce and tighten comments and docstrings across the test suite (#6429) 2026-06-18 01:07:09 -07:00
test_q_galore.py Reduce and tighten comments and docstrings across the test suite (#6429) 2026-06-18 01:07:09 -07:00
test_qat.py Reduce and tighten comments and docstrings across the test suite (#6429) 2026-06-18 01:07:09 -07:00
test_rope_scaling_drift.py Reduce and tighten comments and docstrings across the test suite (#6429) 2026-06-18 01:07:09 -07:00
test_trunc_normal_patch.py Reduce and tighten comments and docstrings across the test suite (#6429) 2026-06-18 01:07:09 -07:00