Skip to content

Commit

Permalink
Add hello world codegen example (#2163)
Browse files Browse the repository at this point in the history
This PR adds a codegen inference example for the hello world model to demonstrate how to invoke the code generator and build the generated source. For now, we're just checking the generated source into the repo to skip over building out the make rules and also ensuring the generated source complies with formatting rules.

This also fixes a minor formatting issue in the source templates, as clang-format now properly complained about it.

BUG=b/295390000
  • Loading branch information
rascani authored Aug 11, 2023
1 parent 901d830 commit feb2669
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 2 deletions.
10 changes: 10 additions & 0 deletions codegen/examples/hello_world/Makefile.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CODEGEN_HELLO_WORLD_SRCS := \
$(TENSORFLOW_ROOT)codegen/examples/hello_world/hello_world.cc \
$(TENSORFLOW_ROOT)codegen/examples/hello_world/hello_world_model.cc

CODEGEN_HELLO_WORLD_HDRS := \
$(TENSORFLOW_ROOT)codegen/examples/hello_world/hello_world_model.h

# Builds a standalone binary.
$(eval $(call microlite_test,codegen_hello_world,\
$(CODEGEN_HELLO_WORLD_SRCS),,))
18 changes: 18 additions & 0 deletions codegen/examples/hello_world/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Codegen Hello World Example

This is a code-generated example of the hello world model.

To generate the inference code at `codegen/example/hello_world_model.h/.cc`:

```
bazel run codegen:code_generator -- \
--model $(pwd)/tensorflow/lite/micro/examples/hello_world/models/hello_world_int8.tflite \
--output_dir $(pwd)/codegen/examples/hello_world \
--output_name hello_world_model
```

To compile the generated source, you can use the Makefile:

```
make -f tensorflow/lite/micro/tools/make/Makefile codegen_hello_world
```
22 changes: 22 additions & 0 deletions codegen/examples/hello_world/hello_world.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* 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 "hello_world_model.h"

int main(int argc, char** argv) {
hello_world_model::Invoke();

return 0;
}
24 changes: 24 additions & 0 deletions codegen/examples/hello_world/hello_world_model.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* 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.
==============================================================================*/

/* AUTOMATICALLY GENERATED DO NOT MODIFY */

#include "hello_world_model.h"

namespace hello_world_model {

void Invoke() {}

} // namespace hello_world_model
24 changes: 24 additions & 0 deletions codegen/examples/hello_world/hello_world_model.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* 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.
==============================================================================*/

/* AUTOMATICALLY GENERATED DO NOT MODIFY */

#pragma once

namespace hello_world_model {

void Invoke();

} // namespace hello_world_model
2 changes: 1 addition & 1 deletion codegen/templates/inference.cc.mako
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ namespace ${model_name} {

void Invoke() {}

} // ${model_name}
} // namespace ${model_name}
2 changes: 1 addition & 1 deletion codegen/templates/inference.h.mako
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ namespace ${model_name} {

void Invoke();

} // ${model_name}
} // namespace ${model_name}
5 changes: 5 additions & 0 deletions tensorflow/lite/micro/tools/make/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,8 @@ MICRO_LITE_BENCHMARKS := $(wildcard $(TENSORFLOW_ROOT)tensorflow/lite/micro/tool
MICROLITE_BENCHMARK_SRCS := \
$(wildcard $(TENSORFLOW_ROOT)tensorflow/lite/micro/tools/benchmarking/*benchmark.cc)

MICRO_LITE_CODEGEN_EXAMPLES := $(shell find $(TENSORFLOW_ROOT)codegen/examples/ -name Makefile.inc)

MICROLITE_TEST_SRCS := \
$(TENSORFLOW_ROOT)tensorflow/lite/micro/fake_micro_context_test.cc \
$(TENSORFLOW_ROOT)tensorflow/lite/micro/flatbuffer_utils_test.cc \
Expand Down Expand Up @@ -676,6 +678,9 @@ INCLUDES += -I$(GENERATED_SRCS_DIR)$(TENSORFLOW_ROOT)
# Load the examples.
include $(MICRO_LITE_EXAMPLE_TESTS)

# Load codegen examples.
include $(MICRO_LITE_CODEGEN_EXAMPLES)

# Load the integration tests.
include $(MICRO_LITE_INTEGRATION_TESTS)

Expand Down

0 comments on commit feb2669

Please sign in to comment.