Imported Upstream version 1.4.0
[platform/core/ml/nnfw.git] / runtime / onert / backend / cpu / kernel / OneHotLayer.cc
1 /*
2  * Copyright (c) 2020 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 "OneHotLayer.h"
18
19 #include "OperationUtils.h"
20
21 #include <cker/operation/OneHot.h>
22
23 namespace onert
24 {
25 namespace backend
26 {
27 namespace cpu
28 {
29 namespace kernel
30 {
31
32 void OneHotLayer::oneHotFloat32()
33 {
34   nnfw::cker::OneHot<float, int32_t>(
35       _depth, _on_value, _off_value, _axis, convertTensorToCkerShape(_indices),
36       reinterpret_cast<const int32_t *>(_indices->buffer()), convertTensorToCkerShape(_output),
37       reinterpret_cast<float *>(_output->buffer()));
38 }
39
40 void OneHotLayer::oneHotQuant8() { throw std::runtime_error{"OneHot NYI for quantized"}; }
41
42 void OneHotLayer::configure(const operand::Tensor *indices, operand::Tensor *output, int32_t depth,
43                             float on_value, float off_value, int32_t axis)
44 {
45   _indices = indices;
46   _output = output;
47   _depth = depth;
48   _on_value = on_value;
49   _off_value = off_value;
50   _axis = axis;
51   if (_axis == -1)
52     _axis = _indices->num_dimensions();
53 }
54
55 void OneHotLayer::run()
56 {
57   if (_output->data_type() == OperandType::FLOAT32)
58   {
59     oneHotFloat32();
60   }
61   else if (_output->data_type() == OperandType::QUANT8_ASYMM)
62   {
63     oneHotQuant8();
64   }
65 }
66
67 } // namespace kernel
68 } // namespace cpu
69 } // namespace backend
70 } // namespace onert