Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
19228bd1
P
Paddle
项目概览
PaddlePaddle
/
Paddle
12 个月 前同步成功
通知
2292
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看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
19228bd1
编写于
9月 11, 2020
作者:
L
Leo Chen
提交者:
GitHub
9月 11, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Temporally disable zero_copy (#27248)
* temporally disable zero_copy * add test * follow comments
上级
f402d8d8
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
32 addition
and
15 deletion
+32
-15
python/paddle/fluid/dygraph/base.py
python/paddle/fluid/dygraph/base.py
+14
-4
python/paddle/fluid/tests/unittests/test_imperative_numpy_bridge.py
...dle/fluid/tests/unittests/test_imperative_numpy_bridge.py
+14
-6
python/paddle/tensor/creation.py
python/paddle/tensor/creation.py
+4
-5
未找到文件。
python/paddle/fluid/dygraph/base.py
浏览文件 @
19228bd1
...
...
@@ -25,6 +25,7 @@ from .tracer import Tracer
import
logging
import
objgraph
from
..data_feeder
import
convert_dtype
import
warnings
__all__
=
[
'no_grad'
,
'no_grad_'
,
'grad'
,
'guard'
,
'enable_dygraph'
,
'disable_dygraph'
,
...
...
@@ -609,10 +610,10 @@ def to_variable(value, name=None, zero_copy=None, dtype=None):
uint8, uint16, complex64, complex128}.
name(str, optional): The default value is None. Normally there is no
need for user to set this property. For more information, please
refer to :ref:`api_guide_Name` .
refer to :ref:`api_guide_Name` .
zero_copy(bool, optional): Whether to share memory with the input numpy
array. This parameter only works with CPUPlace and will be set to
True when it is None. Default: None.
True when it is None. Default: None.
(Note: zero_copy is discarded temporally for some reason.)
dtype(str, optional): The desired data type of returned ``Variable`` .
Can be 'bool' , 'float16' , 'float32' , 'float64' , 'int8' , 'int16' ,
'int32' , 'int64' , 'uint8' . Default: None.
...
...
@@ -665,8 +666,17 @@ def to_variable(value, name=None, zero_copy=None, dtype=None):
else
:
if
isinstance
(
framework
.
_current_expected_place
(),
framework
.
core
.
CPUPlace
):
if
zero_copy
is
None
:
zero_copy
=
True
#TODO(zhiqiu): we found two problems when enable zero_copy on CPUPlace.
# (1): eigen requires 16-bytes alignments, but the data of numpy array may not statisfy.
# Details: https://eigen.tuxfamily.org/dox/group__TopicUnalignedArrayAssert.html
# (2): when used in flask framework, it may result in hang.
# Details: https://github.com/PaddlePaddle/Paddle/issues/26635
# So, we temporally diable the zero_copy strategy.
if
zero_copy
==
True
:
warnings
.
warn
(
"Currently, zero_copy is not supported, and it will be discarded."
)
zero_copy
=
False
else
:
assert
not
zero_copy
,
"zero_copy mode can only be used with CPUPlace"
...
...
python/paddle/fluid/tests/unittests/test_imperative_numpy_bridge.py
浏览文件 @
19228bd1
...
...
@@ -15,18 +15,26 @@
import
unittest
import
numpy
as
np
import
paddle.fluid
as
fluid
import
warnings
class
TestImperativeNumpyBridge
(
unittest
.
TestCase
):
def
test_tensor_from_numpy
(
self
):
data_np
=
np
.
array
([[
2
,
3
,
1
]]).
astype
(
'float32'
)
with
fluid
.
dygraph
.
guard
(
fluid
.
CPUPlace
()):
var
=
fluid
.
dygraph
.
to_variable
(
data_np
,
zero_copy
=
True
)
self
.
assertTrue
(
np
.
array_equal
(
var
.
numpy
(),
data_np
))
data_np
[
0
][
0
]
=
4
self
.
assertEqual
(
data_np
[
0
][
0
],
4
)
self
.
assertEqual
(
var
[
0
][
0
].
numpy
()[
0
],
4
)
self
.
assertTrue
(
np
.
array_equal
(
var
.
numpy
(),
data_np
))
with
warnings
.
catch_warnings
(
record
=
True
)
as
w
:
warnings
.
simplefilter
(
"always"
)
var
=
fluid
.
dygraph
.
to_variable
(
data_np
,
zero_copy
=
True
)
assert
"Currently, zero_copy is not supported, and it will be discarded."
in
str
(
w
[
-
1
].
message
)
# Temporally diable zero_copy
# var = fluid.dygraph.to_variable(data_np, zero_copy=True)
# self.assertTrue(np.array_equal(var.numpy(), data_np))
# data_np[0][0] = 4
# self.assertEqual(data_np[0][0], 4)
# self.assertEqual(var[0][0].numpy()[0], 4)
# self.assertTrue(np.array_equal(var.numpy(), data_np))
var2
=
fluid
.
dygraph
.
to_variable
(
data_np
,
zero_copy
=
False
)
self
.
assertTrue
(
np
.
array_equal
(
var2
.
numpy
(),
data_np
))
data_np
[
0
][
0
]
=
-
1
...
...
python/paddle/tensor/creation.py
浏览文件 @
19228bd1
...
...
@@ -63,8 +63,7 @@ def to_tensor(data, dtype=None, place=None, stop_gradient=True):
If the ``data`` is already a tensor, and ``dtype`` or ``place`` does't change, no copy
will be performed and return origin tensor, otherwise a new tensor will be constructed
and returned. Similarly, if the data is an numpy\.ndarray of with the same ``dtype``
and the current place is cpu, no copy will be performed.
and returned.
The ``ComplexTensor`` is a unique type of paddle. If x is ``ComplexTensor``, then
``x.real`` is the real part, and ``x.imag`` is the imaginary part.
...
...
@@ -209,20 +208,20 @@ def to_tensor(data, dtype=None, place=None, stop_gradient=True):
value
=
data
,
place
=
place
,
persistable
=
False
,
zero_copy
=
Tru
e
,
zero_copy
=
Fals
e
,
stop_gradient
=
stop_gradient
)
else
:
name
=
unique_name
.
generate
(
'generated_tensor'
)
real_tensor
=
paddle
.
Tensor
(
value
=
data
.
real
,
place
=
place
,
zero_copy
=
Tru
e
,
zero_copy
=
Fals
e
,
name
=
name
+
".real"
,
stop_gradient
=
stop_gradient
)
imag_tensor
=
paddle
.
Tensor
(
value
=
data
.
imag
,
place
=
place
,
zero_copy
=
Tru
e
,
zero_copy
=
Fals
e
,
name
=
name
+
".imag"
,
stop_gradient
=
stop_gradient
)
return
paddle
.
ComplexTensor
(
real_tensor
,
imag_tensor
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录