Correct usage of torch::nn::functional::pad and torch::nn::functional::conv1d

username: Esmaeil_Farhang
cooked: <p>can anyone have a sample for the correct use of  and torch::nn::functional::pad in CPP,<br>
this is python code</p>
<p>input_data = torch.nn.functional.pad( input_data.unsqueeze(1), (2,2, 0, 0), mode=‘reflect’)</p>
<p>and I cant convert this line to CPP with libtorch. equal function in libtorch is<br>
torch:;nn::functional::pad()<br>
what is the type of parameters in this function?</p>

username: 21fa417e3fb06e56040c
cooked: <p>Hi<br>
The function exists below link , is still before release.<br>
If you need the following function, either code it yourself or use the nightly build version.</p>
<pre><code class="lang-auto">//usage sample
torch ::nn::functional::detail ::pad( 
input_data.unsqueeze(1), {2,2,0,0},torch::nn::functional::PadFuncOptions().mode(torch::kReplicate));

</code></pre>
<aside class="onebox githubblob">
  <header class="source">
      <a href="https://github.com/pytorch/pytorch/blob/433baf1b9067774aecdeef8e32664827779ddb86/torch/csrc/api/include/torch/nn/functional/padding.h#L31" target="_blank" rel="nofollow noopener">github.com</a>
  </header>
  <article class="onebox-body">
    <h4><a href="https://github.com/pytorch/pytorch/blob/433baf1b9067774aecdeef8e32664827779ddb86/torch/csrc/api/include/torch/nn/functional/padding.h#L31" target="_blank" rel="nofollow noopener">pytorch/pytorch/blob/433baf1b9067774aecdeef8e32664827779ddb86/torch/csrc/api/include/torch/nn/functional/padding.h#L31</a></h4>
<pre class="onebox"><code class="lang-h"><ol class="start lines" start="21" style="counter-reset: li-counter 20 ;">
<li>
</li>
<li>  if (padding.size() &gt; 4) {</li>
<li>    input = torch::cat({input, _narrow_with_range(input, 4, 0, padding[-5 + padding.size()])}, /*dim=*/4);</li>
<li>    input = torch::cat({_narrow_with_range(input, 4, -(padding[-5 + padding.size()] + padding[-6 + padding.size()]), -padding[-5 + padding.size()]), input}, /*dim=*/4);</li>
<li>  }</li>
<li>
</li>
<li>  return input;</li>
<li>}</li>
<li>
</li>
<li>namespace detail {</li>
<li class="selected">inline Tensor pad(const Tensor&amp; input,</li>
<li>                  IntArrayRef pad,</li>
<li>                  PadFuncOptions::mode_t mode,</li>
<li>                  double value) {</li>
<li>  TORCH_CHECK(pad.size() % 2 == 0, "Padding length must be divisible by 2");</li>
<li>  TORCH_CHECK(((int64_t)(pad.size() / 2)) &lt;= input.dim(), "Padding length too large");</li>
<li>  if (c10::get_if&lt;enumtype::kConstant&gt;(&amp;mode)) {</li>
<li>    return torch::constant_pad_nd(input, pad, value);</li>
<li>  } else {</li>
<li>    TORCH_CHECK(</li>
<li>      value == 0,</li>
</ol></code></pre>


  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>


username: Esmaeil_Farhang
cooked: <aside class="quote no-group" data-username="21fa417e3fb06e56040c" data-post="2" data-topic="62778">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/21fa417e3fb06e56040c/48/49713_2.png" class="avatar"> 21fa417e3fb06e56040c:</div>
<blockquote>
<p>torch::kReplicate</p>
</blockquote>
</aside>
<p>thanks a lot. I can run it on ubutnu 18.04 with nightly build version.<br>
i used this code:</p>
<pre><code class="lang-auto">std::vector&lt;int64_t&gt; pad {(int)(filterLength/2), (int)(filterLength/2),0,0};
torch::nn::functional::PadFuncOptions option (pad);
option.mode(torch::kReplicate);
input_data = torch::nn::functional::pad(input_data.unsqueeze(1), option);
</code></pre>

username: 21fa417e3fb06e56040c
cooked: <aside class="quote no-group" data-username="Esmaeil_Farhang" data-post="3" data-topic="62778">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/esmaeil_farhang/48/18356_2.png" class="avatar"> Esmaeil_Farhang:</div>
<blockquote>
<p>pad</p>
</blockquote>
</aside>
<p>i think , can work below code,<br>
Can you try and tell me?</p>
<pre data-code-wrap="c++"><code class="lang-c++"> torch::nn::functional::pad(input_data.unsqueeze(1), torch::nn::functional::PadFuncOptions({(int)(filterLength/2), (int)(filterLength/2),0,0}).mode(torch::kReplicate));
</code></pre>

username: Esmaeil_Farhang
cooked: <p>in runtime occurred:</p>
<pre><code class="lang-auto">`terminate called after throwing an instance of 'c10::Error'
  what():  Calculated padded input size per channel: (7). Kernel size: (8). Kernel size can't be greater than actual input size (check_shape_forward at ../../aten/src/ATen/native/Convolution.cpp:436)
frame #0: c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;) + 0x6a (0x7fbfa34c7bea in /media/gata/Code/denoiser_cpp/libtorch/lib/libc10.so)
frame #1: &lt;unknown function&gt; + 0xbf731c (0x7fbfa42db31c in /media/gata/Code/denoiser_cpp/libtorch/lib/libtorch.so)
frame #2: at::native::_convolution(at::Tensor const&amp;, at::Tensor const&amp;, at::Tensor const&amp;, c10::ArrayRef&lt;long&gt;, c10::ArrayRef&lt;long&gt;, c10::ArrayRef&lt;long&gt;, bool, c10::ArrayRef&lt;long&gt;, long, bool, bool, bool) + 0x48e (0x7fbfa42e1fae in ..
`
</code></pre>

username: yf225
cooked: <p><a class="mention" href="/u/esmaeil_farhang">@Esmaeil_Farhang</a> The equivalent of</p>
<blockquote>
<p>input_data = torch.nn.functional.pad( input_data.unsqueeze(1), (2,2, 0, 0), mode=‘reflect’)</p>
</blockquote>
<p>is</p>
<blockquote>
<p>auto input_data = torch::nn::functional::pad( input_data.unsqueeze(1), torch::nn::functional::PadFuncOptions({2,2, 0, 0}).mode(torch::kReflect))</p>
</blockquote>
<p>To debug the runtime error, it’s best to run the equivalent script in Python first and make sure it works, and then translate it to C++ code.</p>