Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
21bf1ae9
P
Paddle
项目概览
Crayon鑫
/
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看板
提交
21bf1ae9
编写于
7月 15, 2021
作者:
Q
Qi Li
提交者:
GitHub
7月 15, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Revert "[NPU] add ops of bce_loss logical_and logical_or, test=develop (#34159)"
This reverts commit
9e18114f
.
上级
62840afa
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
100 addition
and
610 deletion
+100
-610
paddle/fluid/operators/bce_loss_op_npu.cc
paddle/fluid/operators/bce_loss_op_npu.cc
+0
-77
paddle/fluid/operators/controlflow/logical_op_npu.cc
paddle/fluid/operators/controlflow/logical_op_npu.cc
+12
-47
python/paddle/fluid/tests/unittests/npu/test_bce_loss_npu.py
python/paddle/fluid/tests/unittests/npu/test_bce_loss_npu.py
+0
-266
python/paddle/fluid/tests/unittests/npu/test_logical_op_npu.py
...n/paddle/fluid/tests/unittests/npu/test_logical_op_npu.py
+88
-220
未找到文件。
paddle/fluid/operators/bce_loss_op_npu.cc
已删除
100644 → 0
浏览文件 @
62840afa
/* Copyright (c) 2021 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/bce_loss_op.h"
#include "paddle/fluid/operators/npu_op_runner.h"
namespace
paddle
{
namespace
operators
{
using
Tensor
=
framework
::
Tensor
;
template
<
typename
DeviceContext
,
typename
T
>
class
BCELossNPUKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
auto
*
x
=
ctx
.
Input
<
Tensor
>
(
"X"
);
auto
*
labels
=
ctx
.
Input
<
Tensor
>
(
"Label"
);
auto
*
out
=
ctx
.
Output
<
Tensor
>
(
"Out"
);
out
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
auto
stream
=
ctx
.
template
device_context
<
paddle
::
platform
::
NPUDeviceContext
>()
.
stream
();
const
auto
&
runner
=
NpuOpRunner
(
"BinaryCrossEntropy"
,
{
*
x
,
*
labels
},
{
*
out
},
{{
"reduction"
,
"none"
}});
runner
.
Run
(
stream
);
}
};
template
<
typename
DeviceContext
,
typename
T
>
class
BCELossGradNPUKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
auto
*
x
=
ctx
.
Input
<
Tensor
>
(
"X"
);
auto
*
labels
=
ctx
.
Input
<
Tensor
>
(
"Label"
);
auto
*
dout
=
ctx
.
Input
<
Tensor
>
(
framework
::
GradVarName
(
"Out"
));
auto
*
dx
=
ctx
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"X"
));
dx
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
auto
stream
=
ctx
.
template
device_context
<
paddle
::
platform
::
NPUDeviceContext
>()
.
stream
();
const
auto
&
runner
=
NpuOpRunner
(
"BinaryCrossEntropyGrad"
,
{
*
x
,
*
labels
,
*
dout
},
{
*
dx
},
{{
"reduction"
,
"none"
}});
runner
.
Run
(
stream
);
}
};
}
// namespace operators
}
// namespace paddle
namespace
ops
=
paddle
::
operators
;
namespace
plat
=
paddle
::
platform
;
REGISTER_OP_NPU_KERNEL
(
bce_loss
,
ops
::
BCELossNPUKernel
<
plat
::
CUDADeviceContext
,
float
>
,
ops
::
BCELossNPUKernel
<
plat
::
CUDADeviceContext
,
plat
::
float16
>
);
REGISTER_OP_NPU_KERNEL
(
bce_loss_grad
,
ops
::
BCELossGradNPUKernel
<
plat
::
CUDADeviceContext
,
float
>
,
ops
::
BCELossGradNPUKernel
<
plat
::
CUDADeviceContext
,
plat
::
float16
>
);
paddle/fluid/operators/controlflow/logical_op_npu.cc
浏览文件 @
21bf1ae9
...
...
@@ -12,6 +12,10 @@ 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. */
#ifdef PADDLE_WITH_ASCEND_CL
#include <memory>
#include <string>
#include "paddle/fluid/operators/controlflow/logical_op.h"
#include "paddle/fluid/operators/npu_op_runner.h"
...
...
@@ -25,53 +29,18 @@ class LogicalNotNPUKernel : public framework::OpKernel<T> {
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
auto
*
x
=
ctx
.
Input
<
Tensor
>
(
"X"
);
auto
*
out
=
ctx
.
Output
<
Tensor
>
(
"Out"
);
out
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
auto
stream
=
ctx
.
template
device_context
<
paddle
::
platform
::
NPUDeviceContext
>()
.
stream
();
const
auto
&
runner
=
NpuOpRunner
(
"LogicalNot"
,
{
*
x
},
{
*
out
},
{});
runner
.
Run
(
stream
);
}
};
template
<
typename
DeviceContext
,
typename
T
>
class
LogicalOrNPUKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
auto
*
x
=
ctx
.
Input
<
framework
::
Tensor
>
(
"X"
);
auto
*
y
=
ctx
.
Input
<
framework
::
Tensor
>
(
"Y"
);
auto
*
out
=
ctx
.
Output
<
framework
::
Tensor
>
(
"Out"
);
out
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
auto
stream
=
ctx
.
template
device_context
<
paddle
::
platform
::
NPUDeviceContext
>()
.
stream
();
const
auto
&
runner
=
NpuOpRunner
(
"LogicalOr"
,
{
*
x
,
*
y
},
{
*
out
},
{});
runner
.
Run
(
stream
);
}
};
auto
*
out
=
ctx
.
Output
<
Tensor
>
(
"Out"
);
template
<
typename
DeviceContext
,
typename
T
>
class
LogicalAndPUKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
auto
*
x
=
ctx
.
Input
<
framework
::
Tensor
>
(
"X"
);
auto
*
y
=
ctx
.
Input
<
framework
::
Tensor
>
(
"Y"
);
auto
*
out
=
ctx
.
Output
<
framework
::
Tensor
>
(
"Out"
);
auto
place
=
ctx
.
GetPlace
();
out
->
mutable_data
<
T
>
(
ctx
.
GetPlace
()
);
out
->
mutable_data
<
T
>
(
place
);
auto
stream
=
ctx
.
template
device_context
<
paddle
::
platform
::
NPUDeviceContext
>()
.
stream
();
const
auto
&
runner
=
NpuOpRunner
(
"Logical
And"
,
{
*
x
,
*
y
},
{
*
out
},
{});
const
auto
&
runner
=
NpuOpRunner
(
"Logical
Not"
,
{
*
x
},
{
*
out
},
{});
runner
.
Run
(
stream
);
}
};
...
...
@@ -80,13 +49,9 @@ class LogicalAndPUKernel : public framework::OpKernel<T> {
}
// namespace paddle
namespace
ops
=
paddle
::
operators
;
namespace
plat
=
paddle
::
platform
;
REGISTER_OP_NPU_KERNEL
(
logical_not
,
ops
::
LogicalNotNPUKernel
<
plat
::
NPUDeviceContext
,
bool
>
);
REGISTER_OP_NPU_KERNEL
(
logical_or
,
ops
::
LogicalOrNPUKernel
<
plat
::
NPUDeviceContext
,
bool
>
);
REGISTER_OP_NPU_KERNEL
(
logical_not
,
ops
::
LogicalNotNPUKernel
<
paddle
::
platform
::
NPUDeviceContext
,
bool
>
);
REGISTER_OP_NPU_KERNEL
(
logical_and
,
ops
::
LogicalAndPUKernel
<
plat
::
NPUDeviceContext
,
bool
>
);
#endif
python/paddle/fluid/tests/unittests/npu/test_bce_loss_npu.py
已删除
100644 → 0
浏览文件 @
62840afa
# Copyright (c) 2021 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.
from
__future__
import
print_function
,
division
import
paddle
import
paddle.fluid
as
fluid
import
numpy
as
np
import
unittest
import
sys
sys
.
path
.
append
(
".."
)
from
op_test
import
OpTest
paddle
.
enable_static
()
def
test_static_layer
(
place
,
input_np
,
label_np
,
reduction
=
'mean'
,
weight_np
=
None
):
prog
=
paddle
.
static
.
Program
()
startup_prog
=
paddle
.
static
.
Program
()
with
paddle
.
static
.
program_guard
(
prog
,
startup_prog
):
input
=
paddle
.
fluid
.
data
(
name
=
'input'
,
shape
=
input_np
.
shape
,
dtype
=
'float32'
)
label
=
paddle
.
fluid
.
data
(
name
=
'label'
,
shape
=
label_np
.
shape
,
dtype
=
'float32'
)
if
weight_np
is
not
None
:
weight
=
paddle
.
fluid
.
data
(
name
=
'weight'
,
shape
=
weight_np
.
shape
,
dtype
=
'float32'
)
bce_loss
=
paddle
.
nn
.
loss
.
BCELoss
(
weight
=
weight
,
reduction
=
reduction
)
else
:
bce_loss
=
paddle
.
nn
.
loss
.
BCELoss
(
reduction
=
reduction
)
res
=
bce_loss
(
input
,
label
)
exe
=
paddle
.
static
.
Executor
(
place
)
static_result
=
exe
.
run
(
prog
,
feed
=
{
"input"
:
input_np
,
"label"
:
label_np
}
if
weight_np
is
None
else
{
"input"
:
input_np
,
"label"
:
label_np
,
"weight"
:
weight_np
},
fetch_list
=
[
res
])
return
static_result
def
test_static_functional
(
place
,
input_np
,
label_np
,
reduction
=
'mean'
,
weight_np
=
None
):
prog
=
paddle
.
static
.
Program
()
startup_prog
=
paddle
.
static
.
Program
()
with
paddle
.
static
.
program_guard
(
prog
,
startup_prog
):
input
=
paddle
.
fluid
.
data
(
name
=
'input'
,
shape
=
input_np
.
shape
,
dtype
=
'float32'
)
label
=
paddle
.
fluid
.
data
(
name
=
'label'
,
shape
=
label_np
.
shape
,
dtype
=
'float32'
)
if
weight_np
is
not
None
:
weight
=
paddle
.
fluid
.
data
(
name
=
'weight'
,
shape
=
weight_np
.
shape
,
dtype
=
'float32'
)
res
=
paddle
.
nn
.
functional
.
binary_cross_entropy
(
input
,
label
,
weight
=
weight
,
reduction
=
reduction
)
else
:
res
=
paddle
.
nn
.
functional
.
binary_cross_entropy
(
input
,
label
,
reduction
=
reduction
)
exe
=
paddle
.
static
.
Executor
(
place
)
static_result
=
exe
.
run
(
prog
,
feed
=
{
"input"
:
input_np
,
"label"
:
label_np
}
if
weight_np
is
None
else
{
"input"
:
input_np
,
"label"
:
label_np
,
"weight"
:
weight_np
},
fetch_list
=
[
res
])
return
static_result
def
test_dygraph_layer
(
place
,
input_np
,
label_np
,
reduction
=
'mean'
,
weight_np
=
None
):
paddle
.
disable_static
()
if
weight_np
is
not
None
:
weight
=
paddle
.
to_tensor
(
weight_np
)
bce_loss
=
paddle
.
nn
.
loss
.
BCELoss
(
weight
=
weight
,
reduction
=
reduction
)
else
:
bce_loss
=
paddle
.
nn
.
loss
.
BCELoss
(
reduction
=
reduction
)
dy_res
=
bce_loss
(
paddle
.
to_tensor
(
input_np
),
paddle
.
to_tensor
(
label_np
))
dy_result
=
dy_res
.
numpy
()
paddle
.
enable_static
()
return
dy_result
def
test_dygraph_functional
(
place
,
input_np
,
label_np
,
reduction
=
'mean'
,
weight_np
=
None
):
paddle
.
disable_static
()
input
=
paddle
.
to_tensor
(
input_np
)
label
=
paddle
.
to_tensor
(
label_np
)
if
weight_np
is
not
None
:
weight
=
paddle
.
to_tensor
(
weight_np
)
dy_res
=
paddle
.
nn
.
functional
.
binary_cross_entropy
(
input
,
label
,
weight
=
weight
,
reduction
=
reduction
)
else
:
dy_res
=
paddle
.
nn
.
functional
.
binary_cross_entropy
(
input
,
label
,
reduction
=
reduction
)
dy_result
=
dy_res
.
numpy
()
paddle
.
enable_static
()
return
dy_result
def
calc_bceloss
(
input_np
,
label_np
,
reduction
=
'mean'
,
weight_np
=
None
):
if
weight_np
is
None
:
expected
=
-
1
*
(
label_np
*
np
.
log
(
input_np
)
+
(
1.
-
label_np
)
*
np
.
log
(
1.
-
input_np
))
else
:
expected
=
-
1
*
weight_np
*
(
label_np
*
np
.
log
(
input_np
)
+
(
1.
-
label_np
)
*
np
.
log
(
1.
-
input_np
))
if
reduction
==
'mean'
:
expected
=
np
.
mean
(
expected
)
elif
reduction
==
'sum'
:
expected
=
np
.
sum
(
expected
)
else
:
expected
=
expected
return
expected
@
unittest
.
skipIf
(
not
paddle
.
is_compiled_with_npu
(),
"core is not compiled with NPU"
)
class
TestBCELoss
(
unittest
.
TestCase
):
def
test_BCELoss
(
self
):
input_np
=
np
.
random
.
uniform
(
0.1
,
0.8
,
size
=
(
20
,
30
)).
astype
(
np
.
float32
)
label_np
=
np
.
random
.
randint
(
0
,
2
,
size
=
(
20
,
30
)).
astype
(
np
.
float32
)
places
=
[
fluid
.
CPUPlace
()]
if
fluid
.
core
.
is_compiled_with_npu
():
places
.
append
(
fluid
.
NPUPlace
(
0
))
reductions
=
[
'sum'
,
'mean'
,
'none'
]
for
place
in
places
:
for
reduction
in
reductions
:
static_result
=
test_static_layer
(
place
,
input_np
,
label_np
,
reduction
)
dy_result
=
test_dygraph_layer
(
place
,
input_np
,
label_np
,
reduction
)
expected
=
calc_bceloss
(
input_np
,
label_np
,
reduction
)
self
.
assertTrue
(
np
.
allclose
(
static_result
,
expected
))
self
.
assertTrue
(
np
.
allclose
(
static_result
,
dy_result
))
self
.
assertTrue
(
np
.
allclose
(
dy_result
,
expected
))
static_functional
=
test_static_functional
(
place
,
input_np
,
label_np
,
reduction
)
dy_functional
=
test_dygraph_functional
(
place
,
input_np
,
label_np
,
reduction
)
self
.
assertTrue
(
np
.
allclose
(
static_functional
,
expected
))
self
.
assertTrue
(
np
.
allclose
(
static_functional
,
dy_functional
))
self
.
assertTrue
(
np
.
allclose
(
dy_functional
,
expected
))
def
test_BCELoss_weight
(
self
):
input_np
=
np
.
random
.
uniform
(
0.1
,
0.8
,
size
=
(
2
,
3
,
4
,
10
)).
astype
(
np
.
float32
)
label_np
=
np
.
random
.
randint
(
0
,
2
,
size
=
(
2
,
3
,
4
,
10
)).
astype
(
np
.
float32
)
weight_np
=
np
.
random
.
random
(
size
=
(
3
,
4
,
10
)).
astype
(
np
.
float32
)
place
=
fluid
.
NPUPlace
(
0
)
if
fluid
.
core
.
is_compiled_with_npu
(
)
else
fluid
.
CPUPlace
()
for
reduction
in
[
'sum'
,
'mean'
,
'none'
]:
static_result
=
test_static_layer
(
place
,
input_np
,
label_np
,
reduction
,
weight_np
=
weight_np
)
dy_result
=
test_dygraph_layer
(
place
,
input_np
,
label_np
,
reduction
,
weight_np
=
weight_np
)
expected
=
calc_bceloss
(
input_np
,
label_np
,
reduction
,
weight_np
=
weight_np
)
self
.
assertTrue
(
np
.
allclose
(
static_result
,
expected
))
self
.
assertTrue
(
np
.
allclose
(
static_result
,
dy_result
))
self
.
assertTrue
(
np
.
allclose
(
dy_result
,
expected
))
static_functional
=
test_static_functional
(
place
,
input_np
,
label_np
,
reduction
,
weight_np
=
weight_np
)
dy_functional
=
test_dygraph_functional
(
place
,
input_np
,
label_np
,
reduction
,
weight_np
=
weight_np
)
self
.
assertTrue
(
np
.
allclose
(
static_functional
,
expected
))
self
.
assertTrue
(
np
.
allclose
(
static_functional
,
dy_functional
))
self
.
assertTrue
(
np
.
allclose
(
dy_functional
,
expected
))
def
test_BCELoss_error
(
self
):
paddle
.
disable_static
(
paddle
.
NPUPlace
(
0
))
self
.
assertRaises
(
ValueError
,
paddle
.
nn
.
loss
.
BCELoss
,
reduction
=
"unsupport reduction"
)
input
=
paddle
.
to_tensor
([[
0.1
,
0.3
]],
dtype
=
'float32'
)
label
=
paddle
.
to_tensor
([[
0.0
,
1.0
]],
dtype
=
'float32'
)
self
.
assertRaises
(
ValueError
,
paddle
.
nn
.
functional
.
binary_cross_entropy
,
input
=
input
,
label
=
label
,
reduction
=
"unsupport reduction"
)
paddle
.
enable_static
()
def
bce_loss
(
input
,
label
):
return
-
1
*
(
label
*
np
.
log
(
input
)
+
(
1.
-
label
)
*
np
.
log
(
1.
-
input
))
@
unittest
.
skipIf
(
not
paddle
.
is_compiled_with_npu
(),
"core is not compiled with NPU"
)
class
TestBceLossOp
(
OpTest
):
def
setUp
(
self
):
self
.
set_npu
()
self
.
init_test_case
()
self
.
op_type
=
"bce_loss"
input_np
=
np
.
random
.
uniform
(
0.1
,
0.8
,
self
.
shape
).
astype
(
"float32"
)
label_np
=
np
.
random
.
randint
(
0
,
2
,
self
.
shape
).
astype
(
"float32"
)
output_np
=
bce_loss
(
input_np
,
label_np
)
self
.
inputs
=
{
'X'
:
input_np
,
'Label'
:
label_np
}
self
.
outputs
=
{
'Out'
:
output_np
}
def
set_npu
(
self
):
self
.
__class__
.
use_npu
=
True
self
.
place
=
paddle
.
NPUPlace
(
0
)
def
test_check_output
(
self
):
self
.
check_output_with_place
(
self
.
place
)
def
test_check_grad
(
self
):
self
.
check_grad_with_place
(
self
.
place
,
[
'X'
],
'Out'
)
def
init_test_case
(
self
):
self
.
shape
=
[
10
,
10
]
@
unittest
.
skipIf
(
not
paddle
.
is_compiled_with_npu
(),
"core is not compiled with NPU"
)
class
TestBceLossOpCase1
(
OpTest
):
def
init_test_cast
(
self
):
self
.
shape
=
[
2
,
3
,
4
,
5
]
@
unittest
.
skipIf
(
not
paddle
.
is_compiled_with_npu
(),
"core is not compiled with NPU"
)
class
TestBceLossOpCase2
(
OpTest
):
def
init_test_cast
(
self
):
self
.
shape
=
[
2
,
3
,
20
]
if
__name__
==
"__main__"
:
unittest
.
main
()
python/paddle/fluid/tests/unittests/npu/test_logical_op_npu.py
浏览文件 @
21bf1ae9
#
Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
# Copyright (c) 2021 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
# 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,
...
...
@@ -14,240 +14,108 @@
from
__future__
import
print_function
import
numpy
as
np
import
unittest
import
sys
sys
.
path
.
append
(
".."
)
import
op_test
import
unittest
import
numpy
as
np
from
op_test
import
OpTest
import
paddle
import
paddle.fluid
as
fluid
from
paddle.static
import
Program
,
program_guard
TEST_META_OP_DATA
=
[{
'op_str'
:
'logical_and'
,
'binary_op'
:
True
},
{
'op_str'
:
'logical_or'
,
'binary_op'
:
True
},
{
'op_str'
:
'logical_not'
,
'binary_op'
:
False
}]
TEST_META_SHAPE_DATA
=
{
'XDimLargerThanYDim1'
:
{
'x_shape'
:
[
2
,
3
,
4
,
5
],
'y_shape'
:
[
4
,
5
]
},
'XDimLargerThanYDim2'
:
{
'x_shape'
:
[
2
,
3
,
4
,
5
],
'y_shape'
:
[
4
,
1
]
},
'XDimLargerThanYDim3'
:
{
'x_shape'
:
[
2
,
3
,
4
,
5
],
'y_shape'
:
[
1
,
4
,
1
]
},
'XDimLargerThanYDim4'
:
{
'x_shape'
:
[
2
,
3
,
4
,
5
],
'y_shape'
:
[
3
,
4
,
1
]
},
'XDimLargerThanYDim5'
:
{
'x_shape'
:
[
2
,
3
,
1
,
5
],
'y_shape'
:
[
3
,
1
,
1
]
},
'XDimLessThanYDim1'
:
{
'x_shape'
:
[
4
,
1
],
'y_shape'
:
[
2
,
3
,
4
,
5
]
},
'XDimLessThanYDim2'
:
{
'x_shape'
:
[
1
,
4
,
1
],
'y_shape'
:
[
2
,
3
,
4
,
5
]
},
'XDimLessThanYDim3'
:
{
'x_shape'
:
[
3
,
4
,
1
],
'y_shape'
:
[
2
,
3
,
4
,
5
]
},
'XDimLessThanYDim4'
:
{
'x_shape'
:
[
3
,
1
,
1
],
'y_shape'
:
[
2
,
3
,
1
,
5
]
},
'XDimLessThanYDim5'
:
{
'x_shape'
:
[
4
,
5
],
'y_shape'
:
[
2
,
3
,
4
,
5
]
},
'Axis1InLargerDim'
:
{
'x_shape'
:
[
1
,
4
,
5
],
'y_shape'
:
[
2
,
3
,
1
,
5
]
},
'EqualDim1'
:
{
'x_shape'
:
[
10
,
7
],
'y_shape'
:
[
10
,
7
]
},
'EqualDim2'
:
{
'x_shape'
:
[
1
,
1
,
4
,
5
],
'y_shape'
:
[
2
,
3
,
1
,
5
]
}
}
TEST_META_WRONG_SHAPE_DATA
=
{
'ErrorDim1'
:
{
'x_shape'
:
[
2
,
3
,
4
,
5
],
'y_shape'
:
[
3
,
4
]
},
'ErrorDim2'
:
{
'x_shape'
:
[
2
,
3
,
4
,
5
],
'y_shape'
:
[
4
,
3
]
}
}
def
run_static
(
x_np
,
y_np
,
op_str
,
use_npu
=
False
,
binary_op
=
True
):
paddle
.
enable_static
()
startup_program
=
fluid
.
Program
()
main_program
=
fluid
.
Program
()
place
=
paddle
.
CPUPlace
()
if
use_npu
and
fluid
.
core
.
is_compiled_with_npu
():
place
=
paddle
.
NPUPlace
(
0
)
exe
=
fluid
.
Executor
(
place
)
with
fluid
.
program_guard
(
main_program
,
startup_program
):
x
=
paddle
.
static
.
data
(
name
=
'x'
,
shape
=
x_np
.
shape
,
dtype
=
'bool'
)
op
=
getattr
(
paddle
,
op_str
)
feed_list
=
{
'x'
:
x_np
}
if
not
binary_op
:
res
=
op
(
x
)
else
:
y
=
paddle
.
static
.
data
(
name
=
'y'
,
shape
=
y_np
.
shape
,
dtype
=
'bool'
)
feed_list
[
'y'
]
=
y_np
res
=
op
(
x
,
y
)
exe
.
run
(
startup_program
)
static_result
=
exe
.
run
(
main_program
,
feed
=
feed_list
,
fetch_list
=
[
res
])
return
static_result
def
run_dygraph
(
x_np
,
y_np
,
op_str
,
use_npu
=
False
,
binary_op
=
True
):
place
=
paddle
.
CPUPlace
()
if
use_npu
and
fluid
.
core
.
is_compiled_with_npu
():
place
=
paddle
.
NPUPlace
(
0
)
paddle
.
disable_static
(
place
)
op
=
getattr
(
paddle
,
op_str
)
x
=
paddle
.
to_tensor
(
x_np
)
if
not
binary_op
:
dygraph_result
=
op
(
x
)
else
:
y
=
paddle
.
to_tensor
(
y_np
)
dygraph_result
=
op
(
x
,
y
)
return
dygraph_result
def
np_data_generator
(
np_shape
,
*
args
,
**
kwargs
):
return
np
.
random
.
choice
(
a
=
[
True
,
False
],
size
=
np_shape
).
astype
(
bool
)
def
test
(
unit_test
,
use_npu
=
False
,
test_error
=
False
):
for
op_data
in
TEST_META_OP_DATA
:
meta_data
=
dict
(
op_data
)
meta_data
[
'use_npu'
]
=
use_npu
np_op
=
getattr
(
np
,
meta_data
[
'op_str'
])
META_DATA
=
dict
(
TEST_META_SHAPE_DATA
)
if
test_error
:
META_DATA
=
dict
(
TEST_META_WRONG_SHAPE_DATA
)
for
shape_data
in
META_DATA
.
values
():
meta_data
[
'x_np'
]
=
np_data_generator
(
shape_data
[
'x_shape'
])
meta_data
[
'y_np'
]
=
np_data_generator
(
shape_data
[
'y_shape'
])
if
meta_data
[
'binary_op'
]
and
test_error
:
# catch C++ Exception
unit_test
.
assertRaises
(
BaseException
,
run_static
,
**
meta_data
)
unit_test
.
assertRaises
(
BaseException
,
run_dygraph
,
**
meta_data
)
continue
static_result
=
run_static
(
**
meta_data
)
dygraph_result
=
run_dygraph
(
**
meta_data
)
if
meta_data
[
'binary_op'
]:
np_result
=
np_op
(
meta_data
[
'x_np'
],
meta_data
[
'y_np'
])
else
:
np_result
=
np_op
(
meta_data
[
'x_np'
])
unit_test
.
assertTrue
((
static_result
==
np_result
).
all
())
unit_test
.
assertTrue
((
dygraph_result
.
numpy
()
==
np_result
).
all
())
def
test_type_error
(
unit_test
,
use_npu
,
type_str_map
):
def
check_type
(
op_str
,
x
,
y
,
binary_op
):
op
=
getattr
(
paddle
,
op_str
)
error_type
=
TypeError
if
isinstance
(
x
,
np
.
ndarray
):
x
=
paddle
.
to_tensor
(
x
)
y
=
paddle
.
to_tensor
(
y
)
error_type
=
BaseException
if
binary_op
:
if
type_str_map
[
'x'
]
!=
'bool'
or
type_str_map
[
'y'
]
!=
'bool'
:
unit_test
.
assertRaises
(
error_type
,
op
,
x
=
x
,
y
=
y
)
if
not
fluid
.
in_dygraph_mode
():
unit_test
.
assertRaises
(
error_type
,
op
,
x
=
x
,
y
=
y
,
out
=
1
)
else
:
if
type_str_map
[
'x'
]
!=
'bool'
:
unit_test
.
assertRaises
(
error_type
,
op
,
x
=
x
)
if
not
fluid
.
in_dygraph_mode
():
unit_test
.
assertRaises
(
error_type
,
op
,
x
=
x
,
out
=
1
)
place
=
paddle
.
CPUPlace
()
if
use_npu
and
fluid
.
core
.
is_compiled_with_npu
():
place
=
paddle
.
NPUPlace
(
0
)
for
op_data
in
TEST_META_OP_DATA
:
meta_data
=
dict
(
op_data
)
binary_op
=
meta_data
[
'binary_op'
]
paddle
.
disable_static
(
place
)
x
=
np
.
random
.
choice
(
a
=
[
0
,
1
],
size
=
[
10
]).
astype
(
type_str_map
[
'x'
])
y
=
np
.
random
.
choice
(
a
=
[
0
,
1
],
size
=
[
10
]).
astype
(
type_str_map
[
'y'
])
check_type
(
meta_data
[
'op_str'
],
x
,
y
,
binary_op
)
paddle
.
enable_static
()
startup_program
=
paddle
.
static
.
Program
()
main_program
=
paddle
.
static
.
Program
()
with
paddle
.
static
.
program_guard
(
main_program
,
startup_program
):
x
=
paddle
.
static
.
data
(
name
=
'x'
,
shape
=
[
10
],
dtype
=
type_str_map
[
'x'
])
y
=
paddle
.
static
.
data
(
name
=
'y'
,
shape
=
[
10
],
dtype
=
type_str_map
[
'y'
])
check_type
(
meta_data
[
'op_str'
],
x
,
y
,
binary_op
)
def
type_map_factory
():
x_type_list
=
[
'float32'
,
'float64'
,
'int32'
,
'int64'
,
'bool'
]
y_type_list
=
[
'float32'
,
'float64'
,
'int32'
,
'int64'
,
'bool'
]
return
[{
'x'
:
x_type
,
'y'
:
y_type
}
for
x_type
in
x_type_list
for
y_type
in
y_type_list
]
paddle
.
enable_static
()
SEED
=
2021
@
unittest
.
skipIf
(
not
paddle
.
is_compiled_with_npu
(),
"core is not compiled with NPU"
)
class
TestCPU
(
unittest
.
TestCase
):
def
test
(
self
):
test
(
self
)
class
TestLogicalNot
(
OpTest
):
def
setUp
(
self
):
self
.
set_npu
()
self
.
op_type
=
"logical_not"
self
.
place
=
paddle
.
NPUPlace
(
4
)
self
.
init_dtype
()
np
.
random
.
seed
(
SEED
)
x
=
np
.
random
.
uniform
(
1
,
2
,
[
11
,
17
]).
astype
(
self
.
dtype
)
out
=
np
.
logical_not
(
x
)
def
test_error
(
self
):
test
(
self
,
False
,
True
)
self
.
inputs
=
{
'X'
:
OpTest
.
np_dtype_to_fluid_dtype
(
x
)}
self
.
attrs
=
{}
self
.
outputs
=
{
'Out'
:
out
}
def
test_type_error
(
self
):
type_map_list
=
type_map_factory
()
for
type_map
in
type_map_list
:
test_type_error
(
self
,
False
,
type_map
)
def
set_npu
(
self
):
self
.
__class__
.
use_npu
=
True
def
init_dtype
(
self
):
self
.
dtype
=
np
.
bool
def
test_check_output
(
self
):
self
.
check_output_with_place
(
self
.
place
,
check_dygraph
=
False
)
# TODO(ascendrc): Add grad test
# def test_check_grad(self):
# if self.dtype == np.float16:
# return
# self.check_grad(['X'], 'Out')
#
@
unittest
.
skipIf
(
not
paddle
.
is_compiled_with_npu
(),
"core is not compiled with NPU"
)
class
TestNPU
(
unittest
.
TestCase
):
def
test
(
self
):
test
(
self
,
True
)
class
TestLogcialNotNet
(
unittest
.
TestCase
):
def
_test
(
self
,
run_npu
=
True
):
main_prog
=
paddle
.
static
.
Program
()
startup_prog
=
paddle
.
static
.
Program
()
main_prog
.
random_seed
=
SEED
startup_prog
.
random_seed
=
SEED
np
.
random
.
seed
(
SEED
)
a_np
=
np
.
random
.
random
(
size
=
(
32
,
32
)).
astype
(
'bool'
)
label_np
=
np
.
random
.
randint
(
2
,
size
=
(
32
,
1
)).
astype
(
'int64'
)
with
paddle
.
static
.
program_guard
(
main_prog
,
startup_prog
):
a
=
paddle
.
static
.
data
(
name
=
"a"
,
shape
=
[
32
,
32
],
dtype
=
'bool'
)
label
=
paddle
.
static
.
data
(
name
=
"label"
,
shape
=
[
32
,
1
],
dtype
=
'int64'
)
c
=
paddle
.
logical_not
(
a
)
d
=
paddle
.
cast
(
c
,
dtype
=
"float32"
)
fc_1
=
fluid
.
layers
.
fc
(
input
=
d
,
size
=
128
)
prediction
=
fluid
.
layers
.
fc
(
input
=
fc_1
,
size
=
2
,
act
=
'softmax'
)
cost
=
fluid
.
layers
.
cross_entropy
(
input
=
prediction
,
label
=
label
)
loss
=
fluid
.
layers
.
reduce_mean
(
cost
)
sgd
=
fluid
.
optimizer
.
SGD
(
learning_rate
=
0.01
)
sgd
.
minimize
(
loss
)
if
run_npu
:
place
=
paddle
.
NPUPlace
(
4
)
else
:
place
=
paddle
.
CPUPlace
()
exe
=
paddle
.
static
.
Executor
(
place
)
exe
.
run
(
startup_prog
)
print
(
"Start run on {}"
.
format
(
place
))
for
epoch
in
range
(
100
):
pred_res
,
loss_res
=
exe
.
run
(
main_prog
,
feed
=
{
"a"
:
a_np
,
"label"
:
label_np
},
fetch_list
=
[
prediction
,
loss
])
if
epoch
%
10
==
0
:
print
(
"Epoch {} | Prediction[0]: {}, Loss: {}"
.
format
(
epoch
,
pred_res
[
0
],
loss_res
))
return
pred_res
,
loss_res
def
test_error
(
self
):
test
(
self
,
True
,
True
)
def
test_npu
(
self
):
cpu_pred
,
cpu_loss
=
self
.
_test
(
False
)
npu_pred
,
npu_loss
=
self
.
_test
(
True
)
def
test_type_error
(
self
):
type_map_list
=
type_map_factory
()
for
type_map
in
type_map_list
:
test_type_error
(
self
,
True
,
type_map
)
self
.
assertTrue
(
np
.
allclose
(
npu_pred
,
cpu_pred
))
self
.
assertTrue
(
np
.
allclose
(
npu_loss
,
cpu_loss
))
if
__name__
==
'__main__'
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录