Skip to content

Choose each convolution algorithm when first needed - #3162

Merged
davisking merged 3 commits into
davisking:masterfrom
reunanen:choose-each-convolution-algorithm-when-first-needed
Oct 2, 2026
Merged

davisking merged 3 commits into
davisking:masterfrom
reunanen:choose-each-convolution-algorithm-when-first-needed

Conversation

@reunanen

Copy link
Copy Markdown
Contributor

All three algorithms of a convolution, forward, backward data and backward filters, were benchmarked when it was set up. So a network that only makes predictions benchmarked backward algorithms it never uses, and a transposed convolution, whose forward pass is a backward data pass, benchmarked a forward algorithm it never uses. Now each is taken from the cache or benchmarked the first time the call that needs it is made.

Builds on #3161; only the last commit is new here.

Each thread chooses the cuDNN convolution algorithms by benchmarking them
on the first input it gets for a given stride, padding and filter height
and width. It then reuses them for later inputs with that stride, padding
and filter height and width, whatever the shape of the input and the
number of filters. But the workspace an algorithm needs can grow a lot
with those: for 3x3 filters with a stride and padding of 1,
CUDNN_CONVOLUTION_FWD_ALGO_WINOGRAD_NONFUSED needed 10.7 MiB for 16 filters
on a 3x63x63 input, and 12.4 GiB for 36 filters on a 52x1023x1023 input
(cuDNN 9.0 on an RTX 4080). So algorithms chosen for a small input can
make a later, larger one run out of GPU memory.

set_dnn_choose_algorithms_per_input_shape(true), off by default, makes each
combination of input shape and number of filters get algorithms of its own.
The cache of the chosen algorithms now keeps the 1000 most recently used
entries of each thread, so it can't grow without limit when there are many
input shapes.
All three algorithms of a convolution, forward, backward data and backward
filters, were benchmarked when it was set up. So a network that only makes
predictions benchmarked backward algorithms it never uses, and a transposed
convolution, whose forward pass is a backward data pass, benchmarked a
forward algorithm it never uses. Now each is taken from the cache or
benchmarked the first time the call that needs it is made.
@reunanen

Copy link
Copy Markdown
Contributor Author

The flaky Python test addressed separately in #3163.

…lution-algorithm-when-first-needed

# Conflicts:
#	dlib/cuda/cudnn_dlibapi.cpp
#	dlib/dnn/core_abstract.h
@davisking

Copy link
Copy Markdown
Owner

Awesome

@davisking
davisking merged commit 46fa4a2 into davisking:master Oct 2, 2026
10 of 11 checks passed
@reunanen
reunanen deleted the choose-each-convolution-algorithm-when-first-needed branch October 3, 2026 06:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants