# How to let TMVA & pytorch to use GPU?

**URL:** <https://root-forum.cern.ch/t/how-to-let-tmva-pytorch-to-use-gpu/59088>\
**Category:** TMVA\
**Created:** [April 22, 2024, 3:37pm UTC](https://root-forum.cern.ch/t/how-to-let-tmva-pytorch-to-use-gpu/59088 "2024-04-22T15:37:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![phoenixAspies](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/phoenixaspies/32/25456_2.png) [@phoenixAspies](https://root-forum.cern.ch/u/phoenixAspies)\
**Post date:** [April 22, 2024, 3:37pm UTC](https://root-forum.cern.ch/t/how-to-let-tmva-pytorch-to-use-gpu/59088/1 "2024-04-22T15:37:24Z")

</div>

Hi, my current working environment is ROOT 6.26.14, Python 3.8.19, Windows 10, Pytorch 2.2

I have decided to start my work based on the example code(“tutorials\tmva\pytorch\RegressionPyTorch.py”) given in the tutorial. It does work well, but I found that this code does not use GPU and only works with the CPU. However, PyTorch is clearly capable of working with the GPU, so I tried to insert **model.cuda()** in the middle, and it resulted in an error when running it again. The error message is listed as below:

* * *

```
                     : Option SaveBestOnly: Only model weights with smallest validation loss will be stored

```

: Failed to run python code: trained\_model = fit(model, train\_loader, val\_loader, num\_epochs=numEpochs, batch\_size=batchSize,optimizer=optimizer, criterion=criterion, save\_best=save\_best, scheduler=(schedule, schedulerSteps))  
 : Python error message:  
Traceback (most recent call last):  
File “”, line 1, in   
File “RegressionPyTorch.py”, line 87, in train  
output = model(X)  
File “C:\Users\phoenixAspies.conda\envs\Python38\lib\site-packages\torch\nn\modules\module.py”, line 1511, in \_wrapped\_call\_impl  
return self.\_call\_impl(\*args, \*\*kwargs)  
File “C:\Users\phoenixAspies.conda\envs\Python38\lib\site-packages\torch\nn\modules\module.py”, line 1520, in \_call\_impl  
return forward\_call(\*args, \*\*kwargs)

* * *

I believe my PyTorch and CUDA settings are correct, as when I directly use “import torch; x = torch.randn(4,4); x.cuda()”, the variable is successfully placed on the GPU.

Does anyone know how to let GPU work when using TMVA & PyTorch?

---

<div class="post-metadata">

**Author:** ![vpadulan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/vpadulan/32/25315_2.png) [@vpadulan](https://root-forum.cern.ch/u/vpadulan)\
**Post date:** [April 22, 2024, 8:58pm UTC](https://root-forum.cern.ch/t/how-to-let-tmva-pytorch-to-use-gpu/59088/2 "2024-04-22T20:58:26Z")

</div>

Dear @phoenixAspies ,

Thanks for reaching out to the forum!

I believe @moneta can comment regarding your issue.

Cheers,  
Vincenzo

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [April 24, 2024, 10:38am UTC](https://root-forum.cern.ch/t/how-to-let-tmva-pytorch-to-use-gpu/59088/3 "2024-04-24T10:38:35Z")

</div>

Hello,

I think you need to transfer to the device (the GPU) both the input data and the model. In the case of the RegressionPyTorch function, you need to modify the train and predict functions for this.

Lorenzo

---

<div class="post-metadata">

**Author:** ![phoenixAspies](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/phoenixaspies/32/25456_2.png) [@phoenixAspies](https://root-forum.cern.ch/u/phoenixAspies)\
**Post date:** [April 24, 2024, 3:49pm UTC](https://root-forum.cern.ch/t/how-to-let-tmva-pytorch-to-use-gpu/59088/4 "2024-04-24T15:49:55Z")

</div>

Thanks! But I don’t know how to let transfer the data which TMVA proceeds to GPU, for example, in pytorch, I can simply write model.cuda() / data.cuda(). Is there any function in TMVA to do that?

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [April 24, 2024, 5:09pm UTC](https://root-forum.cern.ch/t/how-to-let-tmva-pytorch-to-use-gpu/59088/5 "2024-04-24T17:09:54Z")

</div>

Hi,  
TMVA has native functions to transfer to CUDA, but these work for training using a model build using TMVA directly (with its DL package).  
In case of PyTorch you would need to call data.cuda() in the train and predict functions, in this way the data should be transfer to GPU.

Lorenzo

---

<div class="post-metadata">

**Author:** ![phoenixAspies](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/phoenixaspies/32/25456_2.png) [@phoenixAspies](https://root-forum.cern.ch/u/phoenixAspies)\
**Post date:** [April 24, 2024, 7:11pm UTC](https://root-forum.cern.ch/t/how-to-let-tmva-pytorch-to-use-gpu/59088/6 "2024-04-24T19:11:40Z")

</div>

Thanks! I found that adding .cuda() to part of the pytorch code is enough to run on GPU now.
