Skip to content

Commit

Permalink
Fix pretty printing and ReverseDiff constructor (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
gdalle authored Jun 27, 2024
1 parent 16f421d commit 59ceff3
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 62 deletions.
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))
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

2 comments on commit 59ceff3

@gdalle
Copy link
Collaborator Author

@gdalle gdalle commented on 59ceff3 Jun 27, 2024

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/109892

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.5.2 -m "<description of version>" 59ceff38b010a8793ee2be4164c9941b73f94057
git push origin v1.5.2

Please sign in to comment.