Skip to content

Commit

Permalink
Special-case ReshapeTransform for singleton inputs (#699)
Browse files Browse the repository at this point in the history
* Special-case ReshapeTransform for singleton inputs

* Use fill(x[], ()) instead
  • Loading branch information
penelopeysm authored Oct 25, 2024
1 parent bd9f465 commit 18af48a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "DynamicPPL"
uuid = "366bfd00-2699-11ea-058f-f148b4cae6d8"
version = "0.30.1"
version = "0.30.2"

[deps]
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
Expand Down
11 changes: 9 additions & 2 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,15 @@ function (f::ReshapeTransform)(x)
if size(x) != f.input_size
throw(DimensionMismatch("Expected input of size $(f.input_size), got $(size(x))"))
end
# The call to `tovec` is only needed in case `x` is a scalar.
return reshape(tovec(x), f.output_size)
if f.output_size == ()
# Specially handle the case where x is a singleton array, see
# https://github.com/JuliaDiff/ReverseDiff.jl/issues/265 and
# https://github.com/TuringLang/DynamicPPL.jl/issues/698
return fill(x[], ())
else
# The call to `tovec` is only needed in case `x` is a scalar.
return reshape(tovec(x), f.output_size)
end
end

function (inv_f::Bijectors.Inverse{<:ReshapeTransform})(x)
Expand Down

2 comments on commit 18af48a

@penelopeysm
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/118055

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.30.2 -m "<description of version>" 18af48aee1e6cdac33ebb02166ecdf9fca694c8a
git push origin v0.30.2

Please sign in to comment.