-
Notifications
You must be signed in to change notification settings - Fork 34
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
Updated train_on_episode_end #320
Open
LucaVendruscolo
wants to merge
2
commits into
Eclectic-Sheep:main
Choose a base branch
from
LucaVendruscolo:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -680,42 +680,46 @@ def main(fabric: Fabric, cfg: Dict[str, Any]): | |
|
||
# Train the agent | ||
if iter_num >= learning_starts: | ||
ratio_steps = policy_step - prefill_steps * policy_steps_per_iter | ||
per_rank_gradient_steps = ratio(ratio_steps / world_size) | ||
if per_rank_gradient_steps > 0: | ||
local_data = rb.sample_tensors( | ||
batch_size=cfg.algo.per_rank_batch_size, | ||
sequence_length=cfg.algo.per_rank_sequence_length, | ||
n_samples=per_rank_gradient_steps, | ||
dtype=None, | ||
device=fabric.device, | ||
from_numpy=cfg.buffer.from_numpy, | ||
) | ||
with timer("Time/train_time", SumMetric, sync_on_compute=cfg.metric.sync_on_compute): | ||
for i in range(per_rank_gradient_steps): | ||
if ( | ||
cumulative_per_rank_gradient_steps % cfg.algo.critic.per_rank_target_network_update_freq | ||
== 0 | ||
): | ||
for cp, tcp in zip(critic.module.parameters(), target_critic.module.parameters()): | ||
tcp.data.copy_(cp.data) | ||
batch = {k: v[i].float() for k, v in local_data.items()} | ||
train( | ||
fabric, | ||
world_model, | ||
actor, | ||
critic, | ||
target_critic, | ||
world_optimizer, | ||
actor_optimizer, | ||
critic_optimizer, | ||
batch, | ||
aggregator, | ||
cfg, | ||
actions_dim, | ||
) | ||
cumulative_per_rank_gradient_steps += 1 | ||
train_step += world_size | ||
is_distributed = fabric.world_size > 1 | ||
if ( | ||
cfg.algo.train_on_episode_end and reset_envs > 0 and not is_distributed | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same for all the files you modified |
||
) or not cfg.algo.train_on_episode_end: | ||
ratio_steps = policy_step - prefill_steps * policy_steps_per_iter | ||
per_rank_gradient_steps = ratio(ratio_steps / world_size) | ||
if per_rank_gradient_steps > 0: | ||
local_data = rb.sample_tensors( | ||
batch_size=cfg.algo.per_rank_batch_size, | ||
sequence_length=cfg.algo.per_rank_sequence_length, | ||
n_samples=per_rank_gradient_steps, | ||
dtype=None, | ||
device=fabric.device, | ||
from_numpy=cfg.buffer.from_numpy, | ||
) | ||
with timer("Time/train_time", SumMetric, sync_on_compute=cfg.metric.sync_on_compute): | ||
for i in range(per_rank_gradient_steps): | ||
if ( | ||
cumulative_per_rank_gradient_steps % cfg.algo.critic.per_rank_target_network_update_freq | ||
== 0 | ||
): | ||
for cp, tcp in zip(critic.module.parameters(), target_critic.module.parameters()): | ||
tcp.data.copy_(cp.data) | ||
batch = {k: v[i].float() for k, v in local_data.items()} | ||
train( | ||
fabric, | ||
world_model, | ||
actor, | ||
critic, | ||
target_critic, | ||
world_optimizer, | ||
actor_optimizer, | ||
critic_optimizer, | ||
batch, | ||
aggregator, | ||
cfg, | ||
actions_dim, | ||
) | ||
cumulative_per_rank_gradient_steps += 1 | ||
train_step += world_size | ||
|
||
# Log metrics | ||
if cfg.metric.log_level > 0 and (policy_step - last_log >= cfg.metric.log_every or iter_num == total_iters): | ||
|
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.
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.
Hi @LucaVendruscolo, here I see a problem: if you set
cfg.algo.train_on_episode_end = True
and you start a distributed training, then you will hav e the following situation:cfg.algo.train_on_episode_end = True
reset_envs > 0 = True
(let us suppose that the episode ended)not is_distribured = False
not cfg.algo.train_on_episode_end = False
This becomes:
(True and True and False) or False = False
In this case, the agent will never enter in the if statement, so the agent will never be trained.
What is missing is the modification of the config
cfg.algo.train_on_episode_end
whenis_distributed
isTrue
.For example, by adding near row 385 something like this:
Or you need to modify the condition in order to take into account the situation described above