-
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] Examples folder cleanup: ModelV2 -> RLModule wrapper for migrating to new API stack (by config). #47427
Merged
sven1977
merged 15 commits into
ray-project:master
from
sven1977:new_api_stack_migration_modelv2_from_config
Sep 4, 2024
+228
−28
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
8aec844
wip
sven1977 69c4db9
wip
sven1977 20614f1
Merge branch 'master' of https://github.com/ray-project/ray into new_…
sven1977 1b96242
Merge branch 'master' of https://github.com/ray-project/ray into new_…
sven1977 ef3a2c5
Merge branch 'master' of https://github.com/ray-project/ray into new_…
sven1977 8bdc5ab
wip
sven1977 c3ce0c9
fixes
sven1977 3fd5a25
wip
sven1977 bfd6e33
wip
sven1977 8ff0ae7
Merge branch 'master' of https://github.com/ray-project/ray into new_…
sven1977 4d9b4b4
fix
sven1977 84c1176
Merge branch 'master' of https://github.com/ray-project/ray into new_…
sven1977 ad892af
wip
sven1977 a9e7055
fix
sven1977 55c5a77
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
72 changes: 72 additions & 0 deletions
72
rllib/examples/rl_modules/migrate_modelv2_to_new_api_stack_by_config.py
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,72 @@ | ||
from ray.rllib.algorithms.ppo import PPOConfig | ||
from ray.rllib.core import DEFAULT_POLICY_ID | ||
from ray.rllib.core.rl_module.rl_module import RLModuleSpec | ||
from ray.rllib.examples.rl_modules.classes.modelv2_to_rlm import ModelV2ToRLModule | ||
from ray.rllib.utils.metrics import ( | ||
ENV_RUNNER_RESULTS, | ||
EPISODE_RETURN_MEAN, | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
# Configure an old stack default ModelV2. | ||
config_old_stack = ( | ||
PPOConfig() | ||
.environment("CartPole-v1") | ||
.training( | ||
lr=0.0003, | ||
num_sgd_iter=6, | ||
vf_loss_coeff=0.01, | ||
# Change the ModelV2 settings a bit. | ||
model={ | ||
"fcnet_hiddens": [32], | ||
"fcnet_activation": "linear", | ||
"use_lstm": True, | ||
"vf_share_layers": True, | ||
}, | ||
) | ||
) | ||
|
||
# Training with the (configured and wrapped) ModelV2. | ||
|
||
# We change the original (old API stack) `config` into a new API stack one: | ||
config_new_stack = ( | ||
config_old_stack.copy(copy_frozen=False) | ||
.api_stack( | ||
enable_rl_module_and_learner=True, | ||
enable_env_runner_and_connector_v2=True, | ||
) | ||
.rl_module( | ||
rl_module_spec=RLModuleSpec( | ||
module_class=ModelV2ToRLModule, | ||
model_config_dict={ | ||
"policy_id": DEFAULT_POLICY_ID, | ||
"old_api_stack_algo_config": config_old_stack, | ||
}, | ||
), | ||
) | ||
) | ||
|
||
# Build the new stack algo. | ||
algo_new_stack = config_new_stack.build() | ||
|
||
# Train until a higher return. | ||
min_return_new_stack = 350.0 | ||
results = None | ||
passed = False | ||
for i in range(100): | ||
results = algo_new_stack.train() | ||
print(results) | ||
if results[ENV_RUNNER_RESULTS][EPISODE_RETURN_MEAN] >= min_return_new_stack: | ||
print( | ||
f"Reached episode return of {min_return_new_stack} -> stopping " | ||
"new API stack training." | ||
) | ||
passed = True | ||
break | ||
|
||
if not passed: | ||
raise ValueError( | ||
"Continuing training on the new stack did not succeed! Last return: " | ||
f"{results[ENV_RUNNER_RESULTS][EPISODE_RETURN_MEAN]}" | ||
) |
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.
Ah yes, now LSTM is possible :)