From 5e43edc474fb8b1068a53df71a9c0873db7b0765 Mon Sep 17 00:00:00 2001 From: jinyaohui Date: Thu, 4 Jun 2020 19:53:40 +0800 Subject: [PATCH] clean pylint --- mindspore/dataset/text/utils.py | 2 +- mindspore/nn/layer/lstm.py | 15 ++++++++------- .../utils/check_gradient.py | 2 +- tests/perf_test/bert/test_bert_train.py | 2 +- tests/st/nccl/test_nccl_all_reduce_op.py | 6 +++--- .../st/networks/models/bert/bert_tdt_lossscale.py | 7 ++++--- tests/st/networks/test_gpu_lenet.py | 3 ++- tests/st/networks/test_gpu_resnet.py | 2 +- tests/st/networks/test_network_main.py | 3 +-- tests/ut/python/nn/test_clip_by_norm.py | 4 ++-- tests/ut/python/ops/test_ops.py | 8 ++++---- tests/ut/python/optimizer/test_debug_location.py | 4 ++-- tests/ut/python/pynative_mode/nn/test_dropout.py | 1 - .../test_graph_return_const_param.py | 6 +++--- tests/ut/python/pynative_mode/test_hook.py | 4 ++-- .../python/pynative_mode/test_insert_grad_of.py | 1 - .../ut/python/pynative_mode/test_parse_method.py | 3 ++- tests/ut/python/pynative_mode/test_training.py | 2 -- tests/ut/python/train/quant/test_quant.py | 5 ----- tests/ut/python/utils/test_callback.py | 6 +++--- tests/ut/python/utils/test_serialize.py | 6 +++--- 21 files changed, 43 insertions(+), 49 deletions(-) diff --git a/mindspore/dataset/text/utils.py b/mindspore/dataset/text/utils.py index f3f442f238..6b77edec9b 100644 --- a/mindspore/dataset/text/utils.py +++ b/mindspore/dataset/text/utils.py @@ -16,8 +16,8 @@ Some basic function for nlp """ from enum import IntEnum -import mindspore._c_dataengine as cde import numpy as np +import mindspore._c_dataengine as cde from .validators import check_from_file, check_from_list, check_from_dict diff --git a/mindspore/nn/layer/lstm.py b/mindspore/nn/layer/lstm.py index 998ca70cb7..5390c67f37 100755 --- a/mindspore/nn/layer/lstm.py +++ b/mindspore/nn/layer/lstm.py @@ -13,15 +13,16 @@ # limitations under the License. # ============================================================================ """lstm""" -from mindspore.ops import operations as P -from mindspore.nn.cell import Cell -from mindspore.common.parameter import Parameter, ParameterTuple -from mindspore.common.initializer import initializer -from mindspore._checkparam import Validator as validator -from mindspore import context +import numpy as np + import mindspore.nn as nn +from mindspore import context +from mindspore._checkparam import Validator as validator +from mindspore.common.initializer import initializer +from mindspore.common.parameter import Parameter, ParameterTuple from mindspore.common.tensor import Tensor -import numpy as np +from mindspore.nn.cell import Cell +from mindspore.ops import operations as P __all__ = ['LSTM', 'LSTMCell'] diff --git a/tests/mindspore_test_framework/utils/check_gradient.py b/tests/mindspore_test_framework/utils/check_gradient.py index cadb0207d6..c2252b8a78 100644 --- a/tests/mindspore_test_framework/utils/check_gradient.py +++ b/tests/mindspore_test_framework/utils/check_gradient.py @@ -18,8 +18,8 @@ from typing import Callable, List, Any -import mindspore._c_expression as _c_expression import numpy as np +import mindspore._c_expression as _c_expression from mindspore import ParameterTuple from mindspore import Tensor diff --git a/tests/perf_test/bert/test_bert_train.py b/tests/perf_test/bert/test_bert_train.py index c2bb88b047..096571adea 100644 --- a/tests/perf_test/bert/test_bert_train.py +++ b/tests/perf_test/bert/test_bert_train.py @@ -22,9 +22,9 @@ import os import mindspore.common.dtype as mstype import mindspore.context as context from mindspore import Tensor -from model_zoo.bert.src import BertConfig, BertNetworkWithLoss, BertTrainOneStepCell, BertTrainOneStepWithLossScaleCell from mindspore.nn.optim import AdamWeightDecayDynamicLR from mindspore.train.loss_scale_manager import DynamicLossScaleManager +from model_zoo.bert.src import BertConfig, BertNetworkWithLoss, BertTrainOneStepCell, BertTrainOneStepWithLossScaleCell from ...dataset_mock import MindData from ...ops_common import nn, np, batch_tuple_tensor, build_construct_graph diff --git a/tests/st/nccl/test_nccl_all_reduce_op.py b/tests/st/nccl/test_nccl_all_reduce_op.py index 13df46254c..dc21a6b438 100644 --- a/tests/st/nccl/test_nccl_all_reduce_op.py +++ b/tests/st/nccl/test_nccl_all_reduce_op.py @@ -91,10 +91,10 @@ class Net2(nn.Cell): self.all_reduce3 = P.AllReduce(self.op2, group=NCCL_WORLD_COMM_GROUP) def construct(self): - x = self.all_reduce1(self.x1) - y = self.all_reduce2(x) + x_ = self.all_reduce1(self.x1) + y = self.all_reduce2(x_) z = self.all_reduce3(y) - return (x, y, z) + return (x_, y, z) def test_AllReduce2(): diff --git a/tests/st/networks/models/bert/bert_tdt_lossscale.py b/tests/st/networks/models/bert/bert_tdt_lossscale.py index 17eece1a6d..ab8eef2e7d 100644 --- a/tests/st/networks/models/bert/bert_tdt_lossscale.py +++ b/tests/st/networks/models/bert/bert_tdt_lossscale.py @@ -17,8 +17,11 @@ import os import time -import pytest + import numpy as np +import pytest +from src.bert_for_pre_training import BertNetworkWithLoss, BertTrainOneStepWithLossScaleCell +from src.bert_model import BertConfig import mindspore.common.dtype as mstype import mindspore.dataset.engine.datasets as de @@ -26,8 +29,6 @@ import mindspore.dataset.transforms.c_transforms as C from mindspore import context from mindspore import log as logger from mindspore.common.tensor import Tensor -from src.bert_model import BertConfig -from src.bert_for_pre_training import BertNetworkWithLoss, BertTrainOneStepWithLossScaleCell from mindspore.nn.optim import Lamb from mindspore.train.callback import Callback from mindspore.train.loss_scale_manager import DynamicLossScaleManager diff --git a/tests/st/networks/test_gpu_lenet.py b/tests/st/networks/test_gpu_lenet.py index d2e1c90981..60d00ff14b 100644 --- a/tests/st/networks/test_gpu_lenet.py +++ b/tests/st/networks/test_gpu_lenet.py @@ -13,8 +13,9 @@ # limitations under the License. # ============================================================================ -import numpy as np import os + +import numpy as np import pytest import mindspore.context as context diff --git a/tests/st/networks/test_gpu_resnet.py b/tests/st/networks/test_gpu_resnet.py index fe74fb63f1..6bd947c712 100644 --- a/tests/st/networks/test_gpu_resnet.py +++ b/tests/st/networks/test_gpu_resnet.py @@ -397,5 +397,5 @@ def test_trainTensor_amp(num_classes=10, epoch=18, batch_size=16): loss = train_network(data, label) losses.append(loss) assert (losses[-1][0].asnumpy() < 1) - assert (losses[-1][1].asnumpy() == False) + assert not losses[-1][1].asnumpy() assert (losses[-1][2].asnumpy() > 1) diff --git a/tests/st/networks/test_network_main.py b/tests/st/networks/test_network_main.py index 3216d25bfe..a05798bfbe 100644 --- a/tests/st/networks/test_network_main.py +++ b/tests/st/networks/test_network_main.py @@ -19,9 +19,8 @@ Usage: python test_network_main.py --net lenet --target Ascend """ import argparse + import numpy as np -import os -import time from models.alexnet import AlexNet from models.lenet import LeNet from models.resnetv1_5 import resnet50 diff --git a/tests/ut/python/nn/test_clip_by_norm.py b/tests/ut/python/nn/test_clip_by_norm.py index 813fada90a..512159974e 100644 --- a/tests/ut/python/nn/test_clip_by_norm.py +++ b/tests/ut/python/nn/test_clip_by_norm.py @@ -35,10 +35,10 @@ def test_clip_by_norm_const(): super(Network, self).__init__() self.norm_value = Tensor(np.array([1]).astype(np.float32)) self.clip = nn.ClipByNorm() - + def construct(self, x): return self.clip(x, self.norm_value) net = Network() x = Tensor(np.array([[-2, 0, 0], [0, 3, 4]]).astype(np.float32)) - output = net(x) + net(x) diff --git a/tests/ut/python/ops/test_ops.py b/tests/ut/python/ops/test_ops.py index f9b7ee6483..70504cffbb 100755 --- a/tests/ut/python/ops/test_ops.py +++ b/tests/ut/python/ops/test_ops.py @@ -14,6 +14,7 @@ # ============================================================================ """ test ops """ import functools + import numpy as np import mindspore.nn as nn @@ -898,7 +899,7 @@ test_case_nn_ops = [ 'skip': ['backward']}), ('BasicLSTMCell', { 'block': P.BasicLSTMCell(keep_prob=1.0, forget_bias=1.0, state_is_tuple=True, activation='tanh'), - 'desc_inputs': [[128, 128], [128, 128], [128, 128], [512, 256, 1, 1],[512, 1, 1, 1]], + 'desc_inputs': [[128, 128], [128, 128], [128, 128], [512, 256, 1, 1], [512, 1, 1, 1]], 'desc_bprop': [[128, 128], [128, 128], [128, 128], [128, 128], [128, 128], [128, 128], [128, 128]], 'skip': []}), ('TopK', { @@ -1324,7 +1325,7 @@ test_case_array_ops = [ 'desc_inputs': [(Tensor(np.array([1], np.float32)), Tensor(np.array([1], np.float32)), Tensor(np.array([1], np.float32)))], - 'desc_bprop': [[3, ]]}), + 'desc_bprop': [[3,]]}), ('Pack_0', { 'block': NetForPackInput(P.Pack()), 'desc_inputs': [[2, 2], [2, 2], [2, 2]], @@ -1486,8 +1487,7 @@ test_case = functools.reduce(lambda x, y: x + y, test_case_lists) test_exec_case = test_case -test_backward_exec_case = filter(lambda x: 'skip' not in x[1] or - 'backward' not in x[1]['skip'], test_case) +test_backward_exec_case = filter(lambda x: 'skip' not in x[1] or 'backward' not in x[1]['skip'], test_case) @non_graph_engine diff --git a/tests/ut/python/optimizer/test_debug_location.py b/tests/ut/python/optimizer/test_debug_location.py index f35a8e3fc3..bdd8e96ca0 100644 --- a/tests/ut/python/optimizer/test_debug_location.py +++ b/tests/ut/python/optimizer/test_debug_location.py @@ -144,7 +144,7 @@ def test_op_forward_infererror(): input_np = np.random.randn(2, 3, 4, 5).astype(np.float32) input_me = Tensor(input_np) net = Net3() - with pytest.raises(TypeError) as e: + with pytest.raises(TypeError): net(input_me) @@ -162,7 +162,7 @@ def test_sequential_resolve_error(): input_np = np.random.randn(2, 3, 4, 5).astype(np.float32) input_me = Tensor(input_np) net = SequenceNet() - with pytest.raises(RuntimeError) as e: + with pytest.raises(RuntimeError): net(input_me) diff --git a/tests/ut/python/pynative_mode/nn/test_dropout.py b/tests/ut/python/pynative_mode/nn/test_dropout.py index f2bfe02c19..3272e92a51 100644 --- a/tests/ut/python/pynative_mode/nn/test_dropout.py +++ b/tests/ut/python/pynative_mode/nn/test_dropout.py @@ -19,7 +19,6 @@ import mindspore.nn as nn from mindspore import Tensor from mindspore import context from mindspore import dtype as mstype -from mindspore.common.api import _executor context.set_context(device_target="Ascend") diff --git a/tests/ut/python/pynative_mode/test_graph_return_const_param.py b/tests/ut/python/pynative_mode/test_graph_return_const_param.py index 67e55fff56..6c55d561d8 100644 --- a/tests/ut/python/pynative_mode/test_graph_return_const_param.py +++ b/tests/ut/python/pynative_mode/test_graph_return_const_param.py @@ -44,7 +44,7 @@ class ChooseInitParameterWithInput(nn.Cell): self.x = Parameter(Tensor(np.ones(2), dtype=mstype.int32), name='x') @ms_function - def construct(self, input): + def construct(self, input_data): return self.x @@ -57,7 +57,7 @@ def test_choose_init_param(): def test_choose_param_with_input(): choose = ChooseInitParameterWithInput() - input = Tensor(np.zeros(2), dtype=mstype.int32) + input_data = Tensor(np.zeros(2), dtype=mstype.int32) expect = Tensor(np.ones(2), dtype=mstype.int32) - out = choose(input) + out = choose(input_data) assert np.allclose(expect.asnumpy(), out.asnumpy()) diff --git a/tests/ut/python/pynative_mode/test_hook.py b/tests/ut/python/pynative_mode/test_hook.py index 6cfab67ce8..35062336c4 100644 --- a/tests/ut/python/pynative_mode/test_hook.py +++ b/tests/ut/python/pynative_mode/test_hook.py @@ -1,10 +1,11 @@ import numpy as np + import mindspore.nn as nn import mindspore.ops.operations as P -from mindspore.ops import composite as C from mindspore import context, Tensor, ParameterTuple from mindspore.common.initializer import TruncatedNormal from mindspore.nn import WithLossCell, Momentum +from mindspore.ops import composite as C context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU") @@ -45,7 +46,6 @@ class LeNet5(nn.Cell): Lenet network Args: num_class (int): Num classes. Default: 10. - Returns: Tensor, output tensor diff --git a/tests/ut/python/pynative_mode/test_insert_grad_of.py b/tests/ut/python/pynative_mode/test_insert_grad_of.py index 9c17c5dcd0..0a28bbbb63 100644 --- a/tests/ut/python/pynative_mode/test_insert_grad_of.py +++ b/tests/ut/python/pynative_mode/test_insert_grad_of.py @@ -21,7 +21,6 @@ from mindspore import Tensor from mindspore import context from mindspore.common.api import ms_function from mindspore.ops import composite as C -from mindspore.ops import functional as F from mindspore.ops import operations as P from ....mindspore_test_framework.utils.bprop_util import bprop from ....mindspore_test_framework.utils.debug_util import PrintShapeTypeCell, PrintGradShapeTypeCell diff --git a/tests/ut/python/pynative_mode/test_parse_method.py b/tests/ut/python/pynative_mode/test_parse_method.py index abbfa6cd3b..8a648ac90e 100644 --- a/tests/ut/python/pynative_mode/test_parse_method.py +++ b/tests/ut/python/pynative_mode/test_parse_method.py @@ -19,9 +19,10 @@ @Desc : test parse the object's method """ import logging +from dataclasses import dataclass + import numpy as np import pytest -from dataclasses import dataclass import mindspore.nn as nn from mindspore import context diff --git a/tests/ut/python/pynative_mode/test_training.py b/tests/ut/python/pynative_mode/test_training.py index 032ae60f60..0d2cb990e9 100644 --- a/tests/ut/python/pynative_mode/test_training.py +++ b/tests/ut/python/pynative_mode/test_training.py @@ -19,9 +19,7 @@ import mindspore.nn as nn from mindspore import context from mindspore.common.tensor import Tensor from mindspore.nn import WithGradCell, WithLossCell -from mindspore.nn.optim import Momentum from mindspore.ops import operations as P -from mindspore.train.model import Model from ..ut_filter import non_graph_engine diff --git a/tests/ut/python/train/quant/test_quant.py b/tests/ut/python/train/quant/test_quant.py index 91698e0138..d11f169e22 100644 --- a/tests/ut/python/train/quant/test_quant.py +++ b/tests/ut/python/train/quant/test_quant.py @@ -13,14 +13,9 @@ # limitations under the License. # ============================================================================ """ tests for quant """ -import numpy as np -from mobilenetv2_combined import MobileNetV2 - import mindspore.context as context -from mindspore import Tensor from mindspore import nn from mindspore.nn.layer import combined -from mindspore.train.quant import quant as qat context.set_context(mode=context.GRAPH_MODE, device_target="GPU") diff --git a/tests/ut/python/utils/test_callback.py b/tests/ut/python/utils/test_callback.py index 26ad1a2277..42504f29c6 100644 --- a/tests/ut/python/utils/test_callback.py +++ b/tests/ut/python/utils/test_callback.py @@ -74,7 +74,7 @@ class LossNet(nn.Cell): return out -def test_Model_Checkpoint_prefix_invalid(): +def test_model_checkpoint_prefix_invalid(): """Test ModelCheckpoint prefix invalid.""" with pytest.raises(ValueError): ModelCheckpoint(123) @@ -338,9 +338,9 @@ def test_step_end_save_graph(): ckpoint_cb.begin(run_context) # import pdb;pdb.set_trace() ckpoint_cb.step_end(run_context) - assert os.path.exists('./test_files/test-graph.meta') == True + assert os.path.exists('./test_files/test-graph.meta') if os.path.exists('./test_files/test-graph.meta'): os.chmod('./test_files/test-graph.meta', stat.S_IWRITE) os.remove('./test_files/test-graph.meta') ckpoint_cb.step_end(run_context) - assert os.path.exists('./test_files/test-graph.meta') == False + assert not os.path.exists('./test_files/test-graph.meta') diff --git a/tests/ut/python/utils/test_serialize.py b/tests/ut/python/utils/test_serialize.py index a248a49bce..0cb8313513 100644 --- a/tests/ut/python/utils/test_serialize.py +++ b/tests/ut/python/utils/test_serialize.py @@ -71,16 +71,16 @@ def setup_module(): def test_save_graph(): """ test_exec_save_graph """ - class Net(nn.Cell): + class Net1(nn.Cell): def __init__(self): - super(Net, self).__init__() + super(Net1, self).__init__() self.add = P.TensorAdd() def construct(self, x, y): z = self.add(x, y) return z - net = Net() + net = Net1() net.set_train() out_me_list = [] x = Tensor(np.random.rand(2, 1, 2, 3).astype(np.float32))