Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
cb8afc24
P
Paddle
项目概览
机器未来
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
cb8afc24
编写于
3月 30, 2022
作者:
P
pangyoki
提交者:
GitHub
3月 30, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add _reset_grad_inplace_version (#41101)
上级
a5bfa797
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
59 addition
and
3 deletion
+59
-3
paddle/fluid/pybind/eager_method.cc
paddle/fluid/pybind/eager_method.cc
+24
-0
paddle/phi/api/include/tensor.h
paddle/phi/api/include/tensor.h
+5
-0
paddle/phi/api/lib/tensor.cc
paddle/phi/api/lib/tensor.cc
+11
-0
python/paddle/fluid/tests/unittests/test_reset_grad_inplace_version.py
.../fluid/tests/unittests/test_reset_grad_inplace_version.py
+19
-3
未找到文件。
paddle/fluid/pybind/eager_method.cc
浏览文件 @
cb8afc24
...
@@ -1308,6 +1308,27 @@ static PyObject* tensor_method_get_rows(TensorObject* self, PyObject* args,
...
@@ -1308,6 +1308,27 @@ static PyObject* tensor_method_get_rows(TensorObject* self, PyObject* args,
EAGER_CATCH_AND_THROW_RETURN_NULL
EAGER_CATCH_AND_THROW_RETURN_NULL
}
}
static
PyObject
*
tensor__reset_grad_inplace_version
(
TensorObject
*
self
,
PyObject
*
args
,
PyObject
*
kwargs
)
{
EAGER_TRY
Py_ssize_t
args_num
=
PyTuple_Size
(
args
);
bool
set_to_zero
=
true
;
if
(
args_num
==
(
Py_ssize_t
)
1
)
{
set_to_zero
=
CastPyArg2AttrBoolean
(
PyTuple_GET_ITEM
(
args
,
0
),
0
);
}
paddle
::
experimental
::
Tensor
*
grad
=
egr
::
EagerUtils
::
mutable_grad
(
self
->
tensor
);
if
(
grad
&&
grad
->
defined
()
&&
grad
->
is_dense_tensor
()
&&
grad
->
initialized
())
{
grad
->
reset_inplace_version
(
set_to_zero
);
}
Py_INCREF
(
Py_None
);
return
Py_None
;
EAGER_CATCH_AND_THROW_RETURN_NULL
}
PyMethodDef
variable_methods
[]
=
{
PyMethodDef
variable_methods
[]
=
{
{
"numpy"
,
(
PyCFunction
)(
void
(
*
)(
void
))
tensor_method_numpy
,
{
"numpy"
,
(
PyCFunction
)(
void
(
*
)(
void
))
tensor_method_numpy
,
METH_VARARGS
|
METH_KEYWORDS
,
NULL
},
METH_VARARGS
|
METH_KEYWORDS
,
NULL
},
...
@@ -1407,6 +1428,9 @@ PyMethodDef variable_methods[] = {
...
@@ -1407,6 +1428,9 @@ PyMethodDef variable_methods[] = {
METH_VARARGS
|
METH_KEYWORDS
,
NULL
},
METH_VARARGS
|
METH_KEYWORDS
,
NULL
},
{
"rows"
,
(
PyCFunction
)(
void
(
*
)(
void
))
tensor_method_get_rows
,
{
"rows"
,
(
PyCFunction
)(
void
(
*
)(
void
))
tensor_method_get_rows
,
METH_VARARGS
|
METH_KEYWORDS
,
NULL
},
METH_VARARGS
|
METH_KEYWORDS
,
NULL
},
{
"_reset_grad_inplace_version"
,
(
PyCFunction
)(
void
(
*
)(
void
))
tensor__reset_grad_inplace_version
,
METH_VARARGS
|
METH_KEYWORDS
,
NULL
},
{
NULL
,
NULL
,
0
,
NULL
}};
{
NULL
,
NULL
,
0
,
NULL
}};
}
// namespace pybind
}
// namespace pybind
...
...
paddle/phi/api/include/tensor.h
浏览文件 @
cb8afc24
...
@@ -516,6 +516,11 @@ class PADDLE_API Tensor final {
...
@@ -516,6 +516,11 @@ class PADDLE_API Tensor final {
*/
*/
uint32_t
current_inplace_version
();
uint32_t
current_inplace_version
();
/**
* @brief Reset inplace version
*/
void
reset_inplace_version
(
bool
set_to_zero
=
false
);
/* Part 10: Auto generated Tensor methods */
/* Part 10: Auto generated Tensor methods */
/* Part 11: Methods of converting SparseTensor and DenseTensor to each other
/* Part 11: Methods of converting SparseTensor and DenseTensor to each other
...
...
paddle/phi/api/lib/tensor.cc
浏览文件 @
cb8afc24
...
@@ -384,5 +384,16 @@ uint32_t Tensor::current_inplace_version() {
...
@@ -384,5 +384,16 @@ uint32_t Tensor::current_inplace_version() {
return
0
;
return
0
;
}
}
void
Tensor
::
reset_inplace_version
(
bool
set_to_zero
)
{
if
(
set_to_zero
)
{
if
(
is_dense_tensor
())
{
auto
&
inplace_version_counter
=
std
::
dynamic_pointer_cast
<
phi
::
DenseTensor
>
(
impl_
)
->
InplaceVersionCounter
();
inplace_version_counter
.
SetInplaceVersionToZero
();
}
}
}
}
// namespace experimental
}
// namespace experimental
}
// namespace paddle
}
// namespace paddle
python/paddle/fluid/tests/unittests/test_reset_grad_inplace_version.py
浏览文件 @
cb8afc24
...
@@ -16,6 +16,7 @@ import paddle
...
@@ -16,6 +16,7 @@ import paddle
import
paddle.fluid
as
fluid
import
paddle.fluid
as
fluid
from
paddle
import
_C_ops
from
paddle
import
_C_ops
from
paddle.fluid
import
framework
from
paddle.fluid
import
framework
from
paddle.fluid.framework
import
_test_eager_guard
import
unittest
import
unittest
paddle
.
set_device
(
'cpu'
)
paddle
.
set_device
(
'cpu'
)
...
@@ -32,7 +33,7 @@ def clear_grad_test_0(w, a):
...
@@ -32,7 +33,7 @@ def clear_grad_test_0(w, a):
class
TestInplaceAndClearGradient
(
unittest
.
TestCase
):
class
TestInplaceAndClearGradient
(
unittest
.
TestCase
):
def
test
(
self
):
def
func_
test
(
self
):
input_data
=
np
.
ones
([
1
,
1
])
input_data
=
np
.
ones
([
1
,
1
])
w
=
paddle
.
to_tensor
(
input_data
,
'float32'
,
stop_gradient
=
False
)
w
=
paddle
.
to_tensor
(
input_data
,
'float32'
,
stop_gradient
=
False
)
...
@@ -45,6 +46,11 @@ class TestInplaceAndClearGradient(unittest.TestCase):
...
@@ -45,6 +46,11 @@ class TestInplaceAndClearGradient(unittest.TestCase):
out
.
backward
()
out
.
backward
()
assert
w
.
grad
[
0
]
==
0.15
assert
w
.
grad
[
0
]
==
0.15
def
test
(
self
):
with
_test_eager_guard
():
self
.
func_test
()
self
.
func_test
()
# Test 2
# Test 2
class
Counter
:
class
Counter
:
...
@@ -67,7 +73,7 @@ def clear_grad_test_1(w, c):
...
@@ -67,7 +73,7 @@ def clear_grad_test_1(w, c):
class
TestInplaceClearGradAccumulation
(
unittest
.
TestCase
):
class
TestInplaceClearGradAccumulation
(
unittest
.
TestCase
):
def
test
(
self
):
def
func_
test
(
self
):
input_data
=
np
.
ones
([
1
,
1
])
input_data
=
np
.
ones
([
1
,
1
])
w
=
paddle
.
to_tensor
(
input_data
,
'float32'
,
stop_gradient
=
False
)
w
=
paddle
.
to_tensor
(
input_data
,
'float32'
,
stop_gradient
=
False
)
c
=
Counter
()
c
=
Counter
()
...
@@ -87,9 +93,14 @@ class TestInplaceClearGradAccumulation(unittest.TestCase):
...
@@ -87,9 +93,14 @@ class TestInplaceClearGradAccumulation(unittest.TestCase):
assert
c
.
num_calls
==
1
assert
c
.
num_calls
==
1
c
.
num_calls
=
0
c
.
num_calls
=
0
def
test
(
self
):
with
_test_eager_guard
():
self
.
func_test
()
self
.
func_test
()
class
TestInplaceClearGradAccumulationAlt
(
unittest
.
TestCase
):
class
TestInplaceClearGradAccumulationAlt
(
unittest
.
TestCase
):
def
test
(
self
):
def
func_
test
(
self
):
input_data
=
np
.
ones
([
1
,
1
])
input_data
=
np
.
ones
([
1
,
1
])
w
=
paddle
.
to_tensor
(
input_data
,
'float32'
,
stop_gradient
=
False
)
w
=
paddle
.
to_tensor
(
input_data
,
'float32'
,
stop_gradient
=
False
)
out
=
_C_ops
.
scale
(
w
,
'scale'
,
0.1
)
out
=
_C_ops
.
scale
(
w
,
'scale'
,
0.1
)
...
@@ -100,6 +111,11 @@ class TestInplaceClearGradAccumulationAlt(unittest.TestCase):
...
@@ -100,6 +111,11 @@ class TestInplaceClearGradAccumulationAlt(unittest.TestCase):
assert
w
.
grad
.
_inplace_version
()
==
1
assert
w
.
grad
.
_inplace_version
()
==
1
def
test
(
self
):
with
_test_eager_guard
():
self
.
func_test
()
self
.
func_test
()
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
unittest
.
main
()
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录