Torch.jit.save - cannot create weak reference to 'numpy.ufunc' object

username: AfonsoSalgadoSousa
cooked: <p>I quantized a model that uses modules from <a href="https://github.com/qiuqiangkong/torchlibrosa" rel="noopener nofollow ugc">torch librosa</a>.<br>
I can quantize it and pass batches through the quantized model, but when trying to save it, the module LogmelFilterBank throws the following error:<br>
<code>cannot create weak reference to 'numpy.ufunc' object</code><br>
The source code for this module is the following:</p>
<pre><code class="lang-auto">class LogmelFilterBank(nn.Module):
    def __init__(self, sr=22050, n_fft=2048, n_mels=64, fmin=0.0, fmax=None, 
        is_log=True, ref=1.0, amin=1e-10, top_db=80.0, freeze_parameters=True):
        r"""Calculate logmel spectrogram using pytorch. The mel filter bank is 
        the pytorch implementation of as librosa.filters.mel 
        """
        super(LogmelFilterBank, self).__init__()

        self.is_log = is_log
        self.ref = ref
        self.amin = amin
        self.top_db = top_db
        if fmax == None:
            fmax = sr//2

        self.melW = librosa.filters.mel(sr=sr, n_fft=n_fft, n_mels=n_mels,
            fmin=fmin, fmax=fmax).T
        # (n_fft // 2 + 1, mel_bins)

        self.melW = nn.Parameter(torch.Tensor(self.melW))

        if freeze_parameters:
            for param in self.parameters():
                param.requires_grad = False

    def forward(self, input):
        r"""Calculate (log) mel spectrogram from spectrogram.

        Args:
            input: (*, n_fft), spectrogram
        
        Returns: 
            output: (*, mel_bins), (log) mel spectrogram
        """

        # Mel spectrogram
        mel_spectrogram = torch.matmul(input, self.melW)
        # (*, mel_bins)

        # Logmel spectrogram
        if self.is_log:
            output = self.power_to_db(mel_spectrogram)
        else:
            output = mel_spectrogram

        return output


    def power_to_db(self, input):
        r"""Power to db, this function is the pytorch implementation of 
        librosa.power_to_lb
        """
        ref_value = self.ref
        log_spec = 10.0 * torch.log10(torch.clamp(input, min=self.amin))
        log_spec -= 10.0 * np.log10(np.maximum(self.amin, ref_value))

        if self.top_db is not None:
            if self.top_db &lt; 0:
                raise librosa.util.exceptions.ParameterError('top_db must be non-negative')
            log_spec = torch.clamp(log_spec, min=log_spec.max().item() - self.top_db)

        return log_spec
</code></pre>
<p>I cannot understand where is the weak reference being thrown or how can I prevent it. I tried using the decorator tags as in <a href="https://discuss.pytorch.org/t/convert-model-to-torch-script-via-scripting-got-typeerror-cannot-create-weak-reference-to-builtin-function-or-method-object/28438">this issue</a>, but could not solve the problem. Thanks for any help you can provide.</p>

username: amyxguo
cooked: <p>I encountered the same problem. Also would like to see if anyone has found a way to fix or prevent it.</p>

username: amyxguo
cooked: <p>I fixed the issue by replacing ndarray math operators (i.e. <code>np.add</code>, <code>np.sub</code>, <code>np.ceil</code>, and <code>+</code> <code>-</code> etc) with torch versions (i.e.<code>torch.add</code>, <code>torch.sub</code> etc).</p>