• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 #ifndef TENSORFLOW_LITE_DELEGATES_HEXAGON_HEXAGON_IMPLEMENTATION_H_
16 #define TENSORFLOW_LITE_DELEGATES_HEXAGON_HEXAGON_IMPLEMENTATION_H_
17 
18 #include "tensorflow/lite/delegates/hexagon/hexagon_nn_interface.h"
19 
20 namespace tflite {
21 // Holds the methods to use to Construct/Execute NN graph using Hexagon NNLib.
22 struct HexagonNN {
23   // Call this function before creating a graph. It allows the environment on
24   // the DSP to configure some settings.
25   hexagon_nn_config_fn* hexagon_nn_config;
26 
27   //   Creates a new graph and returns an identifier to refer to the new graph.
28   //   After a graph is
29   // initialized, nodes can be added to it.
30   // The returned graph is empty and cannot be executed until all nodes have
31   // been added and the graph is finalized with hexagon_nn_prepare(). Multiple
32   // graphs can be created and can be kept alive in the DSP environment
33   // simultaneously.
34   hexagon_nn_init_fn* hexagon_nn_init;
35 
36   // Provides a simple parameter between 0 and 255 to control the power saving
37   // mode.
38   // A level of 255 indicates that preference should be given to minimizing
39   // power consumption. A level of 0 indicates that preference should be given
40   // to executing as fast as possible.
41   //
42   // Returns 0 on success, otherwise failure.
43   hexagon_nn_set_powersave_level_fn* hexagon_nn_set_powersave_level;
44 
45   // Changes the debug verbosity level for messages.
46   hexagon_nn_set_debug_level_fn* hexagon_nn_set_debug_level;
47 
48   // Prepares a network for execution.
49   // This function is required after all the nodes have been appended and before
50   // execution.
51   // This call provides a hook where memory can be allocated, data
52   // can be rearranged, inputs and outputs can be linked up, and things in the
53   // graph can be optimized.
54   // Once a network has been prepared, it can no longer
55   // be appended to, but it can be executed.
56   //
57   // Returns 0 on success, otherwise failure.
58   hexagon_nn_prepare_fn* hexagon_nn_prepare;
59 
60   // Adds an ordinary (non-constant) node to the graph.
61   // Non-constant nodes can have zero or more inputs and zero or more outputs.
62   // An input is described as a source node ID as well as an output index to
63   // refer to which one of several outputs a node may have.
64   // An output is described with a maximum size. The true size of an output can
65   // be computed dynamically, but the caller must define the maximum amount of
66   // data storage required by the output during node creation.
67   //
68   // Returns 0 on success, otherwise failure.
69   hexagon_nn_append_node_fn* hexagon_nn_append_node;
70 
71   // Adds constant nodes to a graph.
72   // Constant nodes produce a single output that can be connected to one graph
73   // node input. Unique node_ids are required for referencing nodes when
74   // connecting the graph (for example, specifying which outputs of earlier
75   // nodes will be used as inputs to particular subsequent nodes). Node_ids are
76   // selected by the caller, but node_id=0 and node_id>0xF0000000 are reserved.
77   // Node_ids must be unique.
78   // *** NOTE: On SDM835 and older targets,
79   // hexagon_nn_append_const_node() will not work properly for arrays larger
80   // than 32 MB. Instead, use hexagon_nn_append_empty_const_node_large_array(),
81   // which expects the same arguments.
82   //
83   // Returns 0 on success, otherwise failure.
84   hexagon_nn_append_const_node_fn* hexagon_nn_append_const_node;
85 
86   // Executes a network, with provided input data and returning output data.
87   // Execution will fail if the network has not been prepared.
88   // Input is provided to the INPUT node, and output is returned from the OUTPUT
89   // node.
90   //
91   // Returns 0 on success, otherwise failure.
92   hexagon_nn_execute_fn* hexagon_nn_execute;
93 
94   // Newer version of hexagon_nn_execute that utilizes hexagon_nn_tensordefs to
95   // represent inputs & outputs. Executes a network with provided input tensors
96   // and returns output tensors. Execution will fail if the network has not
97   // been prepared.
98   //
99   // Returns 0 on success, otherwise failure.
100   hexagon_nn_execute_new_fn* hexagon_nn_execute_new;
101 
102   // Tears down and frees an NN graph. This can be done at any time after
103   // hexagon_nn_init(). After this function has been invoked, the nn_id id is
104   // invalid.
105   //
106   // Returns 0 on success, otherwise failure.
107   hexagon_nn_teardown_fn* hexagon_nn_teardown;
108 
109   hexagon_nn_snpprint_fn* hexagon_nn_snpprint;
110 
111   hexagon_nn_getlog_fn* hexagon_nn_getlog;
112 
113   hexagon_nn_get_perfinfo_fn* hexagon_nn_get_perfinfo;
114 
115   hexagon_nn_reset_perfinfo_fn* hexagon_nn_reset_perfinfo;
116 
117   hexagon_nn_op_id_to_name_fn* hexagon_nn_op_id_to_name;
118 
119   // Should be called once to shutdown DSP and cleanup.
120   hexagon_nn_global_teardown_fn* hexagon_nn_global_teardown;
121 
122   // Should be called once to initialize DSP.
123   hexagon_nn_global_init_fn* hexagon_nn_global_init;
124 
125   // Returns true if the device SoC is supported by hexagon library. False
126   // Otherwise.
127   hexagon_nn_is_device_supported_fn* hexagon_nn_is_device_supported;
128 
129   // Returns the version number of the interface library.
130   hexagon_nn_hexagon_interface_version_fn* hexagon_nn_hexagon_interface_version;
131 
132   hexagon_nn_version_fn* hexagon_nn_version = nullptr;
133 
134   bool interface_loaded = false;
135 };
136 
137 // Returns an instance of HexagonNN.
138 const HexagonNN* HexagonNNImplementation();
139 
140 }  // namespace tflite
141 
142 #endif  // TENSORFLOW_LITE_DELEGATES_HEXAGON_HEXAGON_IMPLEMENTATION_H_
143