Skip to content

Commit 05d9e66

Browse files
Preserve BFloat16 in ProjectTo
Add a BFloat16s extension defining ProjectTo(::BFloat16) = ProjectTo{BFloat16}(), like Float16/Float32/Float64, instead of falling back to ProjectTo{Real}. Closes #718. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 0da460d commit 05d9e66

3 files changed

Lines changed: 22 additions & 2 deletions

File tree

‎Project.toml‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
name = "ChainRulesCore"
22
uuid = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
3-
version = "1.26.2"
3+
version = "1.26.3"
44

55
[deps]
66
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"
77
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
88
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
99

1010
[compat]
11+
BFloat16s = "0.5, 0.6"
1112
BenchmarkTools = "1.5"
1213
Compat = "4.15"
1314
FiniteDifferences = "0.12.32"
@@ -16,9 +17,11 @@ StaticArrays = "1.9.8"
1617
julia = "1.10"
1718

1819
[extensions]
20+
ChainRulesCoreBFloat16sExt = "BFloat16s"
1921
ChainRulesCoreSparseArraysExt = "SparseArrays"
2022

2123
[extras]
24+
BFloat16s = "ab4f0b2a-ad5b-11e8-123f-65d77653426b"
2225
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
2326
FiniteDifferences = "26cc04aa-876d-5657-8c51-4c34ba976000"
2427
OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
@@ -27,7 +30,8 @@ StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
2730
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
2831

2932
[targets]
30-
test = ["Test", "BenchmarkTools", "FiniteDifferences", "OffsetArrays", "SparseArrays", "StaticArrays"]
33+
test = ["Test", "BFloat16s", "BenchmarkTools", "FiniteDifferences", "OffsetArrays", "SparseArrays", "StaticArrays"]
3134

3235
[weakdeps]
36+
BFloat16s = "ab4f0b2a-ad5b-11e8-123f-65d77653426b"
3337
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"

‎ext/ChainRulesCoreBFloat16sExt.jl‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module ChainRulesCoreBFloat16sExt
2+
3+
using ChainRulesCore
4+
using BFloat16s: BFloat16
5+
6+
ChainRulesCore.ProjectTo(::BFloat16) = ProjectTo{BFloat16}()
7+
8+
end

‎test/projection.jl‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using ChainRulesCore, Test
22
using LinearAlgebra, SparseArrays
33
using OffsetArrays, StaticArrays, BenchmarkTools
4+
using BFloat16s
45

56
# Like ForwardDiff.jl's Dual
67
struct Dual{T<:Real} <: Real
@@ -59,6 +60,13 @@ struct NoSuperType end
5960
@test ProjectTo(Dual(1.0, 2.0))(Tangent{Dual}(; value=1.0)) isa Tangent
6061
end
6162

63+
@testset "BFloat16" begin
64+
@test ProjectTo(BFloat16(1))(2.5f0) === BFloat16(2.5)
65+
@test ProjectTo(BFloat16(1))(2.5) === BFloat16(2.5)
66+
@test ProjectTo(BFloat16(1))(2) === BFloat16(2)
67+
@test ProjectTo(BFloat16[1, 2])([1.5f0, 2.5f0]) isa Vector{BFloat16}
68+
end
69+
6270
@testset "Base: arrays of numbers" begin
6371
pvec3 = ProjectTo([1, 2, 3])
6472
@test pvec3(1.0:3.0) === 1.0:3.0

0 commit comments

Comments
 (0)