Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleDetection
提交
725e6448
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看板
提交
725e6448
编写于
2月 09, 2018
作者:
E
emailweixu
提交者:
qingqing01
2月 09, 2018
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
cumsum operator (#8288)
上级
69712ef2
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
352 addition
and
2 deletion
+352
-2
paddle/framework/grad_op_desc_maker.h
paddle/framework/grad_op_desc_maker.h
+5
-0
paddle/framework/op_registry.h
paddle/framework/op_registry.h
+1
-1
paddle/operators/cum_op.h
paddle/operators/cum_op.h
+111
-0
paddle/operators/cumsum_op.cc
paddle/operators/cumsum_op.cc
+82
-0
paddle/operators/cumsum_op.cu
paddle/operators/cumsum_op.cu
+22
-0
python/paddle/v2/fluid/layers/ops.py
python/paddle/v2/fluid/layers/ops.py
+2
-0
python/paddle/v2/fluid/tests/op_test.py
python/paddle/v2/fluid/tests/op_test.py
+2
-1
python/paddle/v2/fluid/tests/test_cumsum_op.py
python/paddle/v2/fluid/tests/test_cumsum_op.py
+127
-0
未找到文件。
paddle/framework/grad_op_desc_maker.h
浏览文件 @
725e6448
...
...
@@ -122,6 +122,11 @@ class GradOpDescMakerBase {
return
it
->
second
;
}
template
<
typename
T
>
inline
const
T
&
Attr
(
const
std
::
string
&
name
)
const
{
return
boost
::
get
<
T
>
(
GetAttr
(
name
));
}
std
::
string
ForwardOpType
()
const
{
return
this
->
fwd_op_
.
Type
();
}
private:
...
...
paddle/framework/op_registry.h
浏览文件 @
725e6448
...
...
@@ -143,7 +143,7 @@ class OpKernelRegistrar : public Registrar {
/**
* Macro to register Operator. When the input is duplicable, you should
* use REGISTER_OP_EX with d
e
op_empty_grad=false instead.
* use REGISTER_OP_EX with d
r
op_empty_grad=false instead.
*/
#define REGISTER_OP(op_type, op_class, op_maker_class, grad_op_type, \
grad_op_class) \
...
...
paddle/operators/cum_op.h
0 → 100644
浏览文件 @
725e6448
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#pragma once
#include "paddle/framework/eigen.h"
#include "paddle/framework/op_registry.h"
#include "paddle/framework/operator.h"
#include "paddle/operators/detail/safe_ref.h"
namespace
paddle
{
namespace
operators
{
template
<
typename
DeviceContext
,
typename
Functor
>
class
CumKernel
:
public
framework
::
OpKernel
<
typename
Functor
::
ELEMENT_TYPE
>
{
public:
using
T
=
typename
Functor
::
ELEMENT_TYPE
;
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
auto
&
X
=
detail
::
Ref
(
context
.
Input
<
framework
::
Tensor
>
(
"X"
),
"Cannot get input tensor X, variable name = %s"
,
context
.
op
().
Input
(
"X"
));
auto
&
Out
=
detail
::
Ref
(
context
.
Output
<
framework
::
Tensor
>
(
"Out"
),
"Cannot get output tensor Out, variable name = %s"
,
context
.
op
().
Output
(
"Out"
));
int
axis
=
context
.
Attr
<
int
>
(
"axis"
);
bool
exclusive
=
context
.
Attr
<
bool
>
(
"exclusive"
);
bool
reverse
=
context
.
Attr
<
bool
>
(
"reverse"
);
auto
x_dims
=
X
.
dims
();
if
(
axis
==
-
1
)
{
axis
=
x_dims
.
size
()
-
1
;
}
PADDLE_ENFORCE_LT
(
axis
,
x_dims
.
size
(),
"axis should be less than the dimensiotn of the input tensor"
);
Out
.
mutable_data
<
T
>
(
context
.
GetPlace
());
int
pre
=
1
;
int
post
=
1
;
int
mid
=
x_dims
[
axis
];
for
(
int
i
=
0
;
i
<
axis
;
++
i
)
{
pre
*=
x_dims
[
i
];
}
for
(
int
i
=
axis
+
1
;
i
<
x_dims
.
size
();
++
i
)
{
post
*=
x_dims
[
i
];
}
auto
x
=
framework
::
EigenVector
<
T
>::
Flatten
(
X
);
auto
out
=
framework
::
EigenVector
<
T
>::
Flatten
(
Out
);
auto
*
place
=
context
.
template
device_context
<
DeviceContext
>().
eigen_device
();
using
IndexT
=
Eigen
::
DenseIndex
;
if
(
pre
==
1
)
{
if
(
post
==
1
)
{
ComputeImp
(
*
place
,
Eigen
::
DSizes
<
IndexT
,
1
>
(
mid
),
x
,
out
,
/* axis= */
0
,
reverse
,
exclusive
);
}
else
{
ComputeImp
(
*
place
,
Eigen
::
DSizes
<
IndexT
,
2
>
(
mid
,
post
),
x
,
out
,
/* axis= */
0
,
reverse
,
exclusive
);
}
}
else
{
if
(
post
==
1
)
{
ComputeImp
(
*
place
,
Eigen
::
DSizes
<
IndexT
,
2
>
(
pre
,
mid
),
x
,
out
,
/* axis= */
1
,
reverse
,
exclusive
);
}
else
{
ComputeImp
(
*
place
,
Eigen
::
DSizes
<
IndexT
,
3
>
(
pre
,
mid
,
post
),
x
,
out
,
/* axis= */
1
,
reverse
,
exclusive
);
}
}
}
private:
template
<
typename
Device
,
typename
Dim
,
typename
X
,
typename
Out
>
void
ComputeImp
(
Device
d
,
const
Dim
&
dims
,
X
x
,
Out
out
,
int
axis
,
bool
reverse
,
bool
exclusive
)
const
{
if
(
!
reverse
)
{
out
.
reshape
(
dims
).
device
(
d
)
=
Functor
()(
x
.
reshape
(
dims
),
axis
,
exclusive
);
}
else
{
std
::
array
<
bool
,
Dim
::
count
>
rev
;
rev
.
fill
(
false
);
rev
[
axis
]
=
reverse
;
out
.
reshape
(
dims
).
device
(
d
)
=
Functor
()(
x
.
reshape
(
dims
).
reverse
(
rev
),
axis
,
exclusive
).
reverse
(
rev
);
}
}
};
template
<
typename
T
>
struct
CumsumFunctor
{
using
ELEMENT_TYPE
=
T
;
template
<
typename
X
>
const
typename
X
::
TensorScanSumOp
operator
()(
X
x
,
int
axis
,
bool
exclusive
)
const
{
return
x
.
cumsum
(
axis
,
exclusive
);
}
};
}
// namespace operators
}
// namespace paddle
paddle/operators/cumsum_op.cc
0 → 100644
浏览文件 @
725e6448
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#include "paddle/operators/cum_op.h"
namespace
paddle
{
namespace
operators
{
class
CumOp
:
public
framework
::
OperatorWithKernel
{
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
{
ctx
->
SetOutputDim
(
"Out"
,
ctx
->
GetInputDim
(
"X"
));
ctx
->
ShareLoD
(
"X"
,
/*->*/
"Out"
);
}
};
class
CumsumOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
public:
CumsumOpMaker
(
OpProto
*
proto
,
OpAttrChecker
*
op_checker
)
:
framework
::
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"X"
,
"Input of Cumsum operator"
);
AddOutput
(
"Out"
,
"Output of Cumsum operator"
);
AddAttr
<
int
>
(
"axis"
,
"(int, default -1). The dimenstion to accumulate along. "
"-1 means the last dimenstion"
)
.
SetDefault
(
-
1
)
.
EqualGreaterThan
(
-
1
);
AddAttr
<
bool
>
(
"exclusive"
,
"bool, default false). Whether to perform exclusive cumsum"
)
.
SetDefault
(
false
);
AddAttr
<
bool
>
(
"reverse"
,
"bool, default false). If true, the cumsum is performed in "
"the reversed direction"
)
.
SetDefault
(
false
);
AddComment
(
R"DOC(
The cumulative sum of the elements along a given axis.
By default, the first element of the result is the same of the first element of
the input. If exlusive is true, the first element of the result is 0.
)DOC"
);
}
};
class
CumsumGradMaker
:
public
framework
::
SingleGradOpDescMaker
{
public:
using
framework
::
SingleGradOpDescMaker
::
SingleGradOpDescMaker
;
protected:
std
::
unique_ptr
<
framework
::
OpDesc
>
Apply
()
const
override
{
auto
*
grad_op
=
new
framework
::
OpDesc
();
grad_op
->
SetType
(
"cumsum"
);
grad_op
->
SetInput
(
"X"
,
OutputGrad
(
"Out"
));
grad_op
->
SetOutput
(
"Out"
,
InputGrad
(
"X"
));
grad_op
->
SetAttr
(
"axis"
,
Attr
<
int
>
(
"axis"
));
grad_op
->
SetAttr
(
"reverse"
,
!
Attr
<
bool
>
(
"reverse"
));
grad_op
->
SetAttr
(
"exclusive"
,
Attr
<
bool
>
(
"exclusive"
));
return
std
::
unique_ptr
<
framework
::
OpDesc
>
(
grad_op
);
}
};
}
// namespace operators
}
// namespace paddle
namespace
ops
=
paddle
::
operators
;
using
CPU
=
paddle
::
platform
::
CPUDeviceContext
;
REGISTER_OPERATOR
(
cumsum
,
ops
::
CumOp
,
ops
::
CumsumOpMaker
,
ops
::
CumsumGradMaker
);
REGISTER_OP_CPU_KERNEL
(
cumsum
,
ops
::
CumKernel
<
CPU
,
ops
::
CumsumFunctor
<
float
>>
,
ops
::
CumKernel
<
CPU
,
ops
::
CumsumFunctor
<
double
>>
,
ops
::
CumKernel
<
CPU
,
ops
::
CumsumFunctor
<
int
>>
)
paddle/operators/cumsum_op.cu
0 → 100644
浏览文件 @
725e6448
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#include "paddle/operators/cum_op.h"
namespace
ops
=
paddle
::
operators
;
using
CUDA
=
paddle
::
platform
::
CUDADeviceContext
;
REGISTER_OP_CUDA_KERNEL
(
cumsum
,
ops
::
CumKernel
<
CUDA
,
ops
::
CumsumFunctor
<
float
>>
,
ops
::
CumKernel
<
CUDA
,
ops
::
CumsumFunctor
<
double
>>
,
ops
::
CumKernel
<
CUDA
,
ops
::
CumsumFunctor
<
int
>>
)
python/paddle/v2/fluid/layers/ops.py
浏览文件 @
725e6448
...
...
@@ -65,6 +65,8 @@ __all__ = [
'logical_or'
,
'logical_xor'
,
'logical_not'
,
'uniform_random'
,
'cumsum'
,
]
+
__activations__
for
_OP
in
set
(
__all__
):
...
...
python/paddle/v2/fluid/tests/op_test.py
浏览文件 @
725e6448
...
...
@@ -326,7 +326,8 @@ class OpTest(unittest.TestCase):
self
.
assertTrue
(
np
.
allclose
(
actual_t
,
expect_t
,
atol
=
atol
),
"Output ("
+
out_name
+
") has diff at "
+
str
(
place
))
"Output ("
+
out_name
+
") has diff at "
+
str
(
place
)
+
str
(
actual_t
)
+
str
(
expect_t
))
if
isinstance
(
expect
,
tuple
):
self
.
assertListEqual
(
actual
.
lod
(),
expect
[
1
],
"Output ("
+
out_name
+
...
...
python/paddle/v2/fluid/tests/test_cumsum_op.py
0 → 100644
浏览文件 @
725e6448
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import
unittest
import
numpy
as
np
from
op_test
import
OpTest
class
TestSumOp1
(
OpTest
):
def
setUp
(
self
):
self
.
op_type
=
"cumsum"
self
.
attrs
=
{
'axis'
:
2
}
self
.
inputs
=
{
'X'
:
np
.
random
.
random
((
5
,
6
,
10
)).
astype
(
"float64"
)}
self
.
outputs
=
{
'Out'
:
self
.
inputs
[
'X'
].
cumsum
(
axis
=
2
)}
def
test_check_output
(
self
):
self
.
check_output
()
def
test_check_grad
(
self
):
self
.
check_grad
([
'X'
],
'Out'
)
class
TestSumOp2
(
OpTest
):
def
setUp
(
self
):
self
.
op_type
=
"cumsum"
self
.
attrs
=
{
'axis'
:
-
1
,
'reverse'
:
True
}
self
.
inputs
=
{
'X'
:
np
.
random
.
random
((
5
,
6
,
10
)).
astype
(
"float64"
)}
self
.
outputs
=
{
'Out'
:
np
.
flip
(
np
.
flip
(
self
.
inputs
[
'X'
],
axis
=
2
).
cumsum
(
axis
=
2
),
axis
=
2
)
}
def
test_check_output
(
self
):
self
.
check_output
()
def
test_check_grad
(
self
):
self
.
check_grad
([
'X'
],
'Out'
)
class
TestSumOp3
(
OpTest
):
def
setUp
(
self
):
self
.
op_type
=
"cumsum"
self
.
attrs
=
{
'axis'
:
1
}
self
.
inputs
=
{
'X'
:
np
.
random
.
random
((
5
,
6
,
10
)).
astype
(
"float64"
)}
self
.
outputs
=
{
'Out'
:
self
.
inputs
[
'X'
].
cumsum
(
axis
=
1
)}
def
test_check_output
(
self
):
self
.
check_output
()
def
test_check_grad
(
self
):
self
.
check_grad
([
'X'
],
'Out'
)
class
TestSumOp4
(
OpTest
):
def
setUp
(
self
):
self
.
op_type
=
"cumsum"
self
.
attrs
=
{
'axis'
:
0
}
self
.
inputs
=
{
'X'
:
np
.
random
.
random
((
5
,
6
,
10
)).
astype
(
"float64"
)}
self
.
outputs
=
{
'Out'
:
self
.
inputs
[
'X'
].
cumsum
(
axis
=
0
)}
def
test_check_output
(
self
):
self
.
check_output
()
def
test_check_grad
(
self
):
self
.
check_grad
([
'X'
],
'Out'
)
class
TestSumOp5
(
OpTest
):
def
setUp
(
self
):
self
.
op_type
=
"cumsum"
self
.
inputs
=
{
'X'
:
np
.
random
.
random
((
5
,
6
)).
astype
(
"float64"
)}
self
.
outputs
=
{
'Out'
:
self
.
inputs
[
'X'
].
cumsum
(
axis
=
1
)}
def
test_check_output
(
self
):
self
.
check_output
()
def
test_check_grad
(
self
):
self
.
check_grad
([
'X'
],
'Out'
)
class
TestSumOp7
(
OpTest
):
def
setUp
(
self
):
self
.
op_type
=
"cumsum"
self
.
inputs
=
{
'X'
:
np
.
random
.
random
((
6
)).
astype
(
"float64"
)}
self
.
outputs
=
{
'Out'
:
self
.
inputs
[
'X'
].
cumsum
(
axis
=
0
)}
def
test_check_output
(
self
):
self
.
check_output
()
def
test_check_grad
(
self
):
self
.
check_grad
([
'X'
],
'Out'
)
class
TestSumOp8
(
OpTest
):
def
setUp
(
self
):
self
.
op_type
=
"cumsum"
self
.
attrs
=
{
'axis'
:
2
,
"exclusive"
:
True
}
a
=
np
.
random
.
random
((
5
,
6
,
3
)).
astype
(
"float64"
)
self
.
inputs
=
{
'X'
:
a
}
self
.
outputs
=
{
'Out'
:
np
.
concatenate
(
(
np
.
zeros
(
(
5
,
6
,
1
),
dtype
=
np
.
float64
),
a
[:,
:,
:
-
1
].
cumsum
(
axis
=
2
)),
axis
=
2
)
}
def
test_check_output
(
self
):
self
.
check_output
()
def
test_check_grad
(
self
):
self
.
check_grad
([
'X'
],
'Out'
)
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录