fix pylint warnings

pull/1348/head
Yi Huaijie 5 years ago
parent bd845dd0b7
commit 14fe72f383

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

@ -1,26 +1,26 @@
# Copyright 2020 Huawei Technologies Co., Ltd # Copyright 2020 Huawei Technologies Co., Ltd
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.
# You may obtain a copy of the License at # You may obtain a copy of the License at
# #
# http://www.apache.org/licenses/LICENSE-2.0 # http://www.apache.org/licenses/LICENSE-2.0
# #
# Unless required by applicable law or agreed to in writing, software # Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, # distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
# ============================================================================ # ============================================================================
import os import os
import pytest import pytest
@pytest.mark.level0 @pytest.mark.level0
@pytest.mark.platform_x86_ascend_training @pytest.mark.platform_x86_ascend_training
@pytest.mark.platform_arm_ascend_training @pytest.mark.platform_arm_ascend_training
@pytest.mark.env_single @pytest.mark.env_single
def test_expand_loss(): def test_expand_loss():
sh_path = os.path.split(os.path.realpath(__file__))[0] sh_path = os.path.split(os.path.realpath(__file__))[0]
ret = os.system(f"sh {sh_path}/run_auto_parallel_loss_expand.sh") ret = os.system(f"sh {sh_path}/run_auto_parallel_loss_expand.sh")
assert (ret == 0) assert ret == 0

@ -1,22 +1,21 @@
# Copyright 2020 Huawei Technologies Co., Ltd # Copyright 2020 Huawei Technologies Co., Ltd
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.
# You may obtain a copy of the License at # You may obtain a copy of the License at
# #
# http://www.apache.org/licenses/LICENSE-2.0 # http://www.apache.org/licenses/LICENSE-2.0
# #
# Unless required by applicable law or agreed to in writing, software # Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, # distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
# ============================================================================ # ============================================================================
import os import os
import pytest
def test_expand_loss():
def test_expand_loss(): ret = os.system("sh run_onehot_model_parallel.sh")
ret = os.system("sh run_onehot_model_parallel.sh") assert ret == 0
assert (ret == 0)

@ -13,8 +13,8 @@
# limitations under the License. # limitations under the License.
# ============================================================================ # ============================================================================
import numpy as np
import os import os
import numpy as np
import pytest import pytest
import mindspore.common.dtype as mstype import mindspore.common.dtype as mstype
@ -37,31 +37,29 @@ init()
context.set_auto_parallel_context(mirror_mean=True, parallel_mode=ParallelMode.AUTO_PARALLEL) context.set_auto_parallel_context(mirror_mean=True, parallel_mode=ParallelMode.AUTO_PARALLEL)
def weight_variable(shape, factor=0.1): def weight_variable():
return One() return One()
def _conv3x3(in_channels, out_channels, stride=1, padding=0, pad_mode='same'): def _conv3x3(in_channels, out_channels, stride=1, padding=0, pad_mode='same'):
init_value = weight_variable((out_channels, in_channels, 3, 3)) init_value = weight_variable()
return nn.Conv2d(in_channels, out_channels, return nn.Conv2d(in_channels, out_channels,
kernel_size=3, stride=stride, padding=padding, pad_mode=pad_mode, weight_init=init_value) kernel_size=3, stride=stride, padding=padding, pad_mode=pad_mode, weight_init=init_value)
def _conv1x1(in_channels, out_channels, stride=1, padding=0, pad_mode='same'): def _conv1x1(in_channels, out_channels, stride=1, padding=0, pad_mode='same'):
init_value = weight_variable((out_channels, in_channels, 1, 1)) init_value = weight_variable()
return nn.Conv2d(in_channels, out_channels, return nn.Conv2d(in_channels, out_channels,
kernel_size=1, stride=stride, padding=padding, pad_mode=pad_mode, weight_init=init_value) kernel_size=1, stride=stride, padding=padding, pad_mode=pad_mode, weight_init=init_value)
def _conv7x7(in_channels, out_channels, stride=1, padding=0, pad_mode='same'): def _conv7x7(in_channels, out_channels, stride=1, padding=0, pad_mode='same'):
init_value = weight_variable((out_channels, in_channels, 7, 7)) init_value = weight_variable()
return nn.Conv2d(in_channels, out_channels, return nn.Conv2d(in_channels, out_channels,
kernel_size=7, stride=stride, padding=padding, pad_mode=pad_mode, weight_init=init_value) kernel_size=7, stride=stride, padding=padding, pad_mode=pad_mode, weight_init=init_value)
def _fused_bn(channels, momentum=0.9): def _fused_bn(channels, momentum=0.9):
init_weight = weight_variable((channels,))
init_bias = weight_variable((channels,))
return nn.BatchNorm2d(channels, momentum=momentum) return nn.BatchNorm2d(channels, momentum=momentum)
@ -210,8 +208,8 @@ class ResNet(nn.Cell):
self.mean = P.ReduceMean(keep_dims=True) self.mean = P.ReduceMean(keep_dims=True)
self.end_point = nn.Dense(2048, num_classes, has_bias=True, self.end_point = nn.Dense(2048, num_classes, has_bias=True,
weight_init=weight_variable((num_classes, 2048)), weight_init=weight_variable(),
bias_init=weight_variable((num_classes,))) bias_init=weight_variable())
self.squeeze = P.Squeeze() self.squeeze = P.Squeeze()
self.cast = P.Cast() self.cast = P.Cast()
@ -345,9 +343,8 @@ class Dataset():
raise StopIteration raise StopIteration
self.index += 1 self.index += 1
if self.input_num == 2: if self.input_num == 2:
return self.predict, self.label return (self.predict, self.label)
else: return (self.predict,)
return self.predict,
def reset(self): def reset(self):
self.index = 0 self.index = 0
@ -364,7 +361,7 @@ class ModelCallback(Callback):
super(ModelCallback, self).__init__() super(ModelCallback, self).__init__()
self.loss_list = [] self.loss_list = []
def epoch_end(self, run_context, *args): def epoch_end(self, run_context):
cb_params = run_context.original_args() cb_params = run_context.original_args()
result = cb_params.net_outputs result = cb_params.net_outputs
self.loss_list.append(result.asnumpy().mean()) self.loss_list.append(result.asnumpy().mean())
@ -376,9 +373,9 @@ class ModelCallback(Callback):
def test_train_feed(num_classes=8192): def test_train_feed(num_classes=8192):
set_algo_parameters(elementwise_op_strategy_follow=True) set_algo_parameters(elementwise_op_strategy_follow=True)
parallel_callback = ModelCallback() parallel_callback = ModelCallback()
dataGen = DataGenerator() data_gen = DataGenerator()
input_full, input_part = dataGen.input_data((32 * 2, 3, 224, 224)) _, input_part = data_gen.input_data((32 * 2, 3, 224, 224))
label_full, label_part = dataGen.label_data((32 * 2,)) _, label_part = data_gen.label_data((32 * 2,))
dataset = Dataset(input_part, label_part) dataset = Dataset(input_part, label_part)
net = resnet50(num_classes) net = resnet50(num_classes)
loss = SoftmaxCrossEntropyExpand(sparse=True) loss = SoftmaxCrossEntropyExpand(sparse=True)
@ -396,9 +393,9 @@ def test_train_feed(num_classes=8192):
def test_train_feed2(num_classes=1001): def test_train_feed2(num_classes=1001):
set_algo_parameters(elementwise_op_strategy_follow=True) set_algo_parameters(elementwise_op_strategy_follow=True)
parallel_callback = ModelCallback() parallel_callback = ModelCallback()
dataGen = DataGenerator() data_gen = DataGenerator()
input_full, input_part = dataGen.input_data((32 * 2, 3, 224, 224)) _, input_part = data_gen.input_data((32 * 2, 3, 224, 224))
label_full, label_part = dataGen.label_data((32 * 2,)) _, label_part = data_gen.label_data((32 * 2,))
dataset = Dataset(input_part, label_part) dataset = Dataset(input_part, label_part)
net = resnet50(num_classes) net = resnet50(num_classes)
loss = SoftmaxCrossEntropyExpand(sparse=True) loss = SoftmaxCrossEntropyExpand(sparse=True)

@ -1,17 +1,17 @@
# Copyright 2020 Huawei Technologies Co., Ltd # Copyright 2020 Huawei Technologies Co., Ltd
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.
# You may obtain a copy of the License at # You may obtain a copy of the License at
# #
# http://www.apache.org/licenses/LICENSE-2.0 # http://www.apache.org/licenses/LICENSE-2.0
# #
# Unless required by applicable law or agreed to in writing, software # Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, # distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import sys import sys
sys.path.append("../../..") sys.path.append("../../..")

@ -25,7 +25,6 @@ from mindspore.nn import Dense
from mindspore.nn import Momentum from mindspore.nn import Momentum
from mindspore.nn import ReLU from mindspore.nn import ReLU
from mindspore.nn import TrainOneStepCell, WithLossCell from mindspore.nn import TrainOneStepCell, WithLossCell
from mindspore.ops.operations import Split
from mindspore.ops.operations.comm_ops import AllReduce, AllGather, _AlltoAll, ReduceOp, ReduceScatter from mindspore.ops.operations.comm_ops import AllReduce, AllGather, _AlltoAll, ReduceOp, ReduceScatter
from mindspore.ops.operations.comm_ops import Broadcast from mindspore.ops.operations.comm_ops import Broadcast

@ -16,8 +16,8 @@
@File : test_data_parallel_lenet.py @File : test_data_parallel_lenet.py
@Desc : test data parallel lenet @Desc : test data parallel lenet
""" """
import numpy as np
import os import os
import numpy as np
import mindspore.context as context import mindspore.context as context
import mindspore.nn as nn import mindspore.nn as nn
@ -80,7 +80,6 @@ def test_lenet5_train_step_training_pynative():
context.reset_auto_parallel_context() context.reset_auto_parallel_context()
context.set_auto_parallel_context(parallel_mode=ParallelMode.DATA_PARALLEL, context.set_auto_parallel_context(parallel_mode=ParallelMode.DATA_PARALLEL,
device_num=8, mirror_mean=True) device_num=8, mirror_mean=True)
size = 3
predict = Tensor(np.ones([1, 1, 32, 32]).astype(np.float32) * 0.01) predict = Tensor(np.ones([1, 1, 32, 32]).astype(np.float32) * 0.01)
label = Tensor(np.zeros([1, 10]).astype(np.float32)) label = Tensor(np.zeros([1, 10]).astype(np.float32))
DatasetLenet(predict, label, 2) DatasetLenet(predict, label, 2)

@ -19,7 +19,7 @@ from mindspore.parallel._utils import _reset_op_id
from mindspore.parallel.algo_parameter_config import reset_algo_parameters from mindspore.parallel.algo_parameter_config import reset_algo_parameters
def setup_module(module): def setup_module():
auto_parallel_context().set_enable_all_reduce_fusion(enable_all_reduce_fusion=True) auto_parallel_context().set_enable_all_reduce_fusion(enable_all_reduce_fusion=True)
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend", save_graphs=False) context.set_context(mode=context.GRAPH_MODE, device_target="Ascend", save_graphs=False)
reset_cost_model_context() reset_cost_model_context()

@ -1,120 +1,120 @@
# Copyright 2019 Huawei Technologies Co., Ltd # Copyright 2019 Huawei Technologies Co., Ltd
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.
# You may obtain a copy of the License at # You may obtain a copy of the License at
# #
# http://www.apache.org/licenses/LICENSE-2.0 # http://www.apache.org/licenses/LICENSE-2.0
# #
# Unless required by applicable law or agreed to in writing, software # Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, # distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import numpy as np import os
import os import numpy as np
import mindspore as ms import mindspore as ms
import mindspore.communication.management as distributedTool import mindspore.communication.management as distributedTool
from mindspore import context from mindspore import context
from mindspore.common.tensor import Tensor from mindspore.common.tensor import Tensor
from mindspore.nn import Cell from mindspore.nn import Cell
from mindspore.nn import Dropout from mindspore.nn import Dropout
device_num = 4 device_num = 4
device_id = int(os.environ["RANK_ID"]) device_id = int(os.environ["RANK_ID"])
path = "./output/" path = "./output/"
def setup_module(): def setup_module():
print("~~~~~~~~~~~set up~~~~~~~~~~~~~") print("~~~~~~~~~~~set up~~~~~~~~~~~~~")
context.set_context(mode=context.GRAPH_MODE) context.set_context(mode=context.GRAPH_MODE)
context.set_auto_parallel_context(device_num=device_num, global_rank=device_id) context.set_auto_parallel_context(device_num=device_num, global_rank=device_id)
distributedTool.init() distributedTool.init()
distributedTool.create_group("0-3", [0, 1, 2, 3]) distributedTool.create_group("0-3", [0, 1, 2, 3])
print("~~~~~~~~~~~set up finished~~~~~~~~~~~~~") print("~~~~~~~~~~~set up finished~~~~~~~~~~~~~")
def teardown_module(): def teardown_module():
print("~~~~~~~~~~~~tear down~~~~~~~~~~") print("~~~~~~~~~~~~tear down~~~~~~~~~~")
class Net(Cell): class Net(Cell):
def __init__(self, keep_prob, seed0, seed1, strategy=None): def __init__(self, keep_prob, seed0, seed1, strategy=None):
super(Net, self).__init__() super(Net, self).__init__()
self.drop = Dropout(keep_prob, seed0, seed1, dtype=ms.float32, strategy=strategy) self.drop = Dropout(keep_prob, seed0, seed1, dtype=ms.float32, strategy=strategy)
def construct(self, input): def construct(self, input_):
x = self.drop(input) x = self.drop(input_)
return x return x
# pylint: disable=comparison-with-itself # pylint: disable=comparison-with-itself
class DropoutFactory: class DropoutFactory:
def __init__(self, input_shape, keep_prob, seed0, seed1, strategy0=None): def __init__(self, input_shape, keep_prob, seed0, seed1, strategy0=None):
size = 1 size = 1
prefix = "" prefix = ""
for s in input_shape: for s in input_shape:
prefix = prefix + str(s) prefix = prefix + str(s)
size = size * s size = size * s
self.prefix = prefix self.prefix = prefix
number_range = min(10, size) number_range = min(10, size)
self.input_np = np.reshape(np.arange(0, size) % number_range, input_shape).astype(np.float32) self.input_np = np.reshape(np.arange(0, size) % number_range, input_shape).astype(np.float32)
self.keep_prob = keep_prob self.keep_prob = keep_prob
self.seed0 = seed0 self.seed0 = seed0
self.seed1 = seed1 self.seed1 = seed1
self.strategy0 = strategy0 self.strategy0 = strategy0
need_dev_num = 1 need_dev_num = 1
for s in strategy0[1]: for s in strategy0[1]:
need_dev_num = need_dev_num * s need_dev_num = need_dev_num * s
self.x_id = device_id % need_dev_num self.x_id = device_id % need_dev_num
self.out_id = device_id % need_dev_num self.out_id = device_id % need_dev_num
def get_parallel_blocks(self, input_, strategy): def get_parallel_blocks(self, input_, strategy):
blocks = [input_] blocks = [input_]
i = 0 i = 0
for stra in strategy: for stra in strategy:
temp = [] temp = []
while len(blocks) > 0: while len(blocks) > 0:
block = blocks.pop(0) block = blocks.pop(0)
temp.extend(np.split(block, stra, axis=i)) temp.extend(np.split(block, stra, axis=i))
blocks.extend(temp) blocks.extend(temp)
i += 1 i += 1
return blocks return blocks
def d4_tensor_compare(self, input, out_me): def d4_tensor_compare(self, input_, out_me):
[a, b, c, d] = input.shape [a, b, c, d] = input_.shape
for i in range(a): for i in range(a):
for j in range(b): for j in range(b):
for k in range(c): for k in range(c):
for e in range(d): for e in range(d):
if out_me[i, j, k, e] == 0: if out_me[i, j, k, e] == 0:
assert True == True assert True
else: else:
assert np.allclose(out_me[i, j, k, e], input[i, j, k, e] * (1 / 0.4), 0.0001, 0.0001) assert np.allclose(out_me[i, j, k, e], input_[i, j, k, e] * (1 / 0.4), 0.0001, 0.0001)
def forward_mindspore_parallel_impl(self): def forward_mindspore_parallel_impl(self):
x = Tensor(self.input_np) x = Tensor(self.input_np)
inputs_x = self.get_parallel_blocks(self.input_np, self.strategy0[1]) inputs_x = self.get_parallel_blocks(self.input_np, self.strategy0[1])
x1 = Tensor(inputs_x[self.x_id]) x1 = Tensor(inputs_x[self.x_id])
net = Net(0.4, 0, 0, strategy=self.strategy0) net = Net(0.4, 0, 0, strategy=self.strategy0)
context.set_auto_parallel_context(parallel_mode="semi_auto_parallel") context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
net.set_auto_parallel() net.set_auto_parallel()
out = net(x, parallel_inputs_compile=[x], parallel_inputs_run=[x1]) out = net(x, parallel_inputs_compile=[x], parallel_inputs_run=[x1])
return out.asnumpy() return out.asnumpy()
def forward_cmp(self): def forward_cmp(self):
out_mindspore_parallel = self.forward_mindspore_parallel_impl() out_mindspore_parallel = self.forward_mindspore_parallel_impl()
input_blocks = self.get_parallel_blocks(self.input_np, self.strategy0[1]) input_blocks = self.get_parallel_blocks(self.input_np, self.strategy0[1])
self.d4_tensor_compare(input_blocks[self.out_id], out_mindspore_parallel) self.d4_tensor_compare(input_blocks[self.out_id], out_mindspore_parallel)
def test_reid_dropout_forward_seed_F32_64_512_8_8(): def test_reid_dropout_forward_seed_F32_64_512_8_8():
fact = DropoutFactory(input_shape=(64, 512, 8, 8), keep_prob=0.4, seed0=0, seed1=0, strategy0=(0, (4, 1, 1, 1))) fact = DropoutFactory(input_shape=(64, 512, 8, 8), keep_prob=0.4, seed0=0, seed1=0, strategy0=(0, (4, 1, 1, 1)))
fact.forward_cmp() fact.forward_cmp()
def test_reid_dropout_forward_seed_F32_64_512_8_8_repeat(): def test_reid_dropout_forward_seed_F32_64_512_8_8_repeat():
fact = DropoutFactory(input_shape=(64, 512, 8, 8), keep_prob=0.4, seed0=0, seed1=0, strategy0=(0, (2, 1, 1, 1))) fact = DropoutFactory(input_shape=(64, 512, 8, 8), keep_prob=0.4, seed0=0, seed1=0, strategy0=(0, (2, 1, 1, 1)))
fact.forward_cmp() fact.forward_cmp()

@ -54,7 +54,7 @@ class Grad(nn.Cell):
return C.grad_all(self.network)(x, y) return C.grad_all(self.network)(x, y)
def compile(net, x, y): def compile_net(net, x, y):
net.set_auto_parallel() net.set_auto_parallel()
_executor.compile(net, x, y) _executor.compile(net, x, y)
@ -69,7 +69,7 @@ def test_add_relu_stride_slice():
x = Tensor(np.ones([128, 32]), dtype=ms.float32) x = Tensor(np.ones([128, 32]), dtype=ms.float32)
y = Tensor(np.ones([128, 32]), dtype=ms.float32) y = Tensor(np.ones([128, 32]), dtype=ms.float32)
compile(net, x, y) compile_net(net, x, y)
def test_add_relu_all_gather(): def test_add_relu_all_gather():
@ -82,4 +82,4 @@ def test_add_relu_all_gather():
x = Tensor(np.ones([128, 32]), dtype=ms.float32) x = Tensor(np.ones([128, 32]), dtype=ms.float32)
y = Tensor(np.ones([128, 32]), dtype=ms.float32) y = Tensor(np.ones([128, 32]), dtype=ms.float32)
compile(net, x, y) compile_net(net, x, y)

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save