Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add mwe for fypp preprocessor #60

Merged
merged 20 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
eac2260
add mwe for fypp preprocessor
TomMelt Nov 3, 2023
e94e0c9
working example of _fortran only_ interface
TomMelt Nov 17, 2023
2f08e98
Add suffix to fypp functions to show dimensionality.
jatkinson1000 Nov 18, 2023
2c471db
Tidy some of fypp file and add some documentation.
jatkinson1000 Nov 18, 2023
c2e50ef
Tidy enumerator docs
jatkinson1000 Nov 18, 2023
e89507f
Add pre-commit hook file to check fypp validity.
jatkinson1000 Nov 18, 2023
424b3ed
Bring fypp and f90 in line and tidy docs.
jatkinson1000 Nov 18, 2023
7fe963b
Add fypp workflow attempt.
jatkinson1000 Nov 18, 2023
1c5b814
Allow fypp workflow to run hook using chmod +x.
jatkinson1000 Nov 18, 2023
9d4b3d7
fypp runs on all pushes, so no need to duplicate on PRs.
jatkinson1000 Nov 18, 2023
0cc57b6
Modify fypp workflow to fail if ftorch.f90 does not match expected re…
jatkinson1000 Nov 18, 2023
f53cbc7
Update README example consistent with torch_tensor_from_array approach.
jatkinson1000 Nov 18, 2023
a55cdc3
Update example 1 to use torch_tensor_from_array.
jatkinson1000 Nov 19, 2023
b3c479f
Correction to example Fortran in README to provide missing output par…
jatkinson1000 Nov 20, 2023
6616c35
Update ftorch to standardise argument order.
jatkinson1000 Nov 21, 2023
8f8345d
Remove tensor_tests as outdated and to be replaced by cgdrag examples…
jatkinson1000 Nov 21, 2023
656fa9d
Update pre-commit hook to be bash.
jatkinson1000 Nov 21, 2023
703bc5a
Create a githooks directory for pre-commit hook.
jatkinson1000 Nov 21, 2023
d74e650
Create a githooks directory for pre-commit hook
jatkinson1000 Nov 21, 2023
7943978
update order of args for torch_tensor_from_blob
TomMelt Nov 21, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 17 additions & 22 deletions examples/2_ResNet18/resnet_infer_fortran.f90
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
program inference

! Imports primitives used to interface with C
use, intrinsic :: iso_c_binding, only: c_sp=>c_float, c_dp=>c_double, c_int64_t, c_loc
use, intrinsic :: iso_fortran_env, only : sp => real32, dp => real64
use, intrinsic :: iso_fortran_env, only : sp => real32
! Import our library for interfacing with PyTorch
use :: ftorch

implicit none

! Define working precision for C primitives
! Precision must match `wp` in resnet18.py and `wp_torch` in pt2ts.py
integer, parameter :: c_wp = c_sp
integer, parameter :: wp = sp
integer, parameter :: torch_wp = torch_kFloat32

call main()

Expand All @@ -25,21 +19,21 @@ subroutine main()
integer :: num_args, ix
character(len=128), dimension(:), allocatable :: args

! Set up types of input and output data and the interface with C
! Set up types of input and output data
type(torch_module) :: model
type(torch_tensor), dimension(1) :: in_tensor
type(torch_tensor) :: out_tensor

real(c_wp), dimension(:,:,:,:), allocatable, target :: in_data
integer(c_int), parameter :: n_inputs = 1
real(c_wp), dimension(:,:), allocatable, target :: out_data
real(wp), dimension(:,:,:,:), allocatable, target :: in_data
real(wp), dimension(:,:), allocatable, target :: out_data
integer, parameter :: n_inputs = 1

integer(c_int), parameter :: in_dims = 4
integer(c_int64_t) :: in_shape(in_dims) = [1, 3, 224, 224]
integer(c_int) :: in_layout(in_dims) = [1,2,3,4]
integer(c_int), parameter :: out_dims = 2
integer(c_int64_t) :: out_shape(out_dims) = [1, 1000]
integer(c_int) :: out_layout(out_dims) = [1,2]
integer, parameter :: in_dims = 4
integer :: in_shape(in_dims) = [1, 3, 224, 224]
integer :: in_layout(in_dims) = [1,2,3,4]
integer, parameter :: out_dims = 2
integer :: out_shape(out_dims) = [1, 1000]
integer :: out_layout(out_dims) = [1,2]

! Binary file containing input tensor
character(len=*), parameter :: filename = '../data/image_tensor.dat'
Expand Down Expand Up @@ -72,8 +66,9 @@ subroutine main()
call load_data(filename, tensor_length, in_data)

! Create input/output tensors from the above arrays
in_tensor(1) = torch_tensor_from_blob(c_loc(in_data), in_dims, in_shape, torch_wp, torch_kCPU, in_layout)
out_tensor = torch_tensor_from_blob(c_loc(out_data), out_dims, out_shape, torch_wp, torch_kCPU, out_layout)
in_tensor(1) = torch_tensor_from_array(in_data, in_layout, torch_kCPU)

out_tensor = torch_tensor_from_array(out_data, out_layout, torch_kCPU)

! Load ML model (edit this line to use different models)
model = torch_module_load(args(1))
Expand Down Expand Up @@ -113,9 +108,9 @@ subroutine load_data(filename, tensor_length, in_data)

character(len=*), intent(in) :: filename
integer, intent(in) :: tensor_length
real(c_wp), dimension(:,:,:,:), intent(out) :: in_data
real(wp), dimension(:,:,:,:), intent(out) :: in_data

real(c_wp) :: flat_data(tensor_length)
real(wp) :: flat_data(tensor_length)
integer :: ios
character(len=100) :: ioerrmsg

Expand Down Expand Up @@ -166,7 +161,7 @@ subroutine calc_probs(out_data, probabilities)

implicit none

real(c_wp), dimension(:,:), intent(in) :: out_data
real(wp), dimension(:,:), intent(in) :: out_data
real(wp), dimension(:,:), intent(out) :: probabilities
real(wp) :: prob_sum

Expand Down
Loading