Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NNAdapter]Move stack op to converter #7332

Merged
merged 4 commits into from
Oct 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lite/backends/nnadapter/nnadapter/core/operation/all.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ REGISTER_OPERATION(SLICE, PrepareSlice)
REGISTER_OPERATION(SOFTMAX, PrepareSoftmax)
REGISTER_OPERATION(SPLIT, PrepareSplit)
REGISTER_OPERATION(SQUEEZE, PrepareSqueeze)
REGISTER_OPERATION(STACK, PrepareStack)
REGISTER_OPERATION(SUB, PrepareElementwise)
REGISTER_OPERATION(SWISH, PrepareUnaryActivations)
REGISTER_OPERATION(TANH, PrepareUnaryActivations)
Expand Down
54 changes: 54 additions & 0 deletions lite/backends/nnadapter/nnadapter/core/operation/stack.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// 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 "core/operation/stack.h"
#include "core/hal/types.h"
#include "utility/debug.h"
#include "utility/logging.h"
#include "utility/modeling.h"
#include "utility/utility.h"

namespace nnadapter {
namespace operation {

int PrepareStack(hal::Operation* operation) {
STACK_OPERATION_EXTRACT_INPUTS_OUTPUTS

// Infer the shape and type of output operands
auto input_type = input_operands[0]->type;
auto& output_type = output_operand->type;
CopyOperandTypeWithQuantParams(&output_type, input_type);

output_type.dimensions.count = input_dimensions_count + 1;

auto infer_output_shape = [&](int32_t* input_dimensions,
int32_t* output_dimensions) {
for (uint32_t j = input_dimensions_count; j > axis; j--) {
output_dimensions[j] = output_dimensions[j - 1];
}
output_dimensions[axis] = input_count - 1;
};

infer_output_shape(input_type.dimensions.data, output_type.dimensions.data);
for (uint32_t i = 0; i < output_type.dimensions.dynamic_count; i++) {
infer_output_shape(input_type.dimensions.dynamic_data[i],
output_type.dimensions.dynamic_data[i]);
}

NNADAPTER_VLOG(5) << "output: " << OperandToString(output_operand);
return NNADAPTER_NO_ERROR;
}

} // namespace operation
} // namespace nnadapter
4 changes: 4 additions & 0 deletions lite/backends/nnadapter/nnadapter/core/operation/stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ namespace operation {
/* Axis */ \
auto axis = \
*reinterpret_cast<int32_t*>(input_operands[input_count - 1]->buffer); \
auto input_dimensions_count = input_operands[0]->type.dimensions.count; \
if (axis < 0) axis += (input_dimensions_count + 1); \
NNADAPTER_CHECK_GE(axis, 0); \
NNADAPTER_CHECK_LE(axis, input_dimensions_count); \
NNADAPTER_VLOG(5) << "axis=" << axis; \
/* Output */ \
auto output_operand = output_operands[0]; \
Expand Down
2 changes: 0 additions & 2 deletions lite/kernels/nnadapter/bridges/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ lite_cc_library(subgraph_bridge_dropout_op_nnadapter SRCS dropout_op.cc DEPS ${n
lite_cc_library(subgraph_bridge_expand_op_nnadapter SRCS expand_op.cc DEPS ${nnadapter_subgraph_bridge_deps})
lite_cc_library(subgraph_bridge_range_op_nnadapter SRCS range_op.cc DEPS ${nnadapter_subgraph_bridge_deps})
lite_cc_library(subgraph_bridge_pad_op_nnadapter SRCS pad_op.cc DEPS ${nnadapter_subgraph_bridge_deps})
lite_cc_library(subgraph_bridge_stack_op_nnadapter SRCS stack_op.cc DEPS ${nnadapter_subgraph_bridge_deps})

set(nnadapter_subgraph_bridges
core
Expand All @@ -38,7 +37,6 @@ set(nnadapter_subgraph_bridges
subgraph_bridge_dropout_op_nnadapter
subgraph_bridge_expand_op_nnadapter
subgraph_bridge_range_op_nnadapter
subgraph_bridge_stack_op_nnadapter
subgraph_bridge_pad_op_nnadapter
CACHE INTERNAL "nnadapter_subgraph_bridges")

Expand Down
1 change: 0 additions & 1 deletion lite/kernels/nnadapter/bridges/paddle_use_bridges.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,3 @@ USE_SUBGRAPH_BRIDGE(range, kNNAdapter, "huawei_ascend_npu");
USE_SUBGRAPH_BRIDGE(p_norm, kNNAdapter, "huawei_ascend_npu");
USE_SUBGRAPH_BRIDGE(pad2d, kNNAdapter, "huawei_ascend_npu");
USE_SUBGRAPH_BRIDGE(pad3d, kNNAdapter, "huawei_ascend_npu");
USE_SUBGRAPH_BRIDGE(stack, kNNAdapter, "huawei_ascend_npu");
104 changes: 0 additions & 104 deletions lite/kernels/nnadapter/bridges/stack_op.cc

This file was deleted.

1 change: 1 addition & 0 deletions lite/kernels/nnadapter/converter/all.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ REGISTER_CONVERTER(shape, ConvertShape, "huawei_ascend_npu");
REGISTER_CONVERTER(slice, ConvertSlice, "huawei_ascend_npu");
REGISTER_CONVERTER(squeeze, ConvertSqueeze, "huawei_ascend_npu");
REGISTER_CONVERTER(squeeze2, ConvertSqueeze, "huawei_ascend_npu");
REGISTER_CONVERTER(stack, ConvertStack, "huawei_ascend_npu");
REGISTER_CONVERTER(fill_constant, ConvertFillConstant, "huawei_ascend_npu");
REGISTER_CONVERTER(fill_any_like, ConvertFillAnyLike, "huawei_ascend_npu");
REGISTER_CONVERTER(
Expand Down
60 changes: 60 additions & 0 deletions lite/kernels/nnadapter/converter/stack.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// 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 "lite/kernels/nnadapter/converter/converter.h"

namespace paddle {
namespace lite {
namespace kernels {
namespace nnadapter {

int ConvertStack(Converter* converter, OpInfo* op, Scope* scope) {
// Input operand
auto x_names = op->Input("X");
std::vector<NNAdapterOperand*> input_operands;
for (size_t i = 0; i < x_names.size(); i++) {
auto x_name = x_names[i];
auto x_scale_name = "X" + paddle::lite::to_string(i) + "_scale";
std::vector<float> x_scales;
if (op->HasInputScale(x_scale_name, true)) {
x_scales = op->GetInputScale(x_scale_name, true);
}
auto input_operand =
converter->AddInputOperand(scope, x_name, {}, x_scales);
input_operands.push_back(input_operand);
}

// Axis operand
int axis = op->GetAttr<int>("axis");
auto axis_operand = converter->AddConstantOperand(axis);
input_operands.push_back(axis_operand);

// Output operand
auto y_name = op->Output("Y").front();
auto y_scale_name = "Y0_scale";
std::vector<float> y_scales;
if (op->HasOutputScale(y_scale_name, true)) {
y_scales = op->GetOutputScale(y_scale_name, true);
}
auto output_operand = converter->AddOutputOperand(y_name, y_scales);

// Stack operation
converter->AddOperation(NNADAPTER_STACK, input_operands, {output_operand});
return NO_ERROR;
}

} // namespace nnadapter
} // namespace kernels
} // namespace lite
} // namespace paddle