Commit graph

4,672 commits

Author SHA1 Message Date
Roland Tannous
ed18f9b9dd Flatten GGUF subdirs in export and fix metadata lookup in scanner 2026-02-26 11:35:04 +04:00
Roland Tannous
90f012a444 Write export metadata for GGUF exports to fix Unknown base model 2026-02-26 11:24:32 +04:00
Roland Tannous
2ce63f09c4 Add gguf to toLoraSummary inline type 2026-02-26 10:59:39 +04:00
Roland Tannous
1b822a943c Revert "Add gguf to frontend export_type unions"
This reverts commit 782af39949.
2026-02-26 10:56:26 +04:00
Roland Tannous
782af39949 Add gguf to frontend export_type unions 2026-02-26 10:54:49 +04:00
Roland Tannous
609ae4809a Merge pull request #229 from unslothai/feat/dataset-list-sorting
Feat: Sort and filter dataset search results by model type relevance
2026-02-26 10:40:02 +04:00
Roland Tannous
ea9b22000e Merge pull request #245 from unslothai/fix/datetime-utc-python39-compatibility
fix: replace datetime.UTC with timezone.utc for Python 3.9+ compatibility
2026-02-26 10:37:01 +04:00
imagineer99
852dff564e feat: added datasets of size 5M and 10M to pretraining size category 2026-02-26 06:32:45 +00:00
imagineer99
6e535ed0eb fix: filter OCR datasets from non-vision hub results 2026-02-26 06:27:52 +00:00
Roland Tannous
e0127c0d4c Merge branch 'nightly' 2026-02-26 10:25:41 +04:00
Roland Tannous
808f4655c9 Merge pull request #243 from unslothai/fix/setup-unbound-variable
resolved unbound variable error
2026-02-26 10:18:21 +04:00
samit
04aee4a4c6 updated to make the delete preset work 2026-02-25 21:36:50 -08:00
imagineer99
1c55e2fbaa fix: remove dataset metadata badges from HF dataset dropdowns 2026-02-26 03:57:55 +00:00
samit
7bc752d5f9 passed checkpoint as a parameter to presets 2026-02-25 18:08:02 -08:00
Wasim Yousef Said
2f84bbf0e0 Merge pull request #264 from unslothai/fix/attachment-tsx-type-error
fix(attachment): replace never exhaustive check to fix Colab TS2322 b…
2026-02-25 17:19:44 -08:00
Leo Borcherding
62c6fd9f46 fix(attachment): replace never exhaustive check to fix Colab TS2322 build error
`attachment.type` resolves to `string & {}` via @assistant-ui/store@0.1.6's
generic type chain when installed through npm (package-lock.json), breaking
the `const _exhaustiveCheck: never = type` exhaustive check pattern.

Replace with a direct throw that compiles cleanly across library versions
while preserving identical runtime behaviour.

Fixes #263

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-25 17:56:02 -06:00
Daniel Han
3fc6cfd32d Fix transformers v5 RoPE inv_freq corruption and generate() BatchEncoding compat (#4112)
* Fix transformers v5 RoPE inv_freq corruption during model loading

Transformers v5 initializes models on the meta device, then
_move_missing_keys_from_meta_to_device() replaces all non-persistent
buffers with torch.empty_like() (uninitialized memory). Vanilla
transformers restores inv_freq via _init_weights() checking for
original_inv_freq, but Unsloth's LlamaRotaryEmbedding subclasses
lack this attribute, so inv_freq stays corrupted with garbage values.

This caused 5-11x higher training loss on transformers v5 for all
models using Unsloth's rope (Llama 3.x, Qwen3, Mistral, TinyLlama,
Granite). Models using native transformers rope (Gemma, Phi-4,
Falcon-H1) were unaffected.

The fix recomputes inv_freq from the stored base/dim after model
loading, applies model-specific scaling via _apply_inv_freq_scaling(),
and rebuilds cos/sin caches. Also handles LongRopeRotaryEmbedding
(Phi-3.5 style short/long inv_freq). Guarded by transformers >= 5.0.0
so it is a no-op on v4.

Tested on: Llama 3.1 8B, Llama 3.2 3B, Qwen3 14B, Qwen3 4B, Phi-4,
TinyLlama, Mistral 7B, Gemma2 2B, Falcon-H1 -- all v5 losses now
match v4 baselines to < 0.004 absolute difference.

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

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

* Unpack BatchEncoding in generate() for v4/v5 backwards compatibility

Old notebooks pass the full tokenizer output as input_ids:

    inputs = tokenizer(..., return_tensors="pt").to("cuda")
    model.generate(input_ids=inputs, ...)

This worked on transformers v4 because generate() internally
extracted the tensor. Transformers v5 calls .shape on input_ids
directly, which crashes since BatchEncoding has no .shape attribute.

Fix: in unsloth_fast_generate(), detect when input_ids is a dict-like
object (BatchEncoding) and unpack its contents into separate kwargs
before forwarding to the underlying generate(). This makes both old
and new notebook patterns work on both v4 and v5.

* Remove redundant seen_ids dedup in _fix_rope_inv_freq

named_modules() already deduplicates with remove_duplicate=True (default).
Also clarify that native v5 rotary classes (Gemma3 etc.) have original_inv_freq
which transformers v5's _init_weights() uses to restore inv_freq, so they do
not need this fix.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2026-02-25 08:18:45 -08:00
DoubleMathew
6d0f864369 Fix/pr 3699 leftpad prefill main (#4100)
* Fix left-padding masks and positions in batched decode/prefill

* Fix batched generation with left padding

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

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

* Fix attention mask handling, padding_idx zeroing, and Mistral batched generation

1. attention_dispatch.py: Fall back from flash/xformers to SDPA when an
   attention_mask is present, since flash attention only supports causal
   masking via flag and cannot consume arbitrary padding masks.

2. gemma2.py: Apply attention_mask during decode inference for bsz > 1.
   Guard against boolean SWA/GA flags with isinstance check. Slice mask
   to match K/V length when sliding window is active. Remove dead
   commented-out SDPA branch (SDPA does not support softcapping).

3. granite.py: Apply attention_mask during decode inference for bsz > 1.
   Remove dead commented-out SDPA branch and misleading comment.

4. mistral.py: Fix 2D-to-4D padding mask conversion -- convert 0/1 mask
   to additive format (0 for keep, -inf for mask) before combining with
   the causal mask. Force SDPA backend when attention_mask is present.

5. llama.py: Skip zeroing embed_tokens.weight[padding_idx] when the
   embedding is weight-tied to lm_head, since zeroing the shared weight
   forces logit(pad) = 0 which is higher than real token logits in models
   like Gemma, causing the decoder to emit pad tokens as gibberish. Also
   add eos != pad guard, clean up unused _seq_length variable, and fix
   get_max_cache_shape handling.

6. vision.py: Same padding_idx fix as llama.py for the vision model
   loading path.

Tested on gemma-2b-it, gemma-2-2b-it, Llama-3.2-1B, Mistral-7B-v0.3,
Qwen2.5-0.5B, Qwen3-0.6B with flash-attn 2.8.3 active. All outputs
coherent, zero crashes, zero resize warnings.

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

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

* Inference path optimizations: eliminate per-layer GPU-CPU sync, cache inspect.signature, add Granite SDPA split

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

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

* More inference path optimizations across model files

- gemma: hoist rotary_seq_len computation to model level (eliminates N
  per-layer GPU-CPU syncs from position_ids.max().item()), pre-convert
  attention mask to bool once for all layers, use scalar float multiply
  instead of torch.tensor allocation for embedding scaling
- gemma2: use in-place tanh_() for softcap attention, use scalar float
  multiply for embedding scaling
- granite: pre-convert attention mask to bool once for all layers
- cohere: use in-place neg_() for rotary embedding (consistent with
  all other model files)
- falcon_h1: use in-place mul_() for key_multiplier scaling
- llama: use in-place tanh_() for logit softcapping

* Revert scalar multiply for Gemma/Gemma2 embedding scaling

The original torch.tensor(..., dtype=hidden_states.dtype) is intentional:
sqrt(3072) rounds to 55.5 in bfloat16 vs 55.4256 in float32. A plain
scalar multiply may compute at higher precision internally, producing
different results. Restore the explicit dtype-cast tensor to match the
training path in LlamaModel_fast_forward.

* Fix hardcoded cuda:0 device strings and add Cohere .eq(0) bool mask

Replace 15 hardcoded "cuda:0" with f"{DEVICE_TYPE_TORCH}:0" across
gemma.py, gemma2.py, cohere.py, and falcon_h1.py to support multi-GPU
and non-CUDA devices (XPU, etc.). Add .eq(0) bool mask pre-conversion
in CohereModel_fast_forward_inference for batched inference consistency
with llama.py, granite.py, and gemma.py.

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

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

* Disable flex_attention for Mllama (Llama 3.2 Vision)

Mllama's _update_causal_mask uses the deprecated make_flex_block_causal_mask
which creates a BlockMask with Q_LEN=KV_LEN=total_seq_len. During decode
with KV cache, q_len=1 but the block_mask still has Q_LEN=total_seq_len,
causing a ValueError. This is an upstream transformers issue -- newer models
use flex_attention_mask from masking_utils which handles decode correctly
via cache_position, but mllama has not been updated yet.

Add mllama to the exclusion list in prefer_flex_attn_if_supported alongside
gpt_oss so it falls back to sdpa, which works correctly for both training
and inference.

* Fix off-by-one in sliding window K/V slicing for gemma2, qwen3, falcon_h1, cohere

The old formula `slicing_tokens = 1 - sliding_window` uses negative indexing
that keeps `sliding_window - 1` tokens instead of `sliding_window`. For example
with sliding_window=32 and kv_seq_len=100, `1-32 = -31` keeps indices 69..99
(31 tokens) instead of the correct 68..99 (32 tokens).

Replace with `start = kv_seq_len - sliding_window` to match the fix already
applied in llama.py and the canonical definition in transformers masking_utils
(sliding_window_overlay: kv_idx > q_idx - W, which keeps exactly W tokens).

Also add attention_mask slicing after K/V trim in qwen3, falcon_h1, and cohere
to prevent mask/K dimension mismatch during batched SDPA inference, matching
the pattern already used in llama.py.

Currently only gemma2 (sliding_window=4096) is actively affected. The other
three models have sliding_window=None in their configs so the code path is
not triggered, but this keeps it correct for any future models that set it.

* Fix Gemma2 softcapping order: apply mask after softcap, not before

The attention mask must be applied AFTER logit softcapping, not before.
Both the Google DeepMind reference implementation (google-deepmind/gemma,
gm/nn/_modules.py lines 254-277) and transformers' eager_attention_forward
(gemma2/modeling_gemma2.py lines 187-193) use this order:

  1. logits = Q @ K^T * scale
  2. logits = tanh(logits / softcap) * softcap   # softcap first
  3. logits = logits + mask                       # mask after
  4. probs  = softmax(logits)

The PR had the mask addition before softcapping, which causes tanh to
clamp the -inf mask values to -softcap instead of preserving them as -inf
for softmax. While the practical impact is small (masked positions get
~1e-23 probability instead of exact zero), this should match upstream.

* Clarify GQA condition precedence and remove stale comments

Add explicit parentheses to grouped query attention conditions in
llama.py, qwen3.py, granite.py to make operator precedence clear.
The expression `bsz == 1 or not X and Y` relies on Python binding
`not` > `and` > `or` which is correct but easy to misread.

Remove dead commented-out code (`# else: # Knn, Vnn = Knn, Vnn`)
and stale mask comments (`# if attention_mask ...`) from the bsz==1
fast path in llama, qwen3, cohere, falcon_h1, gemma2 inference
functions. These were leftover from the pre-batched-inference
structure and no longer apply.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Daniel Han <danielhanchen@gmail.com>
2026-02-25 07:21:04 -08:00
Daniel Han
9b51b14b2b Support Python 3.14 in package metadata (#4113) 2026-02-25 07:17:16 -08:00
Roland Tannous
c21cf2ffcf Add GGUF tag for exported models in chat page selector 2026-02-25 19:01:47 +04:00
Roland Tannous
bfb1403032 Relocate GGUF exports into exports/ directory 2026-02-25 18:54:39 +04:00
Datta Nimmaturi
3f9e03ff1b Allow fp8 for non fast inference (#3904)
* Allow fp8 for non fast inference

* Extensive fp8 alow and quantizer patch

* Clean up commented-out code, duplicate import, and revert unnecessary Version() changes

- Delete commented-out FP8 fast_inference guard in FastModel (loader.py)
  instead of leaving it commented -- matches FastLanguageModel which was
  properly deleted
- Delete commented-out fast_inference guard in loader_utils.py
- Remove duplicate `from transformers import GenerationConfig, CompileConfig`
  in vision.py (line 112 already imports both plus AutoConfig)
- Revert Version(trl.__version__) back to Version(trl) in trainer.py --
  trainer.py imports Version from unsloth_zoo.utils which already handles
  module objects

---------

Co-authored-by: Daniel Han <danielhanchen@gmail.com>
2026-02-25 06:52:18 -08:00
Daniel Han
00fe9a40c0 Add resilience to TRL internal API reclassification (#4111)
* Add resilience to TRL internal API reclassification

TRL is moving toward v1.0 and will reclassify several
currently-importable symbols as internal with no stability
guarantees. This adds try/except cascading imports with local
fallbacks so Unsloth keeps working regardless of whether TRL
removes, moves, or restructures these symbols.

Changes:
- rl.py: Add try/except cascade for unwrap_model_for_generation
  with local contextmanager fallback. Wire sanitize_logprob from
  RL_REPLACEMENTS into the compiled trainer template (same pipeline
  as selective_log_softmax and other global functions). Add import
  math and import logging to the template header.
- rl_replacements.py: Remove inline import of sanitize_logprob
  from trl.scripts.vllm_serve in the regex replacement. The
  function is now a module-level global in the compiled file.
- tokenizer_utils.py: Wrap dynamic exec import with per-item
  fallback so a single removed symbol does not break the entire
  bulk import.

Depends on unslothai/unsloth-zoo#516.

Tested across all TRL versions from 0.22.2 through 0.29.0.dev0
(git main). Training losses and grad norms are bit-identical
to unpatched runs.

* [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-02-25 06:34:21 -08:00
Irfan Ali
30fac638ad fix: correct gpt-oss Ollama generation prompt and add quantization wa… (#4087)
* Warn when save_pretrained_gguf overrides quantization to MXFP4 for GPT-OSS

GPT-OSS only supports MXFP4 format. If the user passes a different
quantization_method, log a warning via logger.warning_once before
overriding. Pass quantization_method=None to suppress the warning.

* [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-02-25 04:39:16 -08:00
Roland Tannous
a8b5b7ed58 Fix GGUF models missing from chat page model search
GGUF was in the global EXCLUDED_TAGS set which filtered it from all
consumers of useHfModelSearch, including the chat page. Move GGUF
exclusion to an opt-in excludeGguf option so only training and
onboarding pages filter out GGUF models.
2026-02-25 16:21:08 +04:00
Roland Tannous
f92e4a3e1b Merge pull request #261 from unslothai/feat/gguf-llama-cpp-inference
Add GGUF model inference via llama-server with quantization variant selection
2026-02-25 16:07:39 +04:00
Roland Tannous
01082b84e5 Merge branch 'nightly' into feat/gguf-llama-cpp-inference 2026-02-25 16:06:03 +04:00
Roland Tannous
a1e064b1c4 Remove UNSLOTH_ENABLE_LOGGING from export pipeline 2026-02-25 16:00:24 +04:00
Roland Tannous
a7fe8a388c Filter GGUF models from training page model selectors
GGUF models can't be fine-tuned, so hide them from the training/studio
page while keeping them available for inference on the chat page.

- Add "gguf" to EXCLUDED_TAGS in HF model search hook
- Filter local models with .gguf extension or -GGUF in ID
2026-02-25 15:47:45 +04:00
samit
bbe208ea38 reduced broad padding 2026-02-25 03:44:05 -08:00
samit
0eab635666 added space to show model/dataset name 2026-02-25 03:40:44 -08:00
Roland Tannous
299ce77467 added vision.py patch for vision processor from PR#260 2026-02-25 11:39:17 +00:00
Roland Tannous
cfaa2f2074 Merge pull request #249 from unslothai/fix/section-card-corner-bleed
Fix: Clip section card overflow to prevent background bleed
2026-02-25 15:27:46 +04:00
Roland Tannous
cb3e4f2c26 Merge pull request #259 from unslothai/feat/dataset-subsets-split
Feat/dataset subsets split
2026-02-25 15:27:12 +04:00
Roland Tannous
96217b5056 Merge pull request #246 from unslothai/fix/dataset-custom-mapping-heuristic
adding custom mapping according to the chat templates
2026-02-25 15:26:36 +04:00
Roland Tannous
d2fe02ff04 Merge pull request #260 from unslothai/fix/fix-vision-processor-unsloth-bug
fix: correct vision.py patch path to unsloth/models/vision.py + add V…
2026-02-25 15:21:01 +04:00
Daniel Han
78963ca19c Fix Nemotron-H and Nemotron-VL model support (#4105)
* Fix Nemotron-H and Nemotron-VL model support

- Add Mamba kernel precision settings for Nemotron-H hybrid models
- Fix VL model auto_model selection for models that only register
  AutoModelForCausalLM in their auto_map
- Skip quantization of out_proj for Nemotron-H Mamba layers

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

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

* Simplify VLM auto_model selection logic

Reduce three branches to two since the first and third both assign
AutoModelForVision2Seq. The simplified condition checks whether the
auto_map exclusively registers AutoModelForCausalLM without the VLM
class, and defaults to AutoModelForVision2Seq otherwise.

* [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-02-25 03:14:12 -08:00
Shine1i
db11f1a601 style(studio): align card heights and restore dataset advanced section placement 2026-02-25 12:11:06 +01:00
Roland Tannous
40719c4a6f fix: correct vision.py patch path to unsloth/models/vision.py + add VLM processor diagnostic 2026-02-25 11:06:22 +00:00
Roland Tannous
6f0b7bc38a fix: use raw github URL for vision.py patch + add VLM processor diagnostic logging 2026-02-25 10:29:05 +00:00
Shine1i
cb57d48e7e chore: add tour label next to navbar tour icon 2026-02-25 11:23:43 +01:00
Manan17
6e8e70c987 fixing the chatml None error 2026-02-25 10:23:13 +00:00
Shine1i
122311a6b1 fix recipe output path, remove tracked root datasets 2026-02-25 11:19:10 +01:00
Wasim Yousef Said
c67da8f349 Merge pull request #257 from unslothai/feature/chat-model-switch-warning
feat: chat model switching toast and add image detection logic
2026-02-25 01:58:56 -08:00
Shine1i
44d6abb36c feat: chat model switching toast and add image detection logic 2026-02-25 10:55:53 +01:00
Wasim Yousef Said
a793660960 Merge pull request #254 from unslothai/feature/theme-fix
feat: fix markdown rendering, UI adjustments
2026-02-25 00:48:09 -08:00
Shine1i
8732f3befb feat: fix markdown rendering, UI adjustments 2026-02-25 09:46:13 +01:00
samit
a199e3f682 rebase with nightly 2026-02-25 00:36:25 -08:00
Wasim Yousef Said
468ec99489 Merge pull request #253 from unslothai/feature/theme-fix
feat: fix dark mode support and refine UI assets
2026-02-25 00:27:43 -08:00
Shine1i
adcbf78553 feat: fix dark mode support and refine UI assets 2026-02-25 09:23:05 +01:00