Skip to content

Conversation

jmitrevs
Copy link
Contributor

@jmitrevs jmitrevs commented Aug 4, 2025

Description

This is an further work on PR #1292: Added a transformation for the QONNX operator BipolarQuant to hls4ml operator BinaryQuantizer.

  • Added a ONNX BipolarQuant Operator.
  • Added three transformations (optimizations) that transform the qonnx operator to an hls4ml operator.

Adding those changes showed issues in hls4ml binary handling, that this PR continues to fix.

Also included is treating IntQuant as an alias for Quant.

Type of change

For a new feature or function, please create an issue first to discuss it
with us before submitting a pull request.

Note: Please delete options that are not relevant.

  • Bug fix (non-breaking change that fixes an issue)
  • Documentation update
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • A new research paper code implementation
  • Other (Specify)

Tests

This PR includes the BipolarQuant tests added to test_qonnx.py from the original PR (PR #1292).

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.

jvreca and others added 22 commits April 28, 2025 16:16
…is not applicable for BinaryQuantizer since it does not have a scaling factor
…zations, since it was already removed."

This reverts commit 89e2136.
@jmitrevs jmitrevs mentioned this pull request Aug 4, 2025
12 tasks
@jmitrevs jmitrevs added the please test Trigger testing by creating local PR branch label Sep 4, 2025
@jmitrevs jmitrevs marked this pull request as ready for review September 4, 2025 10:20
@jmitrevs
Copy link
Contributor Author

jmitrevs commented Sep 4, 2025

Nominally this is now ready and passes the tests when I ran manually, so I am setting ready for review. One thing I want to confirm, though, is that the non-unit scale tests actually test the proper handling of the scale, i.e. that the scales actually make a difference.

@jmitrevs
Copy link
Contributor Author

jmitrevs commented Sep 4, 2025

Note, some of the fixes are general bnn fixes, not just BipolarQuant fixes.

@jmitrevs
Copy link
Contributor Author

jmitrevs commented Sep 4, 2025

Nominally this is now ready and passes the tests when I ran manually, so I am setting ready for review. One thing I want to confirm, though, is that the non-unit scale tests actually test the proper handling of the scale, i.e. that the scales actually make a difference.

I did confirm at least that the outputs are different for the three different models when changing the scale, so although far from exhaustive, it does seem like the scale is at least handled for these models.

@@ -693,6 +693,11 @@ def replace_node(self, old_node, new_node):
repl = {old_name: new_name for old_name, new_name in zip(old_node.outputs, new_node.outputs)}
repl.update({old_name: new_name for old_name, new_name in zip(old_node.inputs, new_node.inputs)})

for old_output in old_node.outputs:
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a reason for moving this block around?



class BipolarQuantConstantParameters(OptimizerPass):
"""Remove Constant from the BipolarQaunt node parameters (but not input[0])"""
Copy link
Contributor

Choose a reason for hiding this comment

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

minor typo (Qaunt)

bias = np.array(0) # zeropt not defined for bipolar quants

# caclucate the new value -- actually not needed because bias == 0
# new_val = const_node.get_attr('value') / scale + bias
Copy link
Contributor

Choose a reason for hiding this comment

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

remove commented code.

@@ -0,0 +1,250 @@
"""
Copy link
Contributor

Choose a reason for hiding this comment

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

The stuff in here are quite significant overlap with the normal quant_opt.py. I am wondering if it would be better to merge the two and handle the bipolar stuff as as special cases in the same optimizers. But that would result in a bunch of if statements, so I'm not sure which option would be cleaner and easier to maintain.

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.

3 participants