Skip to content

Commit 0a0badf

Browse files
authored
fix Nan number bug in matrix multiplication (#17)
* done * add tests * add tests
1 parent fb777fb commit 0a0badf

5 files changed

Lines changed: 55 additions & 2 deletions

File tree

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "TropicalGEMM"
22
uuid = "a4ad3063-64a7-4bad-8738-34ed09bc0236"
33
authors = ["GiggleLiu <cacate0129@gmail.com> and contributors"]
4-
version = "0.1.6"
4+
version = "0.1.7"
55

66
[deps]
77
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,9 @@ The benchmark and plotting scripts could be found in the benchmarks folder.
3131

3232
![Float64](benchmarks/benchmark-float64.png)
3333
![Float32](benchmarks/benchmark-float32.png)
34+
35+
36+
## Warnings
37+
38+
It is expected to have an ambiguity error when one uses both `TropicalGEMM` and `CUDA`.
39+
If you see these errors, please include `example/cudapatch.jl` in your project.

examples/cudapatch.jl

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# you will need this to use Tropical numbers in CUDA.
2+
using CUDA
3+
CUDA.allowscalar(false)
4+
using TropicalGEMM: XTranspose, NativeTypes, Tropical, TropicalTypes, CountingTropical
5+
using LinearAlgebra
6+
using Test
7+
8+
const CTranspose{T} = Transpose{T, <:StridedCuVecOrMat{T}}
9+
for TT in [:(Tropical{<:NativeTypes}), :TropicalTypes]
10+
for RT in [TT, :Real]
11+
for (TA, CTA) in [(:CuMatrix, :CuMatrix), (:CTranspose, :(Transpose{<:Any, <:StridedCuVecOrMat}))]
12+
for (TB, CTB) in [(:CuMatrix, :CuMatrix), (:CTranspose, :(Transpose{<:Any, <:StridedCuVecOrMat}))]
13+
@eval function LinearAlgebra.mul!(o::CuMatrix{T}, a::$TA{T}, b::$TB{T}, α::$RT, β::$RT) where {T<:$TT}
14+
CUDA.CUBLAS.gemm_dispatch!(o, a, b, α, β)
15+
end
16+
end
17+
end
18+
end
19+
end
20+
21+
@testset "cuda patch" begin
22+
for T in [Tropical{Float64}, CountingTropical{Float64,Float64}]
23+
a = T.(CUDA.randn(4, 4))
24+
b = T.(CUDA.randn(4))
25+
for A in [transpose(a), a, transpose(b)]
26+
for B in [transpose(a), a, b]
27+
if !(size(A) == (1,4) && size(B) == (4,))
28+
res0 = Array(A) * Array(B)
29+
res1 = A * B
30+
res2 = mul!(CUDA.zeros(T, size(res0)...), A, B, true, false)
31+
@test Array(res1) res0
32+
@test Array(res2) res0
33+
end
34+
end
35+
end
36+
end
37+
end

src/gemm.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,11 @@ for TA in [:AbstractMatrix, :XTranspose]
149149
@eval function LinearAlgebra.mul!(o::AbstractMatrix{T}, a::$TA{T}, b::$TB{T}, α::Number, β::Number) where {T<:Tropical{<:NativeTypes}}
150150
α = _convert_to_tropical(T, α)
151151
β = _convert_to_tropical(T, β)
152+
if iszero(β)
153+
@avx for j=1:size(o, 2), i=1:size(o, 1)
154+
o[i,j] = zero(T)
155+
end
156+
end
152157
Octavian.matmul!(o, a, b, α, β)
153158
end
154159
end

test/gemm.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using TropicalGEMM, Octavian, LoopVectorization
2-
using TropicalNumbers
2+
using TropicalNumbers, LinearAlgebra
33
using VectorizationBase: VecUnroll, Vec
44
using TropicalGEMM: naive_mul!
55
using Test
@@ -78,4 +78,9 @@ end
7878
@testset "fix julia-1.5" begin
7979
x=Tropical(Vec(1.0, 2.0))
8080
@test VecUnroll((x, x)) === Tropical(VecUnroll((Vec(1.0, 2.0), Vec(1.0, 2.0))))
81+
end
82+
83+
@testset "fix nan bug" begin
84+
res = LinearAlgebra.mul!(Tropical.(fill(NaN, 2, 2)), transpose(Tropical.(randn(2,2))), Tropical.(randn(2,2)), 1, 0)
85+
@test !any(isnan, res)
8186
end

0 commit comments

Comments
 (0)