How to check grads in each step of model?

Hi there:
I’ve implement a Pytorch version of Retrieval-based-Voice-Conversion(RVC for short) at here.
The question is,when i wanna export my implementation pipeline into ONNX using below code:

with torch.inference_mode(), torch.cuda.amp.autocast(enabled=False):
 torch.onnx.export(
     pipeline, 
     (audio.cuda(),),
     "pipeline.onnx",
     input_names=["input"],
     output_names=["output"],
     opset_version=14
 )

It rasing below error:

RuntimeError: Cannot insert a Tensor that requires grad as a constant. Consider making it a parameter or input, or detaching the gradient
Tensor:
 0.6670
[ torch.cuda.HalfTensor{1} ]

Typically rasing with an nn.BatchNorm2d cell called at rmvpe.py at line 244.

So how could i fix this error,since this implementation finally will deploy on C# or model serving platform like NVIDIA Triton.

The error occurs because PyTorch’s torch.onnx.export() method attempts to export a tensor that requires gradients, which is not allowed in ONNX. To fix this, detach the tensor from the computation graph using audio.detach() before passing it to torch.onnx.export(). This prevents the tensor from requiring gradients and allows the export to work smoothly. Additionally, ensure your model is in evaluation mode (model.eval()) before exporting, as this affects the behavior of layers like BatchNorm2d.