Skip to content

Issues installing tensorflow on M1 mac #493

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
njtierney opened this issue Dec 7, 2021 · 8 comments
Closed

Issues installing tensorflow on M1 mac #493

njtierney opened this issue Dec 7, 2021 · 8 comments

Comments

@njtierney
Copy link

As per #465 - I followed the instructions - the installation process seems to succeed, but it doesn't work when I try and use tensorflow.

Here's a reprex:

library(tensorflow)
install_tensorflow()
#> Installing: tensorflow-deps
#> Installing: tensorflow-macos
#> Installing: tensorflow-metal
as_tensor(1)
#> Error: Valid installation of TensorFlow not found.
#> 
#> Python environments searched for 'tensorflow' package:
#>  /Users/nick/Library/r-miniconda-arm64/envs/r-reticulate/bin/python3.8
#> 
#> Python exception encountered:
#>  NotFoundError: dlopen(/Users/nick/Library/r-miniconda-arm64/envs/r-reticulate/lib/python3.8/site-packages/tensorflow-plugins/libmetal_plugin.dylib, 6): Symbol not found: _OBJC_CLASS_$_MPSGraphCompilationDescriptor
#>   Referenced from: /Users/nick/Library/r-miniconda-arm64/envs/r-reticulate/lib/python3.8/site-packages/tensorflow-plugins/libmetal_plugin.dylib (which was built for Mac OS X 12.0)
#>   Expected in: /System/Library/Frameworks/MetalPerformanceShadersGraph.framework/Versions/A/MetalPerformanceShadersGraph
#> 
#> 
#> Detailed traceback:
#>   File "/Library/Frameworks/R.framework/Versions/4.1-arm64/Resources/library/reticulate/python/rpytools/loader.py", line 39, in _import_hook
#>     module = _import(
#>   File "/Users/nick/Library/r-miniconda-arm64/envs/r-reticulate/lib/python3.8/site-packages/tensorflow/__init__.py", line 449, in <module>
#>     _ll.load_library(_plugin_dir)
#>   File "/Users/nick/Library/r-miniconda-arm64/envs/r-reticulate/lib/python3.8/site-packages/tensorflow/python/framework/load_library.py", line 155, in load_library
#>     py_tf.TF_LoadLibrary(lib)
#> 
#> 
#> You can install TensorFlow using the install_tensorflow() function.

Created on 2021-12-07 by the reprex package (v2.0.1)

Session info
sessioninfo::session_info()
#> ─ Session info  🖖🏼  👨🏽‍🏫  🎸   ─────────────────────────────────────────────────
#>  hash: vulcan salute: medium-light skin tone, man teacher: medium skin tone, guitar
#> 
#>  setting  value
#>  version  R version 4.1.2 (2021-11-01)
#>  os       macOS Big Sur 11.2.2
#>  system   aarch64, darwin20
#>  ui       X11
#>  language (EN)
#>  collate  en_AU.UTF-8
#>  ctype    en_AU.UTF-8
#>  tz       Australia/Perth
#>  date     2021-12-07
#>  pandoc   2.16.1 @ /Applications/RStudio.app/Contents/MacOS/quarto/bin/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version    date (UTC) lib source
#>  backports     1.4.0      2021-11-23 [1] CRAN (R 4.1.1)
#>  base64enc     0.1-3      2015-07-28 [1] CRAN (R 4.1.0)
#>  cli           3.1.0      2021-10-27 [1] CRAN (R 4.1.1)
#>  crayon        1.4.2      2021-10-29 [1] CRAN (R 4.1.1)
#>  digest        0.6.29     2021-12-01 [1] CRAN (R 4.1.1)
#>  ellipsis      0.3.2      2021-04-29 [1] CRAN (R 4.1.0)
#>  evaluate      0.14       2019-05-28 [1] CRAN (R 4.1.0)
#>  fansi         0.5.0      2021-05-25 [1] CRAN (R 4.1.0)
#>  fastmap       1.1.0      2021-01-25 [1] CRAN (R 4.1.0)
#>  fs            1.5.1      2021-11-30 [1] CRAN (R 4.1.1)
#>  glue          1.5.1      2021-11-30 [1] CRAN (R 4.1.1)
#>  here          1.0.1      2020-12-13 [1] CRAN (R 4.1.0)
#>  highr         0.9        2021-04-16 [1] CRAN (R 4.1.0)
#>  htmltools     0.5.2      2021-08-25 [1] CRAN (R 4.1.1)
#>  jsonlite      1.7.2      2020-12-09 [1] CRAN (R 4.1.0)
#>  knitr         1.36       2021-09-29 [1] CRAN (R 4.1.1)
#>  lattice       0.20-45    2021-09-22 [1] CRAN (R 4.1.2)
#>  lifecycle     1.0.1      2021-09-24 [1] CRAN (R 4.1.1)
#>  magrittr      2.0.1      2020-11-17 [1] CRAN (R 4.1.0)
#>  Matrix        1.3-4      2021-06-01 [1] CRAN (R 4.1.2)
#>  pillar        1.6.4      2021-10-18 [1] CRAN (R 4.1.1)
#>  pkgconfig     2.0.3      2019-09-22 [1] CRAN (R 4.1.0)
#>  png           0.1-7      2013-12-03 [1] CRAN (R 4.1.0)
#>  purrr         0.3.4      2020-04-17 [1] CRAN (R 4.1.0)
#>  R.cache       0.15.0     2021-04-30 [1] CRAN (R 4.1.0)
#>  R.methodsS3   1.8.1      2020-08-26 [1] CRAN (R 4.1.0)
#>  R.oo          1.24.0     2020-08-26 [1] CRAN (R 4.1.0)
#>  R.utils       2.11.0     2021-09-26 [1] CRAN (R 4.1.1)
#>  Rcpp          1.0.7      2021-07-07 [1] CRAN (R 4.1.0)
#>  reprex        2.0.1      2021-08-05 [1] CRAN (R 4.1.1)
#>  reticulate    1.22-9000  2021-12-07 [1] Github (rstudio/reticulate@d15b9e9)
#>  rlang         0.4.12     2021-10-18 [1] CRAN (R 4.1.1)
#>  rmarkdown     2.11       2021-09-14 [1] CRAN (R 4.1.1)
#>  rprojroot     2.0.2      2020-11-15 [1] CRAN (R 4.1.0)
#>  rstudioapi    0.13       2020-11-12 [1] CRAN (R 4.1.0)
#>  sessioninfo   1.2.1      2021-11-02 [1] CRAN (R 4.1.1)
#>  stringi       1.7.6      2021-11-29 [1] CRAN (R 4.1.1)
#>  stringr       1.4.0      2019-02-10 [1] CRAN (R 4.1.1)
#>  styler        1.6.2      2021-09-23 [1] CRAN (R 4.1.1)
#>  tensorflow  * 2.7.0.9000 2021-12-07 [1] Github (rstudio/tensorflow@701d402)
#>  tfruns        1.5.0      2021-02-26 [1] CRAN (R 4.1.0)
#>  tibble        3.1.6      2021-11-07 [1] CRAN (R 4.1.1)
#>  utf8          1.2.2      2021-07-24 [1] CRAN (R 4.1.0)
#>  vctrs         0.3.8      2021-04-29 [1] CRAN (R 4.1.0)
#>  whisker       0.4        2019-08-28 [1] CRAN (R 4.1.0)
#>  withr         2.4.3      2021-11-30 [1] CRAN (R 4.1.1)
#>  xfun          0.28       2021-11-04 [1] CRAN (R 4.1.1)
#>  yaml          2.2.1      2020-02-01 [1] CRAN (R 4.1.0)
#> 
#>  [1] /Library/Frameworks/R.framework/Versions/4.1-arm64/Resources/library
#> 
#> ─ Python configuration ───────────────────────────────────────────────────────
#>  python:         /Users/nick/Library/r-miniconda-arm64/envs/r-reticulate/bin/python
#>  libpython:      /Users/nick/Library/r-miniconda-arm64/envs/r-reticulate/lib/libpython3.8.dylib
#>  pythonhome:     /Users/nick/Library/r-miniconda-arm64/envs/r-reticulate:/Users/nick/Library/r-miniconda-arm64/envs/r-reticulate
#>  version:        3.8.12 | packaged by conda-forge | (default, Oct 12 2021, 21:21:17)  [Clang 11.1.0 ]
#>  numpy:          /Users/nick/Library/r-miniconda-arm64/envs/r-reticulate/lib/python3.8/site-packages/numpy
#>  numpy_version:  1.19.5
#> 
#> ──────────────────────────────────────────────────────────────────────────────

Here's a screenshot of the installation process as well:

image

image

image

image

image

image

image

I did try and restart before I used tensorflow above - here is what happens when I try and do it again:

image
image

@kevinushey
Copy link
Collaborator

tensorflow/tensorflow#53309 looks related?

@t-kalinowski
Copy link
Member

Thank you for the issue! Looking through the log, it seems like the main notable difference between my machine and yours is I'm on macOS 12.0.1, and you're on macOS 11. The error message also suggests that that's the root cause

Seems like the Tensorflow 2.7 build from Apple requires macOS 12. I suspect that Tensorflow 2.6 might work for you on macOS 11, but sadly, I don't have an M1 Mac with macOS 11 to test on. Can you please run the following and tell me if it leaves you with a working installation?
(in terminal bash/zsh)

rm -rf ~/Library/r-miniconda-arm64
Rscript -e 'remotes::install_github("rstudio/reticulate")'
Rscript -e 'reticulate::install_miniconda()'
source ~/Library/r-miniconda-arm64/bin/activate
conda activate r-reticulate
conda install -y -c apple tensorflow-deps==2.6
pip install tensorflow-macos==2.6.0
pip install tensorflow-metal

@njtierney
Copy link
Author

OK! I'll try that now.

@njtierney
Copy link
Author

OK, I tried that, using your instructions into bash, and unfortunately it did not work:

(I restarted R before trying it)

image

I could try and install OSX 12 if you like?

@t-kalinowski
Copy link
Member

I don't want to recommend upgrading to macOS 12 for this (especially before a .1 release).
What if you specify the prior version of tensorflow-metal? Can you try this:

source ~/Library/r-miniconda-arm64/bin/activate
conda activate r-reticulate
pip install tensorflow-metal==0.2.0

@kevinushey
Copy link
Collaborator

FWIW I saw the following when trying to install Tensorflow on my M1 macOS machine:

> install_tensorflow()
Installing: tensorflow-deps
Collecting package metadata (current_repodata.json): done
Solving environment: done

# All requested packages already installed.

Collecting package metadata (current_repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.
Solving environment: failed with repodata from current_repodata.json, will retry with next repodata source.

ResolvePackageNotFound:
  - conda==4.10.3

Error: one or more Python packages failed to install [error code 1]

@kevinushey
Copy link
Collaborator

I was able to get things running after nuking my ~/miniforge3 installation and letting reticulate install + use its own copy.

@Hhannzch
Copy link

Hhannzch commented Dec 8, 2021

@t-kalinowski Thanks a lot. It works for my computer (macOS11, tensorflow==2.6.0 tensorflow-deps==2.6 tensorflow-metal==0.2.0)

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

4 participants