Skip to content

Improve accuracy and speed of Float16 conversions from Normed{UInt16} and Normed{UInt8} - #372

Open
kimikage wants to merge 1 commit into
JuliaMath:masterfrom
kimikage:n0f8_n0f16_to_float16
Open

kimikage wants to merge 1 commit into
JuliaMath:masterfrom
kimikage:n0f8_n0f16_to_float16

Conversation

@kimikage

@kimikage kimikage commented Oct 29, 2026 •

Copy link
Copy Markdown
Collaborator

This fixes the double-rounding issue in N2f14, N1f15, and N0f16. (Fixes #364)

julia> Float16(reinterpret(N2f14, 0x4007))
Float16(1.0)   # before
Float16(1.001) # after

julia> Float16(reinterpret(N1f15, 0x800f))
Float16(1.0)   # before
Float16(1.001) # after

julia> Float16(reinterpret(N0f16, 0xffaf))
Float16(0.999)) # before
Float16(0.9985) # after

julia> Float16(reinterpret(N0f16, 0xffef))
Float16(1.0)     # before
Float16(0.9995)) # after

This also make the tests more robust regarding issue #246.

This improves performance for Normed(UInt16, f) where f in 8:15. Performance improvements for other types are minor.

@codecov

codecov Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.78%. Comparing base (c4a4edd) to head (714a854).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #372      +/-   ##
==========================================
+ Coverage   96.73%   96.78%   +0.05%     
==========================================
  Files           7        7              
  Lines         827      840      +13     
==========================================
+ Hits          800      813      +13     
  Misses         27       27              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@kimikage
kimikage force-pushed the n0f8_n0f16_to_float16 branch 2 times, most recently from 5071851 to bada879 Compare October 29, 2026 07:08
@kimikage

kimikage commented Oct 29, 2026 •

Copy link
Copy Markdown
Collaborator Author

Benchmark

using FixedPointNumbers, BenchmarkTools

f16s = zeros(Float16, 1000, 1000);
for f in 2:8
    N = Normed{UInt8, f}
    ns = rand(N, 1000, 1000);
    println(N)
    @btime $f16s .= Float16.($ns)
end
for f in 2:16
    N = Normed{UInt16, f}
    ns = rand(N, 1000, 1000);
    println(N)
    @btime $f16s .= Float16.($ns)
end

Julia v1.13.3

julia> versioninfo()
Julia Version 1.13.3
Commit 67ac47b9cd (2026-10-25 15:28 UTC)
Build Info:
  Official https://julialang.org release
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: 8 × 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz
  WORD_SIZE: 64
  LLVM: libLLVM-20.1.8 (ORCJIT, tigerlake)
  GC: Built with stock GC
Threads: 1 default, 1 interactive, 1 GC (on 8 virtual cores)
from before (v0.9.3) after
N6f2 72.500 μs 63.800 μs
N5f3 77.000 μs 63.400 μs
N4f4 74.600 μs 62.200 μs
N3f5 70.500 μs 64.300 μs
N2f6 70.600 μs 61.900 μs
N1f7 76.900 μs 65.600 μs
N0f8 71.300 μs 69.100 μs
from before (v0.9.3) after
N14f2 88.200 μs 81.800 μs
N13f3 85.900 μs 84.500 μs
N12f4 88.300 μs 85.900 μs
N11f5 85.900 μs 81.700 μs
N10f6 85.900 μs 79.500 μs
N9f7 93.300 μs 79.200 μs
N8f8 501.200 μs 81.800 μs
N7f9 435.800 μs 79.200 μs
N6f10 501.200 μs 86.400 μs
N5f11 501.200 μs 76.400 μs
N4f12 477.300 μs 102.900 μs
N3f13 541.800 μs 87.000 μs
N2f14 541.800 μs 95.400 μs
N1f15 541.800 μs 101.100 μs
N0f16 116.600 μs 79.500 μs

@kimikage

Copy link
Copy Markdown
Collaborator Author

Julia v1.10.12

julia> versioninfo()
Julia Version 1.10.12
Commit d93beab124 (2026-08-15 10:29 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: 8 × 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-15.0.7 (ORCJIT, tigerlake)
Threads: 1 default, 0 interactive, 1 GC (on 8 virtual cores)
from before (v0.9.3) after
N6f2 136.800 μs 114.000 μs
N5f3 152.500 μs 144.700 μs
N4f4 153.000 μs 134.000 μs
N3f5 152.000 μs 124.700 μs
N2f6 155.900 μs 134.000 μs
N1f7 157.100 μs 137.400 μs
N0f8 138.800 μs 137.400 μs
from before (v0.9.3) after
N14f2 155.700 μs 133.700 μs
N13f3 182.700 μs 170.000 μs
N12f4 174.100 μs 178.000 μs
N11f5 178.300 μs 142.200 μs
N10f6 182.800 μs 140.500 μs
N9f7 178.300 μs 135.200 μs
N8f8 455.800 μs 146.900 μs
N7f9 436.600 μs 149.300 μs
N6f10 436.400 μs 138.500 μs
N5f11 487.900 μs 135.300 μs
N4f12 462.500 μs 118.700 μs
N3f13 436.800 μs 113.000 μs
N2f14 435.800 μs 122.200 μs
N1f15 524.600 μs 103.900 μs
N0f16 228.400 μs 228.000 μs

@kimikage
kimikage force-pushed the n0f8_n0f16_to_float16 branch from bada879 to 714a854 Compare October 29, 2026 08:00
@kimikage kimikage changed the title [WIP] Improve accuracy of Float16 conversions from Normed{UInt8} and Normed{UInt16} [WIP] Improve accuracy and speed of Float16 conversions from Normed{UInt16} and Normed{UInt8} Oct 29, 2026
@kimikage
kimikage marked this pull request as ready for review October 29, 2026 09:02
@kimikage kimikage changed the title [WIP] Improve accuracy and speed of Float16 conversions from Normed{UInt16} and Normed{UInt8} Improve accuracy and speed of Float16 conversions from Normed{UInt16} and Normed{UInt8} Oct 29, 2026
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.

Specialization of N0f16 -> Float16 conversion

1 participant