Skip to content
This repository was archived by the owner on Mar 12, 2021. It is now read-only.

Upsampling GPU Kernel for Flux #293

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions src/dnn/CUDNN.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ end
include("libcudnn.jl")
include("helpers.jl")
include("nnlib.jl")
include("upsample.jl")

version() = VersionNumber(cudnnGetProperty(CUDAapi.MAJOR_VERSION),
cudnnGetProperty(CUDAapi.MINOR_VERSION),
Expand Down
59 changes: 59 additions & 0 deletions src/dnn/upsample.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
function upsample_kernel(state, y, x, height, width, channels, batch, stride, scale)
i = @linearidx y state

y_idx = i
y_h = (i - 1) % (height * stride[1]) + 1
i = (i - 1) ÷ (height * stride[1])
y_w = i % (width * stride[2]) + 1
i = i ÷ (width * stride[2])
y_c = i % channels + 1
i = i ÷ channels
y_b = i % batch + 1

x_h = (y_h - 1) ÷ stride[1] + 1
x_w = (y_w - 1) ÷ stride[2] + 1
x_c = y_c
x_idx = (y_b - 1) * width * height * channels + (x_c - 1) * width * height + (x_w - 1) * height + x_h

@inbounds y[y_idx] = scale * x[x_idx]

return nothing
end

function upsample(x::CuArray, stride, scale = 1)
(height, width, channels, batch) = size(x)
y = similar(x, (height * stride[1], width * stride[2], channels, batch))
gpu_call(upsample_kernel, y, (y, x, height, width, channels, batch, stride, scale))
return y
end

function ∇upsample_kernel(state, y, x, height, width, channels, batch, stride, scale)
i = @linearidx y state

y_idx = i
y_h = (i - 1) % (height * stride[1]) + 1
i = (i - 1) ÷ (height * stride[1])
y_w = i % (width * stride[2]) + 1
i = i ÷ (width * stride[2])
y_c = i % channels + 1
i = i ÷ channels
y_b = i % batch + 1

x_h = (y_h - 1) ÷ stride[1] + 1
x_w = (y_w - 1) ÷ stride[2] + 1
x_c = y_c
x_idx = (y_b - 1) * width * height * channels + (x_c - 1) * width * height + (x_w - 1) * height + x_h

@inbounds x[x_idx] += y[y_idx] / scale

return nothing
end

function ∇upsample(dy::CuArray, stride, scale = 1)
(height, width, channels, batch) = size(dy)
@assert height % stride[1] == 0
@assert width % stride[2] == 0
dx = similar(dy, (height ÷ stride[1], width ÷ stride[2], channels, batch))
gpu_call(∇upsample_kernel, dy, (dy, dx, height, width, channels, batch, stride, scale))
return dx
end