Skip to content

Commit c060e99

Browse files
authored
[GCU] Add kernels (#837)
1 parent e1fffca commit c060e99

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+14302
-191
lines changed

backends/gcu/README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ cd backends/gcu
2424

2525
# 2) Before compiling, you need to ensure that the PaddlePaddle installation package is installed in the environment.
2626
# Just install the PaddlePaddle CPU version directly.
27-
pip install paddlepaddle==0.0.0 -f https://www.paddlepaddle.org.cn/whl/linux/cpu-mkl/develop.html
27+
python -m pip install paddlepaddle==0.0.0 -f https://www.paddlepaddle.org.cn/whl/linux/cpu-mkl/develop.html
2828

2929
# 3) Start compiling, and submodules will be downloaded on demand during compilation.
3030
mkdir -p build && cd build
3131
cmake .. -DWITH_TESTING=ON -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
3232
make -j $(nproc)
3333

3434
# 4) The compiled product is in the build/dist path and installed using pip.
35-
pip install --force-reinstall -U build/dist/paddle_custom_gcu*.whl
35+
python -m pip install --force-reinstall -U build/dist/paddle_custom_gcu*.whl
3636
```
3737

3838
### Functional Verification
@@ -49,4 +49,7 @@ python -c "import paddle_custom_device; paddle_custom_device.gcu.version()"
4949
version: 0.0.0.ffc0377-2.4.1
5050
commit: ffc037739c55508532ee67b565517be2b4ae584d
5151
plugin version: 0.0.1
52+
53+
# 3) Unit test, compiled with -DWITH_TESTING=ON and executed in the build directory.
54+
ctest
5255
```

backends/gcu/README_cn.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ cd PaddleCustomDevice
2323
cd backends/gcu
2424

2525
# 2) 编译之前需确保环境下装有飞桨安装包,直接安装飞桨CPU版本即可
26-
pip install paddlepaddle==0.0.0 -f https://www.paddlepaddle.org.cn/whl/linux/cpu-mkl/develop.html
26+
python -m pip install paddlepaddle==0.0.0 -f https://www.paddlepaddle.org.cn/whl/linux/cpu-mkl/develop.html
2727

2828
# 3) 编译,编译时会按需下载submodule
2929
mkdir -p build && cd build
3030
cmake .. -DWITH_TESTING=ON -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
3131
make -j $(nproc)
3232

3333
# 4) 编译产出在build/dist路径下,使用pip安装
34-
pip install --force-reinstall -U build/dist/paddle_custom_gcu*.whl
34+
python -m pip install --force-reinstall -U build/dist/paddle_custom_gcu*.whl
3535
```
3636

3737
### 功能验证
@@ -48,4 +48,7 @@ python -c "import paddle_custom_device; paddle_custom_device.gcu.version()"
4848
version: 0.0.0.ffc0377-2.4.1
4949
commit: ffc037739c55508532ee67b565517be2b4ae584d
5050
plugin version: 0.0.1
51+
52+
# 3) 单元测试,带上-DWITH_TESTING=ON编译后在build目录下执行
53+
ctest
5154
```

backends/gcu/backend/equivalence_trans/all_ops.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,23 @@ limitations under the License. */
1616
// common help func
1717
#include "backend/equivalence_trans/utils.h"
1818
// INSENSITIVE OPS
19+
#include "backend/equivalence_trans/insensitive_ops/accuracy.h"
20+
#include "backend/equivalence_trans/insensitive_ops/activation.h"
21+
#include "backend/equivalence_trans/insensitive_ops/adam.h"
22+
#include "backend/equivalence_trans/insensitive_ops/adamw.h"
23+
#include "backend/equivalence_trans/insensitive_ops/atan.h"
24+
#include "backend/equivalence_trans/insensitive_ops/cos.h"
25+
#include "backend/equivalence_trans/insensitive_ops/elementwise_binary.h"
1926
#include "backend/equivalence_trans/insensitive_ops/elementwise_unary.h"
27+
#include "backend/equivalence_trans/insensitive_ops/floor.h"
28+
#include "backend/equivalence_trans/insensitive_ops/gelu.h"
29+
#include "backend/equivalence_trans/insensitive_ops/log.h"
30+
#include "backend/equivalence_trans/insensitive_ops/matmul_v2.h"
31+
#include "backend/equivalence_trans/insensitive_ops/maximum.h"
32+
#include "backend/equivalence_trans/insensitive_ops/mean.h"
33+
#include "backend/equivalence_trans/insensitive_ops/minimum.h"
34+
#include "backend/equivalence_trans/insensitive_ops/momentum.h"
35+
#include "backend/equivalence_trans/insensitive_ops/reduce_x.h"
36+
#include "backend/equivalence_trans/insensitive_ops/rmsprop.h"
37+
#include "backend/equivalence_trans/insensitive_ops/sqrt.h"
38+
#include "backend/equivalence_trans/insensitive_ops/tanh.h"
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/* Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License. */
14+
15+
#pragma once
16+
17+
#include <memory>
18+
#include <string>
19+
#include <vector>
20+
21+
#include "backend/register/register.h"
22+
23+
namespace backend {
24+
const char *const kAccuracy = "accuracy";
25+
26+
IMPLEMT_EQUIVALENCE_TRANS_FUNC(
27+
gcu_builder, op, map_inputs, running_mode, AccuracyEquivalenceTrans) {
28+
auto indices_op = *(map_inputs["Indices"].at(0));
29+
builder::Op out_op = *(map_inputs["Out"].at(0));
30+
auto label_op = *(map_inputs["Label"].at(0));
31+
std::vector<builder::Op> outputs;
32+
int64_t num_samples = out_op.GetType().GetShape()[0];
33+
34+
auto total_data = builder::FullLike(
35+
indices_op, num_samples, indices_op.GetType().GetPrimitiveType(), {});
36+
int64_t dims = static_cast<int64_t>(indices_op.GetType().GetRank());
37+
std::vector<int64_t> broadcast_dimensions(dims, 0);
38+
std::iota(broadcast_dimensions.begin(), broadcast_dimensions.end(), 0);
39+
40+
auto label_broad = builder::BroadcastInDim(
41+
label_op, broadcast_dimensions, indices_op.GetType());
42+
auto correct_data = builder::Equal(indices_op, label_broad);
43+
correct_data = builder::Convert(correct_data,
44+
{correct_data.GetType().GetShape(),
45+
indices_op.GetType().GetPrimitiveType()});
46+
47+
std::vector<int64_t> perm(label_op.GetType().GetRank(), 0);
48+
std::iota(perm.begin(), perm.end(), 0);
49+
auto correct_num =
50+
builder::ReduceSum(correct_data, false, perm, total_data.GetType());
51+
52+
auto total_data_f =
53+
builder::Convert(total_data, {{}, out_op.GetType().GetPrimitiveType()});
54+
auto correct_num_f =
55+
builder::Convert(correct_num, {{}, out_op.GetType().GetPrimitiveType()});
56+
auto accuracy_data = builder::Div(correct_num_f, total_data_f);
57+
58+
outputs.push_back(accuracy_data);
59+
outputs.push_back(correct_num);
60+
outputs.push_back(total_data);
61+
62+
std::vector<std::string> output_names{"Accuracy", "Correct", "Total"};
63+
auto output_name_map = op->Outputs();
64+
std::string output_names_attr(output_name_map[output_names[0]][0]);
65+
for (size_t i = 1; i < output_names.size(); ++i) {
66+
output_names_attr += ";" + output_name_map[output_names[i]][0];
67+
}
68+
builder::Op result = builder::Tuple(outputs);
69+
result.SetAttribute(kAttrOpOutVarName,
70+
builder::Attribute(output_names_attr.c_str()));
71+
return std::make_shared<GcuOp>(result);
72+
}
73+
74+
EQUIVALENCE_TRANS_FUNC_REG(kAccuracy, INSENSITIVE, AccuracyEquivalenceTrans);
75+
76+
} // namespace backend

0 commit comments

Comments
 (0)