Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
8ddf51ff
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2299
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
8ddf51ff
编写于
8月 03, 2023
作者:
X
xiaoguoguo626807
提交者:
GitHub
8月 03, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add eq and hash (#55909)
上级
275a8102
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
59 addition
and
20 deletion
+59
-20
paddle/fluid/pybind/ir.cc
paddle/fluid/pybind/ir.cc
+28
-13
paddle/ir/core/value.cc
paddle/ir/core/value.cc
+9
-0
paddle/ir/core/value.h
paddle/ir/core/value.h
+5
-0
test/ir/new_ir/test_ir_pybind.py
test/ir/new_ir/test_ir_pybind.py
+17
-7
未找到文件。
paddle/fluid/pybind/ir.cc
浏览文件 @
8ddf51ff
...
...
@@ -176,7 +176,13 @@ void BindValue(py::module *m) {
.
def
(
"get_defining_op"
,
&
Value
::
GetDefiningOp
,
return_value_policy
::
reference
)
.
def
(
"__eq__"
,
&
Value
::
operator
==
);
.
def
(
"__eq__"
,
&
Value
::
operator
==
)
.
def
(
"__eq__"
,
[](
Value
&
self
,
OpResult
&
other
)
{
return
self
.
impl
()
==
other
.
value_impl
();
})
.
def
(
"__hash__"
,
[](
const
Value
&
self
)
{
return
std
::
hash
<
ir
::
Value
>
{}(
self
);
});
}
void
BindOpOperand
(
py
::
module
*
m
)
{
...
...
@@ -218,18 +224,27 @@ void BindOpResult(py::module *m) {
ir
::
ArrayAttribute
::
get
(
ir
::
IrContext
::
Instance
(),
stop_gradients
));
})
.
def
(
"get_stop_gradient"
,
[](
OpResult
&
self
)
{
auto
*
defining_op
=
self
.
owner
();
if
(
defining_op
->
HasAttribute
(
kAttrStopGradients
))
{
auto
stop_gradients
=
defining_op
->
attribute
(
kAttrStopGradients
)
.
dyn_cast
<
ir
::
ArrayAttribute
>
()
.
AsVector
();
return
stop_gradients
[
self
.
GetResultIndex
()]
.
dyn_cast
<
ir
::
BoolAttribute
>
()
.
data
();
}
else
{
return
false
;
}
.
def
(
"get_stop_gradient"
,
[](
OpResult
&
self
)
{
auto
*
defining_op
=
self
.
owner
();
if
(
defining_op
->
HasAttribute
(
kAttrStopGradients
))
{
auto
stop_gradients
=
defining_op
->
attribute
(
kAttrStopGradients
)
.
dyn_cast
<
ir
::
ArrayAttribute
>
()
.
AsVector
();
return
stop_gradients
[
self
.
GetResultIndex
()]
.
dyn_cast
<
ir
::
BoolAttribute
>
()
.
data
();
}
else
{
return
false
;
}
})
.
def
(
"__eq__"
,
&
OpResult
::
operator
==
)
.
def
(
"__eq__"
,
[](
OpResult
&
self
,
Value
&
other
)
{
return
self
.
value_impl
()
==
other
.
impl
();
})
.
def
(
"__hash__"
,
[](
OpResult
&
self
)
{
return
std
::
hash
<
ir
::
Value
>
{}(
self
.
dyn_cast
<
ir
::
Value
>
());
});
}
...
...
paddle/ir/core/value.cc
浏览文件 @
8ddf51ff
...
...
@@ -122,6 +122,15 @@ detail::OpResultImpl *OpResult::impl() const {
return
reinterpret_cast
<
detail
::
OpResultImpl
*>
(
impl_
);
}
bool
OpResult
::
operator
==
(
const
OpResult
&
other
)
const
{
return
impl_
==
other
.
impl_
;
}
detail
::
ValueImpl
*
OpResult
::
value_impl
()
const
{
IR_ENFORCE
(
impl_
,
"Can't use value_impl() interface while value is null."
);
return
impl_
;
}
uint32_t
OpResult
::
GetValidInlineIndex
(
uint32_t
index
)
{
uint32_t
max_inline_index
=
ir
::
detail
::
OpResultImpl
::
GetMaxInlineResultIndex
();
...
...
paddle/ir/core/value.h
浏览文件 @
8ddf51ff
...
...
@@ -192,8 +192,12 @@ class IR_API OpResult : public Value {
uint32_t
GetResultIndex
()
const
;
bool
operator
==
(
const
OpResult
&
other
)
const
;
friend
Operation
;
detail
::
ValueImpl
*
value_impl
()
const
;
private:
static
uint32_t
GetValidInlineIndex
(
uint32_t
index
);
...
...
@@ -209,4 +213,5 @@ struct hash<ir::Value> {
return
std
::
hash
<
const
ir
::
detail
::
ValueImpl
*>
()(
obj
.
impl_
);
}
};
}
// namespace std
test/ir/new_ir/test_ir_pybind.py
浏览文件 @
8ddf51ff
...
...
@@ -84,12 +84,26 @@ class TestPybind(unittest.TestCase):
matmul_op
.
result
(
0
).
set_stop_gradient
(
True
)
self
.
assertEqual
(
matmul_op
.
result
(
0
).
get_stop_gradient
(),
True
)
# test opresult hash
result_set
=
set
()
for
opresult
in
matmul_op
.
results
():
result_set
.
add
(
opresult
)
# self.assertTrue(add_op.operands()[0].source() in result_set)
# self.assertEqual(add_op.operands_source()[0] , matmul_op.results()[0],)
# test opresult hash and hash(opresult) == hash(operesult)
self
.
assertTrue
(
add_op
.
operands
()[
0
].
source
()
in
result_set
)
# test value hash and hash(value) == hash(operesult)
self
.
assertTrue
(
add_op
.
operands_source
()[
0
]
in
result_set
)
# test value == value
self
.
assertEqual
(
add_op
.
operands_source
()[
0
],
add_op
.
operands_source
()[
0
]
)
# test value == opresult
self
.
assertEqual
(
add_op
.
operands_source
()[
0
],
matmul_op
.
results
()[
0
])
# test opresult == value
self
.
assertEqual
(
add_op
.
operands
()[
0
].
source
(),
add_op
.
operands_source
()[
0
]
)
# test opresult == opresult
self
.
assertEqual
(
add_op
.
operands
()[
0
].
source
(),
matmul_op
.
results
()[
0
])
self
.
assertEqual
(
tanh_op
.
operands
()[
0
].
source
().
get_defining_op
().
name
(),
"pd.add"
...
...
@@ -100,10 +114,6 @@ class TestPybind(unittest.TestCase):
tanh_op
.
operands
()[
0
].
source
().
get_defining_op
().
name
(),
"pd.matmul"
)
self
.
assertEqual
(
tanh_op
.
operands
()[
0
].
source
().
get_defining_op
(),
tanh_op
.
operands_source
()[
0
].
get_defining_op
(),
)
self
.
assertEqual
(
add_op
.
result
(
0
).
use_empty
(),
True
)
def
test_type
(
self
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录