Skip to content

Feature: batchnorm gpu test#14

Open
harz05 wants to merge 2 commits intoML4EP:gpu/alpakafrom
harz05:feat/batchnorm-gpu-test
Open

Feature: batchnorm gpu test#14
harz05 wants to merge 2 commits intoML4EP:gpu/alpakafrom
harz05:feat/batchnorm-gpu-test

Conversation

@harz05
Copy link
Copy Markdown

@harz05 harz05 commented Mar 20, 2026

Closes #4

This implements the 3 GPU/Alpaka methods for ROperator_BatchNormalization and adds a test in the Alpaka CUDA test suite.

ROperator_BatchNormalization.hxx

  • Generate_GPU_Kernel_ALPAKA : elementwise kernel struct. Initialize() already pre-expands per-channel scale/bias/mean tensors to the full [N,C,H,W] shape and folds 1/sqrt(var + eps) into scale, so the per-element formula reduces to y[i] = (x[i] - mean[i]) * scale[i] + bias[i]. Kernel pattern is same like other operators
  • Generate_GPU_Kernel_Definitions_ALPAKA : declares the kernel instance used at launch.
  • Generate_GPU_ALPAKA : sets up work division via alpaka::getValidWorkDiv and dispatches the kernel with alpaka::exec. Uses the deviceBuf_<name> buffers for all weight tensors consistent with how RModel_ALPAKA.cxx allocates them.

Files for testing:

  • BatchNormModelGenerator.py : creates BatchNorm.onnx from nn.BatchNorm2d(2) in eval mode with fixed weights. Input shape (1, 2, 2, 2), scale [1, 2], bias [0, 0.5], running mean [0.5, 3], running var [1, 4]. Also prints the reference floats used in the .ref.hxx.
  • input_models/BatchNorm.onnx : the exported ONNX model (opset 13).
  • input_models/references/BatchNorm.ref.hxx : 8 reference output values computed by PyTorch.
  • TestCustomModelsFromONNXForAlpakaCuda.cxx : added SofieAlpakaTest.BatchNorm which loads the model on GPU, runs inference, and checks each output element against the reference within tolerance.

Testing:

Tested on Google Colab (NVIDIA T4, CUDA 12.x). New test passes and all 26 tests in the suite pass with no regressions.

image

Copy link
Copy Markdown
Member

@sanjibansg sanjibansg left a comment

Choose a reason for hiding this comment

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

Hi @harz05,
Thanks for the PR. A question regarding the onnx file for testing.

Comment on lines +2 to +6
"""Generate BatchNorm.onnx and print reference output values for BatchNorm.ref.hxx.

Model: nn.BatchNorm2d(2) in eval mode, input shape (1, 2, 2, 2).
"""

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.

How will this file be executed? Also, since you provide the .onnx file anyway, how will this script be useful?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thank you for reviewing,
this file is standalone and I included it for reproducibility so that the model and ref values can be regenerated and verified independently. python3 BatchNormModelGenerator.py executes the file and we get the .onnx along with the ref values.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants