Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Work around eachslice limitation on Julia <v1.9 #323

Merged
merged 3 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions src/bijectors/product_bijector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ inverse(b::ProductBijector) = ProductBijector(map(inverse, b.bs))

function _product_bijector_check_dim(::Val{N}, ::Val{M}) where {N,M}
if N > M
throw(
DimensionMismatch(
"Number of bijectors needs to be smaller than or equal to the number of dimensions",
),
)
msg = """
Number of bijectors needs to be smaller than or equal to the number of dimensions
"""
throw(DimensionMismatch(msg))
end
end

Expand All @@ -23,7 +22,18 @@ function _product_bijector_slices(

# If N < M, then the bijectors expect an input vector of dimension `M - N`.
# To achieve this, we need to slice along the last `N` dimensions.
return eachslice(x; dims=ntuple(i -> i + (M - N), N))
slice_indices = ntuple(i -> i + (M - N), N)
if VERSION >= v"1.9"
return eachslice(x; dims=slice_indices)
else
# Earlier Julia versions can't eachslice over multiple dimensions, so reshape the
# slice dimensions into a single one.
other_dims = tuple((size(x, i) for i in 1:(M - N))...)
slice_dims = tuple((size(x, i) for i in (1 + M - N):M)...)
x_reshaped = reshape(x, other_dims..., prod(slice_dims))
slices = eachslice(x_reshaped; dims=M - N + 1)
return reshape(collect(slices), slice_dims)
end
end

# Specialization for case where we're just applying elementwise.
Expand Down
15 changes: 9 additions & 6 deletions test/ad/chainrules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,17 @@ end

if @isdefined Tapir
rng = Xoshiro(123456)
Tapir.TestUtils.test_rrule!!(
rng, Bijectors.find_alpha, x, y, z; is_primitive=true, perf_flag=:none
Tapir.TestUtils.test_rule(
rng, Bijectors.find_alpha, x, y, z;
is_primitive=true, perf_flag=:none, interp=Tapir.TapirInterpreter()
)
Tapir.TestUtils.test_rrule!!(
rng, Bijectors.find_alpha, x, y, 3; is_primitive=true, perf_flag=:none
Tapir.TestUtils.test_rule(
rng, Bijectors.find_alpha, x, y, 3;
is_primitive=true, perf_flag=:none, interp=Tapir.TapirInterpreter()
)
Tapir.TestUtils.test_rrule!!(
rng, Bijectors.find_alpha, x, y, UInt32(3); is_primitive=true, perf_flag=:none
Tapir.TestUtils.test_rule(
rng, Bijectors.find_alpha, x, y, UInt32(3);
is_primitive=true, perf_flag=:none, interp=Tapir.TapirInterpreter()
)
end

Expand Down
Loading