Skip to content

[BugFix] Fix Binary reshaping #3084

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 1 commit into from
Jul 18, 2025
Merged

Conversation

LCarmi
Copy link
Contributor

@LCarmi LCarmi commented Jul 18, 2025

Description

Fixes a bug introduced by #3077 where shapes for newly instantiated Binary specs are incompatible with the value of n passed.

In particular the following pattern is used in unsqueeze and other shape-modifying operations:

def unsqueeze(self, dim: int):
        shape = _unsqueezed_shape(self.shape, dim)
        return self.__class__(
            n=self.shape[-1] if len(self.shape) > 0 else None,
            shape=shape,
            device=self.device,
            dtype=self.dtype,
        )

However, this is wrong because we should extract n from the new shape rather than from the current self.shape.

We fix by switching to check on the new shape:

def unsqueeze(self, dim: int):
        shape = _unsqueezed_shape(self.shape, dim)
        return self.__class__(
            n=shape[-1] if len(shape) > 0 else None,
            shape=shape,
            device=self.device,
            dtype=self.dtype,
        )

Motivation and Context

Without this change, we have value errors raised due to shape mismatches:

import pytest
import torch
from torchrl.data import Binary


def test__Binary__if_empty_cannot_expand() -> None:
    spec = Binary(shape=())
    new_shape = (3, 3)

    with pytest.raises(ValueError, match="The last value of the shape must match 'n'"):
        spec.expand(new_shape)

NOTE: I have tried to include a test to check for this, but I don't know if that is the correct place and scope.

Types of changes

What types of changes does your code introduce? Remove all that do not apply:

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds core functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation (update in the documentation)
  • Example (update in the folder of examples)

Checklist

Go over all the following points, and put an x in all the boxes that apply.
If you are unsure about any of these, don't hesitate to ask. We are here to help!

  • I have read the CONTRIBUTION guide
  • My change requires a change to the documentation.
  • I have updated the tests accordingly (required for a bug fix or a new feature).
  • I have updated the documentation accordingly.

Copy link

pytorch-bot bot commented Jul 18, 2025

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/rl/3084

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

❌ 3 New Failures, 2 Pending, 2 Unrelated Failures

As of commit 0e449da with merge base 4001d9c (image):

NEW FAILURES - The following jobs have failed:

BROKEN TRUNK - The following jobs failed but were present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

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

Copy link

meta-cla bot commented Jul 18, 2025

Hi @LCarmi!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

Copy link

meta-cla bot commented Jul 18, 2025

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@meta-cla meta-cla 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 Jul 18, 2025
Copy link
Collaborator

@vmoens vmoens left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks (and sorry!)

@vmoens vmoens added the bug Something isn't working label Jul 18, 2025
@vmoens vmoens force-pushed the lc/fix_binary_shape branch from f072295 to 0e449da Compare July 18, 2025 20:11
@vmoens vmoens merged commit 163e23f into pytorch:main Jul 18, 2025
96 of 103 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working 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