Avoid torch type-error under torch.compile #1922
Merged
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.
Description
In
RolloutBuffer.compute_returns_and_advantage
a numpy array with dtype bool is used as a, operand for subtraction with a python scalar. This relies on some automatic casting rules which pytorch does not support (in particular promoting bool arrays to int/float). When this region is hit by torch-compile a runtime error is encountered. Casting the bool array to an integer dtype resolves the issue.After the changes at pytorch/pytorch#124481 land, the example presented in this issue will successfully compile with this small change.
Motivation and Context
This change has no observable impact on the behaviors of programs executed normally. It prevents an error when a user's program uses torch-compile.
If this change is not desired the closed PR will document a work-around for anyone who hits this issue.
Types of changes
Probably the closest option, but I wouldn't classify this as a bug. The issue is only present when a program is manipulated by a 3rd party library.
Checklist
make format
(required)make check-codestyle
andmake lint
(required)make pytest
andmake type
both pass. (required)make doc
(required)Note: You can run most of the checks using
make commit-checks
.Note: we are using a maximum length of 127 characters per line