Skip to content

DOC Fix broken docstring examples in peft_model and tuner models - #3657

Open
blipbyte wants to merge 1 commit into
huggingface:mainfrom
blipbyte:doc-fix-broken-docstring-examples
Open

DOC Fix broken docstring examples in peft_model and tuner models#3657
blipbyte wants to merge 1 commit into
huggingface:mainfrom
blipbyte:doc-fix-broken-docstring-examples

Conversation

@blipbyte

@blipbyte blipbyte commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

Fixes #3644. Approved by @BenjaminBossan in
#3644 (comment).

Docstrings only. No runtime code is touched.

peft_model.py — four examples that cannot run

Four of the six PeftModelFor* examples raise TypeError when run as
written, because they pass config fields that were removed in 2023:

  • postprocess_past_key_value_function, removed in a7dd0347 (2023-02-08),
    appears in the PeftModelForSequenceClassification, PeftModelForCausalLM
    and PeftModelForTokenClassification examples.
  • enable_lora, removed in c21afbe8 (2023-03-28), appears in the
    PeftModelForSeq2SeqLM example.
TypeError: PrefixTuningConfig.__init__() got an unexpected keyword argument 'postprocess_past_key_value_function'
TypeError: LoraConfig.__init__() got an unexpected keyword argument 'enable_lora'

Both fields were deleted rather than renamed, so there is no replacement key
and the lines are simply removed. The remaining keys still describe the
configuration each example demonstrates.

All six classes are pulled into the API reference via [[autodoc]], so these
are the examples a reader copies off the docs site.

PeftModelForTokenClassification had a second problem behind the first: it
imported AutoModelForSequenceClassification but called
AutoModelForTokenClassification, so it would still have failed with a
NameError once the config was valid. The import now matches the class.

lora, hira, beft — examples that mislead

These three construct the tuner class directly:

>>> lora_model = LoraModel(model, config, "default")

That silently writes a base-model checkpoint instead of an adapter: BaseTuner.__getattr__ forwards to the wrapped module, so save_pretrained resolves to PreTrainedModel.save_pretrained rather than failing. Switched to get_peft_model, matching the form you asked for in the #3254 review for ia3 and adalora; these three were never swept.

Also removed the unused PeftModel import from the lora and hira k-bit
examples.

beft additionally passed peft_type="Beft", which BeftConfig.__post_init__
overwrites with PeftType.BEFT. It was the only example where the value was
also wrong — the enum is "BEFT", not "Beft", and the mismatch is silent.
The same redundant peft_type= appears in prompt_tuning, p_tuning,
ia3, adalora and prefix_tuning, but there the string matches the enum,
so I left them out of scope. Happy to remove those too if you'd prefer.

Testing

Nothing in CI executes these examples — make test runs pytest tests/, and
no doctest collection touches src/. That is why they sat broken since 2023.
So I ran them directly.

Before: 4 of 6 fail with the TypeErrors above. After: all six build their
config, and five run end to end. The sixth, PeftModelForCausalLM, needs
gpt2-large, which is not in my local cache.

The three tuner examples were run as edited too. Each now returns a PeftModel, so save_pretrained writes an adapter. The direct form resolves save_pretrained to PreTrainedModel.save_pretrained and writes a base-model checkpoint instead — no error either way.

Also run: make quality (ruff, format, doc-builder, doc coverage 131/131) and
pytest tests/test_config.py tests/test_decoder_models.py — 7064 passed,
2718 skipped.

Per the issue discussion, no test that executes the examples was added —
@BenjaminBossan said it wasn't needed.

AI assistance: I used Claude Code to help audit and draft this. I reviewed
every changed line, ran every command above myself, and can defend the change
end-to-end.

Four of the six `PeftModelFor*` docstring examples in `peft_model.py`
raise `TypeError` when run as written, because they pass config fields
removed in 2023:

- `postprocess_past_key_value_function`, removed in a7dd034
  (2023-02-08), in the SequenceClassification, CausalLM and
  TokenClassification examples.
- `enable_lora`, removed in c21afbe (2023-03-28), in the Seq2SeqLM
  example.

Both fields were deleted rather than renamed, so the lines are removed
with no replacement key. All six classes are pulled into the API
reference via `[[autodoc]]`, so these are the examples a reader copies
off the docs site.

`PeftModelForTokenClassification` had a second problem behind the
first: it imported `AutoModelForSequenceClassification` but called
`AutoModelForTokenClassification`. The import now matches the class.

The lora, hira and beft examples constructed the tuner class directly
(`LoraModel(model, config, "default")`), which returns an object with
no `save_pretrained` — a reader who copies it cannot save their
adapter. Switched to `get_peft_model`, matching the form used in ia3
and adalora since huggingface#3254. Also dropped the unused `PeftModel` import
from the lora and hira k-bit examples, and `peft_type="Beft"` from
beft, which `BeftConfig.__post_init__` silently overwrites and whose
value did not match the enum (`PeftType.BEFT` is "BEFT").

No runtime behavior change; all edits inside docstrings.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Docstring examples in peft_model.py: four of six PeftModelFor* examples fail on removed config fields

1 participant