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

Refactoring of FloatingPointValue constructors for ease of use and extension #110

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

mkorbel1
Copy link
Contributor

Description & Motivation

Making it easier to extend FloatingPointValue and reuse constructors, removing factorys, etc.

Related Issue(s)

N/A

Testing

Existing tests should cover.

Backwards-compatibility

Is this a breaking change that will not be backwards-compatible? If yes, how so?

No

Documentation

Does the change require any updates to documentation? If so, where? Are they included?

No

Copy link
Contributor

@desmonddak desmonddak left a comment

Choose a reason for hiding this comment

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

This structure looks good. I think I need to add more tests for the new classes,
but the major restructuring is to have each FP type with its own class. E.g., the 8-bit classes are now separated into two types.

sign: LogicValue.ofBigInt(sign ? BigInt.one : BigInt.zero, 1),
exponent: LogicValue.ofBigInt(BigInt.from(exponent), exponentWidth),
mantissa:
LogicValue.ofBigInt(BigInt.from(mantissa), mantissaWidth));

/// Construct a [FloatingPointValue] from a Logic word
factory FloatingPointValue.fromLogic(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Why is this called fromLogic instead of fromLogicValue? Also, the comment says "word"? Also, this shouldn't be a factory?

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm struggling with having both factory and non-factory constructors. For example, fromDouble() is a factory constructor as it does a lot of work before it can initialize fields. But that forces sub-classes to also have a factory method that first checks for some things and then calls the base factory method, possibly then checking for other things.


/// Numeric conversion of a [FloatingPoint8E4M3Value] from a host double
factory FloatingPoint8E4M3Value.fromDouble(double inDouble) {
if ((inDouble > maxValue) | (inDouble < minValue)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

can we move this consideration of max/min into the base fromDouble in FloatingPointLogic?

Copy link
Contributor

Choose a reason for hiding this comment

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

This is specific to E4M3. it's also wrong, so I have fixed it (it fails for 0.0 and negatives).

/// Factory (static) constructor of a [FloatingPointValue] from
/// sign, mantissa and exponent
factory FloatingPointValue(
/// A Map from the (exponentWidth, mantissaWidth) pair to the
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I feel like we should update this doc comment to explain how it's used and how people can leverage it to override default behavior

Copy link
Contributor

Choose a reason for hiding this comment

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

I am not sure of the utility of the map. We can work without it for now. I think if people are accessing the constructors from the base class, they won't get the special behavior of the sub-class constructors that I need to add. We need to figure out what we want here, as FPV(e=4,m=3) is not going to be the same type as FP8E4M3 since the latter does not represent infinity.

Copy link
Contributor

Choose a reason for hiding this comment

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

Unfortunately the test for E4M3 calls the base class constructor and does not validate the fromDouble routine. So we have bugs in the very basic tests for the subtypes I need to check.

@mkorbel1
Copy link
Contributor Author

Yes, this is looking a lot better!

@mkorbel1 mkorbel1 marked this pull request as ready for review October 17, 2024 18:58
@mkorbel1 mkorbel1 mentioned this pull request Oct 17, 2024
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