Repository navigation
Run Unsloth's patched forwards only on Unsloth-loaded models - #15101
Merged
Merged
Conversation
A plain transformers model loaded after FastLanguageModel in the same process picked up the class-level fast forwards and failed in forward or generate (missing max_seq_length, xformers dispatch, dtype mismatch). The patched methods now dispatch on a per-module marker set at load and fall back to the method the class held before the first patch; the Unsloth rotary module answers a stock (x, position_ids) call with per-position cos/sin.
Member
Author
|
@codex review |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Didn't find any major issues. Bravo. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
[skip ci]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
After
FastLanguageModel.from_pretrainedloads a Llama-family checkpoint, a plaintransformersmodel loaded in the same process fails on forward or generate. This happens when someone evaluates a saved model with stock transformers in the same notebook:AttributeError: 'LlamaForCausalLM' object has no attribute 'max_seq_length'ongenerate(bf16 plain load)RuntimeError: expected mat1 and mat2 to have the same dtypeon forward (fp32 plain load)NotImplementedError: No operator found for memory_efficient_attention_forward(small head dims)FastLlamaModel.pre_patch(unsloth/models/llama.py) assigns the fast forwards to the transformers classes themselves (LlamaAttention,LlamaDecoderLayer,LlamaModel,LlamaForCausalLM.forward,prepare_inputs_for_generation,_reorder_cache). Every later instance of those classes, including ones built by plain transformers, therefore runs Unsloth's paths without the state they assume (max_seq_length, which is set only on models Unsloth loaded, plus the dtype and attention setup).Change
pre_patch, each patched method that has a recorded original is wrapped in a dispatcher. The originals come from the existing_unsloth_pre_patch_originalsrecord. A module marked_unsloth_fast_pathruns the Unsloth method. Any other instance gets the method the class held before the first patch, or the inherited one when the class did not define it.functools.wrapskeeps the signature and qualname, so Trainer column filtering and the loss-utils lookups are unchanged.__init__is not wrapped.FastLlamaModel.from_pretrainedmarks every module of the model it loaded (beforepost_patch), andget_peft_modelmarks it again at the point where it already setsmax_seq_lengthon every module.LlamaRotaryEmbeddingis replaced. That module now answers the stockrotary_emb(x, position_ids)call with per-position cos/sin inx.dtype. Unsloth's own code only reads the cache throughget_cached, so its paths are unchanged.restore_transformers_familystill restores the originals for FastModel.Tests
tests/test_plain_transformers_after_fast_language_model.pyrestore_transformers_familystill restores.AutoModelForCausalLMload in the same process. Forward and generate match a fresh-process plain run. A secondFastLanguageModelload gives bitwise equal logits.max_seq_lengthAttributeError). After the fix both pass.The non-zero fp32 diffs come from the RoPE table being built on another device, not from a different path.
test_fast_language_model_after_fast_model,test_plain_load_after_unsloth_rope,test_ppo_trainer_patches,test_decode_gqa_in_place,test_fused_ce_logit_scale,test_longrope_short_long_boundary,test_mistral_sliding_window_mask,test_rope_cache_current_device,test_rms_rope_compile,utils/test_rope_scaling_drift,utils/test_packingall pass, excepttest_rms_rope_compile::test_tiny_llama_causal_lm_compiles_fullgraph. That one fails identically on main (an xformersLowerTriangularMaskgraph break under fullgraph). The trace on head passes through the new dispatcher without a break.Not covered
FastLanguageModel.get_peft_modelis marked there and keeps the fast path, as today. Calling a fast forward on such a model beforeget_peft_modelnow takes the stock path.