# Load pytorch .pt file in the audio plugin

**URL:** <https://forum.juce.com/t/load-pytorch-pt-file-in-the-audio-plugin/37850>\
**Category:** Audio Plugins\
**Created:** [March 1, 2020, 1:40pm UTC](https://forum.juce.com/t/load-pytorch-pt-file-in-the-audio-plugin/37850 "2020-03-01T13:40:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![YangHungChih](https://avatars.discourse-cdn.com/v4/letter/y/8e7dd6/32.png) [@YangHungChih](https://forum.juce.com/u/YangHungChih)\
**Post date:** [March 1, 2020, 1:40pm UTC](https://forum.juce.com/t/load-pytorch-pt-file-in-the-audio-plugin/37850/1 "2020-03-01T13:40:54Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex026/uploads/juce/original/2X/7/781d7c73fbd2c6cce2eec3b6a62707bf10da5ed0.png)

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]);
			}
		}
		++startSample;

    }
    
    }

```

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

Can anyone help me?

---

<div class="post-metadata">

**Author:** ![PluginPenguin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/pluginpenguin/32/10203_2.png) [@PluginPenguin](https://forum.juce.com/u/PluginPenguin)\
**Post date:** [March 1, 2020, 5:58pm UTC](https://forum.juce.com/t/load-pytorch-pt-file-in-the-audio-plugin/37850/2 "2020-03-01T17:58:04Z")

</div>

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

```auto
*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 😉

---

<div class="post-metadata">

**Author:** ![YangHungChih](https://avatars.discourse-cdn.com/v4/letter/y/8e7dd6/32.png) [@YangHungChih](https://forum.juce.com/u/YangHungChih)\
**Post date:** [March 3, 2020, 5:34am UTC](https://forum.juce.com/t/load-pytorch-pt-file-in-the-audio-plugin/37850/3 "2020-03-03T05:34:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![raikarsagar](https://avatars.discourse-cdn.com/v4/letter/r/ce7236/32.png) [@raikarsagar](https://forum.juce.com/u/raikarsagar)\
**Post date:** [May 2, 2020, 6:55am UTC](https://forum.juce.com/t/load-pytorch-pt-file-in-the-audio-plugin/37850/4 "2020-05-02T06:55:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pak](https://avatars.discourse-cdn.com/v4/letter/p/4bbf92/32.png) [@pak](https://forum.juce.com/u/pak)\
**Post date:** [June 21, 2021, 7:08am UTC](https://forum.juce.com/t/load-pytorch-pt-file-in-the-audio-plugin/37850/5 "2021-06-21T07:08:53Z")

</div>

Is have sample project on git? Thanks

---

<div class="post-metadata">

**Author:** ![200gaga](https://avatars.discourse-cdn.com/v4/letter/2/dbc845/32.png) [@200gaga](https://forum.juce.com/u/200gaga)\
**Post date:** [October 19, 2022, 11:27pm UTC](https://forum.juce.com/t/load-pytorch-pt-file-in-the-audio-plugin/37850/6 "2022-10-19T23:27:46Z")

</div>

Hi Yang,

May I ask how you solve the dependencies when you distribute the plugin?
