Skip to content

Building TF.Lite benchmark tool with TF.text ops support #50924

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
thisisjoshstone opened this issue Jul 23, 2021 · 9 comments
Closed

Building TF.Lite benchmark tool with TF.text ops support #50924

thisisjoshstone opened this issue Jul 23, 2021 · 9 comments
Assignees
Labels
comp:lite TF Lite related issues stat:awaiting tensorflower Status - Awaiting response from tensorflower subtype: ubuntu/linux Ubuntu/Linux Build/Installation Issues type:build/install Build and install issues

Comments

@thisisjoshstone
Copy link

thisisjoshstone commented Jul 23, 2021

Does anyone have an example of re-building a TF.Lite binary with support for tensorflow-text operations?

I have a TF.Lite model which includes USE-multilingual, which uses several Sentencepiece ops from tensorflow-text. After reading tensorflow/hub#463 and a few others, I was able to successfully covert the model, and it returns results during inference as expected when using the TF.Lite Interpreter.

I'd like to run the TF.Lite native benchmark tool against this model as well, but doing so requires building the tool from source with tensorflow-text ops. Thus far, I haven't been able to build a working version of the tool for my model. A few things I've tried are listed below, any help would be much appreciated. Thanks!

Attempt 1: Include tensorflow-text as an external repository and target tensorflow_text:ops_lib

My first attempt was to build tensorflow_text:ops_lib directly into the tool from source, which seems to be the more Bazel-appropriate way to tackle the problem. The diff below shows my most recent attempt to build the benchmark tool in this manner.

Diff

diff --git a/tensorflow/BUILD b/tensorflow/BUILD
index 3ef74d742ef..c653a384c69 100644
--- a/tensorflow/BUILD
+++ b/tensorflow/BUILD
@@ -38,7 +38,7 @@ load(
 load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
 
 package(
-    default_visibility = [":internal"],
+    default_visibility = ["//visibility:public"],
     licenses = ["notice"],  # Apache 2.0
 )
 
diff --git a/tensorflow/lite/c/BUILD b/tensorflow/lite/c/BUILD
index 3119cf30557..3b7ff060518 100644
--- a/tensorflow/lite/c/BUILD
+++ b/tensorflow/lite/c/BUILD
@@ -38,6 +38,8 @@ tflite_cc_shared_object(
         ":c_api_experimental",
         ":exported_symbols.lds",
         ":version_script.lds",
+        "//tensorflow/lite/delegates/flex:delegate",
+        "@org_tensorflow_text//tensorflow_text:ops_lib",
     ],
 )
 
diff --git a/tensorflow/lite/tools/benchmark/BUILD b/tensorflow/lite/tools/benchmark/BUILD
index 815efb776e9..0793db4a417 100644
--- a/tensorflow/lite/tools/benchmark/BUILD
+++ b/tensorflow/lite/tools/benchmark/BUILD
@@ -24,9 +24,12 @@ cc_library(
     deps = [
         ":benchmark_tflite_model_lib",
         "//tensorflow/lite/tools:logging",
+        "//tensorflow/lite/delegates/flex:delegate",
+        "@org_tensorflow_text//tensorflow_text:ops_lib",
     ],
 )
 
+
 cc_binary(
     name = "benchmark_model",
     copts = common_copts,
diff --git a/tensorflow/workspace0.bzl b/tensorflow/workspace0.bzl
index 22374bc1297..f8483aa2f53 100644
--- a/tensorflow/workspace0.bzl
+++ b/tensorflow/workspace0.bzl
@@ -102,6 +102,38 @@ def workspace():
         ],
     )
 
+    # TF.text repo
+    http_archive(
+        name = "org_tensorflow_text",
+        sha256 = "d82856dc04c04bbce347f72d0ad7df59bb6b26b7030f96f25f036cfe8a138312",
+        strip_prefix = "text-2.5.0",
+        urls = [
+            "https://github.com/tensorflow/text/archive/v2.5.0.zip",
+        ],
+        patches = ["@//third_party/tf_text:tftext10.patch"],
+        patch_args = ["-p1"],
+    )
+
+    # TF.text dependencies
+    http_archive(
+        name = "com_google_sentencepiece",
+        strip_prefix = "sentencepiece-1.0.0",
+        sha256 = "c05901f30a1d0ed64cbcf40eba08e48894e1b0e985777217b7c9036cac631346",
+        urls = [
+            "https://github.com/google/sentencepiece/archive/1.0.0.zip",
+        ],
+    )
+
+    http_archive(
+        name = "com_google_glog",
+        sha256 = "1ee310e5d0a19b9d584a855000434bb724aa744745d5b8ab1855c85bff8a8e21",
+        strip_prefix = "glog-028d37889a1e80e8a07da1b8945ac706259e5fd8",
+        urls = [
+            "https://mirror.bazel.build/github.com/google/glog/archive/028d37889a1e80e8a07da1b8945ac706259e5fd8.tar.gz",
+            "https://github.com/google/glog/archive/028d37889a1e80e8a07da1b8945ac706259e5fd8.tar.gz",
+        ],
+    )
+
     bazel_toolchains_repositories()
 
     # Use `swift_rules_dependencies` to fetch the toolchains. With the
@@ -117,6 +149,7 @@ def workspace():
     grpc_extra_deps()
     config_googleapis()
 
+
 # Alias so it can be loaded without assigning to a different symbol to prevent
 # shadowing previous loads and trigger a buildifier warning.
 tf_workspace0 = workspace
diff --git a/third_party/eigen3/BUILD b/third_party/eigen3/BUILD
index e2253e18f8a..c0802240b4b 100644
--- a/third_party/eigen3/BUILD
+++ b/third_party/eigen3/BUILD
@@ -47,7 +47,7 @@ filegroup(
         ["**/*"],
         exclude = ["**/OWNERS"],
     ),
-    visibility = ["//tensorflow:__subpackages__"],
+    visibility = ["//visibility:public"],
 )
 
 filegroup(
@@ -74,4 +74,5 @@ genrule(
     done
     """,
     tags = ["manual"],
+    visibility = ["//visibility:public"],
 )
diff --git a/third_party/icu/BUILD.bazel b/third_party/icu/BUILD.bazel
index 14cadffc841..691585ce5ba 100644
--- a/third_party/icu/BUILD.bazel
+++ b/third_party/icu/BUILD.bazel
@@ -19,6 +19,16 @@ cc_library(
     ],
 )
 
+alias(
+    name = "nfkc",
+    actual = ":common",
+)
+
+alias(
+    name = "nfkc_cf",
+    actual = ":common",
+)
+
 cc_library(
     name = "common",
     hdrs = glob(["icu4c/source/common/unicode/*.h"]),
diff --git a/third_party/tf_text/tftext10.patch b/third_party/tf_text/tftext10.patch
new file mode 100644
index 00000000000..cd313cd3ad3
--- /dev/null
+++ b/third_party/tf_text/tftext10.patch
@@ -0,0 +1,29 @@
+diff --git a/tensorflow_text/tftext.bzl b/tensorflow_text/tftext.bzl
+index 7f703b3..b840c0f 100644
+--- a/tensorflow_text/tftext.bzl
++++ b/tensorflow_text/tftext.bzl
+@@ -93,8 +93,8 @@ def tf_deps(deps = []):
+             "@org_tensorflow//tensorflow/core:portable_tensorflow_lib_lite",
+         ],
+         "//conditions:default": [
+-            "@local_config_tf//:libtensorflow_framework",
+-            "@local_config_tf//:tf_header_lib",
++            "@org_tensorflow//tensorflow:libtensorflow_framework_import_lib",
++            #"@org_tensorflow//tensorflow/c:headers",
+         ] + deps + oss_deps,
+     })
+ 
+diff --git a/third_party/sentencepiece/processor.patch b/third_party/sentencepiece/processor.patch
+index 5fa1b84..c5a7358 100644
+--- a/third_party/sentencepiece/processor.patch
++++ b/third_party/sentencepiece/processor.patch
+@@ -22,8 +22,7 @@ index b4298d2..7ce779f 100644
+ +              "@org_tensorflow//tensorflow/core:tflite_portable_logging",
+ +            ],
+ +            "//conditions:default": [
+-+              "@local_config_tf//:libtensorflow_framework",
+-+              "@local_config_tf//:tf_header_lib",
+++              "@org_tensorflow//tensorflow:libtensorflow_framework_import_lib",
+ +              "@com_google_absl//absl/functional:function_ref",
+ +              "@com_google_absl//absl/strings:cord",
+ +            ],

This build fails with a number of different dependency path/access errors, depending on Bazel build order. Typically, it either can't find an Eigen header or a Protobuf header when starting from tensorflow-text source (this is what led me to attempt changing some of the rule visibilities, but with no luck). I've included a couple of these error messages for reference.

Example 1

ERROR: /home/ubuntu/.cache/bazel/bazel_ubuntu/b813f517b143a3c1665dd902035fe00f/external/org_tensorflow_text/tensorflow_text/core/kernels/BUILD:355:23: C++ compilation of rule '@org_tensorflow_text//tensorflow_text/core/kernels:sentencepiece_kernels' failed (Exit 1): gcc failed: error executing command
(cd /home/ubuntu/.cache/bazel/bazel_ubuntu/b813f517b143a3c1665dd902035fe00f/execroot/org_tensorflow &&
exec env -
CUDA_TOOLKIT_PATH=/usr/local/cuda-10.0
LD_LIBRARY_PATH=/usr/local/cuda/lib:/usr/local/cuda/lib64:/usr/local/cuda/extras/CUPTI/lib64:/opt/amazon/efa/lib:/opt/amazon/openmpi/lib:/usr/local/lib:/usr/lib:
PATH=/home/ubuntu/.cache/bazelisk/downloads/bazelbuild/bazel-3.7.2-linux-x86_64/bin:/usr/local/cuda:/home/ubuntu/src/tensorflow/.venv/bin:/opt/amazon/openmpi/bin/:/opt/amazon/efa/bin/:/home/ubuntu/anaconda3/condabin:/home/ubuntu/.dl_binaries/bin:/usr/local/cuda/bin:/opt/aws/neuron/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
PWD=/proc/self/cwd
PYTHON_BIN_PATH=/home/ubuntu/src/tensorflow/.venv/bin/python3
PYTHON_LIB_PATH=/home/ubuntu/src/tensorflow/.venv/lib/python3.6/site-packages
TF2_BEHAVIOR=1
/usr/bin/gcc -U_FORTIFY_SOURCE -fstack-protector -Wall -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-pointer -g0 -O2 '-D_FORTIFY_SOURCE=1' -DNDEBUG -ffunction-sections -fdata-sections '-std=c++0x' -MD -MF bazel-out/k8-opt/bin/external/org_tensorflow_text/tensorflow_text/core/kernels/objs/sentencepiece_kernels/sentencepiece_kernels.d '-frandom-seed=bazel-out/k8-opt/bin/external/org_tensorflow_text/tensorflow_text/core/kernels/objs/sentencepiece_kernels/sentencepiece_kernels.o' -iquote external/org_tensorflow_text -iquote bazel-out/k8-opt/bin/external/org_tensorflow_text -iquote external/com_google_absl -iquote bazel-out/k8-opt/bin/external/com_google_absl -iquote external/com_google_sentencepiece -iquote bazel-out/k8-opt/bin/external/com_google_sentencepiece -iquote external/com_google_protobuf -iquote bazel-out/k8-opt/bin/external/com_google_protobuf -iquote external/zlib -iquote bazel-out/k8-opt/bin/external/zlib -iquote external/com_github_gflags_gflags -iquote bazel-out/k8-opt/bin/external/com_github_gflags_gflags -iquote external/com_google_glog -iquote bazel-out/k8-opt/bin/external/com_google_glog -iquote external/com_google_googletest -iquote bazel-out/k8-opt/bin/external/com_google_googletest -iquote . -iquote bazel-out/k8-opt/bin -Ibazel-out/k8-opt/bin/external/com_google_glog/virtual_includes/glog -isystem external/com_google_protobuf/src -isystem bazel-out/k8-opt/bin/external/com_google_protobuf/src -isystem external/zlib -isystem bazel-out/k8-opt/bin/external/zlib -isystem external/com_github_gflags_gflags/include -isystem bazel-out/k8-opt/bin/external/com_github_gflags_gflags/include -isystem external/com_google_googletest/googlemock -isystem bazel-out/k8-opt/bin/external/com_google_googletest/googlemock -isystem external/com_google_googletest/googlemock/include -isystem bazel-out/k8-opt/bin/external/com_google_googletest/googlemock/include -isystem external/com_google_googletest/googletest -isystem bazel-out/k8-opt/bin/external/com_google_googletest/googletest -isystem external/com_google_googletest/googletest/include -isystem bazel-out/k8-opt/bin/external/com_google_googletest/googletest/include -w -DAUTOLOAD_DYNAMIC_KERNELS '-std=c++14' -fno-canonical-system-headers -Wno-builtin-macro-redefined '-D__DATE="redacted"' '-D__TIMESTAMP
="redacted"' '-D__TIME
_="redacted"' -c external/org_tensorflow_text/tensorflow_text/core/kernels/sentencepiece_kernels.cc -o bazel-out/k8-opt/bin/external/org_tensorflow_text/tensorflow_text/core/kernels/_objs/sentencepiece_kernels/sentencepiece_kernels.o)
Execution platform: @local_execution_config_platform//:platform
In file included from ./tensorflow/core/framework/bounds_check.h:21,
from external/org_tensorflow_text/tensorflow_text/core/kernels/sentencepiece_kernels.cc:25:
./third_party/eigen3/Eigen/Core:1:10: fatal error: Eigen/Core: No such file or directory
#include "Eigen/Core"
^~~~~~~~~~~~
compilation terminated.

Example 2

ERROR: /home/ubuntu/.cache/bazel/bazel_ubuntu/b813f517b143a3c1665dd902035fe00f/external/org_tensorflow_text/tensorflow_text/BUILD:1162:19: C++ compilation of rule '@org_tensorflow_text//tensorflow_text:wordpiece_tokenizer_cc' failed (Exit 1): gcc failed: error executing command
(cd /home/ubuntu/.cache/bazel/bazel_ubuntu/b813f517b143a3c1665dd902035fe00f/execroot/org_tensorflow &&
exec env -
LD_LIBRARY_PATH=/usr/local/cuda/lib:/usr/local/cuda/lib64:/usr/local/cuda/extras/CUPTI/lib64:/opt/amazon/efa/lib:/opt/amazon/openmpi/lib:/usr/local/lib:/usr/lib:
PATH=/home/ubuntu/.cache/bazelisk/downloads/bazelbuild/bazel-3.7.2-linux-x86_64/bin:/home/ubuntu/src/tensorflow/.venv/bin:/opt/amazon/openmpi/bin/:/opt/amazon/efa/bin/:/home/ubuntu/anaconda3/condabin:/home/ubuntu/.dl_binaries/bin:/usr/local/cuda/bin:/opt/aws/neuron/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
PWD=/proc/self/cwd
PYTHON_BIN_PATH=/home/ubuntu/src/tensorflow/.venv/bin/python
PYTHON_LIB_PATH=/home/ubuntu/src/tensorflow/.venv/lib/python3.6/site-packages
TF2_BEHAVIOR=1
/usr/bin/gcc -U_FORTIFY_SOURCE -fstack-protector -Wall -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-pointer -g0 -O2 '-D_FORTIFY_SOURCE=1' -DNDEBUG -ffunction-sections -fdata-sections '-std=c++0x' -MD -MF bazel-out/k8-opt/bin/external/org_tensorflow_text/tensorflow_text/objs/wordpiece_tokenizer_cc/wordpiece_op.pic.d '-frandom-seed=bazel-out/k8-opt/bin/external/org_tensorflow_text/tensorflow_text/objs/wordpiece_tokenizer_cc/wordpiece_op.pic.o' -fPIC -iquote external/org_tensorflow_text -iquote bazel-out/k8-opt/bin/external/org_tensorflow_text -iquote external/icu -iquote bazel-out/k8-opt/bin/external/icu -iquote . -iquote bazel-out/k8-opt/bin -iquote external/com_google_absl -iquote bazel-out/k8-opt/bin/external/com_google_absl -isystem external/icu/icu4c/source/common -isystem bazel-out/k8-opt/bin/external/icu/icu4c/source/common -w -DAUTOLOAD_DYNAMIC_KERNELS '-std=c++14' -pthread -fno-canonical-system-headers -Wno-builtin-macro-redefined '-D__DATE="redacted"' '-D__TIMESTAMP
="redacted"' '-D__TIME__="redacted"' -c external/org_tensorflow_text/tensorflow_text/core/ops/wordpiece_op.cc -o bazel-out/k8-opt/bin/external/org_tensorflow_text/tensorflow_text/_objs/wordpiece_tokenizer_cc/wordpiece_op.pic.o)
Execution platform: @local_execution_config_platform//:platform
In file included from ./tensorflow/core/framework/op_def_builder.h:24,
from ./tensorflow/core/framework/op.h:23,
from external/org_tensorflow_text/tensorflow_text/core/ops/wordpiece_op.cc:15:
bazel-out/k8-opt/bin/tensorflow/core/framework/op_def.pb.h:10:10: fatal error: google/protobuf/port_def.inc: No such file or directory
#include <google/protobuf/port_def.inc>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.

Attempt 2: Compile tensorflow-text ops separately and link library

I was able to compile tensorflow-text with linkstatic=1 and linkshared=True, hoping this would give me the best chance at a usable library (based on the linkshared description in the Bazel cc_binary docs). However, when I try to link this in tensorflow/lite/tools/benchmark:benchmark_model, the build completes, but gives the following runtime error:

ubuntu@ip-10-22-3-51:~/src/tensorflow$ bazel-bin/tensorflow/lite/tools/benchmark/benchmark_model --help
bazel-bin/tensorflow/lite/tools/benchmark/benchmark_model: error while loading shared libraries: libtensorflow_framework.so.2: cannot open shared object file: No such file or directory

When I try to link that library as well (which doesn't make much sense to include in a separate TF build, admittedly), I get the following runtime crash:

ubuntu@ip-10-22-3-51:~/src/tensorflow$ bazel-bin/tensorflow/lite/tools/benchmark/benchmark_model --help
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc
Aborted (core dumped)

System information

  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Ubuntu 16.04
  • Mobile device (e.g. iPhone 8, Pixel 2, Samsung Galaxy) if the issue happens on mobile device: n/a
  • TensorFlow installed from (source or binary): source
  • TensorFlow version: checked out from v2.5.0
  • Python version: 3.6.3
  • Installed using virtualenv? pip? conda?: virtualenv
  • Bazel version (if compiling from source): 3.7.2 (via Bazelisk)
  • GCC/Compiler version (if compiling from source): gcc 8.4.0
  • CUDA/cuDNN version: CUDA Version 10.0.130/cuDNN 7.5
  • GPU model and memory: NVIDIA GV100GL Tesla V100 SXM2 (16GB)
@thisisjoshstone thisisjoshstone added the type:build/install Build and install issues label Jul 23, 2021
@UsharaniPagadala UsharaniPagadala added comp:lite TF Lite related issues comp:ops OPs related issues subtype: ubuntu/linux Ubuntu/Linux Build/Installation Issues labels Jul 23, 2021
@ymodak ymodak added stat:awaiting tensorflower Status - Awaiting response from tensorflower and removed comp:ops OPs related issues labels Jul 26, 2021
@ymodak ymodak assigned terryheo and unassigned ymodak Jul 26, 2021
@wangtz
Copy link
Member

wangtz commented Jul 26, 2021

Hi Tian / Thai,

Wondering if you have an example of using building TFLite with tf.text flex ops?

Thanks,
Tiezhen

@thaink
Copy link
Member

thaink commented Jul 26, 2021

For the first attempt, you should modify benchmark_model_plus_flex and use that tool instead of benchmark_model

If you want a smaller binary size, You might try to define a new custom library as: https://github.com/tensorflow/tflite-support/blob/master/tensorflow_lite_support/custom_ops/BUILD#L10 then use it to replace the //tensorflow/lite/delegates/flex:delegate in benchmark_model_plus_flex

@thisisjoshstone
Copy link
Author

Hey @thaink, @wangtz, thanks for the quick response!

I hadn't seen the flex benchmark tool, was just adding the flex delegate to the base benchmark_model. Will use benchmark_model_plus_flex going forward.

I've tried building that tool this morning, but I'm seeing an Eigen dependency error on the build similar to what I was seeing previously. Details below:

Additional change for `benchmark_model_plus_flex`

diff --git a/tensorflow/lite/tools/benchmark/BUILD b/tensorflow/lite/tools/benchmark/BUILD
index 0793db4a417..91d7b4dfa15 100644
--- a/tensorflow/lite/tools/benchmark/BUILD
+++ b/tensorflow/lite/tools/benchmark/BUILD
@@ -91,6 +91,7 @@ tf_cc_binary(
         "//tensorflow/lite/delegates/flex:delegate",
         "//tensorflow/lite/testing:init_tensorflow",
         "//tensorflow/lite/tools:logging",
+        "@org_tensorflow_text//tensorflow_text:ops_lib",
     ],
 )

Build error

bazel build --config=monolithic -c opt tensorflow/lite/tools/benchmark:benchmark_model_plus_flex --verbose_failures
Starting local Bazel server and connecting to it...
INFO: Options provided by the client:
Inherited 'common' options: --isatty=1 --terminal_columns=202
INFO: Reading rc options for 'build' from /home/ubuntu/src/tensorflow/.bazelrc:
Inherited 'common' options: --experimental_repo_remote_exec
INFO: Reading rc options for 'build' from /home/ubuntu/src/tensorflow/.bazelrc:
'build' options: --define framework_shared_object=true --java_toolchain=@tf_toolchains//toolchains/java:tf_java_toolchain --host_java_toolchain=@tf_toolchains//toolchains/java:tf_java_toolchain --define=use_fast_cpp_protos=true --define=allow_oversize_protos=true --spawn_strategy=standalone -c opt --announce_rc --define=grpc_no_ares=true --noincompatible_remove_legacy_whole_archive --noincompatible_prohibit_aapt1 --enable_platform_specific_config --define=with_xla_support=true --config=short_logs --config=v2
INFO: Reading rc options for 'build' from /home/ubuntu/src/tensorflow/.tf_configure.bazelrc:
'build' options: --action_env PYTHON_BIN_PATH=/home/ubuntu/src/tensorflow/.venv/bin/python3 --action_env PYTHON_LIB_PATH=/home/ubuntu/src/tensorflow/.venv/lib/python3.6/site-packages --python_path=/home/ubuntu/src/tensorflow/.venv/bin/python3 --action_env CUDA_TOOLKIT_PATH=/usr/local/cuda-10.0
INFO: Found applicable config definition build:short_logs in file /home/ubuntu/src/tensorflow/.bazelrc: --output_filter=DONT_MATCH_ANYTHING
INFO: Found applicable config definition build:v2 in file /home/ubuntu/src/tensorflow/.bazelrc: --define=tf_api_version=2 --action_env=TF2_BEHAVIOR=1
INFO: Found applicable config definition build:monolithic in file /home/ubuntu/src/tensorflow/.bazelrc: --define framework_shared_object=false
INFO: Found applicable config definition build:linux in file /home/ubuntu/src/tensorflow/.bazelrc: --copt=-w --host_copt=-w --define=PREFIX=/usr --define=LIBDIR=$(PREFIX)/lib --define=INCLUDEDIR=$(PREFIX)/include --define=PROTOBUF_INCLUDE_PATH=$(PREFIX)/include --cxxopt=-std=c++14 --host_cxxopt=-std=c++14 --config=dynamic_kernels
INFO: Found applicable config definition build:dynamic_kernels in file /home/ubuntu/src/tensorflow/.bazelrc: --define=dynamic_loaded_kernels=true --copt=-DAUTOLOAD_DYNAMIC_KERNELS
INFO: Analyzed target //tensorflow/lite/tools/benchmark:benchmark_model_plus_flex (259 packages loaded, 21443 targets configured).
INFO: Found 1 target...
ERROR: /home/ubuntu/.cache/bazel/bazel_ubuntu/b813f517b143a3c1665dd902035fe00f/external/org_tensorflow_text/tensorflow_text/BUILD:1010:19: C++ compilation of rule '@org_tensorflow_text//tensorflow_text:unicode_script_tokenizer_cc' failed (Exit 1): gcc failed: error executing command
(cd /home/ubuntu/.cache/bazel/bazel_ubuntu/b813f517b143a3c1665dd902035fe00f/execroot/org_tensorflow &&
exec env -
CUDA_TOOLKIT_PATH=/usr/local/cuda-10.0
LD_LIBRARY_PATH=/usr/local/cuda/lib:/usr/local/cuda/lib64:/usr/local/cuda/extras/CUPTI/lib64:/opt/amazon/efa/lib:/opt/amazon/openmpi/lib:/usr/local/lib:/usr/lib:
PATH=/home/ubuntu/.cache/bazelisk/downloads/bazelbuild/bazel-3.7.2-linux-x86_64/bin:/usr/local/cuda:/opt/amazon/openmpi/bin/:/opt/amazon/efa/bin/:/home/ubuntu/anaconda3/condabin:/home/ubuntu/.dl_binaries/bin:/usr/local/cuda/bin:/opt/aws/neuron/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
PWD=/proc/self/cwd
PYTHON_BIN_PATH=/home/ubuntu/src/tensorflow/.venv/bin/python3
PYTHON_LIB_PATH=/home/ubuntu/src/tensorflow/.venv/lib/python3.6/site-packages
TF2_BEHAVIOR=1
/usr/bin/gcc -U_FORTIFY_SOURCE -fstack-protector -Wall -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-pointer -g0 -O2 '-D_FORTIFY_SOURCE=1' -DNDEBUG -ffunction-sections -fdata-sections '-std=c++0x' -MD -MF bazel-out/k8-opt/bin/external/org_tensorflow_text/tensorflow_text/objs/unicode_script_tokenizer_cc/unicode_script_tokenize_op.d '-frandom-seed=bazel-out/k8-opt/bin/external/org_tensorflow_text/tensorflow_text/objs/unicode_script_tokenizer_cc/unicode_script_tokenize_op.o' -iquote external/org_tensorflow_text -iquote bazel-out/k8-opt/bin/external/org_tensorflow_text -iquote external/icu -iquote bazel-out/k8-opt/bin/external/icu -iquote . -iquote bazel-out/k8-opt/bin -iquote external/com_google_absl -iquote bazel-out/k8-opt/bin/external/com_google_absl -isystem external/icu/icu4c/source/common -isystem bazel-out/k8-opt/bin/external/icu/icu4c/source/common -w -DAUTOLOAD_DYNAMIC_KERNELS '-std=c++14' -pthread -fno-canonical-system-headers -Wno-builtin-macro-redefined '-D__DATE="redacted"' '-D__TIMESTAMP
="redacted"' '-D__TIME__="redacted"' -c external/org_tensorflow_text/tensorflow_text/core/ops/unicode_script_tokenize_op.cc -o bazel-out/k8-opt/bin/external/org_tensorflow_text/tensorflow_text/_objs/unicode_script_tokenizer_cc/unicode_script_tokenize_op.o)
Execution platform: @local_execution_config_platform//:platform
In file included from ./tensorflow/core/framework/tensor_shape.h:21,
from ./tensorflow/core/framework/partial_tensor_shape.h:20,
from ./tensorflow/core/framework/attr_value_util.h:23,
from ./tensorflow/core/framework/node_def_util.h:23,
from ./tensorflow/core/framework/shape_inference.h:21,
from ./tensorflow/core/framework/common_shape_fns.h:20,
from external/org_tensorflow_text/tensorflow_text/core/ops/unicode_script_tokenize_op.cc:18:
./third_party/eigen3/unsupported/Eigen/CXX11/Tensor:1:10: fatal error: unsupported/Eigen/CXX11/Tensor: No such file or directory
#include "unsupported/Eigen/CXX11/Tensor"
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.

Any other ideas on what may be wrong here? I can build a working benchmark tool, even with flex ops, so I think the external dependencies are available for use. It's just that they aren't able to be found when the target file is in the @org_tensorflow_text external repo.

@thaink
Copy link
Member

thaink commented Jul 26, 2021

@broken Is possible to depend on @org_tensorflow_text//tensorflow_text:ops_lib outside of the text repo?

@broken
Copy link
Member

broken commented Jul 26, 2021

Yes; this is what we do with model server.

I have to admit that I'm inexperienced building tf text as flex ops, but the tflite_cc_shared_object in our build does and is working.

From the error message, it looks like the build is using the normal TF deps rather than the mobile target & portable tflite deps. Would adding --config=android fix this?


FYI; the tflite-support library has a few TF Lite version of TF Text ops which includes a mobile version of Sentencepiece. This will be more performant. We're currently working on getting these in the main tf text repo and expanding the offering of native tf lite/text ops.

@thisisjoshstone
Copy link
Author

@broken @thaink Thanks for the quick feedback!! I've been continuing to work on this based on your suggestions. (I wasn't able to get it working yet with that tflite-support library, but it looks really cool -- will try again to use it in the future.) In case others are interested, I was able to finally get builds working by applying the diff I've included here.

Diff

diff --git a/tensorflow/lite/c/BUILD b/tensorflow/lite/c/BUILD
index 3119cf30557..3b7ff060518 100644
--- a/tensorflow/lite/c/BUILD
+++ b/tensorflow/lite/c/BUILD
@@ -38,6 +38,8 @@ tflite_cc_shared_object(
         ":c_api_experimental",
         ":exported_symbols.lds",
         ":version_script.lds",
+        "//tensorflow/lite/delegates/flex:delegate",
+        "@org_tensorflow_text//tensorflow_text:ops_lib",
     ],
 )
 
diff --git a/tensorflow/lite/tools/benchmark/BUILD b/tensorflow/lite/tools/benchmark/BUILD
index 815efb776e9..c100bad4223 100644
--- a/tensorflow/lite/tools/benchmark/BUILD
+++ b/tensorflow/lite/tools/benchmark/BUILD
@@ -88,6 +88,7 @@ tf_cc_binary(
         "//tensorflow/lite/delegates/flex:delegate",
         "//tensorflow/lite/testing:init_tensorflow",
         "//tensorflow/lite/tools:logging",
+        "@org_tensorflow_text//tensorflow_text:ops_lib"
     ],
 )
 
diff --git a/tensorflow/workspace0.bzl b/tensorflow/workspace0.bzl
index 22374bc1297..abf1eb96677 100644
--- a/tensorflow/workspace0.bzl
+++ b/tensorflow/workspace0.bzl
@@ -102,6 +102,38 @@ def workspace():
         ],
     )
 
+    # TF.text repo
+    http_archive(
+        name = "org_tensorflow_text",
+        sha256 = "d82856dc04c04bbce347f72d0ad7df59bb6b26b7030f96f25f036cfe8a138312",
+        strip_prefix = "text-2.5.0",
+        urls = [
+            "https://github.com/tensorflow/text/archive/v2.5.0.zip",
+        ],
+        patches = ["@//third_party/tf_text:tf_text_delegate.patch"],
+        patch_args = ["-p1"],
+    )
+
+    # TF.text dependencies
+    http_archive(
+        name = "com_google_sentencepiece",
+        strip_prefix = "sentencepiece-1.0.0",
+        sha256 = "c05901f30a1d0ed64cbcf40eba08e48894e1b0e985777217b7c9036cac631346",
+        urls = [
+            "https://github.com/google/sentencepiece/archive/1.0.0.zip",
+        ],
+    )
+
+    http_archive(
+        name = "com_google_glog",
+        sha256 = "1ee310e5d0a19b9d584a855000434bb724aa744745d5b8ab1855c85bff8a8e21",
+        strip_prefix = "glog-028d37889a1e80e8a07da1b8945ac706259e5fd8",
+        urls = [
+            "https://mirror.bazel.build/github.com/google/glog/archive/028d37889a1e80e8a07da1b8945ac706259e5fd8.tar.gz",
+            "https://github.com/google/glog/archive/028d37889a1e80e8a07da1b8945ac706259e5fd8.tar.gz",
+        ],
+    )
+
     bazel_toolchains_repositories()
 
     # Use `swift_rules_dependencies` to fetch the toolchains. With the
diff --git a/third_party/icu/BUILD.bazel b/third_party/icu/BUILD.bazel
index 14cadffc841..691585ce5ba 100644
--- a/third_party/icu/BUILD.bazel
+++ b/third_party/icu/BUILD.bazel
@@ -19,6 +19,16 @@ cc_library(
     ],
 )
 
+alias(
+    name = "nfkc",
+    actual = ":common",
+)
+
+alias(
+    name = "nfkc_cf",
+    actual = ":common",
+)
+
 cc_library(
     name = "common",
     hdrs = glob(["icu4c/source/common/unicode/*.h"]),
diff --git a/third_party/tf_text/BUILD b/third_party/tf_text/BUILD
new file mode 100644
index 00000000000..e69de29bb2d
diff --git a/third_party/tf_text/tf_text_delegate.patch b/third_party/tf_text/tf_text_delegate.patch
new file mode 100644
index 00000000000..b0cba5225d5
--- /dev/null
+++ b/third_party/tf_text/tf_text_delegate.patch
@@ -0,0 +1,30 @@
+diff --git a/tensorflow_text/tftext.bzl b/tensorflow_text/tftext.bzl
+index 7f703b3..888bea4 100644
+--- a/tensorflow_text/tftext.bzl
++++ b/tensorflow_text/tftext.bzl
+@@ -93,8 +93,8 @@ def tf_deps(deps = []):
+             "@org_tensorflow//tensorflow/core:portable_tensorflow_lib_lite",
+         ],
+         "//conditions:default": [
+-            "@local_config_tf//:libtensorflow_framework",
+-            "@local_config_tf//:tf_header_lib",
++            "@org_tensorflow//tensorflow/core:framework",
++            "@org_tensorflow//tensorflow/core:lib",
+         ] + deps + oss_deps,
+     })
+ 
+diff --git a/third_party/sentencepiece/processor.patch b/third_party/sentencepiece/processor.patch
+index 5fa1b84..427c104 100644
+--- a/third_party/sentencepiece/processor.patch
++++ b/third_party/sentencepiece/processor.patch
+@@ -22,8 +22,8 @@ index b4298d2..7ce779f 100644
+ +              "@org_tensorflow//tensorflow/core:tflite_portable_logging",
+ +            ],
+ +            "//conditions:default": [
+-+              "@local_config_tf//:libtensorflow_framework",
+-+              "@local_config_tf//:tf_header_lib",
+++              "@org_tensorflow//tensorflow/core:framework",
+++              "@org_tensorflow//tensorflow/core:lib",
+ +              "@com_google_absl//absl/functional:function_ref",
+ +              "@com_google_absl//absl/strings:cord",
+ +            ],

Here are also a few other error messages that I saw along the way but didn't post above, in case it helps someone else arrive here:

Error message

ERROR: Op type not registered 'SentencepieceOp' in binary running on ip-10-22-3-51. Make sure the Op and Kernel are registered in the binary running in this process. Note that if you are loading a saved graph which used ops from tf.contrib, accessing (e.g.) tf.contrib.resampler should be done before importing the graph, as contrib ops are lazily registered when the module is first accessed.
INFO: TfLiteFlexDelegate delegate: 0 nodes delegated out of 3 nodes with 0 partitions.

INFO: TfLiteFlexDelegate delegate: 0 nodes delegated out of 11 nodes with 0 partitions.

INFO: TfLiteFlexDelegate delegate: 0 nodes delegated out of 3 nodes with 0 partitions.

INFO: TfLiteFlexDelegate delegate: 0 nodes delegated out of 17 nodes with 0 partitions.

ERROR: Delegate kernel was not initialized
ERROR: Node number 304 (TfLiteFlexDelegate) failed to prepare.

Error message

/home/ubuntu/.cache/bazel/_bazel_ubuntu/..../external/org_tensorflow_text/tensorflow_text/BUILD:1162:19: no such package '@local_config_tf//': The repository '@local_config_tf' could not be resolved and referenced by '@org_tensorflow_text//tensorflow_text:wordpiece_tokenizer_cc'

Error message

ERROR: /home/ubuntu/src/tensorflow/tensorflow/lite/delegates/flex/BUILD:73:23: error loading package '@org_tensorflow_text//tensorflow_text': Every .bzl file must have a corresponding package, but '@org_tensorflow_text//tensorflow/lite:build_def.bzl' does not have one. Please create a BUILD file in the same or any parent directory. Note that this BUILD file does not need to do anything except exist. and referenced by '//tensorflow/lite/delegates/flex:delegate'

Error message

ERROR: Analysis of target '//tensorflow_text:tensorflowlite_c' failed; build aborted: no such package 'tensorflow': BUILD file not found in any of the following directories. Add a BUILD file to a directory to mark it as a package.

@google-ml-butler
Copy link

Are you satisfied with the resolution of your issue?
Yes
No

@broken
Copy link
Member

broken commented Jul 28, 2021

I'm glad you got it working, and thanks for such a detailed closing comment for the people of the future.

@Sid911
Copy link

Sid911 commented Nov 13, 2022

@thisisjoshstone Thanks for such a detailed issue description and closure comment. I was not looking for tflite but just integrating TF.text ops in a shared library but this helped me along the way.

Usually no one has this detailed issue description, I will be yanking your way of doing it going forward.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp:lite TF Lite related issues stat:awaiting tensorflower Status - Awaiting response from tensorflower subtype: ubuntu/linux Ubuntu/Linux Build/Installation Issues type:build/install Build and install issues
Projects
None yet
Development

No branches or pull requests

9 participants