def testAdvisor(self):
        ops.reset_default_graph()

        with session.Session() as sess:
            x = lib.BuildFullModel()

            sess.run(variables.global_variables_initializer())
            run_meta = config_pb2.RunMetadata()
            _ = sess.run(x,
                         options=config_pb2.RunOptions(
                             trace_level=config_pb2.RunOptions.FULL_TRACE),
                         run_metadata=run_meta)

            advice_pb = model_analyzer.advise(sess.graph, run_meta)
            self.assertTrue(
                'AcceleratorUtilizationChecker' in advice_pb.checkers)
            self.assertTrue('ExpensiveOperationChecker' in advice_pb.checkers)
            self.assertTrue('OperationChecker' in advice_pb.checkers)

            checker = advice_pb.checkers['AcceleratorUtilizationChecker']
            if test.is_gpu_available():
                self.assertGreater(len(checker.reports), 0)
            else:
                self.assertEqual(len(checker.reports), 0)
            checker = advice_pb.checkers['ExpensiveOperationChecker']
            self.assertGreater(len(checker.reports), 0)
Esempio n. 2
0
    def testBasics(self):
        ops.reset_default_graph()
        outfile = os.path.join(test.get_temp_dir(), "dump")
        opts = builder(
            builder.time_and_memory()).with_file_output(outfile).build()

        x = lib.BuildFullModel()

        profile_str = None
        profile_step50 = os.path.join(test.get_temp_dir(), "profile_50")
        with profile_context.ProfileContext(test.get_temp_dir()) as pctx:
            pctx.add_auto_profiling("op",
                                    options=opts,
                                    profile_steps=[15, 50, 100])
            with session.Session() as sess:
                sess.run(variables.global_variables_initializer())
                total_steps = 101 if test.is_gpu_available() else 50
                for i in range(total_steps):
                    sess.run(x)
                    if i == 14 or i == 99:
                        self.assertTrue(gfile.Exists(outfile))
                        gfile.Remove(outfile)
                    if i == 49:
                        self.assertTrue(gfile.Exists(profile_step50))
                        with gfile.Open(outfile, "r") as f:
                            profile_str = f.read()
                        gfile.Remove(outfile)

        with lib.ProfilerFromFile(
                os.path.join(test.get_temp_dir(), "profile_50")) as profiler:
            profiler.profile_operations(options=opts)
            with gfile.Open(outfile, "r") as f:
                self.assertEqual(profile_str, f.read())
    def testTimeline(self):
        ops.reset_default_graph()
        outfile = os.path.join(test.get_temp_dir(), 'timeline')
        opts = (
            builder(
                builder.trainable_variables_parameter()).with_max_depth(100000)
            .with_step(0).with_timeline_output(outfile).with_accounted_types(
                ['.*']).build())

        with session.Session() as sess:
            x = lib.BuildFullModel()

            sess.run(variables.global_variables_initializer())
            run_meta = config_pb2.RunMetadata()
            _ = sess.run(x,
                         options=config_pb2.RunOptions(
                             trace_level=config_pb2.RunOptions.FULL_TRACE),
                         run_metadata=run_meta)

            _ = model_analyzer.profile(sess.graph,
                                       run_meta,
                                       cmd='graph',
                                       options=opts)

            with gfile.Open(outfile + '_0', 'r') as f:
                # Test that a json file is created.
                # TODO(xpan): tfprof Timeline isn't quite correct on Windows.
                # Investigate why.
                if os.name != 'nt':
                    self.assertLess(1000, len(f.read()))
                else:
                    self.assertLess(1, len(f.read()))
  def testComplexCodeView(self):
    ops.reset_default_graph()
    outfile = os.path.join(test.get_temp_dir(), 'dump')
    opts = (builder(builder.trainable_variables_parameter())
            .with_file_output(outfile)
            .with_accounted_types(['.*'])
            .with_node_names(show_name_regexes=
                             ['.*model_analyzer_testlib.py.*'])
            .account_displayed_op_only(False)
            .select(['params', 'float_ops']).build())

    with profile_context.ProfileContext(test.get_temp_dir(),
                                        trace_steps=[],
                                        dump_steps=[]) as pctx:
      with session.Session() as sess:
        x = lib.BuildFullModel()

        sess.run(variables.global_variables_initializer())
        pctx.trace_next_step()
        _ = sess.run(x)
        tfprof_node = pctx.profiler.profile_python(options=opts)

        # pylint: disable=line-too-long
        with gfile.Open(outfile, 'r') as f:
          lines = f.read().split('\n')
          self.assertGreater(len(lines), 5)
          result = '\n'.join([l[:min(len(l), 80)] for l in lines])
          self.assertTrue(
              compat.as_text(lib.CheckAndRemoveDoc(result))
              .startswith('node name | # parameters | # float_ops'))

        self.assertLess(0, tfprof_node.total_exec_micros)
        self.assertEqual(2844, tfprof_node.total_parameters)
        self.assertLess(168800, tfprof_node.total_float_ops)
        self.assertEqual(8, len(tfprof_node.children))
        self.assertEqual('_TFProfRoot', tfprof_node.name)
        self.assertEqual(
            'model_analyzer_testlib.py:63:BuildFullModel',
            tfprof_node.children[0].name)
        self.assertEqual(
            'model_analyzer_testlib.py:63:BuildFullModel (gradient)',
            tfprof_node.children[1].name)
        self.assertEqual(
            'model_analyzer_testlib.py:67:BuildFullModel',
            tfprof_node.children[2].name)
        self.assertEqual(
            'model_analyzer_testlib.py:67:BuildFullModel (gradient)',
            tfprof_node.children[3].name)
        self.assertEqual(
            'model_analyzer_testlib.py:69:BuildFullModel',
            tfprof_node.children[4].name)
        self.assertEqual(
            'model_analyzer_testlib.py:70:BuildFullModel',
            tfprof_node.children[5].name)
        self.assertEqual(
            'model_analyzer_testlib.py:70:BuildFullModel (gradient)',
            tfprof_node.children[6].name)
        self.assertEqual(
            'model_analyzer_testlib.py:72:BuildFullModel',
            tfprof_node.children[7].name)
    def testTimeline(self):
        ops.reset_default_graph()
        opts = model_analyzer.TRAINABLE_VARS_PARAMS_STAT_OPTIONS.copy()
        outfile = os.path.join(test.get_temp_dir(), 'timeline')
        opts['output'] = 'timeline:outfile=' + outfile
        opts['account_type_regexes'] = ['.*']
        opts['max_depth'] = 100000
        opts['step'] = 0

        with session.Session() as sess:
            x = lib.BuildFullModel()

            sess.run(variables.global_variables_initializer())
            run_meta = config_pb2.RunMetadata()
            _ = sess.run(x,
                         options=config_pb2.RunOptions(
                             trace_level=config_pb2.RunOptions.FULL_TRACE),
                         run_metadata=run_meta)

            _ = model_analyzer.profile(sess.graph,
                                       run_meta,
                                       cmd='graph',
                                       options=opts)

            with gfile.Open(outfile, 'r') as f:
                # Test that a json file is created.
                # TODO(xpan): tfprof Timeline isn't quite correct on Windows.
                # Investigate why.
                if os.name != 'nt':
                    self.assertLess(1000, len(f.read()))
                else:
                    self.assertLess(1, len(f.read()))
  def testComplexCodeView(self):
    ops.reset_default_graph()
    outfile = os.path.join(test.get_temp_dir(), 'dump')
    opts = (builder(builder.trainable_variables_parameter())
            .with_file_output(outfile)
            .with_accounted_types(['.*'])
            .with_node_names(show_name_regexes=
                             ['.*model_analyzer_testlib.py.*'])
            .account_displayed_op_only(False)
            .select(['params', 'float_ops']).build())

    with profile_context.ProfileContext(test.get_temp_dir(),
                                        trace_steps=[],
                                        dump_steps=[]) as pctx:
      with session.Session() as sess:
        x = lib.BuildFullModel()

        sess.run(variables.global_variables_initializer())
        pctx.trace_next_step()
        _ = sess.run(x)
        tfprof_node = pctx.profiler.profile_python(options=opts)

        # pylint: disable=line-too-long
        with gfile.Open(outfile, 'r') as f:
          lines = f.read().split('\n')
          result = '\n'.join([l[:min(len(l), 80)] for l in lines])
          self.assertEqual(compat.as_bytes('node name | # parameters | # float_ops\n_TFProfRoot (--/2.84k params, --/168.85k flops)\n  model_analyzer_testlib.py:63:BuildFullModel (0/1.80k params, 0/45.37k flops)\n    model_analyzer_testlib.py:40:BuildSmallModel (0/0 params, 0/0 flops)\n    model_analyzer_testlib.py:44:BuildSmallModel (0/4 params, 0/8 flops)\n    model_analyzer_testlib.py:48:BuildSmallModel (0/648 params, 0/1.30k flops)\n    model_analyzer_testlib.py:49:BuildSmallModel (0/0 params, 0/23.33k flops)\n    model_analyzer_testlib.py:53:BuildSmallModel (0/1.15k params, 0/2.30k flops)\n    model_analyzer_testlib.py:54:BuildSmallModel (0/0 params, 0/18.43k flops)\n  model_analyzer_testlib.py:63:BuildFullModel (gradient) (0/0 params, 0/67.39k f\n    model_analyzer_testlib.py:49:BuildSmallModel (gradient) (0/0 params, 0/46.66\n    model_analyzer_testlib.py:54:BuildSmallModel (gradient) (0/0 params, 0/20.74\n  model_analyzer_testlib.py:67:BuildFullModel (0/1.04k params, 0/18.57k flops)\n  model_analyzer_testlib.py:67:BuildFullModel (gradient) (0/0 params, 0/37.00k f\n  model_analyzer_testlib.py:69:BuildFullModel (0/0 params, 0/0 flops)\n  model_analyzer_testlib.py:70:BuildFullModel (0/0 params, 0/258 flops)\n  model_analyzer_testlib.py:70:BuildFullModel (gradient) (0/0 params, 0/129 flop\n  model_analyzer_testlib.py:72:BuildFullModel (0/0 params, 0/141 flops)\n'),
                           compat.as_bytes(result))

        self.assertLess(0, tfprof_node.total_exec_micros)
        self.assertEqual(2844, tfprof_node.total_parameters)
        self.assertEqual(168854, tfprof_node.total_float_ops)
        self.assertEqual(8, len(tfprof_node.children))
        self.assertEqual('_TFProfRoot', tfprof_node.name)
        self.assertEqual(
            'model_analyzer_testlib.py:63:BuildFullModel',
            tfprof_node.children[0].name)
        self.assertEqual(
            'model_analyzer_testlib.py:63:BuildFullModel (gradient)',
            tfprof_node.children[1].name)
        self.assertEqual(
            'model_analyzer_testlib.py:67:BuildFullModel',
            tfprof_node.children[2].name)
        self.assertEqual(
            'model_analyzer_testlib.py:67:BuildFullModel (gradient)',
            tfprof_node.children[3].name)
        self.assertEqual(
            'model_analyzer_testlib.py:69:BuildFullModel',
            tfprof_node.children[4].name)
        self.assertEqual(
            'model_analyzer_testlib.py:70:BuildFullModel',
            tfprof_node.children[5].name)
        self.assertEqual(
            'model_analyzer_testlib.py:70:BuildFullModel (gradient)',
            tfprof_node.children[6].name)
        self.assertEqual(
            'model_analyzer_testlib.py:72:BuildFullModel',
            tfprof_node.children[7].name)
Esempio n. 7
0
    def testOpView(self):
        ops.reset_default_graph()
        outfile = os.path.join(test.get_temp_dir(), 'dump')

        opts = (builder(
            builder.trainable_variables_parameter()).with_file_output(
                outfile).with_accounted_types([
                    '.*'
                ]).with_min_occurrence(10).order_by('occurrence').select(
                    ['params', 'micros', 'occurrence',
                     'input_shapes']).build())

        with session.Session() as sess:
            x = lib.BuildFullModel()

            sess.run(variables.global_variables_initializer())
            run_meta = config_pb2.RunMetadata()
            _ = sess.run(x,
                         options=config_pb2.RunOptions(
                             trace_level=config_pb2.RunOptions.FULL_TRACE),
                         run_metadata=run_meta)

            tfprof_node = model_analyzer.profile(sess.graph,
                                                 run_meta,
                                                 cmd='op',
                                                 options=opts)

            with gfile.Open(outfile, 'r') as f:
                # pylint: disable=line-too-long
                self.assertEqual(
                    'nodename|totalexecutiontime|acceleratorexecutiontime|cpuexecutiontime|#parameters|opoccurrence(run|defined)|inputshapes\n',
                    f.read().replace('\t', '').replace(' ', '')[0:120])
                # pylint: enable=line-too-long

            total_children = 0
            last_occurrence = 1e32
            input_shapes = 0
            last_total_micros = tfprof_node.total_exec_micros
            last_micros = tfprof_node.exec_micros
            while tfprof_node.children:
                for gnode in tfprof_node.graph_nodes:
                    input_shapes += len(gnode.input_shapes)
                self.assertEqual(len(tfprof_node.children), 1)
                tfprof_node = tfprof_node.children[0]

                self.assertEqual(last_total_micros,
                                 tfprof_node.total_exec_micros + last_micros)
                last_total_micros = tfprof_node.total_exec_micros
                last_micros = tfprof_node.exec_micros

                total_children += 1
                self.assertLessEqual(len(tfprof_node.graph_nodes),
                                     last_occurrence)
                last_occurrence = len(tfprof_node.graph_nodes)

            self.assertEqual(total_children, 15)
            self.assertGreater(input_shapes, 0)
    def testComplexCodeView(self):
        ops.reset_default_graph()
        outfile = os.path.join(test.get_temp_dir(), 'dump')
        opts = (builder(
            builder.trainable_variables_parameter()).with_file_output(
                outfile).with_accounted_types(['.*']).with_node_names(
                    show_name_regexes=['.*model_analyzer_testlib.py.*']).
                account_displayed_op_only(False).select(
                    ['params', 'float_ops']).build())

        with session.Session() as sess:
            x = lib.BuildFullModel()

            sess.run(variables.global_variables_initializer())
            run_meta = config_pb2.RunMetadata()
            _ = sess.run(x,
                         options=config_pb2.RunOptions(
                             trace_level=config_pb2.RunOptions.FULL_TRACE),
                         run_metadata=run_meta)

            tfprof_node = model_analyzer.profile(sess.graph,
                                                 run_meta,
                                                 cmd='code',
                                                 options=opts)

            # pylint: disable=line-too-long
            with gfile.Open(outfile, 'r') as f:
                lines = f.read().split('\n')
                result = '\n'.join([l[:min(len(l), 80)] for l in lines])
                self.assertEqual(
                    'node name | # parameters | # float_ops\n_TFProfRoot (--/2.84k params, --/91.04k flops)\n  model_analyzer_testlib.py:58:BuildFullModel (0/1.80k params, 0/41.76k flops)\n    model_analyzer_testlib.py:35:BuildSmallModel (0/0 params, 0/0 flops)\n    model_analyzer_testlib.py:39:BuildSmallModel (0/4 params, 0/0 flops)\n    model_analyzer_testlib.py:43:BuildSmallModel (0/648 params, 0/0 flops)\n    model_analyzer_testlib.py:44:BuildSmallModel (0/0 params, 0/23.33k flops)\n    model_analyzer_testlib.py:48:BuildSmallModel (0/1.15k params, 0/0 flops)\n    model_analyzer_testlib.py:49:BuildSmallModel (0/0 params, 0/18.43k flops)\n  model_analyzer_testlib.py:58:BuildFullModel (gradient) (0/0 params, 0/0 flops)\n    model_analyzer_testlib.py:44:BuildSmallModel (gradient) (0/0 params, 0/0 flo\n    model_analyzer_testlib.py:49:BuildSmallModel (gradient) (0/0 params, 0/0 flo\n  model_analyzer_testlib.py:62:BuildFullModel (0/1.04k params, 0/16.51k flops)\n  model_analyzer_testlib.py:62:BuildFullModel (gradient) (0/0 params, 0/32.77k f\n  model_analyzer_testlib.py:64:BuildFullModel (0/0 params, 0/0 flops)\n  model_analyzer_testlib.py:65:BuildFullModel (0/0 params, 0/0 flops)\n  model_analyzer_testlib.py:65:BuildFullModel (gradient) (0/0 params, 0/0 flops)\n  model_analyzer_testlib.py:67:BuildFullModel (0/0 params, 0/0 flops)\n',
                    result)

            self.assertLess(0, tfprof_node.total_exec_micros)
            self.assertEqual(2844, tfprof_node.total_parameters)
            self.assertEqual(91040, tfprof_node.total_float_ops)
            self.assertEqual(8, len(tfprof_node.children))
            self.assertEqual('_TFProfRoot', tfprof_node.name)
            self.assertEqual('model_analyzer_testlib.py:58:BuildFullModel',
                             tfprof_node.children[0].name)
            self.assertEqual(
                'model_analyzer_testlib.py:58:BuildFullModel (gradient)',
                tfprof_node.children[1].name)
            self.assertEqual('model_analyzer_testlib.py:62:BuildFullModel',
                             tfprof_node.children[2].name)
            self.assertEqual(
                'model_analyzer_testlib.py:62:BuildFullModel (gradient)',
                tfprof_node.children[3].name)
            self.assertEqual('model_analyzer_testlib.py:64:BuildFullModel',
                             tfprof_node.children[4].name)
            self.assertEqual('model_analyzer_testlib.py:65:BuildFullModel',
                             tfprof_node.children[5].name)
            self.assertEqual(
                'model_analyzer_testlib.py:65:BuildFullModel (gradient)',
                tfprof_node.children[6].name)
            self.assertEqual('model_analyzer_testlib.py:67:BuildFullModel',
                             tfprof_node.children[7].name)
    def testComplexCodeView(self):
        ops.reset_default_graph()
        opts = model_analyzer.TRAINABLE_VARS_PARAMS_STAT_OPTIONS.copy()
        outfile = os.path.join(test.get_temp_dir(), 'dump')
        opts['output'] = 'file:outfile=' + outfile
        opts['account_type_regexes'] = ['.*']
        opts['show_name_regexes'] = ['.*model_analyzer_testlib.py.*']
        opts['account_displayed_op_only'] = False
        opts['select'] = ['params', 'float_ops']

        with session.Session() as sess:
            x = lib.BuildFullModel()

            sess.run(variables.global_variables_initializer())
            run_meta = config_pb2.RunMetadata()
            _ = sess.run(x,
                         options=config_pb2.RunOptions(
                             trace_level=config_pb2.RunOptions.FULL_TRACE),
                         run_metadata=run_meta)

            tfprof_node = model_analyzer.profile(sess.graph,
                                                 run_meta,
                                                 cmd='code',
                                                 options=opts)

            # pylint: disable=line-too-long
            with gfile.Open(outfile, 'r') as f:
                lines = f.read().split('\n')
                result = '\n'.join([l[:min(len(l), 80)] for l in lines])
                self.assertEqual(
                    'node name | # parameters | # float_ops\n_TFProfRoot (--/2.84k params, --/91.04k flops)\n  model_analyzer_testlib.py:58:BuildFullModel:seq.append(array_... (0/1.80k para\n    model_analyzer_testlib.py:35:BuildSmallModel:image = array_ops... (0/0 param\n    model_analyzer_testlib.py:39:BuildSmallModel:initializer=init_... (0/4 param\n    model_analyzer_testlib.py:43:BuildSmallModel:initializer=init_... (0/648 par\n    model_analyzer_testlib.py:44:BuildSmallModel:x = nn_ops.conv2d... (0/0 param\n    model_analyzer_testlib.py:48:BuildSmallModel:initializer=init_... (0/1.15k p\n    model_analyzer_testlib.py:49:BuildSmallModel:x = nn_ops.conv2d... (0/0 param\n  model_analyzer_testlib.py:62:BuildFullModel:cell, array_ops.c... (0/1.04k para\n  model_analyzer_testlib.py:64:BuildFullModel:target = array_op... (0/0 params, \n  model_analyzer_testlib.py:65:BuildFullModel:loss = nn_ops.l2_... (0/0 params, \n  model_analyzer_testlib.py:67:BuildFullModel:return sgd_op.min... (0/0 params, \n',
                    result)

            self.assertLess(0, tfprof_node.total_exec_micros)
            self.assertEqual(2844, tfprof_node.total_parameters)
            self.assertEqual(91040, tfprof_node.total_float_ops)
            self.assertEqual(5, len(tfprof_node.children))
            self.assertEqual('_TFProfRoot', tfprof_node.name)
            self.assertEqual(
                'model_analyzer_testlib.py:58:BuildFullModel:seq.append(array_...',
                tfprof_node.children[0].name)
            self.assertEqual(
                'model_analyzer_testlib.py:62:BuildFullModel:cell, array_ops.c...',
                tfprof_node.children[1].name)
            self.assertEqual(
                'model_analyzer_testlib.py:64:BuildFullModel:target = array_op...',
                tfprof_node.children[2].name)
            self.assertEqual(
                'model_analyzer_testlib.py:65:BuildFullModel:loss = nn_ops.l2_...',
                tfprof_node.children[3].name)
            self.assertEqual(
                'model_analyzer_testlib.py:67:BuildFullModel:return sgd_op.min...',
                tfprof_node.children[4].name)
def _run_loop_model():
  with session.Session() as sess:
    x = lib.BuildFullModel()

    sess.run(variables.global_variables_initializer())
    run_meta = config_pb2.RunMetadata()
    _ = sess.run(x,
                 options=config_pb2.RunOptions(
                     trace_level=config_pb2.RunOptions.FULL_TRACE),
                 run_metadata=run_meta)

    tfprof_node = model_analyzer.profile(
        sess.graph, run_meta,
        options=builder.time_and_memory())
    return tfprof_node, run_meta
    def pprof_test_helper(self, attribute, should_fail=False):
        ops.reset_default_graph()
        outfile = os.path.join(test.get_temp_dir(), attribute + '_pprof.pb.gz')
        opts = (builder(builder.time_and_memory()).select([
            attribute
        ]).with_max_depth(100000).with_node_names(
            trim_name_regexes=['ops.py.*']).with_pprof_output(outfile).build())

        with session.Session() as sess:
            x = lib.BuildFullModel()

            sess.run(variables.global_variables_initializer())
            run_meta = config_pb2.RunMetadata()
            _ = sess.run(x,
                         options=config_pb2.RunOptions(
                             trace_level=config_pb2.RunOptions.FULL_TRACE),
                         run_metadata=run_meta)

            _ = model_analyzer.profile(sess.graph,
                                       run_meta,
                                       cmd='code',
                                       options=opts)

            if should_fail:
                self.assertFalse(gfile.Exists(outfile))
                return

            profile_pb = profile_pb2.Profile()
            with gfile.Open(outfile, 'rb') as f:
                with gzip.GzipFile(fileobj=io.BytesIO(f.read())) as gzipf:
                    profile_pb.ParseFromString(gzipf.read())

            self.assertGreater(len(profile_pb.sample), 10)
            self.assertGreater(len(profile_pb.location), 10)
            self.assertGreater(len(profile_pb.function), 10)
            self.assertGreater(len(profile_pb.string_table), 30)

            has_rnn = False
            has_loop = False
            for s in profile_pb.string_table:
                if s.find('rnn') > 0:
                    has_rnn = True
                if s.find('while') > 0:
                    has_loop = True
                self.assertFalse(s.startswith('ops.py'))
            self.assertTrue(has_rnn)
            self.assertTrue(has_loop)
Esempio n. 12
0
    def testBasics(self):
        ops.reset_default_graph()
        outfile = os.path.join(test.get_temp_dir(), "dump")
        opts = builder(
            builder.time_and_memory()).with_file_output(outfile).build()

        x = lib.BuildFullModel()

        profile_str = None
        profile_step100 = os.path.join(test.get_temp_dir(), "profile_100")
        with profile_context.ProfileContext(test.get_temp_dir()) as pctx:
            pctx.add_auto_profiling("op",
                                    options=opts,
                                    profile_steps=[15, 50, 100])
            with session.Session() as sess:
                sess.run(variables.global_variables_initializer())
                total_steps = 101
                for i in range(total_steps):
                    sess.run(x)
                    if i == 14 or i == 49:
                        self.assertTrue(gfile.Exists(outfile))
                        gfile.Remove(outfile)
                    if i == 99:
                        self.assertTrue(gfile.Exists(profile_step100))
                        with gfile.Open(outfile, "r") as f:
                            profile_str = f.read()
                        gfile.Remove(outfile)

            self.assertEqual(set([15, 50, 100]),
                             set(pctx.get_profiles("op").keys()))

        with lib.ProfilerFromFile(
                os.path.join(test.get_temp_dir(), "profile_100")) as profiler:
            profiler.profile_operations(options=opts)
            with gfile.Open(outfile, "r") as f:

                if test.is_built_with_rocm():
                    # The profiler output for ROCm mode, includes an extra warning related
                    # to the lack of stream tracing in ROCm moed. Need to skip this warning
                    # when doing the diff in ROCm mode
                    profile_str = "\n".join(profile_str.split("\n")[7:])

                self.assertEqual(profile_str, f.read())
Esempio n. 13
0
  def testAutoTracingInDeubMode(self):
    ops.reset_default_graph()
    x = lib.BuildFullModel()

    with profile_context.ProfileContext(test.get_temp_dir(), debug=True):
      with session.Session() as sess:
        self.evaluate(variables.global_variables_initializer())
        for _ in range(10):
          self.evaluate(x)
          for f in gfile.ListDirectory(test.get_temp_dir()):
            # Warm up, no tracing.
            self.assertFalse("run_meta" in f)
        self.evaluate(x)
        self.assertTrue(
            gfile.Exists(os.path.join(test.get_temp_dir(), "run_meta_11")))
        gfile.Remove(os.path.join(test.get_temp_dir(), "run_meta_11"))
        # fetched already.
        self.evaluate(x)
        for f in gfile.ListDirectory(test.get_temp_dir()):
          self.assertFalse("run_meta" in f)
Esempio n. 14
0
  def testDisabled(self):
    ops.reset_default_graph()
    x = lib.BuildFullModel()
    with profile_context.ProfileContext(test.get_temp_dir(),
                                        enabled=False) as pctx:
      with session.Session() as sess:
        self.evaluate(variables.global_variables_initializer())
        for _ in range(10):
          self.evaluate(x)
      self.assertTrue(pctx.profiler is None)
      self.assertTrue(
          getattr(session.BaseSession, "profile_context", None) is None)

    with profile_context.ProfileContext(test.get_temp_dir()) as pctx:
      with session.Session() as sess:
        self.evaluate(variables.global_variables_initializer())
        for _ in range(10):
          self.evaluate(x)
      self.assertFalse(pctx.profiler is None)
      self.assertFalse(
          getattr(session.BaseSession, "profile_context", None) is None)
Esempio n. 15
0
def _run_loop_model():
  config = config_pb2.ConfigProto()
  # Grappler might fuse MatMul with BiasAdd in remapper optimizer.
  config.graph_options.rewrite_options.remapping = (
      rewriter_config_pb2.RewriterConfig.OFF)
  with session.Session(config=config) as sess:
    x = lib.BuildFullModel()

    sess.run(variables.global_variables_initializer())
    run_meta = config_pb2.RunMetadata()
    _ = sess.run(x,
                 options=config_pb2.RunOptions(
                     trace_level=config_pb2.RunOptions.SOFTWARE_TRACE),
                 run_metadata=run_meta)

    opts = builder.time_and_memory()
    opts['order_by'] = 'name'
    opts['output'] = 'none'

    tfprof_node = model_analyzer.profile(
        sess.graph, run_meta, options=opts)
    return tfprof_node, run_meta
Esempio n. 16
0
  def testProfileBasic(self):
    ops.reset_default_graph()
    outfile = os.path.join(test.get_temp_dir(), 'dump')
    opts = (builder(builder.trainable_variables_parameter())
            .with_file_output(outfile)
            .with_accounted_types(['.*'])
            .select(['params', 'float_ops', 'micros', 'bytes',
                     'device', 'op_types', 'occurrence']).build())

    # Test the output without run_meta.
    sess = session.Session()
    r = lib.BuildFullModel()
    sess.run(variables.global_variables_initializer())

    # Test the output with run_meta.
    run_meta = config_pb2.RunMetadata()
    _ = sess.run(r,
                 options=config_pb2.RunOptions(
                     trace_level=config_pb2.RunOptions.FULL_TRACE),
                 run_metadata=run_meta)

    profiler = model_analyzer.Profiler(sess.graph)
    profiler.add_step(1, run_meta)
    profiler.profile_graph(opts)
    with gfile.Open(outfile, 'r') as f:
      profiler_str = f.read()

    model_analyzer.profile(
        sess.graph, cmd='graph', run_meta=run_meta, options=opts)
    with gfile.Open(outfile, 'r') as f:
      pma_str = f.read()
    self.assertEqual(pma_str, profiler_str)

    profiler.profile_name_scope(opts)
    with gfile.Open(outfile, 'r') as f:
      profiler_str = f.read()

    model_analyzer.profile(
        sess.graph, cmd='scope', run_meta=run_meta, options=opts)
    with gfile.Open(outfile, 'r') as f:
      pma_str = f.read()
    self.assertEqual(pma_str, profiler_str)

    profiler.profile_python(opts)
    with gfile.Open(outfile, 'r') as f:
      profiler_str = f.read()

    model_analyzer.profile(
        sess.graph, cmd='code', run_meta=run_meta, options=opts)
    with gfile.Open(outfile, 'r') as f:
      pma_str = f.read()
    self.assertEqual(pma_str, profiler_str)

    profiler.profile_operations(opts)
    with gfile.Open(outfile, 'r') as f:
      profiler_str = f.read()

    model_analyzer.profile(
        sess.graph, cmd='op', run_meta=run_meta, options=opts)
    with gfile.Open(outfile, 'r') as f:
      pma_str = f.read()
    self.assertEqual(pma_str, profiler_str)

    model_analyzer.profile(
        sess.graph, cmd='scope', run_meta=run_meta, options=opts)
    with gfile.Open(outfile, 'r') as f:
      pma_str = f.read()
    self.assertNotEqual(pma_str, profiler_str)