I am extending an existing Fortran project with some machine learning routines. I was wondering if anyone has experience with existing Pytorch or Libtorch libraries for Fortran, such as FTORCH and TorchFort and which you prefer.
Hi @Albert welcome to the Fortran discourse! we’re glad you’re here. I particularly have little experience with doing machine learning in Fortran but I see that @jwallwork23 is here and might have some opinions about this. A similar project adopted by the community is also the neural-fortran project which was developed by @milancurcic for his book. A lot of people here have contributed to it!
Hi both. A group of developers of such packages including myself, Milan, and the authors of TorchFort are currently working on a comparison study that will hopefully give some helpful insight on this issue. Until the results of the study are published, perhaps we could add some preliminary thoughts here.
There are broadly three approaches to expose machine learning functionality in Fortran:
Interface to an established approach. This is what FTorch and TorchFort do - they bind to the C++ libtorch package that underpins PyTorch. Whilst binding to PyTorch brings benefits related to performance and functionality, it can also be a limiting factor, depending on what you’re trying to do.
Use a more general interfacing approach. One option is ENNUF, which supports different backends (e.g., PyTorch, TensorFlow) and automatically generates the Fortran code for inference on a model in the corresponding format. While more flexible in terms of supporting different backends, ENNUF is restricted to inference and cannot do online training (i.e. training in Fortran). A more general approach is provided by SmartSim, which can be used to couple arbitrary Python (or C/C++/Fortran) code into your Fortran program. However, the (human) learning curve associated with using SmartSim is steep.
Use a pure Fortran approach such as neural-fortran or fiats. While these packages may not implement all the functionality available in the popular Python-based deep learning packages, advantages over the other approaches include fewer dependencies, simpler build systems, and the ability to fully control everything from Fortran.
These six packages (and others out there) have different use cases so the one you choose to solve your problem depends on what it is you’re trying to do.
Here are some possible use cases:
If you want to do reinforcement learning then TorchFort has the best support.
If you want to use PyTorch models on non-Nvidia GPUs then FTorch is the one to go for.
If you have a 1D PyTorch or TensorFlow model that you want to quickly make use of in Fortran then ENNUF would be a good option.
If you want full control over arbitrary program components in both Fortran and Python then SmartSim will allow you to do this.
If you want to work in a pure Fortran ecosystem then go for neural-fortran or Fiats. Fiats requires a cutting edge compiler so your toolchain setup might be a factor in deciding which of those packages to go for.
With my FTorch developer hat on I’ll note that we have a nice set of examples to guide you from ‘Hello, ML World!’ through to advanced functionality such as GPUs, batching, and online training. FTorch has been used in several use cases in the literature.