Skip to content

Commit

Permalink
[NNAdapter] Migrate clip, batch_norm, reduce_mean, transpose and pow …
Browse files Browse the repository at this point in the history
…op bridges to the new converter framework (#7297)
  • Loading branch information
csy0225 authored Oct 21, 2021
1 parent b9fcc2b commit a8be278
Show file tree
Hide file tree
Showing 18 changed files with 569 additions and 544 deletions.
4 changes: 4 additions & 0 deletions lite/backends/nnadapter/nnadapter/core/operation/all.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ REGISTER_OPERATION(ASSIGN, PrepareAssign)
REGISTER_OPERATION(ARG_MAX, PrepareArgMinMax)
REGISTER_OPERATION(ARG_MIN, PrepareArgMinMax)
REGISTER_OPERATION(AVERAGE_POOL_2D, PreparePool2D)
REGISTER_OPERATION(BATCH_NORMALIZATION, PrepareBatchNormalization)
REGISTER_OPERATION(CLIP, PrepareClip)
REGISTER_OPERATION(CONCAT, PrepareConcat)
REGISTER_OPERATION(CONV_2D, PrepareConv2D)
REGISTER_OPERATION(CONV_2D_TRANSPOSE, PrepareConv2DTranspose)
Expand Down Expand Up @@ -53,6 +55,7 @@ REGISTER_OPERATION(MUL, PrepareElementwise)
REGISTER_OPERATION(NOT_EQUAL, PrepareComparisons)
REGISTER_OPERATION(POW, PrepareElementwise)
REGISTER_OPERATION(PRELU, PreparePRelu)
REGISTER_OPERATION(REDUCE_MEAN, PrepareReduce)
REGISTER_OPERATION(RELU, PrepareUnaryActivations)
REGISTER_OPERATION(RELU6, PrepareUnaryActivations)
REGISTER_OPERATION(RESHAPE, PrepareReshape)
Expand All @@ -69,6 +72,7 @@ REGISTER_OPERATION(SUB, PrepareElementwise)
REGISTER_OPERATION(SWISH, PrepareUnaryActivations)
REGISTER_OPERATION(TANH, PrepareUnaryActivations)
REGISTER_OPERATION(TOP_K, PrepareTopK)
REGISTER_OPERATION(TRANSPOSE, PrepareTranspose)
REGISTER_OPERATION(UNSQUEEZE, PrepareUnsqueeze)

#endif // NOLINT
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// 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/batch_normalization.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 PrepareBatchNormalization(hal::Operation* operation) {
BATCH_NORMALIZATION_OPERATION_EXTRACT_INPUTS_OUTPUTS

// Infer the shape and type of output operands
CopyOperandTypeExceptQuantParams(&output_operand->type, input_operand->type);
NNADAPTER_VLOG(5) << "output: " << OperandToString(output_operand);
return NNADAPTER_NO_ERROR;
}

} // namespace operation
} // namespace nnadapter
35 changes: 35 additions & 0 deletions lite/backends/nnadapter/nnadapter/core/operation/clip.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// 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/clip.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 PrepareClip(hal::Operation* operation) {
CLIP_OPERATION_EXTRACT_INPUTS_OUTPUTS

// Infer the shape and type of output operands
CopyOperandTypeExceptQuantParams(&output_operand->type, input_operand->type);
NNADAPTER_VLOG(5) << "output: " << OperandToString(output_operand);
return NNADAPTER_NO_ERROR;
}

} // namespace operation
} // namespace nnadapter
82 changes: 82 additions & 0 deletions lite/backends/nnadapter/nnadapter/core/operation/reduce.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// 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/reduce.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 PrepareReduce(hal::Operation* operation) {
REDUCE_OPERATION_EXTRACT_INPUTS_OUTPUTS

// Infer the shape and type of output operands
auto input_type = input_operand->type;
auto& output_type = output_operand->type;
CopyOperandTypeExceptQuantParams(&output_type, input_type);
if (keep_dim) {
output_type.dimensions.count = input_type.dimensions.count;
} else {
output_type.dimensions.count =
(input_type.dimensions.count == axes_size)
? 1
: input_type.dimensions.count - axes_size;
}
auto infer_output_shape = [&](const int32_t* input_dimensions,
const int32_t input_dimension_count,
int32_t* output_dimensions) {
for (int i = 0; i < input_dimension_count; i++) {
output_dimensions[i] = input_dimensions[i];
}
const int kDelFlag = -2;
for (int i = 0; i < axes_size; i++) {
auto axis = axes_data[i] >= 0 ? axes_data[i]
: axes_data[i] + input_dimension_count;
if (keep_dim) {
output_dimensions[axis] = 1;
} else {
output_dimensions[axis] = kDelFlag;
}
}
int output_dimension_index = 0;
for (int i = 0; i < input_dimension_count;) {
if (output_dimensions[i] == kDelFlag) {
i++;
} else {
output_dimensions[output_dimension_index++] = output_dimensions[i++];
}
}
if (output_dimension_index == 0 &&
output_dimensions[output_dimension_index] == kDelFlag) {
output_dimensions[0] = 1;
}
};
infer_output_shape(input_type.dimensions.data,
input_type.dimensions.count,
output_type.dimensions.data);
for (uint32_t i = 0; i < input_type.dimensions.dynamic_count; i++) {
infer_output_shape(input_type.dimensions.dynamic_data[i],
input_type.dimensions.count,
output_type.dimensions.dynamic_data[i]);
}
NNADAPTER_VLOG(5) << "output: " << OperandToString(output_operand);
return NNADAPTER_NO_ERROR;
}

} // namespace operation
} // namespace nnadapter
48 changes: 48 additions & 0 deletions lite/backends/nnadapter/nnadapter/core/operation/transpose.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) 2019 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/transpose.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 PrepareTranspose(hal::Operation* operation) {
TRANSPOSE_OPERATION_EXTRACT_INPUTS_OUTPUTS

// Infer the shape and type of output operands
auto input_type = input_operand->type;
auto& output_type = output_operand->type;
CopyOperandTypeExceptQuantParams(&output_type, input_type);
auto infer_output_shape = [&](int32_t* input_dimensions_data,
int32_t* output_dimensions_data) {
for (uint32_t i = 0; i < perm_count; i++) {
output_dimensions_data[i] = input_dimensions_data[perm_data[i]];
}
};
infer_output_shape(input_type.dimensions.data, output_type.dimensions.data);
for (uint32_t i = 0; i < input_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
10 changes: 0 additions & 10 deletions lite/kernels/nnadapter/bridges/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,8 @@ set(nnadapter_subgraph_bridge_deps core subgraph_bridge_utility_nnadapter subgra

lite_cc_library(subgraph_bridge_fc_op_nnadapter SRCS fc_op.cc DEPS ${nnadapter_subgraph_bridge_deps})
lite_cc_library(subgraph_bridge_scale_op_nnadapter SRCS scale_op.cc DEPS ${nnadapter_subgraph_bridge_deps})
lite_cc_library(subgraph_bridge_transpose_op_nnadapter SRCS transpose_op.cc DEPS ${nnadapter_subgraph_bridge_deps})
lite_cc_library(subgraph_bridge_cast_op_nnadapter SRCS cast_op.cc DEPS ${nnadapter_subgraph_bridge_deps})
lite_cc_library(subgraph_bridge_norm_op_nnadapter SRCS norm_op.cc DEPS ${nnadapter_subgraph_bridge_deps})
lite_cc_library(subgraph_bridge_pow_op_nnadapter SRCS pow_op.cc DEPS ${nnadapter_subgraph_bridge_deps})
lite_cc_library(subgraph_bridge_batch_normalization_op_nnadapter SRCS batch_normalization_op.cc DEPS ${nnadapter_subgraph_bridge_deps})
lite_cc_library(subgraph_bridge_clip_op_nnadapter SRCS clip_op.cc DEPS ${nnadapter_subgraph_bridge_deps})
lite_cc_library(subgraph_bridge_reduce_mean_op_nnadapter SRCS reduce_mean_op.cc DEPS ${nnadapter_subgraph_bridge_deps})
lite_cc_library(subgraph_bridge_dropout_op_nnadapter SRCS dropout_op.cc DEPS ${nnadapter_subgraph_bridge_deps})
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})
Expand All @@ -27,13 +22,8 @@ set(nnadapter_subgraph_bridges
subgraph_bridge_converter_nnadapter
subgraph_bridge_fc_op_nnadapter
subgraph_bridge_scale_op_nnadapter
subgraph_bridge_transpose_op_nnadapter
subgraph_bridge_cast_op_nnadapter
subgraph_bridge_norm_op_nnadapter
subgraph_bridge_pow_op_nnadapter
subgraph_bridge_batch_normalization_op_nnadapter
subgraph_bridge_clip_op_nnadapter
subgraph_bridge_reduce_mean_op_nnadapter
subgraph_bridge_dropout_op_nnadapter
subgraph_bridge_expand_op_nnadapter
subgraph_bridge_range_op_nnadapter
Expand Down
129 changes: 0 additions & 129 deletions lite/kernels/nnadapter/bridges/batch_normalization_op.cc

This file was deleted.

Loading

0 comments on commit a8be278

Please sign in to comment.