Neutone / neutone_sdk

Join the community on Discord for more discussions around Neutone! https://discord.gg/VHSMzb8Wqp
GNU Lesser General Public License v2.1
465 stars 21 forks source link

RuntimeError: Trying to resize storage that is not resizable using SQW in libtorch C++ #49

Closed robclouth closed 2 months ago

robclouth commented 1 year ago

I'm trying to use a SQW wrapped model in C++ with libtorch. It was working in a previous version but something has changed and now i get this error. I'm calling the function like this:

auto inTensor = torch::from_blob (inBufferPtr, { 2, (int) len });
    auto paramsTensor = torch::zeros ({ 4, (int) len });
    auto outTensor = currModel.get_method ("forward_bt") ({ inTensor, paramsTensor }).toOptional<at::Tensor>();

This is the full error:

The following operation failed in the TorchScript interpreter.
Traceback of TorchScript, serialized code (most recent call last):
  File "code/__torch__/___torch_mangle_5.py", line 444, in forward_bt
        w2w_base = self.w2w_base
        model_in_buffer1 = self.model_in_buffer
        model_out0 = (w2w_base).forward(model_in_buffer1, None, )
                      ~~~~~~~~~~~~~~~~~ <--- HERE
        model_out = model_out0
      else:
  File "code/__torch__/___torch_mangle_3.py", line 110, in forward
    else:
      params2, x0 = params0, x
    params3 = (self).aggregate_params(params2, )
               ~~~~~~~~~~~~~~~~~~~~~~ <--- HERE
    use_debug_mode1 = self.use_debug_mode
    if use_debug_mode1:
  File "code/__torch__/___torch_mangle_3.py", line 355, in aggregate_params
      pass
    agg_params = self.agg_params
    _117 = torch.mean(params, [1], True, out=agg_params)
           ~~~~~~~~~~ <--- HERE
    return self.agg_params
  def get_neutone_parameters(self: __torch__.___torch_mangle_3.RAVEModelWrapper) -> List[__torch__.neutone_sdk.parameter.NeutoneParameter]:

Traceback of TorchScript, original code (most recent call last):
  File "<ipython-input-4-29b4efa9a5ba>", line 283, in forward_bt

            if self.params_queue.is_empty():
                model_out = self.w2w_base.forward(self.model_in_buffer, None)
                            ~~~~~~~~~~~~~~~~~~~~~ <--- HERE
            else:
                params_popped_n = self.params_queue.pop(self.params_buffer)
  File "/usr/local/lib/python3.10/dist-packages/neutone_sdk/wavform_to_wavform.py", line 231, in forward
            params = self.params_buffer

        params = self.aggregate_params(params)
                 ~~~~~~~~~~~~~~~~~~~~~ <--- HERE
        if self.use_debug_mode:
            assert params.ndim == 2
  File "/usr/local/lib/python3.10/dist-packages/neutone_sdk/wavform_to_wavform.py", line 174, in aggregate_params
        if self.use_debug_mode:
            assert params.ndim == 2
        tr.mean(params, dim=1, keepdim=True, out=self.agg_params)
        ~~~~~~~ <--- HERE
        return self.agg_params
RuntimeError: Trying to resize storage that is not resizable

What am I doing wrong? The model has 2 defined parameters but previously I was passing in the paramsTensor with the maximum param count value (4) and it was working. Has something changed?

christhetree commented 11 months ago

Hi Rob, I haven't answered this yet because I also can't figure out why this happening just from looking at it. If I manage to reproduce it or have any insight I'll update here, I'll be working with the SDK and plugin more in the next few months. I'm also a lot less experienced with C++ which makes it harder for me to debug errors like this.

As a general comment, it's more difficult for us to debug issues when interfacing with the SDK in C++ since this is outside the scope of what it was built for (it's designed for wrapping Neutone models in Python to be loaded in the Neutone plugin), so it may take longer to resolve issues like this.

christhetree commented 11 months ago

One quick suggestion I just realized is you could experiment with overriding the

def aggregate_params(self, param: Tensor) -> Tensor:

method to see if that helps with your issue? (e.g. try not taking the mean and doing something simpler instead like using just the first value of the block)

robclouth commented 11 months ago

Ok no worries. That's a good idea! Thanks