Skip to content

Add support for ConstantPad1d and ConstantPad2d layers in PyTorch con… #1322

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 8, 2025

Conversation

NALozano1
Copy link
Contributor

Description

This change adds support for nn.ConstantPad1d and nn.ConstantPad2d with zero padding by mapping them to the existing ZeroPadding1D and ZeroPadding2D layers in hls4ml. It allows PyTorch models using constant zero padding to be converted without error. Only zero padding values are currently supported.

Type of change

  • New feature (non-breaking change which adds functionality)

Tests

I verified the new padding layer support by implementing and converting two minimal PyTorch models using nn.ConstantPad1d and nn.ConstantPad2d. These tests check whether hls4ml correctly parses, configures, and converts both 1D and 2D constant padding operations into valid HLS models.

To reproduce:

  1. Define Pad1DModel with nn.ConstantPad1d((2, 3), 0) and Pad2DModel with nn.ConstantPad2d((1, 2, 3, 4), 0).
  2. Run config_from_pytorch_model() on each with appropriate input shapes.
  3. Convert each model using convert_from_pytorch_model().
  4. Print and inspect the resulting HLS layer types.
  5. Run .write() to generate the HLS project.

These tests were run using Python 3.10, PyTorch 2.1, and the latest hls4ml version on a Microsoft environment. Only model generation (.write()) was executed; no synthesis or simulation was performed.

Test Configuration:

Checklist

  • I have read the guidelines for contributing.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have made corresponding changes to the documentation.
  • My changes generate no new warnings.
  • I have installed and run pre-commit on the files I edited or added.
  • I have added tests that prove my fix is effective or that my feature works.

@calad0i
Copy link
Contributor

calad0i commented Jul 6, 2025

Thanks for the PR. It looks good to me, however, could you add a quick test under test/pytest? Either standalone or in test/pytest/test_pytorch_api.py would be fine.

@calad0i calad0i added the please test Trigger testing by creating local PR branch label Jul 7, 2025
Copy link
Contributor

@calad0i calad0i left a comment

Choose a reason for hiding this comment

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

Test failures appear to be unrelated and the intended feature is working. Merging.

@calad0i calad0i merged commit 009bcf5 into fastmachinelearning:main Jul 8, 2025
4 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
please test Trigger testing by creating local PR branch
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants