Imported Upstream version 1.8.0
[platform/core/ml/nnfw.git] / tests / custom_op / FillFrom / kernels / FillFromKernel.cc
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 "nnfw_experimental.h"
18
19 #include "flatbuffers/flexbuffers.h"
20
21 #include <stdexcept>
22 #include <limits>
23
24 extern "C" void FillFromEval(nnfw_custom_kernel_params *params, char *userdata,
25                              size_t userdata_size)
26 {
27   auto userdata_root = flexbuffers::GetRoot(reinterpret_cast<uint8_t *>(userdata), userdata_size);
28
29   auto attr_map = userdata_root.AsMap();
30
31   auto idx = attr_map["idx"].AsInt32();
32   auto val = attr_map["val"].AsFloat();
33
34   int32_t flat_size = 1;
35   for (int32_t i = 0; i < params->inputs[0].type.rank; ++i)
36   {
37     flat_size *= params->inputs[0].type.dims[i];
38   }
39
40   if (!(0 <= idx && idx < flat_size))
41     throw std::runtime_error("Value of attr Idx is out of range");
42
43   auto output_flat = static_cast<float *>(params->outputs[0].allocation);
44   auto input_flat = static_cast<float *>(params->inputs[0].allocation);
45
46   for (int32_t i = 0; i < idx; ++i)
47   {
48     output_flat[i] = input_flat[i];
49   }
50
51   for (int32_t i = idx; i < flat_size; ++i)
52   {
53     output_flat[i] = val;
54   }
55 }