Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pytorch - Where is “conv1d” implemented?

Tags:

pytorch

I wanted to see how the conv1d module is implemented https://pytorch.org/docs/stable/_modules/torch/nn/modules/conv.html#Conv1d. So I looked at functional.py but still couldn’t find the looping and cross-correlation computation.

Then I searched Github by keyword ‘conv1d’, checked conv.cpp https://github.com/pytorch/pytorch/blob/eb5d28ecefb9d78d4fff5fac099e70e5eb3fbe2e/torch/csrc/api/src/nn/modules/conv.cpp 1 but still couldn’t locate where the computation is happening.

My question is two-fold.

  1. Where is the source code that "conv1d” is implemented?

  2. In general, if I want to check how the modules are implemented, where is the best place to find? Any pointer to the documentation will be appreciated. Thank you.

like image 722
aerin Avatar asked Dec 26 '18 04:12

aerin


People also ask

What is Conv1d in PyTorch?

The PyTorch conv1d is defined as a one-dimensional convolution that is applied over an input signal collected from some input planes. In detail, we will discuss Conv1d using PyTorch in python.

Is Conv1d same as linear layer?

Figure-3: PyTorch code to showcase that Conv1d and Linear layer operations are equivalent.

What is groups in Conv1d?

groups controls the connections between inputs and outputs. in_channels and out_channels must both be divisible by groups .


2 Answers

  1. It depends on the backend (GPU, CPU, distributed etc) but in the most interesting case of GPU it's pulled from cuDNN which is released in binary format and thus you can't inspect its source code. It's a similar story for CPU MKLDNN. I am not aware of any place where PyTorch would "handroll" it's own convolution kernels, but I may be wrong. EDIT: indeed, I was wrong as pointed out in an answer below.
  2. It's difficult without knowing how PyTorch is structured. A lot of code is actually being autogenerated based on various markup files, as explained here. Figuring this out requires a lot of jumping around. For instance, the conv.cpp file you're linking uses torch::conv1d, which is defined here and uses at::convolution which in turn uses at::_convolution, which dispatches to multiple variants, for instance at::cudnn_convolution. at::cudnn_convolution is, I believe, created here via a markup file and just plugs in directly to cuDNN implementation (though I cannot pinpoint the exact point in code when that happens).
like image 97
Jatentaki Avatar answered Oct 17 '22 21:10

Jatentaki


Below is an answer that I got from pytorch discussion board:

I believe the “handroll”-ed convolution is defined here: https://github.com/pytorch/pytorch/blob/master/aten/src/THNN/generic/SpatialConvolutionMM.c 3

The NN module implementations are here: https://github.com/pytorch/pytorch/tree/master/aten/src The GPU version is in THCUNN and the CPU version in THNN

like image 26
aerin Avatar answered Oct 17 '22 22:10

aerin