-
-
Notifications
You must be signed in to change notification settings - Fork 127
An attempt to combine dense, depthwise and groupwise conv through DenseConvDims
#146
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
Open
arhik
wants to merge
2
commits into
FluxML:master
Choose a base branch
from
arhik:common_convdims
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
export AbstractDims | ||
|
||
""" | ||
AbstractDims | ||
|
||
Type system-level information about convolution dimensions. Critical for things like | ||
`im2col!()` to generate efficient code, and helpful to reduce the number of kwargs | ||
getting passed around. | ||
|
||
We don't want to specialize on things like image size/channel count, so we generally | ||
store those as fields, just for convenience, and to allow for non-breaking changes when | ||
we decide we _do_ want to specialize on those values. We always want to specialize on | ||
things like stride, padding, dilation, and kernel flipping though. | ||
""" | ||
abstract type AbstractDims{N, S, P, D, F} end | ||
|
||
# Hack to get rid of type parameters | ||
function basetype(::Type{C}) where {C <: AbstractDims} | ||
if C <: ConvDims | ||
return ConvDims | ||
elseif C <: PoolDims | ||
return PoolDims | ||
else | ||
return nothing | ||
end | ||
end | ||
|
||
# Obvious getter definitions for the type system-level definitions | ||
spatial_dims(c::AbstractDims{N,S,P,D,F}) where {N, S, P, D, F} = N | ||
stride(c::AbstractDims{N,S,P,D,F}) where {N, S, P, D, F} = S | ||
padding(c::AbstractDims{N,S,P,D,F}) where {N, S, P, D, F} = P | ||
dilation(c::AbstractDims{N,S,P,D,F}) where {N, S, P, D, F} = D | ||
flipkernel(c::AbstractDims{N,S,P,D,F}) where {N, S, P, D, F} = F | ||
|
||
""" | ||
im2col_dims(c::AbstractDims) | ||
|
||
im2col calculates, for each output pixel, the "convolution" of N kernels where N is the | ||
number of output channels, by doing a matrix multiply. The dimensions of that matrix | ||
are given by this function. | ||
""" | ||
im2col_dims(c::AbstractDims) = (prod(output_size(c)), prod(kernel_size(c))*channels_in(c)) | ||
|
||
# Protect your skin, kids. Also do common validation of stride, padding, etc... | ||
function check_spdf(x_size::NTuple{N}, w_size::NTuple{N}, stride, padding, dilation) where {N} | ||
# Number of spatial dimensions in `x` and `w`. | ||
nd = N - 2 | ||
|
||
# Given a number, duplicate it out to have `nd` length. If it's already a collection, | ||
# just splat it out into a tuple so it's always a tuple. We'll lint length later. | ||
expand_size(p::Number) = ntuple(_ -> Int(p), nd) | ||
expand_size(p) = tuple(p...) | ||
|
||
# Convert stride, padding, dilation, etc.. to fully-specified tuples | ||
pstride = expand_size(stride) | ||
pdilation = expand_size(dilation) | ||
ppadding = expand_size(padding) | ||
|
||
if length(pstride) != nd | ||
throw(DimensionMismatch("Stride $(length(stride))d, should be $(nd)d!")) | ||
end | ||
if length(pdilation) != nd | ||
throw(DimensionMismatch("Dilation $(length(pdilation))d, should be $(nd)d!")) | ||
end | ||
|
||
# padding is kind of a special case; we allow it to be either 2-length or 4-length, | ||
# since we support asymmetrical padding | ||
if length(ppadding) != 2*nd | ||
if length(ppadding) == nd | ||
# Do this repeat dance so that we get lo/hi symmetrical padding | ||
ppadding = tuple(repeat(collect(ppadding), inner=2)...) | ||
else | ||
throw(DimensionMismatch("Padding $(length(ppadding))d, should be either $(nd)d or $(2*nd)d!")) | ||
end | ||
end | ||
|
||
# Assert that kernel size * dilation is <= padded input size | ||
for idx in 1:nd | ||
Is = x_size[idx] | ||
Pl = ppadding[(idx - 1)*2 + 1] | ||
Ph = ppadding[(idx - 1)*2 + 2] | ||
Ks = w_size[idx] | ||
Ds = pdilation[idx] | ||
if Is + Pl + Ph < (Ks - 1)*Ds + 1 | ||
throw(DimensionMismatch("Kernel * dilation (($Ks - 1) * $Ds + 1) cannot be larger than input + padding ($Is + $Pl + $Ph)!")) | ||
end | ||
end | ||
|
||
return pstride, ppadding, pdilation | ||
end | ||
|
||
""" | ||
output_size(c::AbstractDims) | ||
|
||
Calculate the output (spatial) dimensions of the convolution. Get channel count via | ||
`channels_out(c)`, and batch count is unknowable. | ||
""" | ||
function output_size(c::AbstractDims) | ||
I = input_size(c) | ||
K = kernel_size(c) | ||
S = stride(c) | ||
P = padding(c) | ||
D = dilation(c) | ||
|
||
return ntuple(spatial_dims(c)) do i | ||
return div(I[i] + P[(i-1)*2 + 1] + P[(i-1)*2 + 2] - (K[i] - 1) * D[i] - 1, S[i]) + 1 | ||
end | ||
end | ||
|
||
# Override show() for these beauties | ||
function Base.show(io::IO, cdims::C) where {C <: AbstractDims} | ||
I = (input_size(cdims)..., channels_in(cdims)) | ||
O = (output_size(cdims)..., channels_out(cdims)) | ||
K = kernel_size(cdims) | ||
S = stride(cdims) | ||
P = padding(cdims) | ||
D = dilation(cdims) | ||
F = flipkernel(cdims) | ||
print(io, "$(basetype(C)): $I * $K -> $O, stride: $S pad: $P, dil: $D, flip: $F") | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@staticfloat. This is only change. Only weights dimensions shrink as in here by groupcount value in third axis. if groupcount == 1 Nothing changes. When groupcount == 2 (lets say), then one group of weights operate only on the half of the input channels. and produce only one output channel. These weights groups have to be occupied across all the channels and we will have to use new group of weights (occupying all input channels in blocks) until their output matches output number of channels.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When groupcount == 7; we should make sure input channels can be divided exactly into 7 groups. Then we should check if output channels are multiple of groupcount(Since one group can only produce one output).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should then distribute these 7 groups to operate on different input channels in blocks of div(input_channels(), 7)