Skip to content

Commit

Permalink
Check if config is nothing before accessing fields (#281)
Browse files Browse the repository at this point in the history
* Check if config is nothing before accessing fields

* Explicitly return

Co-authored-by: Will Tebbutt <willtebbutt00@gmail.com>

* Instantiate default config on the fly

* Add a test for config=nothing

---------

Co-authored-by: Will Tebbutt <willtebbutt00@gmail.com>
  • Loading branch information
penelopeysm and willtebbutt authored Oct 4, 2024
1 parent 942eead commit 27b5e48
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 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.5"
version = "0.4.6"

[deps]
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
Expand Down
6 changes: 5 additions & 1 deletion ext/MooncakeLogDensityProblemsADExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,12 @@ function logdensity_and_gradient(∇l::MooncakeGradientLogDensity, x::Vector{Flo
end

# Interop with ADTypes.
function getconfig(x::ADTypes.AutoMooncake)
c = x.config
return isnothing(c) ? Mooncake.Config() : c
end
function ADgradient(x::ADTypes.AutoMooncake, ℓ)
debug_mode = x.config.debug_mode
debug_mode = getconfig(x).debug_mode
if debug_mode
msg = "Running Mooncake in debug mode. This mode is computationally expensive, " *
"should only be used when debugging a problem with AD, and turned off in " *
Expand Down
4 changes: 4 additions & 0 deletions test/integration_testing/logdensityproblemsad_interop.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ test_gradient(x) = -2 .* x
@test ADgradient(ADTypes.AutoMooncake(; config), l) isa typeof(∇l)
@test parent(∇l) === l

# Check that nothing for config works.
@test ADgradient(ADTypes.AutoMooncake(; config=nothing), l) isa typeof(∇l)
@test parent(∇l) === l

# Run in debug mode.
debug_config = Mooncake.Config(; debug_mode=true)
@test parent(ADgradient(ADTypes.AutoMooncake(; config=debug_config), l)) === l
Expand Down

2 comments on commit 27b5e48

@willtebbutt
Copy link
Member

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

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.6 -m "<description of version>" 27b5e486435413a10d45dbd74914b52efd9b1d3f
git push origin v0.4.6

Please sign in to comment.