PyTorchAlpaka: Batched inference with TensorCollections, eval and frozen model, FP16 convsersion support - #50498
Conversation
|
cms-bot internal usage |
|
+code-checks Logs: https://cmssdt.cern.ch/SDT/code-checks/cms-sw-PR-50498/48664 |
|
type ngt |
|
-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)
|
|
-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)
|
|
+code-checks Logs: https://cmssdt.cern.ch/SDT/code-checks/cms-sw-PR-50498/48691 |
|
Pull request #50498 was updated. |
3047b29 to
d8d412e
Compare
c25bbaf to
82379ed
Compare
|
+code-checks Logs: https://cmssdt.cern.ch/SDT/code-checks/cms-sw-PR-50498/49074
|
|
Pull request #50498 was updated. |
82379ed to
9f505be
Compare
|
+code-checks Logs: https://cmssdt.cern.ch/SDT/code-checks/cms-sw-PR-50498/49075
|
|
Pull request #50498 was updated. |
|
@cmsbuild, please test |
|
A new Pull Request was created by @EmanueleCoradin for master. It involves the following packages:
@Dr15Jones, @fwyzard, @hjkwon260, @makortel, @smuzaffar, @valsdav, @y19y19 can you please review it and eventually sign? Thanks. cms-bot commands are listed here |
There was a problem hiding this comment.
I think DataFormats/Math (or HeterogeneousCore/AlpakaMath) would be a better place for this code.
| GENERATE_SOA_LAYOUT(ParticleLayoutFPX, SOA_COLUMN(FPX, pt), SOA_COLUMN(FPX, eta), SOA_COLUMN(FPX, phi)) | ||
| using ParticleSoAFPX = ParticleLayoutFPX<>; |
There was a problem hiding this comment.
The use of FPX here introduces an ODR violation, no?
The code compiled with ALPAKA_ACC_GPU_CUDA_ENABLED sees the version with __half, while other code sees float.
|
-1 Failed Tests: UnitTests Failed Unit TestsI found 1 errors in the following unit tests: ---> test testSoADataTypesSerialSync had ERRORS Comparison SummarySummary:
|
|
Let me expand on @makortel's comment. The approach to using half precision explored in this PR is not really workable: much of the work done implementing the SoA interface and the Using a type with a different size on different back-ends breaks this assumption. This makes it impossible to transparently copy the data between host and device, and to automatically convert between a host and device collection. More in general, why do you propose to limit the use of fp16 to the CUDA backend ? |
|
-heterogeneous |
|
I would propose to split this PR in two parts, one that adds support for batches, one that adds support for fp16. For the batching part, I would suggest to investigate if it's possible to support a tensor size that is not a multiple of the batch size. For the fp16 part, if usage of fp16 should only ever be internal to Torch, it should not be exposed as a data type to CMSSW. If usage of fp16 should be exposed to CMSSW, please define or use an fp16 type that is available and behaves in the same way on all devices. namespace cms {
#if defined(ALPAKA_ACC_GPU_CUDA_ENABLED) or defined(ALPAKA_ACC_GPU_HIP_ENABLED)
// on NVIDIA or AMD GPUs
using float16_t = ::__half;
#else
// on CPU
using float16_t = ::_Float16;
#endif
} // namespace cms |
|
Thanks @makortel and @fwyzard for the feedback. My intention with FPX was to provide a way for users to store data in SoA format using FP16 and pass it to PyTorch. Following @fwyzard, I will close this PR and split it into two updated parts. |
This PR results from a collaboration with @valsdav. Thanks also to @Electricks94, who helped implement batched inference support and gave me great suggestions to improve the code.
PR description:
This PR extends the PyTorchAlpaka interface with support for batched inference, half-precision execution, and improved model lifecycle handling.
Main features
Batched inference via TensorCollection: Introduces native support for splitting a single SoA into multiple logical batches. TensorCollection now supports (batch_size, total_size) construction and computes per-batch offsets internally. Constraint: total_size must be divisible by batch_size (no support for partial batches)
FPX abstraction (FP32 / FP16 portability): Inspired by the definition used by LSTCore, it adds a portable floating-point type (FPX) which
A PyTorch bridge is included to map __half → torch::kHalf.
to()function is overloaded, supporting dtype-aware device transfers.Validation
Validation is performed via dedicated mini-batch inference producers:
SimpleNetMiniBatchTinyResNetMiniBatchThe latter reproduces the same inference as the non-batched implementations using the new batching API and the FPX data type.
InspectionSinkhas been extended to compare outputs between the standard and the batched inference.A relative difference check is enforced:
|ref - batched| / ref < 1e-5It can be run by:
Notes / limitations