ONE - On-device Neural Engine
Loading...
Searching...
No Matches
ReduceMean.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "ReduceMean.h"
18
19#include "ONNXHelpers.h"
20#include "AttributeHelpers.h"
21
23
24#include <numeric>
25
26namespace mir_onnx
27{
28
29void convertReduceMeanV1(const onnx::NodeProto &onnx_node, ConverterContext *context)
30{
31 const auto inputs = context->getNodeInputs(onnx_node);
32 assert(inputs.size() == 1);
33
34 const auto axes = getAttributeValue<std::vector<std::int64_t>>(onnx_node, "axes");
35 const auto keepdims = getAttributeValue<int64_t>(onnx_node, "keepdims", 1);
36
37 std::vector<int32_t> reduce_dims;
38 if (axes.empty())
39 { // reduce over all dimensions
40 reduce_dims.resize(inputs[0]->getShape().rank());
41 std::iota(reduce_dims.begin(), reduce_dims.end(), 0);
42 }
43 else
44 {
45 auto rank = inputs[0]->getShape().rank();
46
47 std::transform(axes.begin(), axes.end(), std::back_inserter(reduce_dims),
48 [rank](int64_t axis) { return axis < 0 ? axis + rank : axis; });
49 }
50 // Keep the reduced dimension or not, default 1 mean keep reduced dimension.
51 bool keep_dims = static_cast<bool>(keepdims);
52
53 mir::Graph *graph = context->getGraph();
54 auto result =
55 createOp<mir::ops::ReduceMeanOp>(graph, inputs[0], reduce_dims, keep_dims)->getOutput(0);
56
57 context->setNodeOutputs(onnx_node, {result});
58}
59
60} // namespace mir_onnx
void setNodeOutputs(const onnx::NodeProto &onnx_node, const std::vector< mir::Operation::Output * > &outputs)
std::vector< mir::Operation::Output * > getNodeInputs(const onnx::NodeProto &onnx_node) const
void convertReduceMeanV1(const onnx::NodeProto &onnx_node, ConverterContext *context)