Skip to content

Commit feb2669

Browse files
authored
Add hello world codegen example (#2163)
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
1 parent 901d830 commit feb2669

File tree

8 files changed

+105
-2
lines changed

8 files changed

+105
-2
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CODEGEN_HELLO_WORLD_SRCS := \
2+
$(TENSORFLOW_ROOT)codegen/examples/hello_world/hello_world.cc \
3+
$(TENSORFLOW_ROOT)codegen/examples/hello_world/hello_world_model.cc
4+
5+
CODEGEN_HELLO_WORLD_HDRS := \
6+
$(TENSORFLOW_ROOT)codegen/examples/hello_world/hello_world_model.h
7+
8+
# Builds a standalone binary.
9+
$(eval $(call microlite_test,codegen_hello_world,\
10+
$(CODEGEN_HELLO_WORLD_SRCS),,))
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Codegen Hello World Example
2+
3+
This is a code-generated example of the hello world model.
4+
5+
To generate the inference code at `codegen/example/hello_world_model.h/.cc`:
6+
7+
```
8+
bazel run codegen:code_generator -- \
9+
--model $(pwd)/tensorflow/lite/micro/examples/hello_world/models/hello_world_int8.tflite \
10+
--output_dir $(pwd)/codegen/examples/hello_world \
11+
--output_name hello_world_model
12+
```
13+
14+
To compile the generated source, you can use the Makefile:
15+
16+
```
17+
make -f tensorflow/lite/micro/tools/make/Makefile codegen_hello_world
18+
```
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* Copyright 2023 The TensorFlow Authors. All Rights Reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
==============================================================================*/
15+
16+
#include "hello_world_model.h"
17+
18+
int main(int argc, char** argv) {
19+
hello_world_model::Invoke();
20+
21+
return 0;
22+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* Copyright 2023 The TensorFlow Authors. All Rights Reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
==============================================================================*/
15+
16+
/* AUTOMATICALLY GENERATED DO NOT MODIFY */
17+
18+
#include "hello_world_model.h"
19+
20+
namespace hello_world_model {
21+
22+
void Invoke() {}
23+
24+
} // namespace hello_world_model
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* Copyright 2023 The TensorFlow Authors. All Rights Reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
==============================================================================*/
15+
16+
/* AUTOMATICALLY GENERATED DO NOT MODIFY */
17+
18+
#pragma once
19+
20+
namespace hello_world_model {
21+
22+
void Invoke();
23+
24+
} // namespace hello_world_model

codegen/templates/inference.cc.mako

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ namespace ${model_name} {
2121

2222
void Invoke() {}
2323

24-
} // ${model_name}
24+
} // namespace ${model_name}

codegen/templates/inference.h.mako

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ namespace ${model_name} {
2121

2222
void Invoke();
2323

24-
} // ${model_name}
24+
} // namespace ${model_name}

tensorflow/lite/micro/tools/make/Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,8 @@ MICRO_LITE_BENCHMARKS := $(wildcard $(TENSORFLOW_ROOT)tensorflow/lite/micro/tool
287287
MICROLITE_BENCHMARK_SRCS := \
288288
$(wildcard $(TENSORFLOW_ROOT)tensorflow/lite/micro/tools/benchmarking/*benchmark.cc)
289289

290+
MICRO_LITE_CODEGEN_EXAMPLES := $(shell find $(TENSORFLOW_ROOT)codegen/examples/ -name Makefile.inc)
291+
290292
MICROLITE_TEST_SRCS := \
291293
$(TENSORFLOW_ROOT)tensorflow/lite/micro/fake_micro_context_test.cc \
292294
$(TENSORFLOW_ROOT)tensorflow/lite/micro/flatbuffer_utils_test.cc \
@@ -676,6 +678,9 @@ INCLUDES += -I$(GENERATED_SRCS_DIR)$(TENSORFLOW_ROOT)
676678
# Load the examples.
677679
include $(MICRO_LITE_EXAMPLE_TESTS)
678680

681+
# Load codegen examples.
682+
include $(MICRO_LITE_CODEGEN_EXAMPLES)
683+
679684
# Load the integration tests.
680685
include $(MICRO_LITE_INTEGRATION_TESTS)
681686

0 commit comments

Comments
 (0)