MOFA2 icon indicating copy to clipboard operation
MOFA2 copied to clipboard

How to specify GPU device in MOFA2

Open yulijia opened this issue 2 years ago • 5 comments

Hi, I run MOFA2 in R with reticulate. I have 2 GPUs in my machine, do you know how to specify the GPU device to run MOFA2? Usually, in conda environment, I can set up the GPU device by export CUDA_VISIBLE_DEVICES=1,0 or CUDA_VISIBLE_DEVICES=1 ./cuda_executable But I don't know how to do it in R.

yulijia avatar Sep 03 '22 15:09 yulijia

Hey @yulijia,

I think you can still set environmental variables in R. Not sure if that is going to work but we'll make it even easier with the upcoming change in the Python backend: https://github.com/bioFAM/mofapy2/issues/9.

Would a train_opts$gpu_device be something that you consider a good solution for your use case?

gtca avatar Sep 05 '22 15:09 gtca

Hi @gtca , Thank you for your quick response! Unfortunately, set environmental variables in R doesn't work in Rstudio server.

I think train_opts$gpu_device may the best choice for specific GPU devices in the R package. If it is possible, please also consider something like model = nn.DataParallel(model).cuda() to implement data parallelism on all GPU devices when the memory of single GPU is not enough to train the model.

yulijia avatar Sep 05 '22 23:09 yulijia

Hi @gtca

I recently updated the mofapy2 package (0.6.4 -> 0.7.0) on my server and run the package using mofa r package with GPU.

However, it is never run on GPU anymore.

I have installed Cupy, CUDA Toolkit, dwf-python, and mofapy2. However, it seems that regardless of the specified gpu_device number, the code is defaulting to the CPU version. But Previous version, I ran it on GPU successfully.

Do you know how to solve the problem?

Here is my code to run mofa.

## cbmc is a SeuratData, https://github.com/satijalab/seurat-data

MOFAobject <- create_mofa(cbmc)

data_opts <- get_default_data_options(MOFAobject)
model_opts <- get_default_model_options(MOFAobject)
train_opts <- get_default_training_options(MOFAobject)
train_opts$gpu_mode <- TRUE
train_opts$gpu_device=1

MOFAobject <- prepare_mofa(
  object = MOFAobject,
  data_options = data_opts,
  model_options = model_opts,
  training_options = train_opts
)

keyword="cbmc"
outfile =  paste0("./",keyword,"_model.hdf5")

use_condaenv(condaenv = "mofa2",conda="/home/ylj/miniconda3/bin/conda",required=T)
use_python("/home/ylj/miniconda3/envs/mofa2/bin/python3.8",required = T)

py_module_available("mofapy2") ##return TRUE
.mofapy2_version <- "0.7.0"

MOFA2:::.run_mofa_reticulate(MOFAobject, outfile, save_data=TRUE)## throw error at this line

MOFAobject.trained <- load_model(outfile)
factors <- get_factors(MOFAobject.trained, factors = "all")

Here are the packages that in my conda env

_libgcc_mutex             0.1                 conda_forge    conda-forge
_openmp_mutex             4.5                       2_gnu    conda-forge
anndata                   0.8.0                    pypi_0    pypi
argparse                  1.4.0                    pypi_0    pypi
brotli-python             1.1.0            py38h17151c0_1    conda-forge
bzip2                     1.0.8                hd590300_5    conda-forge
ca-certificates           2023.11.17           hbcca054_0    conda-forge
certifi                   2023.11.17         pyhd8ed1ab_0    conda-forge
charset-normalizer        3.3.2              pyhd8ed1ab_0    conda-forge
cudatoolkit               11.8.0              h4ba93d1_12    conda-forge
cupy-cuda110              12.2.0                   pypi_0    pypi
dtw-python                1.3.0            py38h01eb140_2    conda-forge
fastrlock                 0.8.2                    pypi_0    pypi
h5py                      3.10.0                   pypi_0    pypi
idna                      3.5                pyhd8ed1ab_0    conda-forge
joblib                    1.3.2                    pypi_0    pypi
ld_impl_linux-64          2.40                 h41732ed_0    conda-forge
libblas                   3.9.0           20_linux64_openblas    conda-forge
libcblas                  3.9.0           20_linux64_openblas    conda-forge
libffi                    3.4.2                h7f98852_5    conda-forge
libgcc-ng                 13.2.0               h807b86a_3    conda-forge
libgfortran-ng            13.2.0               h69a702a_3    conda-forge
libgfortran5              13.2.0               ha4646dd_3    conda-forge
libgomp                   13.2.0               h807b86a_3    conda-forge
liblapack                 3.9.0           20_linux64_openblas    conda-forge
libnsl                    2.0.1                hd590300_0    conda-forge
libopenblas               0.3.25          pthreads_h413a1c8_0    conda-forge
libsqlite                 3.44.2               h2797004_0    conda-forge
libstdcxx-ng              13.2.0               h7e041cc_3    conda-forge
libuuid                   2.38.1               h0b41bf4_0    conda-forge
libzlib                   1.2.13               hd590300_5    conda-forge
mofapy2                   0.7.0                    pypi_0    pypi
natsort                   8.4.0                    pypi_0    pypi
ncurses                   6.4                  h59595ed_2    conda-forge
numpy                     1.24.4           py38h59b608b_0    conda-forge
openssl                   3.2.0                hd590300_0    conda-forge
packaging                 23.2               pyhd8ed1ab_0    conda-forge
pandas                    1.5.3                    pypi_0    pypi
pip                       23.3.1             pyhd8ed1ab_0    conda-forge
platformdirs              4.0.0              pyhd8ed1ab_0    conda-forge
pooch                     1.8.0              pyhd8ed1ab_0    conda-forge
pysocks                   1.7.1              pyha2e5f31_6    conda-forge
python                    3.8.18          hd12c33a_0_cpython    conda-forge
python-dateutil           2.8.2                    pypi_0    pypi
python_abi                3.8                      4_cp38    conda-forge
pytz                      2023.3.post1             pypi_0    pypi
readline                  8.2                  h8228510_1    conda-forge
requests                  2.31.0             pyhd8ed1ab_0    conda-forge
scikit-learn              1.3.2                    pypi_0    pypi
scipy                     1.10.1           py38h59b608b_3    conda-forge
setuptools                68.2.2             pyhd8ed1ab_0    conda-forge
six                       1.16.0                   pypi_0    pypi
threadpoolctl             3.2.0                    pypi_0    pypi
tk                        8.6.13          noxft_h4845f30_101    conda-forge
typing_extensions         4.8.0              pyha770c72_0    conda-forge
urllib3                   2.1.0              pyhd8ed1ab_0    conda-forge
wheel                     0.41.3             pyhd8ed1ab_0    conda-forge
xz                        5.2.6                h166bdaf_0    conda-forge

yulijia avatar Nov 25 '23 07:11 yulijia

Hi @gtca ,

Problem solved, i forget to install cupy...

so the correct packages in env should includes:

_libgcc_mutex             0.1                 conda_forge    conda-forge
_openmp_mutex             4.5                       2_gnu    conda-forge
anndata                   0.9.2                    pypi_0    pypi
bzip2                     1.0.8                hd590300_5    conda-forge
ca-certificates           2023.11.17           hbcca054_0    conda-forge
cuda-version              11.8                 h70ddcb2_2    conda-forge
cudatoolkit               11.8.0              h4ba93d1_12    conda-forge
cupy                      12.2.0           py38h7b7cd4b_4    conda-forge
cupy-cuda11x              12.2.0                   pypi_0    pypi
fastrlock                 0.8.2            py38h17151c0_1    conda-forge
h5py                      3.10.0                   pypi_0    pypi
joblib                    1.3.2                    pypi_0    pypi
ld_impl_linux-64          2.40                 h41732ed_0    conda-forge
libblas                   3.9.0           20_linux64_openblas    conda-forge
libcblas                  3.9.0           20_linux64_openblas    conda-forge
libffi                    3.4.2                h7f98852_5    conda-forge
libgcc-ng                 13.2.0               h807b86a_3    conda-forge
libgfortran-ng            13.2.0               h69a702a_3    conda-forge
libgfortran5              13.2.0               ha4646dd_3    conda-forge
libgomp                   13.2.0               h807b86a_3    conda-forge
liblapack                 3.9.0           20_linux64_openblas    conda-forge
libnsl                    2.0.1                hd590300_0    conda-forge
libopenblas               0.3.25          pthreads_h413a1c8_0    conda-forge
libsqlite                 3.44.2               h2797004_0    conda-forge
libstdcxx-ng              13.2.0               h7e041cc_3    conda-forge
libuuid                   2.38.1               h0b41bf4_0    conda-forge
libzlib                   1.2.13               hd590300_5    conda-forge
mofapy2                   0.7.1                    pypi_0    pypi
natsort                   8.4.0                    pypi_0    pypi
ncurses                   6.4                  h59595ed_2    conda-forge
numpy                     1.24.4           py38h59b608b_0    conda-forge
openssl                   3.2.0                hd590300_0    conda-forge
packaging                 23.2                     pypi_0    pypi
pandas                    2.0.3                    pypi_0    pypi
pip                       23.3.1             pyhd8ed1ab_0    conda-forge
python                    3.8.18          hd12c33a_0_cpython    conda-forge
python-dateutil           2.8.2                    pypi_0    pypi
python_abi                3.8                      4_cp38    conda-forge
pytz                      2023.3.post1             pypi_0    pypi
readline                  8.2                  h8228510_1    conda-forge
scikit-learn              1.3.2                    pypi_0    pypi
scipy                     1.10.1                   pypi_0    pypi
setuptools                68.2.2             pyhd8ed1ab_0    conda-forge
six                       1.16.0                   pypi_0    pypi
threadpoolctl             3.2.0                    pypi_0    pypi
tk                        8.6.13          noxft_h4845f30_101    conda-forge
tzdata                    2023.3                   pypi_0    pypi
wheel                     0.41.3             pyhd8ed1ab_0    conda-forge
xz                        5.2.6                h166bdaf_0    conda-forge

yulijia avatar Nov 25 '23 12:11 yulijia

@yulijia Glad it got resolved!

gtca avatar Nov 27 '23 14:11 gtca