-
Notifications
You must be signed in to change notification settings - Fork 836
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
codegen: Add Bazel macro for inference library (#2366)
The TFLM code_generator takes a TFLite model and generates C/C++ source code that can then be compiled into a binary. This change adds a Bazel macro for invoking the code generator and creating a cc_library with the resulting sources. It also updates the checked-in hello_world example with an appropriate BUILD file and updates the script to use Bazel instead of make. BUG=b/294230402
- Loading branch information
Showing
7 changed files
with
89 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
""" Build rule for generating ML inference code from TFLite model. """ | ||
|
||
load("//tensorflow/lite/micro:build_def.bzl", "micro_copts") | ||
|
||
def tflm_inference_library( | ||
name, | ||
tflite_model, | ||
visibility = None): | ||
"""Creates a C++ library capable of performing ML inference of the provided | ||
model. | ||
Args: | ||
name: Target name. | ||
tflite_model: TFLite Model to generate inference from. | ||
visibility: Visibility for the C++ library. | ||
""" | ||
generated_target = name + "_gen" | ||
native.genrule( | ||
name = generated_target, | ||
srcs = [tflite_model], | ||
outs = [name + ".h", name + ".cc"], | ||
tools = ["//codegen:code_generator"], | ||
cmd = "$(location //codegen:code_generator) " + | ||
"--model=$< --output_dir=$(RULEDIR) --output_name=%s" % name, | ||
visibility = ["//visibility:private"], | ||
) | ||
|
||
native.cc_library( | ||
name = name, | ||
hdrs = [name + ".h"], | ||
srcs = [name + ".cc"], | ||
deps = [ | ||
generated_target, | ||
"//codegen/runtime:micro_codegen_context", | ||
"//tensorflow/lite/c:common", | ||
"//tensorflow/lite/c:c_api_types", | ||
"//tensorflow/lite/kernels/internal:compatibility", | ||
"//tensorflow/lite/micro/kernels:micro_ops", | ||
"//tensorflow/lite/micro:micro_common", | ||
"//tensorflow/lite/micro:micro_context", | ||
], | ||
copts = micro_copts(), | ||
visibility = visibility, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
load("//codegen:build_def.bzl", "tflm_inference_library") | ||
|
||
package(default_visibility = ["//visibility:public"]) | ||
|
||
tflm_inference_library( | ||
name = "hello_world_model", | ||
tflite_model = "//tensorflow/lite/micro/examples/hello_world/models:hello_world_int8.tflite", | ||
) | ||
|
||
cc_binary( | ||
name = "hello_world", | ||
srcs = ["hello_world.cc"], | ||
deps = [":hello_world_model"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
load("//tensorflow/lite/micro:build_def.bzl", "micro_copts") | ||
|
||
package(default_visibility = ["//visibility:public"]) | ||
|
||
cc_library( | ||
name = "micro_codegen_context", | ||
srcs = ["micro_codegen_context.cc"], | ||
hdrs = ["micro_codegen_context.h"], | ||
copts = micro_copts(), | ||
deps = [ | ||
"//tensorflow/lite/c:common", | ||
"//tensorflow/lite/kernels:op_macros", | ||
"//tensorflow/lite/kernels/internal:compatibility", | ||
"//tensorflow/lite/micro:micro_context", | ||
"//tensorflow/lite/micro:micro_graph", | ||
"//tensorflow/lite/micro:micro_log", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters