-
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.
This PR creates the initial scaffolding for the codegen preprocessor. The preprocessor is a target-specific binary that will take a model, load it into a TFLM Interpreter, perform the Init & Prepare stages, then serialize the resulting data structures to an output file. Currently, all this binary does is load the model file and write an output file that simply contains the source model path in it. This will be expanded as we expose the data. BUG=b/295076056
- Loading branch information
Showing
6 changed files
with
180 additions
and
3 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
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 |
---|---|---|
@@ -1,18 +1,51 @@ | ||
# Codegen Hello World Example | ||
|
||
This is a code-generated example of the hello world model. | ||
This is a code-generated example of the hello world model. The process is | ||
currently somewhat involved: | ||
|
||
## Build the preprocessor for your target | ||
|
||
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 | ||
``` | ||
|
||
## Run the preprocessor | ||
|
||
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. | ||
|
||
### 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 | ||
``` | ||
|
||
## Generate the inference code | ||
|
||
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 | ||
``` | ||
|
||
To compile the generated source, you can use the Makefile: | ||
## 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,17 @@ | ||
# 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. | ||
# ============================================================================== | ||
|
||
CODEGEN_PREPROCESSOR_SRCS := \ | ||
$(TENSORFLOW_ROOT)codegen/preprocessor/main.cc |
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,87 @@ | ||
/* 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. | ||
==============================================================================*/ | ||
|
||
#include <cstdlib> | ||
#include <fstream> | ||
#include <iostream> | ||
#include <memory> | ||
|
||
#include "codegen/preprocessor/preprocessor_schema_generated.h" | ||
#include "flatbuffers/flatbuffers.h" | ||
#include "tensorflow/lite/schema/schema_generated.h" | ||
|
||
namespace { | ||
|
||
std::unique_ptr<char[]> ReadModelFile(const char* model_file_name) { | ||
std::ifstream model_file(model_file_name, std::ios::binary); | ||
if (!model_file.is_open()) { | ||
std::cerr << "codegen_preprocessor: could not open model file: " | ||
<< model_file_name << std::endl; | ||
return nullptr; | ||
} | ||
|
||
model_file.seekg(0, std::ios::end); | ||
size_t num_bytes = model_file.tellg(); | ||
std::unique_ptr<char[]> model_data(new char[num_bytes]); | ||
model_file.read(model_data.get(), num_bytes); | ||
|
||
return model_data; | ||
} | ||
|
||
int WriteOutputFile(const char* output_file_name, | ||
flatbuffers::span<uint8_t> output) { | ||
std::ofstream output_file(output_file_name, std::ios::trunc); | ||
if (!output_file.is_open()) { | ||
std::cerr << "codegen_preprocessor: could not open output file: " | ||
<< output_file_name << std::endl; | ||
return EXIT_FAILURE; | ||
} | ||
|
||
output_file.write(reinterpret_cast<char*>(output.data()), output.size()); | ||
return 0; | ||
} | ||
|
||
} // namespace | ||
|
||
int main(int argc, char* argv[]) { | ||
if (argc < 2) { | ||
std::cerr << "codegen_preprocessor: invalid usage!" << std::endl; | ||
std::cerr << "usage: codegen_preprocessor <tflite_model> <output_file>" | ||
<< std::endl; | ||
return EXIT_FAILURE; | ||
} | ||
|
||
const char* model_file_name = argv[1]; | ||
const char* output_file_name = argv[2]; | ||
|
||
const auto model_data = ReadModelFile(model_file_name); | ||
if (!model_data) { | ||
return EXIT_FAILURE; | ||
} | ||
|
||
// We have to create our own allocator, as the typical TFLM runtime disables | ||
// its use (to avoid dynamic allocation). | ||
flatbuffers::DefaultAllocator allocator; | ||
flatbuffers::FlatBufferBuilder builder{2048, &allocator}; | ||
const auto input_model_path = builder.CreateString(model_file_name); | ||
|
||
// Do the preprocess work. | ||
|
||
tflm::codegen::preprocessor::DataBuilder data_builder(builder); | ||
data_builder.add_input_model_path(input_model_path); | ||
builder.Finish(data_builder.Finish()); | ||
|
||
return WriteOutputFile(output_file_name, builder.GetBufferSpan()); | ||
} |
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