From 5b769dfb204911ffa59e21b3fb75ed22de35c46c Mon Sep 17 00:00:00 2001 From: chenzomi Date: Fri, 16 Oct 2020 10:39:49 +0800 Subject: [PATCH] [ME] Bug fix --- mindspore/nn/layer/image.py | 3 +- mindspore/train/dataset_helper.py | 4 +- mindspore/train/model.py | 4 +- .../cv/resnet_thor/src/dataset_helper.py | 4 +- .../nlp/bert_thor/src/dataset_helper.py | 4 +- .../official/nlp/bert_thor/src/model_thor.py | 4 +- .../components/executor/check_exceptions.py | 50 ------------------- .../pipeline/forward/compile_forward.py | 3 +- .../pipeline/forward/verify_exception.py | 3 +- tests/ut/python/nn/test_checkparameter.py | 48 +++++++----------- .../pynative_mode/nn/test_checkparameter.py | 10 ++-- 11 files changed, 37 insertions(+), 100 deletions(-) delete mode 100644 tests/mindspore_test_framework/components/executor/check_exceptions.py diff --git a/mindspore/nn/layer/image.py b/mindspore/nn/layer/image.py index a4846d2da91..cfafd8b5d43 100644 --- a/mindspore/nn/layer/image.py +++ b/mindspore/nn/layer/image.py @@ -451,7 +451,8 @@ class CentralCrop(Cell): def __init__(self, central_fraction): super(CentralCrop, self).__init__() validator.check_value_type("central_fraction", central_fraction, [float], self.cls_name) - self.central_fraction = validator.check_float_range(0.0, 1.0, Rel.INC_RIGHT, 'central_fraction', central_fraction, self.cls_name) + self.central_fraction = validator.check_float_range(central_fraction, 0.0, 1.0, Rel.INC_RIGHT, + 'central_fraction', self.cls_name) self.slice = P.Slice() def construct(self, image): diff --git a/mindspore/train/dataset_helper.py b/mindspore/train/dataset_helper.py index 34b1d87027d..153cf05cea4 100644 --- a/mindspore/train/dataset_helper.py +++ b/mindspore/train/dataset_helper.py @@ -16,7 +16,7 @@ import math import os -from mindspore._checkparam import Validator, check_int +from mindspore._checkparam import Validator from .. import context, nn from ._utils import _exec_datagraph, _get_types_and_shapes, _construct_tensor_list from ..nn.wrap import GetNextSingleOp @@ -124,7 +124,7 @@ class DatasetHelper: def __init__(self, dataset, dataset_sink_mode=True, sink_size=-1, epoch_num=1): dataset_sink_mode = Validator.check_bool(dataset_sink_mode) - check_int(sink_size) + Validator.check_is_int(sink_size) if sink_size < -1 or sink_size == 0: raise ValueError("The sink_size must be -1 or positive, but got sink_size {}.".format(sink_size)) diff --git a/mindspore/train/model.py b/mindspore/train/model.py index 3d8c3c73192..0ef0a766a52 100755 --- a/mindspore/train/model.py +++ b/mindspore/train/model.py @@ -22,7 +22,7 @@ import numpy as np from mindspore import log as logger from ..common.tensor import Tensor from ..nn.metrics import get_metrics -from .._checkparam import check_input_data, check_output_data, Validator, check_int +from .._checkparam import check_input_data, check_output_data, Validator from .callback import _InternalCallbackParam, RunContext, _CallbackManager from .. import context from ..parallel._utils import _get_parallel_mode, _get_device_num, _get_global_rank, \ @@ -551,7 +551,7 @@ class Model: dataset_sink_mode = Validator.check_bool(dataset_sink_mode) if sink_size == -1: sink_size = train_dataset.get_dataset_size() - check_int(sink_size) + Validator.check_is_int(sink_size) if sink_size < -1 or sink_size == 0: raise ValueError("The sink_size must be -1 or positive, but got sink_size {}.".format(sink_size)) diff --git a/model_zoo/official/cv/resnet_thor/src/dataset_helper.py b/model_zoo/official/cv/resnet_thor/src/dataset_helper.py index 6deb490bc3b..5fe046a258a 100644 --- a/model_zoo/official/cv/resnet_thor/src/dataset_helper.py +++ b/model_zoo/official/cv/resnet_thor/src/dataset_helper.py @@ -15,7 +15,7 @@ """Dataset help for minddata dataset""" import math import os -from mindspore._checkparam import Validator, check_int +from mindspore._checkparam import Validator from mindspore import context from mindspore.train._utils import _exec_datagraph, _get_types_and_shapes from mindspore.nn.wrap import GetNextSingleOp @@ -62,7 +62,7 @@ class DatasetHelper: def __init__(self, dataset, dataset_sink_mode=True, sink_size=-1, epoch_num=1, iter_first_order=1): dataset_sink_mode = Validator.check_bool(dataset_sink_mode) - check_int(sink_size) + Validator.check_is_int(sink_size) if sink_size < -1 or sink_size == 0: raise ValueError("The sink_size must be -1 or positive, but got sink_size {}.".format(sink_size)) diff --git a/model_zoo/official/nlp/bert_thor/src/dataset_helper.py b/model_zoo/official/nlp/bert_thor/src/dataset_helper.py index 1c4e65dbadb..2acda88f856 100644 --- a/model_zoo/official/nlp/bert_thor/src/dataset_helper.py +++ b/model_zoo/official/nlp/bert_thor/src/dataset_helper.py @@ -16,7 +16,7 @@ import os from mindspore import context -from mindspore._checkparam import Validator, check_int +from mindspore._checkparam import Validator from mindspore.parallel._utils import _get_device_num, _need_to_full, _to_full_shapes from mindspore.train._utils import _exec_datagraph, _get_types_and_shapes @@ -59,7 +59,7 @@ class DatasetHelper: def __init__(self, dataset, dataset_sink_mode=True, sink_size=-1, epoch_num=1, iter_first_order=0): dataset_sink_mode = Validator.check_bool(dataset_sink_mode) - check_int(sink_size) + Validator.check_is_int(sink_size) if sink_size < -1 or sink_size == 0: raise ValueError("The sink_size must be -1 or positive, but got sink_size {}.".format(sink_size)) diff --git a/model_zoo/official/nlp/bert_thor/src/model_thor.py b/model_zoo/official/nlp/bert_thor/src/model_thor.py index 0b2f24020d5..7a197da4f7d 100644 --- a/model_zoo/official/nlp/bert_thor/src/model_thor.py +++ b/model_zoo/official/nlp/bert_thor/src/model_thor.py @@ -22,7 +22,7 @@ from mindspore._c_expression import init_exec_dataset from mindspore import context from mindspore import log as logger from mindspore import nn -from mindspore._checkparam import check_input_data, check_output_data, Validator, check_int +from mindspore._checkparam import check_input_data, check_output_data, Validator from mindspore.common import dtype as mstype from mindspore.common.dtype import pytype_to_dtype from mindspore.common.tensor import Tensor @@ -604,7 +604,7 @@ class Model: >>> model.train(2, dataset) """ dataset_sink_mode = Validator.check_bool(dataset_sink_mode) - check_int(sink_size) + Validator.check_is_int(sink_size) if sink_size < -1 or sink_size == 0: raise ValueError("The sink_size must be -1 or positive, but got sink_size {}.".format(sink_size)) diff --git a/tests/mindspore_test_framework/components/executor/check_exceptions.py b/tests/mindspore_test_framework/components/executor/check_exceptions.py deleted file mode 100644 index 29f7d136c25..00000000000 --- a/tests/mindspore_test_framework/components/executor/check_exceptions.py +++ /dev/null @@ -1,50 +0,0 @@ -# Copyright 2020 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. -# ============================================================================ - -"""Component that Check if the function raises the expected Exception.""" - -import sys - -import pytest - -from ...components.icomponent import IExectorComponent -from ...utils import keyword - - -class CheckExceptionsEC(IExectorComponent): - """ - Check if the function raises the expected Exception and the error message contains specified keywords if not None. - - Examples: - { - 'block': f, - 'exception': Exception, - 'error_keywords': ['TensorAdd', 'shape'] - } - """ - - def __call__(self): - f = self.function[keyword.block] - args = self.inputs[keyword.desc_inputs] - e = self.function.get(keyword.exception, Exception) - error_kws = self.function.get(keyword.error_keywords, None) - try: - with pytest.raises(e) as exec_info: - f(*args) - except: - raise Exception(f"Expect {e}, but got {sys.exc_info()[0]}") - if error_kws and any(keyword not in str(exec_info.value) for keyword in error_kws): - raise ValueError('Error message `{}` does not contain all keywords `{}`'.format( - str(exec_info.value), error_kws)) diff --git a/tests/mindspore_test_framework/pipeline/forward/compile_forward.py b/tests/mindspore_test_framework/pipeline/forward/compile_forward.py index 467d1072009..b88fda7e4ea 100644 --- a/tests/mindspore_test_framework/pipeline/forward/compile_forward.py +++ b/tests/mindspore_test_framework/pipeline/forward/compile_forward.py @@ -15,7 +15,6 @@ """Pipelines for forward computing.""" -from ...components.executor.check_exceptions import CheckExceptionsEC from ...components.executor.exec_forward import IdentityEC from ...components.facade.me_facade import MeFacadeFC from ...components.function.compile_block import CompileBlockBC @@ -61,4 +60,4 @@ pipeline_for_compile_forward_ge_graph_for_case_by_case_config = [MeFacadeFC, Gen IdCartesianProductFIPC, IdentityEC] pipeline_for_compile_forward_ge_graph_for_case_by_case_config_exception = [MeFacadeFC, GenerateFromShapeDC, RunBlockBC, - IdCartesianProductFIPC, CheckExceptionsEC] + IdCartesianProductFIPC] diff --git a/tests/mindspore_test_framework/pipeline/forward/verify_exception.py b/tests/mindspore_test_framework/pipeline/forward/verify_exception.py index ee9e3d4a0d7..9b9b3524866 100644 --- a/tests/mindspore_test_framework/pipeline/forward/verify_exception.py +++ b/tests/mindspore_test_framework/pipeline/forward/verify_exception.py @@ -15,7 +15,6 @@ """Pipelines for exception checking.""" -from ...components.executor.check_exceptions import CheckExceptionsEC from ...components.facade.me_facade import MeFacadeFC from ...components.function.get_function_from_config import IdentityBC from ...components.function_inputs_policy.cartesian_product_on_id_for_function_inputs import IdCartesianProductFIPC @@ -34,4 +33,4 @@ Example: ] """ pipeline_for_verify_exception_for_case_by_case_config = [MeFacadeFC, IdentityDC, IdentityBC, - IdCartesianProductFIPC, CheckExceptionsEC] + IdCartesianProductFIPC] diff --git a/tests/ut/python/nn/test_checkparameter.py b/tests/ut/python/nn/test_checkparameter.py index e6b67b67aea..fc72a31bb68 100644 --- a/tests/ut/python/nn/test_checkparameter.py +++ b/tests/ut/python/nn/test_checkparameter.py @@ -15,27 +15,15 @@ """ test checkparameter """ import pytest import numpy as np -from mindspore._checkparam import check_input_format, Validator, twice, Rel +from mindspore._checkparam import twice, Validator kernel_size = 5 kernel_size1 = twice(kernel_size) assert kernel_size1 == (5, 5) -def test_check_integer1(): - with pytest.raises(TypeError): - Validator.check_integer("input", 0, Rel.GE, "number") - -def test_check_integer2(): - with pytest.raises(ValueError): - Validator.check_integer(-1, 0, Rel.GE, "number") - -def test_check_integer3(): - input = np.random.randint(0, 100) - assert Validator.check_integer(input, 0, Rel.GE, "number") == input - def test_check_int1(): - input = np.random.randint(-100, 100) - assert Validator.check_is_int(input) == input + a = np.random.randint(-100, 100) + assert Validator.check_is_int(a) == a def test_check_int2(): with pytest.raises(TypeError): @@ -56,16 +44,16 @@ def test_check_is_int5(): Validator.check_is_int(False) def test_check_positive_int1(): - input = np.random.randint(0, 100) - assert Validator.check_positive_int(input) == input + a = np.random.randint(0, 100) + assert Validator.check_positive_int(a) == a def test_check_positive_int2(): - input = np.random.randint(-100, 0) + a = np.random.randint(-100, 0) with pytest.raises(ValueError): - Validator.check_positive_int(input) + Validator.check_positive_int(a) def test_check_positive_int3(): - with pytest.raises(ValueError): + with pytest.raises(TypeError): Validator.check_positive_int(3.3) def test_check_positive_int4(): @@ -73,16 +61,16 @@ def test_check_positive_int4(): Validator.check_positive_int("str") def test_check_negative_int1(): - input = np.random.randint(-100, -1) - assert Validator.check_negative_int(input) == input + a = np.random.randint(-100, -1) + assert Validator.check_negative_int(a) == a def test_check_negative_int2(): - input = np.random.randint(0, 100) + a = np.random.randint(0, 100) with pytest.raises(ValueError): - Validator.check_negative_int(input) + Validator.check_negative_int(a) def test_check_negative_int3(): - with pytest.raises(ValueError): + with pytest.raises(TypeError): Validator.check_negative_int(3.3) def test_check_negative_int4(): @@ -90,16 +78,16 @@ def test_check_negative_int4(): Validator.check_negative_int("str") def test_check_non_positive_int1(): - input = np.random.randint(-100, 0) - assert Validator.check_non_positive_int(input) == input + a = np.random.randint(-100, 0) + assert Validator.check_non_positive_int(a) == a def test_check_non_positive_int2(): - input = np.random.randint(1, 100) + a = np.random.randint(1, 100) with pytest.raises(ValueError): - Validator.check_non_positive_int(input) + Validator.check_non_positive_int(a) def test_check_non_positive_int3(): - with pytest.raises(ValueError): + with pytest.raises(TypeError): Validator.check_non_positive_int(3.3) def test_check_non_positive_int4(): diff --git a/tests/ut/python/pynative_mode/nn/test_checkparameter.py b/tests/ut/python/pynative_mode/nn/test_checkparameter.py index 048c4a71189..4fb0f30777a 100644 --- a/tests/ut/python/pynative_mode/nn/test_checkparameter.py +++ b/tests/ut/python/pynative_mode/nn/test_checkparameter.py @@ -15,7 +15,7 @@ """ test_checkparameter """ import pytest -from mindspore._checkparam import check_int, Validator, check_input_format, _expand_tuple +from mindspore._checkparam import Validator, check_input_format, _expand_tuple once = _expand_tuple(1) twice = _expand_tuple(2) @@ -26,7 +26,7 @@ assert kernel_size1 == (5, 5) def test_check_int_1(): - assert check_int(3) == 3 + assert Validator.check_is_int(3) == 3 def check_int_positive_1(): @@ -45,17 +45,17 @@ def test_NCHW3(): def test_check_int_2(): with pytest.raises(TypeError): - check_int(3.3) + Validator.check_is_int(3.3) def test_check_int_3(): with pytest.raises(TypeError): - check_int("str") + Validator.check_is_int("str") def test_check_int_4(): with pytest.raises(TypeError): - check_int(True) + Validator.check_is_int(True) def test_check_bool_1():