Skip to content
This repository was archived by the owner on Jul 1, 2024. It is now read-only.

Commit 3437cfe

Browse files
authored
Fix TF backwards compatibility issues (#333)
* Enable Grappler only for TF version >=2.6 * Fix type conversion errors for TF 2.4.4 * Fix type conversion issues for TF 2.4.4 * Make base version of TF for Grappler support 2.8 * Fix code formatting
1 parent cede95a commit 3437cfe

File tree

5 files changed

+31
-13
lines changed

5 files changed

+31
-13
lines changed

openvino_tensorflow/CMakeLists.txt

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,19 @@ set(SRC
5050
grappler/costs/cost_analyzer.cc
5151
)
5252

53+
string(REPLACE "." ";" TF_VERSION_LIST ${TensorFlow_VERSION})
54+
list(GET TF_VERSION_LIST 0 TF_MAJOR_VERSION)
55+
list(GET TF_VERSION_LIST 1 TF_MINOR_VERSION)
56+
57+
# Enable Grappler only for TF >= 2.8
58+
if(NOT ((TF_MAJOR_VERSION STRGREATER_EQUAL "2") AND (TF_MINOR_VERSION STRGREATER_EQUAL "8")))
59+
list(REMOVE_ITEM SRC grappler/ovtf_optimizer.cc)
60+
list(REMOVE_ITEM SRC grappler/add_identityn.cc)
61+
list(REMOVE_ITEM SRC grappler/costs/measuring_cost_estimator.cc)
62+
list(REMOVE_ITEM SRC grappler/costs/robust_stats.cc)
63+
list(REMOVE_ITEM SRC grappler/costs/cost_analyzer.cc)
64+
endif()
65+
5366
if(OPENVINO_BUILD_VERSION)
5467
add_compile_definitions(OPENVINO_BUILD_VERSION="${OPENVINO_BUILD_VERSION}")
5568
endif()
@@ -87,8 +100,6 @@ target_compile_definitions(
87100

88101
target_include_directories(${LIB_NAME} PUBLIC "${NGRAPH_INSTALL_DIR}/include")
89102

90-
string(REPLACE "." ";" TF_VERSION_LIST ${TensorFlow_VERSION})
91-
list(GET TF_VERSION_LIST 0 TF_MAJOR_VERSION)
92103
add_compile_definitions(TF_VERSION=${TF_MAJOR_VERSION})
93104

94105
#------------------------------------------------------------------------------

openvino_tensorflow/deassign_clusters.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ Status DeassignClusters(Graph* graph) {
399399
pair_idx_cost.first = cluster_idx;
400400
pair_idx_cost.second = 0;
401401
for (auto n : g.nodes()) {
402-
int64_t node_cost = 0;
402+
tensorflow::int64 node_cost = 0;
403403
if (GetNodeAttr(n->attrs(), "cost", &node_cost) != Status::OK())
404404
continue;
405405
pair_idx_cost.second += node_cost;

openvino_tensorflow/encapsulate_clusters.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ Status Encapsulator::AnalysisPass() {
348348
}
349349
// This snippet is required only for Grappler pass
350350
if (!api::IsRewritePassEnabled()) {
351-
int64_t node_cost = 0;
351+
tensorflow::int64 node_cost = 0;
352352
if (GetNodeAttr(node->attrs(), "cost", &node_cost) != Status::OK())
353353
continue;
354354
cluster_cost_map_in_ms[cluster_idx] += node_cost;
@@ -470,7 +470,7 @@ Status Encapsulator::RewritePass(
470470
.Attr("Targuments", input_types)
471471
.Attr("Tresults", cluster_output_dt_map[cluster_idx])
472472
.Attr("ngraph_graph_id", graph_id)
473-
.Attr("cluster_cost", cluster_cost_map_in_ms[cluster_idx])
473+
.Attr("cluster_cost", (int)cluster_cost_map_in_ms[cluster_idx])
474474
.Device(device_name_map[cluster_idx])
475475
.Input(inputs);
476476
if (!device_config.empty()) {

openvino_tensorflow/kernels/encapsulate_op.cc

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class NGraphEncapsulateOp : public OpKernel {
7272
std::shared_ptr<tensorflow::Session> m_session;
7373
std::vector<std::string> m_session_input_names;
7474
std::vector<std::string> m_session_output_names;
75-
int64_t m_cluster_cost_in_ms;
75+
tensorflow::int64 m_cluster_cost_in_ms;
7676
static std::map<size_t, bool> s_tf_timing_run_enabled_map;
7777
static std::map<size_t, float> s_ovtf_cluster_timings_map;
7878
int64_t m_iter;
@@ -135,8 +135,8 @@ NGraphEncapsulateOp::NGraphEncapsulateOp(OpKernelConstruction* ctx)
135135
}
136136

137137
if (!api::IsRewritePassEnabled()) {
138-
OP_REQUIRES_OK(
139-
ctx, ctx->GetAttr<int64_t>("cluster_cost", &m_cluster_cost_in_ms));
138+
OP_REQUIRES_OK(ctx, ctx->GetAttr<tensorflow::int64>("cluster_cost",
139+
&m_cluster_cost_in_ms));
140140
}
141141

142142
//
@@ -211,9 +211,9 @@ void NGraphEncapsulateOp::Compute(OpKernelContext* ctx) {
211211
if (api::IsRewritePassEnabled() &&
212212
s_tf_timing_run_enabled_map[m_cluster_id]) {
213213
// Measure the timing of cluster through force TF run
214-
int64_t start_ns = profiler::GetCurrentTimeNanos();
214+
int64_t start_ns = absl::GetCurrentTimeNanos();
215215
OP_REQUIRES_OK(ctx, Fallback(ctx));
216-
int64_t duration_in_ms = (profiler::GetCurrentTimeNanos() - start_ns) / 1e6;
216+
int64_t duration_in_ms = (absl::GetCurrentTimeNanos() - start_ns) / 1e6;
217217
OVTF_VLOG(1) << "Iter: " << m_iter;
218218
OVTF_VLOG(1) << "TF: Cluster " << m_cluster_id << " took " << duration_in_ms
219219
<< " ms.";
@@ -409,10 +409,9 @@ void NGraphEncapsulateOp::Compute(OpKernelContext* ctx) {
409409
OVTF_VLOG(4) << "NGraphEncapsulateOp::Compute call starting for cluster "
410410
<< m_cluster_id;
411411
try {
412-
int64_t start_ns = profiler::GetCurrentTimeNanos();
412+
int64_t start_ns = absl::GetCurrentTimeNanos();
413413
ng_exec->Call(ng_inputs, ng_func_outputs, multi_req_execution);
414-
int64_t duration_in_ms =
415-
(profiler::GetCurrentTimeNanos() - start_ns) / 1e6;
414+
int64_t duration_in_ms = (absl::GetCurrentTimeNanos() - start_ns) / 1e6;
416415
OVTF_VLOG(1) << "Iter: " << m_iter;
417416
OVTF_VLOG(1) << "OVTF: Cluster " << m_cluster_id << " took "
418417
<< duration_in_ms << " ms.";

python/openvino_tensorflow/__init__.in.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@
7171
TF_GIT_VERSION = tf.version.GIT_VERSION
7272
TF_VERSION_NEEDED = "${TensorFlow_VERSION}"
7373
TF_GIT_VERSION_BUILT_WITH = "${TensorFlow_GIT_VERSION}"
74+
TF_MAJOR_VERSION = int(TF_VERSION.split(".")[0])
75+
TF_MINOR_VERSION = int(TF_VERSION.split(".")[1])
7476

7577
rewriter_config = rewriter_config_pb2.RewriterConfig()
7678
rewriter_config.meta_optimizer_iterations = (rewriter_config_pb2.RewriterConfig.ONE)
@@ -263,6 +265,9 @@ def optimize_graph_with_openvino_tf1(frozen_model_file,
263265
The optimized GraphDef
264266
"""
265267

268+
if not ((TF_MAJOR_VERSION >= 2) and (TF_MINOR_VERSION >= 8)):
269+
raise AssertionError("Only TF Versions >= 2.8.x are supported for the optimize_graph APIs")
270+
266271
if not os.path.exists(frozen_model_file):
267272
raise AssertionError("Could not find frozen model path")
268273

@@ -346,6 +351,9 @@ def optimize_graph_with_openvino_tf2(saved_model_dir,
346351
"""
347352

348353
#[TODO] Add support for taking direct tf.Graph or tf.function inputs
354+
355+
if not ((TF_MAJOR_VERSION >= 2) and (TF_MINOR_VERSION >= 8)):
356+
raise AssertionError("Only TF Versions >= 2.8.x are supported for the optimize_graph APIs")
349357

350358
if not os.path.exists(saved_model_dir):
351359
raise AssertionError("Could not find saved model path")

0 commit comments

Comments
 (0)