summaryrefslogtreecommitdiff
path: root/runtimes/neurun/frontend/tflite/loader.h
blob: c398cbc00dbe3d384bd79a20b343d37bf34f2208 (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
/*
 * Copyright (c) 2019 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.
 */

#ifndef __TFLITE_LOADER_LOADER_H__
#define __TFLITE_LOADER_LOADER_H__

#include "schema_generated.h"

#include "graph/Graph.h"

#include <fstream>
#include <map>

namespace tflite_loader
{
/**
 * @brief TFLite model loader
 */
class Loader
{
public:
  /**
   * @brief Construct a new Loader object
   *
   * @param graph reference on graph
   */
  explicit Loader(neurun::graph::Graph &graph) : _graph(graph){};
  ~Loader() = default;
  /**
   * @brief Load a model from file
   *
   * @param file_path
   */
  void loadFromFile(const char *file_path);
  /**
   * @brief Load a model from buffer
   *
   * @param ref Pointers on begin and end of buffer
   */
  void loadFromBuffer(std::pair<const char *, const char *> ref);

private:
  void loadModel();
  // Helper functions
  // Load subgraphs
  void loadSubgraph(const tflite::SubGraph *subgraph);
  // Load data from buffer to tensor on index
  void loadConstantTensor(const tflite::Buffer *buffer, const uint32_t &index);
  // Create operands form tflite::Tensor
  void loadOperand(const tflite::Tensor *tensor);
  // Create operations from tflite::Operator
  void loadOperation(const tflite::Operator *op);
  // Load Strides and Paddings from options to param
  template <typename Param, typename OptionsType>
  void loadStridesAndPaddings(Param &param, const OptionsType *options);
  // Load Pool2D param
  template <typename Param> void loadPool2D(Param &param, const tflite::Pool2DOptions *options);
  // Create new Operand from Shape and TypeInfo
  template <typename T>
  neurun::model::OperandIndex createOperand(const uint8_t *ptr, const neurun::model::Shape &shape,
                                            const neurun::model::TypeInfo &type_info);

  // Operations
  void loadConv2D(const tflite::Operator *op);
  void loadDepthwiseConv2D(const tflite::Operator *op);
  void loadTransposeConv(const tflite::Operator *op);
  void loadAvgPool2D(const tflite::Operator *op);
  void loadReshape(const tflite::Operator *op);
  void loadSoftmax(const tflite::Operator *op);
  void loadMaxPool2D(const tflite::Operator *op);
  void loadConcatenation(const tflite::Operator *op);
  void loadFC(const tflite::Operator *op);
  void loadAdd(const tflite::Operator *op);
  void loadSub(const tflite::Operator *op);
  void loadMul(const tflite::Operator *op);
  void loadDiv(const tflite::Operator *op);
  void loadRelu(const tflite::Operator *op);
  void loadRelu6(const tflite::Operator *op);
  void loadRsqrt(const tflite::Operator *op);
  void loadSqrt(const tflite::Operator *op);
  void loadSquaredDifference(const tflite::Operator *op);
  void loadTanh(const tflite::Operator *op);
  void loadTranspose(const tflite::Operator *op);
  void loadMean(const tflite::Operator *op);

  void loadCustom(const tflite::Operator *op);

private:
  // Buffer for loading (if needed)
  std::vector<char> _buffer;
  // Reference on loadable Graph
  neurun::graph::Graph &_graph;
  // Mapping from tflite tensor index to Graph OperandIndex
  std::map<uint32_t, neurun::model::OperandIndex> _tensor_to_operand;
  // Mapping from operator code to BuiltinOperator
  std::vector<tflite::BuiltinOperator> _op_code_to_builtin_op;
  std::unordered_map<uint32_t, std::string> _opcode_index_to_custom_opcode;
};

} // namespace tflite_loader

#endif //__TFLITE_LOADER_LOADER_H__