def test_command_line_api(self, inp_format, inp_loc, out_format, commandline, ng_device): # Only run this test when grappler is enabled if not ngraph_bridge.is_grappler_enabled(): return assert TestConversionScript.format_and_loc_match(inp_format, inp_loc) out_loc = inp_loc.split('.')[0] + '_modified' + ( '' if out_format == 'savedmodel' else ('.' + out_format)) try: (shutil.rmtree, os.remove)[os.path.isfile(out_loc)](out_loc) except: pass conversion_successful = False try: if commandline: # In CI this test is expected to be run out of artifacts/test/python command_executor('python ../../tools/tf2ngraph.py --input' + inp_format + ' ' + inp_loc + ' --outnodes out_node --output' + out_format + ' ' + out_loc + ' --ngbackend ' + ng_device) else: convert(inp_format, inp_loc, out_format, out_loc, ['out_node'], ng_device) conversion_successful = True finally: if not conversion_successful: try: (shutil.rmtree, os.remove)[os.path.isfile(out_loc)](out_loc) except: pass assert conversion_successful gdef = get_gdef(out_format, out_loc) (shutil.rmtree, os.remove)[os.path.isfile(out_loc)](out_loc) with tf.Graph().as_default() as g: tf.import_graph_def(gdef, name='') # The graph should have exactly one encapsulate assert len([ 0 for i in g.get_operations() if i.type == 'NGraphEncapsulate' ]) == 1 x = self.get_tensor(g, "x:0", False) y = self.get_tensor(g, "y:0", False) out = self.get_tensor(g, "out_node:0", False) sess_fn = lambda sess: sess.run( [out], feed_dict={i: np.zeros((10,)) for i in [x, y]}) res1 = self.with_ngraph(sess_fn) res2 = self.without_ngraph(sess_fn) exp = [0.5 * np.ones((10,))] # Note both run on Host (because NgraphEncapsulate can only run on host) assert np.isclose(res1, res2).all() # Comparing with expected value assert np.isclose(res1, exp).all()
def test_command_line_api(self, inp_format, inp_loc, out_node_name, save_ng_clusters, out_format, commandline, ng_device, shape_hints, precompile): # Only run this test when grappler is enabled if not ngraph_bridge.is_grappler_enabled(): return # Store and unset env variable NGRAPH_TF_BACKEND because the test # implicitly tests with different options env_var_map = self.store_env_variables(["NGRAPH_TF_BACKEND"]) self.set_env_variable("NGRAPH_TF_BACKEND", ng_device) ngraph_bridge.set_backend("INTERPRETER") assert Testtf2ngraph.format_and_loc_match(inp_format, inp_loc) out_loc = inp_loc.split('.')[0] + '_modified' + ('' if out_format == 'savedmodel' else ('.' + out_format)) try: (shutil.rmtree, os.remove)[os.path.isfile(out_loc)](out_loc) except: pass conversion_successful = False try: optional_backend_params = { 'CPU': { 'device_config': '0' }, 'INTERPRETER': { 'test_echo': '1' } }[ng_device] config_file_name = 'temp_config_file.json' Tf2ngraphJson.dump_json(config_file_name, optional_backend_params, shape_hints) if commandline: # In CI this test is expected to be run out of artifacts/test/python # out_node_str is empty if out_node_name is None. # Automatic output node inference display diagnostic logs # But the tf2ngraph call will still fail command = [ 'python', '../../tools/tf2ngraph.py', '--input_' + inp_format, inp_loc, '--output_' + out_format, out_loc, '--ng_backend', ng_device, '--config_file', config_file_name ] if out_node_name is not None: command.extend(['--output_nodes', out_node_name]) if precompile: command.append('--precompile') if save_ng_clusters: command.append('--save_ng_clusters') p = Popen(command, stdin=PIPE, stdout=PIPE, stderr=PIPE) output, err = p.communicate() rc = p.returncode if out_node_name is None: assert rc != 0, "Call to tf2ngraph should fail when no output name is provided" return else: convert(inp_format, inp_loc, out_format, out_loc, ['out_node'], ng_device, "", optional_backend_params, shape_hints, precompile, save_ng_clusters) file_present = 'ngraph_cluster_0.pbtxt' in os.listdir() assert save_ng_clusters == file_present conversion_successful = True finally: if not conversion_successful: try: (shutil.rmtree, os.remove)[os.path.isfile(out_loc)](out_loc) os.remove(config_file_name) except: pass if save_ng_clusters and 'ngraph_cluster_0.pbtxt' in os.listdir(): os.remove('ngraph_cluster_0.pbtxt') assert conversion_successful gdef = get_gdef(out_format, out_loc) (shutil.rmtree, os.remove)[os.path.isfile(out_loc)](out_loc) os.remove(config_file_name) with tf.Graph().as_default() as g: tf.import_graph_def(gdef, name='') # The graph should have exactly one encapsulate assert len([ 0 for i in g.get_operations() if i.type == 'NGraphEncapsulate' ]) == 1 # TODO: check that the encapsulate op has correct backend and extra params attached to it x = self.get_tensor(g, "x:0", False) y = self.get_tensor(g, "y:0", False) out = self.get_tensor(g, "out_node:0", False) sess_fn = lambda sess: sess.run( [out], feed_dict={i: np.zeros((10, )) for i in [x, y]}) res1 = self.with_ngraph(sess_fn) res2 = self.without_ngraph(sess_fn) exp = [0.5 * np.ones((10, ))] # Note both run on Host (because NgraphEncapsulate can only run on host) assert np.isclose(res1, res2).all() # Comparing with expected value assert np.isclose(res1, exp).all() # Restore env variable NGRAPH_TF_BACKEND that was stored self.restore_env_variables(env_var_map)
def test_command_line_api(self, inp_format, inp_loc, out_format, commandline, ng_device, shape_hints, precompile): # Only run this test when grappler is enabled if not ngraph_bridge.is_grappler_enabled(): return assert Testtf2ngraph.format_and_loc_match(inp_format, inp_loc) out_loc = inp_loc.split('.')[0] + '_modified' + ('' if out_format == 'savedmodel' else ('.' + out_format)) try: (shutil.rmtree, os.remove)[os.path.isfile(out_loc)](out_loc) except: pass conversion_successful = False try: optional_backend_params = { 'CPU': { 'device_config': '0' }, 'INTERPRETER': { 'test_echo': '1' } }[ng_device] config_file_name = 'temp_config_file.json' Tf2ngraphJson.dump_json(config_file_name, optional_backend_params, shape_hints) if commandline: # In CI this test is expected to be run out of artifacts/test/python command_executor('python ../../tools/tf2ngraph.py --input_' + inp_format + ' ' + inp_loc + ' --output_nodes out_node --output_' + out_format + ' ' + out_loc + ' --ng_backend ' + ng_device + ' --config_file ' + config_file_name + ("", " --precompile ")[precompile]) else: convert(inp_format, inp_loc, out_format, out_loc, ['out_node'], ng_device, optional_backend_params, shape_hints, precompile) conversion_successful = True finally: if not conversion_successful: try: (shutil.rmtree, os.remove)[os.path.isfile(out_loc)](out_loc) os.remove(config_file_name) except: pass assert conversion_successful gdef = get_gdef(out_format, out_loc) (shutil.rmtree, os.remove)[os.path.isfile(out_loc)](out_loc) os.remove(config_file_name) with tf.Graph().as_default() as g: tf.import_graph_def(gdef, name='') # The graph should have exactly one encapsulate assert len([ 0 for i in g.get_operations() if i.type == 'NGraphEncapsulate' ]) == 1 # TODO: check that the encapsulate op has correct backend and extra params attached to it x = self.get_tensor(g, "x:0", False) y = self.get_tensor(g, "y:0", False) out = self.get_tensor(g, "out_node:0", False) sess_fn = lambda sess: sess.run( [out], feed_dict={i: np.zeros((10, )) for i in [x, y]}) res1 = self.with_ngraph(sess_fn) res2 = self.without_ngraph(sess_fn) exp = [0.5 * np.ones((10, ))] # Note both run on Host (because NgraphEncapsulate can only run on host) assert np.isclose(res1, res2).all() # Comparing with expected value assert np.isclose(res1, exp).all()