使用Reticulate加载TensorFlow时R崩溃:非法操作数错误
在RStudio加载TensorFlow时崩溃的问题
现象
在RStudio中加载TensorFlow时,会话直接异常终止,报错:
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
绕过RStudio,用repl_python()直接调用Python环境导入TensorFlow时,得到更详细的错误:
> 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()
安装方式
通过reticulate在虚拟环境中安装TensorFlow:
virtualenv_install("test-1-env", "tensorflow")
安装过程无异常,日志显示:
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)
环境配置
> 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. 检查CPU指令集兼容性
illegal operand报错大多是因为TensorFlow预编译版本用到了CPU不支持的指令集(如AVX2、FMA)。Ubuntu默认安装的TensorFlow针对新CPU优化,若你的CPU不支持这些指令就会触发错误。
用以下命令检查CPU支持的指令集:
cat /proc/cpuinfo | grep flags
如果输出里没有avx2、fma字段,说明需要安装兼容旧CPU的TensorFlow版本。
2. 安装兼容旧CPU的TensorFlow
进入虚拟环境:
source /home/jon/.virtualenvs/test-1-env/bin/activate
安装兼容版本(以2.15.0为例):
pip install tensorflow==2.15.0 --no-binary :all:
也可以通过conda-forge安装预编译的兼容版:
conda install -c conda-forge tensorflow=2.15.0
3. 验证Reticulate环境配置
确保Reticulate正确指向目标虚拟环境:
library(reticulate) use_virtualenv("test-1-env", required = TRUE) # 检查Python环境配置 py_config() # 尝试导入TensorFlow验证 tf <- import("tensorflow") tf$`__version__`
4. 禁用TensorFlow的CPU优化
若安装兼容版后仍有问题,可尝试禁用CPU优化:
Sys.setenv(TF_CPP_MIN_LOG_LEVEL = "2") Sys.setenv(TF_ENABLE_ONEDNN_OPTS = "0") library(reticulate) use_virtualenv("test-1-env") tf <- import("tensorflow")
5. 排查RStudio兼容性
- 更新RStudio到最新版本
- 禁用RStudio中所有无关插件后重试
- 用终端启动R,再加载TensorFlow,排查是否是RStudio自身的问题
内容的提问来源于stack exchange,提问作者myfatson
相关产品推荐
相关产品推荐

