Forward and backward implementation of max pool 2d

username: jfurmain
cooked: <p>Hi,</p>
<p>I’d like to extend max pooling 2d with a new idea. However, for this I need the extend the forward and backward pass of max pooling. Ideally, I would use the cudnn implementation to compute the forward and backward pass of max pooling, but as far as I can see, these are not exposed in the python API. I’ve been looking through the source code, but I’m unable to find the implementation of max_pooling2d. <em>In short: Is there a way to expose the forward and backward pass implementation to the python api?</em> And related: how is max_pool2d implemented, as far as I understand the functions are automatically generated but further than that I’m very confused.</p>
<p>Thanks.</p>

username: richard
cooked: <p><a href="https://github.com/pytorch/pytorch/blob/master/aten/src/ATen/nn.yaml#L171" rel="nofollow noopener">this</a> says it is implemented as SpatialDilatedMaxPooling. You can find the code for that <a href="https://github.com/pytorch/pytorch/blob/502aaf39cf4a878f9e4f849e5f409573aa598aa9/aten/src/THNN/generic/SpatialDilatedMaxPooling.c" rel="nofollow noopener">here</a> and <a href="https://github.com/pytorch/pytorch/blob/0257f5d19f0585f9a82bc06e0c4987e2136332c9/aten/src/THCUNN/generic/SpatialDilatedMaxPooling.cu" rel="nofollow noopener">here</a></p>
<p>I’m not sure what your use case is but if it’s simple you can just perform operations to the output of max_pool2d, not worry about implementing a <code>backward</code>, and autograd will take care of it.</p>

username: jfurmain
cooked: <p>Thanks, that’s very helpful.</p>
<p>My case is basically the following: I want to obtain max pooling indices from a tensor A, and then get the values at the locations of those indices in a tensor B of the same dimensionality. So far, I haven’t figured how to do this and was thinking I could do this by using the fwd/bwd implementations of max_pooling2d, but maybe I’m making things to complicated?</p>

username: jfurmain
cooked: <p>I was able to do this using torch.gather. No need to dive into pytorch internals.</p>

username: Shubhi_Shukla
cooked: <p>Hey, I also want to extend PyTorch’s maxpool implementation. The links mentioned here seems to be obsolete now. Can you help me with the current location of the implementation in the library and also what is the process of compiling and running the updated code.<br>
Thanks in advance <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=10" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

username: ptrblck
cooked: <p>The CPU implementation should be located <a href="https://github.com/pytorch/pytorch/blob/385773cb7784ecef0d37df241d7182f390138627/aten/src/ATen/native/cpu/MaxPoolKernel.cpp">here</a> and the CUDA one <a href="https://github.com/pytorch/pytorch/blob/385773cb7784ecef0d37df241d7182f390138627/aten/src/ATen/native/cuda/DilatedMaxPool2d.cu">here</a>.</p>
<p>Check the <a href="https://github.com/pytorch/pytorch/blob/master/CONTRIBUTING.md">Contributing docs</a> to see how to incrementally rebuild (and test) your changes. <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=10" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

username: Shubhi_Shukla
cooked: <p>Thanks, that was helpful <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=10" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>