Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
b04c78ef
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2299
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看板
未验证
提交
b04c78ef
编写于
11月 25, 2020
作者:
J
joejiong
提交者:
GitHub
11月 25, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Update pow (#29000)
Simple code clean up
上级
9479961d
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
11 addition
and
21 deletion
+11
-21
paddle/fluid/operators/elementwise/elementwise_pow_op.h
paddle/fluid/operators/elementwise/elementwise_pow_op.h
+8
-11
python/paddle/tensor/math.py
python/paddle/tensor/math.py
+3
-10
未找到文件。
paddle/fluid/operators/elementwise/elementwise_pow_op.h
浏览文件 @
b04c78ef
...
@@ -23,20 +23,17 @@ namespace operators {
...
@@ -23,20 +23,17 @@ namespace operators {
template
<
typename
T
>
template
<
typename
T
>
struct
PowFunctor
{
struct
PowFunctor
{
inline
HOSTDEVICE
T
operator
()(
T
a
,
T
b
)
const
{
inline
HOSTDEVICE
T
operator
()(
T
a
,
T
b
)
const
{
// TODO(wujionghao): A potential speed improvement is supporting different
// TODO(wujionghao): A potential speed improvement is supporting different
// types in C++.
// types in C++.
// #ifdef __CUDA_ARCH__
#ifdef __CUDA_ARCH__
// // On CUDAPlace, std::pow(3, 1) calls pow(float, float), and
// On CUDAPlace, std::pow(3, 1) calls pow(float, float), and
// // it will return a float number like 2.99... , which floor to 2
// it will return a float number like 2.99... , which floor to 2
// // when cast to int by default and it is wrong.
// when cast to int by default and it is wrong.
// // Use llrint to cast it to the nearest integer, which is 3.
// Use llrint to cast it to the nearest integer, which is 3.
// if (std::is_integral<T>::value) {
// return std::llrint(std::pow(a, b));
// }
// #endif
if
(
std
::
is_integral
<
T
>::
value
)
{
if
(
std
::
is_integral
<
T
>::
value
)
{
return
std
::
llrint
(
std
::
pow
(
a
,
b
));
return
std
::
llrint
(
std
::
pow
(
a
,
b
));
}
}
#endif
return
std
::
pow
(
a
,
b
);
return
std
::
pow
(
a
,
b
);
}
}
};
};
...
...
python/paddle/tensor/math.py
浏览文件 @
b04c78ef
...
@@ -172,12 +172,12 @@ def pow(x, y, name=None):
...
@@ -172,12 +172,12 @@ def pow(x, y, name=None):
x = paddle.to_tensor([1, 2, 3])
x = paddle.to_tensor([1, 2, 3])
y = 2
y = 2
res = paddle.pow(x, y)
res = paddle.pow(x, y)
print(res
.numpy()
) # [1 4 9]
print(res) # [1 4 9]
# example 2: y is a Tensor
# example 2: y is a Tensor
y = paddle.full(shape=[1], fill_value=2, dtype='float32')
y = paddle.full(shape=[1], fill_value=2, dtype='float32')
res = paddle.pow(x, y)
res = paddle.pow(x, y)
print(res
.numpy()
) # [1 4 9]
print(res) # [1 4 9]
"""
"""
# in dynamic graph mode
# in dynamic graph mode
...
@@ -185,14 +185,9 @@ def pow(x, y, name=None):
...
@@ -185,14 +185,9 @@ def pow(x, y, name=None):
if
isinstance
(
y
,
(
int
,
float
)):
if
isinstance
(
y
,
(
int
,
float
)):
return
core
.
ops
.
pow
(
x
,
'factor'
,
y
)
return
core
.
ops
.
pow
(
x
,
'factor'
,
y
)
elif
isinstance
(
y
,
(
paddle
.
Tensor
,
Variable
)):
elif
isinstance
(
y
,
(
paddle
.
Tensor
,
Variable
)):
if
x
.
dtype
!=
y
.
dtype
:
if
x
.
dtype
!=
y
.
dtype
:
y
=
cast
(
y
,
dtype
=
'float64'
)
y
=
cast
(
y
,
dtype
=
'float64'
)
x
=
cast
(
x
,
dtype
=
'float64'
)
x
=
cast
(
x
,
dtype
=
'float64'
)
out_dygraph
=
_elementwise_op_in_dygraph
(
x
,
y
,
axis
=-
1
,
act
=
None
,
op_name
=
'elementwise_pow'
)
return
out_dygraph
return
_elementwise_op_in_dygraph
(
return
_elementwise_op_in_dygraph
(
x
,
y
,
axis
=-
1
,
act
=
None
,
op_name
=
'elementwise_pow'
)
x
,
y
,
axis
=-
1
,
act
=
None
,
op_name
=
'elementwise_pow'
)
else
:
else
:
...
@@ -213,9 +208,7 @@ def pow(x, y, name=None):
...
@@ -213,9 +208,7 @@ def pow(x, y, name=None):
if
x
.
dtype
!=
y
.
dtype
:
if
x
.
dtype
!=
y
.
dtype
:
y
=
cast
(
y
,
dtype
=
'float64'
)
y
=
cast
(
y
,
dtype
=
'float64'
)
x
=
cast
(
x
,
dtype
=
'float64'
)
x
=
cast
(
x
,
dtype
=
'float64'
)
out
=
helper
.
create_variable_for_type_inference
(
dtype
=
x
.
dtype
)
out
=
helper
.
create_variable_for_type_inference
(
dtype
=
x
.
dtype
)
else
:
out
=
helper
.
create_variable_for_type_inference
(
dtype
=
x
.
dtype
)
return
_elementwise_op
(
LayerHelper
(
'elementwise_pow'
,
**
locals
()))
return
_elementwise_op
(
LayerHelper
(
'elementwise_pow'
,
**
locals
()))
else
:
else
:
raise
TypeError
(
'y must be scalar or tensor type, but received: %s '
%
(
type
(
y
)))
raise
TypeError
(
'y must be scalar or tensor type, but received: %s '
%
(
type
(
y
)))
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录