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

Complete restructure #73

Merged
merged 24 commits into from
Dec 28, 2023
Merged

Complete restructure #73

merged 24 commits into from
Dec 28, 2023

Conversation

milankl
Copy link
Owner

@milankl milankl commented Dec 21, 2023

@avleenk2312 @tomkimpson This PR will completely restructure this package because we had badly defined things too specific and so it does this now as general.jl which defines all functions for ::AbstractStochasticFloat and types.jl which defines only what's necessary for a specific type, e.g. BFloat16sr is just

primitive type BFloat16sr <: AbstractStochasticFloat 16 end
Base.float(::Type{BFloat16sr}) = BFloat16       # corresponding deterministic float
stochastic_float(::Type{BFloat16}) = BFloat16sr # and stochastic float
BFloat16sr(x::BFloat16) = stochastic_float(x)   # direct conversion
Base.uinttype(::Type{BFloat16sr}) = UInt16      # corresponding uint
Base.widen(::Type{BFloat16sr}) = Float32        # higher precision format for exact arithmetic

"""Stochastically round x::Float32 to BFloat16 with distance-proportional probabilities."""
function stochastic_round(T::Type{BFloat16},x::Float32)
    ui = reinterpret(UInt32,x)
    ui += rand(Xor128[],UInt16)			        # add stochastic perturbation in [0,u)
    return reinterpret(BFloat16,(ui >> 16) % UInt16)	# round to zero and convert to BFloat16
end

As you see this frees a lot of lines of code that were previously used to redefine for every type which could be just collected into a single more general formulation. Draft for now as I still need to adapt all tests but it also DONE.

fixes #72 #70 #63

@madnanabid

@milankl milankl marked this pull request as ready for review December 22, 2023 14:21
@milankl milankl linked an issue Dec 22, 2023 that may be closed by this pull request
@milankl
Copy link
Owner Author

milankl commented Dec 22, 2023

This will be v0.8 because I've removed BFloat16_stochastic_round etc in favour for a more general stochastic_round(T::Type{<:AbstractFloat},x)

@milankl milankl merged commit dd229f8 into main Dec 28, 2023
1 check passed
@milankl milankl deleted the mk/restructure branch January 16, 2024 19:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant