-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NNAdapter] Migrate clip, batch_norm, reduce_mean, transpose and pow …
…op bridges to the new converter framework (#7297)
- Loading branch information
Showing
18 changed files
with
569 additions
and
544 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
lite/backends/nnadapter/nnadapter/core/operation/batch_normalization.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
82
lite/backends/nnadapter/nnadapter/core/operation/reduce.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
48
lite/backends/nnadapter/nnadapter/core/operation/transpose.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 0 additions & 129 deletions
129
lite/kernels/nnadapter/bridges/batch_normalization_op.cc
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.