Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleDetection
提交
a114a39e
P
PaddleDetection
项目概览
PaddlePaddle
/
PaddleDetection
大约 1 年 前同步成功
通知
695
Star
11112
Fork
2696
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
184
列表
看板
标记
里程碑
合并请求
40
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleDetection
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
184
Issue
184
列表
看板
标记
里程碑
合并请求
40
合并请求
40
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
a114a39e
编写于
6月 13, 2019
作者:
T
tensor-tang
提交者:
GitHub
6月 13, 2019
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
cherry pick concat op support negative axis (#18050)
test=release/1.5
上级
73eacf3e
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
36 addition
and
6 deletion
+36
-6
paddle/fluid/operators/concat_op.cc
paddle/fluid/operators/concat_op.cc
+8
-2
paddle/fluid/operators/concat_op.h
paddle/fluid/operators/concat_op.h
+13
-3
python/paddle/fluid/tests/unittests/test_concat_op.py
python/paddle/fluid/tests/unittests/test_concat_op.py
+15
-1
未找到文件。
paddle/fluid/operators/concat_op.cc
浏览文件 @
a114a39e
...
...
@@ -36,7 +36,10 @@ class ConcatOp : public framework::OperatorWithKernel {
"Output(Out) of ConcatOp should not be null."
);
auto
ins
=
ctx
->
GetInputsDim
(
"X"
);
size_t
axis
=
static_cast
<
size_t
>
(
ctx
->
Attrs
().
Get
<
int
>
(
"axis"
));
size_t
axis
=
ComputeAxis
(
static_cast
<
int64_t
>
(
ctx
->
Attrs
().
Get
<
int
>
(
"axis"
)),
static_cast
<
int64_t
>
(
ins
[
0
].
size
()));
const
size_t
n
=
ins
.
size
();
PADDLE_ENFORCE_GT
(
n
,
0
,
"Input tensors count should > 0."
);
...
...
@@ -115,7 +118,10 @@ class ConcatOpMaker : public framework::OpProtoAndCheckerMaker {
"(bool, default false) Indicates if MKL-DNN kernel will be used"
)
.
SetDefault
(
false
);
AddAttr
<
int
>
(
"axis"
,
"The axis along which the input tensors will be concatenated."
)
"The axis along which the input tensors will be concatenated."
"The axis could also be negative numbers. Negative axis is "
"interpreted as counting from the end of the rank."
"i.e., axis + rank(X) th dimension."
)
.
SetDefault
(
0
);
AddAttr
<
bool
>
(
"use_quantizer"
,
"(bool, default false) "
...
...
paddle/fluid/operators/concat_op.h
浏览文件 @
a114a39e
...
...
@@ -23,13 +23,22 @@ limitations under the License. */
namespace
paddle
{
namespace
operators
{
static
inline
int64_t
ComputeAxis
(
int64_t
axis
,
int64_t
rank
)
{
if
(
axis
<
0
)
{
axis
=
axis
+
rank
;
}
return
axis
>
0
?
axis
:
0
;
}
template
<
typename
DeviceContext
,
typename
T
>
class
ConcatKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
auto
ins
=
ctx
.
MultiInput
<
framework
::
Tensor
>
(
"X"
);
framework
::
Tensor
*
out
=
ctx
.
Output
<
framework
::
Tensor
>
(
"Out"
);
int64_t
axis
=
static_cast
<
int64_t
>
(
ctx
.
Attr
<
int
>
(
"axis"
));
PADDLE_ENFORCE
(
ins
[
0
],
"The input should not be null."
);
auto
axis
=
ComputeAxis
(
static_cast
<
int64_t
>
(
ctx
.
Attr
<
int
>
(
"axis"
)),
static_cast
<
int64_t
>
(
ins
[
0
]
->
dims
().
size
()));
auto
place
=
ctx
.
GetPlace
();
out
->
mutable_data
<
T
>
(
place
);
...
...
@@ -83,8 +92,9 @@ class ConcatGradKernel : public framework::OpKernel<T> {
}
}
}
int64_t
axis
=
static_cast
<
int64_t
>
(
ctx
.
Attr
<
int
>
(
"axis"
));
PADDLE_ENFORCE
(
ins
[
0
],
"The input should not be null."
);
auto
axis
=
ComputeAxis
(
static_cast
<
int64_t
>
(
ctx
.
Attr
<
int
>
(
"axis"
)),
static_cast
<
int64_t
>
(
ins
[
0
]
->
dims
().
size
()));
// get output tensor that the name is not kEmptyVarName
std
::
vector
<
framework
::
Tensor
*>
outputs
;
...
...
python/paddle/fluid/tests/unittests/test_concat_op.py
浏览文件 @
a114a39e
...
...
@@ -25,9 +25,15 @@ class TestConcatOp(OpTest):
self
.
init_test_data
()
self
.
inputs
=
{
'X'
:
[(
'x0'
,
self
.
x0
),
(
'x1'
,
self
.
x1
),
(
'x2'
,
self
.
x2
)]}
self
.
attrs
=
{
'axis'
:
self
.
axis
}
if
self
.
axis
<
0
:
self
.
actual_axis
=
self
.
axis
+
len
(
self
.
x0
.
shape
)
self
.
actual_axis
=
self
.
actual_axis
if
self
.
actual_axis
>
0
else
0
else
:
self
.
actual_axis
=
self
.
axis
self
.
outputs
=
{
'Out'
:
np
.
concatenate
(
(
self
.
x0
,
self
.
x1
,
self
.
x2
),
axis
=
self
.
axis
)
(
self
.
x0
,
self
.
x1
,
self
.
x2
),
axis
=
self
.
a
ctual_a
xis
)
}
def
test_check_output
(
self
):
...
...
@@ -75,5 +81,13 @@ class TestConcatOp4(TestConcatOp):
pass
class
TestConcatOp5
(
TestConcatOp
):
def
init_test_data
(
self
):
self
.
x0
=
np
.
random
.
random
((
2
,
1
,
4
,
5
)).
astype
(
'float32'
)
self
.
x1
=
np
.
random
.
random
((
2
,
2
,
4
,
5
)).
astype
(
'float32'
)
self
.
x2
=
np
.
random
.
random
((
2
,
3
,
4
,
5
)).
astype
(
'float32'
)
self
.
axis
=
-
3
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录