Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
79e08ece
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
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看板
体验新版 GitCode,发现更多精彩内容 >>
提交
79e08ece
编写于
10月 20, 2019
作者:
D
danleifeng
提交者:
gongweibao
10月 20, 2019
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add assertions on whether elementwise_div divison is zero (#20618)
上级
fd49ebcb
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
56 addition
and
9 deletion
+56
-9
paddle/fluid/operators/elementwise/elementwise_div_op.cc
paddle/fluid/operators/elementwise/elementwise_div_op.cc
+3
-6
paddle/fluid/operators/elementwise/elementwise_op_function.cu.h
.../fluid/operators/elementwise/elementwise_op_function.cu.h
+33
-3
python/paddle/fluid/tests/unittests/test_elementwise_div_op.py
...n/paddle/fluid/tests/unittests/test_elementwise_div_op.py
+20
-0
未找到文件。
paddle/fluid/operators/elementwise/elementwise_div_op.cc
浏览文件 @
79e08ece
...
...
@@ -32,6 +32,8 @@ struct SameDimsElemwiseDiv<
}
};
// use default div function for int32/int64 type because of divison zero
// checking.
template
<
typename
T
>
struct
SameDimsElemwiseDiv
<
platform
::
CPUDeviceContext
,
T
,
...
...
@@ -39,12 +41,7 @@ struct SameDimsElemwiseDiv<
void
operator
()(
const
framework
::
ExecutionContext
&
ctx
,
const
framework
::
Tensor
*
x
,
const
framework
::
Tensor
*
y
,
framework
::
Tensor
*
z
)
{
auto
eigen_x
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
x
);
auto
eigen_y
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
y
);
auto
eigen_z
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
z
);
auto
&
place
=
*
ctx
.
template
device_context
<
platform
::
CPUDeviceContext
>()
.
eigen_device
();
eigen_z
.
device
(
place
)
=
eigen_x
/
eigen_y
;
default_elementwise_div
<
platform
::
CPUDeviceContext
,
T
>
(
ctx
,
x
,
y
,
z
);
}
};
...
...
paddle/fluid/operators/elementwise/elementwise_op_function.cu.h
浏览文件 @
79e08ece
/* Copyright (c) 2016 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.
...
...
@@ -12,9 +15,9 @@ limitations under the License. */
#pragma once
#include <glog/logging.h>
#include "paddle/fluid/platform/enforce.h"
#include "paddle/fluid/platform/float16.h"
#include "paddle/fluid/platform/hostdevice.h"
#define PADDLE_CUDA_THREAD_SIZE 512
#ifdef PADDLE_WITH_CUDA
...
...
@@ -29,11 +32,14 @@ limitations under the License. */
#define __h2div h2div
#endif
#define DIV_ERROR_INFO \
"InvalidArgumentError: Integer division by zero encountered in " \
"divide.Please check.\n"
namespace
paddle
{
namespace
operators
{
#define DEFINE_SIMPLE_BINARY_FUNCTOR(Func, expr) \
template <typename T
>
\
template <typename T
, class Enable = void>
\
struct Func##Functor { \
inline HOSTDEVICE T operator()(const T& a, const T& b) const { \
return a expr b; \
...
...
@@ -46,8 +52,18 @@ DEFINE_SIMPLE_BINARY_FUNCTOR(Mul, *)
DEFINE_SIMPLE_BINARY_FUNCTOR
(
Div
,
/
)
#undef DEFINE_SIMPLE_BINARY_FUNCTOR
// special div functor for int32/int64. check divison has a zero
template
<
typename
T
>
struct
DivFunctor
<
T
,
typename
std
::
enable_if
<
std
::
is_integral
<
T
>::
value
>::
type
>
{
inline
HOSTDEVICE
T
operator
()(
const
T
&
a
,
const
T
&
b
)
const
{
PADDLE_ENFORCE
(
b
!=
0
,
DIV_ERROR_INFO
);
return
a
/
b
;
}
};
#define DEFINE_SIMPLE_CUDA_BINARY_FUNCTOR(Func, expr) \
template <typename T
>
\
template <typename T
, class Enable = void>
\
struct Func##RangeFunctor { \
Func##RangeFunctor(const T* x, const T* y, T* z) : x_(x), y_(y), z_(z) {} \
inline HOSTDEVICE void operator()(size_t id) const { \
...
...
@@ -63,6 +79,20 @@ DEFINE_SIMPLE_CUDA_BINARY_FUNCTOR(Mul, *)
DEFINE_SIMPLE_CUDA_BINARY_FUNCTOR
(
Div
,
/
)
#undef DEFINE_SIMPLE_CUDA_BINARY_FUNCTOR
// special div functor for int32/int64. check divison has a zero
template
<
typename
T
>
struct
DivRangeFunctor
<
T
,
typename
std
::
enable_if
<
std
::
is_integral
<
T
>::
value
>::
type
>
{
DivRangeFunctor
(
const
T
*
x
,
const
T
*
y
,
T
*
z
)
:
x_
(
x
),
y_
(
y
),
z_
(
z
)
{}
inline
HOSTDEVICE
void
operator
()(
size_t
id
)
const
{
PADDLE_ENFORCE
(
y_
[
id
]
!=
0
,
DIV_ERROR_INFO
);
z_
[
id
]
=
x_
[
id
]
/
y_
[
id
];
}
const
T
*
x_
;
const
T
*
y_
;
T
*
z_
;
};
#ifdef PADDLE_CUDA_FP16
inline
DEVICE
half2
half2_add
(
const
half2
&
a
,
const
half2
&
b
)
{
#if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 530
...
...
python/paddle/fluid/tests/unittests/test_elementwise_div_op.py
浏览文件 @
79e08ece
...
...
@@ -151,6 +151,26 @@ class TestElementwiseDivOp_broadcast_5(ElementwiseDivOp):
self
.
outputs
=
{
'Out'
:
np
.
divide
(
self
.
inputs
[
'X'
],
self
.
inputs
[
'Y'
])}
class
TestElementwiseDivOp_INT
(
OpTest
):
def
setUp
(
self
):
self
.
op_type
=
"elementwise_div"
self
.
dtype
=
np
.
int32
self
.
init_dtype
()
self
.
inputs
=
{
'X'
:
np
.
random
.
randint
(
1
,
5
,
size
=
[
2
,
3
]).
astype
(
self
.
dtype
),
'Y'
:
np
.
random
.
randint
(
1
,
5
,
size
=
[
2
,
3
]).
astype
(
self
.
dtype
)
}
self
.
outputs
=
{
'Out'
:
self
.
inputs
[
'X'
]
//
self
.
inputs
[
'Y'
]}
def
test_check_output
(
self
):
self
.
check_output
()
def
init_dtype
(
self
):
pass
class
TestElementwiseDivOpFp16
(
ElementwiseDivOp
):
def
init_dtype
(
self
):
self
.
dtype
=
np
.
float16
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录