@@ -45,9 +45,9 @@ for (front_name, backend) in (
45
45
# We only define 3d conv primitives, we reshape lower down to get 1d and 2d convolution
46
46
@eval begin
47
47
# im2col-accelerated function forwarding definition
48
- function $ (Symbol (" $(front_name) !" ))(
49
- out:: AbstractArray{T,5} , in1:: AbstractArray{T,5} ,
50
- in2:: AbstractArray{T,5} , cdims:: ConvDims ; kwargs... ) where {T <: $G }
48
+ @timeit_debug to function $ (Symbol (" $(front_name) !" ))(
49
+ out:: AbstractArray{T,5} , in1:: AbstractArray{T,5} ,
50
+ in2:: AbstractArray{T,5} , cdims:: ConvDims ; kwargs... ) where {T <: $G }
51
51
$ (Symbol (" $(front_name) _$(backend) !" ))(out, in1, in2, cdims; kwargs... )
52
52
end
53
53
end
@@ -62,9 +62,9 @@ for front_name in (:conv, :∇conv_data, :∇conv_filter,
62
62
for N in (3 , 4 )
63
63
@eval begin
64
64
function $ (Symbol (" $(front_name)$(backend) !" ))(
65
- y:: AbstractArray{yT,$N} , x:: AbstractArray{xT,$N} ,
66
- w:: AbstractArray{wT,$N} , cdims:: ConvDims ;
67
- kwargs... ) where {yT, xT, wT}
65
+ y:: AbstractArray{yT,$N} , x:: AbstractArray{xT,$N} ,
66
+ w:: AbstractArray{wT,$N} , cdims:: ConvDims ;
67
+ kwargs... ) where {yT, xT, wT}
68
68
$ (Symbol (" $(front_name)$(backend) !" ))(
69
69
insert_singleton_spatial_dimension (y, $ (5 - N)),
70
70
insert_singleton_spatial_dimension (x, $ (5 - N)),
88
88
for front_name in (:conv , :∇conv_data , :∇conv_filter ,
89
89
:depthwiseconv , :∇depthwiseconv_data , :∇depthwiseconv_filter )
90
90
@eval begin
91
- function $ (Symbol (" $(front_name) !" ))(out:: AbstractArray , in1:: AbstractArray ,
92
- in2:: AbstractArray , cdims:: ConvDims ; kwargs... )
93
- @debug " Slow fallback implementation invoked for $(front_name) ! You probably don't want this; check your datatypes."
94
- $ (Symbol (" $(front_name) _direct!" ))(out, in1, in2, cdims; kwargs... )
91
+ function $ (Symbol (" $(front_name) !" ))(
92
+ y:: AbstractArray{yT,N} , in1:: AbstractArray{T1,N} ,
93
+ in2:: AbstractArray{T2,N} , cdims:: ConvDims ;
94
+ kwargs... ) where {yT, T1, T2, N}
95
+ @debug string (" Slow fallback implementation invoked for $(front_name) ! " ,
96
+ " You probably don't want this; check your datatypes." )
97
+ $ (Symbol (" $(front_name) _direct!" ))(y, in1, in2, cdims; kwargs... )
95
98
end
96
99
end
97
100
end
98
101
99
- # Finally, let's generate auto-allocating versions of all our functions, for all backends:
102
+ # Finally, let's generate auto-allocating versions of all our functions, for all backends.
103
+ # We `@timeit` these methods separately, as we want to know how much time is spent in
104
+ # allocation. :P
100
105
for backend in (Symbol (), :_direct , :_im2col )
101
106
# First make auto-allocating versions of the conv()-like calls:
102
107
for name in (:conv , :depthwiseconv )
103
108
@eval begin
104
- function $ (Symbol (" $(name)$(backend) " ))(
105
- x:: AbstractArray{xT,N} , w:: AbstractArray{wT,N} ,
106
- cdims:: ConvDims ; kwargs... ) where {xT, wT, N}
107
- yT = promote_type (xT, wT)
108
- # Annoyingly, we must allocate with `zeros()` because if we were to use
109
- # the faster `similar()`, it may have NaNs within it, which will poison
110
- # the output because we support accumulation (even with `beta = 0` the
111
- # NaNs poison us as NaN * 0 == NaN). This is a bit of a shame, but it's
112
- # not really that bad as if you're truly interested in performance, you
113
- # should be allocating your own `y` and calling the non-allocating
114
- # variant of this method anyway.
115
- y = zeros (yT, output_size (cdims)... , channels_out (cdims), size (x, N))
109
+ @timeit_debug to function $ (Symbol (" $(name)$(backend) " ))(
110
+ x:: AbstractArray{xT,N} , w:: AbstractArray{wT,N} ,
111
+ cdims:: ConvDims ; kwargs... ) where {xT, wT, N}
112
+ y = similar (x, promote_type (xT, wT), output_size (cdims)... ,
113
+ channels_out (cdims), size (x,N))
116
114
return $ (Symbol (" $(name)$(backend) !" ))(y, x, w, cdims; kwargs... )
117
115
end
118
116
end
119
117
end
120
118
121
119
for name in (:∇conv_data , :∇depthwiseconv_data )
122
120
@eval begin
123
- function $ (Symbol (" $(name)$(backend) " ))(
124
- dy:: AbstractArray{yT,N} , w:: AbstractArray{wT,N} ,
125
- cdims:: cdT ; kwargs... ) where {yT, wT, N, cdT <: ConvDims }
126
- # Again, allocate with zeros
127
- dx = zeros (yT, input_size (cdims) ... , channels_in (cdims), size (dy, N))
121
+ @timeit_debug to function $ (Symbol (" $(name)$(backend) " ))(
122
+ dy:: AbstractArray{yT,N} , w:: AbstractArray{wT,N} ,
123
+ cdims:: ConvDims ; kwargs... ) where {yT, wT, N}
124
+ dx = similar (dy, input_size (cdims) ... , channels_in (cdims),
125
+ size (dy, N))
128
126
return $ (Symbol (" $(name)$(backend) !" ))(dx, dy, w, cdims; kwargs... )
129
127
end
130
128
end
@@ -133,23 +131,21 @@ for backend in (Symbol(), :_direct, :_im2col)
133
131
# We do the conv/depthwiseconv filter backprops separately, as the shape calculation
134
132
# for `w` is slightly different for depthwise than for normal dense convolution.
135
133
@eval begin
136
- function $ (Symbol (" ∇conv_filter$(backend) " ))(
137
- x:: AbstractArray{xT,N} , dy:: AbstractArray{yT,N} ,
138
- cdims:: cdT ; kwargs... ) where {xT, yT, N, cdT <: ConvDims }
139
- # Again, allocate with zeros
140
- dw = zeros (yT, kernel_size (cdims)... , channels_in (cdims),
141
- channels_out (cdims))
134
+ @timeit_debug to function $ (Symbol (" ∇conv_filter$(backend) " ))(
135
+ x:: AbstractArray{xT,N} , dy:: AbstractArray{yT,N} ,
136
+ cdims:: ConvDims ; kwargs... ) where {xT, yT, N}
137
+ dw = similar (dy, kernel_size (cdims)... , channels_in (cdims),
138
+ channels_out (cdims))
142
139
return $ (Symbol (" ∇conv_filter$(backend) !" ))(dw, x, dy, cdims; kwargs... )
143
140
end
144
141
end
145
142
146
143
@eval begin
147
- function $ (Symbol (" ∇depthwiseconv_filter$(backend) " ))(
148
- x:: AbstractArray{xT,N} , dy:: AbstractArray{yT,N} ,
149
- cdims:: cdT ; kwargs... ) where {xT, yT, N, cdT <: ConvDims }
150
- # Again, allocate with zeros
151
- dw = zeros (yT, kernel_size (cdims)... , channel_multiplier (cdims),
152
- channels_in (cdims))
144
+ @timeit_debug to function $ (Symbol (" ∇depthwiseconv_filter$(backend) " ))(
145
+ x:: AbstractArray{xT,N} , dy:: AbstractArray{yT,N} ,
146
+ cdims:: ConvDims ; kwargs... ) where {xT, yT, N}
147
+ dw = similar (dy, kernel_size (cdims)... , channel_multiplier (cdims),
148
+ channels_in (cdims))
153
149
return $ (Symbol (" ∇depthwiseconv_filter$(backend) !" ))(dw, x, dy, cdims;
154
150
kwargs... )
155
151
end
0 commit comments