FTorch icon indicating copy to clipboard operation
FTorch copied to clipboard

Investigate creating tensors directly on GPU

Open ElliottKasoar opened this issue 2 years ago • 2 comments

Currently, data is (probably) explicitly moved to the target device, rather than created directly on the desired device, as is preferable.

Creating the tensor directly on the device would be closer to the previous form of the code (see changes for GPUs), although that exact implementation did not seem to work.

ElliottKasoar avatar Dec 15 '23 20:12 ElliottKasoar

@niccolozanotti did you look at this at some point, or am I imagining that?

Just recapping old issues. I wonder if this is something we'd ever want given data is always coming to/from Fortran. It may be useful for @jwallwork23 with autograd functionality perhaps...?

jatkinson1000 avatar Jul 23 '25 13:07 jatkinson1000

@jatkinson1000 So, I've looked and from the time of writing of this things have changed a bit. We now support creating a tensor directly on the GPU through torch_tensor_zeros or torch_tensor_ones. This is because we don't call torch::to anymore in torch_zeros and torch_ones (see here compared to this referenced in the issue). However, this approach does not link a fortran array as torch_tensor_from_array which has to call torch::to from within torch_from_blob since data is coming from CPU.

Also, to recap before closing, in #360 we added torch_tensor_to which, given a source torch_tensor and a target_tensor (ftorch derived types) will move the source one to the target device and dtype. This is the other way to move between devices.

niccolozanotti avatar Jul 23 '25 13:07 niccolozanotti