diff --git a/doc/primitives/convolution.md b/doc/primitives/convolution.md index 069c781cc03..4b0c62c00dd 100644 --- a/doc/primitives/convolution.md +++ b/doc/primitives/convolution.md @@ -100,6 +100,16 @@ Here: - \f$OW = \left\lfloor{\frac{IW - DKW + PW_L + PW_R}{SW}} \right\rfloor + 1,\f$ where \f$DKW = 1 + (KW - 1) \cdot (DW + 1)\f$. +@note In oneDNN, convolution without dilation is defined by setting the dilation +parameters to `0`. This differs from PyTorch and TensorFlow, where a non-dilated +case corresponds to a dilation value of `1`. + +@note For convolution with dilation, PyTorch and TensorFlow use alternative +formulas to calculate dimensions: \f$DKH_alt = 1 + (KH - 1) \cdot DH_alt\f$, and +\f$DKW_alt = 1 + (KW - 1) \cdot DW_alt\f$. To apply the formulas with oneDNN, +the dilation parameters need to be adjusted as follows: +\f$DH_onednn = DH_alt - 1\f$, and \f$DW_onednn = DW_alt - 1\f$. + #### Deconvolution (Transposed Convolution) Deconvolutions (also called fractionally strided convolutions or transposed diff --git a/examples/primitives/pooling.cpp b/examples/primitives/pooling.cpp index 92a2c877801..1d52fcf5eae 100644 --- a/examples/primitives/pooling.cpp +++ b/examples/primitives/pooling.cpp @@ -1,5 +1,5 @@ /******************************************************************************* -* Copyright 2020-2022 Intel Corporation +* Copyright 2020-2025 Intel Corporation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -66,6 +66,14 @@ void pooling_example(dnnl::engine::kind engine_kind) { DH = 1, // height-wise dilation DW = 1; // width-wise dilation + // oneDNN uses the following formula to calculate dimensions: + // dst = (src - ((weights - 1) * (dilation_onednn + 1) + 1))/ stride + 1 + // PyTorch and TensorFlow use an alternative formula: + // dst = (src - ((weights - 1) * dilation_alt + 1))/ stride + 1 + // To apply this alternative formula with oneDNN, the dilation parameter + // needs to be adjusted: dilation_onednn = dilation_alt - 1. + // The following formulas calculate the output dimensions using the + // formula in oneDNN. const memory::dim OH = (IH - ((KH - 1) * DH + KH) + PH_L + PH_R) / SH + 1; const memory::dim OW = (IW - ((KW - 1) * DW + KW) + PW_L + PW_R) / SW + 1;