how to set inter_op_parallelism_threads and intra_op_parallelism_threads for seesion configuration in Tensorflow c++

浪尽此生 提交于 2021-02-11 13:52:46

问题


I know how to set intra and inter threads in python tensorflow which is as shown below

n_cpus=1
sess = tf.Session(config=tf.ConfigProto(
     device_count={ "CPU": n_cpus },
    inter_op_parallelism_threads=n_cpus,
    intra_op_parallelism_threads=8
));

But I need to implement this in c++. SO any equivalent code to set inter and intra threads in tensorflow c++ ??


回答1:


You can implement this in C++ by using the following code.

Status LoadGraph(string graph_file_name, std::unique_ptr<tensorflow::Session>* session) {
  tensorflow::GraphDef graph_def;
  Status load_graph_status =
      ReadBinaryProto(tensorflow::Env::Default(), graph_file_name, &graph_def);
  if (!load_graph_status.ok()) {
    return tensorflow::errors::NotFound("Failed to load compute graph at '",
                                        graph_file_name, "'");
  }
  
  // initialize the number of worker threads
    tensorflow::SessionOptions options;
    tensorflow::ConfigProto & config = options.config;
    config.set_inter_op_parallelism_threads(1);
    config.set_intra_op_parallelism_threads(1);
    config.set_use_per_session_threads(false); 

  session->reset(tensorflow::NewSession(options));
  Status session_create_status = (*session)->Create(graph_def);
  if (!session_create_status.ok()) {
    return session_create_status;
  }

  return Status::OK();
}


来源:https://stackoverflow.com/questions/61361334/how-to-set-inter-op-parallelism-threads-and-intra-op-parallelism-threads-for-see

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!