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

Cherry-pick V2.10 NNAdapter #7496

Merged
merged 4 commits into from
Nov 1, 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
3 changes: 3 additions & 0 deletions lite/api/paddle_use_passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,6 @@ USE_MIR_PASS(__xpu__dynamic_lstm_fuse_pass);
USE_MIR_PASS(__xpu__multi_softmax_fuse_pass);
USE_MIR_PASS(__xpu__max_pooling_pad_zero_detect_fuse_pass);
USE_MIR_PASS(x86_int8_attribute_pass);
USE_MIR_PASS(fill_range_fuse_pass);
USE_MIR_PASS(range_calc_offline_pass);
USE_MIR_PASS(p_norm_fill_constant_max_div_fuse_pass);
10 changes: 10 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,9 @@ 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(CAST, PrepareCast)
REGISTER_OPERATION(CLIP, PrepareClip)
REGISTER_OPERATION(CONCAT, PrepareConcat)
REGISTER_OPERATION(CONV_2D, PrepareConv2D)
REGISTER_OPERATION(CONV_2D_TRANSPOSE, PrepareConv2DTranspose)
Expand All @@ -39,6 +42,7 @@ REGISTER_OPERATION(GREATER, PrepareComparisons)
REGISTER_OPERATION(GREATER_EQUAL, PrepareComparisons)
REGISTER_OPERATION(HARD_SIGMOID, PrepareHardSigmoidSwish)
REGISTER_OPERATION(HARD_SWISH, PrepareHardSigmoidSwish)
REGISTER_OPERATION(EXPAND, PrepareExpand)
REGISTER_OPERATION(INSTANCE_NORMALIZATION, PrepareInstanceNormalization)
REGISTER_OPERATION(LAYER_NORMALIZATION, PrepareLayerNormalization)
REGISTER_OPERATION(LEAKY_RELU, PrepareLeakyRelu)
Expand All @@ -50,9 +54,13 @@ REGISTER_OPERATION(MAX, PrepareElementwise)
REGISTER_OPERATION(MAX_POOL_2D, PreparePool2D)
REGISTER_OPERATION(MIN, PrepareElementwise)
REGISTER_OPERATION(MUL, PrepareElementwise)
REGISTER_OPERATION(LP_NORMALIZATION, PrepareLpNormalization)
REGISTER_OPERATION(NOT_EQUAL, PrepareComparisons)
REGISTER_OPERATION(PAD, PreparePad)
REGISTER_OPERATION(POW, PrepareElementwise)
REGISTER_OPERATION(PRELU, PreparePRelu)
REGISTER_OPERATION(RANGE, PrepareRange)
REGISTER_OPERATION(REDUCE_MEAN, PrepareReduce)
REGISTER_OPERATION(RELU, PrepareUnaryActivations)
REGISTER_OPERATION(RELU6, PrepareUnaryActivations)
REGISTER_OPERATION(RESHAPE, PrepareReshape)
Expand All @@ -64,10 +72,12 @@ 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)
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
36 changes: 36 additions & 0 deletions lite/backends/nnadapter/nnadapter/core/operation/cast.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// 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/cast.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 PrepareCast(hal::Operation* operation) {
CAST_OPERATION_EXTRACT_INPUTS_OUTPUTS

// Infer the shape and type of output operands
CopyOperandTypeExceptQuantParams(&output_operand->type, input_operand->type);
output_operand->type.precision = dtype;
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
99 changes: 99 additions & 0 deletions lite/backends/nnadapter/nnadapter/core/operation/expand.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// 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/expand.h"
#include <vector>
#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 PrepareExpand(hal::Operation* operation) {
EXPAND_OPERATION_EXTRACT_INPUTS_OUTPUTS

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

output_type.dimensions.count = shape_count;

auto infer_output_shape = [&](int32_t* input_dimensions_data,
uint32_t input_dimensions_count,
int32_t* output_dimensions_data) {
std::vector<int> input_dims_vec;
for (uint32_t i = 0; i < input_dimensions_count; i++) {
input_dims_vec.push_back(input_dimensions_data[i]);
}
auto diff = shape_count - input_dimensions_count;
input_dims_vec.insert(input_dims_vec.begin(), diff, 1);
std::vector<int> final_expand_shape(input_dimensions_count);
for (uint32_t i = 0; i < input_dims_vec.size(); ++i) {
NNADAPTER_CHECK_NE(shape_data[i], 0)
<< "The expanded size cannot be zero.";
if (i < diff) {
// shape_data = [3,4,-1,-1], X = [10,2] --> // final_expand_shape =
// [3,4,10,2]
NNADAPTER_CHECK_GT(shape_data[i], 0)
<< "The expanded size " << shape_data[i]
<< "for non-existing dimensions must be positive for expand_v2 op.";
final_expand_shape[i] = shape_data[i];
} else if (shape_data[i] > 0) {
// shape_data = [3,4,10,4], X = [10,1] --> final_expand_shape =
// [3,4,10,4]
if (input_dims_vec[i] != 1) {
NNADAPTER_CHECK_EQ(input_dims_vec[i], shape_data[i])
<< "The value " << input_dims_vec[i]
<< " of the non-singleton dimension does not match the "
"corresponding value "
<< shape_data[i] << " in shape for expand_v2 op.";
final_expand_shape[i] = shape_data[i];
} else {
final_expand_shape[i] = shape_data[i];
}
} else {
// shape_data = [3,4,-1,-1], X = [10,2] --> final_expand_shape =
// [3,4,10,2]
NNADAPTER_CHECK_EQ(shape_data[i], -1)
<< "When the value in shape is negative for expand_v2 op, "
"only -1 is supported, but the value received is "
<< shape_data[i];
final_expand_shape[i] = input_dims_vec[i];
}
}

for (uint32_t i = 0; i < shape_count; ++i) {
shape_data[i] = final_expand_shape[i];
output_dimensions_data[i] = final_expand_shape[i];
}
};

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
22 changes: 20 additions & 2 deletions lite/backends/nnadapter/nnadapter/core/operation/expand.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,26 @@ namespace operation {
NNADAPTER_VLOG(5) << "input_operand: " << OperandToString(input_operand); \
/* Shape */ \
auto shape_operand = input_operands[1]; \
NNADAPTER_VLOG(5) << "shape operand: " \
<< OperandValueToString(shape_operand); \
NNADAPTER_VLOG(5) << "shape operand: " << OperandToString(shape_operand); \
uint32_t shape_count; \
int32_t* shape_data; \
auto& shape_type = shape_operand->type; \
if (IsConstantOperand(shape_operand)) { \
shape_count = shape_operand->length / sizeof(int32_t); \
shape_data = reinterpret_cast<int32_t*>(shape_operand->buffer); \
} else if (shape_type.lifetime == NNADAPTER_TEMPORARY_SHAPE) { \
auto shape_operand_dimension = \
*reinterpret_cast<NNAdapterOperandDimensionType*>( \
shape_operand->buffer); \
shape_count = shape_operand_dimension.count; \
shape_data = shape_operand_dimension.data; \
} else { \
shape_count = shape_operand->type.dimensions.count; \
shape_data = shape_operand->type.dimensions.data; \
} \
for (uint32_t i = 0; i < shape_count; i++) { \
NNADAPTER_VLOG(5) << "shape[" << i << "] = " << shape_data[i]; \
} \
/* Output */ \
auto output_operand = output_operands[0]; \
NNADAPTER_VLOG(5) << "output_operand: " << OperandToString(output_operand);
Expand Down
1 change: 1 addition & 0 deletions lite/backends/nnadapter/nnadapter/core/operation/fill.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ int PrepareFill(hal::Operation* operation) {
return NNADAPTER_INVALID_PARAMETER;
}
output_type.precision = value_operand->type.precision;
output_type.lifetime = NNADAPTER_TEMPORARY_VARIABLE;
NNADAPTER_VLOG(5) << "output: " << OperandToString(output_operand);
return NNADAPTER_NO_ERROR;
}
Expand Down
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/lp_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 PrepareLpNormalization(hal::Operation* operation) {
LP_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
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace operation {
auto& output_operands = operation->output_operands; \
auto input_count = input_operands.size(); \
auto output_count = output_operands.size(); \
NNADAPTER_CHECK_EQ(input_count, 5); \
NNADAPTER_CHECK_EQ(input_count, 4); \
NNADAPTER_CHECK_EQ(output_count, 1); \
/* Input */ \
auto input_operand = input_operands[0]; \
Expand All @@ -40,12 +40,11 @@ namespace operation {
/* P */ \
auto p = *reinterpret_cast<int32_t*>(input_operands[2]->buffer); \
NNADAPTER_VLOG(5) << "p: " << p; \
NNADAPTER_CHECK(p == 1 || p == 2) \
<< "lp normalization only support p = 1 or p = 2."; \
/* Epsilon */ \
auto epsilon = *reinterpret_cast<float*>(input_operands[3]->buffer); \
NNADAPTER_VLOG(5) << "epsilon: " << epsilon; \
/* Keepdim */ \
auto keepdim = *reinterpret_cast<bool*>(input_operands[4]->buffer); \
NNADAPTER_VLOG(5) << "keepdim: " << keepdim;
NNADAPTER_VLOG(5) << "epsilon: " << epsilon;

} // namespace operation
} // namespace nnadapter
Loading