Cuda is recognized but the gpu is not running.

Hi, I am running my scripts on CPU and not on GPU, although it recognizes cuda when I run my scripts.

What can it be? is it wrongly set CUDA_HOME?

Is CUDA supported by this system? True
CUDA version: 11.3
ID of current CUDA device: 0
Name of current CUDA device: NVIDIA A10

Could you describe your use case and issue a bit more, please?
Are you seeing any errors, did you make sure to move the model and inputs to the device etc.?

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.

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.
I attach my script in case you identify my error.

Thanks!!!

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()

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.
Move the model as well as the inputs to the GPU via:

x = x.to(device)
model.to(device)

and the GPU should be used.

I modified the following:

calculator = torchani.models.ANI2x().ase()
calculator = calculator.to(‘cuda’)

and I get the following error:

ttributeError: ‘Calculator’ object has no attribute ‘to’

What can it be?

You need to call to('cuda') on an nn.Module object, while calculator seems to be a Calculator object created by TorchANI. Based on these docs I guess something like calculator.model.to('cuda') might work.

Looks like it would be working! but …

RuntimeError: I expected all tensors to be on the same device, but it found at least two devices, cuda:0 and cpu!

what would I miss to move to gpu? sorry for so much trouble!

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 x = x.to('cuda') before passing them to the model.