Cuda is recognized but the gpu is not running.

username: CamilaClemente
cooked: <p>Hi, I am running my scripts on CPU and not on GPU, although it recognizes cuda when I run my scripts.</p>
<p>What can it be? is it wrongly set CUDA_HOME?</p>
<p>Is CUDA supported by this system? True<br>
CUDA version: 11.3<br>
ID of current CUDA device: 0<br>
Name of current CUDA device: NVIDIA A10</p>

username: ptrblck
cooked: <p>Could you describe your use case and issue a bit more, please?<br>
Are you seeing any errors, did you make sure to move the model and inputs to the device etc.?</p>

username: CamilaClemente
cooked: <p>Of course! in case I am not explaining myself well please let me know! I am not an expert in the subject, I am learning.</p>
<p>I don’t see any error in the output of my script, it makes me the perfect calculations but in cpu and not in gpu.<br>
I attach my script in case you identify my error.</p>
<p>Thanks!!!</p>
<pre><code class="lang-auto">from __future__ import print_function
import ase.io
from ase.io import read
from ase.io import write
from ase.md import MDLogger
from ase.io.trajectory import Trajectory
from ase.optimize import BFGS
from ase import units
import torch
import torchani

import os
import numpy as np

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print('Using device:', device)
print()

calculator = torchani.models.ANI2x().ase()

#funcion que minimiza E y devuelve el valor
def mincalc_E_ani(lig):
    atoms = read(lig)
    print(atoms)
    atoms.set_calculator(calculator)
    E=atoms.get_potential_energy()*23/len(atoms)
    opt = BFGS(atoms)
    opt.run(fmax=0.01,steps=500)
    E0=atoms.get_potential_energy()*23/len(atoms)
    write('xyz/%s_min.xyz'%lig, atoms,'xyz')
    return E,E0


#funcion que encuentra el minimo local energia de un ligando (antes era global)
def enc_min_glob(lig):
    E,E0=mincalc_E_ani('posesH_pdb/%s'%lig)
    f_out=open('energies2/%s.ene'%lig,'w')
    f_out.write('%s\n'%str(E))
    f_out.write('%s\n'%str(E0))
    f_out.close()
    
#ligandos = [ligand[:-4] for ligand in os.listdir('ligands_filt') if (ligand[-4:] == '.pdb' and '.rand' not in ligand)]
#ligandos.sort()
#print(len(ligandos))

os.system('mkdir energies2')

listaligandos=open('listaligandos')

for lig in listaligandos.readlines():
    lig=lig.rstrip('\n')
    if not os.path.isfile('energies2/%s.ene'%lig):
        a = enc_min_glob(lig)
        print('listo')
    else:
        print('yaestaba')
        
listaligandos.close()
</code></pre>

username: ptrblck
cooked: <p>Thanks for the code. You’ve never transferred any data to the GPU, so it’s expected that the default device (CPU) will be used.<br>
Move the model as well as the inputs to the GPU via:</p>
<pre><code class="lang-python">x = x.to(device)
model.to(device)
</code></pre>
<p>and the GPU should be used.</p>

username: CamilaClemente
cooked: <p>I modified the following:</p>
<p>calculator = torchani.models.ANI2x().ase()<br>
calculator = calculator.to(‘cuda’)</p>
<p>and I get the following error:</p>
<p>ttributeError: ‘Calculator’ object has no attribute ‘to’</p>
<p>What can it be?</p>

username: ptrblck
cooked: <p>You need to call <code>to('cuda')</code> on an <code>nn.Module</code> object, while <code>calculator</code> seems to be a <code>Calculator</code> object created by TorchANI. Based on <a href="https://aiqm.github.io/torchani/api.html?highlight=calculator#torchani.ase.Calculator">these docs</a> I guess something like <code>calculator.model.to('cuda')</code> might work.</p>

username: CamilaClemente
cooked: <p>Looks like it would be working! but …</p>
<p>RuntimeError: I expected all tensors to be on the same device, but it found at least two devices, cuda:0 and cpu!</p>
<p>what would I miss to move to gpu? sorry for so much trouble!</p>

username: ptrblck
cooked: <p>This error is raised if some tensors are on the GPU while others are still on the CPU. Based on your current changes I would guess you’ve properly moved the model with its parameters to the GPU while the input tensors might still be on the CPU. If so, move them via <code>x = x.to('cuda')</code> before passing them to the model.</p>