LibLantern issue during shiny deployment of R app using torch.

The app runs and works locally and utilizes a torch based NN, but when deployed via shiny won't predict and just disconnects from the server owing to an error message: Warning: Error in : Lantern is not loaded. Please use install_torch() to install additional dependencies. I did see this earlier post(Problem with LibLantern library during rsconnect deployment of R torch-based app.) but trying this:

Sys.setenv(TORCH_HOME='/srv/connect/apps/appname')
library(torch)

did not work

1 Like

You might need to set both:

Sys.setenv(TORCH_INSTALL=1)
Sys.setenv(TORCH_HOME=path.expand("libtorch/"))

torch doesn't automatically install the additional dependencies if not running in interactive mode.

1 Like

So i get this error:

:information_source: Additional software needs to be downloaded and installed for torch to work correctly.
trying URL 'https://storage.googleapis.com/torch-lantern-builds/binaries/refs/heads/cran/v0.11.0/latest/lantern-0.11.0+cpu+x86_64-Linux.zip'
downloaded 5.4 MB
Warning: :information_source: torch failed to start, restart your R session to try again.
:information_source: You might need to reinstall torch using install_torch()
:heavy_multiplication_x: /srv/connect/apps/appname/libtorch/lib/liblantern.so -
/srv/connect/apps/appname/libtorch/lib/libtorch_cpu.so: cannot read file data
Caused by error in cpp_lantern_init():
! /srv/connect/apps/appname/libtorch/lib/liblantern.so - /srv/connect/apps/appname/libtorch/lib/libtorch_cpu.so: cannot read file data

I used as you mentioned these lines:

Sys.setenv(TORCH_INSTALL=1)
Sys.setenv(TORCH_HOME=path.expand("libtorch/"))
library(torch)

did I perhaps miss additional needed code?

You have to make sure that the directory pointed by TORCH_HOME is both writeable and readable when the app is launched, and that it's allowed to download files. Depending on the server configuration this directory might not be writable, can you try installing the additional deps into the app directory with something like:

Sys.setenv(TORCH_HOME=path.expand("libtorch/"))

Yeah I get this error still:
Warning: Error in : Lantern is not loaded. Please use install_torch() to install additional dependencies.

I have tried the above as well as this but no luck:

Sys.setenv(TORCH_HOME=path.expand("libtorch/"))
library(torch)
install_torch()
Sys.setenv(TORCH_INSTALL=1)
library(torch)

The env vars must be set before torch loads:

Sys.setenv(TORCH_INSTALL=1)
Sys.setenv(TORCH_HOME=path.expand("libtorch/"))
library(torch)

Other packages might trigger a torch loading (eg, if you load a package that depends on torch), So i suggest using this the first thing in your app.

1 Like

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.