You are a contributor-focused assistant for this repo. Prioritize small, reviewable changes and help users succeed with CI.
New Contributors: See CONTRIBUTING.md for complete setup and workflow details.
Commands (Run Early)
- Style/format:
make style - Repo checks:
make check-repo - Auto-fix repo consistency:
make fix-repo - Core tests:
make test - Examples tests:
make test-examples
Environment Setup If the environment is not yet set up:
- Requires Python 3.10+ but 3.10 recommended as the minimal supported Python
- Install dev dependencies:
uv pip install -e ".[dev]"orpip install -e ".[dev]"ifuvis unavailable - Verify install with
make check-repo
Quick Start
- Prefer the smallest change that fixes the issue.
- Use
rgto locate files; avoid broad file reads. - If you touched copied code, modular files, docs TOCs, or docstrings, run
make fix-repo. - Otherwise, run
make stylebefore you finish.
Example Flows
- Small code change: edit ->
make style-> targetedpytest ... - Modular/copies change: edit source/modular ->
make fix-repo-> targetedpytest ... - Docs change: edit ->
make fix-repo-> (optional) doc build
Repo Map
src/transformers: core library code.src/transformers/models: per-model implementations.tests: core tests and model tests.docs/source/en: documentation sources (MDX-like Markdown).
Boundaries
- Always: keep diffs minimal, follow existing patterns, prefer targeted tests.
- Ask first: adding dependencies, modifying CI, changing public APIs, large refactors.
- Never: edit generated modeling files when a
modular_<name>.pyexists. - Dependencies: see CONTRIBUTING.md#do-you-want-to-add-documentation for adding new dependencies (ask maintainers first).
Tech Stack
- Python package with PyTorch focus; tests run with
pytest, formatting viaruff. - Docs built with
doc-builder(seedocs/README.md).
Copies and Modular Models
- If a file has
# Copied from ..., change the source and runmake fix-repoto propagate. - If a model has
modular_<name>.py, do not edit generatedmodeling_*.pyfiles directly. Edit modular and runmake fix-repo(orpython utils/modular_model_converter.py <name>). - Files with
This file was automatically generated fromin second line are generated. - Protobuf files, ending with
_pb2.pyare also generated.
Adding Models the Modular Way Modular Transformers lets you add models by inheriting from existing models, significantly reducing code:
- pick a CamelCase unique name for model and use it everywhere. All modules should be prefixed with that name following PEP8 convention.
- Create a
modular_<name>.pyfile insrc/transformers/models/<name>/ - Import and inherit from similar models (e.g.,
class MyModelConfig(LlamaConfig)) - Only define what changes: new attributes, modified layers, different behavior
- Run
python utils/modular_model_converter.py <name>to generate standard files - Run
make check-repoto verify the conversion and check for issues - The linter automatically unravels inheritance, flattens dependencies, and copies required functions
Key patterns:
super().__init__(...)unravels parent body; adddel self.attributeafter to remove unwanted attributesclass MyClass(ParentClass): passcopies parent definition exactly with renamed references- Import functions directly (e.g.,
from ..llama.modeling_llama import eager_attention_forward) - Docstring variables set to
Nonereuse parent docstrings automatically - Override methods only when logic changes; decorators inherit unless you specify new ones
See docs/source/en/modular_transformers.md for full guide with Olmo2 example.
Testing (smallest relevant set)
- Targeted model tests:
pytest tests/models/<name>/test_modeling_<name>.py - Tokenizers/processors:
pytest tests/models/<name>/test_tokenization_<name>.pyortest_processing_<name>.py - Examples:
make test-examples - Full suite:
make test - Slow tests:
RUN_SLOW=1 pytest ...
CI Test Selection
- CI runs only tests impacted by the diff.
- Reproduce locally:
python utils/tests_fetcher.py - Run list:
python -m pytest -n 8 --dist=loadfile -rA -s $(cat test_list.txt)
Docs Notes
- Docs use MDX-like syntax; keep formatting minimal and consistent.
- Adding a doc requires updating the Toc tree with
make check-repo. - Local docs build uses
doc-builderand dev extras indocs/README.md.
Makefile Truth
make styleruns ruff format/check plus repo formatters.make check-reporuns CI-style consistency checks.make fix-repoauto-fixes copies, modular conversions, doc TOCs, doctest lists, and docstrings.
Git Workflow
- Keep PRs small; avoid unrelated refactors and sweeping reformatting.
- When uncertain, prefer targeted changes and ask before widening scope.
- See CONTRIBUTING.md#create-a-pull-request for full PR workflow.
Troubleshooting
make stylefailures: read output carefully—most are auto-fixed, some need manual fixes. Common: import ordering, docstring formatting.make check-repofailures: check specific error message. If it mentions copies/modular/TOC/docstrings, runmake fix-repo.- Test failures: check test output for assertion errors. Verify changes didn't break existing behavior.
- Import errors after install: ensure you ran
pip install -e ".[dev]"with-eflag for editable mode. - Slow tests timing out: use
RUN_SLOW=1environment variable only when needed.
Common Errors
- "import failed, unprotected imports": you added an import without proper guards. Check
__init__.pypatterns. - "modular conversion failed": modular file has syntax errors. Run
python utils/modular_model_converter.py <name>to debug. - "copies check failed": edited a
# Copied from ...file directly. Edit the source file instead and runmake fix-repo. - "doc TOC check failed": added a doc without updating
docs/source/en/_toctree.yml. Add your file there.
Output Example
- Good: "Edited
src/transformers/models/bert/modeling_bert.py, ranmake style, ranpytest tests/models/bert/test_modeling_bert.py."