Skip to content
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

Incompatibility with Mac M1 (ARM-based) #26

Open
joseangel-mm opened this issue Jan 9, 2024 · 15 comments
Open

Incompatibility with Mac M1 (ARM-based) #26

joseangel-mm opened this issue Jan 9, 2024 · 15 comments

Comments

@joseangel-mm
Copy link

Hi, I am working on a project that uses C tensorflow library on Golang and I am struggle with some issues in my Mac M1 (ARM-based).

Basically, in the past I had some issues with the TensorFlow library itself but I solved that changing the GOOS environmental variable to ios instead of darwin.

Once I did that change, when I run my app (go run main.go), I get the following error, so I would like to know if github.com/wamuir/graft/tensorflow is compatible with ARM Mac:

internal/tensor/prepare_tensor.go:13:77: undefined: tf.Tensor
internal/tensor/prepare_tensor.go:20:22: undefined: tf.NewTensor

The previous error (when I have GOOS='darwin'), was the following:

/usr/local/go/pkg/tool/darwin_arm64/link: running clang failed: exit status 1
ld: warning: ignoring file /usr/local/lib/libtensorflow.dylib, building for macOS-arm64 but attempting to link with file built for macOS-x86_64
Undefined symbols for architecture arm64:
  "_TFE_ContextListDevices", referenced from:
      __cgo_13f1810e087e_Cfunc_TFE_ContextListDevices in 000004.o
     (maybe you meant: __cgo_13f1810e087e_Cfunc_TFE_ContextListDevices)
  "_TFE_ContextOptionsSetAsync", referenced from:
      __cgo_13f1810e087e_Cfunc_TFE_ContextOptionsSetAsync in 000004.o
     (maybe you meant: __cgo_13f1810e087e_Cfunc_TFE_ContextOptionsSetAsync)
  "_TFE_ContextOptionsSetConfig", referenced from:
      __cgo_13f1810e087e_Cfunc_TFE_ContextOptionsSetConfig in 000004.o
     (maybe you meant: __cgo_13f1810e087e_Cfunc_TFE_ContextOptionsSetConfig)
  "_TFE_DeleteContext", referenced from:
      __cgo_13f1810e087e_Cfunc_TFE_DeleteContext in 000004.o
     (maybe you meant: __cgo_13f1810e087e_Cfunc_TFE_DeleteContextOptions, __cgo_13f1810e087e_Cfunc_TFE_DeleteContext )
...
  "_TF_Version", referenced from:
      __cgo_13f1810e087e_Cfunc_TF_Version in 000014.o
     (maybe you meant: __cgo_13f1810e087e_Cfunc_TF_Version)
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

The version of github.com/wamuir/graft I am using is 0.7.0 and the TensorFlow version is libtensorflow-cpu-darwin-x86_64-2.15.0 that is located in /usr/local/lib.

Thanks in advanced.

@joseangel-mm joseangel-mm changed the title Incompatibility with Mac M1 Incompatibility with Mac M1 (ARM-based) Jan 9, 2024
@wamuir
Copy link
Owner

wamuir commented Jan 9, 2024

No incompatibility that I'm aware of, the typical issue for Mac is a mismatch between libtensorflow build and system architecture. That's what it looks like here. For instance, I see that your architecture is arm64 but the libtensorflow build is for x86_64. Probably you should obtain a libtensorflow build that matches your system (or just build the libtensorflow package from source). Probably also double-check your import statements within internal/tensor/prepare_tensor.go, etc.

@joseangel-mm
Copy link
Author

Hi @wamuir, thank you for the response.
Checking what you mentioned, in this page the only file for Mac is the one I mentioned in the description. Regarding the option of building libtensorflow package from source, is there any article/tutorial to do it?

@wamuir
Copy link
Owner

wamuir commented Jan 10, 2024

Yes, see Build from Source at the bottom of the page that you linked.

@vicentsegui
Copy link

I'm also having issues with M1. When using this from a normal go project it works ok, I can run the example and it will work just fine. However it fails with if I try to use bazel in the project. I then tried to downlaod graft and build it independently but I hit the same error:

git clone https://github.com/wamuir/graft
cd graft/tensorflow
bazel build  :all
ERROR: /Users/segui/Projects/bower/graft/third_party/org_tensorflow/BUILD:8:6: configurable attribute "actual" in //third_party/org_tensorflow:libtensorflow doesn't match this configuration. Would a default condition help?

Conditions checked:
 //tools/conditions:linux_x86_64_cpu
 //tools/conditions:linux_x86_64_gpu
 //tools/conditions:macos_x86_64_cpu

To see a condition's definition, run: bazel query --output=build <condition label>.

This instance of //third_party/org_tensorflow:libtensorflow has configuration identifier 3f1b74d. To inspect its configuration, run: bazel config 3f1b74d.

For more help, see https://bazel.build/docs/configurable-attributes#faq-select-choose-condition.

ERROR: Analysis of target '//tensorflow:go_default_library' failed; build aborted:
INFO: Elapsed time: 0.089s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (0 packages loaded, 0 targets configured)

Seems like there is no option to build for m1?

@joseangel-mm
Copy link
Author

Hi @vicentsegui, I tried in the past to build the TensorFlow from source but no luck, finally I downloaded it from homebrew and pointed the env var to that path.

@vicentsegui
Copy link

Hi @joseangel-mm I also have the brew installed version (I adjust the CGO_LFLAGS and CGO_LDFLAGS accordingly), the problem comes when trying to compile the actual go bindings which seem to be confused about what to build because there is no arm64 macos platform defined. @joseangel-mm did you setup any other env paths?

@joseangel-mm
Copy link
Author

As I commented, finally I used the TensorFlow from homebrew and I updated these variable environments.

@wamuir
Copy link
Owner

wamuir commented Jan 24, 2024

@vicentsegui correct, for ARM this library will only work using Go directly after installing a pre-built libtensorflow. And for ARM this library will not work using Bazel. The reason for this is that running Bazel build on this library does not compile libtensorflow, rather it downloads the official libtensorflow release for that platform (system architecture). And there is not an official libtensorflow release for ARM.

@vicentsegui
Copy link

Could we add a local_repository build rule for the macos arm version that links to the homebrew version?

@wamuir
Copy link
Owner

wamuir commented Jan 27, 2024

@vicentsegui let's see if this works. Try:

git clone -b bzl-test-other-platforms https://github.com/wamuir/graft.git
cd graft/tensorflow
bazel build --repo_env=LIBTENSORFLOW_PKG_URL=file:///path/to/libtensorflow.tar.gz :all

Replace file:///path/to/libtensorflow.tar.gz with the URL for your build of libtensorflow.tar.gz (for this test branch make sure you have a libtensorflow 2.15.0 build). The default for LIBTENSORFLOW_PKG_URL is file:///opt/libtensorflow.tar.gz.

@vicentsegui
Copy link

I packed my libtensorflow with

$ tar -czf ../../libtensorflow.tar.gz -C /opt/homebrew/Cellar/libtensorflow/2.15.0/ .

The libraries can be found under lib/ inside the package, trying to compile complains about them missing:

ERROR: /private/var/tmp/_bazel_segui/a673c150a613381a6be8a0e9ca895c36/external/libtensorflow_other_build/BUILD.bazel:55:10: SolibSymlink _solib_darwin_arm64/_U@libtensorflow_Uother_Ubuild_S_S_Clibtensorflow_Uimport_Ulib___Ulib/libtensorflow.2.dylib failed: missing input file '@libtensorflow_other_build//:lib/libtensorflow.2.dylib'
ERROR: /private/var/tmp/_bazel_segui/a673c150a613381a6be8a0e9ca895c36/external/libtensorflow_other_build/BUILD.bazel:55:10: SolibSymlink _solib_darwin_arm64/_U@libtensorflow_Uother_Ubuild_S_S_Clibtensorflow_Uimport_Ulib___Ulib/libtensorflow.2.dylib failed: 1 input file(s) do not exist
ERROR: /private/var/tmp/_bazel_segui/a673c150a613381a6be8a0e9ca895c36/external/libtensorflow_other_build/BUILD.bazel:55:10 SolibSymlink _solib_darwin_arm64/_U@libtensorflow_Uother_Ubuild_S_S_Clibtensorflow_Uimport_Ulib___Ulib/libtensorflow.2.dylib failed: 1 input file(s) do not exist

Also tried with $ tar -czf ../../libtensorflow.tar.gz -C /opt/homebrew/Cellar/libtensorflow/2.15.0/lib . but got the same error. I wonder if it's worth trying to use some bazel/pkg-config integration (like https://github.com/cherrry/bazel_pkg_config/ or https://github.com/claudiouzelac/bazel-boost/tree/master/tools/build_rules/config) instead.

@vicentsegui
Copy link

I gave it another try today and managed to make it worl @wamuir I think I had the wrong tar on the path. It now builds tensorflow on M1.

@vicentsegui
Copy link

Do you plan on merging this to your main branch?

@wamuir
Copy link
Owner

wamuir commented Feb 7, 2024

@vicentsegui yes #28 will merge it into nightly (default branch)

@vicentsegui
Copy link

Hi, seems like the new release now supports macos m1 so this may no longer be needed?
Full aarch64 Linux and Arm64 macOS wheels are now published to the tensorflow pypi repository and no longer redirect to a separate package.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants