-
Notifications
You must be signed in to change notification settings - Fork 2.8k
[ORPO] Move ORPOTrainer to experimental #4480
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2d2306a
Move ORPOTrainer and ORPOConfig to experimental
behroozazarkhalili 18040a4
Address reviewer feedback on ORPO experimental migration
behroozazarkhalili 9d7c53c
Fix ruff linting errors - remove unused imports
behroozazarkhalili 92e218b
Fix import path for testing_utils in ORPO test file
behroozazarkhalili c2db596
Fix import ordering in ORPO trainer
behroozazarkhalili b6815e3
Squashed commit of the following:
qgallouedec dbf140d
style
qgallouedec e0ea650
Merge branch 'main' into refactor/move-orpo-to-experimental
qgallouedec b13c018
finish pr
qgallouedec File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # Copyright 2020-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. | ||
|
|
||
| from .orpo_config import ORPOConfig | ||
| from .orpo_trainer import ORPOTrainer | ||
|
|
||
|
|
||
| __all__ = ["ORPOConfig", "ORPOTrainer"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,179 @@ | ||
| # Copyright 2020-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. | ||
|
|
||
| from dataclasses import dataclass, field | ||
| from typing import Any | ||
|
|
||
| from transformers import TrainingArguments | ||
|
|
||
|
|
||
| @dataclass | ||
| class ORPOConfig(TrainingArguments): | ||
| r""" | ||
| Configuration class for the [`experimental.orpo.ORPOTrainer`]. | ||
|
|
||
| This class includes only the parameters that are specific to ORPO training. For a full list of training arguments, | ||
| please refer to the [`~transformers.TrainingArguments`] documentation. Note that default values in this class may | ||
| differ from those in [`~transformers.TrainingArguments`]. | ||
|
|
||
| Using [`~transformers.HfArgumentParser`] we can turn this class into | ||
| [argparse](https://docs.python.org/3/library/argparse#module-argparse) arguments that can be specified on the | ||
| command line. | ||
|
|
||
| Parameters: | ||
| max_length (`int` or `None`, *optional*, defaults to `1024`): | ||
| Maximum length of the sequences (prompt + completion) in the batch. This argument is required if you want | ||
| to use the default data collator. | ||
| max_prompt_length (`int` or `None`, *optional*, defaults to `512`): | ||
| Maximum length of the prompt. This argument is required if you want to use the default data collator. | ||
| max_completion_length (`int`, *optional*): | ||
| Maximum length of the completion. This argument is required if you want to use the default data collator | ||
| and your model is an encoder-decoder. | ||
| beta (`float`, *optional*, defaults to `0.1`): | ||
| Parameter controlling the relative ratio loss weight in the ORPO loss. In the | ||
| [paper](https://huggingface.co/papers/2403.07691), it is denoted by λ. In the | ||
| [code](https://github.com/xfactlab/orpo), it is denoted by `alpha`. | ||
| disable_dropout (`bool`, *optional*, defaults to `True`): | ||
| Whether to disable dropout in the model. | ||
| label_pad_token_id (`int`, *optional*, defaults to `-100`): | ||
| Label pad token id. This argument is required if you want to use the default data collator. | ||
| padding_value (`int`, *optional*): | ||
| Padding value to use. If `None`, the padding value of the tokenizer is used. | ||
| truncation_mode (`str`, *optional*, defaults to `"keep_end"`): | ||
| Truncation mode to use when the prompt is too long. Possible values are `"keep_end"` or `"keep_start"`. | ||
| This argument is required if you want to use the default data collator. | ||
| generate_during_eval (`bool`, *optional*, defaults to `False`): | ||
| If `True`, generates and logs completions from the model to W&B or Comet during evaluation. | ||
| is_encoder_decoder (`bool`, *optional*): | ||
| When using the `model_init` argument (callable) to instantiate the model instead of the `model` argument, | ||
| you need to specify if the model returned by the callable is an encoder-decoder model. | ||
| model_init_kwargs (`dict[str, Any]`, *optional*): | ||
| Keyword arguments to pass to `AutoModelForCausalLM.from_pretrained` when instantiating the model from a | ||
| string. | ||
| dataset_num_proc (`int`, *optional*): | ||
| Number of processes to use for processing the dataset. | ||
| """ | ||
|
|
||
| _VALID_DICT_FIELDS = TrainingArguments._VALID_DICT_FIELDS + ["model_init_kwargs"] | ||
|
|
||
| # Parameters whose default values are overridden from TrainingArguments | ||
| learning_rate: float = field( | ||
| default=1e-6, | ||
| metadata={"help": "The initial learning rate for AdamW."}, | ||
| ) | ||
| logging_steps: float = field( | ||
| default=10, | ||
| metadata={ | ||
| "help": "Log every X updates steps. Should be an integer or a float in range `[0,1)`. If smaller than 1, " | ||
| "will be interpreted as ratio of total training steps." | ||
| }, | ||
| ) | ||
| gradient_checkpointing: bool = field( | ||
| default=True, | ||
| metadata={ | ||
| "help": "If True, use gradient checkpointing to save memory at the expense of slower backward pass." | ||
| }, | ||
| ) | ||
| bf16: bool | None = field( | ||
| default=None, | ||
| metadata={ | ||
| "help": "Whether to use bf16 (mixed) precision instead of 32-bit. Requires Ampere or higher NVIDIA " | ||
| "architecture or Intel XPU or using CPU (use_cpu) or Ascend NPU. If not set, it defaults to `True` if " | ||
| "`fp16` is not set." | ||
| }, | ||
| ) | ||
| # Transformers 4.57.0 introduced a bug that caused the dtype of `lr_scheduler_kwargs` to be unparsable. This issue | ||
| # was fixed in https://github.com/huggingface/transformers/pull/41322, but the fix has not yet been released. We | ||
| # add a temporary workaround here, which can be removed once the fix is available—likely in Transformers 4.57.2. | ||
| lr_scheduler_kwargs: dict | str | None = field( | ||
| default=None, | ||
| metadata={ | ||
| "help": "Additional parameters for the lr_scheduler, such as {'num_cycles': 1} for cosine with hard " | ||
| "restarts." | ||
| }, | ||
| ) | ||
|
|
||
| max_length: int | None = field( | ||
| default=1024, | ||
| metadata={"help": "Maximum length of the sequences (prompt + completion) in the batch."}, | ||
| ) | ||
| max_prompt_length: int | None = field( | ||
| default=512, | ||
| metadata={ | ||
| "help": "Maximum length of the prompt. This argument is required if you want to use the default data " | ||
| "collator and your model is an encoder-decoder." | ||
| }, | ||
| ) | ||
| max_completion_length: int | None = field( | ||
| default=None, | ||
| metadata={ | ||
| "help": "Maximum length of the completion. This argument is required if you want to use the default data " | ||
| "collator and your model is an encoder-decoder." | ||
| }, | ||
| ) | ||
| beta: float = field( | ||
| default=0.1, | ||
| metadata={ | ||
| "help": "Parameter controlling the relative ratio loss weight in the ORPO loss. In the paper, it is " | ||
| "denoted by λ." | ||
| }, | ||
| ) | ||
| disable_dropout: bool = field( | ||
| default=True, | ||
| metadata={"help": "Whether to disable dropout in the model."}, | ||
| ) | ||
| label_pad_token_id: int = field( | ||
| default=-100, | ||
| metadata={ | ||
| "help": "Label pad token id. This argument is required if you want to use the default data collator." | ||
| }, | ||
| ) | ||
| padding_value: int | None = field( | ||
| default=None, | ||
| metadata={"help": "Padding value to use. If `None`, the padding value of the tokenizer is used."}, | ||
| ) | ||
| truncation_mode: str = field( | ||
| default="keep_end", | ||
| metadata={ | ||
| "help": "Truncation mode to use when the prompt is too long.", | ||
| "choices": ["keep_end", "keep_start"], | ||
| }, | ||
| ) | ||
| generate_during_eval: bool = field( | ||
| default=False, | ||
| metadata={"help": "If `True`, generates and logs completions from the model to W&B during evaluation."}, | ||
| ) | ||
| is_encoder_decoder: bool | None = field( | ||
| default=None, | ||
| metadata={ | ||
| "help": "When using the `model_init` argument (callable) to instantiate the model instead of the `model` " | ||
| "argument, you need to specify if the model returned by the callable is an encoder-decoder model." | ||
| }, | ||
| ) | ||
| model_init_kwargs: dict[str, Any] | None = field( | ||
| default=None, | ||
| metadata={ | ||
| "help": "Keyword arguments to pass to `AutoModelForCausalLM.from_pretrained` when instantiating the model " | ||
| "from a string." | ||
| }, | ||
| ) | ||
| dataset_num_proc: int | None = field( | ||
| default=None, | ||
| metadata={"help": "Number of processes to use for processing the dataset."}, | ||
| ) | ||
|
|
||
| def __post_init__(self): | ||
| self.bf16 = not (self.fp16) if self.bf16 is None else self.bf16 | ||
|
|
||
| super().__post_init__() |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.