Skip to content

Commit

Permalink
Remove some literals from shared data (#343)
Browse files Browse the repository at this point in the history
* Improve which literals get put in shared data

* Check that strings do not appear in shared data

* Remove redundant line space

* Tidy up testing

* Bump patch
  • Loading branch information
willtebbutt authored Nov 4, 2024
1 parent 9d67929 commit 112f117
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Mooncake"
uuid = "da2b9cff-9c12-43a0-ae48-6db2b0edb7d6"
authors = ["Will Tebbutt, Hong Ge, and contributors"]
version = "0.4.32"
version = "0.4.33"

[deps]
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
Expand Down
11 changes: 8 additions & 3 deletions src/interpreter/s2s_reverse_mode_ad.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,12 @@ IDInstPair[
=#
function shared_data_stmts(p::SharedDataPairs)::Vector{IDInstPair}
return map(enumerate(p.pairs)) do (n, p)
return (p[1], new_inst(Expr(:call, getfield, Argument(1), n)))
return (p[1], new_inst(Expr(:call, get_shared_data_field, Argument(1), n)))
end
end

@inline get_shared_data_field(shared_data, n) = getfield(shared_data, n)

#=
The block stack is the stack used to keep track of which basic blocks are visited on the
forwards pass, and therefore which blocks need to be visited on the reverse pass. There is
Expand Down Expand Up @@ -453,10 +455,13 @@ end
# a bits type, then it is returned. If it is not, then the CoDual is put into shared data,
# and the ID associated to it in the forwards- and reverse-passes returned.
function const_codual(stmt, info::ADInfo)
x = uninit_fcodual(get_const_primal_value(stmt))
return isbitstype(_typeof(x)) ? x : add_data!(info, x)
v = get_const_primal_value(stmt)
x = uninit_fcodual(v)
return safe_for_literal(v) ? x : add_data!(info, x)
end

safe_for_literal(v) = v isa String || v isa Type || isbitstype(_typeof(v))

inc_or_const(stmt, info::ADInfo) = is_active(stmt) ? __inc(stmt) : const_codual(stmt, info)

# Get the value associated to `x`. For `GlobalRef`s, verify that `x` is indeed a constant.
Expand Down
8 changes: 8 additions & 0 deletions test/interpreter/s2s_reverse_mode_ad.jl
Original file line number Diff line number Diff line change
Expand Up @@ -315,4 +315,12 @@ end
rule = Mooncake.build_rrule(f, 0.0)
@benchmark Mooncake.value_and_gradient!!($rule, $f, $(Ref(0.0))[])
end
@testset "literal Strings do not appear in shared data" begin
f() = "hello"
@test length(build_rrule(Tuple{typeof(f)}).fwds_oc.oc.captures) == 2
end
@testset "Literal Types do not appear in shared data" begin
f() = Float64
@test length(build_rrule(Tuple{typeof(f)}).fwds_oc.oc.captures) == 2
end
end

2 comments on commit 112f117

@willtebbutt
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 register()

@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/118649

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 v0.4.33 -m "<description of version>" 112f117c00fe22a28f05e1b800e280726501f65e
git push origin v0.4.33

Please sign in to comment.