Loading Tensorflow with Reticulate crashes R when loading - illegal operand

79 Views Asked by At

I am trying to load Tensorflow in RStudio and everytime it crashes with the following error:

"ERROR The previous R session terminated abnormally; LOGGED FROM: rstudio::core::Error {anonymous}::rInit(const rstudio::r::session::RInitInfo&) src/cpp/session/SessionMain.cpp:728"

If I bypass RStudio, I get more details:

> repl_python()
Python 3.10.12 (/home/jon/.virtualenvs/test-1-env/bin/python)
Reticulate 1.34.0 REPL -- A Python interpreter in R.
Enter 'exit' or 'quit' to exit the REPL and return to R.
>>> import tensorflow

 *** caught illegal operation ***
address 0x7fb5c8bb2ecb, cause 'illegal operand'

Traceback:
 1: py_call_impl(callable, call_args$unnamed, call_args$named)
 2: builtins$eval(compiled, globals, locals)
 3: py_compile_eval(code, capture = FALSE)
 4: doTryCatch(return(expr), name, parentenv, handler)
 5: tryCatchOne(expr, names, parentenv, handlers[[1L]])
 6: tryCatchList(expr, names[-nh], parentenv, handlers[-nh])
 7: doTryCatch(return(expr), name, parentenv, handler)
 8: tryCatchOne(tryCatchList(expr, names[-nh], parentenv, handlers[-nh]),     names[nh], parentenv, handlers[[nh]])
 9: tryCatchList(expr, classes, parentenv, handlers)
10: tryCatch(py_compile_eval(code, capture = FALSE), error = handle_error,     interrupt = handle_interrupt)
11: repl()
12: doTryCatch(return(expr), name, parentenv, handler)
13: tryCatchOne(expr, names, parentenv, handlers[[1L]])
14: tryCatchList(expr, classes, parentenv, handlers)
15: tryCatch(repl(), interrupt = identity)
16: repl_python()

I installed tensorflow into a virtual environment as such:

virtualenv_install("test-1-env", "tensorflow")

Looks like it installed right:

Using virtual environment 'test-1-env' ...
+ /home/jon/.virtualenvs/test-1-env/bin/python -m pip install --upgrade --no-user tensorflow
Requirement already satisfied: tensorflow in /home/jon/.virtualenvs/test-1-env/lib/python3.10/site-packages (2.15.0)
...
Requirement already satisfied: oauthlib>=3.0.0 in /home/jon/.virtualenvs/test-1-env/lib/python3.10/site-packages (from requests-oauthlib>=0.7.0->google-auth-oauthlib<2,>=0.5->tensorboard<2.16,>=2.15->tensorflow) (3.2.2)

Here's my setup:

> sessionInfo()
R version 4.3.2 (2023-10-31)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 22.04.3 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3 
LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.20.so;  LAPACK version 3.10.0

locale:
 [1] LC_CTYPE=C.UTF-8       LC_NUMERIC=C           LC_TIME=C.UTF-8        LC_COLLATE=C.UTF-8    
 [5] LC_MONETARY=C.UTF-8    LC_MESSAGES=C.UTF-8    LC_PAPER=C.UTF-8       LC_NAME=C             
 [9] LC_ADDRESS=C           LC_TELEPHONE=C         LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C   

time zone: America/New_York
tzcode source: system (glibc)

attached base packages:
[1] stats     graphics  grDevices datasets  utils     methods   base     

other attached packages:
 [1] arrow_14.0.0.2    dplyr_1.1.4       reticulate_1.34.0 plotly_4.10.3     ggplot2_3.4.4    
 [6] nat_1.10.4        rgl_1.2.8         scales_1.3.0      cgalMeshes_2.2.0  knitr_1.45       

1

There are 1 best solutions below

0
On BEST ANSWER

I ran Tensorflow directly from my Python envrionment and got the following output:

(test-1-env) jon@rstudio:~$ python3
Python 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow
Illegal instruction (core dumped)

The Illegal instruction output led me to this thread which reminded me of a similar issue I had previously when running MongoDB in a VM. I dropped into the VM config and passed through the Host CPU and reran from Python3. I got this:

>>> import tensorflow
2023-12-04 22:11:23.707476: I external/local_tsl/tsl/cuda/cudart_stub.cc:31] Could not find cuda drivers on your machine, GPU will not be used.
2023-12-04 22:11:24.967510: E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:9261] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered
2023-12-04 22:11:24.967591: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:607] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered
2023-12-04 22:11:25.232737: E external/local_xla/xla/stream_executor/cuda/cuda_blas.cc:1515] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered
2023-12-04 22:11:25.797885: I external/local_tsl/tsl/cuda/cudart_stub.cc:31] Could not find cuda drivers on your machine, GPU will not be used.
2023-12-04 22:11:25.803339: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
2023-12-04 22:11:28.099383: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT

And suddenly, R doesn't crash when I try to load it!