Loading PyTorch model using BinaryData


Just to preface I am a total c++ noob so hopefully my issues are easily answered!

I am attempting to load a PyTorch jit model into a JUCE project roughly following the Pytorch guide. I am using Juce 6 with cmake. Inside my cmake I have the following line juce_add_binary_data(NeuralDX7Model SOURCES NeuralDX7.jit)

  1. After adding this line and running cmake -B build/ -GUnix\ Makefiles -DCMAKE_PREFIX_PATH=libtorch suddenly takes over 10G of memory to complete, where as previously it was much less than that. Is that normal?

  2. Attempting to load the binary using auto* tmp = BinaryData::NeuralDX7_jit; results in the compilation error /usr/bin/ld: libNeuralDX7Model.a(BinaryData1.cpp.o): relocation R_X86_64_PC32 against symbol '_ZN10BinaryData13NeuralDX7_jitE' can not be used when making a shared object; recompile with -fPIC. I tried cmake --build build -j7 -- -fPIC but it didn’t work.

  3. I am not totally sure how to go from the const char* that BinaryData returns to a loaded PyTorch model. I think the most promising looking function for this is here but it requires an std::istream object, SO says to do this but I’m not sure if that answer is tailored for printable strings and if loading binary with it would break those assumptions.

Any help appreciated!

Thanks for trying out JUCE 6 and the new CMake support!

That sounds unusual, but it’s difficult to say where the issue may lie. Is there a particular line of output before the memory spike?

CMake binary data targets don’t have PIC enabled by default. If you know you’ll be using a particular binary data target in a plugin, add the following to your cmakelists after adding the binary data target:

set_target_properties(my_binary_data PROPERTIES # replace target name

Hi @nintoracaudio

BinaryData in JUCE is for data that you essentially want compiled into your program. Do you specifically want this?

If not, I think it would be easier to load your Torch model from disk at runtime as suggested in the tutorial you linked. This will avoid jumping through various hoops you’re experiencing.


Thanks for the fast replies!

A pleasure, thanks for providing such an awesome framework :slight_smile:

Memory spikes while cmake reports running this line
[ 2%] Building CXX object CMakeFiles/NeuralDX7Model.dir/juce_binarydata_NeuralDX7Model/JuceLibraryCode/BinaryData1.cpp.o, the binary is 26MB. I tried compiling without the binary and memory usage maxed out around 3GB

Thanks, that works :+1:

Yes, I would rather package everything into a single binary, I think it makes things easier for the end user

For anyone that stumbles across this thread, this is the solution I came up with for loading libtorch jit models with JUCE BinaryData;

    std::stringstream modelStream;
    modelStream.write(BinaryData::Model_jit, BinaryData::Model_jitSize);
    model = torch::jit::load(modelStream);
1 Like