Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
alf
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Philipp Sauer
alf
Commits
efc304ed
Unverified
Commit
efc304ed
authored
2 years ago
by
Break Yang
Committed by
GitHub
2 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Use torch.div for floor div in replay buffer (#1396)
* Use torch.div for floor div in replay buffer * Address comments
parent
3b64a133
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
alf/experience_replayers/replay_buffer.py
+9
-7
9 additions, 7 deletions
alf/experience_replayers/replay_buffer.py
alf/experience_replayers/segment_tree.py
+1
-4
1 addition, 4 deletions
alf/experience_replayers/segment_tree.py
with
10 additions
and
11 deletions
alf/experience_replayers/replay_buffer.py
+
9
−
7
View file @
efc304ed
...
...
@@ -226,9 +226,9 @@ class ReplayBuffer(RingBuffer):
def
_index_to_env_id_idx
(
self
,
indices
):
"""
Convert indices used by SegmentTree to (env_id, idx).
"""
#
need to use `//` here. Newer versions of pytorch will do automatic
#
type promtion and will generate float indices if `/` is used
.
env_ids
=
indices
//
self
.
_max_length
#
Here ``torch.div`` with ``rounding_mode="floor"`` will produce
#
result with integer dtype
.
env_ids
=
torch
.
div
(
indices
,
self
.
_max_length
,
rounding_mode
=
"
floor
"
)
return
env_ids
,
indices
%
self
.
_max_length
def
_change_mini_batch_length
(
self
,
mini_batch_length
):
...
...
@@ -530,10 +530,12 @@ class ReplayBuffer(RingBuffer):
n = (current_pos - idx - 1) / L
"""
# need to use `//` here. Newer versions of pytorch will do automatic
# type promtion and will generate float indices if `/` is used.
return
((
self
.
_current_pos
[
env_ids
]
-
x
-
1
)
//
self
.
_max_length
)
*
self
.
_max_length
+
x
# Here ``torch.div`` with ``rounding_mode="floor"`` will produce
# result with integer dtype.
return
torch
.
div
(
self
.
_current_pos
[
env_ids
]
-
x
-
1
,
self
.
_max_length
,
rounding_mode
=
"
floor
"
)
*
self
.
_max_length
+
x
def
_set_default_return
(
self
,
env_ids
):
ind
=
(
env_ids
,
self
.
circular
(
self
.
_current_pos
[
env_ids
]
-
1
))
...
...
This diff is collapsed.
Click to expand it.
alf/experience_replayers/segment_tree.py
+
1
−
4
View file @
efc304ed
...
...
@@ -68,10 +68,7 @@ class SegmentTree(nn.Module):
"""
Calculate the parent value from its children.
"""
# need to use `//` here. Newer versions of pytorch will do automatic
# type promtion and will generate float indices if `/` is used.
indices
=
indices
//
2
indices
=
torch
.
unique
(
indices
)
indices
=
torch
.
unique
(
indices
>>
1
)
left
=
self
.
_values
[
indices
*
2
]
right
=
self
.
_values
[
indices
*
2
+
1
]
self
.
_values
[
indices
]
=
op
(
left
,
right
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment