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

[WIP] Padding bug in GRPO #2425

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

krammnic
Copy link
Contributor

Context

What is the purpose of this PR? Is it to

  • add a new feature
  • fix a bug
  • update tests and/or documentation
  • other (please add here)

Please link to any issues this PR addresses.

Changelog

What are the changes made in this PR?

Test plan

Please make sure to do each of the following if applicable to your PR. If you're unsure about any one of these just ask and we will happily help. We also have a contributing page for some guidance on contributing.

  • run pre-commit hooks and linters (make sure you've first installed via pre-commit install)
  • add unit tests for any new functionality
  • update docstrings for any new or updated methods or classes
  • run unit tests via pytest tests
  • run recipe tests via pytest tests -m integration_test
  • manually run any new or modified recipes with sufficient proof of correctness
  • include relevant commands and any other artifacts in this summary (pastes of loss curves, eval results, etc.)

UX

If your function changed a public API, please add a dummy example of what the user experience will look like when calling it.
Here is a docstring example
and a tutorial example

  • I did not change any public API
  • I have added an example to docs or docstrings

I still need to verify that everything is correct, but here is a fix reproduction code:

import torch
from typing import NamedTuple, List

class GRPOTrajectory(NamedTuple):
    query_responses: torch.Tensor  # [BxG, P+L]
    logprobs: torch.Tensor         # [BxG, L]
    ref_logprobs: torch.Tensor     # [BxG, L]
    rewards: torch.Tensor          # [BxG]
    successes: torch.Tensor
    advantages: torch.Tensor       # [BxG]
    masks: torch.Tensor            # [BxG, P+L, P+L]  
    position_ids: torch.Tensor     # [BxG, P+L]
    response_padding_masks: torch.Tensor  # [BxG, L]
    seq_lens: torch.Tensor

trajectory1 = GRPOTrajectory(
    query_responses=torch.randint(0, 100, (16, 512)),
    logprobs=torch.randn(16, 512),
    ref_logprobs=torch.randn(16, 512),
    rewards=torch.randn(16),
    successes=torch.randn(16),
    advantages=torch.randn(16),
    masks=torch.ones(16, 512, 512, dtype=torch.bool),  
    position_ids=torch.arange(512).expand(16, -1),
    response_padding_masks=torch.ones(16, 512, dtype=torch.bool),
    seq_lens=torch.full((16,), 512)
)

trajectory2 = GRPOTrajectory(
    query_responses=torch.randint(0, 100, (16, 600)),
    logprobs=torch.randn(16, 300),
    ref_logprobs=torch.randn(16, 300),
    rewards=torch.randn(16),
    successes=torch.randn(16),
    advantages=torch.randn(16),
    masks=torch.ones(16, 300, 300, dtype=torch.bool), 
    position_ids=torch.arange(300).expand(16, -1),
    response_padding_masks=torch.ones(16, 300, dtype=torch.bool),
    seq_lens=torch.full((16,), 300)
)
trajectory3 = GRPOTrajectory(
    query_responses=torch.randint(0, 100, (16, 600)),
    logprobs=torch.randn(16, 300),
    ref_logprobs=torch.randn(16, 24),
    rewards=torch.randn(16),
    successes=torch.randn(16),
    advantages=torch.randn(16),
    masks=torch.ones(16, 300, 300, dtype=torch.bool),  
    position_ids=torch.arange(300).expand(16, -1),
    response_padding_masks=torch.ones(16, 300, dtype=torch.bool),
    seq_lens=torch.full((16,), 300)
)

trajectories = [trajectory1, trajectory2, trajectory3]

def _pad_tensor(tensor: torch.Tensor, target_dim: int, pad_value: float, dim: int = 1) -> torch.Tensor:
    pad_size = target_dim - tensor.shape[dim]
    if pad_size <= 0:
        return tensor
    
    pad = [0] * (2 * tensor.ndim)
    pad[-2*(dim+1)] = 0     
    pad[-2*(dim+1)+1] = pad_size
    return torch.nn.functional.pad(tensor, pad, value=pad_value)

max_p_plus_l = max(t.query_responses.shape[1] for t in trajectories)
max_l = max(t.logprobs.shape[1] for t in trajectories)

padded_trajectories = []
for traj in trajectories:
    padded_masks = _pad_tensor(
        _pad_tensor(traj.masks, max_p_plus_l, 0, dim=2),  # Pad width
        max_p_plus_l, 
        0, 
        dim=1 
    )
    
    padded_trajectories.append(GRPOTrajectory(
        query_responses=_pad_tensor(traj.query_responses, max_p_plus_l, 1, dim=1),
        logprobs=_pad_tensor(traj.logprobs, max_l, 1.0, dim=1),
        ref_logprobs=_pad_tensor(traj.ref_logprobs, max_l, 1.0, dim=1),
        rewards=traj.rewards,
        successes=traj.successes,
        advantages=traj.advantages,
        masks=padded_masks,
        position_ids=_pad_tensor(traj.position_ids, max_p_plus_l, 0, dim=1),
        response_padding_masks=_pad_tensor(traj.response_padding_masks, max_l, False, dim=1),
        seq_lens=traj.seq_lens
    ))


concatenated = GRPOTrajectory(*map(torch.cat, zip(*padded_trajectories)))
print(concatenated.query_responses.shape)

Copy link

pytorch-bot bot commented Feb 22, 2025

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/torchtune/2425

Note: Links to docs will display an error until the docs builds have been completed.

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Feb 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants