Skip to content

Commit

Permalink
🏛️ Improve DPO configuration documentation structure (huggingface#2561)
Browse files Browse the repository at this point in the history
* better structure dpo config

* fix tests

* fix regex

* add contributing guidelines
  • Loading branch information
qgallouedec authored Jan 12, 2025
1 parent edabe0a commit 751fb1d
Show file tree
Hide file tree
Showing 4 changed files with 335 additions and 221 deletions.
101 changes: 100 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pip install -e .[dev]

## Fixing outstanding issues

If you notice an issue with the existing code and have a fix in mind, feel free to [start contributing](#create-a-pull-request) and open a Pull Request!
If you notice an issue with the existing code and have a fix in mind, feel free to [start contributing](#submitting-a-pull-request-pr) and open a Pull Request!

## Submitting a bug-related issue or feature request

Expand Down Expand Up @@ -257,6 +257,105 @@ That's how `make test` is implemented (without the `pip install` line)!
You can specify a smaller set of tests to test only the feature
you're working on.
### Writing documentation
High-quality documentation is crucial for maintaining a project that is easy to use, understand, and extend. When adding new features, ensure they are thoroughly documented to maintain consistency and clarity throughout the project.
To illustrate what good documentation looks like, here’s an example of a well-documented function:
````python
def replicate_str(string: str, n: int, sep: str = " ") -> str:
r"""
Replicate a string `n` times with a separator.
Args:
string (`str`):
String to replicate.
n (`int`):
Number of times to replicate the string.
sep (`str`, *optional*, defaults to `" "`):
Separator to use between each replication.
Returns:
`str`: The replicated string.
Examples:
```python
>>> replicate_str("hello", 3)
"hello hello hello"
>>> replicate_str("hello", 3, sep=", ")
"hello, hello, hello"
```
"""
return sep.join([string] * n)
````
* **Line Wrapping:** Applied a consistent line wrap at column 120 to improve readability.
* **Definite Articles:** Removed definite articles where possible to streamline language. (Eg: Changed "The string to replicate" to "String to replicate")
* **Type Annotations:**
* Always include type definitions, indicating if a parameter is optional and specifying the default value.
* Note that `Optional` means that the value can be `None`, and `*optional*` means that it is not required for the user to pass a value.
E.g., for arguments that can't be `None` and aren't required:
```python
foo (`int`, *optional*, defaults to `4`):
```
For arguments that can be `None` and are required:
```python
foo (`Optional[int]`):
```
for arguments that can be `None` and aren't required:

```python
foo (`Optional[int]`, *optional*, defaults to `None`):
```

* **String Defaults:**
* Ensured that default string values are wrapped in double quotes:

```python
defaults to `"foo"`
```

* **Dictionary Typing:**
* Replaced generic `dict` type hints with more explicit `dict[str, Any]` to clarify expected key-value pairs.
* **Default Value Formatting:**
* Consistently surrounded default values with backticks for improved formatting:

```python
defaults to `4`
```

* **Sub-sectioning:** When the number of arguments is large, consider breaking them into sub-sections for better readability.

```python
def calculate_statistics(data: list[float], precision: int = 2, include_variance: bool = False) -> dict[str, float]:
r"""
Calculates basic statistics for a given dataset.
Args:
> Data inputs
data (`list[float]`):
A list of numerical values to analyze.
> Configuration parameters
precision (`int`, *optional*, defaults to `2`):
Number of decimal places to round the results.
include_variance (`bool`, *optional*, defaults to `False`):
Whether to include the variance of the dataset in the results.
Returns:
`dict[str, float]`:
A dictionary containing calculated statistics such as mean, median, and optionally variance.
"""
...
```

### Deprecation and Backward Compatibility

Our approach to deprecation and backward compatibility is flexible and based on the feature’s usage and impact. Each deprecation is carefully evaluated, aiming to balance innovation with user needs.
Expand Down
31 changes: 12 additions & 19 deletions tests/test_dpo_trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,9 +457,10 @@ def test_dpo_trainer_padding_token_is_none(self):

with self.assertRaisesRegex(
ValueError,
expected_regex=r"Can't find `pad_token_id` in the `processing_class`. "
r"Explicitly set `tokenizer.pad_token` \(e.g. `tokenizer.pad_token = tokenizer.eos_token`\) "
r"before instantiating the trainer.",
expected_regex=r"`padding_value` is not specified in `DPOConfig`, and `pad_token_id` is missing in "
r"the `processing_class`. Please either set the `padding_value` argument in `DPOConfig`, or set "
r"`tokenizer.pad_token` \(e.g., `tokenizer.pad_token = tokenizer.eos_token`\) before instantiating "
r"the trainer.",
):
trainer = DPOTrainer(
model=self.model,
Expand Down Expand Up @@ -490,24 +491,16 @@ def test_dpo_trainer_w_dataset_num_proc(self):
dummy_dataset = load_dataset("trl-internal-testing/zen", "standard_preference")

tokenizer = AutoTokenizer.from_pretrained(self.model_id)
tokenizer.pad_token = None

with self.assertRaisesRegex(
ValueError,
expected_regex=r"Can't find `pad_token_id` in the `processing_class`. "
r"Explicitly set `tokenizer.pad_token` \(e.g. `tokenizer.pad_token = tokenizer.eos_token`\) "
r"before instantiating the trainer.",
):
trainer = DPOTrainer(
model=self.model,
ref_model=None,
args=training_args,
processing_class=tokenizer,
train_dataset=dummy_dataset["train"],
eval_dataset=dummy_dataset["test"],
)
trainer = DPOTrainer(
model=self.model,
args=training_args,
processing_class=tokenizer,
train_dataset=dummy_dataset["train"],
eval_dataset=dummy_dataset["test"],
)

trainer.train()
trainer.train()

def test_tr_dpo_trainer(self):
with tempfile.TemporaryDirectory() as tmp_dir:
Expand Down
Loading

0 comments on commit 751fb1d

Please sign in to comment.