Load pytorch .pt file in the audio plugin

I am trying to load .pt file in the constructor of PluginProcessor.h like this:

and use *module in my SynthVoice.h like this:

 void renderNextBlock (AudioBuffer< float > &outputBuffer, int startSample, int numSamples)
	while (--numSamples >= 0)
		std::vector<torch::jit::IValue> inputs;
		inputs.push_back(torch::rand({ 1, 201, 1 }));
		auto output = module->forward(inputs).toTensor();
		auto temp = output.accessor<float, 3>();
		for (auto i = outputBuffer.getNumChannels(); --i >= 0;)
			for (int j = 0; j < temp.size(1); j++)
				outputBuffer.addSample(i, startSample, temp[0][j][0]);


AudioPluginHost.exe crashed when I dragged my audio plugin into it.

Can anyone help me?

Note: I don’t know PyTorch at all, so these comments all address common C++ issues:

First of all: You can find the exact location where your crash happens yourself using your debugger. If you are not familiar with such a common tool, I’d strongly advise you to get familiar with it to become able to help yourself fixing simple problems. You should specify the AudioPluginHost as the application to execute and then start a debugging session from your IDE whith your plugin project loaded. Then perform the same steps and the debugger will stop at the line of code where your crash happens.

That being said, the line

*module = torch::jit::load ("first_ascending_model.pt");

looks suspicious. You are dereferencing this module pointer and assigning a new object to it. How and where is module declared and where do you actually allocate the memory it points to? If you didn’t allocate any memory (and this is what I guess as this line is the very first line of your constructor) you try to assign the object returned by torch::jit::load to a non-existing memory location, which will obviously lead to a crash. In case your debugger tells you that this is your fault, I strongly recommend you to make yourself familiar with the concept of pointers and memory allocation in C++, if you did not understand this completely, you will likely write a lot of crashing code.

Beneath this, I see that you are creating std::vector instances inside your renderNextBlock function. While this should not be a reason for a crash, this is an operation that allocates under the hood and therefore is not suited for usage in a realtime capable rendering callback. You should move all your std::vector creation and push_back operations out of this function and make your inputs vector a class member instead, so that you can prepare it beforehand in your constructor instead of creating new instances over and over. While we are at this, have you investigated if the functions of the PyTorch library that you use in your processing are realtime safe under the hood (e.g. non-blocking, non-allocating)? Otherwise you’ll hardly achieve realtime performance with this approach at all…

One last thing looking at your screenshot: Please do yourself a favour and change the font in your code editor to a monospaced font – code will immediately look so much more structured :wink:

Thanks for your reply. I have already solved my problem. I upgraded my pytorch version and downloaded the debug version of pytorch and my problem was solved.

Hi Yang,
How to compile the plugin along with libtorch library to load a torchscript? Could you please share some insight into this.
Thank you

1 Like

Is have sample project on git? Thanks