Exemple #1
0
    def test_create_framework_parser_fail_1(self, *args):
        """Test the function of fail to create framework parser."""
        args[0].side_effect = ValidationError(
            {'profiler': {"The path is invalid!"}})

        with pytest.raises(ProfilerPathErrorException) as exc_info:
            FrameworkParser('JOB1', '0')
        assert exc_info.value.error_code == '50546081'
        assert exc_info.value.message == 'Path error. Profiling path is invalid.'
Exemple #2
0
    def test_create_framework_parser_fail_3(self, *args):
        """Test the function of fail to create framework parser."""
        args[0].return_value = True
        args[1].return_value = []
        FrameworkParser._raw_data_dir = '/var/log/npu/profiling'

        with pytest.raises(ProfilerFileNotFoundException) as exc_info:
            FrameworkParser('JOB1', '0')
        assert exc_info.value.error_code == '50546084'
        assert exc_info.value.message == 'The file <Framework> not found.'
Exemple #3
0
    def test_create_framework_parser_fail_2(self, *args):
        """Test the function of fail to create framework parser."""
        args[0].return_value = False
        FrameworkParser._raw_data_dir = '/var/log/npu/profiling'

        with pytest.raises(ProfilerDirNotFoundException) as exc_info:
            FrameworkParser('JOB1', '0')
        assert exc_info.value.error_code == '50546083'
        assert exc_info.value.message == \
               'The dir </var/log/npu/profiling/JOB1> not found.'
Exemple #4
0
    def setup_method(self):
        """Initialization before test case execution."""
        FrameworkParser._raw_data_dir = RAW_DATA_BASE

        self._output_path_1 = tempfile.mkdtemp(prefix='test_framework_parser_')
        self._parser_1 = FrameworkParser('JOB1', '0', self._output_path_1)

        self._output_path_2 = tempfile.mkdtemp(prefix='test_framework_parser_')
        self._parser_2 = FrameworkParser('JOB2', '0', self._output_path_2)

        self._output_path_4 = tempfile.mkdtemp(prefix='test_framework_parser_')
        self._parser_4 = FrameworkParser('JOB4', '0', self._output_path_4)
Exemple #5
0
class TestFrameworkParser:
    """Test the class of `FrameworkParser`."""
    def setup_method(self):
        """Initialization before test case execution."""
        FrameworkParser._raw_data_dir = RAW_DATA_BASE

        self._output_path_1 = tempfile.mkdtemp(prefix='test_framework_parser_')
        self._parser_1 = FrameworkParser('JOB1', '0', self._output_path_1)

        self._output_path_2 = tempfile.mkdtemp(prefix='test_framework_parser_')
        self._parser_2 = FrameworkParser('JOB2', '0', self._output_path_2)

        self._output_path_4 = tempfile.mkdtemp(prefix='test_framework_parser_')
        self._parser_4 = FrameworkParser('JOB4', '0', self._output_path_4)

    def teardown_method(self) -> None:
        """Clear up after test case execution."""
        shutil.rmtree(self._output_path_1)
        shutil.rmtree(self._output_path_2)
        shutil.rmtree(self._output_path_4)
        FrameworkParser._raw_data_dir = '/var/log/npu/profiling'

    def test_save_path(self):
        """Test the querying save path function."""
        expect_result = os.path.join(self._output_path_1,
                                     'framework_raw_0.csv')
        assert expect_result == self._parser_1.save_path

        expect_result = os.path.join(self._output_path_2,
                                     'framework_raw_0.csv')
        assert expect_result == self._parser_2.save_path

    def test_point_info(self):
        """Test the querying point info function."""
        expect_result = {1: 'Default/Cast-op6', 2: 'Default/TransData-op7'}
        assert expect_result == self._parser_4.point_info

    def test_to_task_id_full_op_name_dict(self):
        """Test the querying task id and full operator name dict function."""
        expect_result = {
            '51517':
            'Default/Cast-op6',
            '51518':
            'Default/TransData-op7',
            '51519':
            'Default/network-WithLossCell/_backbone-ResNet/conv1-Conv2d/Cast-op5',
            '51522':
            'Default/network-WithLossCell/_backbone-ResNet/'
            'layer1-SequentialCell/0-ResidualBlock/conv1-Conv2d/Cast-op28'
        }
        assert expect_result == self._parser_1.to_task_id_full_op_name_dict()
        assert expect_result == self._parser_2.to_task_id_full_op_name_dict()

        expect_result = {
            '0_1':
            'Default/Cast-op6',
            '0_2':
            'Default/TransData-op7',
            '0_3':
            'Default/network-WithLossCell/_backbone-ResNet/conv1-Conv2d/Cast-op5',
            '0_4':
            'Default/network-WithLossCell/_backbone-ResNet/layer1-SequentialCell/'
            '0-ResidualBlock/conv1-Conv2d/Cast-op28'
        }
        assert expect_result == self._parser_4.to_task_id_full_op_name_dict()

    def test_parse(self):
        """Test the parse function."""
        expect_framework_file = os.path.join(PROFILER_DIR,
                                             'framework_raw_0.csv')
        expect_framework_file = os.path.realpath(expect_framework_file)
        expect_result = get_framework_result(expect_framework_file)

        self._parser_1.parse()
        framework_file = os.path.join(self._output_path_1,
                                      'framework_raw_0.csv')
        result = get_framework_result(framework_file)
        assert expect_result == result

        self._parser_2.parse()
        framework_file = os.path.join(self._output_path_2,
                                      'framework_raw_0.csv')
        result = get_framework_result(framework_file)
        assert expect_result == result

    @mock.patch(
        'mindinsight.profiler.parser.framework_parser.validate_and_normalize_path'
    )
    def test_create_framework_parser_fail_1(self, *args):
        """Test the function of fail to create framework parser."""
        args[0].side_effect = ValidationError(
            {'profiler': {"The path is invalid!"}})

        with pytest.raises(ProfilerPathErrorException) as exc_info:
            FrameworkParser('JOB1', '0')
        assert exc_info.value.error_code == '50546081'
        assert exc_info.value.message == 'Path error. Profiling path is invalid.'

    @mock.patch('os.path.isdir')
    def test_create_framework_parser_fail_2(self, *args):
        """Test the function of fail to create framework parser."""
        args[0].return_value = False
        FrameworkParser._raw_data_dir = '/var/log/npu/profiling'

        with pytest.raises(ProfilerDirNotFoundException) as exc_info:
            FrameworkParser('JOB1', '0')
        assert exc_info.value.error_code == '50546083'
        assert exc_info.value.message == \
               'The dir </var/log/npu/profiling/JOB1> not found.'

    @mock.patch('os.listdir')
    @mock.patch('os.path.isdir')
    def test_create_framework_parser_fail_3(self, *args):
        """Test the function of fail to create framework parser."""
        args[0].return_value = True
        args[1].return_value = []
        FrameworkParser._raw_data_dir = '/var/log/npu/profiling'

        with pytest.raises(ProfilerFileNotFoundException) as exc_info:
            FrameworkParser('JOB1', '0')
        assert exc_info.value.error_code == '50546084'
        assert exc_info.value.message == 'The file <Framework> not found.'
Exemple #6
0
    def analyse(self):
        """
        Collect and analyse performance data, called after training or during training.

        Examples:
            >>> from mindinsight.profiler import Profiler
            >>> context.set_context(mode=context.GRAPH_MODE, device_target="Ascend",
            >>>                     device_id=int(os.environ["DEVICE_ID"]))
            >>> profiler = Profiler(subgraph='all', is_detail=True, is_show_op_path=False, output_path='./data')
            >>> model = Model(train_network)
            >>> dataset = get_dataset()
            >>> model.train(2, dataset)
            >>> profiler.analyse()
        """

        try:
            from mindspore.communication.management import release
            release()
        except ImportError:
            logger.error("Profiling: fail to import release from mindspore.")

        job_id = self._get_profiling_job_id()
        logger.info("Profiling: job id is %s ", job_id)

        source_path = os.path.join(PROFILING_LOG_BASE_PATH, job_id)
        # parse hwts.log.data.45.dev file, and get task profiling data
        hwts_output_filename = self._hwts_output_filename_target + self._dev_id + ".txt"
        hwts_output_filename = os.path.join(self._output_path, hwts_output_filename)
        hwtslog_parser = HWTSLogParser(source_path, hwts_output_filename)
        result = hwtslog_parser.execute()
        if not result:
            logger.error("Profiling: fail to parse hwts log file.")
            return

        # parse Framework file, and get the relation of op and tasks
        framework_parser = FrameworkParser(job_id, self._dev_id, self._output_path)
        framework_parser.parse()
        op_task_dict = framework_parser.to_task_id_full_op_name_dict()
        if not op_task_dict:
            logger.error("Profiling: fail to parse framework files.")
            return

        # get op compute time from hwts data and framework data, write output_op_compute_time.txt
        opcompute_output_filename = self._opcompute_output_filename_target + self._dev_id + ".txt"
        opcompute_output_filename = os.path.join(self._output_path, opcompute_output_filename)
        optime_parser = OPComputeTimeParser(
            hwts_output_filename, opcompute_output_filename,
            op_task_dict, self._output_path, self._dev_id
        )
        optime_parser.execute()

        # parse DATA_PREPROCESS.dev.AICPU file, write output_data_preprocess_aicpu_x.txt
        output_data_preprocess_aicpu = self._aicpu_op_output_filename_target + self._dev_id + ".txt"
        output_data_preprocess_aicpu = os.path.join(self._output_path, output_data_preprocess_aicpu)
        aicpu_data_parser = DataPreProcessParser(source_path, output_data_preprocess_aicpu)
        aicpu_data_parser.execute()

        # Parsing minddata AICPU profiling
        MinddataParser.execute(source_path, self._output_path, self._dev_id)

        # parse minddata pipeline operator and queue
        try:
            pipeline_parser = MinddataPipelineParser(self._output_path, self._dev_id, self._output_path)
            pipeline_parser.parse()
        except MindInsightException as err:
            logger.warning(err.message)

        # analyse op compute time info
        try:
            self._analyser_op_info()
        except MindInsightException as err:
            logger.warning(err.message)

        # analyse step trace info
        try:
            self._analyse_step_trace(source_path, framework_parser)
        except MindInsightException as err:
            logger.warning(err.message)

        # analyse timeline info
        try:
            self._analyse_timeline()
        except (ProfilerIOException, ProfilerFileNotFoundException, ValidationError) as err:
            logger.warning('Fail to write timeline data: %s', err)
Exemple #7
0
    def analyse(self):
        """
        Collect and analyse performance data, called after training or during training.

        Examples:
            >>> from mindinsight.profiler import Profiler
            >>> context.set_context(mode=context.GRAPH_MODE, device_target=“Ascend”,
            >>>                     device_id=int(os.environ["DEVICE_ID"]))
            >>> profiler = Profiler(subgraph='all', is_detail=True, is_show_op_path=False, output_path='./data')
            >>> model = Model(train_network)
            >>> dataset = get_dataset()
            >>> model.train(2, dataset)
            >>> profiler.analyse()
        """

        try:
            from mindspore.communication.management import release
            release()
        except ImportError:
            logger.error("Profiling: fail to import release from mindspore.")

        logger.info("begin profiler analyse")

        job_id = self._get_profiling_job_id()
        if not job_id:
            msg = ("Fail to get profiling job, please check whether job dir was generated under path %s" \
                   % PROFILING_LOG_BASE_PATH)
            raise RuntimeError(msg)

        logger.info("Profiling: job id is %s ", job_id)

        source_path = os.path.join(PROFILING_LOG_BASE_PATH, job_id)
        # parse hwts.log.data.45.dev file, and get task profiling data
        hwts_output_filename = self._hwts_output_filename_target + self._dev_id + ".txt"
        hwts_output_filename = os.path.join(self._output_path, hwts_output_filename)
        hwtslog_parser = HWTSLogParser(source_path, hwts_output_filename)
        result = hwtslog_parser.execute()
        if not result:
            logger.error("Profiling: fail to parse hwts log file.")
            return

        # parse Framework file, and get the relation of op and tasks
        framework_parser = FrameworkParser(job_id, self._dev_id, self._output_path)
        framework_parser.parse()
        op_task_dict = framework_parser.to_task_id_full_op_name_dict()
        if not op_task_dict:
            logger.error("Profiling: fail to parse framework files.")
            return

        # get op compute time from hwts data and framework data, write output_op_compute_time.txt
        opcompute_output_filename = self._opcompute_output_filename_target + self._dev_id + ".txt"
        opcompute_output_filename = os.path.join(self._output_path, opcompute_output_filename)
        optime_parser = OPComputeTimeParser(hwts_output_filename, opcompute_output_filename, op_task_dict)
        optime_parser.execute()

        # parse DATA_PREPROCESS.dev.AICPU file, write output_data_preprocess_aicpu_x.txt
        output_data_preprocess_aicpu = self._aicpu_op_output_filename_target + self._dev_id + ".txt"
        output_data_preprocess_aicpu = os.path.join(self._output_path, output_data_preprocess_aicpu)
        try:
            aicpu_data_parser = DataPreProcessParser(source_path, output_data_preprocess_aicpu)
            aicpu_data_parser.execute()
        except FileNotFoundError as err:
            logger.exception(err)

        # analyse op compute time info
        try:
            self._analyser_op_info()
        except MindInsightException as err:
            logger.error(err.message)