Skip to content
Merged
Show file tree
Hide file tree
Changes from 35 commits
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
92d6b04
Make pt2ts runnable
joewallwork Feb 25, 2026
006159d
Rename as ftorch_utils and update README
joewallwork Feb 25, 2026
5d57e4d
Separate TorchScript utils into a module
joewallwork Feb 25, 2026
71a8095
Don't accept .ts; convert some errors to warnings
joewallwork Feb 26, 2026
ff93349
Implement load_pytorch_model
joewallwork Mar 4, 2026
ea33c64
Use load_pytorch in pt2ts.py
joewallwork Mar 4, 2026
0aacac2
Move to pyproject.toml
joewallwork Mar 4, 2026
38767df
Install pt2ts into the Python environment
joewallwork Mar 4, 2026
6e775a0
Install ftorch_utils in CI workflows
joewallwork Mar 4, 2026
93afbee
Use torchscript module in examples
joewallwork Mar 4, 2026
b194aa8
DO NOT MERGE: drop workflow changes other than Ubuntu/GNU for now
joewallwork Mar 4, 2026
aba9e1a
Allow existing build dir
joewallwork Mar 4, 2026
1f3398e
Need to activate venv before installing
joewallwork Mar 4, 2026
e8ea1d4
Fix handling of tracing mode
joewallwork Mar 4, 2026
924c41b
Allow writing model and input tensor in simplenet.py
joewallwork Mar 4, 2026
9a08442
Enable testing in pt2ts
joewallwork Mar 4, 2026
a3bf887
Use pt2ts script in SimpleNet example
joewallwork Mar 4, 2026
4f4331a
Fix typo
joewallwork Mar 6, 2026
8d3eedd
TODO: Accept pre-trained models - see ResNet in git stash
joewallwork Mar 4, 2026
a7f0a2d
Use pt2ts script in Batching example
joewallwork Mar 6, 2026
c7c849f
assert_allclose->allclose
joewallwork Mar 6, 2026
b0378fd
Use pt2ts script in MultiIO example
joewallwork Mar 6, 2026
970e499
Account for multiple input tensors in pt2ts script
joewallwork Mar 6, 2026
0db05b3
Drop pt2ts.py from looping example
joewallwork Mar 6, 2026
39c0d1c
Account for single tensor inputs, too
joewallwork Mar 6, 2026
669375f
Introduce functions for subtasks of pt2ts
joewallwork Mar 6, 2026
c14278e
Merge branch 'main' into 368_rework-pt2ts
joewallwork Mar 6, 2026
f8626b7
Changelog
joewallwork Mar 6, 2026
d08e932
Fix pt2ts
joewallwork Mar 6, 2026
54a37d5
Use pt2ts script in MPI example
joewallwork Mar 11, 2026
996a4ee
Use pt2ts script in MultiGPU example
joewallwork Mar 11, 2026
73b13f5
Consistent import strategy for argparse
joewallwork Mar 11, 2026
8c85ee9
Lint
joewallwork Mar 11, 2026
30c0aef
Update example READMEs
joewallwork Mar 11, 2026
6263802
Update mention of pt2ts script in the docs
joewallwork Mar 11, 2026
268bd7a
Apply suggestions from @jatkinson1000 code review
joewallwork Mar 17, 2026
fecde80
Fix build dir issue in static analysis workflow
joewallwork Mar 17, 2026
2b10d54
Ruff lint
joewallwork Mar 17, 2026
375a226
Add CHANGELOG note on changed process for saving models
joewallwork Mar 17, 2026
4deee2e
Update ftorch_utils README
joewallwork Mar 17, 2026
166572c
Adopt ftorch-utils and update description
joewallwork Mar 17, 2026
224990f
Update authors and maintainers
joewallwork Mar 17, 2026
7136b85
Undo line length change
joewallwork Mar 17, 2026
f2807ef
Update generic example in docs
joewallwork Mar 17, 2026
bda9405
Refer to pt2ts docs from gpu docs
joewallwork Mar 17, 2026
70632ab
Add torch to pyproject dependencies
joewallwork Mar 17, 2026
a24f218
Improve clarity of GPU device docs
joewallwork Mar 17, 2026
06655cf
Add note on ResNet example
joewallwork Mar 17, 2026
444f047
Convert matching filename warning to error
joewallwork Mar 17, 2026
2009946
Add missing option to README
joewallwork Mar 18, 2026
e9cb60b
Rename ResNet old-style pt2ts to write_torchscript
joewallwork Mar 20, 2026
fcdbbb4
Use better naming scheme for PyTorch and TorchScript model files
joewallwork Mar 20, 2026
3767fe9
Tweak pt2ts arguments and update examples
joewallwork Mar 20, 2026
f1f6f06
Save example models in binary dirs
joewallwork Mar 20, 2026
f5eb59e
Add numpy and torchvision to core dependencies
joewallwork Mar 20, 2026
2d4747a
Use --extra-index-url and mention in docs
joewallwork Mar 20, 2026
52666ac
More places where mkdir -p is needed
joewallwork Mar 20, 2026
aa2479c
Don't use --extra-index-url on Windows
joewallwork Mar 20, 2026
788b824
Fix example regexes in Windows CI
joewallwork Mar 20, 2026
0377431
Fix Windows CI: pass venv Python executable to CMake (#566)
Copilot Mar 20, 2026
7944250
Update ftorch_utils/README to reflect changes to pt2ts arguments
joewallwork Mar 20, 2026
5247ad8
Merge branch 'main' into 368_rework-pt2ts
joewallwork Mar 25, 2026
dd7249e
Add matplotlib to dependencies for ftorch_utils
joewallwork Mar 25, 2026
f3ad2e2
Apply suggestions from @jatkinson1000 code review
joewallwork Mar 25, 2026
be09700
Apply ruff format
joewallwork Mar 26, 2026
b1b342a
Add clarifying note on installing torch and torchvision
joewallwork Mar 26, 2026
c0d258f
Improve docs on installing dependencies on GPU devices
joewallwork Mar 26, 2026
313c902
Drop file accidentally added in merge
joewallwork Mar 26, 2026
9267d26
Fix code snippet in MultiGPU README
joewallwork Mar 26, 2026
cb3a26f
Add release checklist step for updating ftorch_utils version
joewallwork Mar 26, 2026
06818b4
Merge branch 'main' into 368_rework-pt2ts
joewallwork Mar 26, 2026
2634080
Apply suggestions from @jatkinson1000 code review
joewallwork Mar 26, 2026
8c380ec
Move to dependency groups approach for `pip install`s (#570)
joewallwork Mar 26, 2026
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
4 changes: 2 additions & 2 deletions .github/workflows/static_analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ on:
- '**.py'
- '**.sh'
- '**CMakeLists.txt'
- 'requirements-dev.txt'
- 'pyproject.toml'

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
Expand Down Expand Up @@ -66,7 +66,7 @@ jobs:
python -m venv ../ftorch_venv
. ../ftorch_venv/bin/activate
pip install torch torchvision --index-url https://download.pytorch.org/whl/cpu
pip install -r requirements-dev.txt
pip install -e .[dev]
Comment thread
joewallwork marked this conversation as resolved.
Outdated

# Run CMake build to get compile commands for clang
- name: FTorch CMake
Expand Down
7 changes: 6 additions & 1 deletion .github/workflows/test_suite_ubuntu_cpu_gnu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ jobs:
. ftorch/bin/activate
pip install torch torchvision --index-url https://download.pytorch.org/whl/cpu

- name: Install ftorch_utils
run: |
. ftorch/bin/activate
python -m pip install .

- name: Install OpenMPI
run: |
sudo apt update
Expand All @@ -148,12 +153,12 @@ jobs:
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)/build
mkdir -p ${BUILD_DIR}
Comment thread
jatkinson1000 marked this conversation as resolved.
# Install to /tmp/ to ensure independent from the CMake build in standalone check.
export INSTALL_DIR=/tmp/ftorch-install
mkdir -p ${INSTALL_DIR}
# NOTE: The pFUnit version (pinned during installation above) is used in the install path.
export PFUNIT_DIR=$(pwd)/pFUnit/build/installed/PFUNIT-4.12
mkdir ${BUILD_DIR}
cd ${BUILD_DIR}

cmake .. \
Expand Down
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,19 @@ For specific details see the [FTorch online documentation](https://cambridge-icc

### Added

- Introduced a central, installable `pt2ts` script
[#555](https://github.com/Cambridge-ICCS/FTorch/pull/555)

### Changed

### Removed
- Renamed `utils` as `ftorch_utils` and made it a proper Python module
[#555](https://github.com/Cambridge-ICCS/FTorch/pull/555)
Comment thread
jatkinson1000 marked this conversation as resolved.

### Removed

- Removed `pt2ts.py` scripts from `utils` and all examples
[#555](https://github.com/Cambridge-ICCS/FTorch/pull/555)

### Fixed

### Patch Releases
Expand Down
24 changes: 16 additions & 8 deletions examples/2_SimpleNet/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,25 @@ target_link_libraries(simplenet_infer_fortran PRIVATE FTorch::ftorch)
if(CMAKE_BUILD_TESTS)
include(CTest)

# 1. Check the PyTorch model runs and its outputs meet expectations
add_test(NAME example_simplenet_simplenet
COMMAND ${Python_EXECUTABLE} ${PROJECT_SOURCE_DIR}/simplenet.py)
# 1. Write the PyTorch model out and check it runs and its outputs meet
# expectations
add_test(
NAME example_simplenet_simplenet
COMMAND ${Python_EXECUTABLE} ${PROJECT_SOURCE_DIR}/simplenet.py
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR})

# 2. Check the model is saved to file in the expected location with the
# pt2ts.py script
# 2. Run the pt2ts script to convert the model file to TorchScript format
# and run basic checks
add_test(
NAME example_simplenet_pt2ts
COMMAND ${Python_EXECUTABLE} ${PROJECT_SOURCE_DIR}/pt2ts.py --filepath
${PROJECT_BINARY_DIR}
WORKING_DIRECTORY ${PROJECT_BINARY_DIR})
COMMAND pt2ts ${PROJECT_SOURCE_DIR}/simplenet.py
SimpleNet
${PROJECT_SOURCE_DIR}/saved_simplenet_model_cpu.pt
--input_tensor_file
${PROJECT_SOURCE_DIR}/saved_simplenet_input_tensor_cpu.pt
Comment thread
jatkinson1000 marked this conversation as resolved.
Outdated
--output_model_file
${PROJECT_BINARY_DIR}/saved_simplenet_model_cpu.pt
--test)

# 3. Check the model can be loaded from file and run in Python and that its
# outputs meet expectations
Expand Down
18 changes: 11 additions & 7 deletions examples/2_SimpleNet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ covered in later examples.

## Description

A Python file `simplenet.py` is provided that defines a very simple PyTorch 'net' that takes an input
vector of length 5 and applies a single `Linear` layer to multiply it by 2.
A Python file `simplenet.py` is provided that defines a very simple PyTorch
'net' that takes an input vector of length 5 and applies a single `Linear` layer
to multiply it by 2. Running this file as a script will write the model out in
PyTorch's `.pt` file format.

A modified version of the `pt2ts.py` tool saves this simple net to TorchScript.
The `pt2ts` tool is used to convert the model file to the TorchScript format.

A series of files `simplenet_infer_<LANG>` then bind from other languages to run the
TorchScript model in inference mode.
Expand Down Expand Up @@ -45,13 +47,15 @@ This defines the net and runs it with an input tensor [0.0, 1.0, 2.0, 3.0, 4.0]
```
Model output: tensor([[0., 2., 4., 6., 8.]])
```
You should find that a PyTorch model file `saved_simplenet_model_cpu.pt` is
created.

To save the SimpleNet model to TorchScript run the modified version of the
`pt2ts.py` tool:
To convert the SimpleNet model to TorchScript run the `pt2ts` script:
```
python3 pt2ts.py
pt2ts simplenet.py SimpleNet saved_simplenet_model_cpu.pt
```
which will generate `saved_simplenet_model_cpu.pt` - the TorchScript instance of the net.
Without any further arguments, this will overwrite the
`saved_simplenet_model_cpu.pt` model file so you will receive a warning.

You can check that everything is working by running the `simplenet_infer_python.py` script:
```
Expand Down
201 changes: 0 additions & 201 deletions examples/2_SimpleNet/pt2ts.py

This file was deleted.

32 changes: 28 additions & 4 deletions examples/2_SimpleNet/simplenet.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,39 @@ def forward(self, batch: torch.Tensor) -> torch.Tensor:


if __name__ == "__main__":
model = SimpleNet()
import argparse

# Parse user input
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
"--device_type",
help="Device type to run the inference on",
type=str,
choices=["cpu", "cuda", "hip", "xpu", "mps"],
default="cpu",
)
parsed_args = parser.parse_args()
device_type = parsed_args.device_type

# Construct an instance of the SimpleNet model on the specified device
model = SimpleNet().to(device_type)
model.eval()

input_tensor = torch.Tensor([0.0, 1.0, 2.0, 3.0, 4.0])
with torch.inference_mode():
output_tensor = model(input_tensor)
# Save the model in PyTorch format
torch.save(model.state_dict(), f"saved_simplenet_model_{device_type}.pt")

# Create an arbitrary input tensor and save it in PyTorch format
input_tensor = torch.Tensor([0.0, 1.0, 2.0, 3.0, 4.0]).to(device_type)
torch.save(input_tensor, f"saved_simplenet_input_tensor_{device_type}.pt")

# Propagate the input tensor through the model
with torch.inference_mode():
output_tensor = model(input_tensor).to("cpu")
print(f"Model output: {output_tensor}")

# Perform a basic check of the model output
if not torch.allclose(output_tensor, 2 * input_tensor):
result_error = (
f"result:\n{output_tensor}\ndoes not match expected value:\n"
Expand Down
Loading
Loading