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

Fix padding causing device to host copies #593

Merged
merged 2 commits into from
Jun 19, 2024
Merged
Changes from 1 commit
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
19 changes: 8 additions & 11 deletions src/padding.jl
Original file line number Diff line number Diff line change
Expand Up @@ -255,27 +255,24 @@ julia> pad_reflect(r, (1,2,1,2))
4 1 4 7 4 1
```
"""
function pad_reflect(x::AbstractArray, pad::NTuple{M,Int};
function pad_reflect(x::AbstractArray, pad::NTuple{M,Int};
dims=1:M÷2) where M
length(dims) == M ÷ 2 ||
throw(ArgumentError("The number of dims should be equal to the number of padding dimensions"))
for (i, d) in enumerate(dims)
x = pad_reflect(x, (pad[2i-1], pad[2i]); dims = d)
end
end
return x
end

function pad_reflect(x::AbstractArray{F,N}, pad::NTuple{2,Int};
dims::Int = 1) where {F,N}
function pad_reflect(
x::AbstractArray{F,N}, pad::NTuple{2,Int}; dims::Int = 1,
) where {F,N}
lpad, rpad = pad

n = size(x, dims)
xl = selectdim(x, dims, lpad+1:-1:2)
xr = selectdim(x, dims, n-1:-1:n-rpad)
# Alternative selection, not sure which is faster...
# xl = reverse(selectdim(x, dims, 2:lpad+1), dims)
# xr = reverse(selectdim(x, dims, n-rpad:n-1), dims)
return cat(xl, x, xr, dims = dims)
xl = lpad == 0 ? similar(x, 0) : reverse(selectdim(x, dims, 2:lpad+1); dims)
xr = rpad == 0 ? similar(x, 0) : reverse(selectdim(x, dims, n-rpad:n-1); dims)
return cat(xl, x, xr; dims)
end

"""
Expand Down
Loading