|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +#include <onnx/onnx_pb.h> |
| 5 | +#include <algorithm> |
| 6 | + |
| 7 | +#include "core/common/logging/logging.h" |
| 8 | +#include "core/common/safeint.h" |
| 9 | +#include "core/framework/tensorprotoutils.h" |
| 10 | +#include "core/graph/graph_viewer.h" |
| 11 | +#include "core/providers/common.h" |
| 12 | +#include "core/optimizer/initializer.h" |
| 13 | +#include "core/providers/shared/utils/utils.h" |
| 14 | +#include "core/providers/nnapi/nnapi_builtin/builders/helper.h" |
| 15 | +#include "core/providers/nnapi/nnapi_builtin/builders/model_builder.h" |
| 16 | +#include "core/providers/nnapi/nnapi_builtin/builders/op_builder_factory.h" |
| 17 | +#include "core/providers/nnapi/nnapi_builtin/builders/op_builder_helpers.h" |
| 18 | +#include "core/providers/nnapi/nnapi_builtin/builders/impl/base_op_builder.h" |
| 19 | + |
| 20 | +using namespace android::nn::wrapper; |
| 21 | + |
| 22 | +namespace onnxruntime { |
| 23 | +namespace nnapi { |
| 24 | + |
| 25 | +using namespace op_builder_helpers; |
| 26 | + |
| 27 | +class SplitOpBuilder : public BaseOpBuilder { |
| 28 | + // Add operator related |
| 29 | + public: |
| 30 | + void AddInitializersToSkip(ModelBuilder& model_builder, const NodeUnit& node_unit) const override; |
| 31 | + |
| 32 | + private: |
| 33 | + Status AddToModelBuilderImpl(ModelBuilder& model_builder, const NodeUnit& node_unit) const override; |
| 34 | + |
| 35 | + // Operator support related |
| 36 | + |
| 37 | + private: |
| 38 | + bool IsOpSupportedImpl(const InitializedTensorSet& initializers, const NodeUnit& node_unit, |
| 39 | + const OpSupportCheckParams& params) const override; |
| 40 | + |
| 41 | + // Split opset 13- uses "split" as attribute. Currently it's not supported. |
| 42 | + int GetMinSupportedOpSet(const NodeUnit& /* node_unit */) const override { return 13; } |
| 43 | + |
| 44 | + // NNAPI Split is available since NNAPI feature level 3 |
| 45 | + int32_t GetMinSupportedNNAPIFeatureLevel(const NodeUnit& /* node_unit */, |
| 46 | + const OpSupportCheckParams& /* params */) const override { |
| 47 | + return ANEURALNETWORKS_FEATURE_LEVEL_3; |
| 48 | + } |
| 49 | +}; |
| 50 | + |
| 51 | +// Add operator related |
| 52 | + |
| 53 | +void SplitOpBuilder::AddInitializersToSkip(ModelBuilder& model_builder, const NodeUnit& node_unit) const { |
| 54 | + const auto& input_defs = node_unit.Inputs(); |
| 55 | + |
| 56 | + if (input_defs.size() > 1 && input_defs[1].node_arg.Exists()) { // optional second input "split" |
| 57 | + model_builder.AddInitializerToSkip(input_defs[1].node_arg.Name()); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +Status SplitOpBuilder::AddToModelBuilderImpl(ModelBuilder& model_builder, const NodeUnit& node_unit) const { |
| 62 | + const auto& input_name = node_unit.Inputs()[0].node_arg.Name(); |
| 63 | + const auto& outputs = node_unit.Outputs(); |
| 64 | + |
| 65 | + NodeAttrHelper helper(node_unit); |
| 66 | + const auto axis = helper.Get("axis", 0); |
| 67 | + |
| 68 | + int32_t num_outputs; |
| 69 | + if (node_unit.SinceVersion() >= 18) { |
| 70 | + num_outputs = SafeInt<int32_t>(*helper.GetInt("num_outputs")); |
| 71 | + } else { |
| 72 | + num_outputs = SafeInt<int32_t>(node_unit.Outputs().size()); |
| 73 | + } |
| 74 | + |
| 75 | + std::vector<std::string> output_names; |
| 76 | + output_names.reserve(num_outputs); |
| 77 | + for (int32_t i = 0; i < num_outputs; ++i) { |
| 78 | + output_names.push_back(outputs[i].node_arg.Name()); |
| 79 | + } |
| 80 | + |
| 81 | + ORT_RETURN_IF_ERROR(op_builder_helpers::AddNnapiSplit(model_builder, input_name, axis, output_names)); |
| 82 | + |
| 83 | + return Status::OK(); |
| 84 | +} |
| 85 | + |
| 86 | +// Operator support related |
| 87 | + |
| 88 | +bool SplitOpBuilder::IsOpSupportedImpl(const InitializedTensorSet& initializers, const NodeUnit& node_unit, |
| 89 | + const OpSupportCheckParams& /* params */) const { |
| 90 | + Shape input_shape; |
| 91 | + if (!GetShape(node_unit.Inputs()[0].node_arg, input_shape)) |
| 92 | + return false; |
| 93 | + |
| 94 | + const auto& input_defs = node_unit.Inputs(); |
| 95 | + NodeAttrHelper helper(node_unit); |
| 96 | + const auto axis = helper.Get("axis", 0); |
| 97 | + |
| 98 | + const auto split_dims_at_axis = input_shape[HandleNegativeAxis(axis, input_shape.size())]; |
| 99 | + if (input_defs.size() > 1 && input_defs[1].node_arg.Exists()) { |
| 100 | + // if optional input `split` is provided |
| 101 | + auto split_initializer_it = initializers.find(input_defs[1].node_arg.Name()); |
| 102 | + if (split_initializer_it == initializers.end()) { |
| 103 | + LOGS_DEFAULT(VERBOSE) << "Optional input 'split' must be initializer if provided."; |
| 104 | + return false; |
| 105 | + } |
| 106 | + const auto& splits_tensor = *split_initializer_it->second; |
| 107 | + Initializer unpacked_tensor(splits_tensor); |
| 108 | + auto splits_span = unpacked_tensor.DataAsSpan<int64_t>(); |
| 109 | + uint32_t sum_of_splits = std::accumulate(splits_span.begin(), splits_span.end(), SafeInt<uint32_t>(0)); |
| 110 | + if (sum_of_splits != split_dims_at_axis) { |
| 111 | + LOGS_DEFAULT(VERBOSE) << "Sum of the 'split' input values must equal to the dim value at 'axis' specified. " |
| 112 | + << "dim value at 'axis' specified: " |
| 113 | + << split_dims_at_axis |
| 114 | + << ", sum of 'split' input values: " |
| 115 | + << sum_of_splits; |
| 116 | + return false; |
| 117 | + } |
| 118 | + |
| 119 | + auto it = std::adjacent_find(splits_span.begin(), splits_span.end(), [](const auto& a, const auto& b) { |
| 120 | + return a != b; |
| 121 | + }); |
| 122 | + if (it != splits_span.end()) { |
| 123 | + LOGS_DEFAULT(VERBOSE) << "NNAPI only supports the case that number of splits evenly divides split axis size"; |
| 124 | + return false; |
| 125 | + } |
| 126 | + } else { |
| 127 | + uint32_t num_outputs; |
| 128 | + if (node_unit.SinceVersion() >= 18) { |
| 129 | + auto num_outputs_attr = helper.GetInt("num_outputs"); |
| 130 | + if (!num_outputs_attr.has_value()) { |
| 131 | + LOGS_DEFAULT(VERBOSE) << "No 'num_outputs' provided. For split 18+, num_outputs is a required attribute."; |
| 132 | + return false; |
| 133 | + } |
| 134 | + num_outputs = SafeInt<uint32_t>(*num_outputs_attr); |
| 135 | + if (num_outputs != SafeInt<uint32_t>(node_unit.Outputs().size()) || num_outputs > split_dims_at_axis) { |
| 136 | + LOGS_DEFAULT(VERBOSE) << "Invalid num_outputs provided. " |
| 137 | + << "The value should be less than or equal to the size of dimension being split " |
| 138 | + << "and align with the size of output nodes. Current num_outputs: " |
| 139 | + << num_outputs; |
| 140 | + return false; |
| 141 | + } |
| 142 | + } else { |
| 143 | + num_outputs = SafeInt<uint32_t>(node_unit.Outputs().size()); |
| 144 | + } |
| 145 | + // NNAPI only supports the case where axis can be evenly divided by num of splits |
| 146 | + if (split_dims_at_axis % num_outputs != 0) { |
| 147 | + LOGS_DEFAULT(VERBOSE) << "split count: " << num_outputs << " doesn't evenly divide split dimension: " |
| 148 | + << split_dims_at_axis; |
| 149 | + return false; |
| 150 | + } |
| 151 | + } |
| 152 | + return true; |
| 153 | +} |
| 154 | + |
| 155 | +void CreateSplitOpBuilder(const std::string& op_type, OpBuilderRegistrations& op_registrations) { |
| 156 | + op_registrations.builders.push_back(std::make_unique<SplitOpBuilder>()); |
| 157 | + op_registrations.op_builder_map.emplace(op_type, op_registrations.builders.back().get()); |
| 158 | +} |
| 159 | + |
| 160 | +} // namespace nnapi |
| 161 | +} // namespace onnxruntime |
0 commit comments