Questions on C++ extension for customized convolution layers

YimingGan: <p>Hi, I’m working on creating a C++ extension for a customized depthwise convolution layer on CPU. Besides all the parameters used in at::Tensor _convolution() in pytorch/pytorch/blob/master/aten/src/ATen/native/Convolution.cpp, I have another input which is a mask in the same shape of input channel (const Tensor&amp; mask). I met couple of problems when implementing this new convolution extension:</p>
<ol>
<li>Although I have used namespace at and native as used in pytorch/pytorch/blob/master/aten/src/ATen/native/Convolution.cpp, I’m still facing the problem : <strong>error:</strong> <strong>no type named ‘IntArrayRef’ in namespace ‘at’</strong> May I ask where is this IntArrayRef defined?</li>
<li>I was not be able to use the dispatch as what the source code did at pytorch/pytorch/blob/master/aten/src/ATen/native/Convolution.cpp line 21: DEFINE_DISPATCH(convolution_depthwise3x3_winograd_stub);<br>
The reason I’m using it is because I also want to call convolution_depthwise3x3_winograd_stub();</li>
<li>This may sounds like a silly one, my convolution is executed based on the zeros and ones in my mask, so I was trying to use if-statements. It seems I was not be able to do some comparisons like:<br>
if (mask[i] == 0) or if (mask[i] == torch::zeros(shape)), it will return an error of <strong>error:</strong> <strong>value of type ‘at::Tensor’ is not contextually convertible to ‘bool’</strong>. May I ask how should I do it?</li>
</ol>

YimingGan: <p>So the first question, I was able to solve it. According to <a href="https://pytorch.org/cppdocs/api/typedef_namespacec10_1ae2c46d838a1de023ea17ade42f417669.html#exhale-typedef-namespacec10-1ae2c46d838a1de023ea17ade42f417669" rel="nofollow noopener">https://pytorch.org/cppdocs/api/typedef_namespacec10_1ae2c46d838a1de023ea17ade42f417669.html#exhale-typedef-namespacec10-1ae2c46d838a1de023ea17ade42f417669</a>, IntArrayRef is declared under name space of c10. When I change it to ArrayRef&lt;int64_t&gt;, it seems work.</p>

YimingGan: <p>The new question is that, in this lane, <a href="https://github.com/pytorch/pytorch/blob/bee97d5be0fc89e415c611054d1f6645a35e5577/aten/src/ATen/native/Convolution.cpp#L615" rel="nofollow noopener">https://github.com/pytorch/pytorch/blob/bee97d5be0fc89e415c611054d1f6645a35e5577/aten/src/ATen/native/Convolution.cpp#L615</a><br>
a function expand_param_if_needed() was used but not declared ahead of time. Any clues on where it comes from?</p>

YimingGan: <p>expand_param_if_needed() is in aten/src/ATen/native/utils/ParamUtils.h</p>

YimingGan: <p>The third question, should be using mask[i].item()</p>

Lin_Jia: <p>It seems you have figured out most of your questions. Regarding  DEFINE_DISPATCH, it seems that it is defined here: <a href="https://github.com/pytorch/pytorch/blob/master/aten/src/ATen/native/DispatchStub.h" rel="nofollow noopener">https://github.com/pytorch/pytorch/blob/master/aten/src/ATen/native/DispatchStub.h</a></p>
<p><span class="hashtag">#define</span> DEFINE_DISPATCH(name) struct name name</p>
<p>If you get ATen related header included, you should be able to use it, right?</p>

YimingGan: <p>Hi, yeah. Thank you for the help. I currently solved it!</p>

m.sravya_Reddy: <p>Hello, I am trying to implement something similar. It would be a great help if you could provide your code snippet here. Thanks in advance.</p>