Skip to content
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

MPO #2544

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open

MPO #2544

wants to merge 14 commits into from

Conversation

qgallouedec
Copy link
Member

What does this PR do?

Fixes # (issue)

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a GitHub issue? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines.
  • Did you write any new necessary tests?

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@ariG23498
Copy link
Collaborator

Here is a rough colab notebook that I have created.

In the notebook:

  1. Installed trl from the branch
  2. Used the openbmb/RLAIF-V-Dataset dataset
  3. Used the HuggingFaceTB/SmolVLM-Instruct model

Here are my queries:

  1. Reading the code, we expect the loss_type to be a str with , seperated losses.
    training_args = DPOConfig(
        ...
        loss_type="sigmoid, bco_pair", # <-- a collection of losses
    )
    As far as I understand the MPO paper, it comprises of three losses, the DPO loss, the BCO loss, and the SFT loss. Here I have added the BCO and used sigmoid for the DPO loss, do we also have a way to add the SFT loss here somehow (which I think would be cross entropy loss?)
  2. The weighting parameter in the config (as in this comment)

losses, chosen_rewards, rejected_rewards = self.dpo_loss(
model_output["chosen_logps"], model_output["rejected_logps"], ref_chosen_logps, ref_rejected_logps
)
if "," in self.loss_type:
Copy link
Member Author

@qgallouedec qgallouedec Jan 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ideally, at this point, we would have

  • self.loss_type a list of strings (eg, [“sigmoid”, “bco_pair”])
  • self.loss_type_to_weights a dict of str, float which for each loss type associates a weight.

Parsing for loss type could be done directly in the config, as here:

mixture_coef: list[float] = field(
default_factory=lambda: [0.5],
metadata={
"help": "Logit mixture coefficient for the model and reference model. If a list of floats is provided "
"then the mixture coefficient is selected for each new epoch and the last coefficient is used for the "
"rest of the epochs."
},
)
def __post_init__(self):
super().__post_init__()
if hasattr(self.mixture_coef, "__len__") and len(self.mixture_coef) == 1:
self.mixture_coef = self.mixture_coef[0]

@ariG23498 ariG23498 marked this pull request as ready for review January 24, 2025 07:24
@ariG23498
Copy link
Collaborator

@qgallouedec Here is a rough colab notebook with the current MPO training.

Let me know what you think.

Copy link
Member Author

@qgallouedec qgallouedec left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Some initial comments :)

loss_weights: Optional[Dict[str, float]] = field(
default_factory=lambda: ["your_values"]
)
loss_type: List[str] | str = field(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to check if this work with the parser and the cli

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you could let me know how you might check it, I could do it and report back to you.

Copy link
Member Author

@qgallouedec qgallouedec Jan 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

running this should work:

trl dpo --output_dir tmp_dir --model_name_or_path trl-internal-testing/tiny-Qwen2ForCausalLM-2.5 --dataset_name trl-internal-testing/zen --dataset_config standard_preference --report_to none

Copy link
Collaborator

@ariG23498 ariG23498 Jan 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried the above command, which resulted in an issue with the self.loss_type being None.

Upon changing the command to

trl dpo --output_dir tmp_dir --model_name_or_path trl-internal-testing/tiny-Qwen2ForCausalLM-2.5 --dataset_name trl-internal-testing/zen --dataset_config standard_preference --report_to none --loss-type sigmoid

it started to train. I am not sure why this happens 🤔

@qgallouedec
Copy link
Member Author

qgallouedec commented Jan 24, 2025

Really nice!!!

But don't do this (important):

    # Apply the chat template
    prompt = processor.apply_chat_template(prompt, tokenize=False)
    chosen = processor.apply_chat_template(chosen, tokenize=False)
    rejected = processor.apply_chat_template(rejected, tokenize=False)

The DPO Trainer handle applying the chat template. See #1930 for more info.

This code snippet is present in so many examples online, it's a scourge. 😩

@@ -15,7 +15,7 @@
import warnings
from dataclasses import dataclass, field
from enum import Enum
from typing import Any, Callable, Optional, Union
from typing import Any, Callable, Optional, Union, List, Dict
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can use list and dict instead

loss_weights (`dict[str, float]` or `None`, *optional*, defaults to `None`):
Use to weight a combination of losses. The keys must be in `loss_type`. By default (if not specified in the dict),
the weight for a loss in loss_type is 1.0.
loss_type (`str` or `list`, *optional*, defaults to `"sigmoid"`):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you also document that, when a list is passed, the loss is the sum of these values?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add "sft" as well

curr_losses, curr_chosen_rewards, curr_rejected_rewards = self.dpo_loss(
curr_loss_type, model_output["chosen_logps"], model_output["rejected_logps"], ref_chosen_logps, ref_rejected_logps
)
curr_loss_weight = getattr(self.loss_weights, curr_loss_type, 1.0)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

@ariG23498
Copy link
Collaborator

@qgallouedec I have resolved the merge conflicts and have also worked on the review suggestions. Could you help me with another round of review?

If this looks good, I can start a small training run on VLM with MPO. WDYT?

@ariG23498
Copy link
Collaborator

@qgallouedec a gentle ping here!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants