tensorflow / text

Making text a first-class citizen in TensorFlow.
https://www.tensorflow.org/beta/tutorials/tensorflow_text/intro
Apache License 2.0
1.23k stars 345 forks source link

Errors building tensorflow-text on apple silicon even when using matching versions of tensorflow 2.10 #1077

Open tsdeng opened 1 year ago

tsdeng commented 1 year ago

I'm trying to build the tensorflow-text on a M1 Mac because tensorflow-text is not released for Apple silicon.

When compiling using ./oss_scripts/run_build.sh I see following errors:

In file included from tensorflow_text/core/kernels/byte_splitter_tflite.cc:18:
external/org_tensorflow/tensorflow/lite/kernels/shim/tflite_op_shim.h:128:35: error: no member named 'OpName' in 'tensorflow::text::ByteSplitterWithOffsetsOp<tflite::shim::Runtime::kTfLite>'
    resolver->AddCustom(ImplType::OpName(), GetTfLiteRegistration());
                        ~~~~~~~~~~^
tensorflow_text/core/kernels/byte_splitter_tflite.cc:28:53: note: in instantiation of member function 'tflite::shim::TfLiteOpKernel<tensorflow::text::ByteSplitterWithOffsetsOp>::Add' requested here
      tensorflow::text::ByteSplitterWithOffsetsOp>::Add(resolver);

I would also love to know if anyone is able to get tensorflow-text 2.10 to work on apple silicon.

Setup and reproduce

Tensorflow is installed via conda

conda install -c apple tensorflow-deps=2.10.0
python -m pip install tensorflow-macos==2.10.0
python -m pip install tensorflow-metal==0.6

Bazel is installed from home-brew

brew install bazelisk

Tensorflow-text is downloaded from release page

wget https://github.com/tensorflow/text/archive/refs/tags/v2.10.0.zip
mhaas commented 1 year ago

@DuongTSon OK, I was able to build it, but now there is an error when I try to import:

 from tensorflow_text.core.pybinds import tflite_registrar
ImportError: dlopen(/Users/vedmant/miniconda3/lib/python3.10/site-packages/tensorflow_text/core/pybinds/tflite_registrar.so, 0x0002): tried: '/Users/vedmant/miniconda3/lib/python3.10/site-packages/tensorflow_text/core/pybinds/tflite_registrar.so' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64e'))

I was seeing the same problem (just with arm64 instead of arm64e) and I was reasonably sure that my environment is set up correctly. I fixed/worked around this by explicitly adding --cpu=darwin_arm64 to my .bazelrc:

# Settings for MacOS on ARM CPUs.
build:macos_arm64 --cpu=darwin_arm64
build:macos_arm64 --macos_minimum_os=11.0

Note that the .bazelrcis regenerated every time you call oss_scripts/run_build.sh. To prevent this, I commented out the call to the configure.shscript like this after running for the first time:


# Run configure.
# source oss_scripts/configure.sh

My Python 3.10 is installed via brew and not via conda. That might make a difference and explain why only some people see this error - and why it eventually worked for @vedmant after switching to a different Python binary.

Additionally, to make the resulting wheel work, I had to install the older ldas described above.

With these two tweaks, the guide by @DuongTSon above works fine.

qitao052 commented 1 year ago

I meet a problem when importing tensforflow_text like below:

--> from tensorflow_text.core.pybinds import tflite_registrar ImportError: cannot import name 'tflite_registrar' from 'tensorflow_text.core.pybinds' (unknown location)

Could any help to figure out the problem? Thansk a lot

my environment: MacOS 13.3 conda environment Python 3.10.10 tensorflow 2.12.0

Below is the approach I install the library: Step1: git clone https://github.com/tensorflow/text.git cd text git checkout 2.12

Step2: install bazel by (version 5.3.0) ./oss_scripts/install_bazel.sh

Step3: I modified the run_build.sh by specify the version 5.3.0.

if [ "$installed_bazel_version" != "5.3.0" ]; then

Since the auto generated .bazelversion contains a #Note and always tell me to install 5.3.0 bazel
5.3.0 NOTE: Update Bazel version in tensorflow/tools/ci_build/release/common.sh.oss

Step4: generate the wheel, succeed with several warnings. ./oss_scripts/run_build.sh

Step5: pip install tensorflow_text-2.12.0-cp310-cp310-macosx_11_0_arm64.whl

dhruv1710 commented 1 year ago

Its working great, Thanks to @qitao052 @tsdeng and https://medium.com/@murphy.crosby/building-tensorflow-and-tensorflow-text-on-a-m1-mac-9b90d55e92df .

josharian commented 7 months ago

For anyone else still hitting this, the fix for me was to edit oss_scripts/run_build.sh and add --config="macos_arm64" to bazel build (i.e. before --enable_runfiles ...).