Skip to content

Commit 541bed2

Browse files
authored
Improve @auto_docstring doc and rename args_doc.py to auto_docstring.py (#39439)
* rename `args_doc.py` to `auto_docstring.py` and improve doc * modifs after review
1 parent de0dd31 commit 541bed2

File tree

4 files changed

+180
-30
lines changed

4 files changed

+180
-30
lines changed

docs/source/en/auto_docstring.md

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ Arguments can also be passed directly to `@auto_docstring` for more control. Use
6464
It builds upon the standard Transformer architecture with unique modifications.""",
6565
custom_args="""
6666
custom_parameter (`type`, *optional*, defaults to `default_value`):
67-
A concise description for custom_parameter if not defined or overriding the description in `args_doc.py`.
67+
A concise description for custom_parameter if not defined or overriding the description in `auto_docstring.py`.
6868
internal_helper_arg (`type`, *optional*, defaults to `default_value`):
69-
A concise description for internal_helper_arg if not defined or overriding the description in `args_doc.py`.
69+
A concise description for internal_helper_arg if not defined or overriding the description in `auto_docstring.py`.
7070
"""
7171
)
7272
class MySpecialModel(PreTrainedModel):
@@ -85,13 +85,40 @@ class MySpecialModel(PreTrainedModel):
8585
def __init__(self, config: ConfigType, custom_parameter: "type" = "default_value", internal_helper_arg=None):
8686
r"""
8787
custom_parameter (`type`, *optional*, defaults to `default_value`):
88-
A concise description for custom_parameter if not defined or overriding the description in `args_doc.py`.
88+
A concise description for custom_parameter if not defined or overriding the description in `auto_docstring.py`.
8989
internal_helper_arg (`type`, *optional*, defaults to `default_value`):
90-
A concise description for internal_helper_arg if not defined or overriding the description in `args_doc.py`.
90+
A concise description for internal_helper_arg if not defined or overriding the description in `auto_docstring.py`.
9191
"""
9292
# ...
9393
```
9494

95+
You should also use the `@auto_docstring` decorator for classes that inherit from [`~utils.ModelOutput`].
96+
97+
```python
98+
@dataclass
99+
@auto_docstring(
100+
custom_intro="""
101+
Custom model outputs with additional fields.
102+
"""
103+
)
104+
class MyModelOutput(ImageClassifierOutput):
105+
r"""
106+
loss (`torch.FloatTensor`, *optional*):
107+
The loss of the model.
108+
custom_field (`torch.FloatTensor` of shape `(batch_size, hidden_size)`, *optional*):
109+
A custom output field specific to this model.
110+
"""
111+
112+
# Standard fields like hidden_states, logits, attentions etc. can be automatically documented if the description is the same as the standard arguments.
113+
# However, given that the loss docstring is often different per model, you should document it in the docstring above.
114+
loss: Optional[torch.FloatTensor] = None
115+
logits: Optional[torch.FloatTensor] = None
116+
hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None
117+
attentions: Optional[tuple[torch.FloatTensor, ...]] = None
118+
# Custom fields need to be documented in the docstring above
119+
custom_field: Optional[torch.FloatTensor] = None
120+
```
121+
95122
</hfoption>
96123
<hfoption id="functions">
97124

@@ -171,7 +198,7 @@ class MyModel(PreTrainedModel):
171198

172199
There are some rules for documenting different types of arguments and they're listed below.
173200

174-
- Standard arguments (`input_ids`, `attention_mask`, `pixel_values`, etc.) are defined and retrieved from `args_doc.py`. It is the single source of truth for standard arguments and should not be redefined locally if an argument's description and shape is the same as an argument in `args_doc.py`.
201+
- Standard arguments (`input_ids`, `attention_mask`, `pixel_values`, etc.) are defined and retrieved from `auto_docstring.py`. It is the single source of truth for standard arguments and should not be redefined locally if an argument's description and shape is the same as an argument in `auto_docstring.py`.
175202

176203
If a standard argument behaves differently in your model, then you can override it locally in a `r""" """` block. This local definition has a higher priority. For example, the `labels` argument is often customized per model and typically requires overriding.
177204

@@ -245,15 +272,15 @@ When working with modular files (`modular_model.py`), follow the guidelines belo
245272
The `@auto_docstring` decorator automatically generates docstrings by:
246273

247274
1. Inspecting the signature (arguments, types, defaults) of the decorated class' `__init__` method or the decorated function.
248-
2. Retrieving the predefined docstrings for common arguments (`input_ids`, `attention_mask`, etc.) from internal library sources like [`ModelArgs`], [`ImageProcessorArgs`], and the `args_doc.py` file.
275+
2. Retrieving the predefined docstrings for common arguments (`input_ids`, `attention_mask`, etc.) from internal library sources like [`ModelArgs`], [`ImageProcessorArgs`], and the `auto_docstring.py` file.
249276
3. Adding argument descriptions in one of two ways as shown below.
250277

251278
| method | description | usage |
252279
|---|---|---|
253280
| `r""" """` | add custom docstring content directly to a method signature or within the `__init__` docstring | document new arguments or override standard descriptions |
254281
| `custom_args` | add custom docstrings for specific arguments directly in `@auto_docstring` | define docstring for new arguments once if they're repeated in multiple places in the modeling file |
255282

256-
4. Adding class and function descriptions. For model classes with standard naming patterns, like `ModelForCausalLM`, or if it belongs to a pipeline, `@auto_docstring` automatically generates the appropriate descriptions with `ClassDocstring` from `args_doc.py`.
283+
4. Adding class and function descriptions. For model classes with standard naming patterns, like `ModelForCausalLM`, or if it belongs to a pipeline, `@auto_docstring` automatically generates the appropriate descriptions with `ClassDocstring` from `auto_docstring.py`.
257284

258285
`@auto_docstring` also accepts the `custom_intro` argument to describe a class or function.
259286

src/transformers/utils/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from packaging import version
2222

2323
from .. import __version__
24-
from .args_doc import (
24+
from .auto_docstring import (
2525
ClassAttrs,
2626
ClassDocstring,
2727
ImageProcessorArgs,

src/transformers/utils/args_doc.py renamed to src/transformers/utils/auto_docstring.py

Lines changed: 143 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,7 +1271,7 @@ def _get_model_info(func, parent_class):
12711271
else:
12721272
config_class = "ModelConfig"
12731273
print(
1274-
f"🚨 Config not found for {model_name_lowercase}. You can manually add it to HARDCODED_CONFIG_FOR_MODELS in utils/args_doc.py"
1274+
f"🚨 Config not found for {model_name_lowercase}. You can manually add it to HARDCODED_CONFIG_FOR_MODELS in utils/auto_docstring.py"
12751275
)
12761276

12771277
return model_name_lowercase, class_name, config_class
@@ -1893,27 +1893,150 @@ def auto_class_docstring(cls, custom_intro=None, custom_args=None, checkpoint=No
18931893

18941894

18951895
def auto_docstring(obj=None, *, custom_intro=None, custom_args=None, checkpoint=None):
1896-
"""
1897-
Automatically generates docstrings for classes and methods in the Transformers library.
1898-
1899-
This decorator can be used in the following forms:
1900-
@auto_docstring
1901-
def my_function(...):
1902-
...
1903-
or
1904-
@auto_docstring()
1905-
def my_function(...):
1906-
...
1907-
or
1908-
@auto_docstring(custom_intro="Custom intro", ...)
1909-
def my_function(...):
1910-
...
1896+
r"""
1897+
Automatically generates comprehensive docstrings for model classes and methods in the Transformers library.
1898+
1899+
This decorator reduces boilerplate by automatically including standard argument descriptions while allowing
1900+
overrides to add new or custom arguments. It inspects function signatures, retrieves predefined docstrings
1901+
for common arguments (like `input_ids`, `attention_mask`, etc.), and generates complete documentation
1902+
including examples and return value descriptions.
1903+
1904+
For complete documentation and examples, read this [guide](https://huggingface.co/docs/transformers/auto_docstring).
1905+
1906+
Examples of usage:
1907+
1908+
Basic usage (no parameters):
1909+
```python
1910+
@auto_docstring
1911+
class MyAwesomeModel(PreTrainedModel):
1912+
def __init__(self, config, custom_parameter: int = 10):
1913+
r'''
1914+
custom_parameter (`int`, *optional*, defaults to 10):
1915+
Description of the custom parameter for MyAwesomeModel.
1916+
'''
1917+
super().__init__(config)
1918+
self.custom_parameter = custom_parameter
1919+
```
1920+
1921+
Using `custom_intro` with a class:
1922+
```python
1923+
@auto_docstring(
1924+
custom_intro="This model implements a novel attention mechanism for improved performance."
1925+
)
1926+
class MySpecialModel(PreTrainedModel):
1927+
def __init__(self, config, attention_type: str = "standard"):
1928+
r'''
1929+
attention_type (`str`, *optional*, defaults to "standard"):
1930+
Type of attention mechanism to use.
1931+
'''
1932+
super().__init__(config)
1933+
```
1934+
1935+
Using `custom_intro` with a method, and specify custom arguments and example directly in the docstring:
1936+
```python
1937+
@auto_docstring(
1938+
custom_intro="Performs forward pass with enhanced attention computation."
1939+
)
1940+
def forward(
1941+
self,
1942+
input_ids: Optional[torch.Tensor] = None,
1943+
attention_mask: Optional[torch.Tensor] = None,
1944+
):
1945+
r'''
1946+
custom_parameter (`int`, *optional*, defaults to 10):
1947+
Description of the custom parameter for MyAwesomeModel.
1948+
1949+
Example:
1950+
1951+
```python
1952+
>>> model = MyAwesomeModel(config)
1953+
>>> model.forward(input_ids=torch.tensor([1, 2, 3]), attention_mask=torch.tensor([1, 1, 1]))
1954+
```
1955+
'''
1956+
```
1957+
1958+
Using `custom_args` to define reusable arguments:
1959+
```python
1960+
VISION_ARGS = r'''
1961+
pixel_values (`torch.FloatTensor`, *optional*):
1962+
Pixel values of the input images.
1963+
image_features (`torch.FloatTensor`, *optional*):
1964+
Pre-computed image features for efficient processing.
1965+
'''
1966+
1967+
@auto_docstring(custom_args=VISION_ARGS)
1968+
def encode_images(self, pixel_values=None, image_features=None):
1969+
# ... method implementation
1970+
```
1971+
1972+
Combining `custom_intro` and `custom_args`:
1973+
```python
1974+
MULTIMODAL_ARGS = r'''
1975+
vision_features (`torch.FloatTensor`, *optional*):
1976+
Pre-extracted vision features from the vision encoder.
1977+
fusion_strategy (`str`, *optional*, defaults to "concat"):
1978+
Strategy for fusing text and vision modalities.
1979+
'''
1980+
1981+
@auto_docstring(
1982+
custom_intro="Processes multimodal inputs combining text and vision.",
1983+
custom_args=MULTIMODAL_ARGS
1984+
)
1985+
def forward(
1986+
self,
1987+
input_ids,
1988+
attention_mask=None,
1989+
vision_features=None,
1990+
fusion_strategy="concat"
1991+
):
1992+
# ... multimodal processing
1993+
```
1994+
1995+
Using with ModelOutput classes:
1996+
```python
1997+
@dataclass
1998+
@auto_docstring(
1999+
custom_intro="Custom model outputs with additional fields."
2000+
)
2001+
class MyModelOutput(ImageClassifierOutput):
2002+
r'''
2003+
loss (`torch.FloatTensor`, *optional*):
2004+
The loss of the model.
2005+
custom_field (`torch.FloatTensor` of shape `(batch_size, hidden_size)`, *optional*):
2006+
A custom output field specific to this model.
2007+
'''
2008+
2009+
# Standard fields like hidden_states, logits, attentions etc. can be automatically documented
2010+
# However, given that the loss docstring is often different per model, you should document it above
2011+
loss: Optional[torch.FloatTensor] = None
2012+
logits: Optional[torch.FloatTensor] = None
2013+
hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None
2014+
attentions: Optional[tuple[torch.FloatTensor, ...]] = None
2015+
custom_field: Optional[torch.FloatTensor] = None
2016+
```
19112017
19122018
Args:
1913-
custom_intro (str, optional): Custom introduction text to add to the docstring. This will replace the default
1914-
introduction text generated by the decorator before the Args section.
1915-
checkpoint (str, optional): Checkpoint name to use in the docstring. This should be automatically inferred from the
1916-
model configuration class, but can be overridden if needed.
2019+
custom_intro (`str`, *optional*):
2020+
Custom introduction text to add to the docstring. This replaces the default
2021+
introduction text generated by the decorator before the Args section. Use this to describe what
2022+
makes your model or method special.
2023+
custom_args (`str`, *optional*):
2024+
Custom argument documentation in docstring format. This allows you to define
2025+
argument descriptions once and reuse them across multiple methods. The format should follow the
2026+
standard docstring convention: `arg_name (`type`, *optional*, defaults to `value`): Description.`
2027+
checkpoint (`str`, *optional*):
2028+
Checkpoint name to use in examples within the docstring. This is typically
2029+
automatically inferred from the model configuration class, but can be overridden if needed for
2030+
custom examples.
2031+
2032+
Note:
2033+
- Standard arguments (`input_ids`, `attention_mask`, `pixel_values`, etc.) are automatically documented
2034+
from predefined descriptions and should not be redefined unless their behavior differs in your model.
2035+
- New or custom arguments should be documented in the method's docstring using the `r''' '''` block
2036+
or passed via the `custom_args` parameter.
2037+
- For model classes, the decorator derives parameter descriptions from the `__init__` method's signature
2038+
and docstring.
2039+
- Return value documentation is automatically generated for methods that return ModelOutput subclasses.
19172040
"""
19182041

19192042
def auto_docstring_decorator(obj):

utils/check_docstrings.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
from git import Repo
5050

5151
from transformers.utils import direct_transformers_import
52-
from transformers.utils.args_doc import (
52+
from transformers.utils.auto_docstring import (
5353
ImageProcessorArgs,
5454
ModelArgs,
5555
ModelOutputArgs,
@@ -1487,7 +1487,7 @@ def check_auto_docstrings(overwrite: bool = False, check_all: bool = False):
14871487
if docstring_args_ro_remove_warnings:
14881488
if not overwrite:
14891489
print(
1490-
"Some docstrings are redundant with the ones in `args_doc.py` and will be removed. Run `make fix-copies` or `python utils/check_docstrings.py --fix_and_overwrite` to remove the redundant docstrings."
1490+
"Some docstrings are redundant with the ones in `auto_docstring.py` and will be removed. Run `make fix-copies` or `python utils/check_docstrings.py --fix_and_overwrite` to remove the redundant docstrings."
14911491
)
14921492
print(f"🚨 Redundant docstring for the following arguments in {candidate_file}:")
14931493
for warning in docstring_args_ro_remove_warnings:

0 commit comments

Comments
 (0)