Python is popular and optimal for neural network development and training. However, many scientific codes are written in the Fortran language. How I can call a trained network in my Fortran program?
Asked
Active
Viewed 1,486 times
4
-
If you have the network in the form of a self-contained C++ function, you can call it as any other C++ function. However, make a tutorial for that is way too broad. How is a Tensorflow/Keras network called in the C++ API? – Vladimir F Героям слава Feb 08 '21 at 18:13
-
Vladimir, thank you for the answer. Pytorch network can be saved as a torchscript https://pytorch.org/docs/stable/jit.html Where I can find information on how to use a torchscript in Fortran? – Arsen Feb 08 '21 at 21:21
-
You first have to find out how to use it from C++. Then you can use general methods for calling C++ from Fortran. – Vladimir F Героям слава Feb 08 '21 at 22:10
2 Answers
1
It would not make sense. You are not training the network in Fortran, you are just trying to run the C++ or Python code from Fortran.
You should abstract the training/inference from your Fortran code. You could do the orchestration in Fortran.
- Create your model in Python
- Expose your model thru an API that you can access from Fortran via an httpRequest.
By doing that, you could expose anything you want to your Fortran app.

CrazyBrazilian
- 1,030
- 1
- 11
- 15
-
It would make sense. It is perfectly legitimate to desire to use a trained model in C++ or Fortran. What has http have to do with that? As in https://stackoverflow.com/questions/45027394/running-trained-tensorflow-model-in-c https://stackoverflow.com/questions/36720498/convert-keras-model-to-c Once one has a working C++ function, calling it from Fortran is just routine. – Vladimir F Героям слава Feb 09 '21 at 07:13
-
de-coupling the ML code from the Fortran code would be much easier. You can always couple it via an httpRequest. Unless there are Fortran libraries which are not available in tensorflow/python, I would not do that ( i have done a lot of Fortran and I don't think that is the case) – CrazyBrazilian Feb 09 '21 at 16:14
-
But why? If you can do it in C++, calling the C++ function from Fortran is just a technicality. Http stuff is very rarely done in Fortran, there is not even enough support in the language. Unlike for calling C. – Vladimir F Героям слава Feb 09 '21 at 16:16
-
yes, of course you can do. it is not technically difficult since c++ is compiled and well documented, so marshalling the data and coupling the calls is possible. the main question is why. Based on the question, I'm proposing a solution that could be easier. – CrazyBrazilian Feb 09 '21 at 16:34
-
@VladimirF thank you for your help. Still, I do not have a clear picture of what I shall do. I was able to call my torchscript from a C++ program (as described in the basic tutorial), which I compiled using CMake on Windows. My Fortran code is large (fluid dynamics solver) and is being compiled via gmake. Could you please elaborate more on this? – Arsen Feb 10 '21 at 01:53
-
@Arsen It is hard o say what you do and what you do not know. You certainly need to know how to call C from Fortran in general. Do you know `bind(C)` and `iso_c_binding`? If not, you need to learn it first. There are many tutorials on the web and many q/a at [tag:fortran-iso-c-binding]. – Vladimir F Героям слава Feb 10 '21 at 09:20
-
@Arsen Though not sure, it might be useful to post the question also here https://fortran-lang.discourse.group/ (hoping that someone may be familiar with pytorch + its use from C++ or Fortran). – roygvib Feb 10 '21 at 18:06
1
I was able to call a Torchscript from Fortran through C++ (using g++). See how I did this here: Libtorch works with g++, but fails with Intel compiler However, this does not work for Intel Compiler.

Arsen
- 131
- 10
-
The Intel Compiler also worked. It is just necessary to download the cxx11 ABI (error was with pre-cxx11 ABI). – Arsen Feb 14 '21 at 04:12
-
So, you called Pytourched from Python to C++ then send it to Fortran, Is it correct? Are you even trying this Forpy project on GitHub? 'https://github.com/ylikx/forpy' – ALIN Nov 03 '21 at 08:28
-
No, I called torchscript (c++ backend of pytorch) from c++ and passed this to fortran. – Arsen Nov 03 '21 at 23:50