1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  *   http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied.  See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
20 /*!
21  * \file src/runtime/crt/include/tvm/runtime/crt/internal/graph_runtime/graph_runtime.h
22  * \brief Tiny graph runtime that can run graph containing only tvm PackedFunc.
23  */
24 #ifndef TVM_RUNTIME_CRT_INCLUDE_TVM_RUNTIME_CRT_INTERNAL_GRAPH_RUNTIME_GRAPH_RUNTIME_H_
25 #define TVM_RUNTIME_CRT_INCLUDE_TVM_RUNTIME_CRT_INTERNAL_GRAPH_RUNTIME_GRAPH_RUNTIME_H_
26 
27 #include <tvm/runtime/crt/graph_runtime.h>
28 #include <tvm/runtime/crt/internal/common/ndarray.h>
29 #include <tvm/runtime/crt/internal/graph_runtime/load_json.h>
30 #include <tvm/runtime/crt/module.h>
31 
32 // Memory pool entry.
33 typedef struct TVMGraphRuntimePoolEntry {
34   size_t size;
35   int device_type;
36 } TVMGraphRuntimePoolEntry;
37 
38 // Node entry
39 typedef struct TVMGraphRuntimeNodeEntry {
40   uint32_t node_id;
41   uint32_t index;
42   uint32_t version;
43   // JSON Loader
44   void (*Load)(JSONReader* reader);
45 } TVMGraphRuntimeNodeEntry;
46 
47 // Node
48 typedef struct TVMGraphRuntimeNode {
49   // operator type in string
50   char op_type[16];
51   // name of the op
52   char name[120];
53   // parameters
54   TVMOpParam param;
55   // inputs
56   TVMGraphRuntimeNodeEntry* inputs;
57   // number of inputs
58   size_t inputs_count;
59   // control deps
60   uint32_t control_deps[20];
61   // JSON Loader
62   void (*LoadAttrs)(struct TVMGraphRuntimeNode* node, JSONReader* reader, TVMOpParam* param);
63   // JSON Loader
64   int (*Load)(struct TVMGraphRuntimeNode* node, JSONReader* reader);
65 } TVMGraphRuntimeNode;
66 
67 typedef struct TVMGraphRuntime {
68   /*! \brief The graph nodes. */
69   TVMGraphRuntimeNode* nodes;
70   /*! \brief The graph nodes counter. */
71   uint32_t nodes_count;
72   /*! \brief The argument nodes. */
73   uint32_t* input_nodes;
74   uint32_t input_nodes_count;
75   /*! \brief Used for quick entry indexing. */
76   uint32_t* node_row_ptr;
77   uint32_t node_row_ptr_count;
78   /*! \brief Output entries. */
79   TVMGraphRuntimeNodeEntry* outputs;
80   /*! \brief Output entries counter. */
81   uint32_t outputs_count;
82   /*! \brief Additional graph attributes. */
83   TVMGraphRuntimeGraphAttr attrs;
84   /*! \brief The code module that contains both host and device code. */
85   TVMModuleHandle module_handle;
86   /*! \brief Execution context of all devices including the host. */
87   TVMContext ctxs[1];
88   uint32_t ctxs_count;
89   /*! \brief Common storage pool for all devices. */
90   TVMNDArray* storage_pool;
91   uint32_t storage_pool_count;
92   /*! \brief Data entry of each node. */
93   TVMNDArray* data_entry;
94   uint32_t data_entry_count;
95   /*! \brief Operator on each node. */
96   TVMPackedFunc* op_execs;
97   uint32_t op_execs_count;
98 } TVMGraphRuntime;
99 
100 typedef DLTensor* DLTensorPtr;
101 
102 // private functions
103 void TVMGraphRuntime_SetInput(TVMGraphRuntime* runtime, const char* name, DLTensor* data_in);
104 int TVMGraphRuntime_LoadParams(TVMGraphRuntime* runtime, const char* param_blob,
105                                const uint32_t param_size);
106 void TVMGraphRuntime_Run(TVMGraphRuntime* runtime);
107 int TVMGraphRuntime_GetOutput(TVMGraphRuntime* runtime, const int32_t idx, DLTensor* out);
108 
109 int32_t TVMGraphRuntime_CreateTVMOp(TVMGraphRuntime* runtime, const TVMOpParam* param,
110                                     DLTensorPtr* args, const uint32_t args_count,
111                                     uint32_t num_inputs, TVMPackedFunc* pf);
112 
113 #endif  // TVM_RUNTIME_CRT_INCLUDE_TVM_RUNTIME_CRT_INTERNAL_GRAPH_RUNTIME_GRAPH_RUNTIME_H_
114