DLR-RM / stable-baselines3

PyTorch version of Stable Baselines, reliable implementations of reinforcement learning algorithms.
https://stable-baselines3.readthedocs.io
MIT License
8.35k stars 1.6k forks source link

Avoid torch type-error under torch.compile #1922

Closed amjames closed 1 month ago

amjames commented 1 month ago

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 https://github.com/pytorch/pytorch/pull/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

Note: You can run most of the checks using make commit-checks.

Note: we are using a maximum length of 127 characters per line