-
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] Eager execution for centralized critic example, fix simple optimizer for multiagent #5683
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,11 +4,14 @@ | |
|
||
import logging | ||
import random | ||
from collections import defaultdict | ||
|
||
import ray | ||
from ray.rllib.evaluation.metrics import get_learner_stats | ||
from ray.rllib.evaluation.metrics import LEARNER_STATS_KEY | ||
from ray.rllib.optimizers.multi_gpu_optimizer import _averaged | ||
from ray.rllib.optimizers.policy_optimizer import PolicyOptimizer | ||
from ray.rllib.policy.sample_batch import SampleBatch, MultiAgentBatch | ||
from ray.rllib.policy.sample_batch import SampleBatch, DEFAULT_POLICY_ID, \ | ||
MultiAgentBatch | ||
from ray.rllib.utils.annotations import override | ||
from ray.rllib.utils.filter import RunningStat | ||
from ray.rllib.utils.timer import TimerStat | ||
|
@@ -29,17 +32,22 @@ def __init__(self, | |
workers, | ||
num_sgd_iter=1, | ||
train_batch_size=1, | ||
sgd_minibatch_size=0): | ||
sgd_minibatch_size=0, | ||
standardize_fields=frozenset([])): | ||
PolicyOptimizer.__init__(self, workers) | ||
|
||
self.update_weights_timer = TimerStat() | ||
self.standardize_fields = standardize_fields | ||
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. or [] 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. Changed to froenzeset |
||
self.sample_timer = TimerStat() | ||
self.grad_timer = TimerStat() | ||
self.throughput = RunningStat() | ||
self.num_sgd_iter = num_sgd_iter | ||
self.sgd_minibatch_size = sgd_minibatch_size | ||
self.train_batch_size = train_batch_size | ||
self.learner_stats = {} | ||
self.policies = dict(self.workers.local_worker() | ||
.foreach_trainable_policy(lambda p, i: (i, p))) | ||
logger.debug("Policies to train: {}".format(self.policies)) | ||
|
||
@override(PolicyOptimizer) | ||
def step(self): | ||
|
@@ -63,16 +71,44 @@ def step(self): | |
samples = SampleBatch.concat_samples(samples) | ||
self.sample_timer.push_units_processed(samples.count) | ||
|
||
with self.grad_timer: | ||
for i in range(self.num_sgd_iter): | ||
for minibatch in self._minibatches(samples): | ||
fetches = self.workers.local_worker().learn_on_batch( | ||
minibatch) | ||
self.learner_stats = get_learner_stats(fetches) | ||
if self.num_sgd_iter > 1: | ||
logger.debug("{} {}".format(i, fetches)) | ||
self.grad_timer.push_units_processed(samples.count) | ||
# Handle everything as if multiagent | ||
if isinstance(samples, SampleBatch): | ||
samples = MultiAgentBatch({ | ||
DEFAULT_POLICY_ID: samples | ||
}, samples.count) | ||
|
||
fetches = {} | ||
with self.grad_timer: | ||
for policy_id, policy in self.policies.items(): | ||
if policy_id not in samples.policy_batches: | ||
continue | ||
|
||
batch = samples.policy_batches[policy_id] | ||
for field in self.standardize_fields: | ||
value = batch[field] | ||
standardized = (value - value.mean()) / max( | ||
1e-4, value.std()) | ||
batch[field] = standardized | ||
|
||
for i in range(self.num_sgd_iter): | ||
iter_extra_fetches = defaultdict(list) | ||
for minibatch in self._minibatches(batch): | ||
batch_fetches = ( | ||
self.workers.local_worker().learn_on_batch( | ||
MultiAgentBatch({ | ||
policy_id: minibatch | ||
}, minibatch.count)))[policy_id] | ||
for k, v in batch_fetches[LEARNER_STATS_KEY].items(): | ||
iter_extra_fetches[k].append(v) | ||
logger.debug("{} {}".format(i, | ||
_averaged(iter_extra_fetches))) | ||
fetches[policy_id] = _averaged(iter_extra_fetches) | ||
|
||
self.grad_timer.push_units_processed(samples.count) | ||
if len(fetches) == 1 and DEFAULT_POLICY_ID in fetches: | ||
self.learner_stats = fetches[DEFAULT_POLICY_ID] | ||
else: | ||
self.learner_stats = fetches | ||
self.num_steps_sampled += samples.count | ||
self.num_steps_trained += samples.count | ||
return self.learner_stats | ||
|
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
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.
this should try to fetch gamma from policy config instead right
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.
Not for the example.