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

Fix pretty printing and ReverseDiff constructor #67

Merged
merged 3 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ uuid = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
authors = [
"Vaibhav Dixit <[email protected]>, Guillaume Dalle and contributors",
]
version = "1.5.1"
version = "1.5.2"

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Expand Down
85 changes: 29 additions & 56 deletions src/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ end
mode(::AutoChainRules) = ForwardOrReverseMode() # specialized in the extension

function Base.show(io::IO, backend::AutoChainRules)
print(io, "AutoChainRules(ruleconfig=$(repr(backend.ruleconfig, context=io)))")
print(io, AutoChainRules, "(ruleconfig=", repr(backend.ruleconfig; context = io), ")")
end

"""
Expand Down Expand Up @@ -63,11 +63,9 @@ end
mode(::AutoEnzyme) = ForwardOrReverseMode() # specialized in the extension

function Base.show(io::IO, backend::AutoEnzyme)
if isnothing(backend.mode)
print(io, "AutoEnzyme()")
else
print(io, "AutoEnzyme(mode=$(repr(backend.mode, context=io)))")
end
print(io, AutoEnzyme, "(")
!isnothing(backend.mode) && print(io, "mode=", repr(backend.mode; context = io))
print(io, ")")
end

"""
Expand Down Expand Up @@ -111,21 +109,14 @@ end
mode(::AutoFiniteDiff) = ForwardMode()

function Base.show(io::IO, backend::AutoFiniteDiff)
s = "AutoFiniteDiff("
if backend.fdtype != Val(:forward)
s *= "fdtype=$(repr(backend.fdtype, context=io)), "
end
if backend.fdjtype != backend.fdtype
s *= "fdjtype=$(repr(backend.fdjtype, context=io)), "
end
if backend.fdhtype != Val(:hcentral)
s *= "fdhtype=$(repr(backend.fdhtype, context=io)), "
end
if endswith(s, ", ")
s = s[1:(end - 2)]
end
s *= ")"
print(io, s)
print(io, AutoFiniteDiff, "(")
backend.fdtype != Val(:forward) &&
print(io, "fdtype=", repr(backend.fdtype; context = io), ", ")
backend.fdjtype != backend.fdtype &&
print(io, "fdjtype=", repr(backend.fdjtype; context = io), ", ")
backend.fdhtype != Val(:hcentral) &&
print(io, "fdhtype=", repr(backend.fdhtype; context = io))
Comment on lines +114 to +118
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These don't show the Val?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes they do, there's just no Val in the test case I displayed above. Take a look here:

julia> string(AutoFiniteDiff(fdjtype=Val(:whatever)))
"AutoFiniteDiff(fdjtype=Val{:whatever}(), )"

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ChrisRackauckas I think this one is a strict improvement over the current state, what do you say?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

print(io, ")")
end

"""
Expand All @@ -150,7 +141,7 @@ end
mode(::AutoFiniteDifferences) = ForwardMode()

function Base.show(io::IO, backend::AutoFiniteDifferences)
print(io, "AutoFiniteDifferences(fdm=$(repr(backend.fdm, context=io)))")
print(io, AutoFiniteDifferences, "(fdm=", repr(backend.fdm; context = io), ")")
end

"""
Expand Down Expand Up @@ -183,18 +174,11 @@ end
mode(::AutoForwardDiff) = ForwardMode()

function Base.show(io::IO, backend::AutoForwardDiff{chunksize}) where {chunksize}
s = "AutoForwardDiff("
if chunksize !== nothing
s *= "chunksize=$chunksize, "
end
if backend.tag !== nothing
s *= "tag=$(repr(backend.tag, context=io)), "
end
if endswith(s, ", ")
s = s[1:(end - 2)]
end
s *= ")"
print(io, s)
print(io, AutoForwardDiff, "(")
chunksize !== nothing && print(io, "chunksize=", repr(chunksize; context = io),
(backend.tag !== nothing ? ", " : ""))
backend.tag !== nothing && print(io, "tag=", repr(backend.tag; context = io))
print(io, ")")
end

"""
Expand Down Expand Up @@ -227,18 +211,11 @@ end
mode(::AutoPolyesterForwardDiff) = ForwardMode()

function Base.show(io::IO, backend::AutoPolyesterForwardDiff{chunksize}) where {chunksize}
s = "AutoPolyesterForwardDiff("
if chunksize !== nothing
s *= "chunksize=$chunksize, "
end
if backend.tag !== nothing
s *= "tag=$(repr(backend.tag, context=io)), "
end
if endswith(s, ", ")
s = s[1:(end - 2)]
end
s *= ")"
print(io, s)
print(io, AutoPolyesterForwardDiff, "(")
chunksize !== nothing && print(io, "chunksize=", repr(chunksize; context = io),
(backend.tag !== nothing ? ", " : ""))
backend.tag !== nothing && print(io, "tag=", repr(backend.tag; context = io))
print(io, ")")
end

"""
Expand Down Expand Up @@ -277,11 +254,9 @@ end
mode(::AutoReverseDiff) = ReverseMode()

function Base.show(io::IO, ::AutoReverseDiff{compile}) where {compile}
if !compile
print(io, "AutoReverseDiff()")
else
print(io, "AutoReverseDiff(compile=true)")
end
print(io, AutoReverseDiff, "(")
compile && print(io, "compile=true")
print(io, ")")
end

"""
Expand Down Expand Up @@ -321,11 +296,9 @@ end
mode(::AutoTapir) = ReverseMode()

function Base.show(io::IO, backend::AutoTapir)
if backend.safe_mode
print(io, "AutoTapir()")
else
print(io, "AutoTapir(safe_mode=false)")
end
print(io, AutoTapir, "(")
!(backend.safe_mode) && print(io, "safe_mode=false")
print(io, ")")
end

"""
Expand Down
2 changes: 2 additions & 0 deletions src/legacy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

@deprecate AutoSparseZygote() AutoSparse(AutoZygote())

@deprecate AutoReverseDiff(compile) AutoReverseDiff(; compile)

function mtk_to_symbolics(obj_sparse::Bool, cons_sparse::Bool)
if obj_sparse || cons_sparse
return AutoSparse(AutoSymbolics())
Expand Down
10 changes: 5 additions & 5 deletions src/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,15 @@ function AutoSparse(
end

function Base.show(io::IO, backend::AutoSparse)
s = "AutoSparse(dense_ad=$(repr(backend.dense_ad, context=io)), "
print(io, AutoSparse, "(dense_ad=", repr(backend.dense_ad, context = io))
if backend.sparsity_detector != NoSparsityDetector()
s *= "sparsity_detector=$(repr(backend.sparsity_detector, context=io)), "
print(io, ", sparsity_detector=", repr(backend.sparsity_detector, context = io))
end
if backend.coloring_algorithm != NoColoringAlgorithm()
s *= "coloring_algorithm=$(repr(backend.coloring_algorithm, context=io))), "
print(
io, ", coloring_algorithm=", repr(backend.coloring_algorithm, context = io))
end
s = s[1:(end - 2)] * ")"
print(io, s)
print(io, ")")
end

"""
Expand Down
5 changes: 5 additions & 0 deletions test/legacy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,8 @@ end
@test ad isa AbstractADType
@test dense_ad(ad) isa AutoZygote
end

@testset "AutoReverseDiff without kwarg" begin
ad = @test_deprecated AutoReverseDiff(true)
@test ad.compile
end
38 changes: 38 additions & 0 deletions test/misc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ end
@testset "Printing" begin
for ad in every_ad_with_options()
@test startswith(string(ad), "Auto")
@test contains(string(ad), "(")
@test endswith(string(ad), ")")
end

Expand All @@ -19,3 +20,40 @@ end
@test contains(string(sparse_backend1), string(AutoForwardDiff()))
@test length(string(sparse_backend1)) < length(string(sparse_backend2))
end

import ADTypes

struct FakeSparsityDetector <: ADTypes.AbstractSparsityDetector end
struct FakeColoringAlgorithm <: ADTypes.AbstractColoringAlgorithm end

for backend in [
# dense
ADTypes.AutoChainRules(; ruleconfig = :rc),
ADTypes.AutoDiffractor(),
ADTypes.AutoEnzyme(),
ADTypes.AutoEnzyme(mode = :forward),
ADTypes.AutoFastDifferentiation(),
ADTypes.AutoFiniteDiff(),
ADTypes.AutoFiniteDiff(fdtype = :fd, fdjtype = :fdj, fdhtype = :fdh),
ADTypes.AutoFiniteDifferences(; fdm = :fdm),
ADTypes.AutoForwardDiff(),
ADTypes.AutoForwardDiff(chunksize = 3, tag = :tag),
ADTypes.AutoPolyesterForwardDiff(),
ADTypes.AutoPolyesterForwardDiff(chunksize = 3, tag = :tag),
ADTypes.AutoReverseDiff(),
ADTypes.AutoReverseDiff(compile = true),
ADTypes.AutoSymbolics(),
ADTypes.AutoTapir(),
ADTypes.AutoTapir(safe_mode = false),
ADTypes.AutoTracker(),
ADTypes.AutoZygote(),
# sparse
ADTypes.AutoSparse(ADTypes.AutoForwardDiff()),
ADTypes.AutoSparse(
ADTypes.AutoForwardDiff();
sparsity_detector = FakeSparsityDetector(),
coloring_algorithm = FakeColoringAlgorithm()
)
]
println(backend)
end
Loading