Use GPUToolbox's Float64-free math overrides on devices without fp64 - #640
Merged
Merged
Conversation
Base computes some single-precision math in double precision, e.g., `div` on Float32 (on Julia 1.12 and 1.13), `sind` and `cosd`, or inversion of ComplexF32. On devices that don't support Float64, stack GPUToolbox.Overlays.float64_overrides underneath SPIRVIntrinsics' method table to keep these computations in single precision. This replaces the `div(::Float32, ::Float32)` quirk, which truncated the rounded quotient and thus returned incorrect results (e.g., `div(1f0, 0.1f0) == 10`), for all devices.
Contributor
|
Your PR requires formatting changes to meet the project's style guidelines. Click here to view the suggested changes.diff --git a/src/compiler/compilation.jl b/src/compiler/compilation.jl
index f147240..7e36726 100644
--- a/src/compiler/compilation.jl
+++ b/src/compiler/compilation.jl
@@ -37,10 +37,12 @@ function GPUCompiler.method_table_view(job::oneAPICompilerJob)
parent = SPIRVIntrinsics.method_table
else
# keep single-precision math that Base computes in Float64 out of double precision
- parent = GPUCompiler.StackedMethodTable(job.world, SPIRVIntrinsics.method_table,
- GPUToolbox.Overlays.float64_overrides)
+ parent = GPUCompiler.StackedMethodTable(
+ job.world, SPIRVIntrinsics.method_table,
+ GPUToolbox.Overlays.float64_overrides
+ )
end
- GPUCompiler.StackedMethodTable(job.world, method_table, parent)
+ return GPUCompiler.StackedMethodTable(job.world, method_table, parent)
end
# filter out OpenCL built-ins |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #640 +/- ##
==========================================
+ Coverage 81.53% 81.66% +0.13%
==========================================
Files 50 50
Lines 3607 3611 +4
==========================================
+ Hits 2941 2949 +8
+ Misses 666 662 -4 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Several Base methods compute single-precision results in double precision, so they fail to compile for devices without Float64 support (e.g. Intel Arc):
div,fldandcldof Float32 values on Julia 1.12 and 1.13,sindand friends, inversion and division ofComplexF32,sincospi/cispi, and comparisons between Float32/Float16 values and 32-bit integers. On such devices, this stacksGPUToolbox.Overlays.float64_overrides(JuliaGPU/GPUToolbox.jl#21, GPUToolbox 3.1) underneath SPIRVIntrinsics' method table, which replaces them with Float64-free implementations. Devices that support Float64 keep using Base's more accurate implementations.This also removes the
div(::Float32, ::Float32) = trunc(x / y)quirk, which truncated the rounded quotient and thus returned incorrect results on all devices, e.g.div(1f0, 0.1f0) == 10instead of 9. Devices with Float64 now use Base's implementation, and devices without use the override above, which is exact whenever the quotient is.I couldn't run this on hardware; on macOS, I checked that kernels calling
div,fld,sind,inv//ofComplexF32andhypotcompile without any Float64 when targeting a device without Float64 support, and that they keep Base's implementations otherwise. The same stacking is used by OpenCL.jl, where the tests compile these functions for a device without Float64.