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 graph_runtime.h
22  * \brief Tiny graph runtime that can run graph containing only tvm PackedFunc.
23  */
24 #ifndef TVM_RUNTIME_CRT_GRAPH_RUNTIME_H_
25 #define TVM_RUNTIME_CRT_GRAPH_RUNTIME_H_
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 #include <dlpack/dlpack.h>
32 #include <tvm/runtime/c_runtime_api.h>
33 #include <tvm/runtime/crt/packed_func.h>
34 
35 struct TVMModule;
36 
37 /*! \brief operator attributes about tvm op */
38 typedef struct TVMOpParam {
39   char func_name[120];
40   uint32_t num_inputs;
41   uint32_t num_outputs;
42   uint32_t flatten_data;
43 } TVMOpParam;
44 
45 // Graph attribute
46 typedef struct TVMGraphRuntimeGraphAttr {
47   uint32_t storage_num_not_alloctaed;
48   uint32_t* storage_id;
49   uint32_t* device_index;
50   char* dltype;  // "int8", "int16", "float32"
51   uint32_t dltype_count;
52   int64_t* shape;
53   uint32_t* ndim;
54   uint32_t shape_count;
55 } TVMGraphRuntimeGraphAttr;
56 
57 typedef struct TVMGraphRuntime TVMGraphRuntime;
58 
59 // public functions
60 /*!
61  * \brief Allocate a new GraphRuntime with vmalloc and initialize it.
62  *
63  * \param sym_json JSON-encoded graph.
64  * \param m TVM Module that exposes the functions to call.
65  * \param ctxs runtime execution context.
66  */
67 TVMGraphRuntime* TVMGraphRuntime_Create(const char* sym_json, const struct TVMModule* m,
68                                         const TVMContext* ctxs);
69 
70 int TVMGraphRuntime_GetInputIndex(TVMGraphRuntime* runtime, const char* name);
71 
72 /*!
73  * \brief set input to the graph based on name.
74  * \param runtime The graph runtime.
75  * \param name The name of the input.
76  * \param data_in The input data.
77  */
78 void TVMGraphRuntime_SetInput(TVMGraphRuntime* runtime, const char* name, DLTensor* data_in);
79 
80 /*!
81  * \brief Return NDArray for given output index.
82  * \param runtime The graph runtime.
83  * \param index The output index.
84  * \param out The DLTensor corresponding to given output node index.
85  * \return The result of this function execution.
86  */
87 int TVMGraphRuntime_GetOutput(TVMGraphRuntime* runtime, const int32_t index, DLTensor* out);
88 
89 /*!
90  * \brief Load parameters from parameter blob.
91  * \param runtime The graph runtime.
92  * \param param_blob A binary blob of parameter.
93  * \param param_size The parameter size.
94  * \return The result of this function execution.
95  */
96 int TVMGraphRuntime_LoadParams(TVMGraphRuntime* runtime, const char* param_blob,
97                                const uint32_t param_size);
98 
99 /*!
100  * \brief Execute the graph.
101  * \param runtime The graph runtime.
102  */
103 void TVMGraphRuntime_Run(TVMGraphRuntime* runtime);
104 
105 /*!
106  * \brief Release memory associated with the graph runtime.
107  * \param runtime Pointer to graph runtime.
108  */
109 void TVMGraphRuntime_Release(TVMGraphRuntime** runtime);
110 
111 #ifdef __cplusplus
112 }  // extern "C"
113 #endif
114 
115 #endif  // TVM_RUNTIME_CRT_GRAPH_RUNTIME_H_
116