Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong answers for Tropical{Float32} #19

Open
chriselrod opened this issue Dec 13, 2022 · 3 comments
Open

Wrong answers for Tropical{Float32} #19

chriselrod opened this issue Dec 13, 2022 · 3 comments

Comments

@chriselrod
Copy link
Collaborator

e.g.

julia> using TropicalNumbers, Octavian, TropicalGEMM, BenchmarkTools, Tullio

julia> v2!(r, d) = @tullio (max) r[j, i] = d[k, j] + d[i, k]
v2! (generic function with 1 method)

julia> d = rand(Float32, 4000, 4000); r = similar(d); td = Tropical.(d); tr = similar(td);

julia> typeof(tr)
Matrix{TropicalF32} (alias for Array{Tropical{Float32}, 2})

julia> typeof(td)
Matrix{TropicalF32} (alias for Array{Tropical{Float32}, 2})

julia> @time(Octavian.matmul!(tr, td', td'));
 11.552436 seconds (28.39 M allocations: 1.414 GiB, 7.06% gc time, 198.04% compilation time)

julia> @time(v2!(r,d));
  2.761347 seconds (439.87 k allocations: 27.319 MiB, 20.82% compilation time)

julia> r[1:10,1:10]
10×10 Matrix{Float32}:
 1.98815  1.98032  1.96756  1.97688  1.98138  1.99293  1.99121  1.99046  1.98951  1.99046
 1.98347  1.97355  1.98345  1.96617  1.98862  1.97611  1.98876  1.98316  1.98376  1.98263
 1.98005  1.98607  1.94482  1.99388  1.97896  1.98763  1.97235  1.95483  1.98607  1.97469
 1.97811  1.96782  1.96914  1.99468  1.9638   1.96488  1.99281  1.96719  1.99046  1.99007
 1.97995  1.99206  1.96033  1.99925  1.97888  1.99531  1.98424  1.97277  1.96929  1.99149
 1.98175  1.97349  1.98187  1.96846  1.97438  1.99013  1.98184  1.96848  1.97482  1.99134
 1.97559  1.99141  1.97511  1.98494  1.98186  1.97386  1.95691  1.95286  1.97816  1.98359
 1.96895  1.95868  1.96969  1.98323  1.98321  1.96809  1.96434  1.9723   1.97961  1.9598
 1.97215  1.98366  1.97773  1.98742  1.98507  1.95414  1.96615  1.98585  1.97801  1.97805
 1.98135  1.98764  1.98053  1.96926  1.97798  1.988    1.94989  1.97951  1.98827  1.99674

julia> tr[1:10,1:10]
10×10 Matrix{TropicalF32}:
 1.98814691.95458511.98138091.95968991.98950671.95050241.96699621.93183421.95988131.99388581.96013331.94265151.97896031.97234961.9721921.94187591.99367791.98928671.98162581.94386641.92675451.95174861.97590951.98424391.9692921.99472741.9677511.98547051.95490261.97862031.94665151.97511231.98186451.95691251.97815561.98606591.95999981.97991221.97201981.97014051.9721521.97772861.96719291.95840641.97800831.97739081.9583331.98297021.97573351.95704831.98525641.9765431.99437451.9657731.98231511.98879561.98412291.97844141.96145531.98014281.97676751.97536371.97929221.97165371.98090051.98721621.97940671.95653741.98025821.9635571.98244761.96387931.97121521.98196891.98593021.98826841.97884891.98573581.9794561.96087811.97121751.97730141.97136571.9852411.97949151.95112221.9590551.98356031.95176981.97244861.95613971.97146371.97765361.97380661.98543191.96935741.96785321.98948171.97197481.9628868

while it works for Float64:

julia> d = rand(4000, 4000); r = similar(d); td = Tropical.(d); tr = similar(td);

julia> @time(v2!(r,d));
  2.539927 seconds (261.82 k allocations: 16.076 MiB, 16.36% compilation time)

julia> @time(Octavian.matmul!(tr, td', td'));
  9.220967 seconds (25.62 M allocations: 1.240 GiB, 3.42% gc time, 192.78% compilation time)

julia> tr == Tropical.(r)
true
@chriselrod
Copy link
Collaborator Author

Note this is on the following custom branches:
JuliaLinearAlgebra/Octavian.jl#157
JuliaArrays/ArrayInterface.jl#369
as it threw errors before.

@GiggleLiu
Copy link
Member

GiggleLiu commented Dec 14, 2022

Hey, @chriselrod . Thanks for the issue. The adjoint of Tropical numbers are not well defined.

julia> (Matrix(td') * Matrix(td'))[1:10, 1:10]
ERROR: MethodError: no method matching conj(::TropicalF32)
Closest candidates are:
  conj(::Union{LinearAlgebra.Hermitian{T, S}, LinearAlgebra.Symmetric{T, S}} where {T, S}) at ~/.julia/juliaup/julia-1.8.3+0.x64.linux.gnu/share/julia/stdlib/v1.8/LinearAlgebra/src/symmetric.jl:368
  conj(::LinearAlgebra.UniformScaling) at ~/.julia/juliaup/julia-1.8.3+0.x64.linux.gnu/share/julia/stdlib/v1.8/LinearAlgebra/src/uniformscaling.jl:123
  conj(::LinearAlgebra.Adjoint) at ~/.julia/juliaup/julia-1.8.3+0.x64.linux.gnu/share/julia/stdlib/v1.8/LinearAlgebra/src/adjtrans.jl:345
  ...
Stacktrace:
  [1] adjoint(x::TropicalF32)
    @ Base ./number.jl:213

So I am confused why matmul! can produce a result. If you use the following statement instead,

julia> @time(Octavian.matmul!(tr, transpose(td), transpose(td)));

The result is correct.

My package versions

(jl_QKsjQL) pkg> st
Status `/tmp/jl_QKsjQL/Project.toml`
  [6fd5a793] Octavian v0.3.18
  [a4ad3063] TropicalGEMM v0.1.8 `~/.julia/dev/TropicalGEMM`   # master branch is the same as the registered  version
  [bc48ee85] Tullio v0.3.5

@chriselrod
Copy link
Collaborator Author

If you're curious, the motivation was this slack discussion: https://julialang.slack.com/archives/C67TK21LJ/p1670593631969889

I can confirm that tranpose works. I was using adjoints because ' is faster to type.

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

No branches or pull requests

2 participants