Skip to content

Commit c483158

Browse files
authored
[NNAdapter][HuaweiAscendNPU][Host] Add softplus op and shuffle_channel, interpolate ops supports scale tensor list (#8041)
1 parent d893d7f commit c483158

Some content is hidden

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

54 files changed

+564
-38
lines changed

lite/backends/nnadapter/nnadapter/core/operation/all.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ REGISTER_OPERATION(AVERAGE_POOL_2D, PreparePool2D)
2727
REGISTER_OPERATION(BATCH_NORMALIZATION, PrepareBatchNormalization)
2828
REGISTER_OPERATION(CAST, PrepareCast)
2929
REGISTER_OPERATION(CLIP, PrepareClip)
30+
REGISTER_OPERATION(CHANNEL_SHUFFLE, PrepareChannelShuffle)
3031
REGISTER_OPERATION(CONCAT, PrepareConcat)
3132
REGISTER_OPERATION(CONV_2D, PrepareConv2D)
3233
REGISTER_OPERATION(CONV_2D_TRANSPOSE, PrepareConv2DTranspose)
@@ -81,6 +82,7 @@ REGISTER_OPERATION(SHAPE, PrepareShape)
8182
REGISTER_OPERATION(SIGMOID, PrepareUnaryActivations)
8283
REGISTER_OPERATION(SLICE, PrepareSlice)
8384
REGISTER_OPERATION(SOFTMAX, PrepareSoftmax)
85+
REGISTER_OPERATION(SOFTPLUS, PrepareSoftplus)
8486
REGISTER_OPERATION(SPLIT, PrepareSplit)
8587
REGISTER_OPERATION(SQUARE, PrepareUnaryActivations)
8688
REGISTER_OPERATION(SQUEEZE, PrepareSqueeze)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (c) 2021 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+
#include "core/operation/channel_shuffle.h"
16+
#include "core/hal/types.h"
17+
#include "utility/debug.h"
18+
#include "utility/logging.h"
19+
#include "utility/modeling.h"
20+
#include "utility/utility.h"
21+
22+
namespace nnadapter {
23+
namespace operation {
24+
25+
int PrepareChannelShuffle(hal::Operation* operation) {
26+
CHANNEL_SHUFFLE_OPERATION_EXTRACT_INPUTS_OUTPUTS
27+
28+
// Infer the shape and type of output operands
29+
CopyOperandTypeExceptQuantParams(&output_operand->type, input_operand->type);
30+
NNADAPTER_VLOG(5) << "output: " << OperandToString(output_operand);
31+
return NNADAPTER_NO_ERROR;
32+
}
33+
34+
} // namespace operation
35+
} // namespace nnadapter
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) 2021 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+
namespace nnadapter {
18+
namespace operation {
19+
20+
#define CHANNEL_SHUFFLE_OPERATION_EXTRACT_INPUTS_OUTPUTS \
21+
auto& input_operands = operation->input_operands; \
22+
auto& output_operands = operation->output_operands; \
23+
auto input_count = input_operands.size(); \
24+
auto output_count = output_operands.size(); \
25+
NNADAPTER_CHECK_EQ(input_count, 2); \
26+
NNADAPTER_CHECK_EQ(output_count, 1); \
27+
/* Input */ \
28+
auto input_operand = input_operands[0]; \
29+
NNADAPTER_VLOG(5) << "input_operand: " << OperandToString(input_operand); \
30+
/* Group */ \
31+
int32_t group = *reinterpret_cast<int32_t*>(input_operands[1]->buffer); \
32+
NNADAPTER_VLOG(5) << "group: " << group; \
33+
/* Output */ \
34+
auto output_operand = output_operands[0]; \
35+
NNADAPTER_VLOG(5) << "output: " << OperandToString(output_operand);
36+
37+
} // namespace operation
38+
} // namespace nnadapter
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (c) 2021 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+
#include "core/operation/softplus.h"
16+
#include "core/hal/types.h"
17+
#include "utility/debug.h"
18+
#include "utility/logging.h"
19+
#include "utility/modeling.h"
20+
#include "utility/utility.h"
21+
22+
namespace nnadapter {
23+
namespace operation {
24+
25+
int PrepareSoftplus(hal::Operation* operation) {
26+
SOFTPLUS_OPERATION_EXTRACT_INPUTS_OUTPUTS
27+
28+
// Infer the shape and type of output operands
29+
CopyOperandTypeExceptQuantParams(&output_operand->type, input_operand->type);
30+
NNADAPTER_VLOG(5) << "output: " << OperandToString(output_operand);
31+
return NNADAPTER_NO_ERROR;
32+
}
33+
34+
} // namespace operation
35+
} // namespace nnadapter
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright (c) 2021 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+
namespace nnadapter {
18+
namespace operation {
19+
20+
#define SOFTPLUS_OPERATION_EXTRACT_INPUTS_OUTPUTS \
21+
auto& input_operands = operation->input_operands; \
22+
auto& output_operands = operation->output_operands; \
23+
auto input_count = input_operands.size(); \
24+
auto output_count = output_operands.size(); \
25+
NNADAPTER_CHECK_EQ(input_count, 3); \
26+
NNADAPTER_CHECK_EQ(output_count, 1); \
27+
/* Input */ \
28+
auto input_operand = input_operands[0]; \
29+
NNADAPTER_VLOG(5) << "input_operand: " << OperandToString(input_operand); \
30+
/* Beta */ \
31+
float beta = *reinterpret_cast<float*>(input_operands[1]->buffer); \
32+
NNADAPTER_VLOG(5) << "beta: " << beta; \
33+
/* Threshold */ \
34+
float threshold = *reinterpret_cast<float*>(input_operands[2]->buffer); \
35+
NNADAPTER_VLOG(5) << "threshold: " << threshold; \
36+
/* Output */ \
37+
auto output_operand = output_operands[0]; \
38+
NNADAPTER_VLOG(5) << "output: " << OperandToString(output_operand);
39+
40+
} // namespace operation
41+
} // namespace nnadapter

lite/backends/nnadapter/nnadapter/driver/huawei_ascend_npu/converter/all.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
1+
// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -27,6 +27,7 @@ REGISTER_CONVERTER(AVERAGE_POOL_2D, ConvertPool2D)
2727
REGISTER_CONVERTER(BATCH_NORMALIZATION, ConvertBatchNormalization)
2828
REGISTER_CONVERTER(CAST, ConvertCast)
2929
REGISTER_CONVERTER(CLIP, ConvertClip)
30+
REGISTER_CONVERTER(CHANNEL_SHUFFLE, ConvertChannelShuffle)
3031
REGISTER_CONVERTER(CONCAT, ConvertConcat)
3132
REGISTER_CONVERTER(CONV_2D, ConvertConv2D)
3233
REGISTER_CONVERTER(CONV_2D_TRANSPOSE, ConvertConv2DTranspose)
@@ -81,6 +82,7 @@ REGISTER_CONVERTER(SHAPE, ConvertShape)
8182
REGISTER_CONVERTER(SIGMOID, ConvertUnaryActivations)
8283
REGISTER_CONVERTER(SLICE, ConvertSlice)
8384
REGISTER_CONVERTER(SOFTMAX, ConvertSoftmax)
85+
REGISTER_CONVERTER(SOFTPLUS, ConvertSoftplus)
8486
REGISTER_CONVERTER(SPLIT, ConvertSplit)
8587
REGISTER_CONVERTER(SQUARE, ConvertUnaryActivations)
8688
REGISTER_CONVERTER(SQUEEZE, ConvertSqueeze)

lite/backends/nnadapter/nnadapter/driver/huawei_ascend_npu/converter/batch_normalization.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
1+
// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (c) 2021 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+
#include "core/operation/channel_shuffle.h"
16+
#include "driver/huawei_ascend_npu/converter/converter.h"
17+
#include "utility/debug.h"
18+
#include "utility/logging.h"
19+
20+
namespace nnadapter {
21+
namespace huawei_ascend_npu {
22+
23+
int ConvertChannelShuffle(Converter* converter, hal::Operation* operation) {
24+
CHANNEL_SHUFFLE_OPERATION_EXTRACT_INPUTS_OUTPUTS
25+
26+
// Convert to GE operators
27+
auto input_operator = converter->GetMappedOperator(input_operand);
28+
if (!input_operator) {
29+
input_operator = converter->ConvertOperand(input_operand);
30+
}
31+
auto shuffle_channel_op =
32+
converter->AddOperator<ge::op::ShuffleChannel>(output_operand);
33+
shuffle_channel_op->set_attr_group(group);
34+
SET_INPUT(shuffle_channel_op, x, input_operator);
35+
MAP_OUTPUT(shuffle_channel_op, y, output_operand);
36+
return NNADAPTER_NO_ERROR;
37+
}
38+
39+
} // namespace huawei_ascend_npu
40+
} // namespace nnadapter

lite/backends/nnadapter/nnadapter/driver/huawei_ascend_npu/converter/clip.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
1+
// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.

lite/backends/nnadapter/nnadapter/driver/huawei_ascend_npu/converter/concat.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
1+
// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.

0 commit comments

Comments
 (0)