TransformersModel.load_adapter: attach a PEFT adapter after construction - #714
Open
JadenFiotto-Kaufman wants to merge 1 commit into
Open
JadenFiotto-Kaufman wants to merge 1 commit into
JadenFiotto-Kaufman wants to merge 1 commit into
Conversation
The post-hoc form of the `peft=` constructor kwarg. Before dispatch it grafts only the adapter's config onto the meta module, so the tree gains the adapter modules and remote paths match, and the real weights arrive with the base's at dispatch; safetensors cannot load onto meta, which is what made the fallthrough `PreTrainedModel.load_adapter` fail there (#555). After dispatch it goes through the same swap the remote env path uses, so calling again with another id swaps adapters and `None` removes the current one, with the envoy tree rebuilt around the new structure each time. Mechanically, the swap logic moves out of `_remoteable_set_env` into `_swap_adapter`, with the `rebind` closure promoted to `_rebind`; the env path delegates to them unchanged. Our `load_adapter` shadows transformers' method of the same name deliberately: reached by fallthrough, that one mutates the module structure without the envoy tree noticing. An adapter attached this way ships with remote requests automatically, since `_remoteable_get_env` reads `self.peft`. Requested by @Butanium in #555. Co-Authored-By: Claude Fable 5 (1M context) <noreply@anthropic.com>
JadenFiotto-Kaufman
force-pushed
the
feat/load-adapter
branch
from
September 5, 2026 04:49
9f2669b to
844cd09
Compare
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.
The post-hoc form of the
peft=constructor kwarg, for #555.Before dispatch, only the adapter's config is read: the meta module gains the adapter's architecture, so module paths (and remote requests) match the adapted model, and the real weights arrive with the base's at dispatch. This is what made the fallthrough
PreTrainedModel.load_adapterfail there — safetensors cannot load onto meta.After dispatch, it goes through the same swap the remote env path uses: call again with another id to swap adapters, or
Noneto remove the current one, with the envoy tree rebuilt around the new structure each time.Mechanically: the swap logic moves out of
_remoteable_set_envinto_swap_adapter, itsrebindclosure becomes_rebind, and the env path delegates to them unchanged. Ourload_adapterdeliberately shadows transformers' method of the same name, which mutates the module structure without the envoy tree noticing. Adapters attached this way ship with remote requests automatically, since_remoteable_get_envreadsself.peft.Four new tests in
TestPeftagainst the local LoRA fixture; full suite green.Closes #555
🤖 Generated with Claude Code