From 90a51ffe0dc8c4c382df1c25c073b660876fe495 Mon Sep 17 00:00:00 2001 From: Joe Wallwork Date: Tue, 14 Jan 2025 13:22:55 +0000 Subject: [PATCH 1/2] strides should be initialised to zero; tensor_shape and layout can be assumed length --- src/ftorch.F90 | 5 +++-- src/ftorch.fypp | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/ftorch.F90 b/src/ftorch.F90 index 48749c90..3491ac1c 100644 --- a/src/ftorch.F90 +++ b/src/ftorch.F90 @@ -409,8 +409,8 @@ subroutine torch_tensor_from_blob(tensor, data, ndims, tensor_shape, layout, dty type(torch_tensor), intent(out) :: tensor !! Returned tensor type(c_ptr), intent(in) :: data !! Pointer to data integer(c_int), intent(in) :: ndims !! Number of dimensions of the tensor - integer(c_int64_t), intent(in) :: tensor_shape(*) !! Shape of the tensor - integer(c_int), intent(in) :: layout(*) !! Layout for strides for accessing data + integer(c_int64_t), intent(in) :: tensor_shape(:) !! Shape of the tensor + integer(c_int), intent(in) :: layout(:) !! Layout for strides for accessing data integer(c_int), intent(in) :: dtype !! Data type of the tensor integer(c_int), intent(in) :: device_type !! Device type the tensor will live on (`torch_kCPU` or `torch_kCUDA`) integer, optional, intent(in) :: device_index !! Device index to use for `torch_kCUDA` case @@ -427,6 +427,7 @@ subroutine torch_tensor_from_blob(tensor, data, ndims, tensor_shape, layout, dty requires_grad_value = requires_grad end if + strides(:) = 0 do i = 1, ndims if (i == 1) then strides(layout(i)) = 1 diff --git a/src/ftorch.fypp b/src/ftorch.fypp index d3e060d8..b3da5784 100644 --- a/src/ftorch.fypp +++ b/src/ftorch.fypp @@ -364,8 +364,8 @@ contains type(torch_tensor), intent(out) :: tensor !! Returned tensor type(c_ptr), intent(in) :: data !! Pointer to data integer(c_int), intent(in) :: ndims !! Number of dimensions of the tensor - integer(c_int64_t), intent(in) :: tensor_shape(*) !! Shape of the tensor - integer(c_int), intent(in) :: layout(*) !! Layout for strides for accessing data + integer(c_int64_t), intent(in) :: tensor_shape(:) !! Shape of the tensor + integer(c_int), intent(in) :: layout(:) !! Layout for strides for accessing data integer(c_int), intent(in) :: dtype !! Data type of the tensor integer(c_int), intent(in) :: device_type !! Device type the tensor will live on (`torch_kCPU` or `torch_kCUDA`) integer, optional, intent(in) :: device_index !! Device index to use for `torch_kCUDA` case @@ -382,6 +382,7 @@ contains requires_grad_value = requires_grad end if + strides(:) = 0 do i = 1, ndims if (i == 1) then strides(layout(i)) = 1 From 190782aef685cf7c3e0b3f1161432e9e3a8e2d13 Mon Sep 17 00:00:00 2001 From: Joe Wallwork Date: Tue, 14 Jan 2025 14:50:27 +0000 Subject: [PATCH 2/2] Apply same change elsewhere --- src/ftorch.F90 | 6 +++--- src/ftorch.fypp | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/ftorch.F90 b/src/ftorch.F90 index 3491ac1c..4771dd69 100644 --- a/src/ftorch.F90 +++ b/src/ftorch.F90 @@ -253,7 +253,7 @@ subroutine torch_tensor_empty(tensor, ndims, tensor_shape, dtype, & use, intrinsic :: iso_c_binding, only : c_bool, c_int, c_int64_t type(torch_tensor), intent(out) :: tensor !! Returned tensor integer(c_int), intent(in) :: ndims !! Number of dimensions of the tensor - integer(c_int64_t), intent(in) :: tensor_shape(*) !! Shape of the tensor + integer(c_int64_t), intent(in) :: tensor_shape(:) !! Shape of the tensor integer(c_int), intent(in) :: dtype !! Data type of the tensor integer(c_int), intent(in) :: device_type !! Device type the tensor will live on (`torch_kCPU` or `torch_kCUDA`) integer, optional, intent(in) :: device_index !! Device index to use for `torch_kCUDA` case @@ -304,7 +304,7 @@ subroutine torch_tensor_zeros(tensor, ndims, tensor_shape, dtype, & use, intrinsic :: iso_c_binding, only : c_bool, c_int, c_int64_t type(torch_tensor), intent(out) :: tensor !! Returned tensor integer(c_int), intent(in) :: ndims !! Number of dimensions of the tensor - integer(c_int64_t), intent(in) :: tensor_shape(*) !! Shape of the tensor + integer(c_int64_t), intent(in) :: tensor_shape(:) !! Shape of the tensor integer(c_int), intent(in) :: dtype !! Data type of the tensor integer(c_int), intent(in) :: device_type !! Device type the tensor will live on (`torch_kCPU` or `torch_kCUDA`) integer, optional, intent(in) :: device_index !! Device index to use for `torch_kCUDA` case @@ -355,7 +355,7 @@ subroutine torch_tensor_ones(tensor, ndims, tensor_shape, dtype, & use, intrinsic :: iso_c_binding, only : c_bool, c_int, c_int64_t type(torch_tensor), intent(out) :: tensor !! Returned tensor integer(c_int), intent(in) :: ndims !! Number of dimensions of the tensor - integer(c_int64_t), intent(in) :: tensor_shape(*) !! Shape of the tensor + integer(c_int64_t), intent(in) :: tensor_shape(:) !! Shape of the tensor integer(c_int), intent(in) :: dtype !! Data type of the tensor integer(c_int), intent(in) :: device_type !! Device type the tensor will live on (`torch_kCPU` or `torch_kCUDA`) integer, optional, intent(in) :: device_index !! Device index to use for `torch_kCUDA` case diff --git a/src/ftorch.fypp b/src/ftorch.fypp index b3da5784..1bf5e5dd 100644 --- a/src/ftorch.fypp +++ b/src/ftorch.fypp @@ -208,7 +208,7 @@ contains use, intrinsic :: iso_c_binding, only : c_bool, c_int, c_int64_t type(torch_tensor), intent(out) :: tensor !! Returned tensor integer(c_int), intent(in) :: ndims !! Number of dimensions of the tensor - integer(c_int64_t), intent(in) :: tensor_shape(*) !! Shape of the tensor + integer(c_int64_t), intent(in) :: tensor_shape(:) !! Shape of the tensor integer(c_int), intent(in) :: dtype !! Data type of the tensor integer(c_int), intent(in) :: device_type !! Device type the tensor will live on (`torch_kCPU` or `torch_kCUDA`) integer, optional, intent(in) :: device_index !! Device index to use for `torch_kCUDA` case @@ -259,7 +259,7 @@ contains use, intrinsic :: iso_c_binding, only : c_bool, c_int, c_int64_t type(torch_tensor), intent(out) :: tensor !! Returned tensor integer(c_int), intent(in) :: ndims !! Number of dimensions of the tensor - integer(c_int64_t), intent(in) :: tensor_shape(*) !! Shape of the tensor + integer(c_int64_t), intent(in) :: tensor_shape(:) !! Shape of the tensor integer(c_int), intent(in) :: dtype !! Data type of the tensor integer(c_int), intent(in) :: device_type !! Device type the tensor will live on (`torch_kCPU` or `torch_kCUDA`) integer, optional, intent(in) :: device_index !! Device index to use for `torch_kCUDA` case @@ -310,7 +310,7 @@ contains use, intrinsic :: iso_c_binding, only : c_bool, c_int, c_int64_t type(torch_tensor), intent(out) :: tensor !! Returned tensor integer(c_int), intent(in) :: ndims !! Number of dimensions of the tensor - integer(c_int64_t), intent(in) :: tensor_shape(*) !! Shape of the tensor + integer(c_int64_t), intent(in) :: tensor_shape(:) !! Shape of the tensor integer(c_int), intent(in) :: dtype !! Data type of the tensor integer(c_int), intent(in) :: device_type !! Device type the tensor will live on (`torch_kCPU` or `torch_kCUDA`) integer, optional, intent(in) :: device_index !! Device index to use for `torch_kCUDA` case