Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
29494d70
P
Paddle
项目概览
PaddlePaddle
/
Paddle
1 年多 前同步成功
通知
2302
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看板
未验证
提交
29494d70
编写于
8月 28, 2020
作者:
S
ShenLiang
提交者:
GitHub
8月 28, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix remainder, floor_div (#26732)
* fix remainder, floordiv
上级
623a4c2e
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
67 addition
and
4 deletion
+67
-4
paddle/fluid/operators/elementwise/elementwise_floordiv_op.h
paddle/fluid/operators/elementwise/elementwise_floordiv_op.h
+22
-2
paddle/fluid/operators/elementwise/elementwise_mod_op.h
paddle/fluid/operators/elementwise/elementwise_mod_op.h
+8
-2
python/paddle/fluid/tests/unittests/test_elementwise_floordiv_op.py
...dle/fluid/tests/unittests/test_elementwise_floordiv_op.py
+21
-0
python/paddle/fluid/tests/unittests/test_elementwise_mod_op.py
...n/paddle/fluid/tests/unittests/test_elementwise_mod_op.py
+16
-0
未找到文件。
paddle/fluid/operators/elementwise/elementwise_floordiv_op.h
浏览文件 @
29494d70
...
...
@@ -26,14 +26,34 @@ namespace operators {
template
<
typename
T
>
struct
FloorDivFunctor
{
inline
HOSTDEVICE
T
operator
()(
T
a
,
T
b
)
const
{
return
static_cast
<
T
>
(
floor
(
a
/
b
));
#ifdef __CUDA_ARCH__
if
(
b
==
0
)
{
printf
(
"Error: Divide by zero encounter in floor_divide
\n
"
);
asm
(
"trap;"
);
}
#else
if
(
b
==
0
)
PADDLE_THROW
(
platform
::
errors
::
InvalidArgument
(
"Divide by zero encounter in floor_divide"
));
#endif
return
static_cast
<
T
>
(
std
::
trunc
(
a
/
b
));
}
};
template
<
typename
T
>
struct
InverseFloorDivFunctor
{
inline
HOSTDEVICE
T
operator
()(
T
a
,
T
b
)
const
{
return
static_cast
<
T
>
(
floor
(
b
/
a
));
#ifdef __CUDA_ARCH__
if
(
a
==
0
)
{
printf
(
"Error: Divide by zero encounter in floor_divide
\n
"
);
asm
(
"trap;"
);
}
#else
if
(
a
==
0
)
PADDLE_THROW
(
platform
::
errors
::
InvalidArgument
(
"Divide by zero encounter in floor_divide"
));
#endif
return
static_cast
<
T
>
(
std
::
trunc
(
b
/
a
));
}
};
...
...
paddle/fluid/operators/elementwise/elementwise_mod_op.h
浏览文件 @
29494d70
...
...
@@ -24,13 +24,19 @@ namespace operators {
template
<
typename
T
>
struct
ModFunctor
{
inline
HOSTDEVICE
T
operator
()(
T
a
,
T
b
)
const
{
return
a
%
b
;
}
inline
HOSTDEVICE
T
operator
()(
T
a
,
T
b
)
const
{
T
res
=
a
%
b
;
if
((
res
!=
0
)
&&
((
res
<
0
)
!=
(
b
<
0
)))
res
+=
b
;
return
res
;
}
};
template
<
typename
T
>
struct
ModFunctorFP
{
inline
HOSTDEVICE
T
operator
()(
T
a
,
T
b
)
const
{
return
fmod
(
b
+
fmod
(
a
,
b
),
b
);
T
res
=
fmod
(
a
,
b
);
if
((
res
!=
0
)
&&
((
b
<
0
)
!=
(
res
<
0
)))
res
+=
b
;
return
res
;
}
};
...
...
python/paddle/fluid/tests/unittests/test_elementwise_floordiv_op.py
浏览文件 @
29494d70
...
...
@@ -193,6 +193,27 @@ class TestFloorDivideAPI(unittest.TestCase):
z_expected
=
np
.
array
([
2.
,
0.
,
2.
])
self
.
assertEqual
((
z_expected
==
z
.
numpy
()).
all
(),
True
)
with
fluid
.
dygraph
.
guard
(
fluid
.
CPUPlace
()):
# divide by zero
np_x
=
np
.
array
([
2
,
3
,
4
])
np_y
=
np
.
array
([
0
])
x
=
paddle
.
to_tensor
(
np_x
)
y
=
paddle
.
to_tensor
(
np_y
)
try
:
z
=
x
//
y
except
Exception
as
e
:
print
(
"Error: Divide by zero encounter in floor_divide
\n
"
)
# divide by zero
np_x
=
np
.
array
([
2
])
np_y
=
np
.
array
([
0
,
0
,
0
])
x
=
paddle
.
to_tensor
(
np_x
,
dtype
=
"int32"
)
y
=
paddle
.
to_tensor
(
np_y
,
dtype
=
"int32"
)
try
:
z
=
x
//
y
except
Exception
as
e
:
print
(
"Error: Divide by zero encounter in floor_divide
\n
"
)
if
__name__
==
'__main__'
:
unittest
.
main
()
python/paddle/fluid/tests/unittests/test_elementwise_mod_op.py
浏览文件 @
29494d70
...
...
@@ -204,6 +204,22 @@ class TestRemainderAPI(unittest.TestCase):
z_expected
=
np
.
array
([
1.
,
0.
,
1.
,
1.
,
0.
,
1.
])
self
.
assertEqual
((
z_expected
==
z
.
numpy
()).
all
(),
True
)
np_x
=
np
.
array
([
-
3.3
,
11.5
,
-
2
,
3.5
])
np_y
=
np
.
array
([
-
1.2
,
2.
,
3.3
,
-
2.3
])
x
=
paddle
.
to_tensor
(
np_x
)
y
=
paddle
.
to_tensor
(
np_y
)
z
=
x
%
y
z_expected
=
np
.
array
([
-
0.9
,
1.5
,
1.3
,
-
1.1
])
self
.
assertEqual
(
np
.
allclose
(
z_expected
,
z
.
numpy
()),
True
)
np_x
=
np
.
array
([
-
3
,
11
,
-
2
,
3
])
np_y
=
np
.
array
([
-
1
,
2
,
3
,
-
2
])
x
=
paddle
.
to_tensor
(
np_x
,
dtype
=
"int64"
)
y
=
paddle
.
to_tensor
(
np_y
,
dtype
=
"int64"
)
z
=
x
%
y
z_expected
=
np
.
array
([
0
,
1
,
1
,
-
1
])
self
.
assertEqual
(
np
.
allclose
(
z_expected
,
z
.
numpy
()),
True
)
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录