Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/source/ko/_toctree.yml
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,8 @@
title: ๐Ÿค— Transformers์— ๊ธฐ์—ฌํ•˜๋Š” ๋ฐฉ๋ฒ•
- local: add_new_model
title: ๐Ÿค— Transformers์— ์ƒˆ๋กœ์šด ๋ชจ๋ธ์„ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•
- local: auto_docstring
title: ๋ชจ๋ธ ๋ฌธ์„œํ™”
- local: add_new_pipeline
title: ์–ด๋–ป๊ฒŒ ๐Ÿค— Transformers์— ํŒŒ์ดํ”„๋ผ์ธ์„ ์ถ”๊ฐ€ํ•˜๋‚˜์š”?
- local: testing
Expand Down
280 changes: 280 additions & 0 deletions docs/source/ko/auto_docstring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,280 @@
<!--Copyright 2025 The HuggingFace Team. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.

โš ๏ธ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
rendered properly in your Markdown viewer.

-->

# ๋ชจ๋ธ ๋ฌธ์„œํ™”[[documenting-a-model]]

Transformers์˜ `@auto_docstring` ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋Š” ๋ชจ๋ธ ํด๋ž˜์Šค ๋ฐ ํ•ด๋‹น ๋ฉ”์†Œ๋“œ์— ์ผ๊ด€๋œ docstring์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ํ‘œ์ค€ ์ธ์ž ์„ค๋ช…์„ ์ž๋™์œผ๋กœ ํฌํ•จํ•˜๋ฉด์„œ๋„ ์ƒˆ๋กœ์šด ๋˜๋Š” ์‚ฌ์šฉ์ž ์ง€์ • ์ธ์ž๋ฅผ ์ถ”๊ฐ€ํ•˜๊ธฐ ์œ„ํ•œ ์žฌ์ •์˜๋ฅผ ํ—ˆ์šฉํ•˜์—ฌ ์ƒ์šฉ๊ตฌ ์ฝ”๋“œ๋ฅผ ์ค„์ž…๋‹ˆ๋‹ค. ํ‘œ์ค€ docstring์„ ์ˆ˜๋™์œผ๋กœ ์ถ”๊ฐ€ํ•  ํ•„์š” ์—†์ด ์ƒˆ ์ธ์ž ๋ฌธ์„œํ™”์—๋งŒ ์ง‘์ค‘ํ•  ์ˆ˜ ์žˆ์–ด [์ƒˆ ๋ชจ๋ธ ๊ธฐ์—ฌํ•˜๊ธฐ](./modular_transformers)๊ฐ€ ๋” ์‰ฌ์›Œ์ง‘๋‹ˆ๋‹ค.

์ด ๊ฐ€์ด๋“œ์—์„œ๋Š” `@auto_docstring` ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•๊ณผ ๊ทธ ์ž‘๋™ ๋ฐฉ์‹์„ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค.

## @auto_docstring[[autodocstring]]

๋ชจ๋ธ๋ง ํŒŒ์ผ(`modular_model.py` ๋˜๋Š” `modeling_model.py`)์—์„œ ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒƒ์œผ๋กœ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.

```python
from ...utils import auto_docstring
```

`@auto_docstring` ๋ฅผ ํด๋ž˜์Šค ํ˜น์€ ํ•จ์ˆ˜์— ์ ์šฉํ• ์ง€ ์„ ํƒํ•˜์—ฌ ์•„๋ž˜ ์˜ˆ์‹œ์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•˜์„ธ์š”.

<hfoptions id="type">
<hfoption id="classes">

`@auto_docstring`์„ ํด๋ž˜์Šค ์ •์˜ ๋ฐ”๋กœ ์œ„์— ๋ฐฐ์น˜ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋Š” `__init__` ๋ฉ”์†Œ๋“œ์˜ ์‹œ๊ทธ๋‹ˆ์ฒ˜์™€ docstring์—์„œ ๋งค๊ฐœ๋ณ€์ˆ˜ ์„ค๋ช…์„ ์ถ”์ถœํ•ฉ๋‹ˆ๋‹ค.

```python
from transformers.modeling_utils import PreTrainedModel
from ...utils import auto_docstring

@auto_docstring
class MyAwesomeModel(PreTrainedModel):
def __init__(self, config, custom_parameter: int = 10, another_custom_arg: str = "default"):
r"""
custom_parameter (`int`, *optional*, defaults to 10):
MyAwesomeModel์— ๋Œ€ํ•œ custom_parameter ์„ค๋ช…์ž…๋‹ˆ๋‹ค.
another_custom_arg (`str`, *optional*, defaults to "default"):
๋‹ค๋ฅธ ๊ณ ์œ  ์ธ์ž์— ๋Œ€ํ•œ ๋ฌธ์„œ์ž…๋‹ˆ๋‹ค.
"""
super().__init__(config)
self.custom_parameter = custom_parameter
self.another_custom_arg = another_custom_arg
# ... ... ๋‚˜๋จธ์ง€ ์ดˆ๊ธฐํ™” ์ฝ”๋“œ(init)

# ... ๊ธฐํƒ€ ๋ฉ”์†Œ๋“œ
```

๋ณด๋‹ค ์„ธ๋ฐ€ํ•œ ์ œ์–ด๋ฅผ ์œ„ํ•ด ์ธ์ž๋ฅผ `@auto_docstring` ์— ์ง์ ‘ ์ „๋‹ฌํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. `custom_intro` ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ธ์ž ๋ชฉ๋ก์— ๋Œ€ํ•œ ์„ค๋ช…์„, `custom_args` ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐœ๋ณ„ ์ธ์ž๋“ค์„ ์„ค๋ช…ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

```python
@auto_docstring(
custom_intro="""์ด ๋ชจ๋ธ์€ ํŠน์ • ์‹œ๋„ˆ์ง€ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
ํ‘œ์ค€ Transformer ์•„ํ‚คํ…์ฒ˜๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ณ ์œ ํ•œ ์ˆ˜์ • ์‚ฌํ•ญ์„ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค.""",
custom_args="""
custom_parameter (`type`, *optional*, defaults to `default_value`):
`args_doc.py`์— ์ •์˜๋˜์ง€ ์•Š์•˜๊ฑฐ๋‚˜ ์žฌ์ •์˜ํ•˜๋Š” ๊ฒฝ์šฐ custom_parameter์— ๋Œ€ํ•œ ๊ฐ„๊ฒฐํ•œ ์„ค๋ช…์ž…๋‹ˆ๋‹ค.
internal_helper_arg (`type`, *optional*, defaults to `default_value`):
`args_doc.py`์— ์ •์˜๋˜์ง€ ์•Š์•˜๊ฑฐ๋‚˜ ์žฌ์ •์˜ํ•˜๋Š” ๊ฒฝ์šฐ internal_helper_arg์— ๋Œ€ํ•œ ๊ฐ„๊ฒฐํ•œ ์„ค๋ช…์ž…๋‹ˆ๋‹ค.
"""
)
class MySpecialModel(PreTrainedModel):
def __init__(self, config: ConfigType, custom_parameter: "type" = "default_value", internal_helper_arg=None):
# ...
```

`custom_intro`๋งŒ ์‚ฌ์šฉํ•˜๊ณ  ์‚ฌ์šฉ์ž ์ง€์ • ์ธ์ž๋ฅผ ํด๋ž˜์Šค์— ์ง์ ‘ ์ •์˜ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

```python
@auto_docstring(
custom_intro="""์ด ๋ชจ๋ธ์€ ํŠน์ • ์‹œ๋„ˆ์ง€ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
ํ‘œ์ค€ Transformer ์•„ํ‚คํ…์ฒ˜๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ณ ์œ ํ•œ ์ˆ˜์ • ์‚ฌํ•ญ์„ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค.""",
)
class MySpecialModel(PreTrainedModel):
def __init__(self, config: ConfigType, custom_parameter: "type" = "default_value", internal_helper_arg=None):
r"""
custom_parameter (`type`, *optional*, defaults to `default_value`):
`args_doc.py`์— ์ •์˜๋˜์ง€ ์•Š์•˜๊ฑฐ๋‚˜ ์žฌ์ •์˜ํ•˜๋Š” ๊ฒฝ์šฐ custom_parameter์— ๋Œ€ํ•œ ๊ฐ„๊ฒฐํ•œ ์„ค๋ช…์ž…๋‹ˆ๋‹ค.
internal_helper_arg (`type`, *optional*, defaults to `default_value`):
`args_doc.py`์— ์ •์˜๋˜์ง€ ์•Š์•˜๊ฑฐ๋‚˜ ์žฌ์ •์˜ํ•˜๋Š” ๊ฒฝ์šฐ internal_helper_arg์— ๋Œ€ํ•œ ๊ฐ„๊ฒฐํ•œ ์„ค๋ช…์ž…๋‹ˆ๋‹ค.
"""
# ...
```

</hfoption>
<hfoption id="functions">

`@auto_docstring`์„ ๋ฉ”์†Œ๋“œ ์ •์˜ ๋ฐ”๋กœ ์œ„์— ๋ฐฐ์น˜ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋Š” ํ•จ์ˆ˜ ์‹œ๊ทธ๋‹ˆ์ฒ˜์—์„œ ๋งค๊ฐœ๋ณ€์ˆ˜ ์„ค๋ช…์„ ์ถ”์ถœํ•ฉ๋‹ˆ๋‹ค.

```python
@auto_docstring
def forward(
self,
input_ids: Optional[torch.Tensor] = None,
attention_mask: Optional[torch.Tensor] = None,
new_custom_argument: Optional[torch.Tensor] = None,
arg_documented_in_args_doc: Optional[torch.Tensor] = None,
# ... ๋‹ค๋ฅธ ์ธ์ž๋“ค
) -> Union[Tuple, ModelOutput]: # ๋ฐ˜ํ™˜ ๊ฐ’์— ๋Œ€ํ•œ ์„ค๋ช…์€ ModelOutput ํด๋ž˜์Šค docstring์—์„œ ์ž๋™์œผ๋กœ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค.
r"""
new_custom_argument (`torch.Tensor`, *optional*):
Description of this new custom argument and its expected shape or type.
"""
# ...
```

์ œ์–ด๋ฅผ ๊ฐ•ํ™”ํ•˜๊ธฐ ์œ„ํ•ด ์ธ์ž๋“ค์„ `@auto_docstring`์— ์ง์ ‘ ์ „๋‹ฌํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. `custom_intro` ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ์ธ์ž๋“ค์— ๋Œ€ํ•œ ์ „๋ฐ˜์ ์ธ ์„ค๋ช…์„ ์ž‘์„ฑํ•  ๋•Œ ์‚ฌ์šฉํ•˜๊ณ , `custom_args` ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ๊ฐ ์ธ์ž๋ณ„ ์„ค๋ช…์„ ์ž‘์„ฑํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

docstring์˜ `Returns` ๋ฐ `Examples` ๋ถ€๋ถ„๋„ ์ˆ˜๋™์œผ๋กœ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


```python
MODEL_COMMON_CUSTOM_ARGS = r"""
common_arg_1 (`torch.Tensor`, *optional*, defaults to `default_value`):
common_arg_1์— ๋Œ€ํ•œ ์„ค๋ช…
common_arg_2 (`torch.Tensor`, *optional*, defaults to `default_value`):
common_arg_2์— ๋Œ€ํ•œ ์„ค๋ช…
...
"""

class MyModel(PreTrainedModel):
# ...
@auto_docstring(
custom_intro="""
This is a custom introduction for the function.
"""
custom_args=MODEL_COMMON_CUSTOM_ARGS
)
def forward(
self,
input_ids: Optional[torch.Tensor] = None,
attention_mask: Optional[torch.Tensor] = None,
common_arg_1: Optional[torch.Tensor] = None,
common_arg_2: Optional[torch.Tensor] = None,
#...
function_specific_argument: Optional[torch.Tensor] = None,
# ... ๋‹ค๋ฅธ ์ธ์ž๋“ค
) -> torch.Tensor:
r"""
function_specific_argument (`torch.Tensor`, *optional*):
์ด ํ•จ์ˆ˜์— ํŠน์ •ํ•œ ์ธ์ž์— ๋Œ€ํ•œ ์„ค๋ช…์ž…๋‹ˆ๋‹ค.

Returns:
`torch.Tensor`: ์ผ๋ฐ˜์ ์ธ ํƒ€์ž…์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜์— ๋Œ€ํ•ด ์‚ฌ์šฉ์ž ์ง€์ • "Returns" ์„น์…˜์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Example:

(๊ธฐ๋ณธ ์˜ˆ์‹œ๋ฅผ ์‚ฌ์šฉ์ž ์ง€์ • ์˜ˆ์‹œ๋กœ ์žฌ์ •์˜ํ•˜๊ฑฐ๋‚˜, pipeline์ด ์—†๋Š” ๋ชจ๋ธ ํด๋ž˜์Šค์— ๋Œ€ํ•œ ์˜ˆ์‹œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒฝ์šฐ)

```python
...
```
"""
# ...
```

</hfoption>
</hfoptions>

## ์ธ์ž ๋ฌธ์„œํ™”[[documenting-arguments]]

๋‹ค์–‘ํ•œ ์œ ํ˜•์˜ ์ธ์ž๋ฅผ ๋ฌธ์„œํ™”ํ•˜๊ธฐ ์œ„ํ•œ ๊ทœ์น™์„ ์•„๋ž˜์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

- ํ‘œ์ค€ ์ธ์ž(`input_ids`, `attention_mask`, `pixel_values` ๋“ฑ)๋Š” `args_doc.py`์—์„œ ์ •์˜๋˜์–ด ์žˆ์œผ๋ฉฐ, ํ•ด๋‹น ํŒŒ์ผ์—์„œ ๋ถˆ๋Ÿฌ์™€ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ํ‘œ์ค€ ์ธ์ž์— ๋Œ€ํ•œ ๋‹จ์ผ ์ •๋ณด ์ถœ์ฒ˜์ด๋ฏ€๋กœ, ์ธ์ž์˜ ์„ค๋ช…๊ณผ ํ˜•ํƒœ๊ฐ€ `args_doc.py`์˜ ์ธ์ž์™€ ๋™์ผํ•œ ๊ฒฝ์šฐ์—๋Š” ๋กœ์ปฌ์—์„œ ์žฌ์ •์˜ํ•ด์„œ๋Š” ์•ˆ ๋ฉ๋‹ˆ๋‹ค.

ํ‘œ์ค€ ์ธ์ž๊ฐ€ ๋ชจ๋ธ์—์„œ ๋‹ค๋ฅด๊ฒŒ ๋™์ž‘ํ•˜๋Š” ๊ฒฝ์šฐ, `r""" """` ๋ธ”๋ก ๋‚ด์—์„œ ๋กœ์ปฌ๋กœ ์žฌ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋กœ์ปฌ ์ •์˜๋Š” ๋” ๋†’์€ ์šฐ์„ ์ˆœ์œ„๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, labels ์ธ์ž๋Š” ์ข…์ข… ๋ชจ๋ธ๋ณ„๋กœ ์‚ฌ์šฉ์ž ์ •์˜๋˜๋ฉฐ ์ผ๋ฐ˜์ ์œผ๋กœ ์žฌ์ •์˜๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.


- ์ƒˆ ์ธ์ž๋‚˜ ์‚ฌ์šฉ์ž ์ •์˜ ์ธ์ž๋Š” ํ•จ์ˆ˜ ์‹œ๊ทธ๋‹ˆ์ฒ˜ ๋’ค์˜ `r""" """` ๋ธ”๋ก์—, ํด๋ž˜์Šค์˜ ๊ฒฝ์šฐ `__init__` ๋ฉ”์†Œ๋“œ์˜ docstring์— ๋ฌธ์„œํ™”ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

```py
argument_name (`type`, *optional*, defaults to `X`):
์ธ์ž์— ๋Œ€ํ•œ ์„ค๋ช…์ž…๋‹ˆ๋‹ค.
Explain its purpose, expected shape/type if complex, and default behavior.
์—ฌ๋Ÿฌ ์ค„์— ๊ฑธ์ณ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
```

* `type`์€ ๋ฐฑํ‹ฑ์œผ๋กœ ๊ฐ์‹ธ์„ธ์š”.
* ์ธ์ž๊ฐ€ ํ•„์ˆ˜๊ฐ€ ์•„๋‹ˆ๊ฑฐ๋‚˜ ๊ธฐ๋ณธ๊ฐ’์ด ์žˆ๋Š” ๊ฒฝ์šฐ *optional*์„ ์ถ”๊ฐ€ํ•˜์„ธ์š”.
* ๊ธฐ๋ณธ๊ฐ’์ด ์žˆ๋Š” ๊ฒฝ์šฐ "defaults to X"๋ฅผ ์ถ”๊ฐ€ํ•˜์„ธ์š”. ๊ธฐ๋ณธ๊ฐ’์ด None์ธ ๊ฒฝ์šฐ "defaults to None"์„ ์ถ”๊ฐ€ํ•  ํ•„์š”๋Š” ์—†์Šต๋‹ˆ๋‹ค.

์ด๋Ÿฌํ•œ ์ธ์ž๋“ค์€ `custom_args` ์ธ์ž๋กœ `@auto_docstring`์— ์ „๋‹ฌํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ๋ชจ๋ธ๋ง ํŒŒ์ผ์˜ ์—ฌ๋Ÿฌ ์œ„์น˜์—์„œ ์ƒˆ๋กœ์šด ์ธ์ž๋“ค์˜ docstring ๋ธ”๋ก์ด ๋ฐ˜๋ณต๋˜๋Š” ๊ฒฝ์šฐ ํ•œ ๋ฒˆ๋งŒ ์ •์˜ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

```py
class MyModel(PreTrainedModel):
# ...
@auto_docstring(
custom_intro="""
์ด ํ•จ์ˆ˜์— ๋Œ€ํ•œ ์‚ฌ์šฉ์ž ์ •์˜ ๋„์ž…๋ถ€์ž…๋‹ˆ๋‹ค.
"""
custom_args=r"""
common_arg_1 (`torch.Tensor`, *optional*, defaults to `default_value`):
common_arg_1์— ๋Œ€ํ•œ ์„ค๋ช…
"""
)
```

## docstring ๊ฒ€์‚ฌ[[checking-the-docstrings]]

Transformers๋Š” Pull Request์—์„œ CI(continuous intergration, ์ง€์†์  ํ†ตํ•ฉ) ๊ฒ€์‚ฌ๋ฅผ ํŠธ๋ฆฌ๊ฑฐํ•  ๋•Œ docstring ์œ ํšจ์„ฑ์„ ํ™•์ธํ•˜๋Š” ์œ ํ‹ธ๋ฆฌํ‹ฐ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค. ์Šคํฌ๋ฆฝํŠธ๋Š” ๋‹ค์Œ ๊ธฐ์ค€์„ ๊ฒ€์‚ฌํ•ฉ๋‹ˆ๋‹ค.

* `@auto_docstring` ์ด ๊ด€๋ จ ๋ชจ๋“œ ํด๋ž˜์Šค ๋ฐ ๊ณต๊ฐœ ๋ฉ”์†Œ๋“œ์— ์ ์šฉ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.
* ์ธ์ž๊ฐ€ ์™„์ „ํ•˜๊ณ  ์ผ๊ด€์ ์ธ์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์„œํ™”๋œ ์ธ์ž๊ฐ€ ์‹œ๊ทธ๋‹ˆ์ฒ˜์— ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธํ•˜๊ณ , docstring์˜ ํƒ€์ž… ๋ฐ ๊ธฐ๋ณธ๊ฐ’์ด ์‹œ๊ทธ๋‹ˆ์ฒ˜์™€ ์ผ์น˜ํ•˜๋Š”์ง€ ๊ฒ€์ฆํ•ฉ๋‹ˆ๋‹ค. ์•Œ๋ ค์ง„ ํ‘œ์ค€ ์ธ์ž๊ฐ€ ์•„๋‹ˆ๊ฑฐ๋‚˜ ๋กœ์ปฌ ์„ค๋ช…์ด ์—†๋Š” ์ธ์ž๋Š” ํ”Œ๋ž˜๊ทธ๊ฐ€ ์ง€์ •๋ฉ๋‹ˆ๋‹ค.
* `<fill_type>` ๋ฐ `<fill_docstring>` ๊ฐ™์€ ์ž๋ฆฌ ํ‘œ์‹œ์ž(placeholder)๋ฅผ ์žŠ์ง€ ์•Š๊ณ  ์ฑ„์šฐ๋„๋ก ๋„์™€์ค๋‹ˆ๋‹ค.
* docstring์ด ์˜ˆ์ƒ๋˜๋Š” docstring ์Šคํƒ€์ผ์— ๋”ฐ๋ผ ํ˜•์‹์ด ์ง€์ •๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

์ด๋Ÿฌํ•œ ๊ฒ€์‚ฌ๋ฅผ ์ปค๋ฐ‹ํ•˜๊ธฐ ์ „์— ๋กœ์ปฌ์—์„œ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•˜์„ธ์š”.

```bash
make fix-copies
```

`make fix-copies` ๋Š” ๋‹ค๋ฅธ ์—ฌ๋Ÿฌ ๊ฒ€์‚ฌ๋„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ๊ฒ€์‚ฌ๊ฐ€ ํ•„์š”ํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ, docstring ๋ฐ ์ž๋™ docstring ๊ฒ€์‚ฌ๋งŒ ์ˆ˜ํ–‰ํ•˜๋ ค๋ฉด ์•„๋ž˜ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•˜์„ธ์š”.

```bash
python utils/check_docstrings.py # diff์— ํฌํ•จ๋œ ํŒŒ์ผ๋งŒ ํ™•์ธํ•˜๊ณ  ์ˆ˜์ •ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
# python utils/check_docstrings.py --fix_and_overwrite # diff์˜ ํŒŒ์ผ์„ ์ˆ˜์ •ํ•˜๊ณ  ๋ฎ์–ด์”๋‹ˆ๋‹ค.
# python utils/check_docstrings.py --fix_and_overwrite --check_all # ๋ชจ๋“  ํŒŒ์ผ์„ ์ˆ˜์ •ํ•˜๊ณ  ๋ฎ์–ด์”๋‹ˆ๋‹ค.
```

## modular_model.py ํŒŒ์ผ[[modularmodelpy-files]]

๋ชจ๋“ˆํ™”๋œ ํŒŒ์ผ(`modular_model.py`) ์ž‘์—… ์‹œ `@auto_docstring` ์ ์šฉ์— ๋Œ€ํ•œ ๋‹ค์Œ ์ง€์นจ์„ ๋”ฐ๋ฅด์„ธ์š”.

- ๋ชจ๋“ˆํ™”๋œ ํŒŒ์ผ์˜ ๋…๋ฆฝ ์‹คํ–‰ํ˜• ๋ชจ๋ธ์˜ ๊ฒฝ์šฐ, `modeling_model.py` ํŒŒ์ผ์—์„œ์™€ ๊ฐ™์ด `@auto_docstring`์„ ์ ์šฉํ•ฉ๋‹ˆ๋‹ค.
- ๋‹ค๋ฅธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ชจ๋ธ์„ ์ƒ์†ํ•˜๋Š” ๋ชจ๋ธ์˜ ๊ฒฝ์šฐ, `@auto_docstring`์€ ์ƒ์„ฑ๋œ ๋ชจ๋ธ๋ง ํŒŒ์ผ๋กœ ์ž๋™์œผ๋กœ ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค. ๋ชจ๋“ˆํ™”๋œ ํŒŒ์ผ์— `@auto_docstring`์„ ์ถ”๊ฐ€ํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.

`@auto_docstring` ๋™์ž‘์„ ์ˆ˜์ •ํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ, ๋ชจ๋“ˆํ™”๋œ ํŒŒ์ผ์— ์‚ฌ์šฉ์ž ์ง€์ • ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋ฅผ ์ ์šฉํ•ฉ๋‹ˆ๋‹ค. ์›๋ณธ ํ•จ์ˆ˜ ๋˜๋Š” ํด๋ž˜์Šค์— ์žˆ๋Š” **๋‹ค๋ฅธ ๋ชจ๋“  ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋ฅผ ๋ฐ˜๋“œ์‹œ ํฌํ•จ**ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

> [!WARNING]
> ๋ชจ๋“ˆํ™”๋œ ํŒŒ์ผ์—์„œ ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋ฅผ ์žฌ์ •์˜ํ•  ๋•Œ, ๋ถ€๋ชจ ๋ชจ๋ธ์˜ ํ•ด๋‹น ํ•จ์ˆ˜ ๋˜๋Š” ํด๋ž˜์Šค์— ์ ์šฉ๋œ **๋ชจ๋“ ** ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋ฅผ ํฌํ•จํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ผ๋ถ€ ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋งŒ ์žฌ์ •์˜ํ•˜๋ฉด ๋‹ค๋ฅธ ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋Š” ์ƒ์„ฑ๋œ ๋ชจ๋ธ๋ง ํŒŒ์ผ์— ํฌํ•จ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

## ์ž‘๋™ ๋ฐฉ์‹[[how-it-works]]

`@auto_docstring` ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋Š” ๋‹ค์Œ์„ ํ†ตํ•ด docstring์„ ์ž๋™์œผ๋กœ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

1. ๋ฐ์ฝ”๋ ˆ์ดํŠธ๋œ ํด๋ž˜์Šค์˜ `__init__` ๋ฉ”์†Œ๋“œ ๋˜๋Š” ๋ฐ์ฝ”๋ ˆ์ดํŠธ๋œ ํ•จ์ˆ˜์˜ ์‹œ๊ทธ๋‹ˆ์ฒ˜(์ธ์ž, ํƒ€์ž…, ๊ธฐ๋ณธ๊ฐ’)๋ฅผ ๊ฒ€์‚ฌํ•ฉ๋‹ˆ๋‹ค.
2. ์ผ๋ฐ˜์ ์ธ ์ธ์ž (`input_ids`, `attention_mask`, ๋“ฑ)์— ๋Œ€ํ•ด ๋ฏธ๋ฆฌ ์ •์˜๋œ docstring์„ [`ModelArgs`], [`ImageProcessorArgs`] ๋ฐ `args_doc.py` ํŒŒ์ผ๊ณผ ๊ฐ™์€ ๋‚ด๋ถ€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์†Œ์Šค์—์„œ ๊ฒ€์ƒ‰ํ•ฉ๋‹ˆ๋‹ค.
3. ์•„๋ž˜์™€ ๊ฐ™์ด ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜๋กœ ์ธ์ž ์„ค๋ช…์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

| ๋ฐฉ๋ฒ• | ์„ค๋ช… | ์‚ฌ์šฉ๋ฒ• |
|---|---|---|
| `r""" """` | ๋ฉ”์†Œ๋“œ ์‹œ๊ทธ๋‹ˆ์ฒ˜์— ์ง์ ‘ ๋˜๋Š” `__init__` docstring ๋‚ด์— ์‚ฌ์šฉ์ž ์ง€์ • docstring ๋‚ด์šฉ์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. | ์ƒˆ๋กœ์šด ์ธ์ž๋ฅผ ๋ฌธ์„œํ™”ํ•˜๊ฑฐ๋‚˜ ํ‘œ์ค€ ์„ค๋ช…์„ ์žฌ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. |
| `custom_args` | `@auto_docstring`์— ํŠน์ • ์ธ์ž์— ๋Œ€ํ•œ ์‚ฌ์šฉ์ž ์ง€์ • Docstring์„ ์ง์ ‘ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. | ๋ชจ๋ธ๋ง ํŒŒ์ผ์˜ ์—ฌ๋Ÿฌ ์œ„์น˜์—์„œ ๋ฐ˜๋ณต๋˜๋Š” ๊ฒฝ์šฐ ์ƒˆ๋กœ์šด ์ธ์ž์— ๋Œ€ํ•œ docstring์„ ํ•œ ๋ฒˆ๋งŒ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. |

4. ํด๋ž˜์Šค ๋ฐ ํ•จ์ˆ˜ ์„ค๋ช…์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. `ModelForCausalLM`๊ณผ ๊ฐ™์€ ํ‘œ์ค€ ๋ช…๋ช… ํŒจํ„ด์„ ๊ฐ€์ง„ ๋ชจ๋ธ ํด๋ž˜์Šค ๋˜๋Š” ํŒŒ์ดํ”„๋ผ์ธ์— ์†ํ•˜๋Š” ๊ฒฝ์šฐ, `@auto_docstring`์€ `args_doc.py`์˜ `ClassDocstring`์„ ์‚ฌ์šฉํ•˜์—ฌ ์ ์ ˆํ•œ ์„ค๋ช…์„ ์ž๋™์œผ๋กœ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

๋˜ํ•œ, `@auto_docstring`์€ ํด๋ž˜์Šค ๋˜๋Š” ํ•จ์ˆ˜๋ฅผ ์„ค๋ช…ํ•˜๋Š” `custom_intro` ์ธ์ž๋ฅผ ํ—ˆ์šฉํ•ฉ๋‹ˆ๋‹ค.

5. ํ…œํ”Œ๋ฆฟ ์‹œ์Šคํ…œ์„ ์‚ฌ์šฉํ•ด ์ด๋ฏธ ์ •์˜๋œ docstring์— Transformers [auto_modules](https://github.com/huggingface/transformers/tree/main/src/transformers/models/auto)์˜ ์ •๋ณด(`{{processor_class}}` , `{{config_class}}` ๋“ฑ)๋ฅผ ๋™์ ์œผ๋กœ ์‚ฝ์ž…ํ•ฉ๋‹ˆ๋‹ค.

6. ๋ชจ๋ธ์˜ ์ž‘์—… ๋˜๋Š” pipeline ํ˜ธํ™˜์„ฑ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ์ ์ ˆํ•œ ์‚ฌ์šฉ ์˜ˆ์‹œ๋ฅผ ์ฐพ์Šต๋‹ˆ๋‹ค. ๋ชจ๋ธ์˜ ๊ตฌ์„ฑ ํด๋ž˜์Šค์—์„œ ์ฒดํฌํฌ์ธํŠธ ์ •๋ณด๋ฅผ ์ถ”์ถœํ•˜์—ฌ ์‹ค์ œ ๋ชจ๋ธ ์‹๋ณ„์ž๋ฅผ ํฌํ•จํ•œ ๊ตฌ์ฒด์ ์ธ ์˜ˆ์‹œ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

7. docstring์— ๋ฐ˜ํ™˜ ๊ฐ’์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. `forward`์™€ ๊ฐ™์€ ๋ฉ”์†Œ๋“œ์˜ ๊ฒฝ์šฐ, ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋Š” ๋ฉ”์†Œ๋“œ์˜ ๋ฐ˜ํ™˜ ํƒ€์ž… ์ฃผ์„์„ ๊ธฐ๋ฐ˜์œผ๋กœ docstring์— `Returns` ํ•„๋“œ๋ฅผ ์ž๋™์œผ๋กœ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, ๋ฉ”์†Œ๋“œ๊ฐ€ [`~transformers.utils.ModelOutput`] ์„œ๋ธŒํด๋ž˜์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒฝ์šฐ, `@auto_docstring`์€ ํด๋ž˜์Šค์˜ Docstring์—์„œ ํ•„๋“œ ์„ค๋ช…์„ ์ถ”์ถœํ•˜์—ฌ ํฌ๊ด„์ ์ธ ๋ฐ˜ํ™˜ ๊ฐ’ ์„ค๋ช…์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ํ•จ์ˆ˜์˜ docstring์—์„œ ์‚ฌ์šฉ์ž ์ง€์ • `Returns` ํ•„๋“œ๋ฅผ ์ˆ˜๋™์œผ๋กœ ์ง€์ •ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

8. unpack ์—ฐ์‚ฐ์ž๋กœ ํƒ€์ž…์ด ์ง€์ •๋œ kwargs๋ฅผ ํ’€์–ด์„œ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. ํŠน์ • ๋ฉ”์†Œ๋“œ(`UNROLL_KWARGS_METHODS`์— ์ •์˜๋จ) ๋˜๋Š” ํด๋ž˜์Šค(`UNROLL_KWARGS_CLASSES`์— ์ •์˜๋จ)์˜ ๊ฒฝ์šฐ, ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋Š” `Unpack[KwargsTypedDict]`๋กœ ํƒ€์ž…์ด ์ง€์ •๋œ `**kwargs` ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. `TypedDict`์—์„œ ๋ฌธ์„œ๋ฅผ ์ถ”์ถœํ•˜์—ฌ ๊ฐ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ํ•จ์ˆ˜์˜ docstring์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

ํ˜„์žฌ [`FastImageProcessorKwargs`]๋งŒ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค.

## ๋ชจ๋ฒ” ์‚ฌ๋ก€[[best-practices]]

Transformers์˜ ์ผ๊ด€๋˜๊ณ  ์œ ์ตํ•œ ๋ฌธ์„œ๋ฅผ ์œ ์ง€ํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋˜๋„๋ก ๋‹ค์Œ ๋ชจ๋ฒ” ์‚ฌ๋ก€๋ฅผ ๋”ฐ๋ฅด์„ธ์š”!

* ์ƒˆ๋กœ์šด PyTorch ๋ชจ๋ธ ํด๋ž˜์Šค ([`PreTrainedModel`] ์„œ๋ธŒํด๋ž˜์Šค) ๋ฐ `forward` ๋˜๋Š” `get_text_features`์™€ ๊ฐ™์€ ๊ธฐ๋ณธ ๋ฉ”์†Œ๋“œ์— `@auto_docstring`์„ ์‚ฌ์šฉํ•˜์„ธ์š”.
* ํด๋ž˜์Šค์˜ ๊ฒฝ์šฐ, `@auto_docstring`์€ `__init__` ๋ฉ”์†Œ๋“œ์˜ docstring์—์„œ ํŒŒ๋ผ๋ฏธํ„ฐ ์„ค๋ช…์„ ๊ฐ€์ ธ์˜จ๋‹ค๋Š” ์ ์„ ๊ธฐ์–ตํ•˜์„ธ์š”.
* ํ‘œ์ค€ docstring์„ ์šฐ์„  ์‚ฌ์šฉํ•˜๊ณ , ๋ชจ๋ธ์˜ ๋™์ž‘์ด ๋‹ค๋ฅธ ๊ฒฝ์šฐ์—๋งŒ ๊ณตํ†ต ์ธ์ž๋ฅผ ์žฌ์ •์˜ํ•˜์„ธ์š”.
* ์ƒˆ๋กœ์šด ์ธ์ž๋‚˜ ์‚ฌ์šฉ์ž ์ง€์ • ์ธ์ž๋Š” ๋ช…ํ™•ํ•˜๊ฒŒ ๋ฌธ์„œํ™”ํ•˜์„ธ์š”.
* ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ ์ปค๋ฐ‹ํ•˜๊ธฐ ์ „์— `check_docstrings` ๋ฅผ ๋กœ์ปฌ์—์„œ ๋ฐ˜๋ณต์ ์œผ๋กœ ์‹คํ–‰ํ•˜์—ฌ ํ™•์ธํ•˜์„ธ์š”.