blob: ef030dc5df55e3d77fe5105331d058e552f263d3 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
/*
* Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "Context.h"
#include "Convert.h"
#include <coco/IR/Data.h>
#include <coco/IR/Module.h>
#include <nncc/core/ADT/tensor/Shape.h>
#include <schema_generated.h>
#include <map>
#include <sstream>
using namespace nncc::core::ADT;
namespace tflimport
{
void TensorContext::prepare(const tflite::SubGraph *graph)
{
for (uint32_t tensor_id = 0; tensor_id < graph->tensors()->size(); ++tensor_id)
{
auto const tensor_info = graph->tensors()->Get(tensor_id);
auto const tensor_name = tensor_info->name()->str();
auto const tensor_shape = as_tensor_shape(tensor_info->shape());
auto const tensor_type = tensor_info->type();
_name_ctx[tensor_id] = tensor_name;
_shape_ctx[tensor_id] = tensor_shape;
_type_ctx[tensor_id] = tensor_type;
}
}
TflOpCodeContext::TflOpCodeContext(
const flatbuffers::Vector<flatbuffers::Offset<tflite::OperatorCode>> *opcodes)
{
for (const tflite::OperatorCode *opcode : *opcodes)
{
_opcodes.push_back(opcode);
}
}
tflite::BuiltinOperator TflOpCodeContext::builtin_code(const tflite::Operator *op) const
{
uint32_t index = op->opcode_index();
assert(index < _opcodes.size());
const tflite::OperatorCode *opcode = _opcodes.at(index);
return opcode->builtin_code();
}
std::string TflOpCodeContext::opcode_name(const tflite::Operator *op) const
{
uint32_t index = op->opcode_index();
assert(index < _opcodes.size());
const tflite::OperatorCode *opcode = _opcodes.at(index);
if (!is_valid(opcode))
{
std::ostringstream oss;
oss << "(invalid: " << index << ")";
return oss.str();
}
if (is_custom(opcode))
{
if (!opcode->custom_code())
return "(invalid custom)";
return opcode->custom_code()->c_str();
}
tflite::BuiltinOperator code = opcode->builtin_code();
return EnumNameBuiltinOperator(code);
}
bool TflOpCodeContext::is_valid(const tflite::OperatorCode *opcode)
{
tflite::BuiltinOperator code = opcode->builtin_code();
return (tflite::BuiltinOperator_MIN <= code && code <= tflite::BuiltinOperator_MAX);
}
bool TflOpCodeContext::is_custom(const tflite::OperatorCode *opcode)
{
tflite::BuiltinOperator code = opcode->builtin_code();
return (code == tflite::BuiltinOperator_CUSTOM);
}
TflBufferContext::TflBufferContext(const tflite::Model *tfl_model)
{
const flatbuffers::Vector<flatbuffers::Offset<tflite::Buffer>> *tfl_buffers;
tfl_buffers = tfl_model->buffers();
for (uint32_t buffer_id = 0; buffer_id < tfl_buffers->size(); ++buffer_id)
{
_buffer_ctx[buffer_id] = (*tfl_buffers)[buffer_id];
}
}
} // namespace tflimport
|