test_model.py 28.0 KB
Newer Older
G
gaocongli 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
# Copyright 2019 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================

"""
Function:
    Test the query module about lineage information.
Usage:
    The query module test should be run after lineagemgr/collection/model/test_model_lineage.py
    pytest lineagemgr
"""
import os
from unittest import TestCase

import pytest

L
Li Hongzhang 已提交
28
from mindinsight.lineagemgr.model import filter_summary_lineage, get_summary_lineage
李鸿章 已提交
29 30 31
from mindinsight.lineagemgr.common.exceptions.exceptions import (LineageFileNotFoundError, LineageParamSummaryPathError,
                                                                 LineageParamTypeError, LineageParamValueError,
                                                                 LineageSearchConditionParamError)
L
Li Hongzhang 已提交
32 33 34
from .conftest import BASE_SUMMARY_DIR, DATASET_GRAPH, SUMMARY_DIR, SUMMARY_DIR_2
from ....ut.lineagemgr.querier import event_data
from ....utils.tools import assert_equal_lineages
K
kouzhenzhong 已提交
35

C
chenchao99 已提交
36 37 38 39 40 41 42
LINEAGE_INFO_RUN1 = {
    'summary_dir': os.path.join(BASE_SUMMARY_DIR, 'run1'),
    'metric': {
        'accuracy': 0.78
    },
    'hyper_parameters': {
        'optimizer': 'Momentum',
43
        'learning_rate': 0.12,
C
chenchao99 已提交
44 45 46 47 48 49 50 51 52 53
        'loss_function': 'SoftmaxCrossEntropyWithLogits',
        'epoch': 14,
        'parallel_mode': 'stand_alone',
        'device_num': 2,
        'batch_size': 32
    },
    'algorithm': {
        'network': 'ResNet'
    },
    'train_dataset': {
54
        'train_dataset_size': 1024
C
chenchao99 已提交
55 56
    },
    'valid_dataset': {
57
        'valid_dataset_size': 1024
C
chenchao99 已提交
58 59 60 61 62 63 64 65 66 67
    },
    'model': {
        'path': '{"ckpt": "'
                + BASE_SUMMARY_DIR + '/run1/CKPtest_model.ckpt"}',
        'size': 64
    },
    'dataset_graph': DATASET_GRAPH
}
LINEAGE_FILTRATION_EXCEPT_RUN = {
    'summary_dir': os.path.join(BASE_SUMMARY_DIR, 'except_run'),
68 69 70 71 72 73 74 75 76
    'model_lineage': {
        'loss_function': 'SoftmaxCrossEntropyWithLogits',
        'train_dataset_path': None,
        'train_dataset_count': 1024,
        'test_dataset_path': None,
        'test_dataset_count': None,
        'user_defined': {},
        'network': 'ResNet',
        'optimizer': 'Momentum',
77
        'learning_rate': 0.12,
78 79
        'epoch': 10,
        'batch_size': 32,
L
luopengting 已提交
80
        'device_num': 2,
81
        'loss': 0.03,
82 83 84 85 86
        'model_size': 64,
        'metric': {},
        'dataset_mark': 2
    },
    'dataset_graph': DATASET_GRAPH
C
chenchao99 已提交
87 88 89
}
LINEAGE_FILTRATION_RUN1 = {
    'summary_dir': os.path.join(BASE_SUMMARY_DIR, 'run1'),
90 91 92
    'model_lineage': {
        'loss_function': 'SoftmaxCrossEntropyWithLogits',
        'train_dataset_path': None,
93
        'train_dataset_count': 1024,
94
        'test_dataset_path': None,
95
        'test_dataset_count': 1024,
96 97 98 99 100
        'user_defined': {
            'info': 'info1',
            'version': 'v1',
            'eval_version': 'version2'
        },
101 102
        'network': 'ResNet',
        'optimizer': 'Momentum',
103
        'learning_rate': 0.12,
104 105
        'epoch': 14,
        'batch_size': 32,
L
luopengting 已提交
106
        'device_num': 2,
107 108 109 110 111 112
        'loss': None,
        'model_size': 64,
        'metric': {
            'accuracy': 0.78
        },
        'dataset_mark': 2
C
chenchao99 已提交
113
    },
114
    'dataset_graph': DATASET_GRAPH
C
chenchao99 已提交
115 116 117
}
LINEAGE_FILTRATION_RUN2 = {
    'summary_dir': os.path.join(BASE_SUMMARY_DIR, 'run2'),
118
    'model_lineage': {
119
        'loss_function': "SoftmaxCrossEntropyWithLogits",
120
        'train_dataset_path': None,
121
        'train_dataset_count': 1024,
122
        'test_dataset_path': None,
123
        'test_dataset_count': 1024,
124
        'user_defined': {},
125 126
        'network': "ResNet",
        'optimizer': "Momentum",
127
        'learning_rate': 0.12,
128 129 130
        'epoch': 10,
        'batch_size': 32,
        'device_num': 2,
131
        'loss': 0.03,
132
        'model_size': 10,
133
        'metric': {
134
            'accuracy': 2.78
135 136
        },
        'dataset_mark': 3
C
chenchao99 已提交
137
    },
138
    'dataset_graph': DATASET_GRAPH
C
chenchao99 已提交
139 140 141
}


G
gaocongli 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
@pytest.mark.usefixtures("create_summary_dir")
class TestModelApi(TestCase):
    """Test lineage information query interface."""

    @classmethod
    def setup_class(cls):
        """The initial process."""
        cls.dir_with_empty_lineage = os.path.join(
            BASE_SUMMARY_DIR, 'dir_with_empty_lineage'
        )
        os.makedirs(cls.dir_with_empty_lineage)
        ms_file = os.path.join(
            cls.dir_with_empty_lineage, 'empty.summary.1581499502.bms_MS'
        )
        lineage_file = ms_file + '_lineage'
        with open(ms_file, mode='w'):
            pass
        with open(lineage_file, mode='w'):
            pass

        cls.empty_dir = os.path.join(BASE_SUMMARY_DIR, 'empty_dir')
        os.makedirs(cls.empty_dir)

    @pytest.mark.level0
    @pytest.mark.platform_arm_ascend_training
    @pytest.mark.platform_x86_gpu_training
    @pytest.mark.platform_x86_ascend_training
    @pytest.mark.platform_x86_cpu
    @pytest.mark.env_single
    def test_get_summary_lineage(self):
        """Test the interface of get_summary_lineage."""
L
Li Hongzhang 已提交
173 174 175
        total_res = get_summary_lineage(None, SUMMARY_DIR)
        partial_res1 = get_summary_lineage(None, SUMMARY_DIR, ['hyper_parameters'])
        partial_res2 = get_summary_lineage(None, SUMMARY_DIR, ['metric', 'algorithm'])
C
chenchao99 已提交
176
        expect_total_res = LINEAGE_INFO_RUN1
G
gaocongli 已提交
177 178 179 180
        expect_partial_res1 = {
            'summary_dir': os.path.join(BASE_SUMMARY_DIR, 'run1'),
            'hyper_parameters': {
                'optimizer': 'Momentum',
181
                'learning_rate': 0.12,
G
gaocongli 已提交
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
                'loss_function': 'SoftmaxCrossEntropyWithLogits',
                'epoch': 14,
                'parallel_mode': 'stand_alone',
                'device_num': 2,
                'batch_size': 32
            }
        }
        expect_partial_res2 = {
            'summary_dir': os.path.join(BASE_SUMMARY_DIR, 'run1'),
            'metric': {
                'accuracy': 0.78
            },
            'algorithm': {
                'network': 'ResNet'
            }
        }
198 199 200
        assert_equal_lineages(expect_total_res, total_res, self.assertDictEqual)
        assert_equal_lineages(expect_partial_res1, partial_res1, self.assertDictEqual)
        assert_equal_lineages(expect_partial_res2, partial_res2, self.assertDictEqual)
G
gaocongli 已提交
201 202

        # the lineage summary file is empty
L
Li Hongzhang 已提交
203
        result = get_summary_lineage(None, self.dir_with_empty_lineage)
G
gaocongli 已提交
204 205 206 207 208 209
        assert {} == result

        # keys is empty list
        expect_result = {
            'summary_dir': SUMMARY_DIR
        }
L
Li Hongzhang 已提交
210
        result = get_summary_lineage(None, SUMMARY_DIR, [])
G
gaocongli 已提交
211 212 213 214 215 216 217 218
        assert expect_result == result

    @pytest.mark.level0
    @pytest.mark.platform_arm_ascend_training
    @pytest.mark.platform_x86_gpu_training
    @pytest.mark.platform_x86_ascend_training
    @pytest.mark.platform_x86_cpu
    @pytest.mark.env_single
C
chenchao99 已提交
219
    def test_get_summary_lineage_exception_1(self):
G
gaocongli 已提交
220 221 222 223 224 225
        """Test the interface of get_summary_lineage with exception."""
        # summary path does not exist
        self.assertRaisesRegex(
            LineageParamSummaryPathError,
            'The summary path does not exist or is not a dir.',
            get_summary_lineage,
L
Li Hongzhang 已提交
226
            None,
G
gaocongli 已提交
227 228 229 230 231 232 233 234
            '/tmp/fake/dir'
        )

        # summary path is relative path
        self.assertRaisesRegex(
            LineageParamSummaryPathError,
            'The summary path is invalid.',
            get_summary_lineage,
L
Li Hongzhang 已提交
235
            None,
G
gaocongli 已提交
236 237 238 239 240 241 242 243
            'tmp'
        )

        # the type of input param is invalid
        self.assertRaisesRegex(
            LineageParamSummaryPathError,
            'The summary path is invalid.',
            get_summary_lineage,
L
Li Hongzhang 已提交
244
            None,
G
gaocongli 已提交
245 246 247 248 249 250 251 252
            ['/root/linage1', '/root/lineage2']
        )

        # summary path is empty str
        self.assertRaisesRegex(
            LineageParamSummaryPathError,
            'The summary path is invalid.',
            get_summary_lineage,
L
Li Hongzhang 已提交
253
            None,
G
gaocongli 已提交
254 255 256 257 258 259 260 261 262
            '',
            keys=None
        )

        # summary path invalid
        self.assertRaisesRegex(
            LineageParamSummaryPathError,
            'The summary path is invalid.',
            get_summary_lineage,
L
Li Hongzhang 已提交
263
            None,
G
gaocongli 已提交
264 265 266 267
            '\\',
            keys=None
        )

C
chenchao99 已提交
268 269 270 271 272 273 274 275
    @pytest.mark.level0
    @pytest.mark.platform_arm_ascend_training
    @pytest.mark.platform_x86_gpu_training
    @pytest.mark.platform_x86_ascend_training
    @pytest.mark.platform_x86_cpu
    @pytest.mark.env_single
    def test_get_summary_lineage_exception_2(self):
        """Test the interface of get_summary_lineage with exception."""
G
gaocongli 已提交
276 277 278 279 280
        # keys is invalid
        self.assertRaisesRegex(
            LineageParamValueError,
            'Keys must be in',
            get_summary_lineage,
L
Li Hongzhang 已提交
281
            None,
G
gaocongli 已提交
282 283 284 285 286 287 288 289
            SUMMARY_DIR,
            ['metric', 'fake_name']
        )

        self.assertRaisesRegex(
            LineageParamTypeError,
            'Keys must be list.',
            get_summary_lineage,
L
Li Hongzhang 已提交
290
            None,
G
gaocongli 已提交
291 292 293 294 295 296 297 298
            SUMMARY_DIR,
            0
        )

        self.assertRaisesRegex(
            LineageParamTypeError,
            'Keys must be list.',
            get_summary_lineage,
L
Li Hongzhang 已提交
299
            None,
G
gaocongli 已提交
300 301 302 303 304 305 306 307
            SUMMARY_DIR,
            0.1
        )

        self.assertRaisesRegex(
            LineageParamTypeError,
            'Keys must be list.',
            get_summary_lineage,
L
Li Hongzhang 已提交
308
            None,
G
gaocongli 已提交
309 310 311 312 313 314 315 316
            SUMMARY_DIR,
            True
        )

        self.assertRaisesRegex(
            LineageParamTypeError,
            'Element of keys must be str.',
            get_summary_lineage,
L
Li Hongzhang 已提交
317
            None,
G
gaocongli 已提交
318 319 320 321 322 323 324 325
            SUMMARY_DIR,
            [1, 2, 3]
        )

        self.assertRaisesRegex(
            LineageParamTypeError,
            'Keys must be list.',
            get_summary_lineage,
L
Li Hongzhang 已提交
326
            None,
G
gaocongli 已提交
327 328 329 330 331 332 333 334
            SUMMARY_DIR,
            (3, 4)
        )

        self.assertRaisesRegex(
            LineageParamTypeError,
            'Keys must be list.',
            get_summary_lineage,
L
Li Hongzhang 已提交
335
            None,
G
gaocongli 已提交
336 337 338 339 340 341 342 343 344 345 346 347 348
            SUMMARY_DIR,
            {'a': 'b'}
        )

    @pytest.mark.level0
    @pytest.mark.platform_arm_ascend_training
    @pytest.mark.platform_x86_gpu_training
    @pytest.mark.platform_x86_ascend_training
    @pytest.mark.platform_x86_cpu
    @pytest.mark.env_single
    def test_filter_summary_lineage(self):
        """Test the interface of filter_summary_lineage."""
        expect_result = {
349
            'customized': event_data.CUSTOMIZED__1,
G
gaocongli 已提交
350
            'object': [
C
chenchao99 已提交
351 352 353
                LINEAGE_FILTRATION_EXCEPT_RUN,
                LINEAGE_FILTRATION_RUN1,
                LINEAGE_FILTRATION_RUN2
G
gaocongli 已提交
354 355 356 357 358 359 360
            ],
            'count': 3
        }

        search_condition = {
            'sorted_name': 'summary_dir'
        }
L
Li Hongzhang 已提交
361
        res = filter_summary_lineage(None, BASE_SUMMARY_DIR, search_condition)
G
gaocongli 已提交
362 363
        expect_objects = expect_result.get('object')
        for idx, res_object in enumerate(res.get('object')):
364
            expect_objects[idx]['model_lineage']['dataset_mark'] = res_object['model_lineage'].get('dataset_mark')
365
        assert_equal_lineages(expect_result, res, self.assertDictEqual)
G
gaocongli 已提交
366 367

        expect_result = {
368
            'customized': {},
G
gaocongli 已提交
369 370 371
            'object': [],
            'count': 0
        }
L
Li Hongzhang 已提交
372
        res = filter_summary_lineage(None, self.dir_with_empty_lineage)
G
gaocongli 已提交
373 374
        expect_objects = expect_result.get('object')
        for idx, res_object in enumerate(res.get('object')):
375
            expect_objects[idx]['model_lineage']['dataset_mark'] = res_object['model_lineage'].get('dataset_mark')
376
        assert_equal_lineages(expect_result, res, self.assertDictEqual)
G
gaocongli 已提交
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392

    @pytest.mark.level0
    @pytest.mark.platform_arm_ascend_training
    @pytest.mark.platform_x86_gpu_training
    @pytest.mark.platform_x86_ascend_training
    @pytest.mark.platform_x86_cpu
    @pytest.mark.env_single
    def test_filter_summary_lineage_with_condition_1(self):
        """Test the interface of filter_summary_lineage with condition."""
        search_condition = {
            'summary_dir': {
                'in': [
                    SUMMARY_DIR,
                    SUMMARY_DIR_2
                ]
            },
393
            'metric/accuracy': {
G
gaocongli 已提交
394 395 396
                'lt': 3.0,
                'gt': 0.5
            },
397
            'sorted_name': 'metric/accuracy',
G
gaocongli 已提交
398 399 400 401 402
            'sorted_type': 'descending',
            'limit': 3,
            'offset': 0
        }
        expect_result = {
403
            'customized': event_data.CUSTOMIZED__1,
G
gaocongli 已提交
404
            'object': [
C
chenchao99 已提交
405 406
                LINEAGE_FILTRATION_RUN2,
                LINEAGE_FILTRATION_RUN1
G
gaocongli 已提交
407 408 409
            ],
            'count': 2
        }
L
Li Hongzhang 已提交
410
        partial_res = filter_summary_lineage(None, BASE_SUMMARY_DIR, search_condition)
G
gaocongli 已提交
411 412
        expect_objects = expect_result.get('object')
        for idx, res_object in enumerate(partial_res.get('object')):
413
            expect_objects[idx]['model_lineage']['dataset_mark'] = res_object['model_lineage'].get('dataset_mark')
414
        assert_equal_lineages(expect_result, partial_res, self.assertDictEqual)
G
gaocongli 已提交
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430

    @pytest.mark.level0
    @pytest.mark.platform_arm_ascend_training
    @pytest.mark.platform_x86_gpu_training
    @pytest.mark.platform_x86_ascend_training
    @pytest.mark.platform_x86_cpu
    @pytest.mark.env_single
    def test_filter_summary_lineage_with_condition_2(self):
        """Test the interface of filter_summary_lineage with condition."""
        search_condition = {
            'summary_dir': {
                'in': [
                    './run1',
                    './run2'
                ]
            },
431
            'metric/accuracy': {
G
gaocongli 已提交
432 433 434
                'lt': 3.0,
                'gt': 0.5
            },
435
            'sorted_name': 'metric/accuracy',
G
gaocongli 已提交
436 437 438 439 440
            'sorted_type': 'descending',
            'limit': 3,
            'offset': 0
        }
        expect_result = {
441
            'customized': event_data.CUSTOMIZED__1,
G
gaocongli 已提交
442
            'object': [
C
chenchao99 已提交
443 444
                LINEAGE_FILTRATION_RUN2,
                LINEAGE_FILTRATION_RUN1
G
gaocongli 已提交
445 446 447
            ],
            'count': 2
        }
L
Li Hongzhang 已提交
448
        partial_res = filter_summary_lineage(None, BASE_SUMMARY_DIR, search_condition)
G
gaocongli 已提交
449 450
        expect_objects = expect_result.get('object')
        for idx, res_object in enumerate(partial_res.get('object')):
451
            expect_objects[idx]['model_lineage']['dataset_mark'] = res_object['model_lineage'].get('dataset_mark')
452
        assert_equal_lineages(expect_result, partial_res, self.assertDictEqual)
G
gaocongli 已提交
453 454 455 456 457 458 459 460 461 462 463 464 465

    @pytest.mark.level0
    @pytest.mark.platform_arm_ascend_training
    @pytest.mark.platform_x86_gpu_training
    @pytest.mark.platform_x86_ascend_training
    @pytest.mark.platform_x86_cpu
    @pytest.mark.env_single
    def test_filter_summary_lineage_with_condition_3(self):
        """Test the interface of filter_summary_lineage with condition."""
        search_condition1 = {
            'batch_size': {
                'ge': 30
            },
466
            'sorted_name': 'metric/accuracy',
G
gaocongli 已提交
467 468
        }
        expect_result = {
469
            'customized': event_data.CUSTOMIZED__1,
G
gaocongli 已提交
470
            'object': [
C
chenchao99 已提交
471
                LINEAGE_FILTRATION_EXCEPT_RUN,
472 473
                LINEAGE_FILTRATION_RUN1,
                LINEAGE_FILTRATION_RUN2
G
gaocongli 已提交
474
            ],
475
            'count': 3
G
gaocongli 已提交
476
        }
L
Li Hongzhang 已提交
477
        partial_res1 = filter_summary_lineage(None, BASE_SUMMARY_DIR, search_condition1)
G
gaocongli 已提交
478 479
        expect_objects = expect_result.get('object')
        for idx, res_object in enumerate(partial_res1.get('object')):
480
            expect_objects[idx]['model_lineage']['dataset_mark'] = res_object['model_lineage'].get('dataset_mark')
481
        assert_equal_lineages(expect_result, partial_res1, self.assertDictEqual)
G
gaocongli 已提交
482 483 484 485 486

        search_condition2 = {
            'batch_size': {
                'lt': 30
            },
487 488 489
            'lineage_type': {
                'eq': 'model'
            },
G
gaocongli 已提交
490 491
        }
        expect_result = {
492
            'customized': {},
G
gaocongli 已提交
493 494 495
            'object': [],
            'count': 0
        }
L
Li Hongzhang 已提交
496
        partial_res2 = filter_summary_lineage(None, BASE_SUMMARY_DIR, search_condition2)
G
gaocongli 已提交
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
        assert expect_result == partial_res2

    @pytest.mark.level0
    @pytest.mark.platform_arm_ascend_training
    @pytest.mark.platform_x86_gpu_training
    @pytest.mark.platform_x86_ascend_training
    @pytest.mark.platform_x86_cpu
    @pytest.mark.env_single
    def test_filter_summary_lineage_with_lineage_type(self):
        """Test the interface of filter_summary_lineage with lineage_type."""
        summary_dir = os.path.join(BASE_SUMMARY_DIR, 'except_run')
        search_condition = {
            'summary_dir': {
                'in': [summary_dir]
            },
512 513 514
            'lineage_type': {
                'eq': 'dataset'
            },
G
gaocongli 已提交
515 516
        }
        expect_result = {
517
            'customized': {},
G
gaocongli 已提交
518 519 520 521 522 523 524 525
            'object': [
                {
                    'summary_dir': summary_dir,
                    'dataset_graph': DATASET_GRAPH
                }
            ],
            'count': 1
        }
L
Li Hongzhang 已提交
526
        res = filter_summary_lineage(None, BASE_SUMMARY_DIR, search_condition)
G
gaocongli 已提交
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
        assert expect_result == res

    @pytest.mark.level0
    @pytest.mark.platform_arm_ascend_training
    @pytest.mark.platform_x86_gpu_training
    @pytest.mark.platform_x86_ascend_training
    @pytest.mark.platform_x86_cpu
    @pytest.mark.env_single
    def test_filter_summary_lineage_exception_1(self):
        """Test the abnormal execution of the filter_summary_lineage interface."""
        # summary base dir is relative path
        self.assertRaisesRegex(
            LineageParamSummaryPathError,
            'The summary path is invalid.',
            filter_summary_lineage,
L
Li Hongzhang 已提交
542
            None,
G
gaocongli 已提交
543 544 545 546 547 548 549 550
            'relative_path'
        )

        # summary base dir does not exist
        self.assertRaisesRegex(
            LineageParamSummaryPathError,
            'The summary path does not exist or is not a dir.',
            filter_summary_lineage,
L
Li Hongzhang 已提交
551
            None,
G
gaocongli 已提交
552 553 554 555 556 557 558 559
            '/path/does/not/exist'
        )

        # no summary log file under summary_base_dir
        self.assertRaisesRegex(
            LineageFileNotFoundError,
            'There is no summary log file under summary_base_dir.',
            filter_summary_lineage,
L
Li Hongzhang 已提交
560
            None,
G
gaocongli 已提交
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
            self.empty_dir
        )

    @pytest.mark.level0
    @pytest.mark.platform_arm_ascend_training
    @pytest.mark.platform_x86_gpu_training
    @pytest.mark.platform_x86_ascend_training
    @pytest.mark.platform_x86_cpu
    @pytest.mark.env_single
    def test_filter_summary_lineage_exception_2(self):
        """Test the abnormal execution of the filter_summary_lineage interface."""
        # search_condition type error
        search_condition = {
            'summary_dir': 1.0
        }
        self.assertRaisesRegex(
            LineageSearchConditionParamError,
            'The search_condition element summary_dir should be dict.',
            filter_summary_lineage,
L
Li Hongzhang 已提交
580
            None,
G
gaocongli 已提交
581 582 583 584 585 586 587 588 589 590 591 592
            BASE_SUMMARY_DIR,
            search_condition
        )

        # only sorted_type (no sorted_name)
        search_condition = {
            'sorted_type': 'descending'
        }
        self.assertRaisesRegex(
            LineageSearchConditionParamError,
            'The sorted_name have to exist when sorted_type exists.',
            filter_summary_lineage,
L
Li Hongzhang 已提交
593
            None,
G
gaocongli 已提交
594 595 596 597 598 599 600 601 602 603
            BASE_SUMMARY_DIR,
            search_condition
        )

        # search condition is not dict or None
        search_condition = []
        self.assertRaisesRegex(
            LineageSearchConditionParamError,
            'Invalid search_condition type, it should be dict.',
            filter_summary_lineage,
L
Li Hongzhang 已提交
604
            None,
G
gaocongli 已提交
605 606 607 608 609 610 611 612 613 614 615 616
            BASE_SUMMARY_DIR,
            search_condition
        )

        # the condition of limit is invalid
        search_condition = {
            'limit': True
        }
        self.assertRaisesRegex(
            LineageSearchConditionParamError,
            'The limit must be int.',
            filter_summary_lineage,
L
Li Hongzhang 已提交
617
            None,
G
gaocongli 已提交
618 619 620 621
            BASE_SUMMARY_DIR,
            search_condition
        )

C
chenchao99 已提交
622 623 624 625 626 627 628 629
    @pytest.mark.level0
    @pytest.mark.platform_arm_ascend_training
    @pytest.mark.platform_x86_gpu_training
    @pytest.mark.platform_x86_ascend_training
    @pytest.mark.platform_x86_cpu
    @pytest.mark.env_single
    def test_filter_summary_lineage_exception_3(self):
        """Test the abnormal execution of the filter_summary_lineage interface."""
G
gaocongli 已提交
630 631 632 633 634 635 636 637
        # the condition of offset is invalid
        search_condition = {
            'offset': 1.0
        }
        self.assertRaisesRegex(
            LineageSearchConditionParamError,
            'The offset must be int.',
            filter_summary_lineage,
L
Li Hongzhang 已提交
638
            None,
G
gaocongli 已提交
639 640 641 642 643 644 645 646 647 648 649 650 651 652
            BASE_SUMMARY_DIR,
            search_condition
        )

        # the search attribute not supported
        search_condition = {
            'dataset_graph': {
                'le': 1
            }
        }
        self.assertRaisesRegex(
            LineageSearchConditionParamError,
            'The search attribute not supported.',
            filter_summary_lineage,
L
Li Hongzhang 已提交
653
            None,
G
gaocongli 已提交
654 655 656 657
            BASE_SUMMARY_DIR,
            search_condition
        )

C
chenchao99 已提交
658 659 660 661 662 663 664 665
    @pytest.mark.level0
    @pytest.mark.platform_arm_ascend_training
    @pytest.mark.platform_x86_gpu_training
    @pytest.mark.platform_x86_ascend_training
    @pytest.mark.platform_x86_cpu
    @pytest.mark.env_single
    def test_filter_summary_lineage_exception_4(self):
        """Test the abnormal execution of the filter_summary_lineage interface."""
G
gaocongli 已提交
666 667 668 669 670 671 672 673 674
        # the sorted_type not supported
        search_condition = {
            'sorted_name': 'summary_dir',
            'sorted_type': 'xxx'
        }
        self.assertRaisesRegex(
            LineageSearchConditionParamError,
            'The sorted_type must be ascending or descending',
            filter_summary_lineage,
L
Li Hongzhang 已提交
675
            None,
G
gaocongli 已提交
676 677 678 679 680 681 682 683 684 685 686 687 688 689
            BASE_SUMMARY_DIR,
            search_condition
        )

        # the search condition not supported
        search_condition = {
            'batch_size': {
                'dd': 30
            }
        }
        self.assertRaisesRegex(
            LineageSearchConditionParamError,
            'The compare condition should be in',
            filter_summary_lineage,
L
Li Hongzhang 已提交
690
            None,
G
gaocongli 已提交
691 692 693 694 695 696
            BASE_SUMMARY_DIR,
            search_condition
        )

        # the search condition type error
        search_condition = {
697
            'metric/accuracy': {
G
gaocongli 已提交
698 699 700 701 702
                'lt': 'xxx'
            }
        }
        self.assertRaisesRegex(
            LineageSearchConditionParamError,
703
            'The parameter metric/accuracy is invalid.',
G
gaocongli 已提交
704
            filter_summary_lineage,
L
Li Hongzhang 已提交
705
            None,
G
gaocongli 已提交
706 707 708 709
            BASE_SUMMARY_DIR,
            search_condition
        )

C
chenchao99 已提交
710 711 712 713 714 715 716 717
    @pytest.mark.level0
    @pytest.mark.platform_arm_ascend_training
    @pytest.mark.platform_x86_gpu_training
    @pytest.mark.platform_x86_ascend_training
    @pytest.mark.platform_x86_cpu
    @pytest.mark.env_single
    def test_filter_summary_lineage_exception_5(self):
        """Test the abnormal execution of the filter_summary_lineage interface."""
G
gaocongli 已提交
718 719 720 721 722 723 724 725 726 727 728 729
        # the summary dir is invalid in search condition
        search_condition = {
            'summary_dir': {
                'in': [
                    'xxx'
                ]
            }
        }
        self.assertRaisesRegex(
            LineageParamSummaryPathError,
            'The summary path is invalid.',
            filter_summary_lineage,
L
Li Hongzhang 已提交
730
            None,
G
gaocongli 已提交
731 732 733 734 735 736 737 738 739 740
            BASE_SUMMARY_DIR,
            search_condition
        )

    @pytest.mark.level0
    @pytest.mark.platform_arm_ascend_training
    @pytest.mark.platform_x86_gpu_training
    @pytest.mark.platform_x86_ascend_training
    @pytest.mark.platform_x86_cpu
    @pytest.mark.env_single
C
chenchao99 已提交
741
    def test_filter_summary_lineage_exception_6(self):
G
gaocongli 已提交
742 743 744
        """Test the abnormal execution of the filter_summary_lineage interface."""
        # gt > lt
        search_condition1 = {
745
            'metric/accuracy': {
G
gaocongli 已提交
746 747 748 749 750
                'gt': 1,
                'lt': 0.5
            }
        }
        expect_result = {
751
            'customized': {},
G
gaocongli 已提交
752 753 754
            'object': [],
            'count': 0
        }
L
Li Hongzhang 已提交
755
        partial_res1 = filter_summary_lineage(None, BASE_SUMMARY_DIR, search_condition1)
G
gaocongli 已提交
756 757 758 759 760 761 762 763 764 765 766
        assert expect_result == partial_res1

        # the (offset + 1) * limit > count
        search_condition2 = {
            'loss': {
                'lt': 1
            },
            'limit': 1,
            'offset': 4
        }
        expect_result = {
767
            'customized': {},
G
gaocongli 已提交
768
            'object': [],
769
            'count': 2
G
gaocongli 已提交
770
        }
L
Li Hongzhang 已提交
771
        partial_res2 = filter_summary_lineage(None, BASE_SUMMARY_DIR, search_condition2)
G
gaocongli 已提交
772
        assert expect_result == partial_res2
773 774 775 776 777 778 779 780 781

    @pytest.mark.level0
    @pytest.mark.platform_arm_ascend_training
    @pytest.mark.platform_x86_gpu_training
    @pytest.mark.platform_x86_ascend_training
    @pytest.mark.platform_x86_cpu
    @pytest.mark.env_single
    def test_filter_summary_lineage_exception_7(self):
        """Test the abnormal execution of the filter_summary_lineage interface."""
782
        condition_keys = ["summary_dir", "lineage_type", "loss_function", "optimizer", "network", "dataset_mark"]
783 784 785 786 787 788 789 790 791
        for condition_key in condition_keys:
            # the condition type not supported in summary_dir and lineage_type
            search_condition = {
                condition_key: {
                    'lt': '/xxx'
                }
            }
            self.assertRaisesRegex(
                LineageSearchConditionParamError,
792
                f'The parameter {condition_key} is invalid. Its operation should be `in` or `eq`.',
793
                filter_summary_lineage,
L
Li Hongzhang 已提交
794
                None,
795 796 797 798 799 800 801 802 803 804 805 806 807
                BASE_SUMMARY_DIR,
                search_condition
            )

            # more than one operation in summary_dir and lineage_type
            search_condition = {
                condition_key: {
                    'in': ['/xxx', '/yyy'],
                    'eq': '/zzz',
                }
            }
            self.assertRaisesRegex(
                LineageSearchConditionParamError,
808
                f'The parameter {condition_key} is invalid. More than one operation.',
809
                filter_summary_lineage,
L
Li Hongzhang 已提交
810
                None,
811 812 813
                BASE_SUMMARY_DIR,
                search_condition
            )
814 815 816 817 818 819 820 821

    @pytest.mark.level0
    @pytest.mark.platform_arm_ascend_training
    @pytest.mark.platform_x86_gpu_training
    @pytest.mark.platform_x86_ascend_training
    @pytest.mark.platform_x86_cpu
    @pytest.mark.env_single
    def test_filter_summary_lineage_exception_8(self):
822
        """Test the abnormal execution of the filter_summary_lineage interface."""
823 824 825 826
        invalid_lineage_types = ['xxx', None]
        for lineage_type in invalid_lineage_types:
            search_condition = {
                'lineage_type': {
827
                    'eq': lineage_type
828 829 830 831 832 833
                }
            }
            self.assertRaisesRegex(
                LineageSearchConditionParamError,
                "The parameter lineage_type is invalid. It should be 'dataset' or 'model'.",
                filter_summary_lineage,
L
Li Hongzhang 已提交
834
                None,
835 836 837 838 839 840 841 842 843 844 845
                BASE_SUMMARY_DIR,
                search_condition
            )

    @pytest.mark.level0
    @pytest.mark.platform_arm_ascend_training
    @pytest.mark.platform_x86_gpu_training
    @pytest.mark.platform_x86_ascend_training
    @pytest.mark.platform_x86_cpu
    @pytest.mark.env_single
    def test_filter_summary_lineage_exception_9(self):
846
        """Test the abnormal execution of the filter_summary_lineage interface."""
847 848 849 850 851 852 853 854 855
        invalid_sorted_names = ['xxx', 'metric_', 1]
        for sorted_name in invalid_sorted_names:
            search_condition = {
                'sorted_name': sorted_name
            }
            self.assertRaisesRegex(
                LineageSearchConditionParamError,
                'The sorted_name must be in',
                filter_summary_lineage,
L
Li Hongzhang 已提交
856
                None,
857 858 859
                BASE_SUMMARY_DIR,
                search_condition
            )