Skip to content

Commit

Permalink
Refactoring to separate TF and nGraph APIs (#798)
Browse files Browse the repository at this point in the history
* Remove TF Status/Errors from bridge internal API. Use exceptions instead.
* Split TF and bridge utils into separate files
* Use absl string split and join functions instead of ngraph join and split
* Move TF op supported check inside backend
* Add Backend::name() to get a backend's name
* Remove nGraph tracing
  • Loading branch information
Abhishek Kulkarni committed Feb 5, 2021
1 parent faa0d4d commit ea64224
Show file tree
Hide file tree
Showing 28 changed files with 668 additions and 742 deletions.
68 changes: 20 additions & 48 deletions examples/cpp/infer_multiple_networks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@

#include "ngraph_bridge/backend_manager.h"
#include "ngraph_bridge/timer.h"
#include "ngraph_bridge/utils.h"
#include "ngraph_bridge/version.h"

#include "inference_engine.h"
Expand All @@ -62,15 +61,6 @@ void PrintAvailableBackends() {
}
}

// Sets the specified backend. This backend must be set BEFORE running
// the computation
tf::Status SetNGraphBackend(const string& backend_name) {
// Select a backend
tf::Status status =
tf::ngraph_bridge::BackendManager::SetBackend(backend_name);
return status;
}

void PrintVersion() {
// Tensorflow version info
std::cout << "Tensorflow version: " << tensorflow::ngraph_bridge::tf_version()
Expand Down Expand Up @@ -158,12 +148,6 @@ int main(int argc, char** argv) {
return -1;
}

const char* backend = "CPU";
if (SetNGraphBackend(backend) != tf::Status::OK()) {
std::cout << "Error: Cannot set the backend: " << backend << std::endl;
return -1;
}

std::cout << "Component versions\n";
PrintVersion();

Expand Down Expand Up @@ -194,27 +178,23 @@ int main(int argc, char** argv) {
TF_CHECK_OK(benchmark::InferenceEngine::CreateSession(graph, session_three));
session_db[session_three.get()] = "Three";
std::vector<Tensor> outputs;
{
NG_TRACE("Compilation", "Compilation", "");

//
// Warm-up i.e., Call it onces to get the nGraph compilation done
//
Tensor next_image;
TF_CHECK_OK(inference_engine.GetNextImage(next_image));
// Run inference once. This will trigger a compilation
tf::ngraph_bridge::Timer compilation_time;
TF_CHECK_OK(session_one->Run({{input_layer, next_image}}, {output_layer},
{}, &outputs));
TF_CHECK_OK(session_two->Run({{input_layer, next_image}}, {output_layer},
//
// Warm-up i.e., Call it onces to get the nGraph compilation done
//
Tensor next_image;
TF_CHECK_OK(inference_engine.GetNextImage(next_image));
// Run inference once. This will trigger a compilation
tf::ngraph_bridge::Timer compilation_time;
TF_CHECK_OK(session_one->Run({{input_layer, next_image}}, {output_layer}, {},
&outputs));
TF_CHECK_OK(session_two->Run({{input_layer, next_image}}, {output_layer}, {},
&outputs));
TF_CHECK_OK(session_three->Run({{input_layer, next_image}}, {output_layer},
{}, &outputs));
TF_CHECK_OK(session_three->Run({{input_layer, next_image}}, {output_layer},
{}, &outputs));
compilation_time.Stop();
compilation_time.Stop();

cout << "Compilation took: " << compilation_time.ElapsedInMS() << " ms"
<< endl;
}
cout << "Compilation took: " << compilation_time.ElapsedInMS() << " ms"
<< endl;
//
// Add these sessions to the queue
//
Expand All @@ -241,8 +221,6 @@ int main(int argc, char** argv) {
// Run the inference loop
//-----------------------------------------
for (int i = 0; i < iteration_count; i++) {
NG_TRACE(oss.str(), to_string(i), "");

tf::ngraph_bridge::Timer get_image_timer;
//
// Get the image
Expand All @@ -255,21 +233,15 @@ int main(int argc, char** argv) {
// Get the next available network model (i.e., session)
//
tf::ngraph_bridge::Timer execute_inference_timer;
unique_ptr<Session> next_available_session;
{
NG_TRACE("Get Session", string("Iteration") + to_string(i), "");
next_available_session = session_queue.GetNextAvailable();
}
unique_ptr<Session> next_available_session =
session_queue.GetNextAvailable();

//
// Run inference on this network model (i.e., session)
//
{
NG_TRACE("Run Session", string("Iteration") + to_string(i), "");
TF_CHECK_OK(next_available_session->Run({{input_layer, next_image}},
{output_layer}, {},
&output_each_thread));
}
TF_CHECK_OK(next_available_session->Run({{input_layer, next_image}},
{output_layer}, {},
&output_each_thread));
Session* next_session_ptr = next_available_session.get();
session_queue.Add(move(next_available_session));
execute_inference_timer.Stop();
Expand Down
26 changes: 10 additions & 16 deletions examples/cpp/infer_single_network.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
#include "inference_engine.h"
#include "ngraph_bridge/backend_manager.h"
#include "ngraph_bridge/timer.h"
#include "ngraph_bridge/utils.h"
#include "ngraph_bridge/version.h"

using namespace std;
Expand Down Expand Up @@ -163,20 +162,16 @@ int main(int argc, char** argv) {

Tensor next_image;
std::vector<Tensor> outputs;
{
NG_TRACE("Compilation", "Compilation", "");

// Call it onces to get the nGraph compilation done
TF_CHECK_OK(inference_engine.GetNextImage(next_image));
// Run inference once. This will trigger a compilation
tf::ngraph_bridge::Timer compilation_time;
TF_CHECK_OK(the_session->Run({{input_layer, next_image}}, {output_layer},
{}, &outputs));
compilation_time.Stop();

cout << "Compilation took: " << compilation_time.ElapsedInMS() << " ms"
<< endl;
}
// Call it onces to get the nGraph compilation done
TF_CHECK_OK(inference_engine.GetNextImage(next_image));
// Run inference once. This will trigger a compilation
tf::ngraph_bridge::Timer compilation_time;
TF_CHECK_OK(the_session->Run({{input_layer, next_image}}, {output_layer}, {},
&outputs));
compilation_time.Stop();

cout << "Compilation took: " << compilation_time.ElapsedInMS() << " ms"
<< endl;

atomic<int> total_time_in_ms{0};
atomic<int> total_images_processed{0};
Expand All @@ -185,7 +180,6 @@ int main(int argc, char** argv) {
ostringstream oss;
oss << "Worker_" << worker_id;
for (int i = 0; i < iteration_count; i++) {
NG_TRACE(oss.str(), to_string(i), "");
tf::ngraph_bridge::Timer iteration_timer;
// Get the image
Tensor next_image;
Expand Down
1 change: 1 addition & 0 deletions ngraph_bridge/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ set(SRC
pass/transpose_sinking.cc
tf_graphcycles.cc
tf_deadness_analysis.cc
tf_utils.cc
utils.cc
version.cc
)
Expand Down
14 changes: 10 additions & 4 deletions ngraph_bridge/api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,21 @@ bool IsEnabled() { return _is_enabled; }
vector<string> ListBackends() { return BackendManager::GetSupportedBackends(); }

bool SetBackend(const string& type) {
return (BackendManager::SetBackend(type) == Status::OK());
try {
BackendManager::SetBackend(type);
} catch (...) {
return false;
}
return true;
}

string GetBackend() {
string backend;
if (BackendManager::GetBackendName(backend) != Status::OK()) {
try {
auto backend = BackendManager::GetBackend();
return backend->Name();
} catch (...) {
return "";
}
return backend;
}

void StartLoggingPlacement() { _is_logging_placement = true; }
Expand Down
1 change: 1 addition & 0 deletions ngraph_bridge/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ extern void StopLoggingPlacement();
extern bool IsLoggingPlacement();

extern std::set<string> GetDisabledOps();

extern void SetDisabledOps(std::set<string>);
extern void SetDisabledOps(string);

Expand Down
8 changes: 5 additions & 3 deletions ngraph_bridge/assign_clusters.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#include <iostream>
#include <sstream>

#include "absl/strings/str_join.h"
#include "absl/strings/str_split.h"
#include "tensorflow/core/framework/attr_value_util.h"
#include "tensorflow/core/framework/graph.pb.h"
#include "tensorflow/core/framework/node_def_util.h"
Expand All @@ -33,7 +35,6 @@
#include "mark_for_clustering.h"
#include "tf_deadness_analysis.h"
#include "tf_graphcycles.h"
#include "utils.h"

using namespace std;

Expand Down Expand Up @@ -650,7 +651,8 @@ Status AssignClusters(Graph* graph) {
"assigned an encapsulate)\n";
for (auto it : cluster_separation_reason) {
num_non_contracted += it.second.size();
auto cluster_id_vector = ngraph::split(it.first, ',');
std::vector<std::string> cluster_id_vector =
absl::StrSplit(it.first, ',');
// function to find if this cluster became an ngraph_cluster
// returns ngraph_cluster id if yes, else returns -1
auto find_in_map = [&cluster_to_encapsulate, &cluster_id_vector](int x) {
Expand Down Expand Up @@ -688,7 +690,7 @@ Status AssignClusters(Graph* graph) {
to_string(dst_encapsulate) + "] predicate: " +
std::get<1>(deadness_predicates_tpl) +
" Neighbours predicates: " +
ngraph::join(std::get<2>(deadness_predicates_tpl)) + "\n");
absl::StrJoin(std::get<2>(deadness_predicates_tpl), "\n"));
}
}
reason_count_clusters[inner_itr]++;
Expand Down
Loading

0 comments on commit ea64224

Please sign in to comment.