BEMACallback crashes at on_train_begin with a PEFT (LoRA) model
Reproduction
from datasets import Dataset
from transformers import AutoModelForCausalLM, AutoTokenizer
from peft import LoraConfig, get_peft_model
from trl import SFTConfig, SFTTrainer
from trl.trainer.callbacks import BEMACallback
model_id = "trl-internal-testing/tiny-Qwen2ForCausalLM-2.5"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = get_peft_model(AutoModelForCausalLM.from_pretrained(model_id),
LoraConfig(target_modules=["q_proj", "v_proj"]))
dataset = Dataset.from_dict({"text": ["Hello world this is a test sentence."] * 8})
trainer = SFTTrainer(model=model, args=SFTConfig(output_dir="out", max_steps=2, report_to="none"),
train_dataset=dataset, processing_class=tokenizer,
callbacks=[BEMACallback(update_freq=1)])
trainer.train()
outputs:
Traceback (most recent call last):
...
File ".../trl/trainer/callbacks.py", line 705, in on_train_begin
self.running_model = type(model)(model.config).to(self.device)
TypeError: PeftModel.__init__() missing 1 required positional argument: 'peft_config'
BEMACallback.on_train_begin builds the shadow model with type(model)(model.config), which isn't a valid constructor for PeftModel (it takes the base model and the adapter config, not just a config). Fixing only the constructor surfaces a second failure: _update_bema_weights zips the trainable-only parameter list against running_model.parameters() (every parameter of the shadow model), which raises under strict=True once a model has frozen parameters — as any PEFT model does, since LoRA leaves the base weights frozen.
How did you hit this?
Found while auditing open issues/PRs for unclaimed, reproducible bugs, not from a production run. Verified by running the reproduction above on CPU with a real tiny model and peft.
System Info
- Platform: macOS-26.5.2-arm64
- Python version: 3.12
- PyTorch version: 2.14.1
- Transformers version: 5.18.0
- Datasets version: 5.1.0
- PEFT version: 0.21.2
- accelerator(s): cpu
- TRL version: main @ 5b365bf0
Checklist
- [x] I have checked that my issue isn't already filed (see open issues)
- [x] I have included my system information
- [x] Any code provided is minimal, complete, and reproducible
- [x] Any code provided is properly formatted in code blocks
- [x] Any traceback provided is complete
I have a fix ready (construct the shadow model from the base model + active adapter config instead of type(model)(model.config), and index running_model's parameters by name instead of position) plus a regression test. Could this be assigned to me so I can open the PR?
Thanks @sahil-mangla, this is already tracked: the PeftModel constructor crash in #7511, and the trainable-vs-all-parameters misalignment in #7480. Both threads already have your diagnosis and the fix direction you describe, so I'm closing this as a duplicate to keep the discussion in one place.