From 237c096d9a733e9741fe19d02d2da286cfdeb7a9 Mon Sep 17 00:00:00 2001 From: melt Date: Wed, 19 Jun 2024 12:44:19 +0100 Subject: [PATCH 1/4] add windows support to CMake --- src/CMakeLists.txt | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d309ef20..6265c478 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -3,6 +3,11 @@ set(PROJECT_NAME FTorch) set(LIB_NAME ftorch) set(PACKAGE_VERSION 0.1) +if (WIN32) + set (CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS TRUE) + set (BUILD_SHARED_LIBS TRUE) +endif () + project(${PROJECT_NAME} VERSION ${PACKAGE_VERSION} LANGUAGES C CXX Fortran) include(FortranCInterface) From dab010428443b6dfa5483f78b19a99c67140c739 Mon Sep 17 00:00:00 2001 From: melt Date: Wed, 19 Jun 2024 12:44:47 +0100 Subject: [PATCH 2/4] add temporary fix to example 1 I am debugging issue with windows user. I noticed the latest version of ifx/ifort builds with a different integer type which leads to error: ``` error #6284: There is no matching specific function for this generic function reference. [TORCH_TENSOR_FROM_ARRAY] in_tensors(1) = torch_tensor_from_array(in_data, tensor_layout, torch_kCPU) -------------------^ ``` This can be removed by forcing the integer type to int64 --- examples/1_SimpleNet/simplenet_infer_fortran.f90 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/1_SimpleNet/simplenet_infer_fortran.f90 b/examples/1_SimpleNet/simplenet_infer_fortran.f90 index aacf06b7..48c3b767 100644 --- a/examples/1_SimpleNet/simplenet_infer_fortran.f90 +++ b/examples/1_SimpleNet/simplenet_infer_fortran.f90 @@ -1,7 +1,7 @@ program inference ! Import precision info from iso - use, intrinsic :: iso_fortran_env, only : sp => real32 + use, intrinsic :: iso_fortran_env, only : sp => real32, int64 ! Import our library for interfacing with PyTorch use ftorch @@ -17,7 +17,7 @@ program inference ! Set up Fortran data structures real(wp), dimension(5), target :: in_data real(wp), dimension(5), target :: out_data - integer :: tensor_layout(1) = [1] + integer(int64) :: tensor_layout(1) = [1] ! Set up Torch data structures ! The net, a vector of input tensors (in this case we only have one), and the output tensor From 9b44d955cc8457b3ad6859c13041ac123c6f5bc6 Mon Sep 17 00:00:00 2001 From: melt Date: Fri, 13 Sep 2024 15:12:20 +0100 Subject: [PATCH 3/4] feat: set integer size for FTorch FT_INT Integer size for FTorch can now be controlled using parameter `FT_INT`. Currently, the default is set to `int32` but this could be changed if required. --- src/ftorch.f90 | 102 ++++++++++++++++++++++++------------------------ src/ftorch.fypp | 10 +++-- 2 files changed, 58 insertions(+), 54 deletions(-) diff --git a/src/ftorch.f90 b/src/ftorch.f90 index 0009ed0c..5f1bdfd0 100644 --- a/src/ftorch.f90 +++ b/src/ftorch.f90 @@ -14,6 +14,8 @@ module ftorch implicit none + integer, parameter :: FT_INT = int32 ! set integer size for FTorch library + !> Type for holding a torch neural net (nn.Module). type torch_model type(c_ptr) :: p = c_null_ptr !! pointer to the neural net in memory @@ -280,7 +282,7 @@ end function torch_tensor_get_device_index !> Deallocates an array of tensors. subroutine torch_tensor_array_delete(tensor_array) type(torch_tensor), dimension(:), intent(inout) :: tensor_array - integer :: i + integer(FT_INT) :: i ! use bounds rather than (1, N) because it's safer do i = lbound(tensor_array, dim=1), ubound(tensor_array, dim=1) @@ -373,7 +375,7 @@ subroutine torch_model_forward(model, input_tensors, output_tensors, requires_gr logical, optional, intent(in) :: requires_grad !! Whether gradients need to be computed for the created tensor logical :: requires_grad_value !! Whether gradients need to be computed for the created tensor - integer :: i + integer(FT_INT) :: i integer(c_int) :: n_inputs integer(c_int) :: n_outputs type(c_ptr), dimension(size(input_tensors)), target :: input_ptrs @@ -443,7 +445,7 @@ subroutine torch_tensor_from_array_int8_1d(tensor, data_in, layout, & ! inputs integer(kind=int8), intent(in), target :: data_in(:) !! Input data that tensor will point at - integer, intent(in) :: layout(1) !! Control order of indices + integer(FT_INT), intent(in) :: layout(1) !! Control order of indices integer(c_int), intent(in) :: c_device_type !! Device type the tensor will live on (`torch_kCPU` or `torch_kCUDA`) integer(c_int), optional, intent(in) :: device_index !! device index to use for `torch_kCUDA` case logical, optional, intent(in) :: requires_grad !! Whether gradients need to be computed for the created tensor @@ -453,7 +455,7 @@ subroutine torch_tensor_from_array_int8_1d(tensor, data_in, layout, & integer(c_int), parameter :: c_dtype = torch_kInt8 !! Data type integer(c_int64_t) :: strides(1) !! Strides for accessing data integer(c_int), parameter :: ndims = 1 !! Number of dimension of input data - integer :: i + integer(FT_INT) :: i integer(c_int) :: device_index_value logical :: requires_grad_value !! Whether gradients need to be computed for the created tensor @@ -497,7 +499,7 @@ subroutine torch_tensor_from_array_int8_2d(tensor, data_in, layout, & ! inputs integer(kind=int8), intent(in), target :: data_in(:,:) !! Input data that tensor will point at - integer, intent(in) :: layout(2) !! Control order of indices + integer(FT_INT), intent(in) :: layout(2) !! Control order of indices integer(c_int), intent(in) :: c_device_type !! Device type the tensor will live on (`torch_kCPU` or `torch_kCUDA`) integer(c_int), optional, intent(in) :: device_index !! device index to use for `torch_kCUDA` case logical, optional, intent(in) :: requires_grad !! Whether gradients need to be computed for the created tensor @@ -507,7 +509,7 @@ subroutine torch_tensor_from_array_int8_2d(tensor, data_in, layout, & integer(c_int), parameter :: c_dtype = torch_kInt8 !! Data type integer(c_int64_t) :: strides(2) !! Strides for accessing data integer(c_int), parameter :: ndims = 2 !! Number of dimension of input data - integer :: i + integer(FT_INT) :: i integer(c_int) :: device_index_value logical :: requires_grad_value !! Whether gradients need to be computed for the created tensor @@ -551,7 +553,7 @@ subroutine torch_tensor_from_array_int8_3d(tensor, data_in, layout, & ! inputs integer(kind=int8), intent(in), target :: data_in(:,:,:) !! Input data that tensor will point at - integer, intent(in) :: layout(3) !! Control order of indices + integer(FT_INT), intent(in) :: layout(3) !! Control order of indices integer(c_int), intent(in) :: c_device_type !! Device type the tensor will live on (`torch_kCPU` or `torch_kCUDA`) integer(c_int), optional, intent(in) :: device_index !! device index to use for `torch_kCUDA` case logical, optional, intent(in) :: requires_grad !! Whether gradients need to be computed for the created tensor @@ -561,7 +563,7 @@ subroutine torch_tensor_from_array_int8_3d(tensor, data_in, layout, & integer(c_int), parameter :: c_dtype = torch_kInt8 !! Data type integer(c_int64_t) :: strides(3) !! Strides for accessing data integer(c_int), parameter :: ndims = 3 !! Number of dimension of input data - integer :: i + integer(FT_INT) :: i integer(c_int) :: device_index_value logical :: requires_grad_value !! Whether gradients need to be computed for the created tensor @@ -605,7 +607,7 @@ subroutine torch_tensor_from_array_int8_4d(tensor, data_in, layout, & ! inputs integer(kind=int8), intent(in), target :: data_in(:,:,:,:) !! Input data that tensor will point at - integer, intent(in) :: layout(4) !! Control order of indices + integer(FT_INT), intent(in) :: layout(4) !! Control order of indices integer(c_int), intent(in) :: c_device_type !! Device type the tensor will live on (`torch_kCPU` or `torch_kCUDA`) integer(c_int), optional, intent(in) :: device_index !! device index to use for `torch_kCUDA` case logical, optional, intent(in) :: requires_grad !! Whether gradients need to be computed for the created tensor @@ -615,7 +617,7 @@ subroutine torch_tensor_from_array_int8_4d(tensor, data_in, layout, & integer(c_int), parameter :: c_dtype = torch_kInt8 !! Data type integer(c_int64_t) :: strides(4) !! Strides for accessing data integer(c_int), parameter :: ndims = 4 !! Number of dimension of input data - integer :: i + integer(FT_INT) :: i integer(c_int) :: device_index_value logical :: requires_grad_value !! Whether gradients need to be computed for the created tensor @@ -659,7 +661,7 @@ subroutine torch_tensor_from_array_int16_1d(tensor, data_in, layout, & ! inputs integer(kind=int16), intent(in), target :: data_in(:) !! Input data that tensor will point at - integer, intent(in) :: layout(1) !! Control order of indices + integer(FT_INT), intent(in) :: layout(1) !! Control order of indices integer(c_int), intent(in) :: c_device_type !! Device type the tensor will live on (`torch_kCPU` or `torch_kCUDA`) integer(c_int), optional, intent(in) :: device_index !! device index to use for `torch_kCUDA` case logical, optional, intent(in) :: requires_grad !! Whether gradients need to be computed for the created tensor @@ -669,7 +671,7 @@ subroutine torch_tensor_from_array_int16_1d(tensor, data_in, layout, & integer(c_int), parameter :: c_dtype = torch_kInt16 !! Data type integer(c_int64_t) :: strides(1) !! Strides for accessing data integer(c_int), parameter :: ndims = 1 !! Number of dimension of input data - integer :: i + integer(FT_INT) :: i integer(c_int) :: device_index_value logical :: requires_grad_value !! Whether gradients need to be computed for the created tensor @@ -713,7 +715,7 @@ subroutine torch_tensor_from_array_int16_2d(tensor, data_in, layout, & ! inputs integer(kind=int16), intent(in), target :: data_in(:,:) !! Input data that tensor will point at - integer, intent(in) :: layout(2) !! Control order of indices + integer(FT_INT), intent(in) :: layout(2) !! Control order of indices integer(c_int), intent(in) :: c_device_type !! Device type the tensor will live on (`torch_kCPU` or `torch_kCUDA`) integer(c_int), optional, intent(in) :: device_index !! device index to use for `torch_kCUDA` case logical, optional, intent(in) :: requires_grad !! Whether gradients need to be computed for the created tensor @@ -723,7 +725,7 @@ subroutine torch_tensor_from_array_int16_2d(tensor, data_in, layout, & integer(c_int), parameter :: c_dtype = torch_kInt16 !! Data type integer(c_int64_t) :: strides(2) !! Strides for accessing data integer(c_int), parameter :: ndims = 2 !! Number of dimension of input data - integer :: i + integer(FT_INT) :: i integer(c_int) :: device_index_value logical :: requires_grad_value !! Whether gradients need to be computed for the created tensor @@ -767,7 +769,7 @@ subroutine torch_tensor_from_array_int16_3d(tensor, data_in, layout, & ! inputs integer(kind=int16), intent(in), target :: data_in(:,:,:) !! Input data that tensor will point at - integer, intent(in) :: layout(3) !! Control order of indices + integer(FT_INT), intent(in) :: layout(3) !! Control order of indices integer(c_int), intent(in) :: c_device_type !! Device type the tensor will live on (`torch_kCPU` or `torch_kCUDA`) integer(c_int), optional, intent(in) :: device_index !! device index to use for `torch_kCUDA` case logical, optional, intent(in) :: requires_grad !! Whether gradients need to be computed for the created tensor @@ -777,7 +779,7 @@ subroutine torch_tensor_from_array_int16_3d(tensor, data_in, layout, & integer(c_int), parameter :: c_dtype = torch_kInt16 !! Data type integer(c_int64_t) :: strides(3) !! Strides for accessing data integer(c_int), parameter :: ndims = 3 !! Number of dimension of input data - integer :: i + integer(FT_INT) :: i integer(c_int) :: device_index_value logical :: requires_grad_value !! Whether gradients need to be computed for the created tensor @@ -821,7 +823,7 @@ subroutine torch_tensor_from_array_int16_4d(tensor, data_in, layout, & ! inputs integer(kind=int16), intent(in), target :: data_in(:,:,:,:) !! Input data that tensor will point at - integer, intent(in) :: layout(4) !! Control order of indices + integer(FT_INT), intent(in) :: layout(4) !! Control order of indices integer(c_int), intent(in) :: c_device_type !! Device type the tensor will live on (`torch_kCPU` or `torch_kCUDA`) integer(c_int), optional, intent(in) :: device_index !! device index to use for `torch_kCUDA` case logical, optional, intent(in) :: requires_grad !! Whether gradients need to be computed for the created tensor @@ -831,7 +833,7 @@ subroutine torch_tensor_from_array_int16_4d(tensor, data_in, layout, & integer(c_int), parameter :: c_dtype = torch_kInt16 !! Data type integer(c_int64_t) :: strides(4) !! Strides for accessing data integer(c_int), parameter :: ndims = 4 !! Number of dimension of input data - integer :: i + integer(FT_INT) :: i integer(c_int) :: device_index_value logical :: requires_grad_value !! Whether gradients need to be computed for the created tensor @@ -875,7 +877,7 @@ subroutine torch_tensor_from_array_int32_1d(tensor, data_in, layout, & ! inputs integer(kind=int32), intent(in), target :: data_in(:) !! Input data that tensor will point at - integer, intent(in) :: layout(1) !! Control order of indices + integer(FT_INT), intent(in) :: layout(1) !! Control order of indices integer(c_int), intent(in) :: c_device_type !! Device type the tensor will live on (`torch_kCPU` or `torch_kCUDA`) integer(c_int), optional, intent(in) :: device_index !! device index to use for `torch_kCUDA` case logical, optional, intent(in) :: requires_grad !! Whether gradients need to be computed for the created tensor @@ -885,7 +887,7 @@ subroutine torch_tensor_from_array_int32_1d(tensor, data_in, layout, & integer(c_int), parameter :: c_dtype = torch_kInt32 !! Data type integer(c_int64_t) :: strides(1) !! Strides for accessing data integer(c_int), parameter :: ndims = 1 !! Number of dimension of input data - integer :: i + integer(FT_INT) :: i integer(c_int) :: device_index_value logical :: requires_grad_value !! Whether gradients need to be computed for the created tensor @@ -929,7 +931,7 @@ subroutine torch_tensor_from_array_int32_2d(tensor, data_in, layout, & ! inputs integer(kind=int32), intent(in), target :: data_in(:,:) !! Input data that tensor will point at - integer, intent(in) :: layout(2) !! Control order of indices + integer(FT_INT), intent(in) :: layout(2) !! Control order of indices integer(c_int), intent(in) :: c_device_type !! Device type the tensor will live on (`torch_kCPU` or `torch_kCUDA`) integer(c_int), optional, intent(in) :: device_index !! device index to use for `torch_kCUDA` case logical, optional, intent(in) :: requires_grad !! Whether gradients need to be computed for the created tensor @@ -939,7 +941,7 @@ subroutine torch_tensor_from_array_int32_2d(tensor, data_in, layout, & integer(c_int), parameter :: c_dtype = torch_kInt32 !! Data type integer(c_int64_t) :: strides(2) !! Strides for accessing data integer(c_int), parameter :: ndims = 2 !! Number of dimension of input data - integer :: i + integer(FT_INT) :: i integer(c_int) :: device_index_value logical :: requires_grad_value !! Whether gradients need to be computed for the created tensor @@ -983,7 +985,7 @@ subroutine torch_tensor_from_array_int32_3d(tensor, data_in, layout, & ! inputs integer(kind=int32), intent(in), target :: data_in(:,:,:) !! Input data that tensor will point at - integer, intent(in) :: layout(3) !! Control order of indices + integer(FT_INT), intent(in) :: layout(3) !! Control order of indices integer(c_int), intent(in) :: c_device_type !! Device type the tensor will live on (`torch_kCPU` or `torch_kCUDA`) integer(c_int), optional, intent(in) :: device_index !! device index to use for `torch_kCUDA` case logical, optional, intent(in) :: requires_grad !! Whether gradients need to be computed for the created tensor @@ -993,7 +995,7 @@ subroutine torch_tensor_from_array_int32_3d(tensor, data_in, layout, & integer(c_int), parameter :: c_dtype = torch_kInt32 !! Data type integer(c_int64_t) :: strides(3) !! Strides for accessing data integer(c_int), parameter :: ndims = 3 !! Number of dimension of input data - integer :: i + integer(FT_INT) :: i integer(c_int) :: device_index_value logical :: requires_grad_value !! Whether gradients need to be computed for the created tensor @@ -1037,7 +1039,7 @@ subroutine torch_tensor_from_array_int32_4d(tensor, data_in, layout, & ! inputs integer(kind=int32), intent(in), target :: data_in(:,:,:,:) !! Input data that tensor will point at - integer, intent(in) :: layout(4) !! Control order of indices + integer(FT_INT), intent(in) :: layout(4) !! Control order of indices integer(c_int), intent(in) :: c_device_type !! Device type the tensor will live on (`torch_kCPU` or `torch_kCUDA`) integer(c_int), optional, intent(in) :: device_index !! device index to use for `torch_kCUDA` case logical, optional, intent(in) :: requires_grad !! Whether gradients need to be computed for the created tensor @@ -1047,7 +1049,7 @@ subroutine torch_tensor_from_array_int32_4d(tensor, data_in, layout, & integer(c_int), parameter :: c_dtype = torch_kInt32 !! Data type integer(c_int64_t) :: strides(4) !! Strides for accessing data integer(c_int), parameter :: ndims = 4 !! Number of dimension of input data - integer :: i + integer(FT_INT) :: i integer(c_int) :: device_index_value logical :: requires_grad_value !! Whether gradients need to be computed for the created tensor @@ -1091,7 +1093,7 @@ subroutine torch_tensor_from_array_int64_1d(tensor, data_in, layout, & ! inputs integer(kind=int64), intent(in), target :: data_in(:) !! Input data that tensor will point at - integer, intent(in) :: layout(1) !! Control order of indices + integer(FT_INT), intent(in) :: layout(1) !! Control order of indices integer(c_int), intent(in) :: c_device_type !! Device type the tensor will live on (`torch_kCPU` or `torch_kCUDA`) integer(c_int), optional, intent(in) :: device_index !! device index to use for `torch_kCUDA` case logical, optional, intent(in) :: requires_grad !! Whether gradients need to be computed for the created tensor @@ -1101,7 +1103,7 @@ subroutine torch_tensor_from_array_int64_1d(tensor, data_in, layout, & integer(c_int), parameter :: c_dtype = torch_kInt64 !! Data type integer(c_int64_t) :: strides(1) !! Strides for accessing data integer(c_int), parameter :: ndims = 1 !! Number of dimension of input data - integer :: i + integer(FT_INT) :: i integer(c_int) :: device_index_value logical :: requires_grad_value !! Whether gradients need to be computed for the created tensor @@ -1145,7 +1147,7 @@ subroutine torch_tensor_from_array_int64_2d(tensor, data_in, layout, & ! inputs integer(kind=int64), intent(in), target :: data_in(:,:) !! Input data that tensor will point at - integer, intent(in) :: layout(2) !! Control order of indices + integer(FT_INT), intent(in) :: layout(2) !! Control order of indices integer(c_int), intent(in) :: c_device_type !! Device type the tensor will live on (`torch_kCPU` or `torch_kCUDA`) integer(c_int), optional, intent(in) :: device_index !! device index to use for `torch_kCUDA` case logical, optional, intent(in) :: requires_grad !! Whether gradients need to be computed for the created tensor @@ -1155,7 +1157,7 @@ subroutine torch_tensor_from_array_int64_2d(tensor, data_in, layout, & integer(c_int), parameter :: c_dtype = torch_kInt64 !! Data type integer(c_int64_t) :: strides(2) !! Strides for accessing data integer(c_int), parameter :: ndims = 2 !! Number of dimension of input data - integer :: i + integer(FT_INT) :: i integer(c_int) :: device_index_value logical :: requires_grad_value !! Whether gradients need to be computed for the created tensor @@ -1199,7 +1201,7 @@ subroutine torch_tensor_from_array_int64_3d(tensor, data_in, layout, & ! inputs integer(kind=int64), intent(in), target :: data_in(:,:,:) !! Input data that tensor will point at - integer, intent(in) :: layout(3) !! Control order of indices + integer(FT_INT), intent(in) :: layout(3) !! Control order of indices integer(c_int), intent(in) :: c_device_type !! Device type the tensor will live on (`torch_kCPU` or `torch_kCUDA`) integer(c_int), optional, intent(in) :: device_index !! device index to use for `torch_kCUDA` case logical, optional, intent(in) :: requires_grad !! Whether gradients need to be computed for the created tensor @@ -1209,7 +1211,7 @@ subroutine torch_tensor_from_array_int64_3d(tensor, data_in, layout, & integer(c_int), parameter :: c_dtype = torch_kInt64 !! Data type integer(c_int64_t) :: strides(3) !! Strides for accessing data integer(c_int), parameter :: ndims = 3 !! Number of dimension of input data - integer :: i + integer(FT_INT) :: i integer(c_int) :: device_index_value logical :: requires_grad_value !! Whether gradients need to be computed for the created tensor @@ -1253,7 +1255,7 @@ subroutine torch_tensor_from_array_int64_4d(tensor, data_in, layout, & ! inputs integer(kind=int64), intent(in), target :: data_in(:,:,:,:) !! Input data that tensor will point at - integer, intent(in) :: layout(4) !! Control order of indices + integer(FT_INT), intent(in) :: layout(4) !! Control order of indices integer(c_int), intent(in) :: c_device_type !! Device type the tensor will live on (`torch_kCPU` or `torch_kCUDA`) integer(c_int), optional, intent(in) :: device_index !! device index to use for `torch_kCUDA` case logical, optional, intent(in) :: requires_grad !! Whether gradients need to be computed for the created tensor @@ -1263,7 +1265,7 @@ subroutine torch_tensor_from_array_int64_4d(tensor, data_in, layout, & integer(c_int), parameter :: c_dtype = torch_kInt64 !! Data type integer(c_int64_t) :: strides(4) !! Strides for accessing data integer(c_int), parameter :: ndims = 4 !! Number of dimension of input data - integer :: i + integer(FT_INT) :: i integer(c_int) :: device_index_value logical :: requires_grad_value !! Whether gradients need to be computed for the created tensor @@ -1307,7 +1309,7 @@ subroutine torch_tensor_from_array_real32_1d(tensor, data_in, layout, & ! inputs real(kind=real32), intent(in), target :: data_in(:) !! Input data that tensor will point at - integer, intent(in) :: layout(1) !! Control order of indices + integer(FT_INT), intent(in) :: layout(1) !! Control order of indices integer(c_int), intent(in) :: c_device_type !! Device type the tensor will live on (`torch_kCPU` or `torch_kCUDA`) integer(c_int), optional, intent(in) :: device_index !! device index to use for `torch_kCUDA` case logical, optional, intent(in) :: requires_grad !! Whether gradients need to be computed for the created tensor @@ -1317,7 +1319,7 @@ subroutine torch_tensor_from_array_real32_1d(tensor, data_in, layout, & integer(c_int), parameter :: c_dtype = torch_kFloat32 !! Data type integer(c_int64_t) :: strides(1) !! Strides for accessing data integer(c_int), parameter :: ndims = 1 !! Number of dimension of input data - integer :: i + integer(FT_INT) :: i integer(c_int) :: device_index_value logical :: requires_grad_value !! Whether gradients need to be computed for the created tensor @@ -1361,7 +1363,7 @@ subroutine torch_tensor_from_array_real32_2d(tensor, data_in, layout, & ! inputs real(kind=real32), intent(in), target :: data_in(:,:) !! Input data that tensor will point at - integer, intent(in) :: layout(2) !! Control order of indices + integer(FT_INT), intent(in) :: layout(2) !! Control order of indices integer(c_int), intent(in) :: c_device_type !! Device type the tensor will live on (`torch_kCPU` or `torch_kCUDA`) integer(c_int), optional, intent(in) :: device_index !! device index to use for `torch_kCUDA` case logical, optional, intent(in) :: requires_grad !! Whether gradients need to be computed for the created tensor @@ -1371,7 +1373,7 @@ subroutine torch_tensor_from_array_real32_2d(tensor, data_in, layout, & integer(c_int), parameter :: c_dtype = torch_kFloat32 !! Data type integer(c_int64_t) :: strides(2) !! Strides for accessing data integer(c_int), parameter :: ndims = 2 !! Number of dimension of input data - integer :: i + integer(FT_INT) :: i integer(c_int) :: device_index_value logical :: requires_grad_value !! Whether gradients need to be computed for the created tensor @@ -1415,7 +1417,7 @@ subroutine torch_tensor_from_array_real32_3d(tensor, data_in, layout, & ! inputs real(kind=real32), intent(in), target :: data_in(:,:,:) !! Input data that tensor will point at - integer, intent(in) :: layout(3) !! Control order of indices + integer(FT_INT), intent(in) :: layout(3) !! Control order of indices integer(c_int), intent(in) :: c_device_type !! Device type the tensor will live on (`torch_kCPU` or `torch_kCUDA`) integer(c_int), optional, intent(in) :: device_index !! device index to use for `torch_kCUDA` case logical, optional, intent(in) :: requires_grad !! Whether gradients need to be computed for the created tensor @@ -1425,7 +1427,7 @@ subroutine torch_tensor_from_array_real32_3d(tensor, data_in, layout, & integer(c_int), parameter :: c_dtype = torch_kFloat32 !! Data type integer(c_int64_t) :: strides(3) !! Strides for accessing data integer(c_int), parameter :: ndims = 3 !! Number of dimension of input data - integer :: i + integer(FT_INT) :: i integer(c_int) :: device_index_value logical :: requires_grad_value !! Whether gradients need to be computed for the created tensor @@ -1469,7 +1471,7 @@ subroutine torch_tensor_from_array_real32_4d(tensor, data_in, layout, & ! inputs real(kind=real32), intent(in), target :: data_in(:,:,:,:) !! Input data that tensor will point at - integer, intent(in) :: layout(4) !! Control order of indices + integer(FT_INT), intent(in) :: layout(4) !! Control order of indices integer(c_int), intent(in) :: c_device_type !! Device type the tensor will live on (`torch_kCPU` or `torch_kCUDA`) integer(c_int), optional, intent(in) :: device_index !! device index to use for `torch_kCUDA` case logical, optional, intent(in) :: requires_grad !! Whether gradients need to be computed for the created tensor @@ -1479,7 +1481,7 @@ subroutine torch_tensor_from_array_real32_4d(tensor, data_in, layout, & integer(c_int), parameter :: c_dtype = torch_kFloat32 !! Data type integer(c_int64_t) :: strides(4) !! Strides for accessing data integer(c_int), parameter :: ndims = 4 !! Number of dimension of input data - integer :: i + integer(FT_INT) :: i integer(c_int) :: device_index_value logical :: requires_grad_value !! Whether gradients need to be computed for the created tensor @@ -1523,7 +1525,7 @@ subroutine torch_tensor_from_array_real64_1d(tensor, data_in, layout, & ! inputs real(kind=real64), intent(in), target :: data_in(:) !! Input data that tensor will point at - integer, intent(in) :: layout(1) !! Control order of indices + integer(FT_INT), intent(in) :: layout(1) !! Control order of indices integer(c_int), intent(in) :: c_device_type !! Device type the tensor will live on (`torch_kCPU` or `torch_kCUDA`) integer(c_int), optional, intent(in) :: device_index !! device index to use for `torch_kCUDA` case logical, optional, intent(in) :: requires_grad !! Whether gradients need to be computed for the created tensor @@ -1533,7 +1535,7 @@ subroutine torch_tensor_from_array_real64_1d(tensor, data_in, layout, & integer(c_int), parameter :: c_dtype = torch_kFloat64 !! Data type integer(c_int64_t) :: strides(1) !! Strides for accessing data integer(c_int), parameter :: ndims = 1 !! Number of dimension of input data - integer :: i + integer(FT_INT) :: i integer(c_int) :: device_index_value logical :: requires_grad_value !! Whether gradients need to be computed for the created tensor @@ -1577,7 +1579,7 @@ subroutine torch_tensor_from_array_real64_2d(tensor, data_in, layout, & ! inputs real(kind=real64), intent(in), target :: data_in(:,:) !! Input data that tensor will point at - integer, intent(in) :: layout(2) !! Control order of indices + integer(FT_INT), intent(in) :: layout(2) !! Control order of indices integer(c_int), intent(in) :: c_device_type !! Device type the tensor will live on (`torch_kCPU` or `torch_kCUDA`) integer(c_int), optional, intent(in) :: device_index !! device index to use for `torch_kCUDA` case logical, optional, intent(in) :: requires_grad !! Whether gradients need to be computed for the created tensor @@ -1587,7 +1589,7 @@ subroutine torch_tensor_from_array_real64_2d(tensor, data_in, layout, & integer(c_int), parameter :: c_dtype = torch_kFloat64 !! Data type integer(c_int64_t) :: strides(2) !! Strides for accessing data integer(c_int), parameter :: ndims = 2 !! Number of dimension of input data - integer :: i + integer(FT_INT) :: i integer(c_int) :: device_index_value logical :: requires_grad_value !! Whether gradients need to be computed for the created tensor @@ -1631,7 +1633,7 @@ subroutine torch_tensor_from_array_real64_3d(tensor, data_in, layout, & ! inputs real(kind=real64), intent(in), target :: data_in(:,:,:) !! Input data that tensor will point at - integer, intent(in) :: layout(3) !! Control order of indices + integer(FT_INT), intent(in) :: layout(3) !! Control order of indices integer(c_int), intent(in) :: c_device_type !! Device type the tensor will live on (`torch_kCPU` or `torch_kCUDA`) integer(c_int), optional, intent(in) :: device_index !! device index to use for `torch_kCUDA` case logical, optional, intent(in) :: requires_grad !! Whether gradients need to be computed for the created tensor @@ -1641,7 +1643,7 @@ subroutine torch_tensor_from_array_real64_3d(tensor, data_in, layout, & integer(c_int), parameter :: c_dtype = torch_kFloat64 !! Data type integer(c_int64_t) :: strides(3) !! Strides for accessing data integer(c_int), parameter :: ndims = 3 !! Number of dimension of input data - integer :: i + integer(FT_INT) :: i integer(c_int) :: device_index_value logical :: requires_grad_value !! Whether gradients need to be computed for the created tensor @@ -1685,7 +1687,7 @@ subroutine torch_tensor_from_array_real64_4d(tensor, data_in, layout, & ! inputs real(kind=real64), intent(in), target :: data_in(:,:,:,:) !! Input data that tensor will point at - integer, intent(in) :: layout(4) !! Control order of indices + integer(FT_INT), intent(in) :: layout(4) !! Control order of indices integer(c_int), intent(in) :: c_device_type !! Device type the tensor will live on (`torch_kCPU` or `torch_kCUDA`) integer(c_int), optional, intent(in) :: device_index !! device index to use for `torch_kCUDA` case logical, optional, intent(in) :: requires_grad !! Whether gradients need to be computed for the created tensor @@ -1695,7 +1697,7 @@ subroutine torch_tensor_from_array_real64_4d(tensor, data_in, layout, & integer(c_int), parameter :: c_dtype = torch_kFloat64 !! Data type integer(c_int64_t) :: strides(4) !! Strides for accessing data integer(c_int), parameter :: ndims = 4 !! Number of dimension of input data - integer :: i + integer(FT_INT) :: i integer(c_int) :: device_index_value logical :: requires_grad_value !! Whether gradients need to be computed for the created tensor diff --git a/src/ftorch.fypp b/src/ftorch.fypp index b90b7f73..90a04e04 100644 --- a/src/ftorch.fypp +++ b/src/ftorch.fypp @@ -31,6 +31,8 @@ module ftorch implicit none + integer, parameter :: FT_INT = int32 ! set integer size for FTorch library + !> Type for holding a torch neural net (nn.Module). type torch_model type(c_ptr) :: p = c_null_ptr !! pointer to the neural net in memory @@ -278,7 +280,7 @@ contains !> Deallocates an array of tensors. subroutine torch_tensor_array_delete(tensor_array) type(torch_tensor), dimension(:), intent(inout) :: tensor_array - integer :: i + integer(FT_INT) :: i ! use bounds rather than (1, N) because it's safer do i = lbound(tensor_array, dim=1), ubound(tensor_array, dim=1) @@ -371,7 +373,7 @@ contains logical, optional, intent(in) :: requires_grad !! Whether gradients need to be computed for the created tensor logical :: requires_grad_value !! Whether gradients need to be computed for the created tensor - integer :: i + integer(FT_INT) :: i integer(c_int) :: n_inputs integer(c_int) :: n_outputs type(c_ptr), dimension(size(input_tensors)), target :: input_ptrs @@ -443,7 +445,7 @@ contains ! inputs ${f_type(PREC)}$(kind=${PREC}$), intent(in), target :: data_in${ranksuffix(RANK)}$ !! Input data that tensor will point at - integer, intent(in) :: layout(${RANK}$) !! Control order of indices + integer(FT_INT), intent(in) :: layout(${RANK}$) !! Control order of indices integer(c_int), intent(in) :: c_device_type !! Device type the tensor will live on (`torch_kCPU` or `torch_kCUDA`) integer(c_int), optional, intent(in) :: device_index !! device index to use for `torch_kCUDA` case logical, optional, intent(in) :: requires_grad !! Whether gradients need to be computed for the created tensor @@ -453,7 +455,7 @@ contains integer(c_int), parameter :: c_dtype = ${enum_from_prec(PREC)}$ !! Data type integer(c_int64_t) :: strides(${RANK}$) !! Strides for accessing data integer(c_int), parameter :: ndims = ${RANK}$ !! Number of dimension of input data - integer :: i + integer(FT_INT) :: i integer(c_int) :: device_index_value logical :: requires_grad_value !! Whether gradients need to be computed for the created tensor From 36cad9118a2c2957f31edbe8a5747136231d6a13 Mon Sep 17 00:00:00 2001 From: melt Date: Fri, 13 Sep 2024 15:13:40 +0100 Subject: [PATCH 4/4] test: update examples to use FT_INT --- examples/1_SimpleNet/simplenet_infer_fortran.f90 | 2 +- examples/2_ResNet18/resnet_infer_fortran.f90 | 4 ++-- examples/4_MultiIO/multiionet_infer_fortran.f90 | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/1_SimpleNet/simplenet_infer_fortran.f90 b/examples/1_SimpleNet/simplenet_infer_fortran.f90 index 48c3b767..5e979277 100644 --- a/examples/1_SimpleNet/simplenet_infer_fortran.f90 +++ b/examples/1_SimpleNet/simplenet_infer_fortran.f90 @@ -17,7 +17,7 @@ program inference ! Set up Fortran data structures real(wp), dimension(5), target :: in_data real(wp), dimension(5), target :: out_data - integer(int64) :: tensor_layout(1) = [1] + integer(FT_INT) :: tensor_layout(1) = [1] ! Set up Torch data structures ! The net, a vector of input tensors (in this case we only have one), and the output tensor diff --git a/examples/2_ResNet18/resnet_infer_fortran.f90 b/examples/2_ResNet18/resnet_infer_fortran.f90 index 8717c125..539ff5ad 100644 --- a/examples/2_ResNet18/resnet_infer_fortran.f90 +++ b/examples/2_ResNet18/resnet_infer_fortran.f90 @@ -29,10 +29,10 @@ subroutine main() integer, parameter :: in_dims = 4 integer :: in_shape(in_dims) = [1, 3, 224, 224] - integer :: in_layout(in_dims) = [1, 2, 3, 4] + integer(FT_INT) :: 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] + integer(FT_INT) :: out_layout(out_dims) = [1, 2] ! Path to input data character(len=100) :: data_dir diff --git a/examples/4_MultiIO/multiionet_infer_fortran.f90 b/examples/4_MultiIO/multiionet_infer_fortran.f90 index d63524a1..e3aec31a 100644 --- a/examples/4_MultiIO/multiionet_infer_fortran.f90 +++ b/examples/4_MultiIO/multiionet_infer_fortran.f90 @@ -19,7 +19,7 @@ program inference real(wp), dimension(4), target :: in_data2 real(wp), dimension(4), target :: out_data1 real(wp), dimension(4), target :: out_data2 - integer :: tensor_layout(1) = [1] + integer(FT_INT) :: tensor_layout(1) = [1] ! Set up Torch data structures ! The net, a vector of input tensors (in this case we only have one), and the output tensor