Skip to content
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

No longer make ExponentPrecisionType and XnorPrecisionType inherit from IntegerPrecisionType #845

Merged
merged 4 commits into from
Aug 17, 2023

Conversation

jmitrevs
Copy link
Contributor

@jmitrevs jmitrevs commented Aug 4, 2023

Description

When trying to propagate precisions, isinstance(var, (IntegerPrecisionType, FixedPrecisionType)) produces incorrect results if the variable is ExponentPrecisionType or XnorPrecisionType. The "is a" requirement for inheritance is violated. Therefore it's better to remove the inheritance, which this PR does. It also cleans up a bit of the remaining string parsing there was and switches from the old % string formatting to f-strings or .format().

Type of change

It is not really a "bug" but the previous inheritance structure caused issues with yet to be added precision propagation.

  • Bug fix (non-breaking change that fixes an issue)

Tests

This should not break anything. The qkeras tests in particular are the main tests for this.

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.

@jmitrevs jmitrevs added this to the v0.8.0 milestone Aug 4, 2023
@jmitrevs
Copy link
Contributor Author

jmitrevs commented Aug 4, 2023

About the pytest failure, Quartus Winograd is broken for Xnor inputs. #804 forces im2col instead of Winograd for this reason. I can hide this failure if desired.

"""
Convenience class to differentiate 'regular' integers from BNN Xnor ones
"""

def __init__(self):
super().__init__(width=1, signed=False)

def __str__(self):
typestring = 'uint<1>'
Copy link
Contributor Author

@jmitrevs jmitrevs Aug 5, 2023

Choose a reason for hiding this comment

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

I am truthfully not much of a fan of the typestring. What is it's function? Is it to have a string representation in the configs? There is not a 1:1 between a typestring and and the type. Part of me thinks this should be 'xnor' and not 'uint<1>', though this requires a few more changes downstream. (There would be a corresponding change for exponent precision types.)

Copy link
Contributor

Choose a reason for hiding this comment

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

Not 100% sure, but this may be the last leftover from the previous way of doing things. I think that it is only used in WeightVariable.update_precision() to set a new string format that is then not used anywhere (it was used only in print_array_to_cpp in the writer, but this was changed). If it doesn't hurt your eyes too much I would leave it as-is here, and have a follow up PR that removes it, size_cpp() and perhaps the whole dim_names concept as one of our usual "cleanup" PRs that we do prior to a release.

@@ -311,16 +314,20 @@ def __eq__(self, other):
return eq


class XnorPrecisionType(IntegerPrecisionType):
class XnorPrecisionType(PrecisionType):
"""
Convenience class to differentiate 'regular' integers from BNN Xnor ones
"""

def __init__(self):
super().__init__(width=1, signed=False)
Copy link
Contributor Author

@jmitrevs jmitrevs Aug 5, 2023

Choose a reason for hiding this comment

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

Logically xnor is signed, even if we represent it as unit<1>. How it gets implemented should be a backend issue, not a definition issue. Leaving it unsigned for now, though, since that's a bigger change.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think the XNOR idea was that it is a single bit implementation, the more general definition of BNN is signed (-1 and 1), but that's a different case.

@jmitrevs
Copy link
Contributor Author

I think after #804 is merged the pytest failure will go away. But I am curious what people think about the "review" comments I made.

@vloncar vloncar merged commit a9fee5d into main Aug 17, 2023
3 checks passed
@vloncar vloncar deleted the types_inheritance branch August 17, 2023 16:49
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