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

Move input array checks out of autograd example #231

Merged
merged 6 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
19 changes: 0 additions & 19 deletions examples/6_Autograd/autograd.f90
Original file line number Diff line number Diff line change
Expand Up @@ -67,28 +67,9 @@ program example
stop 999
end if

! Check first input array is unchanged by the arithmetic operations
expected(:,1) = [2.0_wp, 3.0_wp]
test_pass = assert_allclose(in_data1, expected, test_name="torch_tensor_to_array", rtol=1e-5)
if (.not. test_pass) then
call clean_up()
print *, "Error :: in_data1 was changed during arithmetic operations"
stop 999
end if

! Check second input array is unchanged by the arithmetic operations
expected(:,1) = [6.0_wp, 4.0_wp]
test_pass = assert_allclose(in_data2, expected, test_name="torch_tensor_to_array", rtol=1e-5)
if (.not. test_pass) then
call clean_up()
print *, "Error :: in_data2 was changed during arithmetic operations"
stop 999
end if

! Back-propagation
! TODO: Requires API extension

! Cleanup
call clean_up()
write (*,*) "Autograd example ran successfully"

Expand Down
5 changes: 5 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ This directory contains a number of examples of how to use the library:
This example demonstrates how to structure code in these cases, separating reading
in of a net from the call to the forward pass.

6. Autograd
- **This example is currently under development.** Eventually, it will
demonstrate automatic differentation in FTorch by leveraging PyTorch's
Autograd module.

To run select examples as integration tests, use the CMake argument
```
-DCMAKE_BUILD_TESTS=TRUE
Expand Down
114 changes: 84 additions & 30 deletions src/test/unit/test_constructors.pf
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,21 @@ subroutine test_torch_tensor_zeros()
! Check that the tensor values are all zero
expected(:,:) = 0.0
test_pass = assert_allclose(out_data, expected, test_name="test_torch_tensor_zeros")
@assertTrue(test_pass)
@assertEqual(shape(out_data), shape(expected))
if (.not. test_pass) then
call clean_up()
print *, "Error :: incorrect output from torch_tensor_zeros subroutine"
stop 999
end if

! Cleanup
nullify(out_data)
call torch_tensor_delete(tensor)
call clean_up()

contains

! Subroutine for freeing memory and nullifying pointers used in the unit test
subroutine clean_up()
nullify(out_data)
call torch_tensor_delete(tensor)
end subroutine clean_up

end subroutine test_torch_tensor_zeros

Expand Down Expand Up @@ -132,12 +141,21 @@ subroutine test_torch_tensor_ones()
! Check that the tensor values are all one
expected(:,:) = 1.0
test_pass = assert_allclose(out_data, expected, test_name="test_torch_tensor_ones")
@assertTrue(test_pass)
@assertEqual(shape(out_data), shape(expected))
if (.not. test_pass) then
call clean_up()
print *, "Error :: incorrect output from torch_tensor_ones subroutine"
stop 999
end if

! Cleanup
nullify(out_data)
call torch_tensor_delete(tensor)
call clean_up()

contains

! Subroutine for freeing memory and nullifying pointers used in the unit test
subroutine clean_up()
nullify(out_data)
call torch_tensor_delete(tensor)
end subroutine clean_up

end subroutine test_torch_tensor_ones

Expand Down Expand Up @@ -184,12 +202,21 @@ subroutine test_torch_from_array_1d()
! Compare the data in the tensor to the input data
expected(:) = in_data
test_pass = assert_allclose(out_data, expected, test_name="test_torch_tensor_from_array")
@assertTrue(test_pass)
@assertEqual(shape(out_data), shape(expected))
if (.not. test_pass) then
call clean_up()
print *, "Error :: incorrect output from torch_tensor_from_array subroutine"
stop 999
end if

! Cleanup
nullify(out_data)
call torch_tensor_delete(tensor)
call clean_up()

contains

! Subroutine for freeing memory and nullifying pointers used in the unit test
subroutine clean_up()
nullify(out_data)
call torch_tensor_delete(tensor)
end subroutine clean_up

end subroutine test_torch_from_array_1d

Expand Down Expand Up @@ -236,12 +263,21 @@ subroutine test_torch_from_array_2d()
! Compare the data in the tensor to the input data
expected(:,:) = in_data
test_pass = assert_allclose(out_data, expected, test_name="test_torch_tensor_from_array")
@assertTrue(test_pass)
@assertEqual(shape(out_data), shape(expected))
if (.not. test_pass) then
call clean_up()
print *, "Error :: incorrect output from torch_tensor_from_array subroutine"
stop 999
end if

! Cleanup
nullify(out_data)
call torch_tensor_delete(tensor)
call clean_up()

contains

! Subroutine for freeing memory and nullifying pointers used in the unit test
subroutine clean_up()
nullify(out_data)
call torch_tensor_delete(tensor)
end subroutine clean_up

end subroutine test_torch_from_array_2d

Expand Down Expand Up @@ -286,12 +322,21 @@ subroutine test_torch_from_array_3d()
! Compare the data in the tensor to the input data
expected(:,:,:) = in_data
test_pass = assert_allclose(out_data, expected, test_name="test_torch_tensor_from_array")
@assertTrue(test_pass)
@assertEqual(shape(out_data), shape(expected))
if (.not. test_pass) then
call clean_up()
print *, "Error :: incorrect output from torch_tensor_from_array subroutine"
stop 999
end if

! Cleanup
nullify(out_data)
call torch_tensor_delete(tensor)
call clean_up()

contains

! Subroutine for freeing memory and nullifying pointers used in the unit test
subroutine clean_up()
nullify(out_data)
call torch_tensor_delete(tensor)
end subroutine clean_up

end subroutine test_torch_from_array_3d

Expand Down Expand Up @@ -340,11 +385,20 @@ subroutine test_torch_from_blob()
! Compare the data in the tensor to the input data
expected(:,:) = in_data
test_pass = assert_allclose(out_data, expected, test_name="test_torch_tensor_from_blob")
@assertTrue(test_pass)
@assertEqual(shape(out_data), shape(expected))
if (.not. test_pass) then
call clean_up()
print *, "Error :: incorrect output from torch_tensor_from_array subroutine"
stop 999
end if

! Cleanup
nullify(out_data)
call torch_tensor_delete(tensor)
call clean_up()

contains

! Subroutine for freeing memory and nullifying pointers used in the unit test
subroutine clean_up()
nullify(out_data)
call torch_tensor_delete(tensor)
end subroutine clean_up

end subroutine test_torch_from_blob
Loading
Loading