4

I am working on a C++ application running Tensorflow. The app has to run well with fork() as the code bellow. However, I have learned that Tensorflow runtime is not fork-safe. The code hangs when it reaches to the Session.run() command. As I am a beginner in C++, I would like to ask you for suggestion how should I run Tensorflow in this case.

Edit: The object is created in the main thread and fork() may be called many times.

int main ()
{
  Myapp myapp("path_to_model");
  switch(fork()) {
    case 0: {/* Child */
        myapp.get_embedding(some_data);
        break;
    }
    case -1:{ /* Error */
        cerr << "Problem forking" << endl;
        break;
    }
    default:
        cout << "on the parent process";
        break;
    }
  }

  return 0;
}

myapp:

//#ifndef _MYAPP_H_
//#define _MYAPP_H_
#ifndef MYAPP_H_
#define MYAPP_H_

#include <iostream>
#include <tensorflow/core/public/session.h>
using namespace std;
using namespace tensorflow;

class Myapp {
    string protobuf_fn; // model filename
    public:
        Session *sess;
        /*constructor with model file name*/
        Myapp (string pb_fn){
            //init the session and load the graph
          TF_CHECK_OK(loadModelpb(pb_fn));
        }
        /*read trained model file into the session*/
        tensorflow::Status loadModelpb(string pb_fn);

        /* get an embedding by running Session.run()
         * For now, it hangs when gets called on a child process.
         */
        Tensor get_embedding(Data some_data) const;

};

#endif /*_MYAPP_H_*/

Thank you very much in advance

Hoa Vu
  • 2,865
  • 4
  • 25
  • 33
  • Unrelated to your problem, but names beginning with an underscore and followed by an upper-case letter (like for example `_MYAPP_H_`) are reserved for the compiler and standard library implementations. Do not define such symbols or names yourself. See [What are the rules about using an underscore in a C++ identifier?](https://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) for more details. – Some programmer dude Sep 13 '18 at 09:03
  • @Someprogrammerdude: Thank you. – Hoa Vu Sep 13 '18 at 09:05
  • If you fork first and then initialise Tensorfllow, you should be Ok. – n. m. could be an AI Sep 13 '18 at 09:16
  • @n.m. That sounds reasonable but it may fork many times and the object is created in the main process. I'll add this to the question. – Hoa Vu Sep 13 '18 at 09:21

0 Answers0