unsloth/tests/python
Daniel Han 1c91f49d83
fix: unblock 4 tests deselected/skipped in #5312 (real bugs) (#5359)
* fix: unblock 4 tests deselected/skipped in #5312 (real bugs)

PR #5312 surfaced two real regressions by turning previously-silent
skips into explicit `--deselect` / `pytest.skip(...)` blocks. Both
were left as follow-ups rather than fixed in that PR. This PR fixes
the underlying bugs so the suppressions can be dropped.

1. studio/backend/requirements/no-torch-runtime.txt: pin tokenizers

   Installing with `--no-deps -r no-torch-runtime.txt` (the path
   install.sh takes for the no-torch / GGUF-only mode) resolves
   transformers to 5.3.0 and tokenizers to the latest available
   (0.23.1). transformers 5.3.0 requires
   `tokenizers>=0.22.0,<=0.23.0`, so `from transformers import
   AutoConfig` then fails at import time:

       ImportError: tokenizers>=0.22.0,<=0.23.0 is required for a
       normal functioning of this module, but found
       tokenizers==0.23.1.

   Pin `tokenizers>=0.22.0,<=0.23.0` to match the constraint
   embedded inside every transformers version in the allowed window
   (4.56.0..5.3.0). Verified locally: a fresh `uv venv` + `uv pip
   install --no-deps -r no-torch-runtime.txt` followed by
   `from transformers import AutoConfig` now succeeds.

   Unblocks 3 deselected cases in studio-backend-ci.yml:
     - TestE2ETokenizersFix::test_autoconfig_works_with_no_torch_runtime
       (parametrized py 3.12 + 3.13 -> 2 cases)
     - TestE2EFullNoTorchSandbox::test_autoconfig_succeeds

2. unsloth/models/rl.py: defensive wrapper for _patch_trl_rl_trainers

   _patch_trl_rl_trainers has many internal `try: ... except: ...
   return` branches, but several paths (notably inspect.getsource on
   the thin wrappers TRL 1.x leaves in trl.trainer for trainers that
   moved to trl.experimental) can still propagate exceptions. The
   umbrella patch_trl_rl_trainers() ring-fences each call with
   try/except + warning_once, but direct callers (the CI shim in
   consolidated-tests-ci.yml, downstream tools, end-user scripts)
   used to see the raw exception, which forced #5312's CI heredoc to
   ring-fence with:

       except Exception as e:
           # TRL 1.x renames break the patch helper internally; we
           # accept that here and skip rather than fail the cell.
           pytest.skip(f"_patch_trl_rl_trainers raised: ...")

   Rename the existing implementation to _patch_trl_rl_trainers_impl
   and make _patch_trl_rl_trainers a thin wrapper that catches any
   uncaught exception and routes it through logger.info, matching
   the umbrella wrapper's behaviour. Power users who want the raw
   raising behaviour for their own diagnostics can still call
   _patch_trl_rl_trainers_impl directly.

   Adds tests/python/test_patch_trl_rl_trainers_defensive.py to lock
   the contract: the wrapper must never raise, and it must delegate
   to the impl on the happy path.

   Unblocks 1 skip in consolidated-tests-ci.yml's
   test_compile_sft_trainer_patch.

Follow-up for #5312 once this lands: drop the two `--deselect` lines
in studio-backend-ci.yml's repo-cpu-tests step and drop the
`except Exception ... pytest.skip(f"_patch_trl_rl_trainers raised: ")`
block in consolidated-tests-ci.yml's test_compile_sft_trainer_patch.

* chore: tighten comments and docstrings in the new code

Drop verbose justifications down to one or two lines per site.
The PR description carries the full context; in-file comments
only need to point at the WHY.

* chore(no-torch-runtime): drop redundant lower bound on tokenizers

tokenizers 0.23.0 was never published to PyPI (versions go 0.22.2 ->
0.23.1), so `tokenizers<=0.23.0` resolves to 0.22.2 in practice, the
same version the explicit >=0.22.0,<=0.23.0 pin resolved to. Verified
on Python 3.12 and 3.13.
2026-05-11 02:39:17 -07:00
..
__init__.py Consolidate dual venvs and separate install from update (#4530) 2026-03-25 05:24:21 -07:00
conftest.py fix: add tokenizers to no-torch deps and TORCH_CONSTRAINT for arm64 macOS py313+ (#4748) 2026-04-01 06:12:17 -07:00
test_cross_platform_parity.py Add configurable PyTorch mirror via UNSLOTH_PYTORCH_MIRROR env var (#5024) 2026-04-15 11:39:11 +04:00
test_dpo_vision_processor_passthrough.py Fix DPO trainer multi process hang (#5199) 2026-04-29 04:15:34 -07:00
test_e2e_no_torch_sandbox.py tests: add no-torch / Intel Mac test suite (#4646) 2026-03-27 02:33:45 -07:00
test_fast_sentence_transformer_redirect_lifecycle.py Add Studio PR-time CI: pin enforcement, frontend, backend, wheel smoke (#5298) 2026-05-06 04:41:57 -07:00
test_flash_attn_install_python_stack.py [Studio] Install flash attn at setup time for linux (#4979) 2026-04-14 16:40:17 +04:00
test_gpu_init_ldconfig_guard.py feat(studio): MLX training tab on Apple Silicon (LoRA / full FT, VLM, export) (#5265) 2026-05-05 23:54:58 -07:00
test_install_python_stack.py Consolidate dual venvs and separate install from update (#4530) 2026-03-25 05:24:21 -07:00
test_no_torch_filtering.py [Studio] Install flash attn at setup time for linux (#4979) 2026-04-14 16:40:17 +04:00
test_patch_trl_rl_trainers_defensive.py fix: unblock 4 tests deselected/skipped in #5312 (real bugs) (#5359) 2026-05-11 02:39:17 -07:00
test_studio_import_no_torch.py tests: add no-torch / Intel Mac test suite (#4646) 2026-03-27 02:33:45 -07:00
test_tokenizers_and_torch_constraint.py fix: add tokenizers to no-torch deps and TORCH_CONSTRAINT for arm64 macOS py313+ (#4748) 2026-04-01 06:12:17 -07:00
test_unsloth_run_tool_policy_resolver.py unsloth run: add --enable-tools/--disable-tools server-side tool policy (#5277) 2026-05-05 12:45:15 +04:00