Skip to content
This repository was archived by the owner on Jan 3, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ set(SRC
ngraph_utils.cc
tf_graphcycles.cc
tf_deadness_analysis.cc
ngraph_disable_assert.cc
)

add_library(${LIB_NAME} SHARED ${SRC})
Expand Down
57 changes: 57 additions & 0 deletions src/ngraph_disable_assert.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*******************************************************************************
* Copyright 2017-2018 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
#include "ngraph_disable_assert.h"

#include "tensorflow/core/graph/graph.h"
#include "tensorflow/core/graph/node_builder.h"
#include "tensorflow/core/graph/types.h"

#include "ngraph_utils.h"

using namespace std;

namespace tensorflow {

namespace ngraph_bridge {

//
// Main entry point for disable assert.
//
Status DisableAssert(Graph* graph) {
std::vector<const Edge*> edges;
for (auto node : graph->op_nodes()) {
if (node->type_string() == "Assert") {
NGRAPH_VLOG(4) << "Checking: " << node->name();
for (auto edge : node->out_edges()) {
if (edge->IsControlEdge()) {
if (edge != NULL) {
NGRAPH_VLOG(4) << "Collecting all the control edges";
edges.push_back(edge);
}
}
}
}
}
for (auto edge : edges) {
NGRAPH_VLOG(4) << "Removing control edge: " << edge->DebugString();
graph->RemoveControlEdge(edge);
}
return Status::OK();
}

} // namespace ngraph_bridge

} // namespace tensorflow
29 changes: 29 additions & 0 deletions src/ngraph_disable_assert.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*******************************************************************************
* Copyright 2017-2018 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/

#pragma once

#include "tensorflow/core/graph/graph.h"

namespace tensorflow {

namespace ngraph_bridge {

Status DisableAssert(Graph* graph);

} // namespace ngraph_bridge

} // namespace tensorflow
16 changes: 16 additions & 0 deletions src/ngraph_rewrite_pass.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "ngraph_assign_clusters.h"
#include "ngraph_capture_variables.h"
#include "ngraph_deassign_clusters.h"
#include "ngraph_disable_assert.h"
#include "ngraph_encapsulate_clusters.h"
#include "ngraph_log.h"
#include "ngraph_mark_for_clustering.h"
Expand Down Expand Up @@ -150,6 +151,17 @@ class NGraphVariableCapturePass : public NGraphRewritePass {
DumpGraphs(options, idx, "captured", "Graph With Variables Captured");
}

// Disable "Assert" if specifically asked by the user
if (std::getenv("NGRAPH_TF_DISABLE_ASSERTS") != nullptr) {
TF_RETURN_IF_ERROR(DisableAssert(options.graph->get()));
NGRAPH_VLOG(0) << "Model running with Asserts disabled.";
// If requested, dump captured graphs without asserts
if (DumpDisabledAssertsGraphs()) {
DumpGraphs(options, idx, "assert_disabled",
"Captured Graph without Asserts");
}
}

return Status::OK();
}

Expand All @@ -162,6 +174,10 @@ class NGraphVariableCapturePass : public NGraphRewritePass {
return DumpAllGraphs() ||
std::getenv("NGRAPH_TF_DUMP_CAPTURED_GRAPHS") != nullptr;
}
static bool DumpDisabledAssertsGraphs() {
return DumpAllGraphs() ||
std::getenv("NGRAPH_TF_DISABLE_ASSERTS") != nullptr;
}
};

//
Expand Down
4 changes: 4 additions & 0 deletions test/ci/run-premerge-ci-checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ python -m pytest \
test_slice.py \
test_sigmoidgrad.py \
test_tanhgrad.py

export NGRAPH_TF_DISABLE_ASSERTS=1
python -m pytest test_disable_assert.py
unset NGRAPH_TF_DISABLE_ASSERTS
popd

echo "--------------------------------------------------------------------------"
Expand Down
77 changes: 77 additions & 0 deletions test/python/test_disable_assert.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# ==============================================================================
# Copyright 2018 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""nGraph TensorFlow bridge abs operation test

"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import pytest

import tensorflow as tf
import os

from common import NgraphTest


class TestAssertOperations(NgraphTest):

def test_disable_assert(self):
test_input = ((1, 1))
x = tf.placeholder(tf.int32, shape=(2,))
y = tf.placeholder(tf.int32, shape=(2,))
z = tf.placeholder(tf.int32, shape=(2,))
assert_op = tf.Assert(tf.less_equal(tf.reduce_max(z), 1), [x])

with tf.control_dependencies([assert_op]):
a2 = tf.add(x, y)

def run_test(sess):
return sess.run(
a2, feed_dict={
x: test_input,
y: test_input,
z: test_input
})

assert (
self.with_ngraph(run_test) == self.without_ngraph(run_test)).all()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test wont run with the env variable in the CI, right?


def test_disable_assert_tf_fails_ng_pass(self):
test_input = ((2, 2))
x = tf.placeholder(tf.int32, shape=(2,))
y = tf.placeholder(tf.int32, shape=(2,))
z = tf.placeholder(tf.int32, shape=(2,))
assert_op = tf.Assert(tf.less_equal(tf.reduce_max(z), 1), [x])

with tf.control_dependencies([assert_op]):
a2 = tf.add(x, y)

def run_test(sess):
return sess.run(
a2, feed_dict={
x: test_input,
y: test_input,
z: test_input
})

try:
self.without_ngraph(run_test)
assert False
except tf.errors.InvalidArgumentError as e:
print("hfhrhgthgutrihy")
self.with_ngraph(run_test)