Commit graph

3,319 commits

Author SHA1 Message Date
Datta Nimmaturi
753dcd255f [trl] vllm trl topk fixup (#3935)
* [transformers] [v5] remove unused hybridcache (#3910)

* remote unused hybridcache

* cleanup

* Fix top_k on trl GRPO

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: Daniel Han <danielhanchen@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2026-01-31 06:34:07 -08:00
Pádraic Slattery
84767abe4e chore: Update outdated GitHub Actions version (#3936) 2026-01-27 07:19:38 -08:00
pre-commit-ci[bot]
40067d1bac [pre-commit.ci] pre-commit autoupdate (#3937)
updates:
- [github.com/astral-sh/ruff-pre-commit: v0.14.13 → v0.14.14](https://github.com/astral-sh/ruff-pre-commit/compare/v0.14.13...v0.14.14)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2026-01-27 07:18:26 -08:00
Daniel Han
c1839a2043 Update pyproject.toml 2026-01-27 07:17:45 -08:00
pluesclues
b4c8c93b79 Grpo compile settings update (#3927)
* Add torch compile options for GRPOTrainer

* Update CUDA settings based on device capability

* Add triton persistent TMA matmul condition

* Fix syntax for triton.enable_persistent_tma_matmul

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update rl.py

* Update rl.py

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2026-01-24 17:17:55 -08:00
Michael Han
d4e2ec5c73 Embedding model fine-tuning support 2026-01-22 21:35:46 -08:00
Rachel Li
1e30424ead Guard torch.compile on ROCm when triton_key is missing (#3923)
* Guard torch.compile on ROCm when triton_key missing

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update unsloth/import_fixes.py

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Tighten ROCm Triton import handling

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: Rachel Li <rachelliqx07@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
2026-01-22 15:46:08 -08:00
Michael Han
a6fc72fd35 Embedding model support 2026-01-22 14:22:03 -08:00
Daniel Han
289509206f Update vision.py 2026-01-22 07:40:51 -08:00
electroglyph
17b4d90295 add FastSentenceTransformer for easily finetuning SentenceTransformer models (#3719)
* add FastSentenceTransformer

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Gemini code review suggestions

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* unsloth-zoo patch only fixed usage for XLMRobertaForMaskedLM, this is a fix for XLMRobertaModel

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* refactor do_lower_case

* add some comments

* force disable FP8 loading

* refactor pooling detection, add missing pooling types

* add save_pretrained_merged method which gets modules and config

* fix _save_pretrained_merged

* rename read_pooling_mode, load modules instead of hard-coding em

* comment

* revert save_pretrained_merged change

* propagate trust_remote_code properly

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* add super hacky mpnet patch from hell

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* refactor _load_modules, add for_inference to from_pretrained, add transformers 5 code for mpnet, add distilbert patches

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* add ModernBert

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* deberta-v2 support (provisional), fix remote_code

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* add generic add_pooling_layer logic

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* fix for missing config

* add push_to_hub_merged

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* edit messages, throw exception if no HF token

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* fix device_map mismatch

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* add comments, move import, other suggestions by Datta0

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* re-add adapter removal to save_pretrained_merged, but if saving to folder which had adapters before, leave them

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* add unsloth branding to save_pretrained_merged

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* propagate dtype to internal module when loading for inference

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* fix mpnet gradient checkpointing for torch >= 2.9

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* same thing for transformers 5, oops =)

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Fix FastSentenceTransformer performance: 6x speedup via torch.compile + SDPA

The original implementation was 31% slower than naive SentenceTransformer due to
conflicting decorators from Unsloth's auto-compiler (@torch.compile on attention
modules but @torch.compiler.disable on sub-modules).

Changes:
- Add fast encoder path that bypasses Unsloth patching for encoder models
- Use native torch.compile with mode="reduce-overhead" for 6x speedup
- Auto-detect and enable SDPA for models that support it (BERT, RoBERTa, etc.)
- Change defaults: load_in_16bit=True, load_in_4bit=False (16-bit is optimal)
- Change default: use_gradient_checkpointing=False (conflicts with torch.compile)
- Add UNSLOTH_COMPILE_DISABLE=1 env var to fall back to old path if needed

Supported encoder types: mpnet, bert, distilbert, roberta, xlm-roberta, albert, electra

Benchmark results (BS=32, seq_len=128):
- Naive 16-bit LoRA:     13-50ms per iter
- Unsloth 16-bit LoRA:   2-9ms per iter (5.4x-6.7x faster)
- Memory usage:          61MB-1.3GB (even largest model fits easily)

Note: 4-bit + torch.compile has a PyTorch bug (pytorch/pytorch#90665).
4-bit is also 1.7-1.9x slower than 16-bit due to dequantization overhead,
so 16-bit is recommended for these small encoder models anyway.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Use Unsloth's prepare_model_for_kbit_training for consistency

Changed from peft.prepare_model_for_kbit_training to
unsloth.models._utils.prepare_model_for_kbit_training.

Unsloth's version provides:
- Float32 mixed precision upcasting for LoRA layers
- Better numerical stability
- Consistency with rest of Unsloth codebase

* Use relative imports and add float16 machine support

- Changed absolute import to relative: from ._utils import prepare_model_for_kbit_training
- Added SUPPORTS_BFLOAT16 import for proper dtype detection
- Handle devices that don't support bfloat16 by falling back to float16

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* add save_pretrained_torchao

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Add auto-compile for torch.compile based on training step breakeven analysis

Changes:
- Change default compile_mode from "reduce-overhead" to "default" since CUDA
  Graphs (used by reduce-overhead) is incompatible with PEFT/LoRA
- Add _estimate_compile_threshold() to calculate minimum steps needed for
  torch.compile to be beneficial based on model parameter count
- Add _apply_torch_compile() helper with accelerate unwrap_model bug workaround
- Defer torch.compile application to trainer initialization time so we can
  check max_steps against the breakeven threshold
- Patch SentenceTransformerTrainer to auto-apply compile when max_steps
  exceeds the calculated threshold

Breakeven thresholds (with 1.2x safety margin):
- 22M params (MiniLM): ~1388 steps
- 110M params (mpnet): ~242 steps
- 335M params (snowflake): ~203 steps

This ensures torch.compile warmup cost is only paid when training is long
enough to benefit from the speedup.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* do QAT preparation for fast path

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* fix double loading model, thanks Etherl

* do mpnet gradient checkpoint patch if gc is enabled

* remove distilbert patches from mpnet fix

* sanity check on model params, thanks Etherl

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* add save_pretrained_gguf, thanks Etherl

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Refine compile threshold estimation for sentence transformers

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Daniel Han <danielhanchen@gmail.com>
Co-authored-by: Daniel Han <danielhanchen@users.noreply.github.com>
2026-01-22 07:35:55 -08:00
Daniel Han
292159b413 Versioning 2026-01-22 07:33:59 -08:00
Daniel Han
9dc65a4e7c Handle Transformers 5 vLLM import errors (#3908)
* Handle Transformers 5 vLLM import errors

* Deduplicate vLLM transformers mismatch handling

---------

Co-authored-by: danielhanchen <danielhanchen@users.noreply.github.com>
2026-01-20 01:02:39 -08:00
pluesclues
cf3dbcf959 Fix vllm ipykernel patch (#3907)
* Implement vLLM patch for notebook detection

Add patch for vLLM compatibility in notebook environments.

* Fix sys.stdout.fileno for vLLM compatibility

Patch sys.stdout.fileno for vLLM compatibility in notebooks.

* Add patch_vllm_for_notebooks to initialization

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Harden vLLM notebook stdout patch

* Use logger for vLLM notebook patch

* Clarify vLLM notebook patch log message

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: danielhanchen <danielhanchen@users.noreply.github.com>
2026-01-19 21:04:27 -08:00
pre-commit-ci[bot]
0f6782ccd4 [pre-commit.ci] pre-commit autoupdate (#3905)
updates:
- [github.com/astral-sh/ruff-pre-commit: v0.14.11 → v0.14.13](https://github.com/astral-sh/ruff-pre-commit/compare/v0.14.11...v0.14.13)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2026-01-19 18:42:13 -08:00
electroglyph
20c434cd77 add weight-only int8 QAT scheme and update tests for torchao 0.15.0 (#3859)
* add int8 weight-only QAT scheme, add test, fix tests for current torchao version

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* change quantization to PerAxis

* lambda =/

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* add torchao messages, remove group_size from int8

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* raise exception on missing torchao

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* touch up the torchao imports

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2026-01-16 09:32:29 +05:30
Michael Han
c4a718ca31 Update README.md 2026-01-15 08:01:01 -08:00
Daniel Han
ecd10f2e55 Update pyproject.toml 2026-01-15 07:00:25 -08:00
Daniel Han
6edbfbc435 Update _utils.py 2026-01-15 05:09:26 -08:00
pluesclues
2164423ea6 Merge pull request #3628 from pluesclues/alternative_compute_chunked_loss
Chunk Across Batch and Context length for logprob calculations for grpo
2026-01-15 05:01:19 -08:00
Daniel Han
832fffa40a Merge pull request #3895 from Datta0/rl_ref_trl
[trl] use non lora model as base for RL
2026-01-15 03:33:09 -08:00
pre-commit-ci[bot]
f5dde984a1 [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2026-01-15 11:25:11 +00:00
Datta Nimmaturi
7f6dc63dc8 use non lora model as base for RL 2026-01-15 11:23:21 +00:00
Daniel Han
b55a2e30eb Merge pull request #3879 from ducviet00/fix-gc
Disable gradient checkpointing when explicitly off for vision
2026-01-14 04:32:02 -08:00
Michael Han
45eeae95c5 Update template.md 2026-01-14 03:45:35 -08:00
Daniel Han
9adebb0dcf Merge pull request #3880 from f14-bertolotti/f14-wrong-ndim
wrong number of dimensions
2026-01-12 21:32:48 -08:00
Daniel Han
0f0b870781 Apply suggestion from @danielhanchen 2026-01-12 21:32:20 -08:00
Daniel Han
22ff11315e Merge pull request #3881 from unslothai/pre-commit-ci-update-config
[pre-commit.ci] pre-commit autoupdate
2026-01-12 21:29:58 -08:00
pre-commit-ci[bot]
2f8c4d962b [pre-commit.ci] pre-commit autoupdate
updates:
- [github.com/astral-sh/ruff-pre-commit: v0.14.10 → v0.14.11](https://github.com/astral-sh/ruff-pre-commit/compare/v0.14.10...v0.14.11)
2026-01-12 19:08:13 +00:00
Francesco Bertolotti
eaf3f932e0 wrong number of dimensions 2026-01-12 16:19:43 +01:00
Duc-Viet Hoang
5b422f7a06 Complete disable gradient_checkpointing for vision when use_gradient_checkpointing=False 2026-01-12 10:03:54 +07:00
Daniel Han
bf7b5b06a1 Merge pull request #3865 from ykaitao/ktyang_configure_embedding_for_training
reduce code duplication by _offload_frozen_module_for_training
2026-01-09 21:02:55 -08:00
danielhanchen
6465496ab2 fix: use peft.utils.other for ModulesToSaveWrapper import
ModulesToSaveWrapper was removed from peft.tuners.tuners_utils in PEFT
0.16.0. The class has been available in peft.utils.other since at least
PEFT 0.7.1, which is the minimum version Unsloth requires.

This fixes the ImportError when using PEFT >= 0.16.0.
2026-01-09 23:24:39 +00:00
Kaitao Yang
0bff0ffbe5 reduce code duplication by _offload_frozen_module_for_training 2026-01-09 06:07:38 -08:00
Daniel Han
0e54b817af Merge pull request #3869 from hnxnq7/fix-kaggle-telemetry-detection
Fix Kaggle telemetry misclassification when COLAB_ keys exist
2026-01-08 17:29:23 -08:00
pre-commit-ci[bot]
3ce1060dd1 [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2026-01-09 00:33:01 +00:00
Rachel Li
1193c9f526 Fix Kaggle telemetry detection & address review feedback
- Fix Kaggle misclassification by prioritizing filesystem markers over env vars
- Preserve telemetry pings when statistics is explicitly provided
- Replace bare except with except Exception
- Minor cleanup based on automated review feedback
2026-01-08 19:32:33 -05:00
Rachel Li
84701c55ff Fix telemetry ping regression for explicit statistics
Fixed Codex regression: keep snapshot_download pings for explicit statistics values; detection only runs when statistics is None. Also replaced bare except.
2026-01-08 19:20:24 -05:00
pre-commit-ci[bot]
e7d68f3e57 [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2026-01-09 00:04:59 +00:00
Rachel Li
e13160ddc8 Update _utils.py
fixed indentation
2026-01-08 19:04:30 -05:00
pre-commit-ci[bot]
67bef80b1f [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2026-01-08 23:49:55 +00:00
Rachel Li
1cdf751f8e Fix Kaggle telemetry misclassification when COLAB_ keys exist
Problem: Kaggle notebook environments can expose both KAGGLE_* and COLAB_* environment keys. _get_statistics currently checks COLAB_ before KAGGLE_, causing Kaggle sessions to be labeled colab/colabpro.

Prefer filesystem markers (e.g. /kaggle/working, /content + /opt/colab) before env-key heuristics, then fall back to the existing env-key checks. This avoids misclassification when providers leak overlapping env vars.

Kaggle test notebook: https://www.kaggle.com/code/hnxnq07/kaggle-stats-gathering-test
2026-01-08 18:44:22 -05:00
Daniel Han
ff8f5cd328 Merge pull request #3612 from Vangmay/feature/raw-text-dataprep
Feature/raw text dataprep
2026-01-08 03:38:15 -08:00
pre-commit-ci[bot]
3ac4f3c213 [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2026-01-08 11:35:21 +00:00
Daniel Han
24bbe8a97a Fix bugs and add improvements to RawTextDataLoader
- Fix test file: use return_tokenized instead of return_tensors
- Fix test file: use text_dataset instead of undefined dataset variable
- Move parameter validation to constructor (fail fast on invalid params)
- Add labels field in tokenized output for causal LM training
- Add empty file handling with clear error message
- Add tests for constructor validation and labels field
2026-01-08 11:35:00 +00:00
Daniel Han
84ae73789c Merge pull request #3863 from unslothai/fix/fbgemm-cutlass-errors-sm100
Fix FBGEMM/CUTLASS errors on SM100 (Blackwell) GPUs
2026-01-08 03:19:53 -08:00
pre-commit-ci[bot]
41b7fe0c67 [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2026-01-08 04:15:17 +00:00
danielhanchen
1f85f39e0a Fix FBGEMM/CUTLASS errors on SM100 (Blackwell) GPUs
This PR fixes the "Arch conditional MMA instruction used without targeting
appropriate compute capability. Aborting." errors that occur when using
FBGEMM on Blackwell GPUs (B200/B100, SM100).

Changes:
- Add stderr filters in import_fixes.py for CUTLASS/FBGEMM MMA errors
- Add warning filters for various deprecation messages
- Update check_fbgemm_gpu_version() to disable FBGEMM instead of raising
  an error when old versions are detected
- Update test_has_fbgemm() in fp8.py to catch broader CUTLASS/CUDA errors
  and gracefully fall back to Triton kernels
- Update loader_utils.py to disable FBGEMM instead of raising ValueError
  for old fbgemm_gpu versions

The key behavior change is that FBGEMM errors no longer crash the script.
Instead, FBGEMM is disabled and Triton kernels are used automatically.
This allows Unsloth to work on SM100 GPUs where CUTLASS SM90 kernels fail,
and also gracefully handles old FBGEMM versions.
2026-01-08 04:14:53 +00:00
Daniel Han
6b04af4f49 Merge pull request #3857 from Datta0/modelscope_stats
[ModelScope] Disable stats when modelscope is being used
2026-01-06 02:56:55 -08:00
pre-commit-ci[bot]
46d212c480 [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2026-01-06 10:00:17 +00:00
Datta Nimmaturi
1d84ba5287 Check env var explicitly
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
2026-01-06 15:30:06 +05:30