Skip to content

PyTorchAlpaka: Batched inference with TensorCollections, eval and frozen model, FP16 convsersion support#50498

Draft
EmanueleCoradin wants to merge 1 commit intocms-sw:masterfrom
EmanueleCoradin:batched-inference-PR
Draft

PyTorchAlpaka: Batched inference with TensorCollections, eval and frozen model, FP16 convsersion support#50498
EmanueleCoradin wants to merge 1 commit intocms-sw:masterfrom
EmanueleCoradin:batched-inference-PR

Conversation

@EmanueleCoradin
Copy link

@EmanueleCoradin EmanueleCoradin commented Mar 23, 2026

This PR results from a collaboration with @valsdav. Thanks also to @Electricks94, who helped implement batched inference support.

PR description:

This PR introduces some new features to the PyTorchAlpaka interface:

  • Batched inference support: a single SoA can now be splitted in multiple TensorCollection batches.
  • Eval and Frozen model: Model wrapper now enforces that eval and optionally frozen mode. TorchScript modules cannot be moved to another device once they have been fronzen.
  • Added optional half-precision conversion for input tensors during inference (to_half flag). If the model was exported in FP16 format, the whole inference will be performed in half-precision format, reducing peak GPU memory.

Validation

To validate the PR, I prepared two producers to launch inference in mini-batches: SimpleNetMiniBatch and TinyResNetMiniBatch.
I then modified InspectionSink.cc to check that the relative difference between the output of the default and batch implementation is lower than a compatibility threshold (default 1e-5).
You can launch the tests by:

cd src/PhysicsTools/PyTorchAlpakaTest/test/
cmsRun runPyTorchAlpakaTest.py  --compareBatch -e 3

@cmsbuild
Copy link
Contributor

cmsbuild commented Mar 23, 2026

cms-bot internal usage

@cmsbuild
Copy link
Contributor

@mmusich
Copy link
Contributor

mmusich commented Mar 23, 2026

type ngt

@cmsbuild
Copy link
Contributor

-code-checks

Logs: https://cmssdt.cern.ch/SDT/code-checks/cms-sw-PR-50498/48680

Code check has found code style and quality issues which could be resolved by applying following patch(s)

@cmsbuild
Copy link
Contributor

-code-checks

Logs: https://cmssdt.cern.ch/SDT/code-checks/cms-sw-PR-50498/48687

Code check has found code style and quality issues which could be resolved by applying following patch(s)

@cmsbuild
Copy link
Contributor

@cmsbuild
Copy link
Contributor

Pull request #50498 was updated.

@cmsbuild
Copy link
Contributor

@cmsbuild
Copy link
Contributor

Pull request #50498 was updated.

@cmsbuild
Copy link
Contributor

@cmsbuild
Copy link
Contributor

Pull request #50498 was updated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants