unsloth/tests/utils
Daniel Han 6974e4d37b fix: add FastQwen3_5Model with fused CE loss for Qwen3.5 OOM (#4188)
Qwen3.5 has a 248,320-token vocabulary. At 8K context the full logits
tensor is 8192 x 248320 x 4 = 7.68 GB, which causes OOM on T4/P100.

The unsloth compiler already applies fused CE via apply_fused_lm_head,
but this adds an explicit FastQwen3_5Model dispatch for cleaner routing
and better error messages when Qwen3.5 is not supported.

Changes:
- Add unsloth/models/qwen3_5.py with FastQwen3_5Model that patches
  Qwen3_5ForConditionalGeneration and Qwen3_5ForCausalLM forwards to
  use unsloth_fused_ce_loss directly from hidden_states
- Add loader dispatch for model_type == "qwen3_5" before "qwen3"
- Version gate uses >= 5.0.0 (qwen3_5 only exists in transformers 5.x)
- Guarded import in loader.py with try/except fallback
- GDN layers intentionally left unpatched (flash-linear-attention)
- 23 unit tests covering all 4 code paths

Fixes from original PR #4331 by @vitalis:
- Add explicit _get_dtype import (wildcard import skips _-prefixed names)
- Single-token fast path now checks labels is None before returning early
- Default model name corrected to Qwen/Qwen3.5-9B (8B does not exist)
- Test assertion on nn.Linear removed (not a mock)
- Unused imports removed

Tested: Qwen3.5-0.8B 4bit training, 1.38 GB peak memory, 23/23 tests pass.
Backwards compatible: import unsloth works on transformers 4.57.6.
2026-03-16 22:54:36 +00:00
..
__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 Revert "[pre-commit.ci] auto fixes from pre-commit.com hooks" 2025-12-01 07:24:58 -08:00
cleanup_utils.py Revert "[FIX] Vllm guided decoding params (#3662)" 2025-12-01 05:43:45 -08:00
data_utils.py Revert "[FIX] Vllm guided decoding params (#3662)" 2025-12-01 05:43:45 -08:00
hf_utils.py Revert "[pre-commit.ci] auto fixes from pre-commit.com hooks" 2025-12-01 07:24:58 -08:00
ocr_eval.md Fix Typos in Documentation and Comments (#2721) 2025-06-17 04:34:51 -07:00
ocr_eval.py Revert "[FIX] Vllm guided decoding params (#3662)" 2025-12-01 05:43:45 -08:00
os_utils.py Revert "[FIX] Vllm guided decoding params (#3662)" 2025-12-01 05:43:45 -08: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 Revert "[FIX] Vllm guided decoding params (#3662)" 2025-12-01 05:43:45 -08:00
test_attention_masks.py SFT sample packing (#3566) 2025-12-09 17:36:45 -08:00
test_packing.py Refactor Ollama template wiring and harden packing helpers (#3890) 2026-02-09 04:04:48 -08:00
test_qat.py add weight-only int8 QAT scheme and update tests for torchao 0.15.0 (#3859) 2026-01-16 09:32:29 +05:30
test_qwen3_5.py fix: add FastQwen3_5Model with fused CE loss for Qwen3.5 OOM (#4188) 2026-03-16 22:54:36 +00:00
test_trunc_normal_patch.py Patch trunc_normal_ for low-precision stability (#4027) 2026-02-19 04:40:14 -08:00