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

Fix padding causing device to host copies #593

merged 2 commits into from
Jun 19, 2024

Conversation

pxl-th
Copy link
Member

@pxl-th pxl-th commented Jun 19, 2024

Current version of pad_reflect causes input array to be copied to host during backpropagation because of this rule.

Instead of usign StepRange we should use UnitRange for selectdim and reverse, this avoids expensive copying.

julia> using Revise, CUDA, Zygote

julia> x = CuArray(rand(Float32, 8000, 1));

julia> Δ = CuArray(ones(Float32, 1, 1));

julia> y, back = Zygote.pullback(x) do x
           sum(NNlib.pad_reflect(x, (0, 10)); dims=(1, 2))
       end;

julia> back(Δ);
D to H copy CuArray{Float32, 2, CUDA.DeviceMemory}: (8000, 1) -> Matrix{Float32}: (8000, 1)
D to H copy CuArray{Float32, 2, CUDA.DeviceMemory}: (11, 1) -> Matrix{Float32}: (11, 1)

Besides copying needlessly, on AMDGPU device to host copy causes synchronization if the memory is unpinned or there is no hardware support for this.

Before:

julia> @btime AMDGPU.@sync back($Δ);
  233.065 μs (330 allocations: 75.60 KiB)

Now:

julia> @btime AMDGPU.@sync back($Δ);
  80.891 μs (293 allocations: 11.63 KiB)

Same for pad_symmetric:

Before:

julia> @btime AMDGPU.@sync back($Δ);
  243.315 μs (327 allocations: 75.55 KiB)

Now:

julia> @btime AMDGPU.@sync back($Δ);
  75.781 μs (293 allocations: 11.62 KiB)

PR Checklist

  • Tests are added
  • Documentation, if applicable

@pxl-th pxl-th merged commit 85b17cf into master Jun 19, 2024
12 of 15 checks passed
@pxl-th pxl-th deleted the pxl-th/pad branch June 19, 2024 19:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants