Skip to content

Overhaul pt2ts#555

Merged
joewallwork merged 73 commits intomainfrom
368_rework-pt2ts
Mar 26, 2026
Merged

Overhaul pt2ts#555
joewallwork merged 73 commits intomainfrom
368_rework-pt2ts

Conversation

@joewallwork
Copy link
Copy Markdown
Collaborator

@joewallwork joewallwork commented Feb 25, 2026

Closes #368

For a while I've thought that it'd be more appropriate to have pt2ts as a command-line script that reads in a PyTorch model and writes out a TorchScript model. That's done in this PR.

I feel that other "template" code that's currently in there would be better off existing in examples/integration tests. Some of the testing code can be included in the main pt2ts script. Further, I think we should drop the duplicated pt2ts.py files from the examples, move any specific testing part of them (under if __name__ == "__main__:") to the bottom of the corresponding net definition files (e.g., simplenet.py), and make use of the new pt2ts script in each example. (This has the bonus of testing it, which isn't currently done for the template in utils/pt2ts.py.)

Note that I've made a recommendation to switch to a .ts extension for TorchScript files, although this is just personal preference.

Proposed workplan

  • Separate TorchScript handling functions into a new Python module called ftorch_utils
  • Make use of the Python module in a new pt2ts.py script
  • Update utils README
  • Set ftorch_utils up as a proper Python module
  • Set pt2ts up as a script that gets installed in the Python environment.
  • Install ftorch_utils in the CI
  • Move the testing parts of the pt2ts.py scripts found in the examples into the corresponding net definition files
    • SimpleNet
    • Batching
    • MultiIO
    • Looping
    • MultiGPU
    • MPI
  • Make use of the main pt2ts.py script in the examples
    • SimpleNet
    • Batching
    • MultiIO
    • Looping
    • MultiGPU
    • MPI
  • Update example READMEs
    • SimpleNet
    • Batching
    • MultiIO
    • Looping
    • MultiGPU
    • MPI
  • Update documentation
  • Changelog

I propose to defer the following to follow-up PRs:

@joewallwork joewallwork self-assigned this Feb 25, 2026
@joewallwork joewallwork added the enhancement New feature or request label Feb 25, 2026
@jatkinson1000
Copy link
Copy Markdown
Member

It's come up a couple of times, but there are good reasons not to use .ts as it's picked up by many systems as TypeScript.

@joewallwork
Copy link
Copy Markdown
Collaborator Author

It's come up a couple of times, but there are good reasons not to use .ts as it's picked up by many systems as TypeScript.

Ah okay fair enough. I'll modify it to stick with .pt then.

@joewallwork
Copy link
Copy Markdown
Collaborator Author

@copilot Fix the issue with the Windows CI.

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 20, 2026

@joewallwork I've opened a new pull request, #566, to work on those changes. Once the pull request is ready, I'll request review from you.

* Fix Windows CI: pass venv Python executable to CMake
* Consistent capitalisation

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: joewallwork <22053413+joewallwork@users.noreply.github.com>
@joewallwork
Copy link
Copy Markdown
Collaborator Author

@jatkinson1000 this is now ready for re-review. I found an unrelated problem with the Windows CI that it wasn't actually running any tests because none of the --test-regex example1 etc. regexes were picking anything up after we changed the naming scheme. That has now been fixed.

Copy link
Copy Markdown
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 @joewallwork this is amazing and will make life a lot easier going forwards!

A few small comments, some of which are nits so I think this is really close to merging.

- [CMake](https://cmake.org/) >= 3.18
- Fortran (2008 standard compliant), C++ (must fully support C++17), and C compilers
- [LibTorch](https://pytorch.org/cppdocs/installing.html)[^1] or [PyTorch](https://pytorch.org/)
- TorchVision and NumPy (required for examples)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

PyTorch also explicitly required for examples (not just LibTorch)?
As is mpi4py, matplotlib and more (mentioned below)...

Perhaps we don't specify modules here, or just provide "Other Python dependencies are required for examples/tests" and link to the below section?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Addressing this in #570 by modernising the pip install approach.

It provides the user with the option to [jit.script](https://pytorch.org/docs/stable/generated/torch.jit.script.html#torch.jit.script) or [jit.trace](https://pytorch.org/docs/stable/generated/torch.jit.trace.html#torch.jit.trace).

Dependencies:
- PyTorch
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

pip has some odd things to say here: https://pip.pypa.io/en/stable/cli/pip_install/#finding-packages

But happy to merge this if it seems to be working correctly.

There is no priority in the locations that are searched. Rather they are all checked, and the “best” match for the requirements (in terms of version number - see the specification for details) is selected.

Copy link
Copy Markdown
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 @joewallwork

LGTM pending merge of #570 and two typos, thanks!

joewallwork and others added 2 commits March 26, 2026 14:36
Co-authored-by: Jack Atkinson <109271713+jatkinson1000@users.noreply.github.com>
@joewallwork joewallwork merged commit 137e946 into main Mar 26, 2026
16 checks passed
@joewallwork joewallwork deleted the 368_rework-pt2ts branch March 26, 2026 15:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Create utility library to avoid redundancy in pt2ts.py across examples

3 participants