Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenDILab开源决策智能平台
DI-engine
提交
3de05532
D
DI-engine
项目概览
OpenDILab开源决策智能平台
/
DI-engine
上一次同步 接近 3 年
通知
66
Star
322
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
1
Wiki
分析
仓库
DevOps
项目成员
Pages
D
DI-engine
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
1
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
3de05532
编写于
11月 01, 2021
作者:
N
niuyazhe
提交者:
Xu Jingxin
11月 05, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add naive use time count middleware in buffer
上级
368f2c26
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
59 addition
and
3 deletion
+59
-3
ding/worker/buffer/middleware/__init__.py
ding/worker/buffer/middleware/__init__.py
+1
-0
ding/worker/buffer/middleware/use_time_check.py
ding/worker/buffer/middleware/use_time_check.py
+36
-0
ding/worker/buffer/tests/test_middleware.py
ding/worker/buffer/tests/test_middleware.py
+22
-3
未找到文件。
ding/worker/buffer/middleware/__init__.py
浏览文件 @
3de05532
from
.clone_object
import
clone_object
from
.clone_object
import
clone_object
from
.use_time_check
import
use_time_check
ding/worker/buffer/middleware/use_time_check.py
0 → 100644
浏览文件 @
3de05532
from
typing
import
Callable
,
Any
,
List
from
collections
import
deque
def
use_time_check
(
max_use
:
int
=
float
(
"inf"
))
->
Callable
:
"""
Overview:
This middleware aims to check the usage times of data in buffer. If the usage times of a data is
greater than max_use, this data will be removed from buffer as soon as possible.
"""
def
push
(
next
:
Callable
,
data
:
Any
,
*
args
,
**
kwargs
)
->
None
:
if
'meta'
in
kwargs
:
kwargs
[
'meta'
][
'use_count'
]
=
0
else
:
kwargs
[
'meta'
]
=
{
'use_count'
:
0
}
return
next
(
data
,
*
args
,
**
kwargs
)
def
sample
(
next
:
Callable
,
*
args
,
**
kwargs
)
->
List
[
Any
]:
kwargs
[
'return_index'
]
=
True
kwargs
[
'return_meta'
]
=
True
data
=
next
(
*
args
,
**
kwargs
)
for
i
,
(
d
,
idx
,
meta
)
in
enumerate
(
data
):
meta
[
'use_count'
]
+=
1
if
meta
[
'use_count'
]
>=
max_use
:
print
(
'max_use trigger'
)
# TODO(nyz)
return
data
def
_immutable_object
(
action
:
str
,
next
:
Callable
,
*
args
,
**
kwargs
)
->
Any
:
if
action
==
"push"
:
return
push
(
next
,
*
args
,
**
kwargs
)
elif
action
==
"sample"
:
return
sample
(
next
,
*
args
,
**
kwargs
)
return
next
(
*
args
,
**
kwargs
)
return
_immutable_object
ding/worker/buffer/tests/test_middleware.py
浏览文件 @
3de05532
import
pytest
import
pytest
import
torch
import
torch
from
ding.worker.buffer
import
Buffer
,
Memory
Storage
from
ding.worker.buffer
import
Buffer
,
Deque
Storage
from
ding.worker.buffer.middleware
import
clone_object
from
ding.worker.buffer.middleware
import
clone_object
,
use_time_check
@
pytest
.
mark
.
unittest
@
pytest
.
mark
.
unittest
def
test_clone_object
():
def
test_clone_object
():
buffer
=
Buffer
(
Memory
Storage
(
maxlen
=
10
)).
use
(
clone_object
())
buffer
=
Buffer
(
Deque
Storage
(
maxlen
=
10
)).
use
(
clone_object
())
# Store a dict, a list, a tensor
# Store a dict, a list, a tensor
arr
=
[{
"key"
:
"v1"
},
[
"a"
],
torch
.
Tensor
([
1
,
2
,
3
])]
arr
=
[{
"key"
:
"v1"
},
[
"a"
],
torch
.
Tensor
([
1
,
2
,
3
])]
...
@@ -34,3 +34,22 @@ def test_clone_object():
...
@@ -34,3 +34,22 @@ def test_clone_object():
assert
item
[
0
]
==
1
assert
item
[
0
]
==
1
else
:
else
:
raise
Exception
(
"Unexpected type"
)
raise
Exception
(
"Unexpected type"
)
@
pytest
.
mark
.
tmp
def
test_use_time_check
():
def
get_data
():
return
{
'obs'
:
torch
.
randn
(
4
),
'reward'
:
torch
.
randn
(
1
),
'info'
:
'xxx'
}
N
=
6
buffer
=
Buffer
(
DequeStorage
(
maxlen
=
10
)).
use
(
use_time_check
(
max_use
=
2
))
for
_
in
range
(
N
):
buffer
.
push
(
get_data
())
for
i
in
range
(
2
):
data
=
buffer
.
sample
(
size
=
N
,
replace
=
False
)
assert
len
(
data
)
==
N
print
(
'sample i'
)
buffer
.
sample
(
size
=
6
,
replace
=
False
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录