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

First call to a torch function causes R to crash. #1277

Closed
PM-42 opened this issue Feb 13, 2025 · 15 comments
Closed

First call to a torch function causes R to crash. #1277

PM-42 opened this issue Feb 13, 2025 · 15 comments

Comments

@PM-42
Copy link

PM-42 commented Feb 13, 2025

First call to a torch function causes R to crash. I re-installed torch but the problem persists. No other packages were loaded.
Can anybody help, please. Thanks

System Information:

OS: Win 11
R version: 4.3.1
torch package version: 0.14.1

rTorch: v 0.4.2

Problem:
I am encountering an issue where calling torch_tensor() causes R to crash. Here is a minimal example that reproduces the issue:

Install and load torch library

install.packages("torch") # this is the only package loaded
library(torch)

Ensure Lantern is loaded

torch::torch_manual_seed(42)

Create a minimal tensor

sample_data <- matrix(1:10, nrow = 2, ncol = 5) # Simple data
sample_data_tensor <- torch_tensor(sample_data, dtype = torch_long()) #!!!!!!! CRASH here

@dfalbel
Copy link
Member

dfalbel commented Feb 13, 2025

Also reported in #1273

Could you please install torch from GitHub and see if the speculative fix solves your issue.
You can do so with

remotes::install_github("mlverse/torch")

So sorry for the disruption

@PM-42
Copy link
Author

PM-42 commented Feb 13, 2025 via email

@dfalbel
Copy link
Member

dfalbel commented Feb 13, 2025

Does this error also happens when running outside of RStudio?

@dfalbel
Copy link
Member

dfalbel commented Feb 13, 2025

Also just to confirm, It's not supported to load both torch and rTorch unless they are using exactly the same builds of libtorch, which is very unlikely if you just installed them without any specific configurations/building from source.

@PM-42
Copy link
Author

PM-42 commented Feb 13, 2025 via email

@dfalbel
Copy link
Member

dfalbel commented Feb 13, 2025

I believe there's a binary incompatibility between torch and RStudio. I've tried to fix in the current dev version, but it seems it did not fix your problem right?

I'm not sure what that could be. Could you perhaps try installing a pre-built R binary using:

options(timeout = 600) # increasing timeout is recommended since we will be downloading a 2GB file.
kind <- "cpu"
version <- "0.14.1.9000
options(repos = c(
    torch = sprintf("https://torch-cdn.mlverse.org/packages/%s/%s/", kind, version),
    CRAN = "https://cloud.r-project.org" # or any other from which you want to install the other R dependencies.
))
install.packages("torch")

And see if this fixes the issue in RStudio too?

@jmlanchy
Copy link

Hi,
and thanks to dfalbel.
I had the same crashing outcome that PM-42 had: A R 4.4.2 crash in Rstudio 2024.12.0 Build 467 after running the simple line "torch_tensor(1)".
Re-doing the installation as suggested by dfalbel fixed this hurdle.
thanks again. Can't wait for the next problem!

@dfalbel
Copy link
Member

dfalbel commented Feb 13, 2025

Glad it worked for you @jmlanchy ! Sorry for the disruption!

@PM-42
Copy link
Author

PM-42 commented Feb 13, 2025 via email

@dfalbel
Copy link
Member

dfalbel commented Feb 13, 2025

There's something weird indeed. The first time you reported working on a Windows machine, but it's trying to download a linux file: https://torch-cdn.mlverse.org/packages/cpu/0.14.1.9000/src/contrib/torch_0.14.1.9000_R_x86_64-pc-linux-gnu.tar.gz

@dfalbel
Copy link
Member

dfalbel commented Feb 13, 2025

I don't know what that could be. I think I'd close all R sessions. Start a clear R session and try the installation, before loading any other package. Next test if torch works standalone. We can then see if there's any bad interaction with other packages such as huggingfaceR - which is likely if they endup loading torch using reticulate.

@PM-42
Copy link
Author

PM-42 commented Feb 13, 2025 via email

@PM-42
Copy link
Author

PM-42 commented Feb 13, 2025 via email

@dfalbel
Copy link
Member

dfalbel commented Feb 13, 2025

Awesome! Glad it worked!

@dfalbel dfalbel closed this as completed Feb 13, 2025
@depinedo
Copy link

It worked for me too, thanks! @dfalbel

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