@@ -5,8 +5,8 @@ program example
5
5
6
6
! Import our library for interfacing with PyTorch's Autograd module
7
7
use ftorch, only: assignment (= ), operator (+ ), operator (- ), operator (* ), &
8
- operator (/ ), operator (** ), torch_kCPU, torch_tensor, torch_tensor_delete, &
9
- torch_tensor_from_array, torch_tensor_to_array
8
+ operator (/ ), operator (** ), torch_kCPU, torch_kFloat32, torch_tensor, torch_tensor_delete, &
9
+ torch_tensor_empty, torch_tensor_from_array
10
10
11
11
! Import our tools module for testing utils
12
12
use ftorch_test_utils, only : assert_allclose
@@ -17,60 +17,49 @@ program example
17
17
integer , parameter :: wp = sp
18
18
19
19
! Set up Fortran data structures
20
+ integer , parameter :: ndims = 2
20
21
integer , parameter :: n= 2 , m= 1
21
22
real (wp), dimension (n,m), target :: in_data1
22
23
real (wp), dimension (n,m), target :: in_data2
23
- real (wp), dimension (:,: ), pointer :: out_data
24
+ real (wp), dimension (n,m ), target :: out_data
24
25
real (wp), dimension (n,m) :: expected
25
- integer :: tensor_layout(2 ) = [1 , 2 ]
26
+ integer :: tensor_layout(ndims ) = [1 , 2 ]
26
27
27
28
! Flag for testing
28
29
logical :: test_pass
29
30
30
31
! Set up Torch data structures
31
32
type (torch_tensor) :: a, b, Q
32
33
33
- ! Initialise input arrays as in Python example
34
+ ! Initialise Torch Tensors from input arrays as in Python example
34
35
in_data1(:,1 ) = [2.0_wp , 3.0_wp ]
35
36
in_data2(:,1 ) = [6.0_wp , 4.0_wp ]
36
-
37
- ! Construct a Torch Tensor from a Fortran array
38
37
! TODO: Implement requires_grad=.true.
39
38
call torch_tensor_from_array(a, in_data1, tensor_layout, torch_kCPU)
40
39
call torch_tensor_from_array(b, in_data2, tensor_layout, torch_kCPU)
41
40
41
+ ! Initialise Torch Tensor from array used for output
42
+ call torch_tensor_from_array(Q, out_data, tensor_layout, torch_kCPU)
43
+
42
44
! Check arithmetic operations work for torch_tensors
43
45
write (* ,* ) " a = " , in_data1(:,1 )
44
46
write (* ,* ) " b = " , in_data2(:,1 )
45
47
Q = 3 * (a** 3 - b * b / 3 )
46
48
47
49
! Extract a Fortran array from a Torch tensor
48
- call torch_tensor_to_array(Q, out_data, shape (in_data1))
49
50
write (* ,* ) " Q = 3 * (a ** 3 - b * b / 2) =" , out_data(:,1 )
50
51
51
52
! Check output tensor matches expected value
52
53
expected(:,1 ) = [- 12.0_wp , 65.0_wp ]
53
- test_pass = assert_allclose(out_data, expected, test_name= " torch_tensor_to_array " , rtol = 1e-5 )
54
+ test_pass = assert_allclose(out_data, expected, test_name= " autograd_Q " )
54
55
if (.not. test_pass) then
55
- call clean_up()
56
56
print * , " Error :: out_data does not match expected value"
57
57
stop 999
58
58
end if
59
59
60
60
! Back-propagation
61
61
! TODO: Requires API extension
62
62
63
- call clean_up()
64
63
write (* ,* ) " Autograd example ran successfully"
65
64
66
- contains
67
-
68
- ! Subroutine for freeing memory and nullifying pointers used in the example
69
- subroutine clean_up ()
70
- nullify(out_data)
71
- call torch_tensor_delete(a)
72
- call torch_tensor_delete(b)
73
- call torch_tensor_delete(Q)
74
- end subroutine clean_up
75
-
76
65
end program example
0 commit comments