ConstantPad1d Deprecated

username: Jay_Timbadia
cooked: <p>Hi, I am trying this wavenet model and been facing issue with this ConstantPad1d function.</p>
<pre><code class="lang-auto">class ConstantPad1d(Function):
    def __init__(self, target_size, dimension=0, value=0, pad_start=False):
        super(ConstantPad1d, self).__init__()
        self.target_size = target_size
        self.dimension = dimension
        self.value = value
        self.pad_start = pad_start

    @staticmethod
    def forward(self, input):
        self.num_pad = self.target_size - input.size(self.dimension)
        assert self.num_pad &gt;= 0, 'target size has to be greater than input size'

        self.input_size = input.size()

        size = list(input.size())
        size[self.dimension] = self.target_size
        output = input.new(*tuple(size)).fill_(self.value)
        c_output = output

        if self.pad_start:
            c_output = c_output.narrow(self.dimension, self.num_pad, c_output.size(self.dimension) - self.num_pad)
        else:
            c_output = c_output.narrow(self.dimension, 0, c_output.size(self.dimension) - self.num_pad)

        c_output.copy_(input)
        return output

    @staticmethod
    def backward(self, grad_output):
        grad_input = grad_output.new(*self.input_size).zero_()
        cg_output = grad_output

        if self.pad_start:
            cg_output = cg_output.narrow(self.dimension, self.num_pad, cg_output.size(self.dimension) - self.num_pad)
        else:
            cg_output = cg_output.narrow(self.dimension, 0, cg_output.size(self.dimension) - self.num_pad)

        grad_input.copy_(cg_output)
        return grad_input
</code></pre>
<p>Error <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"><br>
RuntimeError: Legacy autograd function with non-static forward method is deprecated. Please use new-style autograd function with static forward method. (Example: <a href="https://pytorch.org/docs/stable/autograd.html#torch.autograd.Function" class="inline-onebox" rel="noopener nofollow ugc">Automatic differentiation package - torch.autograd — PyTorch 1.10 documentation</a>)</p>
<p>Can anyone please help what I need to change here?</p>
<p>I have even raised on their official github, but I guess they are too busy.</p>
<aside class="onebox githubblob" data-onebox-src="https://github.com/vincentherrmann/pytorch-wavenet/blob/master/wavenet_modules.py">
  <header class="source">

      <a href="https://github.com/vincentherrmann/pytorch-wavenet/blob/master/wavenet_modules.py" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>

  <article class="onebox-body">
    <h4><a href="https://github.com/vincentherrmann/pytorch-wavenet/blob/master/wavenet_modules.py" target="_blank" rel="noopener nofollow ugc">vincentherrmann/pytorch-wavenet/blob/master/wavenet_modules.py</a></h4>


      <pre><code class="lang-py">import math
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn import Parameter
from torch.autograd import Variable, Function
import numpy as np


def dilate(x, dilation, init_dilation=1, pad_start=True):
    """
    :param x: Tensor of size (N, C, L), where N is the input dilation, C is the number of channels, and L is the input length
    :param dilation: Target dilation. Will be the size of the first dimension of the output tensor.
    :param pad_start: If the input length is not compatible with the specified dilation, zero padding is used. This parameter determines wether the zeros are added at the start or at the end.
    :return: The dilated tensor of size (dilation, C, L*N / dilation). The output might be zero padded at the start
    """

    [n, c, l] = x.size()
    dilation_factor = dilation / init_dilation
    if dilation_factor == 1:
</code></pre>



  This file has been truncated. <a href="https://github.com/vincentherrmann/pytorch-wavenet/blob/master/wavenet_modules.py" target="_blank" rel="noopener nofollow ugc">show original</a>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>


username: ptrblck
cooked: <p>I don’t know which part of your code is raising the error, but the linked docs should give you an example on how to use the new <code>autograd.Function</code>s (i.e. via their <code>apply</code> op).<br>
In any case, wouldn’t the PyTorch <a href="https://pytorch.org/docs/stable/generated/torch.nn.ConstantPad1d.html?highlight=pad#torch.nn.ConstantPad1d"><code>torch.nn.ConstantPad1d</code></a> method work instead or is your custom layer using a different implementation?</p>

username: Jay_Timbadia
cooked: <p>Sorry for providing in-sufficient context.</p>
<p>Actually the error is coming when I am calling dilate function, and specific location of error is at ConstantPad1d.</p>
<p>Just copy the entire code and run the sample toy I have mentioned below. The error you will see at ConstantPad1d function.</p>
<pre><code class="lang-auto">from modules import dilate
import torch

x = torch.randn(size=(1, 2, 11))

print(x)

new_x = dilate(x, 2, 1)

print(new_x.size())

print(new_x)
</code></pre>