|
| 1 | + |
| 2 | +function unfold_kernel!(col::AbstractArray{T}, x, col_size, input_size, output_size, kernel_size, flipkernel, stride, pad_lo, dilation, max_idx) where {T} |
| 3 | + index = threadIdx().x + (blockIdx().x - 1) * blockDim().x |
| 4 | + |
| 5 | + @inbounds if index <= max_idx |
| 6 | + i, kw, kh, kd, c, b = CartesianIndices(col_size)[index].I # col indices |
| 7 | + w, h, d = CartesianIndices(output_size)[i].I # x indices |
| 8 | + |
| 9 | + # project |
| 10 | + w, h, d = @. ((w, h, d) - 1)*stride - pad_lo + 1 + ((kw, kh, kd) - 1)*dilation |
| 11 | + |
| 12 | + if !flipkernel |
| 13 | + kw, kh, kd = kernel_size .- (kw, kh, kd) .+ 1 |
| 14 | + end |
| 15 | + |
| 16 | + # check out of bounds |
| 17 | + if !all(checkindex.(Bool, UnitRange.(1, input_size), (w, h, d))) |
| 18 | + col[i, kw, kh, kd, c, b] = T(0) |
| 19 | + return nothing |
| 20 | + end |
| 21 | + |
| 22 | + xval::T = x[w, h, d, c, b] |
| 23 | + col[i, kw, kh, kd, c, b] = xval |
| 24 | + end |
| 25 | + |
| 26 | + return nothing |
| 27 | +end |
| 28 | + |
| 29 | +function fold_kernel!(x::AbstractArray{T}, col, col_size, input_size, output_size, kernel_size, flipkernel, stride, pad_lo, dilation, max_idx) where {T} |
| 30 | + index = threadIdx().x + (blockIdx().x - 1) * blockDim().x |
| 31 | + |
| 32 | + @inbounds if index <= max_idx |
| 33 | + i, kw, kh, kd, c, b = CartesianIndices(col_size)[index].I # col indices |
| 34 | + w, h, d = CartesianIndices(output_size)[i].I # x indices |
| 35 | + |
| 36 | + # project |
| 37 | + w, h, d = @. ((w, h, d) - 1)*stride - pad_lo + 1 + ((kw, kh, kd) - 1)*dilation |
| 38 | + |
| 39 | + # check out of bounds |
| 40 | + if !all(checkindex.(Bool, UnitRange.(1, input_size), (w, h, d))) |
| 41 | + return nothing |
| 42 | + end |
| 43 | + |
| 44 | + if !flipkernel |
| 45 | + kw, kh, kd = kernel_size .- (kw, kh, kd) .+ 1 |
| 46 | + end |
| 47 | + |
| 48 | + cval::T = col[i, kw, kh, kd, c, b] |
| 49 | + CUDA.@atomic x[w, h, d, c, b] += cval |
| 50 | + end |
| 51 | + |
| 52 | + return nothing |
| 53 | +end |
| 54 | + |
| 55 | +function NNlib.unfold!(col::AnyCuArray{cT,3}, x::AnyCuArray{xT,5}, cdims::NNlib.DenseConvDims) where {cT, xT} |
| 56 | + if NNlib.spatial_dims(cdims) != 3 |
| 57 | + throw(DimensionMismatch("unfold!() only accepts 3d convoluitional inputs")) |
| 58 | + end |
| 59 | + |
| 60 | + input_size = NNlib.input_size(cdims) |
| 61 | + C_in = NNlib.channels_in(cdims) |
| 62 | + kernel_size = NNlib.kernel_size(cdims) |
| 63 | + pad_w_lo, pad_w_hi, pad_h_lo, pad_h_hi, pad_d_lo, pad_d_hi = NNlib.padding(cdims) |
| 64 | + pad_lo = (pad_w_lo, pad_h_lo, pad_d_lo) |
| 65 | + dilation = NNlib.dilation(cdims) |
| 66 | + stride = NNlib.stride(cdims) |
| 67 | + output_size = NNlib.output_size(cdims) |
| 68 | + flipkernel = NNlib.flipkernel(cdims) |
| 69 | + |
| 70 | + col_reshaped = reshape(col, (prod(output_size), kernel_size..., C_in, :)) |
| 71 | + |
| 72 | + max_idx = prod(size(col)) |
| 73 | + args = col_reshaped, x, size(col_reshaped), input_size, output_size, kernel_size, flipkernel, stride, pad_lo, dilation, max_idx |
| 74 | + kernel = @cuda launch=false unfold_kernel!(args...) |
| 75 | + config = launch_configuration(kernel.fun; max_threads=256) |
| 76 | + threads = min(max_idx, config.threads) |
| 77 | + blocks = cld(max_idx, threads) |
| 78 | + kernel(args...; threads=threads, blocks=blocks) |
| 79 | + return col |
| 80 | +end |
| 81 | + |
| 82 | +function NNlib.fold!(x::AnyCuArray{xT,5}, col::AnyCuArray{cT,3}, cdims::NNlib.DenseConvDims) where {xT, cT} |
| 83 | + if NNlib.spatial_dims(cdims) != 3 |
| 84 | + throw(DimensionMismatch("fold!() only accepts 3d convoluitional inputs")) |
| 85 | + end |
| 86 | + |
| 87 | + # going to accumulate into x |
| 88 | + fill!(x, xT(0)) |
| 89 | + |
| 90 | + input_size = NNlib.input_size(cdims) |
| 91 | + C_in = NNlib.channels_in(cdims) |
| 92 | + kernel_size = NNlib.kernel_size(cdims) |
| 93 | + pad_w_lo, pad_w_hi, pad_h_lo, pad_h_hi, pad_d_lo, pad_d_hi = NNlib.padding(cdims) |
| 94 | + pad_lo = (pad_w_lo, pad_h_lo, pad_d_lo) |
| 95 | + dilation = NNlib.dilation(cdims) |
| 96 | + stride = NNlib.stride(cdims) |
| 97 | + output_size = NNlib.output_size(cdims) |
| 98 | + flipkernel = NNlib.flipkernel(cdims) |
| 99 | + |
| 100 | + col_reshaped = reshape(col, (prod(output_size), kernel_size..., C_in, :)) |
| 101 | + |
| 102 | + max_idx = prod(size(col)) |
| 103 | + args = x, col_reshaped, size(col_reshaped), input_size, output_size, kernel_size, flipkernel, stride, pad_lo, dilation, max_idx |
| 104 | + kernel = @cuda launch=false fold_kernel!(args...) |
| 105 | + config = launch_configuration(kernel.fun; max_threads=256) |
| 106 | + threads = min(max_idx, config.threads) |
| 107 | + blocks = cld(max_idx, threads) |
| 108 | + kernel(args...; threads=threads, blocks=blocks) |
| 109 | + return x |
| 110 | +end |
| 111 | + |
0 commit comments