Skip to content

Commit

Permalink
Shorten printing of Tangent and thunks (#564)
Browse files Browse the repository at this point in the history
* don't print internal details of thunks

* don't print super-long primal types for Tangent

* add tests

* version

* fix Int32, comments

* fix tests on 1.5
  • Loading branch information
mcabbott authored Jul 14, 2022
1 parent 2b650e1 commit f2e3ac5
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "ChainRulesCore"
uuid = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
version = "1.15.1"
version = "1.15.2"

[deps]
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"
Expand Down
13 changes: 12 additions & 1 deletion src/tangent_types/tangent.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,18 @@ Base.hash(a::Tangent, h::UInt) = Base.hash(backing(canonicalize(a)), h)

function Base.show(io::IO, tangent::Tangent{P}) where {P}
print(io, "Tangent{")
show(io, P)
str = sprint(show, P, context = io)
i = findfirst('{', str)
if isnothing(i)
print(io, str)
else # for Tangent{T{A,B,C}}(stuff), print {A,B,C} in grey, and trim this part if longer than a line:
print(io, str[1:prevind(str, i)])
if length(str) < 80
printstyled(io, str[i:end], color=:light_black)
else
printstyled(io, str[i:prevind(str, 80)], "...", color=:light_black)
end
end
print(io, "}")
if isempty(backing(tangent))
print(io, "()") # so it doesn't show `NamedTuple()`
Expand Down
16 changes: 14 additions & 2 deletions src/tangent_types/thunks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,13 @@ end

function Base.show(io::IO, x::Thunk)
print(io, "Thunk(")
show(io, x.f)
str = sprint(show, x.f, context = io) # often this name is like "ChainRules.var"#1398#1403"{Matrix{Float64}, Matrix{Float64}}"
ind = findfirst("var\"#", str)
if isnothing(ind) || length(str) < 80
printstyled(io, str, color=:light_black)
else
printstyled(io, str[1:ind[5]], "...", color=:light_black)
end
print(io, ")")
end

Expand All @@ -223,7 +229,13 @@ unthunk(x::InplaceableThunk) = unthunk(x.val)

function Base.show(io::IO, x::InplaceableThunk)
print(io, "InplaceableThunk(")
show(io, x.add!)
str = sprint(show, x.add!, context = io)
ind = findfirst("var\"#", str) # look for auto-generated function names, often with huge types
if isnothing(ind)
printstyled(io, str, color=:light_black)
else
printstyled(io, str[1:ind[5]], "...", color=:light_black)
end
print(io, ", ")
show(io, x.val)
print(io, ")")
Expand Down
8 changes: 8 additions & 0 deletions test/tangent_types/tangent.jl
Original file line number Diff line number Diff line change
Expand Up @@ -380,4 +380,12 @@ end
c = Tangent{typeof(nt)}(; a=NoTangent(), b=0.1)
@test nt + c == (; a=1, b=2.1)
end

@testset "printing" begin
t5 = Tuple(rand(3))
nt3 = (x=t5, y=t5, z=nothing)
tang = ProjectTo(nt3)(nt3) # moderately complicated Tangent
@test contains(sprint(show, tang), "...}(x = Tangent") # gets shortened
@test contains(sprint(show, tang), sprint(show, tang.x)) # inner piece appears whole
end
end
12 changes: 12 additions & 0 deletions test/tangent_types/thunks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,16 @@
@test scal!(2, 2.0, v, 1) == scal!(2, @thunk(2.0), v, 1)
@test_throws MutateThunkException LAPACK.trsyl!('C', 'C', m, m, @thunk(m))
end

@testset "printing" begin
@test !contains(sprint(show, @thunk 1+1), "...") # short thunks not abbreviated
th = let x = rand(100)
@thunk x .+ x'
end
@test contains(sprint(show, th), "...") # but long ones are

@test contains(sprint(show, InplaceableThunk(mul!, th)), "mul!") # named functions left in InplaceableThunk
str = sprint(show, InplaceableThunk(z -> z .+ ones(100), th))
@test length(findall("...", str)) == 2 # now both halves shortened
end
end

2 comments on commit f2e3ac5

@mcabbott
Copy link
Member Author

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/64263

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.15.2 -m "<description of version>" f2e3ac50cc45468256c12aaee637915ca0b594ee
git push origin v1.15.2

Please sign in to comment.