ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Gather.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 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 "Gather.h"
18#include "Common.h"
19
20#include "mir/Tensor.h"
21
22namespace mir_interpreter
23{
24
25using namespace mir;
26
27template <typename T, typename IndicesT> struct GatherImpl
28{
29 static void run(const TensorVariant &datav, const TensorVariant &indicesv,
30 const ops::GatherOp &op, mir::TensorVariant &res);
31};
32
33template <typename T, typename IndicesT>
34void GatherImpl<T, IndicesT>::run(const TensorVariant &datav, const TensorVariant &indicesv,
35 const ops::GatherOp &op, TensorVariant &res)
36{
37 const auto &data_shape = datav.getShape();
38 const auto &indices_shape = indicesv.getShape();
39 Tensor<T> data(datav);
40 Tensor<T> output(res);
41 Tensor<IndicesT> indices(indicesv);
42
43 int32_t axis = op.getAxis();
44 if (axis < 0)
45 axis += data_shape.rank();
46 assert(axis >= 0 && axis < data_shape.rank());
47 int32_t axis_size = data_shape.dim(axis);
48 int32_t num_indices = indices_shape.numElements();
49
50 int32_t outer_size = 1;
51 for (int32_t i = 0; i < axis; ++i)
52 outer_size *= data_shape.dim(i);
53
54 int32_t inner_size = 1;
55 for (int32_t i = axis + 1; i < data_shape.rank(); ++i)
56 inner_size *= data_shape.dim(i);
57
58 for (int32_t outer = 0; outer < outer_size; ++outer)
59 {
60 for (int32_t i = 0; i < num_indices; ++i)
61 {
62 auto index = indices.atOffset(i);
63 assert(index >= 0 && index < axis_size);
64 for (int32_t inner = 0; inner < inner_size; inner++)
65 {
66 output.atOffset((outer * num_indices + i) * inner_size + inner) =
67 data.atOffset((outer * axis_size + index) * inner_size + inner);
68 }
69 }
70 }
71}
72
73// a hack to reuse dispath function
74template <typename T> struct GatherByT
75{
76
77 template <typename IndicesT> using GatherWithFixedT = GatherImpl<T, IndicesT>;
78
79 static void run(const TensorVariant &data, const TensorVariant &indices, const ops::GatherOp &op,
80 TensorVariant &res)
81 {
82 dispatch<GatherWithFixedT>(indices.getElementType(), data, indices, op, res);
83 }
84};
85
86void Gather(const TensorVariant &data, const TensorVariant &indices, const ops::GatherOp &op,
87 TensorVariant &res)
88{
89 dispatch<GatherByT>(data.getElementType(), data, indices, op, res);
90}
91
92} // namespace mir_interpreter
T atOffset(int32_t offset) const
Definition Tensor.h:35
const Shape & getShape() const
DataType getElementType() const
Gather operation as defined by ONNX spec. https://github.com/onnx/onnx/blob/master/docs/Operators....
Definition GatherOp.h:33
int32_t getAxis() const
Definition GatherOp.h:46
void Gather(const TensorVariant &data, const TensorVariant &indices, const ops::GatherOp &op, TensorVariant &res)
Definition Gather.cpp:86
static void run(const TensorVariant &data, const TensorVariant &indices, const ops::GatherOp &op, TensorVariant &res)
Definition Gather.cpp:79
static void run(const TensorVariant &datav, const TensorVariant &indicesv, const ops::GatherOp &op, mir::TensorVariant &res)
Definition Gather.cpp:34