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
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
1da38b1
simple pFunit test on torch_tensor_from_blog (somewhat WIP)
dorchard Nov 28, 2023
93735f9
readme file for tests
dorchard Nov 28, 2023
0fc5a7a
clarification about pfunit
dorchard Nov 28, 2023
763a2cf
Build fixes
jwallwork23 Nov 15, 2024
8b76e44
Fix syntax in test_ftorch; rename first test
jwallwork23 Nov 15, 2024
ac5ec91
Rename test helper script; update docs
jwallwork23 Nov 15, 2024
302d690
Build pFUnit on CI
jwallwork23 Nov 15, 2024
028e14a
Unit tests for zeroes and ones
jwallwork23 Jul 29, 2024
3d0e27a
Pass Torch_DIR to CI build
jwallwork23 Nov 15, 2024
2c048f3
Set CMake args identically to local setup
jwallwork23 Nov 15, 2024
75f974c
Drop 'FTorch REQUIRED' for unit test for now (see #133)
jwallwork23 Nov 15, 2024
3fc0393
Install MPI and build pFUnit with MPI
jwallwork23 Nov 15, 2024
6efb3b5
Pin pFUnit version for now
jwallwork23 Nov 15, 2024
9a4c298
Finish test_torch_tensor_zeros
jwallwork23 Nov 15, 2024
3b31eea
Finish test_torch_tensor_ones
jwallwork23 Nov 15, 2024
4cc8f70
Implement torch_tensor_empty
jwallwork23 Nov 18, 2024
48c9f3b
Check pointers are (not) associated in unit tests for constructors
jwallwork23 Nov 18, 2024
be0e2d1
Use parameters in constructor unit test
jwallwork23 Nov 18, 2024
4a0ec95
More on test for torch_tensor_from_blob
jwallwork23 Nov 19, 2024
b44a535
Add test for from_array
jwallwork23 Nov 19, 2024
13746d4
Format torch_tensor_empty
jwallwork23 Nov 25, 2024
b768248
Fix for from_array unit test
jwallwork23 Dec 10, 2024
cb20096
Fix for from_blob unit test
jwallwork23 Dec 10, 2024
51e9aa2
Fix for empty unit test
jwallwork23 Dec 10, 2024
b1e71e0
Fix for zeros unit test
jwallwork23 Dec 10, 2024
313cda1
Fix for ones unit test
jwallwork23 Dec 10, 2024
4f4ab30
Drop requires_grad argument for now
jwallwork23 Dec 10, 2024
664be32
Tweaks to test scripts
jwallwork23 Dec 10, 2024
463c298
Use ftorch_int for test_torch_from_array
jwallwork23 Dec 16, 2024
2415df1
Fix layout usage
jwallwork23 Dec 16, 2024
c49da28
Accept command line inputs for run_test_suite.sh
jwallwork23 Dec 16, 2024
48765c0
Don't build unit tests on Windows
jwallwork23 Dec 18, 2024
23d4a76
Fix test script name
jwallwork23 Dec 18, 2024
3574010
Fix help; lint
jwallwork23 Dec 23, 2024
f11d650
Merge branch 'main' into unit-testing
jwallwork23 Jan 6, 2025
ad67a0e
Separate 1d and 2d tests for from_array
jwallwork23 Jan 6, 2025
d52c389
FUnit is sufficient for now
jwallwork23 Jan 6, 2025
b96fece
switch rest of the imports to FUnit
dorchard Jan 6, 2025
a413d7b
Drop unused variable
jwallwork23 Jan 6, 2025
42f3451
Apply suggestion from @dorchard code review
jwallwork23 Jan 6, 2025
07ab337
Check array shapes
jwallwork23 Jan 6, 2025
4dad25e
Add 3D unit test for torch_tensor_from_array
jwallwork23 Jan 6, 2025
217f31a
Apply suggestions from @dorchard code review
jwallwork23 Jan 6, 2025
f24db4d
Call from_blob inside from_array (#216)
jwallwork23 Jan 6, 2025
cbdefb3
Apply suggestions from @TomMelt code review
jwallwork23 Jan 6, 2025
80c1d18
Apply suggestions from @jatkinson1000 code review
jwallwork23 Jan 6, 2025
ef4615e
Apply other suggestions from @jatkinson1000 code review
jwallwork23 Jan 6, 2025
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
26 changes: 23 additions & 3 deletions .github/workflows/test_suite_ubuntu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,44 @@ jobs:
. ftorch/bin/activate
pip install torch torchvision --index-url https://download.pytorch.org/whl/cpu

# MPI is required by pFUnit
- name: Install an MPI distribution
run: |
sudo apt update
sudo apt install mpich

- name: Install pFUnit
run: |
export FC=/usr/bin/gfortran
export MPIF90=/usr/bin/mpif90
# TODO: Avoid version pinning (needed because version appears in install path)
git clone -b v4.10.0 https://github.com/Goddard-Fortran-Ecosystem/pFUnit.git
mkdir pFUnit/build
cd pFUnit/build
cmake ..
make -j 4 install

- name: Build FTorch
run: |
. ftorch/bin/activate
VN=$(python -c "import sys; print('.'.join(sys.version.split('.')[:2]))")
export Torch_DIR=${VIRTUAL_ENV}/lib/python${VN}/site-packages
export BUILD_DIR=$(pwd)/src/build
# NOTE: The pFUnit version (pinned during installation above) is used in the install path.
export PFUNIT_DIR=$(pwd)/pFUnit/build/installed/PFUNIT-4.10
TomMelt marked this conversation as resolved.
Show resolved Hide resolved
mkdir ${BUILD_DIR}
cd ${BUILD_DIR}
cmake .. \
-DPython_EXECUTABLE="$(which python)" \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX=${BUILD_DIR} \
-DCMAKE_BUILD_TESTS=TRUE \
-DCMAKE_Fortran_FLAGS="-std=${{matrix.std}}"
-DCMAKE_PREFIX_PATH="${PFUNIT_DIR};${Torch_DIR}" \
-DCMAKE_Fortran_FLAGS="-std=${{ matrix.std }}"
cmake --build .
cmake --install .

- name: Integration tests
- name: Test suite
run: |
. ftorch/bin/activate
./run_integration_tests.sh -V
./run_test_suite.sh -V
2 changes: 1 addition & 1 deletion .github/workflows/test_suite_windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,4 @@ jobs:
for /f "tokens=2*" %%i in ('pip show torch ^| findstr /R "^Location"') do set torch_path=%%i
set PATH=C:\Program Files (x86)\FTorch\bin;%PATH%
set PATH=%torch_path%\torch\lib;%PATH%
run_integration_tests.bat
run_test_suite.bat
35 changes: 19 additions & 16 deletions pages/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,38 @@ title: FTorch test suite

## Testing

FTorch's test suite currently comprises of integration tests based on a subset
of the [examples](examples.html). These tests are built and run and their
outputs are analysed to check they contain expected regular expressions.
FTorch's test suite is currently comprised of unit tests, as well as integration
tests based on a subset of the [examples](examples.html). These tests are built
and run and their outputs are analysed.

### Building the integration tests
### Building

To enable FTorch's integration tests, ensure that the `CMAKE_BUILD_TESTS` option
is set to `TRUE` for the build i.e., `-DCMAKE_BUILD_TESTS=True`.
To enable FTorch's test suite, ensure that the `CMAKE_BUILD_TESTS` option
is set to `TRUE` for the build, i.e., `-DCMAKE_BUILD_TESTS=True`.. For the unit
tests, you will also need to install
[pFUnit](https://github.com/Goddard-Fortran-Ecosystem/pFUnit) and pass its
install directory to the `CMAKE_PREFIX_PATH` when building FTorch.

### Running the integration tests
### Running

Once the build is complete, activate the Python virtual environment you created
for FTorch<sup>1</sup> and simply run the
[helper script](https://github.com/Cambridge-ICCS/FTorch/blob/main/run_integration_tests.sh)
in the root FTorch directory. Depending on the OS you are running you will need
to use either:

- `./run_integration_tests.sh` for unix (mac and linux)
- `run_integration_tests.bat` for windows
```
- `./run_test_suite.sh` for Unix (Mac and Linux)
- `run_test_suite.bat` for Windows (note this will only run the integration tests, not unit tests).

This will automatically install any additional Python dependencies for the
examples.

Alternatively, individual tests may be run by going to the corresponding
subdirectory of `${BUILD_DIR}/test/examples` (where `${BUILD_DIR}` is the build
directory for FTorch) and calling `ctest`. This will produce a report on which
tests passed and which failed for your build. Note that some of the examples
have additional dependencies, which may need installing into your virtual
environment.
Alternatively, individual tests may be run by going to `${BUILD_DIR}/test/unit`
or the corresponding subdirectory of `${BUILD_DIR}/test/examples` (where
`${BUILD_DIR}` is the build directory for FTorch) and calling `ctest`. This
will produce a report on which tests passed and which failed for your build.
Note that some of the examples have additional dependencies, which may need
installing into your virtual environment.

> <sup>1</sup> _If you built FTorch against libtorch (rather than creating a
virtual environment) then either
Expand Down
27 changes: 0 additions & 27 deletions run_integration_tests.sh

This file was deleted.

3 changes: 3 additions & 0 deletions run_integration_tests.bat → run_test_suite.bat
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ rem
rem See `src/test/README.md` for more details on integration testing.
rem ---

rem NOTE: This version of run_test_suite only runs the integration tests, not
rem the unit tests. These are not currently supported on Windows.

for /d %%i in (1_SimpleNet 2_ResNet18 4_MultiIO) do (
pushd src\build\test\examples\%%i
rem run the tests
Expand Down
75 changes: 75 additions & 0 deletions run_test_suite.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#!/bin/bash
# ---
# Execute this shell script to run FTorch's test suite. This includes both unit
# tests and integration tests.
#
# Assumes FTorch has been built with the `-DCMAKE_BUILD_TESTS=TRUE` option.
# The `BUILD_DIR` variable in this script should be updated as appropriate for
# your configuration.
#
# See `src/test/README.md` for more details on the test suite.
# ---

set -eu

# Function to display help text
show_help() {
echo "Usage: $0 [BUILD_DIR=<build_dir>] [--verbose | -V]"
echo
echo "Options:"
echo " BUILD_DIR=<build_dir> Specify the build directory (default: src/build)."
echo " --verbose | -V Run with verbose ctest output."
echo " --help | -h Show this help message and exit."
}

# Parse command line arguments
BUILD_DIR="src/build"
VERBOSE=false
HELP=false
for ARG in "$@"; do
case ${ARG} in
BUILD_DIR=*)
BUILD_DIR="${ARG#*=}"
;;
--verbose | -V)
VERBOSE=true
shift
;;
--help | -h)
HELP=true
shift
;;
*)
echo "Unknown argument: ${ARG}"
show_help
exit 1
;;
esac
done

# Check for --help option
if [ "${HELP}" = true ]; then
show_help
exit 0
fi

# Process command line arguments
if [ "${VERBOSE}" = true ]; then
CTEST_ARGS="-V"
else
CTEST_ARGS=""
fi

# Run unit tests
cd "${BUILD_DIR}/test/unit"
ctest "${CTEST_ARGS}"
cd -

# Run integration tests
EXAMPLES="1_SimpleNet 2_ResNet18 4_MultiIO 6_Autograd"
for EXAMPLE in ${EXAMPLES}; do
pip -q install -r examples/"${EXAMPLE}"/requirements.txt
cd "${BUILD_DIR}"/test/examples/"${EXAMPLE}"
ctest "${CTEST_ARGS}"
cd -
done
9 changes: 9 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ if(CMAKE_BUILD_TESTS)
COMPONENTS Interpreter
REQUIRED)

enable_testing()

# Unit tests
# NOTE: We do not currently support unit testing on Windows
if(UNIX)
add_subdirectory(test/unit)
endif()

# Integration tests
file(MAKE_DIRECTORY test/examples)
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/../examples/CMakeLists.txt
DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}/test/examples)
Expand Down
Loading
Loading