ONE - On-device Neural Engine
Loading...
Searching...
No Matches
OneHot.cpp
Go to the documentation of this file.
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 "OneHot.h"
18#include "Convert.h"
19
20namespace tflchef
21{
22
23void TFliteOpOneHot::filler(const tflite::Operator *op, TFliteImport *import,
24 tflchef::ModelRecipe *model_recipe) const
25{
26 // only depth(second input) has constant on recipe cause depth value is used in shape inference.
27 const auto &inputs = *op->inputs();
28
29 const tflite::Tensor *tensor = import->tensors()->Get(inputs[1]);
30 assert(tensor->type() == tflite::TensorType::TensorType_INT32);
31 const tflite::Buffer *buffer = import->buffers()->Get(tensor->buffer());
32
33 if (buffer && buffer->data())
34 {
35 auto vec = extract_buffer<int32_t>(buffer);
36 import->set_tensor_filler(inputs[1], vec);
37 }
38
39 // on/off can be dtype of input/output. let's support INT32/FLOAT32 for now
40 for (int32_t index = 2; index <= 3; ++index)
41 {
42 const tflite::Tensor *tensor = import->tensors()->Get(inputs[index]);
43 const tflite::Buffer *buffer = import->buffers()->Get(tensor->buffer());
44 if (buffer && buffer->data())
45 {
46 switch (tensor->type())
47 {
48 case tflite::TensorType::TensorType_INT32:
49 {
50 auto vec = extract_buffer<int32_t>(buffer);
51 import->set_tensor_filler(inputs[index], vec);
52 break;
53 }
54
55 case tflite::TensorType::TensorType_FLOAT32:
56 {
57 auto vec = extract_buffer<float>(buffer);
58 import->set_tensor_filler(inputs[index], vec);
59 break;
60 }
61
62 default:
63 assert(false);
64 break;
65 }
66 }
67 }
68}
69
70tflchef::Operation *TFliteOpOneHot::build(RecipeChefContext *ctx) const
71{
72 tflchef::Operation *operation = ctx->chefop;
73 const tflite::Operator *op = ctx->tflop;
74
75 auto op_params = op->builtin_options_as_OneHotOptions();
76 assert(op_params != nullptr);
77
78 operation->set_type("OneHot");
79
80 auto op_options = operation->mutable_onehot_options();
81
82 op_options->set_axis(op_params->axis());
83
84 return operation;
85}
86
87} // namespace tflchef
Loads TF lite file and provides helpers to access attributes.
void filler(const tflite::Operator *op, TFliteImport *import, tflchef::ModelRecipe *model_recipe) const override
Definition OneHot.cpp:23
tflchef::Operation * build(RecipeChefContext *ctx) const override
Definition OneHot.cpp:70
tflchef::Operation * chefop
const tflite::Operator * tflop