nc_
nc_

Reputation: 13

Passing RunOptions in TensorFlow C++

I currently have a TensorFlow graph that I created in Python and exported (as a protobuf) into C++. I am running enqueue/dequeue ops (FIFOQueue) via a session->Run(...) call, and need the call to timeout after not being able to run the operation for some time. I am able to do this in Python by feeding RunOptions into sess.run(...). Is there a similar way to do this in C++?

Upvotes: 1

Views: 1761

Answers (1)

javidcf
javidcf

Reputation: 59731

If you look at the headers of the current version (v1.3.0, although it seems to have been the same ever since RunOptions was created for v0.8.0) of tensorflow::Session and tensorflow::ClientSession, there are function signatures marked as "experimental" that allow you to pass a RunOptions object. A pointer RunMetadata argument seems also to be required if you call Run this way, and looking at the implementation it doesn't seem like you can pass nullptr. So you could do something like this:

#include <vector>
#include <tensorflow/core/public/session.h>

int main(int argc, char *argv[])
{
    const int64_t TIMEOUT_MS = ...;  // Timeout in milliseconds
    tensorflow::GraphDef graph = ...;  // Load you graph definition
    tensorflow::Session *newSession;
    auto status = tensorflow::NewSession(tensorflow::SessionOptions(), &newSession);
    if (!status.ok()) { /* Handle error. */ }
    status = session->Create(graph);
    if (!status.ok()) { /* Handle error. */ }
    tensorflow::RunOptions run_options;
    run_options.set_timeout_in_ms(TIMEOUT_MS);
    tensorflow::RunMetadata run_metadata;
    // Run parameters
    std::vector<std::pair<string, Tensor> > inputs = ...;
    std::vector<string> output_tensor_names = ...;
    std::vector<string> target_node_names = ...;
    std::vector<Tensor> outputs;
    // Run
    status = sess.Run(run_options, inputs, output_tensor_names,
                      target_node_names, &outputs, &run_metadata);
    if (!status.ok()) { /* Handle error. */ }
    // Use output
    // ...
    return 0;
}

Looks like the interface has been there for a long time, but since it is marked as experimental it is possible that it displays some bugs or it just changes in an upcoming version.

Upvotes: 1

Related Questions