1"""Definitions for targets that use the TFLite shims.""" 2 3load( 4 "//tensorflow/lite:build_def.bzl", 5 "tflite_copts_warnings", 6 "tflite_custom_c_library", 7 "tflite_jni_binary", 8) 9load("@build_bazel_rules_android//android:rules.bzl", "android_library") 10 11def _concat(lists): 12 """Concatenate a list of lists, without requiring the inner lists to be iterable. 13 14 This allows the inner lists to be obtained by calls to select(). 15 """ 16 result = [] 17 for selected_list in lists: 18 result = result + selected_list 19 return result 20 21def alias_with_tflite(name, actual, **kwargs): 22 """Defines an alias for a target that uses the TFLite shims. 23 24 This rule 'alias_with_tflite' should be used instead of the native 25 'alias' rule whenever the 'actual' target that is being aliased 26 is defined using one of the *_with_tflite build macros. 27 28 Args: 29 name: determines the name used for the alias target. 30 actual: the target that the alias target is aliased to. 31 **kwargs: additional alias parameters. 32 """ 33 native.alias(name = name, actual = actual, **kwargs) 34 35def android_library_with_tflite( 36 name, 37 deps = [], 38 tflite_deps = [], 39 exports = [], 40 tflite_exports = [], 41 **kwargs): 42 """Defines an android_library that uses the TFLite shims. 43 44 This is a hook to allow applying different build flags (etc.) 45 for targets that use the TFLite shims. 46 47 Note that this build rule doesn't itself add any dependencies on 48 TF Lite; this macro should normally be used in conjunction with a 49 direct or indirect 'tflite_deps' dependency on one of the "shim" 50 library targets from //third_party/tensorflow/lite/core/shims:*. 51 52 Args: 53 name: as for android_library. 54 deps: as for android_library. 55 tflite_deps: dependencies on rules that are themselves defined using 56 'cc_library_with_tflite' / 'android_library_with_tflite'. 57 exports: same as for android_library. 58 tflite_exports: exported dependencies that are themselves defined using 59 'cc_library_with_tflite' / 'android_library_with_tflite'. 60 **kwargs: Additional android_library parameters. 61 """ 62 android_library( 63 name = name, 64 exports = exports + tflite_exports, 65 deps = deps + tflite_deps, 66 **kwargs 67 ) 68 69def cc_library_with_tflite( 70 name, 71 srcs = [], 72 tflite_jni_binaries = [], 73 deps = [], 74 tflite_deps = [], 75 tflite_deps_selects = [], 76 **kwargs): 77 """Defines a cc_library that uses the TFLite shims. 78 79 This is a hook to allow applying different build flags (etc.) 80 for targets that use the TFLite shims. 81 82 Note that this build rule doesn't itself add any dependencies on 83 TF Lite; this macro should normally be used in conjunction with a 84 direct or indirect 'tflite_deps' dependency on one of the "shim" 85 library targets from //tensorflow/lite/core/shims:*. 86 87 Args: 88 name: as for cc_library. 89 srcs: as for cc_library. 90 tflite_jni_binaries: dependencies on shared libraries that are defined 91 using 'jni_binary_with_tflite'. 92 deps: as for cc_library. 93 tflite_deps: dependencies on rules that are themselves defined using 94 'cc_library_with_tflite'. 95 tflite_deps_selects: A list of dictionaries that will be converted to dependencies 96 with select on rules. 97 **kwargs: Additional cc_library parameters. 98 """ 99 native.cc_library( 100 name = name, 101 srcs = srcs + tflite_jni_binaries, 102 deps = deps + tflite_deps + _concat([select(map) for map in tflite_deps_selects]), 103 **kwargs 104 ) 105 106def cc_test_with_tflite( 107 name, 108 deps = [], 109 tflite_deps = [], 110 **kwargs): 111 """Defines a cc_test that uses the TFLite shims. 112 113 This is a hook to allow applying different build flags (etc.) 114 for targets that use the TFLite shims. 115 116 Note that this build rule doesn't itself add any dependencies on 117 TF Lite; this macro should normally be used in conjunction with a 118 direct or indirect 'tflite_deps' dependency on one of the "shim" 119 library targets from //third_party/tensorflow/lite/core/shims:*. 120 121 Args: 122 name: as for cc_test. 123 deps: as for cc_test. 124 tflite_deps: dependencies on rules that are themselves defined using 125 'cc_library_with_tflite'. 126 **kwargs: Additional cc_test parameters. 127 """ 128 native.cc_test( 129 name = name, 130 deps = deps + tflite_deps, 131 **kwargs 132 ) 133 134def java_library_with_tflite( 135 name, 136 deps = [], 137 runtime_deps = [], 138 tflite_deps = [], 139 tflite_jni_binaries = [], 140 exports = [], 141 tflite_exports = [], 142 **kwargs): 143 """Defines an java_library that uses the TFLite shims. 144 145 This is a hook to allow applying different build flags (etc.) 146 for targets that use the TFLite shims. 147 148 Note that this build rule doesn't itself add any dependencies on 149 TF Lite; this macro should normally be used in conjunction with a 150 direct or indirect 'tflite_deps' or 'tflite_jni_binaries' dependency 151 on one of the "shim" library targets from 152 //third_party/tensorflow/lite/core/shims:*. 153 154 Args: 155 name: as for java_library. 156 deps: as for java_library. 157 runtime_deps: as for java_library. 158 tflite_deps: dependencies on rules that are themselves defined using 159 'cc_library_with_tflite' / 'java_library_with_tflite'. 160 tflite_jni_binaries: dependencies on shared libraries that are defined 161 using 'jni_binary_with_tflite'. 162 exports: same as for java_library. 163 tflite_exports: exported dependencies that are themselves defined using 164 'cc_library_with_tflite' / 'java_library_with_tflite'. 165 **kwargs: Additional java_library parameters. 166 """ 167 native.java_library( 168 name = name, 169 exports = exports + tflite_exports, 170 deps = deps + tflite_deps + tflite_jni_binaries, 171 **kwargs 172 ) 173 174def java_test_with_tflite( 175 name, 176 deps = [], 177 runtime_deps = [], 178 tflite_deps = [], 179 tflite_jni_binaries = [], 180 **kwargs): 181 """Defines an java_library that uses the TFLite shims. 182 183 This is a hook to allow applying different build flags (etc.) 184 for targets that use the TFLite shims. 185 186 Note that this build rule doesn't itself add any dependencies on 187 TF Lite; this macro should normally be used in conjunction with a 188 direct or indirect 'tflite_deps' or 'tflite_jni_binaries' dependency 189 on one of the "shim" library targets from 190 //third_party/tensorflow/lite/core/shims:*. 191 192 Args: 193 name: as for java_library. 194 deps: as for java_library. 195 runtime_deps: as for java_library. 196 tflite_deps: dependencies on rules that are themselves defined using 197 'cc_library_with_tflite' / 'java_library_with_tflite'. 198 tflite_jni_binaries: dependencies on shared libraries that are defined 199 using 'jni_binary_with_tflite'. 200 **kwargs: Additional java_library parameters. 201 """ 202 native.java_test( 203 name = name, 204 deps = deps + tflite_deps, 205 runtime_deps = deps + tflite_jni_binaries, 206 **kwargs 207 ) 208 209def jni_binary_with_tflite( 210 name, 211 deps = [], 212 tflite_deps = [], 213 **kwargs): 214 """Defines a tflite_jni_binary that uses the TFLite shims. 215 216 This is a hook to allow applying different build flags (etc.) 217 for targets that use the TFLite shims. 218 219 Note that this build rule doesn't itself add any dependencies on 220 TF Lite; this macro should normally be used in conjunction with a 221 direct or indirect 'tflite_deps' dependency on one of the "shim" 222 library targets from //third_party/tensorflow/lite/core/shims:*. 223 224 Args: 225 name: as for tflite_jni_binary. 226 deps: as for tflite_jni_binary. 227 tflite_deps: dependencies on rules that are themselves defined using 228 'cc_library_with_tflite'. 229 **kwargs: Additional tflite_jni_binary parameters. 230 """ 231 tflite_jni_binary( 232 name = name, 233 deps = deps + tflite_deps, 234 **kwargs 235 ) 236 237def custom_c_library_with_tflite( 238 name, 239 models = [], 240 **kwargs): 241 """Generates a tflite c library, stripping off unused operators. 242 243 This library includes the C API and the op kernels used in the given models. 244 245 Args: 246 name: Str, name of the target. 247 models: List of models. This TFLite build will only include 248 operators used in these models. If the list is empty, all builtin 249 operators are included. 250 **kwargs: kwargs to cc_library_with_tflite. 251 """ 252 tflite_custom_c_library( 253 name = "%s_c_api" % name, 254 models = models, 255 ) 256 257 cc_library_with_tflite( 258 name = name, 259 hdrs = ["//tensorflow/lite/core/shims:c/c_api.h"], 260 copts = tflite_copts_warnings(), 261 deps = [ 262 ":%s_c_api" % name, 263 ], 264 **kwargs 265 ) 266