Skip to content

Add native and half math - #644

Open
oscardssmith wants to merge 3 commits into
JuliaGPU:mainfrom
oscardssmith:os/native-math
Open

oscardssmith wants to merge 3 commits into
JuliaGPU:mainfrom
oscardssmith:os/native-math

Conversation

@oscardssmith

Copy link
Copy Markdown

Add the OpenCL native_* and half_* functions (cos, divide, exp, exp2, exp10, log,log2, log10, powr, recip, rsqrt, sin, sqrt, tan) for Float32 and Float16,cand route Base.FastMath's exp/log/div/inv/trig variants to them, like CUDA.jl's fast intrinsics. Without this, @fastmath falls back to the Base impls.

The Float16 native cos/sin/tan go through Float32: PoCL lacks the half versions, and on an Intel Arc B390 the detour is within 7% of the half natives (both 2.8-3.5x faster than the accurate Float16 builtins).

Co-Authored-By: Claude Opus 5.5 noreply@anthropic.com

oscardssmith and others added 3 commits December 2, 2026 20:58
Add the OpenCL native_* functions (cos, divide, exp, exp2, exp10, log,
log2, log10, powr, recip, rsqrt, sin, sqrt, tan) for Float32 and Float16,
and route Base.FastMath's exp/log/div/inv/trig variants to them, like
CUDA.jl's fast intrinsics. Without this, @fastmath falls back to the
correctly rounded builtins.

The Float16 native cos/sin/tan go through Float32: PoCL lacks the half
versions, and on an Intel Arc B390 the detour is within 7% of the half
natives (both 2.8-3.5x faster than the accurate Float16 builtins).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add the OpenCL half_* functions (Float32 with at most 8192 ulp error) for
completeness. They are not faster than native_*: on an Intel Arc B390,
half_exp and half_log2 compile to the native instructions, while half_sin,
half_tan and half_powr take the correctly rounded paths (as slow as the
accurate builtins). @fastmath keeps using native_*.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The native_* results differ from Base's fast variants, so overlay them with
plain `@overlay` rather than `@device_override`'s consistent overlay. They go in
their own `fastmath_overrides` table, which OpenCL leaves out of the stack on the
OpenCL C source path: spirv2clc fails on the native_* extended instructions
("UNIMPLEMENTED extended instruction"), which broke `@fastmath` kernels there.

Tests: keep log inputs away from 1, and cover @fastmath sincos/inv, Float16,
and the spirv2clc path.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@vchuravy

vchuravy commented Dec 3, 2026

Copy link
Copy Markdown
Member

Last time I tried this some of these are slower.

@codecov

codecov Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.06%. Comparing base (ff61f07) to head (33a0aa1).
⚠️ Report is 5 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #644      +/-   ##
==========================================
- Coverage   90.26%   88.06%   -2.20%     
==========================================
  Files          18       18              
  Lines        1684     1684              
==========================================
- Hits         1520     1483      -37     
- Misses        164      201      +37     

☔ 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.

@oscardssmith

Copy link
Copy Markdown
Author

At least on my igpu (intel xe graphics) the native ones are all faster.

This branch has not been deployed

No deployments
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