ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Gather.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
3 * Copyright 2017 The TensorFlow Authors. All Rights Reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#ifndef __NNFW_CKER_GATHER_H__
19#define __NNFW_CKER_GATHER_H__
20
21#include "cker/Shape.h"
22#include "cker/Types.h"
23#include "cker/Utils.h"
24
25namespace nnfw
26{
27namespace cker
28{
29
30template <typename T, typename CoordsT = int32_t>
31inline void Gather(const GatherParams &op_params, const Shape &input_shape, const T *input_data,
32 const Shape &coords_shape, const CoordsT *coords_data, const Shape &,
33 T *output_data)
34{
35 int axis = op_params.axis;
36 if (axis < 0)
37 {
38 axis += input_shape.DimensionsCount();
39 }
40 assert(axis >= 0);
41 assert(axis < input_shape.DimensionsCount());
42 const int axis_size = input_shape.Dims(axis);
43 const int coords_count = coords_shape.FlatSize();
44
45 int outer_size = 1;
46 for (int i = 0; i < axis; ++i)
47 {
48 outer_size *= input_shape.Dims(i);
49 }
50
51 int inner_size = 1;
52 for (int i = axis + 1; i < input_shape.DimensionsCount(); ++i)
53 {
54 inner_size *= input_shape.Dims(i);
55 }
56
57 for (int outer = 0; outer < outer_size; ++outer)
58 {
59 for (int i = 0; i < coords_count; ++i)
60 {
61 assert(coords_data[i] >= 0);
62 assert(coords_data[i] < axis_size);
63 std::memcpy(output_data + (outer * coords_count + i) * inner_size,
64 input_data + (outer * axis_size + coords_data[i]) * inner_size,
65 sizeof(T) * inner_size);
66 }
67 }
68}
69
70} // namespace cker
71} // namespace nnfw
72
73#endif // __NNFW_CKER_GATHER_H__
int32_t DimensionsCount() const
Definition Shape.h:91
int32_t Dims(int i) const
Definition Shape.h:92
int FlatSize() const
Definition Shape.h:181
void Gather(const GatherParams &op_params, const Shape &input_shape, const T *input_data, const Shape &coords_shape, const CoordsT *coords_data, const Shape &, T *output_data)
Definition Gather.h:31
Definition topk_v2.h:30