-
Notifications
You must be signed in to change notification settings - Fork 825
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create Make helpers for running codegen (#2222)
The codegen process is a multi-step process that requires compiling, executing code under simulation, and executing python scripts. To simplify this workflow, this commit adds Make helper functions for generating inference source code from a model and creating a binary with it. It also updates the hello world example to use these helpers and adds an update script for keeping the checked in generated source in sync. BUG=cleanup
- Loading branch information
Showing
9 changed files
with
153 additions
and
48 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 |
---|---|---|
@@ -1,14 +1,10 @@ | ||
# TODO(rjascani): The codegen runtime files (ie, in runtime subdir) should be a | ||
# separate library. | ||
CODEGEN_HELLO_WORLD_SRCS := \ | ||
$(TENSORFLOW_ROOT)codegen/examples/hello_world/hello_world.cc \ | ||
$(TENSORFLOW_ROOT)codegen/examples/hello_world/hello_world_model.cc \ | ||
$(TENSORFLOW_ROOT)codegen/runtime/micro_codegen_context.cc | ||
CODEGEN_HELLO_WORLD_MODEL := \ | ||
$(TENSORFLOW_ROOT)tensorflow/lite/micro/examples/hello_world/models/hello_world_int8.tflite | ||
|
||
CODEGEN_HELLO_WORLD_HDRS := \ | ||
$(TENSORFLOW_ROOT)codegen/examples/hello_world/hello_world_model.h \ | ||
$(TENSORFLOW_ROOT)codegen/runtime/micro_codegen_context.h | ||
CODEGEN_HELLO_WORLD_SRCS := \ | ||
$(TENSORFLOW_ROOT)codegen/examples/hello_world/hello_world.cc | ||
|
||
# Builds a standalone binary. | ||
$(eval $(call microlite_test,codegen_hello_world,\ | ||
$(CODEGEN_HELLO_WORLD_SRCS),,)) | ||
$(eval $(call codegen_model_binary,codegen_hello_world,hello_world_model,\ | ||
$(CODEGEN_HELLO_WORLD_MODEL),$(CODEGEN_HELLO_WORLD_SRCS),,)) | ||
|
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 |
---|---|---|
@@ -1,51 +1,28 @@ | ||
# Codegen Hello World Example | ||
|
||
This is a code-generated example of the hello world model. The process is | ||
currently somewhat involved: | ||
This is a code-generated example of the hello world model. The generated source | ||
is checked in for now so that it can be reviewed during the prototyping stage. | ||
|
||
## Build the preprocessor for your target | ||
## Building the example executable | ||
Please note that this will execute Bazel from make as part of the process. | ||
|
||
This creates a target-specific preprocessor binary capable of performing the | ||
init and prepare stages of the Interpreter and serializing the output. This | ||
binary can be re-used for multiple models. | ||
|
||
### x86 | ||
``` | ||
make -f tensorflow/lite/micro/tools/make/Makefile codegen_preprocessor | ||
make -f tensorflow/lite/micro/tools/make/Makefile codegen_hello_world | ||
``` | ||
|
||
## Run the preprocessor | ||
## Running the example | ||
|
||
The preprocessor will take the provided model, create a TFLM Interpreter, and | ||
allocate tensors. It will then capture and serialize the resulting data | ||
structures needed for inference. For embedded targets, this should be run under | ||
simulation. | ||
TODO(rjascani): The command works, but it'll just crash as we don't have all of | ||
the data structures fully populated yet. | ||
|
||
### x86 | ||
``` | ||
./gen/linux_x86_64_default/bin/codegen_preprocessor \ | ||
$(pwd)/tensorflow/lite/micro/examples/hello_world/models/hello_world_int8.tflite \ | ||
$(pwd)/gen/linux_86_64_default/genfiles/hello_world_int8.ppd | ||
make -f tensorflow/lite/micro/tools/make/Makefile run_codegen_hello_world | ||
``` | ||
|
||
## Generate the inference code | ||
## Updating the generated sources | ||
To update the generated source, you can execute this make target: | ||
|
||
To generate the inference code at `codegen/example/hello_world_model.h/.cc`: | ||
|
||
### x86 | ||
``` | ||
bazel run codegen:code_generator -- \ | ||
--model $(pwd)/tensorflow/lite/micro/examples/hello_world/models/hello_world_int8.tflite \ | ||
--preprocessed_data $(pwd)/gen/linux_86_64_default/genfiles/hello_world_int8.ppd \ | ||
--output_dir $(pwd)/codegen/examples/hello_world \ | ||
--output_name hello_world_model | ||
./codegen/examples/hello_world/update_example_source.sh | ||
``` | ||
|
||
## Compile the generated inference code | ||
|
||
To compile the generated source, you can use the Makefile: | ||
|
||
### x86 | ||
``` | ||
make -f tensorflow/lite/micro/tools/make/Makefile codegen_hello_world | ||
``` |
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,29 @@ | ||
#!/usr/bin/env bash | ||
# Copyright 2023 The TensorFlow Authors. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# ============================================================================== | ||
|
||
# | ||
# Syncs the generated example source code in the repository. | ||
# | ||
|
||
set -e | ||
|
||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | ||
ROOT_DIR=${SCRIPT_DIR}/../../.. | ||
cd "${ROOT_DIR}" | ||
|
||
make -j8 -f tensorflow/lite/micro/tools/make/Makefile codegen_hello_world | ||
cp ./gen/linux_x86_64_default/genfiles/hello_world_model.h ${SCRIPT_DIR} | ||
cp ./gen/linux_x86_64_default/genfiles/hello_world_model.cc ${SCRIPT_DIR} |
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
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