Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
a8e355af
P
Paddle
项目概览
机器未来
/
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看板
未验证
提交
a8e355af
编写于
9月 08, 2020
作者:
Z
zhupengyang
提交者:
GitHub
9月 08, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
revert logsumexp op(#27145)
上级
847f93f9
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
56 addition
and
300 deletion
+56
-300
paddle/fluid/operators/reduce_ops/logsumexp_op.cc
paddle/fluid/operators/reduce_ops/logsumexp_op.cc
+0
-63
paddle/fluid/operators/reduce_ops/logsumexp_op.cu
paddle/fluid/operators/reduce_ops/logsumexp_op.cu
+0
-21
paddle/fluid/operators/reduce_ops/logsumexp_op.h
paddle/fluid/operators/reduce_ops/logsumexp_op.h
+0
-58
paddle/fluid/operators/reduce_ops/logsumexp_op.part.cu
paddle/fluid/operators/reduce_ops/logsumexp_op.part.cu
+0
-22
python/paddle/fluid/tests/unittests/test_logsumexp.py
python/paddle/fluid/tests/unittests/test_logsumexp.py
+47
-115
python/paddle/tensor/math.py
python/paddle/tensor/math.py
+9
-21
未找到文件。
paddle/fluid/operators/reduce_ops/logsumexp_op.cc
已删除
100644 → 0
浏览文件 @
847f93f9
// Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
//
// 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/fluid/operators/reduce_ops/logsumexp_op.h"
#include <memory>
#include <string>
#include <utility>
#include <vector>
namespace
paddle
{
namespace
operators
{
class
LogsumexpOpMaker
:
public
ops
::
ReduceOpMaker
{
protected:
virtual
std
::
string
GetName
()
const
{
return
"logsumexp"
;
}
virtual
std
::
string
GetOpType
()
const
{
return
"Reduce logsumexp"
;
}
};
template
<
typename
T
>
class
LogsumexpGradOpMaker
:
public
framework
::
SingleGradOpMaker
<
T
>
{
public:
using
framework
::
SingleGradOpMaker
<
T
>::
SingleGradOpMaker
;
protected:
void
Apply
(
GradOpPtr
<
T
>
op
)
const
override
{
op
->
SetType
(
"logsumexp_grad"
);
op
->
SetInput
(
"X"
,
this
->
Input
(
"X"
));
op
->
SetInput
(
"Out"
,
this
->
Output
(
"Out"
));
op
->
SetInput
(
framework
::
GradVarName
(
"Out"
),
this
->
OutputGrad
(
"Out"
));
op
->
SetAttrMap
(
this
->
Attrs
());
op
->
SetOutput
(
framework
::
GradVarName
(
"X"
),
this
->
InputGrad
(
"X"
));
}
};
}
// namespace operators
}
// namespace paddle
REGISTER_OPERATOR
(
logsumexp
,
ops
::
ReduceOp
,
ops
::
LogsumexpOpMaker
,
ops
::
LogsumexpGradOpMaker
<
paddle
::
framework
::
OpDesc
>
,
ops
::
LogsumexpGradOpMaker
<
paddle
::
imperative
::
OpBase
>
);
REGISTER_OPERATOR
(
logsumexp_grad
,
ops
::
ReduceGradOp
);
REGISTER_OP_CPU_KERNEL
(
logsumexp
,
ops
::
ReduceKernel
<
paddle
::
platform
::
CPUDeviceContext
,
float
,
ops
::
LogsumexpFunctor
>
,
ops
::
ReduceKernel
<
paddle
::
platform
::
CPUDeviceContext
,
double
,
ops
::
LogsumexpFunctor
>
);
REGISTER_OP_CPU_KERNEL
(
logsumexp_grad
,
ops
::
ReduceGradKernel
<
paddle
::
platform
::
CPUDeviceContext
,
float
,
ops
::
LogsumexpGradFunctor
>
,
ops
::
ReduceGradKernel
<
paddle
::
platform
::
CPUDeviceContext
,
double
,
ops
::
LogsumexpGradFunctor
>
);
paddle/fluid/operators/reduce_ops/logsumexp_op.cu
已删除
100644 → 0
浏览文件 @
847f93f9
// Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
//
// 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/fluid/operators/reduce_ops/logsumexp_op.h"
REGISTER_OP_CUDA_KERNEL
(
logsumexp
,
ops
::
ReduceKernel
<
paddle
::
platform
::
CUDADeviceContext
,
float
,
ops
::
LogsumexpFunctor
>
,
ops
::
ReduceKernel
<
paddle
::
platform
::
CUDADeviceContext
,
double
,
ops
::
LogsumexpFunctor
>
);
paddle/fluid/operators/reduce_ops/logsumexp_op.h
已删除
100644 → 0
浏览文件 @
847f93f9
// Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
//
// 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/fluid/operators/reduce_ops/reduce_op.h"
namespace
paddle
{
namespace
operators
{
struct
LogsumexpFunctor
{
template
<
typename
DeviceContext
,
typename
X
,
typename
Y
,
typename
Dim
>
void
operator
()(
const
DeviceContext
&
place
,
X
*
x
,
Y
*
y
,
const
Dim
&
dim
)
{
auto
x_dim
=
x
->
dimensions
();
auto
t_dim
=
x_dim
;
for
(
int
i
=
0
;
i
<
static_cast
<
int
>
(
dim
.
size
());
i
++
)
{
t_dim
[
dim
[
i
]]
=
1
;
}
auto
r_dim
=
x_dim
;
for
(
int
i
=
0
;
i
<
static_cast
<
int
>
(
r_dim
.
size
());
i
++
)
{
r_dim
[
i
]
=
1
;
}
for
(
int
i
=
0
;
i
<
static_cast
<
int
>
(
dim
.
size
());
i
++
)
{
r_dim
[
dim
[
i
]]
=
x_dim
[
dim
[
i
]];
}
auto
y_dim
=
y
->
dimensions
();
auto
x_max
=
x
->
maximum
(
dim
);
y
->
device
(
place
)
=
(
x_max
+
(
*
x
-
x_max
.
reshape
(
t_dim
).
broadcast
(
r_dim
)).
exp
().
sum
(
dim
).
log
())
.
reshape
(
y_dim
);
}
};
struct
LogsumexpGradFunctor
{
template
<
typename
DeviceContext
,
typename
X
,
typename
Y
,
typename
DX
,
typename
DY
,
typename
Dim
>
void
operator
()(
const
DeviceContext
&
place
,
X
*
x
,
Y
*
y
,
DX
*
dx
,
DY
*
dy
,
const
Dim
&
dim
,
int
size
)
{
dx
->
device
(
place
)
=
dy
->
broadcast
(
dim
)
*
(
*
x
-
y
->
broadcast
(
dim
)).
exp
();
}
};
}
// namespace operators
}
// namespace paddle
paddle/fluid/operators/reduce_ops/logsumexp_op.part.cu
已删除
100644 → 0
浏览文件 @
847f93f9
// Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
//
// 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.
// .part used to speed up nvcc compile
#include "paddle/fluid/operators/reduce_ops/logsumexp_op.h"
REGISTER_OP_CUDA_KERNEL
(
logsumexp_grad
,
ops
::
ReduceGradKernel
<
paddle
::
platform
::
CUDADeviceContext
,
float
,
ops
::
LogsumexpGradFunctor
>
,
ops
::
ReduceGradKernel
<
paddle
::
platform
::
CUDADeviceContext
,
double
,
ops
::
LogsumexpGradFunctor
>
);
python/paddle/fluid/tests/unittests/test_logsumexp.py
浏览文件 @
a8e355af
...
...
@@ -12,128 +12,60 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from
__future__
import
print_function
import
paddle
import
paddle.fluid
as
fluid
import
unittest
import
numpy
as
np
from
op_test
import
OpTest
from
paddle.fluid
import
Program
,
program_guard
from
paddle.fluid.layer_helper
import
LayerHelper
def
ref_logsumexp
(
x
,
axis
=
None
,
keepdim
=
False
,
reduce_all
=
False
):
if
isinstance
(
axis
,
int
):
axis
=
(
axis
,
)
elif
isinstance
(
axis
,
list
):
axis
=
tuple
(
axis
)
if
reduce_all
:
axis
=
None
out
=
np
.
log
(
np
.
exp
(
x
).
sum
(
axis
=
axis
,
keepdims
=
keepdim
))
return
out
class
TestLogsumexp
(
OpTest
):
def
setUp
(
self
):
self
.
op_type
=
'logsumexp'
self
.
shape
=
[
2
,
3
,
4
,
5
]
self
.
dtype
=
'float64'
self
.
axis
=
[
-
1
]
self
.
keepdim
=
False
self
.
reduce_all
=
False
self
.
set_attrs
()
np
.
random
.
seed
(
10
)
x
=
np
.
random
.
uniform
(
-
1
,
1
,
self
.
shape
).
astype
(
self
.
dtype
)
out
=
ref_logsumexp
(
x
,
self
.
axis
,
self
.
keepdim
,
self
.
reduce_all
)
self
.
inputs
=
{
'X'
:
x
}
self
.
outputs
=
{
'Out'
:
out
}
self
.
attrs
=
{
'dim'
:
self
.
axis
,
'keep_dim'
:
self
.
keepdim
,
'reduce_all'
:
self
.
reduce_all
}
def
set_attrs
(
self
):
pass
def
test_check_output
(
self
):
self
.
check_output
()
def
test_check_grad
(
self
):
self
.
check_grad
([
'X'
],
[
'Out'
])
class
TestLogsumexp_shape
(
TestLogsumexp
):
def
set_attrs
(
self
):
self
.
shape
=
[
4
,
5
,
6
]
class
TestLogsumexp_axis
(
TestLogsumexp
):
def
set_attrs
(
self
):
self
.
axis
=
[
0
,
-
1
]
class
TestLogsumexp_axis_all
(
TestLogsumexp
):
def
set_attrs
(
self
):
self
.
axis
=
[
0
,
1
,
2
,
3
]
class
TestLogsumexp_keepdim
(
TestLogsumexp
):
def
set_attrs
(
self
):
self
.
keepdim
=
True
class
TestLogsumexp_reduce_all
(
TestLogsumexp
):
def
set_attrs
(
self
):
self
.
reduce_all
=
True
class
TestLogsumexpError
(
unittest
.
TestCase
):
class
TestLogSumOpError
(
unittest
.
TestCase
):
def
test_errors
(
self
):
with
paddle
.
static
.
program_guard
(
paddle
.
static
.
Program
()):
self
.
assertRaises
(
TypeError
,
paddle
.
logsumexp
,
1
)
x1
=
paddle
.
data
(
name
=
'x1'
,
shape
=
[
120
],
dtype
=
"int32"
)
self
.
assertRaises
(
TypeError
,
paddle
.
logsumexp
,
x1
)
class
TestLogsumexpAPI
(
unittest
.
TestCase
):
def
setUp
(
self
):
self
.
shape
=
[
2
,
3
,
4
,
5
]
self
.
x
=
np
.
random
.
uniform
(
-
1
,
1
,
self
.
shape
).
astype
(
np
.
float32
)
self
.
place
=
paddle
.
CUDAPlace
(
0
)
if
paddle
.
fluid
.
core
.
is_compiled_with_cuda
()
\
else
paddle
.
CPUPlace
()
def
api_case
(
self
,
axis
=
None
,
keepdim
=
False
):
out_ref
=
ref_logsumexp
(
self
.
x
,
axis
,
keepdim
)
with
paddle
.
static
.
program_guard
(
paddle
.
static
.
Program
()):
x
=
paddle
.
data
(
'X'
,
self
.
shape
)
out
=
paddle
.
logsumexp
(
x
,
axis
,
keepdim
)
exe
=
paddle
.
static
.
Executor
(
self
.
place
)
res
=
exe
.
run
(
feed
=
{
'X'
:
self
.
x
},
fetch_list
=
[
out
])
self
.
assertTrue
(
np
.
allclose
(
res
[
0
],
out_ref
))
paddle
.
disable_static
(
self
.
place
)
x
=
paddle
.
to_variable
(
self
.
x
)
out
=
paddle
.
logsumexp
(
x
,
axis
,
keepdim
)
self
.
assertTrue
(
np
.
allclose
(
out
.
numpy
(),
out_ref
))
paddle
.
enable_static
()
def
test_api
(
self
):
self
.
api_case
()
self
.
api_case
(
2
)
self
.
api_case
([
-
1
])
self
.
api_case
([
2
,
-
3
])
self
.
api_case
((
0
,
1
,
-
1
))
self
.
api_case
(
keepdim
=
True
)
def
test_alias
(
self
):
paddle
.
disable_static
(
self
.
place
)
x
=
paddle
.
to_variable
(
self
.
x
)
out1
=
paddle
.
logsumexp
(
x
)
out2
=
paddle
.
tensor
.
logsumexp
(
x
)
out3
=
paddle
.
tensor
.
math
.
logsumexp
(
x
)
out_ref
=
ref_logsumexp
(
self
.
x
)
for
out
in
[
out1
,
out2
,
out3
]:
self
.
assertTrue
(
np
.
allclose
(
out
.
numpy
(),
out_ref
))
paddle
.
enable_static
()
with
program_guard
(
Program
(),
Program
()):
x1
=
fluid
.
layers
.
data
(
name
=
'x1'
,
shape
=
[
120
],
dtype
=
"uint8"
)
self
.
assertRaises
(
Exception
,
paddle
.
logsumexp
,
x1
)
x2
=
fluid
.
layers
.
data
(
name
=
'x2'
,
shape
=
[
2
,
3
],
dtype
=
"int"
)
self
.
assertRaises
(
Exception
,
paddle
.
logsumexp
,
x2
)
x3
=
fluid
.
layers
.
data
(
name
=
'x3'
,
shape
=
[
3
],
dtype
=
"float16"
)
self
.
assertRaises
(
Exception
,
paddle
.
logsumexp
,
x3
)
class
TestLogSumExpOp
(
unittest
.
TestCase
):
def
test_dygraph
(
self
):
with
fluid
.
dygraph
.
guard
():
np_x
=
np
.
random
.
uniform
(
0.1
,
1
,
[
123
]).
astype
(
np
.
float32
)
x
=
fluid
.
dygraph
.
to_variable
(
np_x
)
self
.
assertTrue
(
np
.
allclose
(
paddle
.
logsumexp
(
x
).
numpy
(),
np
.
log
(
np
.
sum
(
np
.
exp
(
np_x
)))))
np_x
=
np
.
random
.
uniform
(
0.1
,
1
,
[
2
,
3
,
4
]).
astype
(
np
.
float32
)
x
=
fluid
.
dygraph
.
to_variable
(
np_x
)
self
.
assertTrue
(
np
.
allclose
(
paddle
.
logsumexp
(
x
,
[
1
,
2
]).
numpy
(),
np
.
log
(
np
.
sum
(
np
.
exp
(
np_x
),
axis
=
(
1
,
2
)))))
np_x
=
np
.
random
.
uniform
(
0.1
,
1
,
[
2
,
3
,
4
]).
astype
(
np
.
float32
)
x
=
fluid
.
dygraph
.
to_variable
(
np_x
)
self
.
assertTrue
(
np
.
allclose
(
paddle
.
logsumexp
(
x
,
[
2
]).
numpy
(),
np
.
log
(
np
.
sum
(
np
.
exp
(
np_x
),
axis
=
(
2
)))))
np_x
=
np
.
random
.
uniform
(
0.1
,
1
,
[
2
,
3
,
4
]).
astype
(
np
.
float32
)
x
=
fluid
.
dygraph
.
to_variable
(
np_x
)
self
.
assertTrue
(
np
.
allclose
(
paddle
.
logsumexp
(
x
,
keepdim
=
True
).
numpy
(),
np
.
log
(
np
.
sum
(
np
.
exp
(
np_x
),
keepdims
=
True
))))
if
__name__
==
'__main__'
:
...
...
python/paddle/tensor/math.py
浏览文件 @
a8e355af
...
...
@@ -86,7 +86,6 @@ __all__ = [
'floor'
,
'increment'
,
'log'
,
'logsumexp'
,
'mul'
,
'multiplex'
,
'pow'
,
...
...
@@ -1177,35 +1176,24 @@ def logsumexp(x, axis=None, keepdim=False, name=None):
.. code-block:: python
import paddle
import numpy as np
paddle.disable_static()
x = paddle.to_tensor([[-1.5, 0., 2.], [3., 1.2, -2.4]])
x = np.array([[-1.5, 0., 2.], [3., 1.2, -2.4]])
x = paddle.to_tensor(x)
out1 = paddle.logsumexp(x) # [3.4691226]
out2 = paddle.logsumexp(x, 1) # [2.15317821, 3.15684602]
"""
if
isinstance
(
axis
,
int
):
axis
=
[
axis
]
reduce_all
=
True
if
axis
is
None
\
or
len
(
axis
)
==
0
\
or
len
(
axis
)
==
len
(
x
.
shape
)
else
False
if
axis
is
None
or
len
(
axis
)
==
0
:
axis
=
[
0
]
if
in_dygraph_mode
():
return
core
.
ops
.
logsumexp
(
x
,
'dim'
,
axis
,
'keep_dim'
,
keepdim
,
'reduce_all'
,
reduce_all
)
check_variable_and_dtype
(
x
,
'x'
,
if
not
in_dygraph_mode
():
check_variable_and_dtype
(
x
,
'x'
,
[
'float32'
,
'float64'
],
'logsumexp'
)
helper
=
LayerHelper
(
'logsumexp'
,
**
locals
())
attrs
=
{
'dim'
:
axis
,
'keep_dim'
:
keepdim
,
'reduce_all'
:
reduce_all
}
out
=
helper
.
create_variable_for_type_inference
(
x
.
dtype
)
helper
.
append_op
(
type
=
'logsumexp'
,
inputs
=
{
'X'
:
x
},
outputs
=
{
'Out'
:
out
},
attrs
=
attrs
)
out
=
paddle
.
exp
(
x
,
name
)
out
=
paddle
.
sum
(
out
,
axis
=
axis
,
keepdim
=
keepdim
,
name
=
name
)
out
=
paddle
.
log
(
out
,
name
)
return
out
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录