-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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] RLModule API: SelfSupervisedLossAPI
for RLModules that bring their own loss (algo independent).
#47581
Merged
sven1977
merged 10 commits into
ray-project:master
from
sven1977:rl_module_api_self_supervised_loss
Sep 11, 2024
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a075ed9
wip
sven1977 0f2928e
Merge branch 'master' of https://github.com/ray-project/ray into rl_m…
sven1977 21048ed
wip
sven1977 7a642aa
wip
sven1977 a86c1c2
LINT
sven1977 07551b2
Merge branch 'master' of https://github.com/ray-project/ray into rl_m…
sven1977 92c5919
fix
sven1977 2b3d3aa
fix
sven1977 8713a3c
Merge branch 'master' of https://github.com/ray-project/ray into rl_m…
sven1977 03258df
fix
sven1977 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
from ray.rllib.core.rl_module.apis.inference_only_api import InferenceOnlyAPI | ||
from ray.rllib.core.rl_module.apis.self_supervised_loss_api import SelfSupervisedLossAPI | ||
from ray.rllib.core.rl_module.apis.target_network_api import TargetNetworkAPI | ||
from ray.rllib.core.rl_module.apis.value_function_api import ValueFunctionAPI | ||
|
||
|
||
__all__ = [ | ||
"InferenceOnlyAPI", | ||
"SelfSupervisedLossAPI", | ||
"TargetNetworkAPI", | ||
"ValueFunctionAPI", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import abc | ||
from typing import Any, Dict, TYPE_CHECKING | ||
|
||
from ray.rllib.utils.typing import ModuleID, TensorType | ||
|
||
if TYPE_CHECKING: | ||
from ray.rllib.algorithms.algorithm_config import AlgorithmConfig | ||
from ray.rllib.core.learner.learner import Learner | ||
|
||
|
||
class SelfSupervisedLossAPI(abc.ABC): | ||
"""An API to be implemented by RLModules that bring their own self-supervised loss. | ||
|
||
Learners will call these model's `compute_self_supervised_loss()` method instead of | ||
the Learner's own `compute_loss_for_module()` method. | ||
The call signature is identical to the Learner's `compute_loss_for_module()` method | ||
except of an additional mandatory `learner` kwarg. | ||
""" | ||
|
||
@abc.abstractmethod | ||
def compute_self_supervised_loss( | ||
self, | ||
*, | ||
learner: "Learner", | ||
module_id: ModuleID, | ||
config: "AlgorithmConfig", | ||
batch: Dict[str, Any], | ||
fwd_out: Dict[str, TensorType], | ||
) -> TensorType: | ||
"""Computes the loss for a single module. | ||
|
||
Think of this as computing loss for a single agent. For multi-agent use-cases | ||
that require more complicated computation for loss, consider overriding the | ||
`compute_losses` method instead. | ||
|
||
Args: | ||
learner: The Learner calling this loss method on the RLModule. | ||
module_id: The ID of the RLModule (within a MultiRLModule). | ||
config: The AlgorithmConfig specific to the given `module_id`. | ||
batch: The sample batch for this particular RLModule. | ||
fwd_out: The output of the forward pass for this particular RLModule. | ||
|
||
Returns: | ||
A single total loss tensor. If you have more than one optimizer on the | ||
provided `module_id` and would like to compute gradients separately using | ||
these different optimizers, simply add up the individual loss terms for | ||
each optimizer and return the sum. Also, for recording/logging any | ||
individual loss terms, you can use the `Learner.metrics.log_value( | ||
key=..., value=...)` or `Learner.metrics.log_dict()` APIs. See: | ||
:py:class:`~ray.rllib.utils.metrics.metrics_logger.MetricsLogger` for more | ||
information. | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very nice comment!