Load unsupported model types instead of raising KeyError in AutoLigerKernelForCausalLM - #1404
Open
nileshpatil6 wants to merge 4 commits into
Open
nileshpatil6 wants to merge 4 commits into
nileshpatil6 wants to merge 4 commits into
Conversation
…KernelForCausalLM
_apply_liger_kernel already degrades gracefully for a model type with no
patching function:
if model_type not in MODEL_TYPE_TO_APPLY_LIGER_FN.keys():
logger.info(f"There are currently no Liger kernels supported for model type: {model_type}.")
return
but auto_model.py then indexes the same dict unconditionally to strip the
kwargs that function consumed:
apply_fn = MODEL_TYPE_TO_APPLY_LIGER_FN[model_type]
so that graceful path is unreachable from the wrapper and
AutoLigerKernelForCausalLM.from_pretrained("gpt2") raises KeyError: 'gpt2'
instead of loading the model unpatched. The class docstring describes it
as a drop-in replacement that applies the kernel "if applicable", and the
README says the code is patched "if the model type is supported", so an
unsupported model is expected to load.
An unsupported model type consumed no kwargs, so all of them belong to
the underlying AutoModel call. This adds a small helper that returns the
kwargs unchanged in that case, used by both from_pretrained and
from_config.
Also fixes from_config returning None when model_type is falsy: it
logged "No Liger kernels will be applied" and then returned no model at
all. The redundant model_type reassignment on the following line shows
the block was meant to fall through.
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.
Summary
AutoLigerKernelForCausalLMraisesKeyErrorfor any model type without a Liger patching function, instead of loading the model unpatched.Details
_apply_liger_kernelalready handles this case deliberately:But
auto_model.pythen indexes the same dict unconditionally, to strip the kwargs that function consumed:so the graceful path above is unreachable from the wrapper. Both
from_pretrained(line 36) andfrom_config(line 55) do it.This contradicts the documented contract. The class docstring says it is "a drop-in replacement for AutoModelForCausalLM that applies the Liger Kernel to the model if applicable", and the README says "If the model type is supported, the modeling code will be automatically patched." Both read as: unsupported models still load.
The intended usage is a one-line swap in a training script:
which now hard-fails the moment that script is pointed at a model Liger does not patch. It also means each new model type in
transformersis a crash here until a patch function is added.Change
An unsupported model type consumed no kwargs, so all of them belong to the underlying
AutoModelcall. A small helper returns the kwargs unchanged in that case, and both classmethods use it.It also fixes
from_configreturningNonewhenmodel_typeis falsy: it logged "No Liger kernels will be applied" and then returned no model at all, so callers got anAttributeErroronNonedownstream. The redundantmodel_type = config.model_typeon the very next line shows the block was meant to fall through rather than bail.Testing
Three cases added to
test/transformers/test_auto_model.py, following the existing mock style. All are CPU-only.Without the change:
With it:
Also checked against a real config, no mocks:
Regression, comparing a clean checkout with the patched tree over
test_auto_model.py,test_monkey_patch.py,test_utils.pyandtest_trainer_integration.py:The delta is exactly the three new tests; the 8 skips are GPU-gated and identical in both runs.
ruff checkandruff format --checkboth pass.Hardware Type: CPU only. This change touches no kernel code, so I ran the CPU-runnable suites above rather than the full GPU matrix. Happy to have CI cover the rest.