Repository navigation
Expand file tree
/
Copy path_basic_coupling.qmd
More file actions
151 lines (110 loc) · 3.78 KB
/
Copy path_basic_coupling.qmd
File metadata and controls
151 lines (110 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# Exercises
## Exercise 1
Now that we have FTorch installed on the system we can move to writing code that uses
it, needing only to link to our installation at compile and runtime.
\
We will start of with a basic example showing how to couple code in Exercise 1:
1. Design and train a PyTorch model.
2. Save PyTorch model to TorchScript.
3. Write Fortran using FTorch to call saved model.
4. Compile and run code, linking to FTorch.
## PyTorch
Examine `exercises/exercise_01/simplenet.py`.
\
This contains a contrived PyTorch model with a single `nn.Linear` layer that will
multiply the input by two.
\
With our virtual environment active we can test this by running the code with^[This will run the `if __name__ == "__main__":` section at the bottom of the file.]:
```default
python3 simplenet.py
```
```
Input: tensor([0., 1., 2., 3., 4.])
Output: tensor([0., 2., 4., 6., 8.])
```
## Offline training with FTorch
{.absolute top=20% left=20% width=60%}
## Saving to TorchScript
To use the net from Fortran we need to save our PyTorch net as TorchScript in a `.pt`
file.
\
FTorch comes with a handy utility `pt2ts` to help with this, which is installed
alongside the Python components of the library.
\
We run `simplenet.py` to produce a PyTorch model file (`.pt` file with the model
architecture and weights), then use the `pt2ts` command to convert it to
TorchScript ready for use from Fortran.
## Saving to TorchScript
We already ran
```sh
python3 simplenet.py
```
Run
```sh
ls
```
to check that the `pytorch_simplenet_input_tensor_cpu.pt` file was created.
Then convert to TorchScript using the `pt2ts` command:
```sh
pt2ts SimpleNet \
--model_definition_file simplenet.py \
--input_model_file pytorch_simplenet_model_cpu.pt \
--output_model_file torchscript_simplenet_model_cpu.pt
```
Now run
```sh
ls
```
to check that the `torchscript_simplenet_input_tensor_cpu.pt` file was created.
:::{.aside}
The `--test` flag runs a basic check that the TorchScript model produces the same
output as the PyTorch model after saving it.
:::
## Offline training with FTorch
{.absolute top=20% left=20% width=60%}
## Calling from Fortran
We are now in a state to use our saved TorchScript model from within Fortran.
`exercises/exercise_01/simplenet_fortran.f90` contains a skeleton code with a Fortran arrays
to hold input data for the net, and the results returned.
\
We will modify it to create the neccessary data structures and load and call the net.
- Import the `ftorch` module.
- Create `torch_tensors` and a `torch_model` to hold the data and net.
- Map Fortran data from arrays to the `torch_tensors`.
- Load the model and print its parameters.
- Call `torch_model_forward` to run the net.
::: aside
See the solution file `simplenet_fortran_sol.f90` for an ideal code.\
For more information on the subroutines and API see the
[online API documentation](https://cambridge-iccs.github.io/FTorch/lists/procedures.html)
:::
## Building the code
Once we have modified the Fortran we need to compile the code and link it to FTorch.
This can be done from the `exercise_01/` subdirectory using CMake as follows:
```sh
mkdir build
cd build
cmake ..
cmake --build .
```
::: aside
The path to the FTorch installation will be picked up automatically in Codespaces so
doesn't need to be specified. If building locally you may need to add FTorch to the
[`LD_LIBRARY_PATH`](https://cambridge-iccs.github.io/FTorch/page/installation/hpc.html).
:::
To run the code we can use the generated executable:
```default
./simplenet_fortran
```
```
Model parameters:
_fwd_seq.0.weight:
2 0 0 0 0
0 2 0 0 0
0 0 2 0 0
0 0 0 2 0
0 0 0 0 2
[ CPUFloatType{5,5} ]
Model output:
0.00000000 2.00000000 4.00000000 6.00000000 8.00000000
```