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

Add support for converting between Stochastic FP numbers, and convenience methods for array conversions #56

Merged
merged 4 commits into from
Jan 18, 2023

Conversation

jprhyne
Copy link

@jprhyne jprhyne commented Apr 12, 2022

  • Motivation
    I would like to convert between Stochastic Floating Points just as we can for Deterministic types, and have convenience functions to convert entire arrays at a time
  • Goal
    Add the above functionality without breaking any existing features, and test the implementation
  • Implementation
    Added extra functions that convert between the stochastic types with deterministic types as a middle step
  • Testing
    Generated large amounts of random floating point numbers, ensured that these numbers were exactly representable in BFloat16, Float16, Float32, and Float64 via "daisy chaining" these conversions, and then testing that conversions were done properly

@milankl milankl self-assigned this Aug 27, 2022
Copy link
Owner

@milankl milankl left a comment

Choose a reason for hiding this comment

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

Sorry, I didn't see this PR before. Thanks for the contribution 😄 Yes, I appreciate the conversions between stochastic rounding formats!

Float16sr(x::Float32sr) = Float16sr(Float64(x))

# Conversion of arrays as long as they are some kind of floating point number
function Float16sr(list::T where T<:AbstractArray{<:Union{BFloat16,AbstractFloat}})
Copy link
Owner

Choose a reason for hiding this comment

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

BFloat16 is already <:AbstractFloat?

julia> BFloat16<:AbstractFloat
true

@@ -1,3 +1,4 @@
import BFloat16s.BFloat16
Copy link
Owner

Choose a reason for hiding this comment

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

instead of adding this to individual files, we can add import BFloat16s: BFloat16 to src/StochasticRounding.jl ?

@@ -53,6 +54,10 @@ Base.Float64(x::Float32sr) = Float64(Float32(x))
Float32sr(x::Integer) = Float32sr(Float32(x))
(::Type{T})(x::Float32sr) where {T<:Integer} = T(Float32(x))

# converting to and from BFloat16
Float32sr(x::BFloat16) = Float32sr(Float64(x))
Copy link
Owner

Choose a reason for hiding this comment

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

This should go via Float32 and not Float64? That will just padd trailing zeros and reinterpret


# Conversions from other stochastic floating points to Float16sr
Float16sr(x::BFloat16sr) = Float16sr(Float32(x))
Float16sr(x::Float32sr) = Float16sr(Float64(x))
Copy link
Owner

Choose a reason for hiding this comment

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

This should go via Float32 not Float64

@milankl milankl added the enhancement New feature or request label Nov 25, 2022
@milankl milankl added this to the v0.7 milestone Nov 25, 2022
@milankl milankl merged commit ee4c157 into milankl:main Jan 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants