on master help sign in
Speed up im2col, vid2col.
f04bfebHuw Campbell
src/Grenade/Layers/Internal/Convolution.hs+4−4
@@ -127,7 +127,7 @@ vid2colUnsafe kernelRows kernelColumns striderows stridecols vidrows vidcols dat
127 numberOfPatches = length starts
127 numberOfPatches = length starts
128 channels = length dataVid
128 channels = length dataVid
129
129
130− dataCol <- U.newMatrix 0 numberOfPatches (channels * kernelSize)
130+ dataCol <- U.newUndefinedMatrix U.RowMajor numberOfPatches (channels * kernelSize)
131
131
132 offsetC <- newSTRef 0
132 offsetC <- newSTRef 0
133
133
@@ -140,7 +140,7 @@ vid2colUnsafe kernelRows kernelColumns striderows stridecols vidrows vidcols dat
140 forM_ [startRow .. startRow + kernelRows -1] $ \kr ->
140 forM_ [startRow .. startRow + kernelRows -1] $ \kr ->
141 forM_ [startCol .. startCol + kernelColumns -1] $ \kc -> do
141 forM_ [startCol .. startCol + kernelColumns -1] $ \kc -> do
142 inputColumn <- readSTRef inputColumnRef
142 inputColumn <- readSTRef inputColumnRef
143− unsafeModifyMatrix dataCol inputRow (inputColumn + offsetC') (+ U.atM' dataIm kr kc)
143+ U.unsafeWriteMatrix dataCol inputRow (inputColumn + offsetC') (U.atM' dataIm kr kc)
144 modifySTRef' inputColumnRef (+1)
144 modifySTRef' inputColumnRef (+1)
145 modifySTRef' inputRowRef (+1)
145 modifySTRef' inputRowRef (+1)
146
146
@@ -154,7 +154,7 @@ im2colUnsafe kernelRows kernelColumns striderows stridecols dataIm = U.runSTMatr
154 kernelSize = kernelRows * kernelColumns
154 kernelSize = kernelRows * kernelColumns
155 numberOfPatches = length starts
155 numberOfPatches = length starts
156
156
157− dataCol <- U.newMatrix 0 numberOfPatches kernelSize
157+ dataCol <- U.newUndefinedMatrix U.RowMajor numberOfPatches kernelSize
158
158
159 inputRowRef <- newSTRef 0
159 inputRowRef <- newSTRef 0
160 forM_ starts $ \(startRow, startCol) -> do
160 forM_ starts $ \(startRow, startCol) -> do
@@ -163,7 +163,7 @@ im2colUnsafe kernelRows kernelColumns striderows stridecols dataIm = U.runSTMatr
163 forM_ [startRow .. startRow + kernelRows -1] $ \kr ->
163 forM_ [startRow .. startRow + kernelRows -1] $ \kr ->
164 forM_ [startCol .. startCol + kernelColumns -1] $ \kc -> do
164 forM_ [startCol .. startCol + kernelColumns -1] $ \kc -> do
165 inputColumn <- readSTRef inputColumnRef
165 inputColumn <- readSTRef inputColumnRef
166− unsafeModifyMatrix dataCol inputRow inputColumn (+ U.atM' dataIm kr kc)
166+ U.unsafeWriteMatrix dataCol inputRow inputColumn (U.atM' dataIm kr kc)
167 modifySTRef' inputColumnRef (+1)
167 modifySTRef' inputColumnRef (+1)
168 modifySTRef' inputRowRef (+1)
168 modifySTRef' inputRowRef (+1)
169
169