Description
Background: I am currently trying to build tflite-micro bindings for rust, as the previous ones are very out of date (2-3 years out of date!). I am pulling from this repo, and my build script (if you wanna look at it, it's rather expansive unfortunately) is here. A lot of the code was copied from the original tfmicro
crate, which is now out of date :(. The process looks something like this:
- I run this python file to build a source in a temp output in
target
to bind to. This is necessary because for some reason, tflite won't compile properly otherwise (I have friends who have this same issue when building for c/c++ directly). - I bind
libm
as a dependency, which has no issues. - I create bindings with
bindgen
, which succeeds yet produces compilation errors down the line. - I compile inline c++ for the library in
src
(currently not in use right now as I'm just trying to get bindings to work). - I compile the library itself successfully.
rustc
compiles my library, and errors out on the buggy bindings (see below).
I've tried compiling older versions, but always get the same error, so this leads me to believe that this is a bindgen
-based issue and not a tflite
one (as they have CI/CD that reports successful compilations/tests). I've also done a cursory search at past issues here and at the tensorflow repo, but nothing brought up is about my issue :((.
I'm sorry that this is such a large amount of code you're recieving, but I really appreciate your response (even if it is just "oh look you forgot a setting in the docs, its right here" -- at least that would get the job done!).
Here are the requested files below:
Input C/C++ Header
Header `__bindgen.cpp` file. Note that it links into the `target` directory, which is where the processed TFLM out put lives to compile from.
The entire header is as thus:
#include "/home/ianp/Projects/tflite-micro/target/debug/build/tflite_micro-0cda1779f845e9e1/out/tflite-micro/tensorflow/lite/micro/kernels/micro_ops.h"
#include "/home/ianp/Projects/tflite-micro/target/debug/build/tflite_micro-0cda1779f845e9e1/out/tflite-micro/tensorflow/lite/micro/micro_op_resolver.h"
#include "/home/ianp/Projects/tflite-micro/target/debug/build/tflite_micro-0cda1779f845e9e1/out/tflite-micro/tensorflow/lite/micro/tflite_bridge/micro_error_reporter.h"
#include "/home/ianp/Projects/tflite-micro/target/debug/build/tflite_micro-0cda1779f845e9e1/out/tflite-micro/tensorflow/lite/micro/micro_interpreter.h"
However, I was able to reduce it down to one line that errors out (by manually knocking out headers in the bindgen config):
#include "/home/ianp/Projects/tflite-micro/target/debug/build/tflite_micro-0cda1779f845e9e1/out/tflite-micro/tensorflow/lite/micro/micro_op_resolver.h"
__bindgen.ii
is not provided as of right now because it is so large that github lags when I type if I have it here! EDIT: I just realized I can use pastebin, but it goes over the maximum of 512 kilobytes.
Bindgen Invocation
Invocation
let bindings = bindgen_cross_builder()
// Configuration
.allowlist_recursively(true) // Allow types under types.
.prepend_enum_name(false) // Don't preprend the enum name to variants -- it's ugly!
.impl_debug(true) // Let us debug the types.
.with_codegen_config(CodegenConfig::TYPES) // TODO: figure out what this does.
.layout_tests(false)
.enable_cxx_namespaces()
.derive_copy(true)
.derive_default(true)
.derive_partialeq(true)
.derive_eq(true)
.size_t_is_usize(true)
.generate_inline_functions(true) // Generate inline functions.
.use_core() // Use core instead of std.
.ctypes_prefix("cty") // Use cty instead of std.
.default_non_copy_union_style(NonCopyUnionStyle::ManuallyDrop) // Fix issue where `ManuallyDrop` is not wrapping values.
.default_enum_style(EnumVariation::Rust {
non_exhaustive: false,
})
// Types
.allowlist_type("tflite::MicroErrorReporter")
.opaque_type("tflite::MicroErrorReporter")
.allowlist_type("tflite::Model")
.opaque_type("tflite::Model")
.allowlist_type("tflite::MicroInterpreter")
.opaque_type("tflite::MicroInterpreter")
.allowlist_type("tflite::ops::micro::AllOpsResolver")
.opaque_type("tflite::ops::micro::AllOpsResolver")
.allowlist_type("TfLiteTensor")
.allowlist_type("FrontendState")
.allowlist_type("FrontendConfig")
.allowlist_type("FrontendOutput")
// Types - blocklist
.blocklist_type("std")
.blocklist_type("tflite::Interpreter_TfLiteDelegatePtr")
.blocklist_type("tflite::Interpreter_State")
// Headers
.header(format!(
"{}/tensorflow/lite/micro/kernels/micro_ops.h",
tensorflow_location.to_string_lossy()
))
.header(format!(
"{}/tensorflow/lite/micro/micro_op_resolver.h",
tensorflow_location.to_string_lossy()
))
.header(format!(
"{}/tensorflow/lite/micro/tflite_bridge/micro_error_reporter.h",
tensorflow_location.to_string_lossy()
))
.header(format!(
"{}/tensorflow/lite/micro/micro_interpreter.h",
tensorflow_location.to_string_lossy()
))
// Inclusions
.clang_arg(format!(
"-include{}/tensorflow/lite/micro/micro_common.h",
tensorflow_location.to_string_lossy()
))
.clang_arg(format!("-I{}", tensorflow_location.to_string_lossy()))
.clang_arg(format!(
"-I{}/tensorflow",
tensorflow_location.to_string_lossy()
))
.clang_arg(format!(
"-I{}/third_party/flatbuffers/include",
tensorflow_location.to_string_lossy()
)) // -> flatbuffers/flatbuffers.h
.clang_arg(format!(
"-I{}/tensorflow/lite",
tensorflow_location.to_string_lossy()
))
.clang_arg(format!(
"-I{}/tensorflow/lite/micro",
tensorflow_location.to_string_lossy()
)) // -> micro/micro_common.h
.clang_arg(format!(
"-I{}/tensorflow/lite/c",
tensorflow_location.to_string_lossy()
)) // -> c/common.h
// Others
.clang_arg("-fretain-comments-from-system-headers") // Allow for parsing comments to create docs.
.clang_arg("-DGEMMLOWP_ALLOW_SLOW_SCALAR_FALLBACK")
.clang_arg("-xc++")
.clang_arg("-std=c++17"); // C++17 is required for flatbuffers
Invocation calls bindgen_cross_builder
, which is detailed in my repository in build/bind.rs
.
Actual Results
Output of `cargo build -vv`
<... previous (non-erroring) compilation of c/c++, several hundred (thousand?) lines...>
[tflite_micro 0.1.0] Building TFLM from source took 38.255181419s
Running `CARGO=/home/ianp/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/cargo CARGO_CRATE_NAME=tflite_micro CARGO_MANIFEST_DIR=/home/ianp/Projects/tflite-micro CARGO_PKG_AUTHORS='Ian Pratt <[email protected]>' CARGO_PKG_DESCRIPTION='' CARGO_PKG_HOMEPAGE='' CARGO_PKG_LICENSE=MIT CARGO_PKG_LICENSE_FILE='' CARGO_PKG_NAME=tflite_micro CARGO_PKG_README=README.md CARGO_PKG_REPOSITORY='' CARGO_PKG_RUST_VERSION='' CARGO_PKG_VERSION=0.1.0 CARGO_PKG_VERSION_MAJOR=0 CARGO_PKG_VERSION_MINOR=1 CARGO_PKG_VERSION_PATCH=0 CARGO_PKG_VERSION_PRE='' CARGO_PRIMARY_PACKAGE=1 LD_LIBRARY_PATH='/home/ianp/Projects/tflite-micro/target/debug/deps:/home/ianp/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib:/home/ianp/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib' OUT_DIR=/home/ianp/Projects/tflite-micro/target/debug/build/tflite_micro-0cda1779f845e9e1/out rustc --crate-name tflite_micro --edition=2021 src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=156 --crate-type lib --emit=dep-info,metadata,link -C embed-bitcode=no -C debuginfo=2 -C metadata=f9978d111d6d65bc -C extra-filename=-f9978d111d6d65bc --out-dir /home/ianp/Projects/tflite-micro/target/debug/deps -C incremental=/home/ianp/Projects/tflite-micro/target/debug/incremental -L dependency=/home/ianp/Projects/tflite-micro/target/debug/deps --extern cpp=/home/ianp/Projects/tflite-micro/target/debug/deps/libcpp-7980aaf4f17423df.rmeta --extern cty=/home/ianp/Projects/tflite-micro/target/debug/deps/libcty-8bcf37a817448e37.rmeta --extern log=/home/ianp/Projects/tflite-micro/target/debug/deps/liblog-286948fa84bc3263.rmeta --extern managed=/home/ianp/Projects/tflite-micro/target/debug/deps/libmanaged-cb0b4714d5a3e512.rmeta --extern ordered_float=/home/ianp/Projects/tflite-micro/target/debug/deps/libordered_float-f486fdd4e98f9127.rmeta -L native=/home/ianp/Projects/tflite-micro/target/debug/build/tflite_micro-0cda1779f845e9e1/out -L native=/home/ianp/Projects/tflite-micro/target/debug/build/tflite_micro-0cda1779f845e9e1/out -l m -l static=rust_cpp_generated -l static=tflm`
error[E0412]: cannot find type `_Iterator` in this scope
--> /home/ianp/Projects/tflite-micro/target/debug/build/tflite_micro-0cda1779f845e9e1/out/tflite_types.rs:2225:44
|
2225 | root::std::_Node_insert_return<_Iterator, _NodeHandle>;
| ^^^^^^^^^
|
::: /home/ianp/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/iter/traits/iterator.rs:74:1
|
74 | pub trait Iterator {
| ------------------ similarly named trait `Iterator` defined here
|
help: a trait with a similar name exists
|
2225 | root::std::_Node_insert_return<Iterator, _NodeHandle>;
| ~~~~~~~~
help: you might be missing a type parameter
|
2224 | pub type _Rb_tree_insert_return_type<_Iterator> =
| +++++++++++
error[E0412]: cannot find type `_NodeHandle` in this scope
--> /home/ianp/Projects/tflite-micro/target/debug/build/tflite_micro-0cda1779f845e9e1/out/tflite_types.rs:2225:55
|
2013 | pub struct _Node_handle<_NodeAlloc> {
| ----------------------------------- similarly named struct `_Node_handle` defined here
...
2225 | root::std::_Node_insert_return<_Iterator, _NodeHandle>;
| ^^^^^^^^^^^
|
help: a struct with a similar name exists
|
2225 | root::std::_Node_insert_return<_Iterator, _Node_handle>;
| ~~~~~~~~~~~~
help: you might be missing a type parameter
|
2224 | pub type _Rb_tree_insert_return_type<_NodeHandle> =
| +++++++++++++
error[E0740]: field must implement `Copy` or be wrapped in `ManuallyDrop<...>` to be used in a union
--> /home/ianp/Projects/tflite-micro/target/debug/build/tflite_micro-0cda1779f845e9e1/out/tflite_types.rs:1906:13
|
1906 | pub _M_val: _Tp,
| ^^^^^^^^^^^^^^^
|
= note: union fields must not have drop side-effects, which is currently enforced via either `Copy` or `ManuallyDrop<...>`
help: wrap the field type in `ManuallyDrop<...>`
|
1906 | pub _M_val: std::mem::ManuallyDrop<_Tp>,
| +++++++++++++++++++++++ +
Some errors have detailed explanations: E0412, E0740.
For more information about an error, try `rustc --explain E0412`.
error: could not compile `tflite_micro` (lib) due to 3 previous errors
Caused by:
process didn't exit successfully: `CARGO=/home/ianp/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/cargo CARGO_CRATE_NAME=tflite_micro CARGO_MANIFEST_DIR=/home/ianp/Projects/tflite-micro CARGO_PKG_AUTHORS='Ian Pratt <[email protected]>' CARGO_PKG_DESCRIPTION='' CARGO_PKG_HOMEPAGE='' CARGO_PKG_LICENSE=MIT CARGO_PKG_LICENSE_FILE='' CARGO_PKG_NAME=tflite_micro CARGO_PKG_README=README.md CARGO_PKG_REPOSITORY='' CARGO_PKG_RUST_VERSION='' CARGO_PKG_VERSION=0.1.0 CARGO_PKG_VERSION_MAJOR=0 CARGO_PKG_VERSION_MINOR=1 CARGO_PKG_VERSION_PATCH=0 CARGO_PKG_VERSION_PRE='' CARGO_PRIMARY_PACKAGE=1 LD_LIBRARY_PATH='/home/ianp/Projects/tflite-micro/target/debug/deps:/home/ianp/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib:/home/ianp/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib' OUT_DIR=/home/ianp/Projects/tflite-micro/target/debug/build/tflite_micro-0cda1779f845e9e1/out rustc --crate-name tflite_micro --edition=2021 src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=156 --crate-type lib --emit=dep-info,metadata,link -C embed-bitcode=no -C debuginfo=2 -C metadata=f9978d111d6d65bc -C extra-filename=-f9978d111d6d65bc --out-dir /home/ianp/Projects/tflite-micro/target/debug/deps -C incremental=/home/ianp/Projects/tflite-micro/target/debug/incremental -L dependency=/home/ianp/Projects/tflite-micro/target/debug/deps --extern cpp=/home/ianp/Projects/tflite-micro/target/debug/deps/libcpp-7980aaf4f17423df.rmeta --extern cty=/home/ianp/Projects/tflite-micro/target/debug/deps/libcty-8bcf37a817448e37.rmeta --extern log=/home/ianp/Projects/tflite-micro/target/debug/deps/liblog-286948fa84bc3263.rmeta --extern managed=/home/ianp/Projects/tflite-micro/target/debug/deps/libmanaged-cb0b4714d5a3e512.rmeta --extern ordered_float=/home/ianp/Projects/tflite-micro/target/debug/deps/libordered_float-f486fdd4e98f9127.rmeta -L native=/home/ianp/Projects/tflite-micro/target/debug/build/tflite_micro-0cda1779f845e9e1/out -L native=/home/ianp/Projects/tflite-micro/target/debug/build/tflite_micro-0cda1779f845e9e1/out -l m -l static=rust_cpp_generated -l static=tflm` (exit status: 1)
Because the generated types (even with reduced #include
s) are so big, I've linked the reduced version here as a pastebin. The lines of interest in the above (reduced) file are lines 1383, 1520 and 1701.
Expected Results
First, I would expect that _M_val
would be wrapped in std::mem::ManuallyDrop
, as I have set default_non_copy_union_style(NonCopyUnionStyle::ManuallyDrop)
. Yet, that union field is not wrapped in ManuallyDrop
!
I have no idea what is going on with _Rb_tree_insert_return_type
, but I'm also pretty sure that's another bindgen
error, as bindgen
seems to be getting confused about the types of some inputs and outputs, and forgetting to place generics and/or invoke native types somewhere.
Again, thanks for your time looking over my error. I appreciate both your efforts and this tool (bindgen
is kinda amazing tbh). I hope I've reduced the burden of searching for the bug somewhat, because that's a rather big library (I could probably have done more, but it's getting late where I am).