Exemplo n.º 1
0
 def testShowCommandErrorNoTagSet(self):
   base_path = test.test_src_dir_path(SAVED_MODEL_PATH)
   self.parser = saved_model_cli.create_parser()
   args = self.parser.parse_args(
       ['show', '--dir', base_path, '--tag_set', 'badtagset'])
   with self.assertRaises(RuntimeError):
     saved_model_cli.show(args)
Exemplo n.º 2
0
    def testAOTCompileCPUFreezesAndCompiles(self, variables_to_feed, func,
                                            target_triple):
        if not test.is_built_with_xla():
            self.skipTest('Skipping test because XLA is not compiled in.')

        saved_model_dir = os.path.join(test.get_temp_dir(), 'dummy_model')
        dummy_model = self.AOTCompileDummyModel()
        func = getattr(dummy_model, func)
        with self.cached_session():
            self.evaluate(dummy_model.var.initializer)
            self.evaluate(dummy_model.write_var.initializer)
            save.save(dummy_model, saved_model_dir, signatures={'func': func})

        self.parser = saved_model_cli.create_parser()
        output_prefix = os.path.join(test.get_temp_dir(),
                                     'aot_compile_cpu_dir/out')
        args = [  # Use the default seving signature_key.
            'aot_compile_cpu', '--dir', saved_model_dir, '--tag_set', 'serve',
            '--signature_def_key', 'func', '--output_prefix', output_prefix,
            '--variables_to_feed', variables_to_feed, '--cpp_class',
            'Generated'
        ]
        if target_triple:
            args.extend(['--target_triple', target_triple])
        args = self.parser.parse_args(args)
        with test.mock.patch.object(logging, 'warn') as captured_warn:
            saved_model_cli.aot_compile_cpu(args)
        self.assertRegex(
            str(captured_warn.call_args),
            'Signature input key \'y\'.*has been pruned while freezing the graph.'
        )
        self.assertTrue(file_io.file_exists('{}.o'.format(output_prefix)))
        self.assertTrue(file_io.file_exists('{}.h'.format(output_prefix)))
        self.assertTrue(
            file_io.file_exists('{}_metadata.o'.format(output_prefix)))
        self.assertTrue(
            file_io.file_exists('{}_makefile.inc'.format(output_prefix)))
        header_contents = file_io.read_file_to_string(
            '{}.h'.format(output_prefix))
        self.assertIn('class Generated', header_contents)
        self.assertIn('arg_feed_x_data', header_contents)
        self.assertIn('result_fetch_res_data', header_contents)
        # arg_y got filtered out as it's not used by the output.
        self.assertNotIn('arg_feed_y_data', header_contents)
        if variables_to_feed:
            # Read-only-variables' setters preserve constness.
            self.assertIn('set_var_param_my_var_data(const float',
                          header_contents)
            self.assertNotIn('set_var_param_my_var_data(float',
                             header_contents)
        if func == dummy_model.func_write:
            # Writeable variables setters do not preserve constness.
            self.assertIn('set_var_param_write_var_data(float',
                          header_contents)
            self.assertNotIn('set_var_param_write_var_data(const float',
                             header_contents)

        makefile_contents = file_io.read_file_to_string(
            '{}_makefile.inc'.format(output_prefix))
        self.assertIn('-D_GLIBCXX_USE_CXX11_ABI=', makefile_contents)
Exemplo n.º 3
0
  def testRunCommandWithDebuggerEnabled(self):
    self.parser = saved_model_cli.create_parser()
    base_path = test.test_src_dir_path(SAVED_MODEL_PATH)
    x = np.array([[1], [2]])
    x_notused = np.zeros((6, 3))
    input_path = os.path.join(test.get_temp_dir(),
                              'testRunCommandNewOutdir_inputs.npz')
    output_dir = os.path.join(test.get_temp_dir(), 'new_dir')
    if os.path.isdir(output_dir):
      shutil.rmtree(output_dir)
    np.savez(input_path, x0=x, x1=x_notused)
    args = self.parser.parse_args([
        'run', '--dir', base_path, '--tag_set', 'serve', '--signature_def',
        'serving_default', '--inputs', 'x=' + input_path + '[x0]', '--outdir',
        output_dir, '--tf_debug'
    ])

    def fake_wrapper_session(sess):
      return sess

    with test.mock.patch.object(local_cli_wrapper,
                                'LocalCLIDebugWrapperSession',
                                side_effect=fake_wrapper_session,
                                autospec=True) as fake:
      saved_model_cli.run(args)
      fake.assert_called_with(test.mock.ANY)

    y_actual = np.load(os.path.join(output_dir, 'y.npy'))
    y_expected = np.array([[2.5], [3.0]])
    self.assertAllClose(y_expected, y_actual)
Exemplo n.º 4
0
  def testRunCommandWithDebuggerEnabled(self):
    self.parser = saved_model_cli.create_parser()
    base_path = test.test_src_dir_path(SAVED_MODEL_PATH)
    x = np.array([[1], [2]])
    x_notused = np.zeros((6, 3))
    input_path = os.path.join(test.get_temp_dir(),
                              'testRunCommandNewOutdir_inputs.npz')
    output_dir = os.path.join(test.get_temp_dir(), 'new_dir')
    if os.path.isdir(output_dir):
      shutil.rmtree(output_dir)
    np.savez(input_path, x0=x, x1=x_notused)
    args = self.parser.parse_args([
        'run', '--dir', base_path, '--tag_set', 'serve', '--signature_def',
        'serving_default', '--inputs', 'x=' + input_path + '[x0]', '--outdir',
        output_dir, '--tf_debug'
    ])

    def fake_wrapper_session(sess):
      return sess

    with test.mock.patch.object(
        local_cli_wrapper,
        'LocalCLIDebugWrapperSession',
        side_effect=fake_wrapper_session,
        autospec=True) as fake:
      saved_model_cli.run(args)
      fake.assert_called_with(test.mock.ANY)

    y_actual = np.load(os.path.join(output_dir, 'y.npy'))
    y_expected = np.array([[2.5], [3.0]])
    self.assertAllClose(y_expected, y_actual)
Exemplo n.º 5
0
 def testShowCommandErrorNoTagSet(self):
   base_path = test.test_src_dir_path(SAVED_MODEL_PATH)
   self.parser = saved_model_cli.create_parser()
   args = self.parser.parse_args(
       ['show', '--dir', base_path, '--tag_set', 'badtagset'])
   with self.assertRaises(RuntimeError):
     saved_model_cli.show(args)
Exemplo n.º 6
0
  def testFreezeModel(self):
    if not test.is_built_with_xla():
      self.skipTest('Skipping test because XLA is not compiled in.')

    variables_to_feed = 'all'
    func = 'func2'
    saved_model_dir = os.path.join(test.get_temp_dir(), 'dummy_model')
    dummy_model = self.AOTCompileDummyModel()
    func = getattr(dummy_model, func)
    with self.cached_session():
      self.evaluate(dummy_model.var.initializer)
      self.evaluate(dummy_model.write_var.initializer)
      save.save(dummy_model, saved_model_dir, signatures={'func': func})

    self.parser = saved_model_cli.create_parser()
    output_prefix = os.path.join(test.get_temp_dir(), 'aot_compile_cpu_dir/out')
    args = [  # Use the default seving signature_key.
        'freeze_model', '--dir', saved_model_dir, '--tag_set', 'serve',
        '--signature_def_key', 'func', '--output_prefix', output_prefix,
        '--variables_to_feed', variables_to_feed
    ]
    args = self.parser.parse_args(args)
    with test.mock.patch.object(logging, 'warn'):
      saved_model_cli.freeze_model(args)
    self.assertTrue(
        file_io.file_exists(os.path.join(output_prefix, 'frozen_graph.pb')))
    self.assertTrue(
        file_io.file_exists(os.path.join(output_prefix, 'config.pbtxt')))
Exemplo n.º 7
0
 def testScanCommand(self):
   self.parser = saved_model_cli.create_parser()
   base_path = test.test_src_dir_path(SAVED_MODEL_PATH)
   args = self.parser.parse_args(['scan', '--dir', base_path])
   with captured_output() as (out, _):
     saved_model_cli.scan(args)
   output = out.getvalue().strip()
   self.assertTrue('does not contain denylisted ops' in output)
Exemplo n.º 8
0
 def testRunCommandInputNotGivenError(self):
   self.parser = saved_model_cli.create_parser()
   base_path = test.test_src_dir_path(SAVED_MODEL_PATH)
   args = self.parser.parse_args([
       'run', '--dir', base_path, '--tag_set', 'serve', '--signature_def',
       'serving_default'
   ])
   with self.assertRaises(AttributeError):
     saved_model_cli.run(args)
Exemplo n.º 9
0
 def testRunCommandInvalidInputKeyError(self):
   self.parser = saved_model_cli.create_parser()
   base_path = test.test_src_dir_path(SAVED_MODEL_PATH)
   args = self.parser.parse_args([
       'run', '--dir', base_path, '--tag_set', 'serve', '--signature_def',
       'regress_x2_to_y3', '--input_exprs', 'x2=np.ones((3,1))'
   ])
   with self.assertRaises(ValueError):
     saved_model_cli.run(args)
Exemplo n.º 10
0
 def testRunCommandInvalidInputKeyError(self):
   self.parser = saved_model_cli.create_parser()
   base_path = test.test_src_dir_path(SAVED_MODEL_PATH)
   args = self.parser.parse_args([
       'run', '--dir', base_path, '--tag_set', 'serve', '--signature_def',
       'regress_x2_to_y3', '--input_exprs', 'x2=np.ones((3,1))'
   ])
   with self.assertRaises(ValueError):
     saved_model_cli.run(args)
Exemplo n.º 11
0
 def testRunCommandInputNotGivenError(self):
   self.parser = saved_model_cli.create_parser()
   base_path = test.test_src_dir_path(SAVED_MODEL_PATH)
   args = self.parser.parse_args([
       'run', '--dir', base_path, '--tag_set', 'serve', '--signature_def',
       'serving_default'
   ])
   with self.assertRaises(AttributeError):
     saved_model_cli.run(args)
Exemplo n.º 12
0
 def testRunCommandInvalidSignature(self):
   self.parser = saved_model_cli.create_parser()
   base_path = test.test_src_dir_path(SAVED_MODEL_PATH)
   args = self.parser.parse_args([
       'run', '--dir', base_path, '--tag_set', 'serve', '--signature_def',
       'INVALID_SIGNATURE', '--input_exprs', 'x2=np.ones((3,1))'
   ])
   with self.assertRaisesRegex(ValueError,
                               'Could not find signature "INVALID_SIGNATURE"'):
     saved_model_cli.run(args)
Exemplo n.º 13
0
 def testShowCommandTags(self):
   base_path = test.test_src_dir_path(SAVED_MODEL_PATH)
   self.parser = saved_model_cli.create_parser()
   args = self.parser.parse_args(['show', '--dir', base_path])
   with captured_output() as (out, err):
     saved_model_cli.show(args)
   output = out.getvalue().strip()
   exp_out = 'The given SavedModel contains the following tag-sets:\nserve'
   self.assertMultiLineEqual(output, exp_out)
   self.assertEqual(err.getvalue().strip(), '')
Exemplo n.º 14
0
 def testShowCommandTags(self):
   base_path = test.test_src_dir_path(SAVED_MODEL_PATH)
   self.parser = saved_model_cli.create_parser()
   args = self.parser.parse_args(['show', '--dir', base_path])
   with captured_output() as (out, err):
     saved_model_cli.show(args)
   output = out.getvalue().strip()
   exp_out = 'The given SavedModel contains the following tag-sets:\n\'serve\''
   self.assertMultiLineEqual(output, exp_out)
   self.assertEqual(err.getvalue().strip(), '')
Exemplo n.º 15
0
 def testRunCommandInputExamplesFeatureBadType(self):
   self.parser = saved_model_cli.create_parser()
   base_path = test.test_src_dir_path(SAVED_MODEL_PATH)
   output_dir = os.path.join(test.get_temp_dir(), 'new_dir')
   args = self.parser.parse_args([
       'run', '--dir', base_path, '--tag_set', 'serve', '--signature_def',
       'regress_x_to_y', '--input_examples', 'inputs=[{"x":[[1],[2]]}]',
       '--outdir', output_dir
   ])
   with self.assertRaisesRegexp(ValueError, 'is not supported'):
     saved_model_cli.run(args)
Exemplo n.º 16
0
 def testRunCommandInputExamplesFeatureValueNotListError(self):
   self.parser = saved_model_cli.create_parser()
   base_path = test.test_src_dir_path(SAVED_MODEL_PATH)
   output_dir = os.path.join(test.get_temp_dir(), 'new_dir')
   args = self.parser.parse_args([
       'run', '--dir', base_path, '--tag_set', 'serve', '--signature_def',
       'regress_x_to_y', '--input_examples', 'inputs=[{"x":8.0,"x2":5.0}]',
       '--outdir', output_dir
   ])
   with self.assertRaisesRegex(ValueError, 'feature value must be a list'):
     saved_model_cli.run(args)
Exemplo n.º 17
0
 def testRunCommandInputExamplesFeatureValueNotListError(self):
   self.parser = saved_model_cli.create_parser()
   base_path = test.test_src_dir_path(SAVED_MODEL_PATH)
   output_dir = os.path.join(test.get_temp_dir(), 'new_dir')
   args = self.parser.parse_args([
       'run', '--dir', base_path, '--tag_set', 'serve', '--signature_def',
       'regress_x_to_y', '--input_examples', 'inputs=[{"x":8.0,"x2":5.0}]',
       '--outdir', output_dir
   ])
   with self.assertRaisesRegexp(ValueError, 'feature value must be a list'):
     saved_model_cli.run(args)
Exemplo n.º 18
0
 def testRunCommandInputExamplesFeatureBadType(self):
   self.parser = saved_model_cli.create_parser()
   base_path = test.test_src_dir_path(SAVED_MODEL_PATH)
   output_dir = os.path.join(test.get_temp_dir(), 'new_dir')
   args = self.parser.parse_args([
       'run', '--dir', base_path, '--tag_set', 'serve', '--signature_def',
       'regress_x_to_y', '--input_examples', 'inputs=[{"x":[[1],[2]]}]',
       '--outdir', output_dir
   ])
   with self.assertRaisesRegex(ValueError, 'is not supported'):
     saved_model_cli.run(args)
Exemplo n.º 19
0
 def testScanCommandFoundDenylistedOp(self):
   self.parser = saved_model_cli.create_parser()
   base_path = test.test_src_dir_path(SAVED_MODEL_PATH)
   args = self.parser.parse_args(
       ['scan', '--dir', base_path, '--tag_set', 'serve'])
   op_denylist = saved_model_cli._OP_DENYLIST
   saved_model_cli._OP_DENYLIST = set(['VariableV2'])
   with captured_output() as (out, _):
     saved_model_cli.scan(args)
   saved_model_cli._OP_DENYLIST = op_denylist
   output = out.getvalue().strip()
   self.assertTrue('\'VariableV2\'' in output)
Exemplo n.º 20
0
 def testRunCommandInputExamples(self):
   self.parser = saved_model_cli.create_parser()
   base_path = test.test_src_dir_path(SAVED_MODEL_PATH)
   output_dir = os.path.join(test.get_temp_dir(), 'new_dir')
   args = self.parser.parse_args([
       'run', '--dir', base_path, '--tag_set', 'serve', '--signature_def',
       'regress_x_to_y', '--input_examples',
       'inputs=[{"x":[8.0],"x2":[5.0]}, {"x":[4.0],"x2":[3.0]}]', '--outdir',
       output_dir
   ])
   saved_model_cli.run(args)
   y_actual = np.load(os.path.join(output_dir, 'outputs.npy'))
   y_expected = np.array([[6.0], [4.0]])
   self.assertAllEqual(y_expected, y_actual)
Exemplo n.º 21
0
 def testRunCommandInputExamples(self):
   self.parser = saved_model_cli.create_parser()
   base_path = test.test_src_dir_path(SAVED_MODEL_PATH)
   output_dir = os.path.join(test.get_temp_dir(), 'new_dir')
   args = self.parser.parse_args([
       'run', '--dir', base_path, '--tag_set', 'serve', '--signature_def',
       'regress_x_to_y', '--input_examples',
       'inputs=[{"x":[8.0],"x2":[5.0]}, {"x":[4.0],"x2":[3.0]}]', '--outdir',
       output_dir
   ])
   saved_model_cli.run(args)
   y_actual = np.load(os.path.join(output_dir, 'outputs.npy'))
   y_expected = np.array([[6.0], [4.0]])
   self.assertAllEqual(y_expected, y_actual)
Exemplo n.º 22
0
  def testAOTCompileCPUWrongSignatureDefKey(self):
    if not test.is_built_with_xla():
      self.skipTest('Skipping test because XLA is not compiled in.')

    self.parser = saved_model_cli.create_parser()
    base_path = test.test_src_dir_path(SAVED_MODEL_PATH)
    output_dir = os.path.join(test.get_temp_dir(), 'aot_compile_cpu_dir')
    args = self.parser.parse_args([
        'aot_compile_cpu', '--dir', base_path, '--tag_set', 'serve',
        '--output_prefix', output_dir, '--cpp_class', 'Compiled',
        '--signature_def_key', 'MISSING'
    ])
    with self.assertRaisesRegex(ValueError, 'Unable to find signature_def'):
      saved_model_cli.aot_compile_cpu(args)
Exemplo n.º 23
0
 def testRunCommandOutputFileExistError(self):
   self.parser = saved_model_cli.create_parser()
   base_path = test.test_src_dir_path(SAVED_MODEL_PATH)
   x = np.array([[1], [2]])
   x_notused = np.zeros((6, 3))
   input_path = os.path.join(test.get_temp_dir(),
                             'testRunCommandOutOverwrite_inputs.npz')
   np.savez(input_path, x0=x, x1=x_notused)
   output_file = os.path.join(test.get_temp_dir(), 'y.npy')
   open(output_file, 'a').close()
   args = self.parser.parse_args([
       'run', '--dir', base_path, '--tag_set', 'serve', '--signature_def',
       'serving_default', '--inputs', 'x=' + input_path + '[x0]', '--outdir',
       test.get_temp_dir()
   ])
   with self.assertRaises(RuntimeError):
     saved_model_cli.run(args)
Exemplo n.º 24
0
 def testRunCommandOutputFileExistError(self):
   self.parser = saved_model_cli.create_parser()
   base_path = test.test_src_dir_path(SAVED_MODEL_PATH)
   x = np.array([[1], [2]])
   x_notused = np.zeros((6, 3))
   input_path = os.path.join(test.get_temp_dir(),
                             'testRunCommandOutOverwrite_inputs.npz')
   np.savez(input_path, x0=x, x1=x_notused)
   output_file = os.path.join(test.get_temp_dir(), 'y.npy')
   open(output_file, 'a').close()
   args = self.parser.parse_args([
       'run', '--dir', base_path, '--tag_set', 'serve', '--signature_def',
       'serving_default', '--inputs', 'x=' + input_path + '[x0]', '--outdir',
       test.get_temp_dir()
   ])
   with self.assertRaises(RuntimeError):
     saved_model_cli.run(args)
Exemplo n.º 25
0
 def testRunCommandOutOverwrite(self):
   self.parser = saved_model_cli.create_parser()
   base_path = test.test_src_dir_path(SAVED_MODEL_PATH)
   x = np.array([[1], [2]])
   x_notused = np.zeros((6, 3))
   input_path = os.path.join(test.get_temp_dir(),
                             'testRunCommandOutOverwrite_inputs.npz')
   np.savez(input_path, x0=x, x1=x_notused)
   output_file = os.path.join(test.get_temp_dir(), 'y.npy')
   open(output_file, 'a').close()
   args = self.parser.parse_args([
       'run', '--dir', base_path, '--tag_set', 'serve', '--signature_def',
       'serving_default', '--inputs', 'x=' + input_path + '[x0]', '--outdir',
       test.get_temp_dir(), '--overwrite'
   ])
   saved_model_cli.run(args)
   y_actual = np.load(output_file)
   y_expected = np.array([[2.5], [3.0]])
   self.assertAllClose(y_expected, y_actual)
Exemplo n.º 26
0
    def testAOTCompileCPUFreezesAndCompiles(self):
        if not test.is_built_with_xla():
            self.skipTest('Skipping test because XLA is not compiled in.')

        class DummyModel(tracking.AutoTrackable):
            """Model compatible with XLA compilation."""
            def __init__(self):
                self.var = variables.Variable(1.0, name='my_var')

            @def_function.function(input_signature=[
                tensor_spec.TensorSpec(shape=(2, 2), dtype=dtypes.float32)
            ])
            def func2(self, x):
                return {'res': x + self.var}

        saved_model_dir = os.path.join(test.get_temp_dir(), 'dummy_model')
        dummy_model = DummyModel()
        with self.cached_session():
            self.evaluate(dummy_model.var.initializer)
            save.save(dummy_model, saved_model_dir)

        self.parser = saved_model_cli.create_parser()
        output_prefix = os.path.join(test.get_temp_dir(),
                                     'aot_compile_cpu_dir/out')
        args = self.parser.parse_args([
            'aot_compile_cpu', '--dir', saved_model_dir, '--tag_set', 'serve',
            '--output_prefix', output_prefix, '--cpp_class', 'Generated'
        ])  # Use the default seving signature_key.
        saved_model_cli.aot_compile_cpu(args)
        self.assertTrue(file_io.file_exists('{}.o'.format(output_prefix)))
        self.assertTrue(file_io.file_exists('{}.h'.format(output_prefix)))
        self.assertTrue(
            file_io.file_exists('{}_metadata.o'.format(output_prefix)))
        self.assertTrue(
            file_io.file_exists('{}_makefile.inc'.format(output_prefix)))
        header_contents = file_io.read_file_to_string(
            '{}.h'.format(output_prefix))
        self.assertIn('class Generated', header_contents)
        self.assertIn('arg_x_data', header_contents)
        self.assertIn('result_res_data', header_contents)
        makefile_contents = file_io.read_file_to_string(
            '{}_makefile.inc'.format(output_prefix))
        self.assertIn('-D_GLIBCXX_USE_CXX11_ABI=', makefile_contents)
Exemplo n.º 27
0
 def testRunCommandOutOverwrite(self):
   self.parser = saved_model_cli.create_parser()
   base_path = test.test_src_dir_path(SAVED_MODEL_PATH)
   x = np.array([[1], [2]])
   x_notused = np.zeros((6, 3))
   input_path = os.path.join(test.get_temp_dir(),
                             'testRunCommandOutOverwrite_inputs.npz')
   np.savez(input_path, x0=x, x1=x_notused)
   output_file = os.path.join(test.get_temp_dir(), 'y.npy')
   open(output_file, 'a').close()
   args = self.parser.parse_args([
       'run', '--dir', base_path, '--tag_set', 'serve', '--signature_def',
       'serving_default', '--inputs', 'x=' + input_path + '[x0]', '--outdir',
       test.get_temp_dir(), '--overwrite'
   ])
   saved_model_cli.run(args)
   y_actual = np.load(output_file)
   y_expected = np.array([[2.5], [3.0]])
   self.assertAllClose(y_expected, y_actual)
Exemplo n.º 28
0
 def testShowCommandSignature(self):
   base_path = test.test_src_dir_path(SAVED_MODEL_PATH)
   self.parser = saved_model_cli.create_parser()
   args = self.parser.parse_args(
       ['show', '--dir', base_path, '--tag_set', 'serve'])
   with captured_output() as (out, err):
     saved_model_cli.show(args)
   output = out.getvalue().strip()
   exp_header = ('The given SavedModel MetaGraphDef contains SignatureDefs '
                 'with the following keys:')
   exp_start = 'SignatureDef key: '
   exp_keys = [
       '"classify_x2_to_y3"', '"classify_x_to_y"', '"regress_x2_to_y3"',
       '"regress_x_to_y"', '"regress_x_to_y2"', '"serving_default"'
   ]
   # Order of signatures does not matter
   self.assertMultiLineEqual(
       output,
       '\n'.join([exp_header] + [exp_start + exp_key for exp_key in exp_keys]))
   self.assertEqual(err.getvalue().strip(), '')
Exemplo n.º 29
0
 def testShowCommandInputsOutputs(self):
   base_path = test.test_src_dir_path(SAVED_MODEL_PATH)
   self.parser = saved_model_cli.create_parser()
   args = self.parser.parse_args([
       'show', '--dir', base_path, '--tag_set', 'serve', '--signature_def',
       'serving_default'
   ])
   with captured_output() as (out, err):
     saved_model_cli.show(args)
   output = out.getvalue().strip()
   expected_output = (
       'The given SavedModel SignatureDef contains the following input(s):\n'
       '  inputs[\'x\'] tensor_info:\n'
       '      dtype: DT_FLOAT\n      shape: (-1, 1)\n      name: x:0\n'
       'The given SavedModel SignatureDef contains the following output(s):\n'
       '  outputs[\'y\'] tensor_info:\n'
       '      dtype: DT_FLOAT\n      shape: (-1, 1)\n      name: y:0\n'
       'Method name is: tensorflow/serving/predict')
   self.assertEqual(output, expected_output)
   self.assertEqual(err.getvalue().strip(), '')
Exemplo n.º 30
0
 def testRunCommandExistingOutdir(self):
   self.parser = saved_model_cli.create_parser()
   base_path = test.test_src_dir_path(SAVED_MODEL_PATH)
   x = np.array([[1], [2]])
   x_notused = np.zeros((6, 3))
   input_path = os.path.join(test.get_temp_dir(), 'testRunCommand_inputs.npz')
   np.savez(input_path, x0=x, x1=x_notused)
   output_file = os.path.join(test.get_temp_dir(), 'outputs.npy')
   if os.path.exists(output_file):
     os.remove(output_file)
   args = self.parser.parse_args([
       'run', '--dir', base_path, '--tag_set', 'serve', '--signature_def',
       'regress_x2_to_y3', '--inputs', 'inputs=' + input_path + '[x0]',
       '--outdir',
       test.get_temp_dir()
   ])
   saved_model_cli.run(args)
   y_actual = np.load(output_file)
   y_expected = np.array([[3.5], [4.0]])
   self.assertAllClose(y_expected, y_actual)
Exemplo n.º 31
0
 def testRunCommandNewOutdir(self):
   self.parser = saved_model_cli.create_parser()
   base_path = test.test_src_dir_path(SAVED_MODEL_PATH)
   x = np.array([[1], [2]])
   x_notused = np.zeros((6, 3))
   input_path = os.path.join(test.get_temp_dir(),
                             'testRunCommandNewOutdir_inputs.npz')
   output_dir = os.path.join(test.get_temp_dir(), 'new_dir')
   if os.path.isdir(output_dir):
     shutil.rmtree(output_dir)
   np.savez(input_path, x0=x, x1=x_notused)
   args = self.parser.parse_args([
       'run', '--dir', base_path, '--tag_set', 'serve', '--signature_def',
       'serving_default', '--inputs', 'x=' + input_path + '[x0]', '--outdir',
       output_dir
   ])
   saved_model_cli.run(args)
   y_actual = np.load(os.path.join(output_dir, 'y.npy'))
   y_expected = np.array([[2.5], [3.0]])
   self.assertAllClose(y_expected, y_actual)
Exemplo n.º 32
0
 def testShowCommandSignature(self):
   base_path = test.test_src_dir_path(SAVED_MODEL_PATH)
   self.parser = saved_model_cli.create_parser()
   args = self.parser.parse_args(
       ['show', '--dir', base_path, '--tag_set', 'serve'])
   with captured_output() as (out, err):
     saved_model_cli.show(args)
   output = out.getvalue().strip()
   exp_header = ('The given SavedModel MetaGraphDef contains SignatureDefs '
                 'with the following keys:')
   exp_start = 'SignatureDef key: '
   exp_keys = [
       '"classify_x2_to_y3"', '"classify_x_to_y"', '"regress_x2_to_y3"',
       '"regress_x_to_y"', '"regress_x_to_y2"', '"serving_default"'
   ]
   # Order of signatures does not matter
   self.assertMultiLineEqual(
       output,
       '\n'.join([exp_header] + [exp_start + exp_key for exp_key in exp_keys]))
   self.assertEqual(err.getvalue().strip(), '')
Exemplo n.º 33
0
 def testShowCommandInputsOutputs(self):
   base_path = test.test_src_dir_path(SAVED_MODEL_PATH)
   self.parser = saved_model_cli.create_parser()
   args = self.parser.parse_args([
       'show', '--dir', base_path, '--tag_set', 'serve', '--signature_def',
       'serving_default'
   ])
   with captured_output() as (out, err):
     saved_model_cli.show(args)
   output = out.getvalue().strip()
   expected_output = (
       'The given SavedModel SignatureDef contains the following input(s):\n'
       '  inputs[\'x\'] tensor_info:\n'
       '      dtype: DT_FLOAT\n      shape: (-1, 1)\n      name: x:0\n'
       'The given SavedModel SignatureDef contains the following output(s):\n'
       '  outputs[\'y\'] tensor_info:\n'
       '      dtype: DT_FLOAT\n      shape: (-1, 1)\n      name: y:0\n'
       'Method name is: tensorflow/serving/predict')
   self.assertEqual(output, expected_output)
   self.assertEqual(err.getvalue().strip(), '')
Exemplo n.º 34
0
 def testRunCommandExistingOutdir(self):
   self.parser = saved_model_cli.create_parser()
   base_path = test.test_src_dir_path(SAVED_MODEL_PATH)
   x = np.array([[1], [2]])
   x_notused = np.zeros((6, 3))
   input_path = os.path.join(test.get_temp_dir(), 'testRunCommand_inputs.npz')
   np.savez(input_path, x0=x, x1=x_notused)
   output_file = os.path.join(test.get_temp_dir(), 'outputs.npy')
   if os.path.exists(output_file):
     os.remove(output_file)
   args = self.parser.parse_args([
       'run', '--dir', base_path, '--tag_set', 'serve', '--signature_def',
       'regress_x2_to_y3', '--inputs', 'inputs=' + input_path + '[x0]',
       '--outdir',
       test.get_temp_dir()
   ])
   saved_model_cli.run(args)
   y_actual = np.load(output_file)
   y_expected = np.array([[3.5], [4.0]])
   self.assertAllClose(y_expected, y_actual)
Exemplo n.º 35
0
 def testRunCommandNewOutdir(self):
   self.parser = saved_model_cli.create_parser()
   base_path = test.test_src_dir_path(SAVED_MODEL_PATH)
   x = np.array([[1], [2]])
   x_notused = np.zeros((6, 3))
   input_path = os.path.join(test.get_temp_dir(),
                             'testRunCommandNewOutdir_inputs.npz')
   output_dir = os.path.join(test.get_temp_dir(), 'new_dir')
   if os.path.isdir(output_dir):
     shutil.rmtree(output_dir)
   np.savez(input_path, x0=x, x1=x_notused)
   args = self.parser.parse_args([
       'run', '--dir', base_path, '--tag_set', 'serve', '--signature_def',
       'serving_default', '--inputs', 'x=' + input_path + '[x0]', '--outdir',
       output_dir
   ])
   saved_model_cli.run(args)
   y_actual = np.load(os.path.join(output_dir, 'y.npy'))
   y_expected = np.array([[2.5], [3.0]])
   self.assertAllClose(y_expected, y_actual)
Exemplo n.º 36
0
  def testShowAllWithPureConcreteFunction(self):

    class DummyModel(tracking.AutoTrackable):
      """Model with a callable concrete function."""

      def __init__(self):
        function = def_function.function(
            self.multiply,
            input_signature=[
                tensor_spec.TensorSpec(shape=(), dtype=dtypes.float32),
                tensor_spec.TensorSpec(shape=(), dtype=dtypes.float32)
            ])
        self.pure_concrete_function = function.get_concrete_function()
        super(DummyModel, self).__init__()

      def multiply(self, a, b):
        return a * b

    saved_model_dir = os.path.join(test.get_temp_dir(), 'dummy_model')
    dummy_model = DummyModel()
    save.save(dummy_model, saved_model_dir)
    self.parser = saved_model_cli.create_parser()
    args = self.parser.parse_args(['show', '--dir', saved_model_dir, '--all'])
    with captured_output() as (out, err):
      saved_model_cli.show(args)
    output = out.getvalue().strip()
    exp_out = """MetaGraphDef with tag-set: 'serve' contains the following SignatureDefs:

signature_def['__saved_model_init_op']:
  The given SavedModel SignatureDef contains the following input(s):
  The given SavedModel SignatureDef contains the following output(s):
    outputs['__saved_model_init_op'] tensor_info:
        dtype: DT_INVALID
        shape: unknown_rank
        name: NoOp
  Method name is: 

signature_def['serving_default']:
  The given SavedModel SignatureDef contains the following input(s):
    inputs['a'] tensor_info:
        dtype: DT_FLOAT
        shape: ()
        name: serving_default_a:0
    inputs['b'] tensor_info:
        dtype: DT_FLOAT
        shape: ()
        name: serving_default_b:0
  The given SavedModel SignatureDef contains the following output(s):
    outputs['output_0'] tensor_info:
        dtype: DT_FLOAT
        shape: ()
        name: PartitionedCall:0
  Method name is: tensorflow/serving/predict

Defined Functions:
  Function Name: 'pure_concrete_function'
    Option #1
      Callable with:
        Argument #1
          a: TensorSpec(shape=(), dtype=tf.float32, name='a')
        Argument #2
          b: TensorSpec(shape=(), dtype=tf.float32, name='b')
""".strip()  # pylint: enable=line-too-long
    self.maxDiff = None  # Produce a useful error msg if the comparison fails
    self.assertMultiLineEqual(output, exp_out)
    self.assertEqual(err.getvalue().strip(), '')
Exemplo n.º 37
0
  def testShowCommandAll(self):
    base_path = test.test_src_dir_path(SAVED_MODEL_PATH)
    self.parser = saved_model_cli.create_parser()
    args = self.parser.parse_args(['show', '--dir', base_path, '--all'])
    with captured_output() as (out, err):
      saved_model_cli.show(args)
    output = out.getvalue().strip()
    # pylint: disable=line-too-long
    exp_out = """MetaGraphDef with tag-set: 'serve' contains the following SignatureDefs:

signature_def['classify_x2_to_y3']:
  The given SavedModel SignatureDef contains the following input(s):
    inputs['inputs'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 1)
        name: x2:0
  The given SavedModel SignatureDef contains the following output(s):
    outputs['scores'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 1)
        name: y3:0
  Method name is: tensorflow/serving/classify

signature_def['classify_x_to_y']:
  The given SavedModel SignatureDef contains the following input(s):
    inputs['inputs'] tensor_info:
        dtype: DT_STRING
        shape: unknown_rank
        name: tf_example:0
  The given SavedModel SignatureDef contains the following output(s):
    outputs['scores'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 1)
        name: y:0
  Method name is: tensorflow/serving/classify

signature_def['regress_x2_to_y3']:
  The given SavedModel SignatureDef contains the following input(s):
    inputs['inputs'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 1)
        name: x2:0
  The given SavedModel SignatureDef contains the following output(s):
    outputs['outputs'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 1)
        name: y3:0
  Method name is: tensorflow/serving/regress

signature_def['regress_x_to_y']:
  The given SavedModel SignatureDef contains the following input(s):
    inputs['inputs'] tensor_info:
        dtype: DT_STRING
        shape: unknown_rank
        name: tf_example:0
  The given SavedModel SignatureDef contains the following output(s):
    outputs['outputs'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 1)
        name: y:0
  Method name is: tensorflow/serving/regress

signature_def['regress_x_to_y2']:
  The given SavedModel SignatureDef contains the following input(s):
    inputs['inputs'] tensor_info:
        dtype: DT_STRING
        shape: unknown_rank
        name: tf_example:0
  The given SavedModel SignatureDef contains the following output(s):
    outputs['outputs'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 1)
        name: y2:0
  Method name is: tensorflow/serving/regress

signature_def['serving_default']:
  The given SavedModel SignatureDef contains the following input(s):
    inputs['x'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 1)
        name: x:0
  The given SavedModel SignatureDef contains the following output(s):
    outputs['y'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 1)
        name: y:0
  Method name is: tensorflow/serving/predict"""
    # pylint: enable=line-too-long
    self.maxDiff = None # Produce a useful error msg if the comparison fails
    self.assertMultiLineEqual(output, exp_out)
    self.assertEqual(err.getvalue().strip(), '')
Exemplo n.º 38
0
  def testShowCommandAll(self):
    base_path = test.test_src_dir_path(SAVED_MODEL_PATH)
    self.parser = saved_model_cli.create_parser()
    args = self.parser.parse_args(['show', '--dir', base_path, '--all'])
    with captured_output() as (out, err):
      saved_model_cli.show(args)
    output = out.getvalue().strip()
    # pylint: disable=line-too-long
    exp_out = """MetaGraphDef with tag-set: 'serve' contains the following SignatureDefs:

signature_def['classify_x2_to_y3']:
  The given SavedModel SignatureDef contains the following input(s):
    inputs['inputs'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 1)
        name: x2:0
  The given SavedModel SignatureDef contains the following output(s):
    outputs['scores'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 1)
        name: y3:0
  Method name is: tensorflow/serving/classify

signature_def['classify_x_to_y']:
  The given SavedModel SignatureDef contains the following input(s):
    inputs['inputs'] tensor_info:
        dtype: DT_STRING
        shape: unknown_rank
        name: tf_example:0
  The given SavedModel SignatureDef contains the following output(s):
    outputs['scores'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 1)
        name: y:0
  Method name is: tensorflow/serving/classify

signature_def['regress_x2_to_y3']:
  The given SavedModel SignatureDef contains the following input(s):
    inputs['inputs'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 1)
        name: x2:0
  The given SavedModel SignatureDef contains the following output(s):
    outputs['outputs'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 1)
        name: y3:0
  Method name is: tensorflow/serving/regress

signature_def['regress_x_to_y']:
  The given SavedModel SignatureDef contains the following input(s):
    inputs['inputs'] tensor_info:
        dtype: DT_STRING
        shape: unknown_rank
        name: tf_example:0
  The given SavedModel SignatureDef contains the following output(s):
    outputs['outputs'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 1)
        name: y:0
  Method name is: tensorflow/serving/regress

signature_def['regress_x_to_y2']:
  The given SavedModel SignatureDef contains the following input(s):
    inputs['inputs'] tensor_info:
        dtype: DT_STRING
        shape: unknown_rank
        name: tf_example:0
  The given SavedModel SignatureDef contains the following output(s):
    outputs['outputs'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 1)
        name: y2:0
  Method name is: tensorflow/serving/regress

signature_def['serving_default']:
  The given SavedModel SignatureDef contains the following input(s):
    inputs['x'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 1)
        name: x:0
  The given SavedModel SignatureDef contains the following output(s):
    outputs['y'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 1)
        name: y:0
  Method name is: tensorflow/serving/predict"""
    # pylint: enable=line-too-long
    self.maxDiff = None  # Produce a useful error msg if the comparison fails
    self.assertMultiLineEqual(output, exp_out)
    self.assertEqual(err.getvalue().strip(), '')
Exemplo n.º 39
0
  def testShowAllWithFunctions(self):

    class DummyModel(tracking.AutoTrackable):
      """Model with callable polymorphic functions specified."""

      @def_function.function
      def func1(self, a, b, c):
        if c:
          return a + b
        else:
          return a * b

      @def_function.function(input_signature=[
          tensor_spec.TensorSpec(shape=(2, 2), dtype=dtypes.float32)
      ])
      def func2(self, x):
        return x + 2

      @def_function.function
      def __call__(self, y, c=7):
        return y + 2 * c

    saved_model_dir = os.path.join(test.get_temp_dir(), 'dummy_model')
    dummy_model = DummyModel()
    # Call with specific values to create new polymorphic function traces.
    dummy_model.func1(constant_op.constant(5), constant_op.constant(9), True)
    dummy_model(constant_op.constant(5))
    save.save(dummy_model, saved_model_dir)
    self.parser = saved_model_cli.create_parser()
    args = self.parser.parse_args(['show', '--dir', saved_model_dir, '--all'])
    with captured_output() as (out, err):
      saved_model_cli.show(args)
    output = out.getvalue().strip()
    exp_out = """MetaGraphDef with tag-set: 'serve' contains the following SignatureDefs:

signature_def['__saved_model_init_op']:
  The given SavedModel SignatureDef contains the following input(s):
  The given SavedModel SignatureDef contains the following output(s):
    outputs['__saved_model_init_op'] tensor_info:
        dtype: DT_INVALID
        shape: unknown_rank
        name: NoOp
  Method name is: 

signature_def['serving_default']:
  The given SavedModel SignatureDef contains the following input(s):
    inputs['x'] tensor_info:
        dtype: DT_FLOAT
        shape: (2, 2)
        name: serving_default_x:0
  The given SavedModel SignatureDef contains the following output(s):
    outputs['output_0'] tensor_info:
        dtype: DT_FLOAT
        shape: (2, 2)
        name: PartitionedCall:0
  Method name is: tensorflow/serving/predict

Defined Functions:
  Function Name: '__call__'
    Option #1
      Callable with:
        Argument #1
          y: TensorSpec(shape=(), dtype=tf.int32, name='y')
        Argument #2
          DType: int
          Value: 7

  Function Name: 'func1'
    Option #1
      Callable with:
        Argument #1
          a: TensorSpec(shape=(), dtype=tf.int32, name='a')
        Argument #2
          b: TensorSpec(shape=(), dtype=tf.int32, name='b')
        Argument #3
          DType: bool
          Value: True

  Function Name: 'func2'
    Option #1
      Callable with:
        Argument #1
          x: TensorSpec(shape=(2, 2), dtype=tf.float32, name='x')
""".strip()  # pylint: enable=line-too-long
    self.maxDiff = None  # Produce a useful error msg if the comparison fails
    self.assertMultiLineEqual(output, exp_out)
    self.assertEqual(err.getvalue().strip(), '')