Optimize im2col - avoid redundant zeroing of packing buffer, improve lookup table construction #165
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.
This adds a couple of further optimizations for im2col:
Avoid redundant zeroing.
If the packing buffer needed for im2col operations needs to be resized, avoid zeroing it, since the packing code will overwrite all elements.
As part of this change, change the callers of the packing functions in
gemm.rs
to always pass an output slice of exactly the correct size, avoiding the need for each packing function to zero unused space at the end, in order to fulfill the contract that the entire slice is initialized after the packing code returns. Previously gemm.rs would always pass an output slice of the same size for every block to be packed. However the last row/column block may be smaller, with unused space at the end.Improve efficiency of lookup table construction in
VirtualIm2Col::new
. See commit for details. But in short, avoid divisions where possible.On the first item, In the Piper models, convolutions get called with progressively larger input images as you get into the deeper layers. I noticed a stall of ~12 ms or so about two-thirds of the way through evaluation, while a packing buffer was resized and zeroed in several threads. Avoiding the zeroing saves maybe 3-4ms. It would be even better if the buffer was just allocated with the largest size it will need at the start though.