1 /** 2 * Copyright (c) Glow Contributors. See CONTRIBUTORS file. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /** 18 * Contributed by Xperi Corporation on August 13, 2019 19 */ 20 21 #ifndef X_INFERENCE_LIB_H 22 #define X_INFERENCE_LIB_H 23 24 #include <stdint.h> 25 #include <stdlib.h> 26 27 #ifdef ENABLE_PERF_MONITORING 28 #include "x_perf_monitor.h" 29 #endif // ENABLE_PERF_MONITORING 30 31 /// Successful return 32 #define X_SUCCESS (0) 33 /// Failure 34 #define X_FAILURE (-1) 35 36 /// Inference function pointer type. 37 typedef void (*InferenceFunctionPtr_t)(uint8_t *, uint8_t *, uint8_t *); 38 39 /// The symbol table entry for Glow bundle symbols. 40 struct SymbolTableEntry { 41 const char *name; 42 uint64_t offset; 43 uint64_t size; 44 char kind; 45 }; 46 47 /// The Glow bundle config structure. 48 struct BundleConfig { 49 uint64_t constWeightVarsMemsize; 50 uint64_t mutWeightVarsMemsize; 51 uint64_t activationMemsize; 52 uint64_t alignment; 53 uint64_t numSymbols; 54 const struct SymbolTableEntry *symbols; 55 }; 56 57 /// The runtime data structure. 58 struct RuntimeData { 59 /// Pointer to the location of constant weights in memory. 60 uint8_t *constWeights; 61 /// Pointer to the location of mutable weights in memory. 62 uint8_t *mutWeights; 63 /// Pointer to the location of activations in memory. 64 uint8_t *activations; 65 /// The inference function 66 InferenceFunctionPtr_t inferenceFunc; 67 /// Offset of the input tensor in memory 68 size_t inputOffset; 69 /// Offset of the output tensor in memory 70 size_t outputOffset; 71 72 #ifdef ENABLE_PERF_MONITORING 73 /// Performance statistics 74 struct PerfStatistics ps; 75 /// Should we monitor performance? 76 int doPerfMonitoring; 77 #endif // ENABLE_PERF_MONITORING 78 }; 79 80 /// Inference IO metadata 81 struct InferenceIO { 82 /// Pointer to the input data 83 void *input; 84 /// Pointer to location of output buffer 85 void *output; 86 /// Should input memory be deallocated (yes if we allocated it, no if we were 87 /// passed a pointer to a previously allocated region) 88 int cleanupInput; 89 /// Should output memory be deallocated (yes if we allocated it, no if we were 90 /// passed a pointer to a previously allocated region) 91 int cleanupOutput; 92 /// Input data length 93 size_t inLen; 94 /// Output data length 95 size_t outLen; 96 /// Batch size 97 size_t batchSize; 98 }; 99 100 /// Network metadata 101 struct NetworkData { 102 /// The Glow bundle config 103 struct BundleConfig *bundleConfig; 104 /// Name of the input tensor 105 char *inputTensorName; 106 /// Name of the output tensor 107 char *outputTensorName; 108 /// Name of the weights file 109 char *weightsFileName; 110 /// Inference function 111 InferenceFunctionPtr_t inferenceFunction; 112 /// Should we monitor performance? 113 int doPerfMonitoring; 114 }; 115 116 /// Initialize runtime data \p runtimeData given the network metadata \p 117 /// networkData. \returns X_SUCCESS on success, X_FAILURE on failure. 118 int initRuntimeData(const struct NetworkData *networkData, 119 struct RuntimeData *runtimeData); 120 121 /// Initialize inference IO metadata given pointers to the IO memory \p inMMap 122 /// and \p outMMap. \p inMMap may be null, then memory is allocated (and 123 /// deallocated when done). \p outMMap may be null, then memory is allocated 124 /// (and deallocated when done). If \p inMMap and \p outMMap are not null, IO 125 /// memory is not allocated/deallocated. returns X_SUCCESS on success, X_FAILURE 126 /// on failure. 127 int initIO(struct InferenceIO *iio, void *inMMap, void *outMMap); 128 129 /// Clean up the runtime data \p runtimeData, which ammounts to deallocating 130 /// allocated resources. 131 void cleanupRuntimeData(struct RuntimeData *runtimeData); 132 133 /// Clean up inference IO \p io, which ammounts to deallocating allocated 134 /// resources. 135 void cleanupIO(struct InferenceIO *io); 136 137 /// Run the inference given Inference IO \p iio and the runtime data \p 138 /// runtimeData. 139 void runInference(const struct InferenceIO *iio, 140 struct RuntimeData *runtimeData); 141 142 #endif // X_INFERENCE_LIB_H 143