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

Unit tests (using pFunit) #76

Merged
merged 47 commits into from
Jan 6, 2025
Merged

Unit tests (using pFunit) #76

merged 47 commits into from
Jan 6, 2025

Conversation

dorchard
Copy link
Member

@dorchard dorchard commented Nov 28, 2023

I have started putting in the infrastructure for us to write some unit tests using pFunit.

Create simple unit tests for main functions

  • Make the cmake portable; I fought a bit, but I don't know enough cmake. We need to be able to link the include directories for ftorch. At the moment the library and include paths need to be specified.
  • Test torch_tensor_from_blob
  • Test torch_tensor_from_array
  • Test torch_tensor_zeros
  • Test torch_tensor_ones
  • Test torch_tensor_empty

Other things using torch_module_forward could be better with an integration test since it will need a lot of setup code anyway.

Additionally, we could make use of fixtures and other features of pFUnit in a follow-up.

Closing this would fix #4

@dorchard
Copy link
Member Author

tests/test_ftorch.pf gives a start of a unit test for torch_tensor_from_blob

@TomMelt TomMelt added the enhancement New feature or request label Nov 30, 2023
@TomMelt TomMelt self-requested a review November 30, 2023 11:11
@jatkinson1000
Copy link
Member

Has this gone anywhere, or has it stagnated?

As far as I see it tests are the only thing preventing us from making a JOSS submission, and with at least 3 research projects using FTorch that are likely to publish soon this is now a matter of urgency.

For something minimal I was thinking we could have some CMake Test to build and run the SimpleNet example for a few different inputs and check the outputs are correct as an integration test.

@jatkinson1000 jatkinson1000 mentioned this pull request Apr 15, 2024
@dorchard
Copy link
Member Author

dorchard commented Apr 15, 2024

There's a start here, but it got a bit stalled as I could use some help with the cmake. Perhaps someone is able to work with me on this? @TomMelt ? Currently has an attempt at using CMake test and does a simple probe of the tensor conversion functions. I agree about running SimpleNet for some integration tests.

@jatkinson1000
Copy link
Member

jatkinson1000 commented Apr 15, 2024

Great, thanks @dorchard
Proposal from the morning is to briefly pause on this in favour of integration tests in #115 so we can make a JOSS submission, then return to this. We can certainly take a look and help with cmake, and the integration tests will use cmake so you might find those useful.

@TomMelt
Copy link
Member

TomMelt commented Apr 17, 2024

Hi @dorchard I'm more than happy to help with the CMake side of things. Let me know when is convenient.

git clone https://github.com/Goddard-Fortran-Ecosystem/pFUnit.git
mkdir pFUnit/build
cd pFUnit/build
cmake ..
Copy link
Contributor

@jwallwork23 jwallwork23 Nov 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try -DCMAKE_INSTALL_PREFIX=... to override the build path to avoid version number.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried this but it didn't seem to help. Pinning the pFUnit version for now (see fdc4d05).

@jwallwork23 jwallwork23 force-pushed the unit-testing branch 2 times, most recently from c84fee1 to 24ecea6 Compare November 15, 2024 16:41
@jwallwork23 jwallwork23 added the testing Related to FTorch testing label Nov 15, 2024
@jwallwork23
Copy link
Contributor

Perhaps this PR could just set up the infrastructure and add tests for the tensor constructors and we could address writing unit tests for the rest of FTorch in a follow-up PR?

@jwallwork23
Copy link
Contributor

Interestingly, I needed to set the integer kind to get these tests working on my Ubuntu laptop, too (see c30bc21). I'll update this to follow @TomMelt's 32-bit int approach once #137 is merged.

@jwallwork23
Copy link
Contributor

[Rebased on top of main to bring in linting changes]

@jwallwork23 jwallwork23 force-pushed the unit-testing branch 3 times, most recently from c384f86 to db4de15 Compare December 16, 2024 11:21
@jwallwork23
Copy link
Contributor

Thanks for the tips @TomMelt - with a few tweaks the tests now pass on Ubuntu. There's a build fail in the Windows CI, although I didn't edit the Windows CI workflow or the part of the CMakeLists that it errors at. Do you have any thoughts on what's gone wrong?
https://github.com/Cambridge-ICCS/FTorch/actions/runs/12351579042/job/34466833553?pr=76

@jwallwork23
Copy link
Contributor

Thanks for the tips @TomMelt - with a few tweaks the tests now pass on Ubuntu. There's a build fail in the Windows CI, although I didn't edit the Windows CI workflow or the part of the CMakeLists that it errors at. Do you have any thoughts on what's gone wrong? https://github.com/Cambridge-ICCS/FTorch/actions/runs/12351579042/job/34466833553?pr=76

Huh, I'm getting a similar error on a PR that doesn't change any of the build system or source code (#206): https://github.com/Cambridge-ICCS/FTorch/actions/runs/12352023942/job/34468182561?pr=206

@jwallwork23
Copy link
Contributor

Merged in the Windows fix from #207 in 23ce977. Turned off unit testing for Windows in f1e9add.

@jwallwork23 jwallwork23 marked this pull request as ready for review December 18, 2024 10:47
@jwallwork23
Copy link
Contributor

@dorchard I can't add you as a reviewer because you authored the initial commits, but any comments you have would be appreciated.

@jwallwork23
Copy link
Contributor

jwallwork23 commented Jan 6, 2025

I'm having issues building the unit tests, e.g.:

[ 37%] Building Fortran object test/unit/CMakeFiles/test_constructors.dir/test_constructors.F90.o
/Users/dorchard/Documents/iccs/FTorch/src/build/test/unit/test_constructors.F90:10:7:

   10 |   use pFUnit
      |       1

I am using pfunit-4.10 whose mod files seem to be named funit.mod instead (changing use pFunit to use funit in test_constructors.f90 makes things build...) Has there been a breaking interface change for pfunit? Is it because I built pfUnit skipping the OpenMP and MPI builds I wonder? I can't seem to identify whether this actually changes the mod file though. Maybe I am missing something obvious in the build setup.

Yes, you need to install with MPI to get pFUnit (p for parallel). However, I realised that isn't actually used at present so switched to use FUnit in d52c389 in the current tests, so that should work for you.

@jwallwork23
Copy link
Contributor

Note that I've opened #216 to fix a related issue with torch_tensor_from_array. It will merge into this PR.

Copy link
Member Author

@dorchard dorchard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a good starting point for our unit tests. I had one suggestion. Having a 3D test would also be good I think, at least on the torch_tensor_from_array side.

src/test/unit/test_constructors.pf Show resolved Hide resolved
src/test/unit/test_constructors.pf Outdated Show resolved Hide resolved
src/test/unit/test_constructors.pf Outdated Show resolved Hide resolved
@jwallwork23 jwallwork23 added the autograd Tasks towards the online training / automatic differentiation feature label Jan 6, 2025
@jwallwork23
Copy link
Contributor

I think this is a good starting point for our unit tests. I had one suggestion. Having a 3D test would also be good I think, at least on the torch_tensor_from_array side.

Good suggestion. I added this in 4dad25e, which required extending the assert_allclose test util to cover the 3D case.

jwallwork23 and others added 2 commits January 6, 2025 14:58
* Call from_blob inside from_array
* Accept requires_grad as logical rather than c_bool; test this arg
@dorchard
Copy link
Member Author

dorchard commented Jan 6, 2025

Looks good to go for me!

@jwallwork23 jwallwork23 mentioned this pull request Jan 6, 2025
Copy link
Member

@TomMelt TomMelt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @jwallwork23 and @dorchard , this is great. Really good to get unit tests into Ftorch 🙌

FWIW, I tested locally on my machine and it works great. Only change I had to make was to the CMAKE_PREFIX_PATH as mentioned in my review.

Once that is changed I think its HOTTOGO 🔥 (sorry couldn't resist a Chappell Roan reference)

.github/workflows/test_suite_ubuntu.yml Outdated Show resolved Hide resolved
.github/workflows/test_suite_ubuntu.yml Show resolved Hide resolved
.github/workflows/test_suite_ubuntu.yml Outdated Show resolved Hide resolved
@jwallwork23 jwallwork23 requested a review from TomMelt January 6, 2025 15:56
Copy link
Member

@TomMelt TomMelt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HOTTOGO 🔥

Copy link
Member

@jatkinson1000 jatkinson1000 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @jwallwork23, this is fantastic.
Since others have run the unit tests and we have been through them together I am going to trust that they are all OK since I can't run locally on my mac right now.

My comments/suggestions focus more on the docs and infrastructure and are minor.

.github/workflows/test_suite_ubuntu.yml Outdated Show resolved Hide resolved
.github/workflows/test_suite_ubuntu.yml Outdated Show resolved Hide resolved
.github/workflows/test_suite_ubuntu.yml Outdated Show resolved Hide resolved
pages/testing.md Outdated Show resolved Hide resolved
pages/testing.md Outdated Show resolved Hide resolved
.github/workflows/test_suite_ubuntu.yml Outdated Show resolved Hide resolved
src/test/README.md Show resolved Hide resolved
src/test/README.md Outdated Show resolved Hide resolved
src/test/unit/CMakeLists.txt Outdated Show resolved Hide resolved
pages/testing.md Outdated Show resolved Hide resolved
@jwallwork23
Copy link
Contributor

Thanks again everyone! Merging now 🚀

@jwallwork23 jwallwork23 merged commit 6055a7d into main Jan 6, 2025
6 checks passed
@jwallwork23 jwallwork23 deleted the unit-testing branch January 6, 2025 17:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
autograd Tasks towards the online training / automatic differentiation feature enhancement New feature or request testing Related to FTorch testing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unit testing
4 participants