Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
99d30bfc
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
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看板
未验证
提交
99d30bfc
编写于
4月 01, 2020
作者:
S
songyouwei
提交者:
GitHub
4月 01, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
speedup slice impl (#23340)
test=develop
上级
1a6ce8b9
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
71 addition
and
64 deletion
+71
-64
paddle/fluid/pybind/imperative.cc
paddle/fluid/pybind/imperative.cc
+71
-64
未找到文件。
paddle/fluid/pybind/imperative.cc
浏览文件 @
99d30bfc
...
...
@@ -217,6 +217,68 @@ static imperative::NameVarBaseMap ConvertToNameVarBaseMap(
return
result
;
}
static
void
ParseIndexingSlice
(
framework
::
LoDTensor
*
tensor
,
PyObject
*
_index
,
std
::
vector
<
int
>
*
slice_axes
,
std
::
vector
<
int
>
*
slice_starts
,
std
::
vector
<
int
>
*
slice_ends
,
std
::
vector
<
int
>
*
slice_strides
,
std
::
vector
<
int
>
*
decrease_axis
,
std
::
vector
<
int
>
*
infer_flags
)
{
// We allow indexing by Integers, Slices, and tuples of those
// types.
// Ellipsis and None are not supported yet.
// wrap to tuple
PyObject
*
index
=
!
PyTuple_Check
(
_index
)
?
PyTuple_Pack
(
1
,
_index
)
:
_index
;
PADDLE_ENFORCE_EQ
(
tensor
->
IsInitialized
(),
true
,
platform
::
errors
::
InvalidArgument
(
"tensor has not been initialized"
));
const
auto
&
shape
=
tensor
->
dims
();
const
int
rank
=
shape
.
size
();
const
int
size
=
PyTuple_GET_SIZE
(
index
);
PADDLE_ENFORCE_EQ
(
size
<=
rank
,
true
,
platform
::
errors
::
InvalidArgument
(
"too many indices (%d) for tensor of dimension %d"
,
size
,
rank
));
for
(
int
dim
=
0
;
dim
<
size
;
++
dim
)
{
PyObject
*
slice_item
=
PyTuple_GetItem
(
index
,
dim
);
PADDLE_ENFORCE_EQ
(
PyNumber_Check
(
slice_item
)
||
PySlice_Check
(
slice_item
),
true
,
platform
::
errors
::
InvalidArgument
(
"We allow indexing by Integers, Slices, and tuples of "
"these types, but received %s in %dth slice item"
,
std
::
string
(
Py_TYPE
(
slice_item
)
->
tp_name
),
dim
+
1
));
infer_flags
->
push_back
(
1
);
int
dim_len
=
shape
[
dim
];
if
(
PyNumber_Check
(
slice_item
))
{
// integer
int
start
=
static_cast
<
int
>
(
PyLong_AsLong
(
slice_item
));
start
=
start
<
0
?
start
+
dim_len
:
start
;
slice_axes
->
push_back
(
dim
);
slice_starts
->
push_back
(
start
);
slice_ends
->
push_back
(
start
+
1
);
slice_strides
->
push_back
(
1
);
decrease_axis
->
push_back
(
dim
);
}
else
{
// slice
Py_ssize_t
start
,
end
,
step
;
// The parameter type for the slice parameter was PySliceObject* before 3.2
#if PY_VERSION_HEX >= 0x03020000
PySlice_GetIndices
(
slice_item
,
dim_len
,
&
start
,
&
end
,
&
step
);
#else
PySlice_GetIndices
(
reinterpret_cast
<
PySliceObject
*>
(
slice_item
),
dim_len
,
&
start
,
&
end
,
&
step
);
#endif
// :: or : or 0:dim_len:1
if
(
start
==
0
&&
end
==
dim_len
&&
step
==
1
)
continue
;
slice_axes
->
push_back
(
dim
);
slice_starts
->
push_back
(
start
);
slice_ends
->
push_back
(
end
);
slice_strides
->
push_back
(
step
);
}
}
if
(
!
PyTuple_Check
(
_index
))
Py_DecRef
(
index
);
}
// Bind Methods
void
BindImperative
(
py
::
module
*
m_ptr
)
{
auto
&
m
=
*
m_ptr
;
...
...
@@ -396,77 +458,22 @@ void BindImperative(py::module *m_ptr) {
.
def
(
"__init__"
,
&
InitVarBaseFromNumpyWithArgDefault
,
py
::
arg
(
"value"
))
.
def
(
"__init__"
,
&
InitVarBaseFromNumpyWithKwargs
)
.
def
(
"__getitem__"
,
[](
imperative
::
VarBase
&
self
,
py
::
handle
_index
)
{
// We allow indexing by Integers, Slices, and tuples of those
// types.
// Ellipsis and None are not supported yet.
[](
std
::
shared_ptr
<
imperative
::
VarBase
>
&
self
,
py
::
handle
_index
)
{
std
::
vector
<
int
>
slice_axes
,
slice_starts
,
slice_ends
,
slice_strides
,
decrease_axis
;
// wrap to tuple
PyObject
*
index
=
!
PyTuple_Check
(
_index
.
ptr
())
?
PyTuple_Pack
(
1
,
_index
.
ptr
())
:
_index
.
ptr
();
const
auto
&
tensor
=
self
.
Var
().
Get
<
framework
::
LoDTensor
>
();
PADDLE_ENFORCE_EQ
(
tensor
.
IsInitialized
(),
true
,
platform
::
errors
::
InvalidArgument
(
"%s has not been initialized"
,
self
.
Name
()));
const
auto
&
shape
=
tensor
.
dims
();
const
int
rank
=
shape
.
size
();
const
int
size
=
PyTuple_GET_SIZE
(
index
);
PADDLE_ENFORCE_EQ
(
size
<=
rank
,
true
,
platform
::
errors
::
InvalidArgument
(
"too many indices (%d) for tensor of dimension %d"
,
size
,
rank
));
for
(
int
dim
=
0
;
dim
<
size
;
++
dim
)
{
PyObject
*
slice_item
=
PyTuple_GetItem
(
index
,
dim
);
PADDLE_ENFORCE_EQ
(
PyNumber_Check
(
slice_item
)
||
PySlice_Check
(
slice_item
),
true
,
platform
::
errors
::
InvalidArgument
(
"We allow indexing by Integers, Slices, and tuples of "
"these types, but received %s in %dth slice item"
,
std
::
string
(
Py_TYPE
(
slice_item
)
->
tp_name
),
dim
+
1
));
int
dim_len
=
shape
[
dim
];
if
(
PyNumber_Check
(
slice_item
))
{
// integer
int
start
=
static_cast
<
int
>
(
PyLong_AsLong
(
slice_item
));
start
=
start
<
0
?
start
+
dim_len
:
start
;
slice_axes
.
push_back
(
dim
);
slice_starts
.
push_back
(
start
);
slice_ends
.
push_back
(
start
+
1
);
slice_strides
.
push_back
(
1
);
decrease_axis
.
push_back
(
dim
);
}
else
{
// slice
Py_ssize_t
start
,
end
,
step
;
// The parameter type for the slice parameter was PySliceObject* before 3.2
#if PY_VERSION_HEX >= 0x03020000
PySlice_GetIndices
(
slice_item
,
dim_len
,
&
start
,
&
end
,
&
step
);
#else
PySlice_GetIndices
(
reinterpret_cast
<
PySliceObject
*>
(
slice_item
),
dim_len
,
&
start
,
&
end
,
&
step
);
#endif
// :: or : or 0:dim_len:1
if
(
start
==
0
&&
end
==
dim_len
&&
step
==
1
)
continue
;
slice_axes
.
push_back
(
dim
);
slice_starts
.
push_back
(
start
);
slice_ends
.
push_back
(
end
);
slice_strides
.
push_back
(
step
);
}
}
if
(
!
PyTuple_Check
(
_index
.
ptr
()))
Py_DecRef
(
index
);
slice_strides
,
decrease_axis
,
infer_flags
;
auto
tensor
=
self
->
MutableVar
()
->
GetMutable
<
framework
::
LoDTensor
>
();
ParseIndexingSlice
(
tensor
,
_index
.
ptr
(),
&
slice_axes
,
&
slice_starts
,
&
slice_ends
,
&
slice_strides
,
&
decrease_axis
,
&
infer_flags
);
// release gil and do tracing
py
::
gil_scoped_release
release
;
const
auto
&
tracer
=
imperative
::
GetCurrentTracer
();
auto
_self
=
self
.
NewVarBase
(
tensor
.
place
(),
false
);
if
(
slice_axes
.
empty
())
{
return
_
self
;
return
self
;
}
else
{
std
::
vector
<
int
>
infer_flags
(
size
,
1
);
imperative
::
NameVarBaseMap
ins
=
{{
"Input"
,
{
_self
}}};
imperative
::
NameVarBaseMap
ins
=
{{
"Input"
,
{
self
}}};
framework
::
AttributeMap
attrs
=
{
{
"axes"
,
slice_axes
},
{
"starts"
,
slice_starts
},
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录