Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
5d6d14bc
P
Paddle
项目概览
Crayon鑫
/
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看板
未验证
提交
5d6d14bc
编写于
4月 04, 2022
作者:
W
wanghuancoder
提交者:
GitHub
4月 04, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[Eager] fix test_var_base (#41397)
* eager test var base * refine, test=develop
上级
afb56e8c
变更
8
展开全部
隐藏空白更改
内联
并排
Showing
8 changed file
with
360 addition
and
67 deletion
+360
-67
paddle/fluid/pybind/eager.cc
paddle/fluid/pybind/eager.cc
+4
-0
paddle/fluid/pybind/eager_method.cc
paddle/fluid/pybind/eager_method.cc
+50
-0
paddle/fluid/pybind/eager_properties.cc
paddle/fluid/pybind/eager_properties.cc
+14
-1
paddle/fluid/pybind/eager_utils.cc
paddle/fluid/pybind/eager_utils.cc
+6
-0
paddle/fluid/pybind/eager_utils.h
paddle/fluid/pybind/eager_utils.h
+1
-0
paddle/phi/api/lib/tensor.cc
paddle/phi/api/lib/tensor.cc
+5
-1
python/paddle/fluid/dygraph/varbase_patch_methods.py
python/paddle/fluid/dygraph/varbase_patch_methods.py
+5
-1
python/paddle/fluid/tests/unittests/test_var_base.py
python/paddle/fluid/tests/unittests/test_var_base.py
+275
-64
未找到文件。
paddle/fluid/pybind/eager.cc
浏览文件 @
5d6d14bc
...
...
@@ -78,6 +78,10 @@ void EmptyTensorInitializer(TensorObject* self, const std::string& name,
phi
::
DenseTensorMeta
(
paddle
::
framework
::
TransToPhiDataType
(
dtype
),
ddims
));
self
->
tensor
.
set_impl
(
dense_tensor
);
}
else
if
(
var_type
==
paddle
::
framework
::
proto
::
VarType
::
SELECTED_ROWS
)
{
std
::
shared_ptr
<
phi
::
SelectedRows
>
tensor
=
std
::
make_shared
<
phi
::
SelectedRows
>
();
self
->
tensor
.
set_impl
(
tensor
);
}
if
(
!
autograd_meta
->
GetMutableGradNode
())
{
...
...
paddle/fluid/pybind/eager_method.cc
浏览文件 @
5d6d14bc
...
...
@@ -465,6 +465,9 @@ static PyObject* tensor__share_buffer_to(TensorObject* self, PyObject* args,
self
->
tensor
.
name
()));
auto
*
src_tensor
=
static_cast
<
paddle
::
framework
::
Tensor
*>
(
self
->
tensor
.
impl
().
get
());
if
(
!
dst_ptr
->
defined
())
{
dst_ptr
->
set_impl
(
std
::
make_shared
<
phi
::
DenseTensor
>
());
}
auto
dst_tensor
=
static_cast
<
paddle
::
framework
::
Tensor
*>
(
dst_ptr
->
impl
().
get
());
dst_tensor
->
ShareDataWith
(
*
src_tensor
);
...
...
@@ -565,6 +568,10 @@ static PyObject* tensor_method_get_underline_tensor(TensorObject* self,
PyObject
*
args
,
PyObject
*
kwargs
)
{
EAGER_TRY
if
(
!
self
->
tensor
.
defined
())
{
Py_IncRef
(
Py_None
);
return
Py_None
;
}
if
(
self
->
tensor
.
is_dense_tensor
())
{
auto
*
tensor
=
static_cast
<
paddle
::
framework
::
LoDTensor
*>
(
self
->
tensor
.
impl
().
get
());
...
...
@@ -577,6 +584,25 @@ static PyObject* tensor_method_get_underline_tensor(TensorObject* self,
EAGER_CATCH_AND_THROW_RETURN_NULL
}
static
PyObject
*
tensor_method_get_underline_selected_rows
(
TensorObject
*
self
,
PyObject
*
args
,
PyObject
*
kwargs
)
{
EAGER_TRY
if
(
!
self
->
tensor
.
defined
())
{
Py_IncRef
(
Py_None
);
return
Py_None
;
}
if
(
self
->
tensor
.
is_selected_rows
())
{
auto
*
selected_rows
=
static_cast
<
phi
::
SelectedRows
*>
(
self
->
tensor
.
impl
().
get
());
return
ToPyObject
(
selected_rows
);
}
else
{
Py_IncRef
(
Py_None
);
return
Py_None
;
}
EAGER_CATCH_AND_THROW_RETURN_NULL
}
static
PyObject
*
tensor__getitem_index_not_tensor
(
TensorObject
*
self
,
PyObject
*
args
,
PyObject
*
kwargs
)
{
...
...
@@ -1214,6 +1240,9 @@ static PyObject* tensor_method_get_non_zero_cols(TensorObject* self,
static
PyObject
*
tensor_method_is_sparse
(
TensorObject
*
self
,
PyObject
*
args
,
PyObject
*
kwargs
)
{
EAGER_TRY
if
(
!
self
->
tensor
.
defined
())
{
return
ToPyObject
(
false
);
}
return
ToPyObject
(
self
->
tensor
.
is_sparse_coo_tensor
()
||
self
->
tensor
.
is_sparse_csr_tensor
());
EAGER_CATCH_AND_THROW_RETURN_NULL
...
...
@@ -1222,6 +1251,9 @@ static PyObject* tensor_method_is_sparse(TensorObject* self, PyObject* args,
static
PyObject
*
tensor_method_is_sparse_coo
(
TensorObject
*
self
,
PyObject
*
args
,
PyObject
*
kwargs
)
{
EAGER_TRY
if
(
!
self
->
tensor
.
defined
())
{
return
ToPyObject
(
false
);
}
return
ToPyObject
(
self
->
tensor
.
is_sparse_coo_tensor
());
EAGER_CATCH_AND_THROW_RETURN_NULL
}
...
...
@@ -1229,6 +1261,9 @@ static PyObject* tensor_method_is_sparse_coo(TensorObject* self, PyObject* args,
static
PyObject
*
tensor_method_is_sparse_csr
(
TensorObject
*
self
,
PyObject
*
args
,
PyObject
*
kwargs
)
{
EAGER_TRY
if
(
!
self
->
tensor
.
defined
())
{
return
ToPyObject
(
false
);
}
return
ToPyObject
(
self
->
tensor
.
is_sparse_csr_tensor
());
EAGER_CATCH_AND_THROW_RETURN_NULL
}
...
...
@@ -1307,6 +1342,9 @@ static PyObject* tensor_method_is_selected_rows(TensorObject* self,
PyObject
*
args
,
PyObject
*
kwargs
)
{
EAGER_TRY
if
(
!
self
->
tensor
.
defined
())
{
return
ToPyObject
(
false
);
}
return
ToPyObject
(
self
->
tensor
.
is_selected_rows
());
EAGER_CATCH_AND_THROW_RETURN_NULL
}
...
...
@@ -1323,6 +1361,13 @@ static PyObject* tensor_method_get_rows(TensorObject* self, PyObject* args,
EAGER_CATCH_AND_THROW_RETURN_NULL
}
static
PyObject
*
tensor_methon_element_size
(
TensorObject
*
self
,
PyObject
*
args
,
PyObject
*
kwargs
)
{
EAGER_TRY
return
ToPyObject
(
paddle
::
experimental
::
SizeOf
(
self
->
tensor
.
dtype
()));
EAGER_CATCH_AND_THROW_RETURN_NULL
}
static
PyObject
*
tensor__reset_grad_inplace_version
(
TensorObject
*
self
,
PyObject
*
args
,
PyObject
*
kwargs
)
{
...
...
@@ -1420,6 +1465,9 @@ PyMethodDef variable_methods[] = {
{
"get_tensor"
,
(
PyCFunction
)(
void
(
*
)(
void
))
tensor_method_get_underline_tensor
,
METH_VARARGS
|
METH_KEYWORDS
,
NULL
},
{
"get_selected_rows"
,
(
PyCFunction
)(
void
(
*
)(
void
))
tensor_method_get_underline_selected_rows
,
METH_VARARGS
|
METH_KEYWORDS
,
NULL
},
{
"_getitem_index_not_tensor"
,
(
PyCFunction
)(
void
(
*
)(
void
))
tensor__getitem_index_not_tensor
,
METH_VARARGS
|
METH_KEYWORDS
,
NULL
},
...
...
@@ -1482,6 +1530,8 @@ PyMethodDef variable_methods[] = {
METH_VARARGS
|
METH_KEYWORDS
,
NULL
},
{
"rows"
,
(
PyCFunction
)(
void
(
*
)(
void
))
tensor_method_get_rows
,
METH_VARARGS
|
METH_KEYWORDS
,
NULL
},
{
"element_size"
,
(
PyCFunction
)(
void
(
*
)(
void
))
tensor_methon_element_size
,
METH_VARARGS
|
METH_KEYWORDS
,
NULL
},
{
"_reset_grad_inplace_version"
,
(
PyCFunction
)(
void
(
*
)(
void
))
tensor__reset_grad_inplace_version
,
METH_VARARGS
|
METH_KEYWORDS
,
NULL
},
...
...
paddle/fluid/pybind/eager_properties.cc
浏览文件 @
5d6d14bc
...
...
@@ -43,8 +43,14 @@ PyObject* tensor_properties_get_name(TensorObject* self, void* closure) {
PyObject
*
tensor_properties_get_type
(
TensorObject
*
self
,
void
*
closure
)
{
EAGER_TRY
if
(
!
self
->
tensor
.
defined
())
{
// be same to old dygraph
return
ToPyObject
(
paddle
::
framework
::
proto
::
VarType
::
LOD_TENSOR
);
}
if
(
self
->
tensor
.
is_dense_tensor
())
{
return
ToPyObject
(
paddle
::
framework
::
proto
::
VarType
::
LOD_TENSOR
);
}
else
if
(
self
->
tensor
.
is_selected_rows
())
{
return
ToPyObject
(
paddle
::
framework
::
proto
::
VarType
::
SELECTED_ROWS
);
}
else
{
Py_INCREF
(
Py_None
);
return
Py_None
;
...
...
@@ -137,8 +143,11 @@ int tensor_properties_set_persistable(TensorObject* self, PyObject* value,
PyObject
*
tensor_properties_get_shape
(
TensorObject
*
self
,
void
*
closure
)
{
EAGER_TRY
auto
ddim
=
self
->
tensor
.
shape
();
std
::
vector
<
int64_t
>
value
;
if
(
!
self
->
tensor
.
defined
())
{
return
ToPyObject
(
value
);
}
auto
ddim
=
self
->
tensor
.
shape
();
size_t
rank
=
static_cast
<
size_t
>
(
ddim
.
size
());
value
.
resize
(
rank
);
for
(
size_t
i
=
0
;
i
<
rank
;
i
++
)
{
...
...
@@ -165,6 +174,10 @@ PyObject* tensor_properties_get_place_str(TensorObject* self, void* closure) {
PyObject
*
tensor_properties_get_dtype
(
TensorObject
*
self
,
void
*
closure
)
{
EAGER_TRY
if
(
!
self
->
tensor
.
defined
())
{
// be same to old dygraph
return
ToPyObject
(
framework
::
proto
::
VarType
::
FP32
);
}
return
ToPyObject
(
paddle
::
framework
::
TransToProtoVarType
(
self
->
tensor
.
type
()));
EAGER_CATCH_AND_THROW_RETURN_NULL
...
...
paddle/fluid/pybind/eager_utils.cc
浏览文件 @
5d6d14bc
...
...
@@ -577,6 +577,12 @@ PyObject* ToPyObject(const paddle::framework::LoDTensor* value) {
return
obj
.
ptr
();
}
PyObject
*
ToPyObject
(
const
phi
::
SelectedRows
*
value
)
{
auto
obj
=
::
pybind11
::
cast
(
value
,
py
::
return_value_policy
::
reference
);
obj
.
inc_ref
();
return
obj
.
ptr
();
}
PyObject
*
ToPyObject
(
const
void
*
value
)
{
if
(
value
==
nullptr
)
{
Py_INCREF
(
Py_None
);
...
...
paddle/fluid/pybind/eager_utils.h
浏览文件 @
5d6d14bc
...
...
@@ -75,6 +75,7 @@ PyObject* ToPyObject(const std::vector<paddle::experimental::Tensor>& value,
bool
return_py_none_if_not_initialize
=
false
);
PyObject
*
ToPyObject
(
const
platform
::
Place
&
value
);
PyObject
*
ToPyObject
(
const
framework
::
LoDTensor
*
value
);
PyObject
*
ToPyObject
(
const
phi
::
SelectedRows
*
value
);
PyObject
*
ToPyObject
(
const
paddle
::
framework
::
proto
::
VarType
::
Type
&
dtype
);
PyObject
*
ToPyObject
(
const
paddle
::
framework
::
proto
::
VarType
&
type
);
PyObject
*
ToPyObject
(
const
void
*
value
);
...
...
paddle/phi/api/lib/tensor.cc
浏览文件 @
5d6d14bc
...
...
@@ -101,7 +101,11 @@ int64_t Tensor::size() const { return impl_->numel(); }
phi
::
DDim
Tensor
::
dims
()
const
{
return
impl_
->
dims
();
}
std
::
vector
<
int64_t
>
Tensor
::
shape
()
const
{
return
phi
::
vectorize
<
int64_t
>
(
impl_
->
dims
());
auto
dims
=
impl_
->
dims
();
if
(
dims
.
size
()
==
1
&&
dims
.
at
(
0
)
==
0
)
{
return
{};
}
return
phi
::
vectorize
<
int64_t
>
(
dims
);
}
void
Tensor
::
reshape
(
const
std
::
vector
<
int64_t
>
&
shape
)
{
...
...
python/paddle/fluid/dygraph/varbase_patch_methods.py
浏览文件 @
5d6d14bc
...
...
@@ -846,7 +846,11 @@ def monkey_patch_varbase():
return
res
@
framework
.
dygraph_only
def
cuda
(
self
,
device_id
,
blocking
):
def
cuda
(
self
,
device_id
=
0
,
blocking
=
True
):
if
device_id
is
None
:
device_id
=
0
if
not
isinstance
(
device_id
,
int
):
raise
ValueError
(
"
\'
device_id
\'
must be a positive integer"
)
if
self
.
place
.
is_gpu_place
():
return
self
else
:
...
...
python/paddle/fluid/tests/unittests/test_var_base.py
浏览文件 @
5d6d14bc
此差异已折叠。
点击以展开。
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录