I want to build a DNN model that I will later integrate into a C++ program.

I heard that PyTorch model is hard to load it on C++ and the integration requires extra code, and it's complicated.

I have searched a bit on the internet and I have found this post: Loading a PyTorch Model in C++. I am still unsure whether or not I will have any problems in practice if I opt for PyTorch.

  • $\begingroup$ Is you network structure complex? Because it might be feasible to roll your own C++ inference engine. A fully-connected layer is just a straight matrix multiply plus activation. $\endgroup$ – MSalters Mar 1 at 18:09
  • $\begingroup$ @MSalters I have variable length input and i will use MLPs for that with 3 hidden layer. The structure of the network is not very complex. But according to what i have seen about pytorch-cpp sigmoid and other are not implemented :(. $\endgroup$ – DINA TAKLIT Mar 2 at 8:01
  • 1
    $\begingroup$ A quick way is to embed python in c++ and call python pytorch model from c++ code $\endgroup$ – zed111 2 days ago

PyTorch now has a C++ frontend. I haven't tried it, but I'm sure you could use that.

Another option, which is more production-tested, is using a message passing framework such as ZeroMQ to communicate requests and results between Python and C++ executables.

  • $\begingroup$ first thank you for you answer. ZeroMQ serve as intermediate btw C++ and python now what if we wanna call directly the model from C++ code what i want to say is like to import it us script that will be used directly with C++ without any intermediate btw. In this case how should we process? Have you understood me ? $\endgroup$ – DINA TAKLIT Feb 26 at 16:23
  • $\begingroup$ You'd have to use the PyTorch C++ frontend for that. There's a tutorial on how to do it here. $\endgroup$ – Omegastick Feb 27 at 1:25
  • $\begingroup$ I have read carefully that tuto but you know this is the only doc that show how and their is lack ex that show how to do this load not only i have seen those who get some error in the process they could not get response, that means this option is not really covered. I need to look more. $\endgroup$ – DINA TAKLIT Feb 27 at 5:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.