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

[RLlib] Add torch.compile config options to old API stack. #47340

Merged
merged 2 commits into from
Aug 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion rllib/policy/torch_policy_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import gymnasium as gym
import numpy as np
from packaging import version
import tree # pip install dm_tree

import ray
Expand Down Expand Up @@ -41,7 +42,10 @@
from ray.rllib.utils.numpy import convert_to_numpy
from ray.rllib.utils.spaces.space_utils import normalize_action
from ray.rllib.utils.threading import with_lock
from ray.rllib.utils.torch_utils import convert_to_torch_tensor
from ray.rllib.utils.torch_utils import (
convert_to_torch_tensor,
TORCH_COMPILE_REQUIRED_VERSION,
)
from ray.rllib.utils.typing import (
AlgorithmConfigDict,
GradInfoDict,
Expand Down Expand Up @@ -520,6 +524,23 @@ def _init_model_and_dist_class(self):
framework=self.framework,
)

# Compile the model, if requested by the user.
if self.config.get("torch_compile_learner"):
if (
torch is not None
and version.parse(torch.__version__) < TORCH_COMPILE_REQUIRED_VERSION
):
raise ValueError("`torch.compile` is not supported for torch < 2.0.0!")

lw = "learner" if self.config.get("worker_index") else "worker"
model = torch.compile(
model,
backend=self.config.get(
f"torch_compile_{lw}_dynamo_backend", "inductor"
),
dynamic=False,
mode=self.config.get(f"torch_compile_{lw}_dynamo_mode"),
)
return model, dist_class

@override(Policy)
Expand Down
Loading