Skip to content

InverseMelScale does not work in inference mode #1902

@mthrok

Description

@mthrok

InverseMelScale uses SGD inside so it does not work when the global context is no_grad or inference_mode. Or even when requires_grad=False would make it fail. This gives bad UX for inference.

There are couple of possible workarounds

  1. Clone Tensor with requires_grad=True inside of InverseMelScale
  2. Use other method such as SVD. (there was some discussion InverseMelScale Implementation #366)
import torch
import torchaudio

trans = torchaudio.transforms.InverseMelScale(n_stft=10)

spec = torch.randn(128, 256, requires_grad=True)
trans(spec)  # pass
with torch.inference_mode():
    trans(spec)  # fail

spec = torch.randn(128, 256, requires_grad=False)
trans(spec)  # fail
with torch.inference_mode():
    trans(spec)  # fail
Traceback (most recent call last):
  File "foo.py", line 10, in <module>
    trans(spec)
  File "/miniconda3/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1102, in _call_impl
    return forward_call(*input, **kwargs)
  File "/Development/torchaudio/torchaudio/transforms.py", line 472, in forward
    new_loss.backward()
  File "/miniconda3/lib/python3.8/site-packages/torch/_tensor.py", line 307, in backward
    torch.autograd.backward(self, gradient, retain_graph, create_graph, inputs=inputs)
  File "/miniconda3/lib/python3.8/site-packages/torch/autograd/__init__.py", line 154, in backward
    Variable._execution_engine.run_backward(
RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions