forked from mindspore-Ecosystem/mindspore
clean gate unused st/tbe_network case
This commit is contained in:
parent
1953a7a09a
commit
6f701d6474
|
@ -26,7 +26,7 @@ from mindspore import Tensor
|
|||
from mindspore.nn import TrainOneStepCell, WithLossCell
|
||||
from mindspore.nn.optim import Momentum
|
||||
from mindspore.ops import operations as P
|
||||
from tests.st.tbe_networks.resnet import resnet50
|
||||
from tests.st.networks.models.resnetv1_5 import resnet50
|
||||
|
||||
context.set_context(mode=context.GRAPH_MODE, memory_optimize_level='O0')
|
||||
|
||||
|
|
|
@ -1,20 +0,0 @@
|
|||
#!/bin/bash
|
||||
# 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.
|
||||
# ============================================================================
|
||||
export LOCAL_HIAI=/usr/local/Ascend
|
||||
export TBE_IMPL_PATH=${LOCAL_HIAI}/latest/opp/built-in/op_impl/ai_core/tbe/impl/
|
||||
export LD_LIBRARY_PATH=${LOCAL_HIAI}/latest/lib64/:${LD_LIBRARY_PATH}
|
||||
export PATH=${LOCAL_HIAI}/latest/compiler/ccec_compiler/bin/:${PATH}
|
||||
export PYTHONPATH=${LOCAL_HIAI}/latest/opp/built-in/op_impl/ai_core/tbe/:${PYTHONPATH}
|
|
@ -1,27 +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.
|
||||
# ============================================================================
|
||||
import numpy as np
|
||||
from resnet_torch import resnet50
|
||||
|
||||
from mindspore import Tensor
|
||||
from mindspore.train.serialization import context, export
|
||||
|
||||
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
|
||||
|
||||
|
||||
def test_resnet50_export(batch_size=1, num_classes=5):
|
||||
input_np = np.random.uniform(0.0, 1.0, size=[batch_size, 3, 224, 224]).astype(np.float32)
|
||||
net = resnet50(batch_size, num_classes)
|
||||
export(net, Tensor(input_np), file_name="./me_resnet50.pb", file_format="AIR")
|
|
@ -1,43 +0,0 @@
|
|||
{
|
||||
"board_id": "0x3000",
|
||||
"chip_info": "910",
|
||||
"deploy_mode": "lab",
|
||||
"group_count": "1",
|
||||
"group_list": [
|
||||
{
|
||||
"device_num": "2",
|
||||
"server_num": "1",
|
||||
"group_name": "",
|
||||
"instance_count": "2",
|
||||
"instance_list": [
|
||||
{
|
||||
"devices": [
|
||||
{
|
||||
"device_id": "0",
|
||||
"device_ip": "[device_ip]"
|
||||
}
|
||||
],
|
||||
"rank_id": "0",
|
||||
"server_id": "[sever_id]"
|
||||
},
|
||||
{
|
||||
"devices": [
|
||||
{
|
||||
"device_id": "1",
|
||||
"device_ip": "[device_ip]"
|
||||
}
|
||||
],
|
||||
"rank_id": "1",
|
||||
"server_id": "[sever_id]"
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"para_plane_nic_location": "device",
|
||||
"para_plane_nic_name": [
|
||||
"eth0",
|
||||
"eth1"
|
||||
],
|
||||
"para_plane_nic_num": "2",
|
||||
"status": "completed"
|
||||
}
|
|
@ -1,43 +0,0 @@
|
|||
{
|
||||
"board_id": "0x0000",
|
||||
"chip_info": "910",
|
||||
"deploy_mode": "lab",
|
||||
"group_count": "1",
|
||||
"group_list": [
|
||||
{
|
||||
"device_num": "2",
|
||||
"server_num": "1",
|
||||
"group_name": "",
|
||||
"instance_count": "2",
|
||||
"instance_list": [
|
||||
{
|
||||
"devices": [
|
||||
{
|
||||
"device_id": "0",
|
||||
"device_ip": "[device_ip]"
|
||||
}
|
||||
],
|
||||
"rank_id": "0",
|
||||
"server_id": "[sever_id]"
|
||||
},
|
||||
{
|
||||
"devices": [
|
||||
{
|
||||
"device_id": "1",
|
||||
"device_ip": "[device_ip]"
|
||||
}
|
||||
],
|
||||
"rank_id": "1",
|
||||
"server_id": "[sever_id]"
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"para_plane_nic_location": "device",
|
||||
"para_plane_nic_name": [
|
||||
"eth0",
|
||||
"eth1"
|
||||
],
|
||||
"para_plane_nic_num": "2",
|
||||
"status": "completed"
|
||||
}
|
|
@ -1,281 +0,0 @@
|
|||
# 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.
|
||||
# ============================================================================
|
||||
import numpy as np
|
||||
|
||||
import mindspore.nn as nn
|
||||
from mindspore import Tensor
|
||||
from mindspore.ops import operations as P
|
||||
|
||||
|
||||
def weight_variable_0(shape):
|
||||
zeros = np.zeros(shape).astype(np.float32)
|
||||
return Tensor(zeros)
|
||||
|
||||
|
||||
def weight_variable_1(shape):
|
||||
ones = np.ones(shape).astype(np.float32)
|
||||
return Tensor(ones)
|
||||
|
||||
|
||||
def conv3x3(in_channels, out_channels, stride=1, padding=0):
|
||||
"""3x3 convolution """
|
||||
return nn.Conv2d(in_channels, out_channels,
|
||||
kernel_size=3, stride=stride, padding=padding, weight_init='XavierUniform',
|
||||
has_bias=False, pad_mode="same")
|
||||
|
||||
|
||||
def conv1x1(in_channels, out_channels, stride=1, padding=0):
|
||||
"""1x1 convolution"""
|
||||
return nn.Conv2d(in_channels, out_channels,
|
||||
kernel_size=1, stride=stride, padding=padding, weight_init='XavierUniform',
|
||||
has_bias=False, pad_mode="same")
|
||||
|
||||
|
||||
def conv7x7(in_channels, out_channels, stride=1, padding=0):
|
||||
"""1x1 convolution"""
|
||||
return nn.Conv2d(in_channels, out_channels,
|
||||
kernel_size=7, stride=stride, padding=padding, weight_init='Uniform',
|
||||
has_bias=False, pad_mode="same")
|
||||
|
||||
|
||||
def bn_with_initialize(out_channels):
|
||||
shape = (out_channels)
|
||||
mean = weight_variable_0(shape)
|
||||
var = weight_variable_1(shape)
|
||||
beta = weight_variable_0(shape)
|
||||
bn = nn.BatchNorm2d(out_channels, momentum=0.99, eps=0.00001, gamma_init='Uniform',
|
||||
beta_init=beta, moving_mean_init=mean, moving_var_init=var)
|
||||
return bn
|
||||
|
||||
|
||||
def bn_with_initialize_last(out_channels):
|
||||
shape = (out_channels)
|
||||
mean = weight_variable_0(shape)
|
||||
var = weight_variable_1(shape)
|
||||
beta = weight_variable_0(shape)
|
||||
bn = nn.BatchNorm2d(out_channels, momentum=0.99, eps=0.00001, gamma_init='Uniform',
|
||||
beta_init=beta, moving_mean_init=mean, moving_var_init=var)
|
||||
return bn
|
||||
|
||||
|
||||
def fc_with_initialize(input_channels, out_channels):
|
||||
return nn.Dense(input_channels, out_channels, weight_init='XavierUniform', bias_init='Uniform')
|
||||
|
||||
|
||||
class ResidualBlock(nn.Cell):
|
||||
expansion = 4
|
||||
|
||||
def __init__(self,
|
||||
in_channels,
|
||||
out_channels,
|
||||
stride=1):
|
||||
super(ResidualBlock, self).__init__()
|
||||
|
||||
out_chls = out_channels // self.expansion
|
||||
self.conv1 = conv1x1(in_channels, out_chls, stride=stride, padding=0)
|
||||
self.bn1 = bn_with_initialize(out_chls)
|
||||
|
||||
self.conv2 = conv3x3(out_chls, out_chls, stride=1, padding=0)
|
||||
self.bn2 = bn_with_initialize(out_chls)
|
||||
|
||||
self.conv3 = conv1x1(out_chls, out_channels, stride=1, padding=0)
|
||||
self.bn3 = bn_with_initialize_last(out_channels)
|
||||
|
||||
self.relu = P.ReLU()
|
||||
self.add = P.Add()
|
||||
|
||||
def construct(self, x):
|
||||
identity = x
|
||||
|
||||
out = self.conv1(x)
|
||||
out = self.bn1(out)
|
||||
out = self.relu(out)
|
||||
|
||||
out = self.conv2(out)
|
||||
out = self.bn2(out)
|
||||
out = self.relu(out)
|
||||
|
||||
out = self.conv3(out)
|
||||
out = self.bn3(out)
|
||||
|
||||
out = self.add(out, identity)
|
||||
out = self.relu(out)
|
||||
|
||||
return out
|
||||
|
||||
|
||||
class ResidualBlockWithDown(nn.Cell):
|
||||
expansion = 4
|
||||
|
||||
def __init__(self,
|
||||
in_channels,
|
||||
out_channels,
|
||||
stride=1,
|
||||
down_sample=False):
|
||||
super(ResidualBlockWithDown, self).__init__()
|
||||
|
||||
out_chls = out_channels // self.expansion
|
||||
self.conv1 = conv1x1(in_channels, out_chls, stride=stride, padding=0)
|
||||
self.bn1 = bn_with_initialize(out_chls)
|
||||
|
||||
self.conv2 = conv3x3(out_chls, out_chls, stride=1, padding=0)
|
||||
self.bn2 = bn_with_initialize(out_chls)
|
||||
|
||||
self.conv3 = conv1x1(out_chls, out_channels, stride=1, padding=0)
|
||||
self.bn3 = bn_with_initialize_last(out_channels)
|
||||
|
||||
self.relu = P.ReLU()
|
||||
self.downSample = down_sample
|
||||
|
||||
self.conv_down_sample = conv1x1(in_channels, out_channels, stride=stride, padding=0)
|
||||
self.bn_down_sample = bn_with_initialize(out_channels)
|
||||
self.add = P.Add()
|
||||
|
||||
def construct(self, x):
|
||||
identity = x
|
||||
|
||||
out = self.conv1(x)
|
||||
out = self.bn1(out)
|
||||
out = self.relu(out)
|
||||
|
||||
out = self.conv2(out)
|
||||
out = self.bn2(out)
|
||||
out = self.relu(out)
|
||||
|
||||
out = self.conv3(out)
|
||||
out = self.bn3(out)
|
||||
|
||||
identity = self.conv_down_sample(identity)
|
||||
identity = self.bn_down_sample(identity)
|
||||
|
||||
out = self.add(out, identity)
|
||||
out = self.relu(out)
|
||||
|
||||
return out
|
||||
|
||||
|
||||
class MakeLayer0(nn.Cell):
|
||||
|
||||
def __init__(self, block, in_channels, out_channels, stride):
|
||||
super(MakeLayer0, self).__init__()
|
||||
self.a = ResidualBlockWithDown(in_channels, out_channels, stride=1, down_sample=True)
|
||||
self.b = block(out_channels, out_channels, stride=stride)
|
||||
self.c = block(out_channels, out_channels, stride=1)
|
||||
|
||||
def construct(self, x):
|
||||
x = self.a(x)
|
||||
x = self.b(x)
|
||||
x = self.c(x)
|
||||
|
||||
return x
|
||||
|
||||
|
||||
class MakeLayer1(nn.Cell):
|
||||
|
||||
def __init__(self, block, in_channels, out_channels, stride):
|
||||
super(MakeLayer1, self).__init__()
|
||||
self.a = ResidualBlockWithDown(in_channels, out_channels, stride=stride, down_sample=True)
|
||||
self.b = block(out_channels, out_channels, stride=1)
|
||||
self.c = block(out_channels, out_channels, stride=1)
|
||||
self.d = block(out_channels, out_channels, stride=1)
|
||||
|
||||
def construct(self, x):
|
||||
x = self.a(x)
|
||||
x = self.b(x)
|
||||
x = self.c(x)
|
||||
x = self.d(x)
|
||||
|
||||
return x
|
||||
|
||||
|
||||
class MakeLayer2(nn.Cell):
|
||||
|
||||
def __init__(self, block, in_channels, out_channels, stride):
|
||||
super(MakeLayer2, self).__init__()
|
||||
self.a = ResidualBlockWithDown(in_channels, out_channels, stride=stride, down_sample=True)
|
||||
self.b = block(out_channels, out_channels, stride=1)
|
||||
self.c = block(out_channels, out_channels, stride=1)
|
||||
self.d = block(out_channels, out_channels, stride=1)
|
||||
self.e = block(out_channels, out_channels, stride=1)
|
||||
self.f = block(out_channels, out_channels, stride=1)
|
||||
|
||||
def construct(self, x):
|
||||
x = self.a(x)
|
||||
x = self.b(x)
|
||||
x = self.c(x)
|
||||
x = self.d(x)
|
||||
x = self.e(x)
|
||||
x = self.f(x)
|
||||
|
||||
return x
|
||||
|
||||
|
||||
class MakeLayer3(nn.Cell):
|
||||
|
||||
def __init__(self, block, in_channels, out_channels, stride):
|
||||
super(MakeLayer3, self).__init__()
|
||||
self.a = ResidualBlockWithDown(in_channels, out_channels, stride=stride, down_sample=True)
|
||||
self.b = block(out_channels, out_channels, stride=1)
|
||||
self.c = block(out_channels, out_channels, stride=1)
|
||||
|
||||
def construct(self, x):
|
||||
x = self.a(x)
|
||||
x = self.b(x)
|
||||
x = self.c(x)
|
||||
|
||||
return x
|
||||
|
||||
|
||||
class ResNet(nn.Cell):
|
||||
|
||||
def __init__(self, block, num_classes=100, batch_size=32):
|
||||
super(ResNet, self).__init__()
|
||||
self.batch_size = batch_size
|
||||
self.num_classes = num_classes
|
||||
|
||||
self.conv1 = conv7x7(3, 64, stride=2, padding=0)
|
||||
|
||||
self.bn1 = bn_with_initialize(64)
|
||||
self.relu = P.ReLU()
|
||||
self.maxpool = P.MaxPoolWithArgmax(kernel_size=3, strides=2, pad_mode="SAME")
|
||||
|
||||
self.layer1 = MakeLayer0(block, in_channels=64, out_channels=256, stride=1)
|
||||
self.layer2 = MakeLayer1(block, in_channels=256, out_channels=512, stride=2)
|
||||
self.layer3 = MakeLayer2(block, in_channels=512, out_channels=1024, stride=2)
|
||||
self.layer4 = MakeLayer3(block, in_channels=1024, out_channels=2048, stride=2)
|
||||
|
||||
self.pool = P.ReduceMean(keep_dims=True)
|
||||
self.squeeze = P.Squeeze(axis=(2, 3))
|
||||
self.fc = fc_with_initialize(512 * block.expansion, num_classes)
|
||||
|
||||
def construct(self, x):
|
||||
x = self.conv1(x)
|
||||
x = self.bn1(x)
|
||||
x = self.relu(x)
|
||||
x = self.maxpool(x)[0]
|
||||
|
||||
x = self.layer1(x)
|
||||
x = self.layer2(x)
|
||||
x = self.layer3(x)
|
||||
x = self.layer4(x)
|
||||
|
||||
x = self.pool(x, (2, 3))
|
||||
x = self.squeeze(x)
|
||||
x = self.fc(x)
|
||||
return x
|
||||
|
||||
|
||||
def resnet50(batch_size, num_classes):
|
||||
return ResNet(ResidualBlock, num_classes, batch_size)
|
|
@ -1,154 +0,0 @@
|
|||
# Copyright 2020-2022 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.
|
||||
# ============================================================================
|
||||
import os
|
||||
import random
|
||||
import argparse
|
||||
import numpy as np
|
||||
from resnet import resnet50
|
||||
|
||||
import mindspore.common.dtype as mstype
|
||||
import mindspore.ops.functional as F
|
||||
from mindspore.train import ModelCheckpoint, CheckpointConfig, LossMonitor, TimeMonitor
|
||||
from mindspore.train.serialization import load_checkpoint, load_param_into_net
|
||||
import mindspore.dataset as ds
|
||||
import mindspore.dataset.transforms as C
|
||||
import mindspore.dataset.vision as vision
|
||||
import mindspore.nn as nn
|
||||
from mindspore import Tensor
|
||||
from mindspore import context
|
||||
from mindspore.communication.management import init
|
||||
from mindspore.nn.optim.momentum import Momentum
|
||||
from mindspore.ops import operations as P
|
||||
from mindspore.train import Model
|
||||
from mindspore.context import ParallelMode
|
||||
|
||||
random.seed(1)
|
||||
np.random.seed(1)
|
||||
ds.config.set_seed(1)
|
||||
|
||||
parser = argparse.ArgumentParser(description='Image classification')
|
||||
parser.add_argument('--run_distribute', type=bool, default=False, help='Run distribute')
|
||||
parser.add_argument('--device_num', type=int, default=1, help='Device num.')
|
||||
parser.add_argument('--do_train', type=bool, default=True, help='Do train or not.')
|
||||
parser.add_argument('--do_eval', type=bool, default=False, help='Do eval or not.')
|
||||
parser.add_argument('--epoch_size', type=int, default=1, help='Epoch size.')
|
||||
parser.add_argument('--batch_size', type=int, default=32, help='Batch size.')
|
||||
parser.add_argument('--num_classes', type=int, default=10, help='Num classes.')
|
||||
parser.add_argument('--checkpoint_path', type=str, default=None, help='Checkpoint file path')
|
||||
parser.add_argument('--dataset_path', type=str, default="/var/log/npu/datasets/cifar", help='Dataset path')
|
||||
args_opt = parser.parse_args()
|
||||
|
||||
device_id = int(os.getenv('DEVICE_ID'))
|
||||
|
||||
data_home = args_opt.dataset_path
|
||||
|
||||
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
|
||||
context.set_context(device_id=device_id)
|
||||
|
||||
|
||||
def create_dataset(repeat_num=1, training=True):
|
||||
data_dir = data_home + "/cifar-10-batches-bin"
|
||||
if not training:
|
||||
data_dir = data_home + "/cifar-10-verify-bin"
|
||||
data_set = ds.Cifar10Dataset(data_dir, num_samples=32)
|
||||
|
||||
if args_opt.run_distribute:
|
||||
rank_id = int(os.getenv('RANK_ID'))
|
||||
rank_size = int(os.getenv('RANK_SIZE'))
|
||||
data_set = ds.Cifar10Dataset(data_dir, num_shards=rank_size, shard_id=rank_id, num_samples=32)
|
||||
|
||||
resize_height = 224
|
||||
resize_width = 224
|
||||
rescale = 1.0 / 255.0
|
||||
shift = 0.0
|
||||
|
||||
# define map operations
|
||||
random_crop_op = vision.RandomCrop((32, 32), (4, 4, 4, 4)) # padding_mode default CONSTANT
|
||||
random_horizontal_op = vision.RandomHorizontalFlip()
|
||||
resize_op = vision.Resize((resize_height, resize_width)) # interpolation default BILINEAR
|
||||
rescale_op = vision.Rescale(rescale, shift)
|
||||
normalize_op = vision.Normalize((0.4465, 0.4822, 0.4914), (0.2010, 0.1994, 0.2023))
|
||||
changeswap_op = vision.HWC2CHW()
|
||||
type_cast_op = C.TypeCast(mstype.int32)
|
||||
|
||||
c_trans = []
|
||||
if training:
|
||||
c_trans = [random_crop_op, random_horizontal_op]
|
||||
c_trans += [resize_op, rescale_op, normalize_op,
|
||||
changeswap_op]
|
||||
|
||||
# apply map operations on images
|
||||
data_set = data_set.map(operations=type_cast_op, input_columns="label")
|
||||
data_set = data_set.map(operations=c_trans, input_columns="image")
|
||||
|
||||
# apply repeat operations
|
||||
data_set = data_set.repeat(repeat_num)
|
||||
|
||||
# apply shuffle operations
|
||||
data_set = data_set.shuffle(buffer_size=10)
|
||||
|
||||
# apply batch operations
|
||||
data_set = data_set.batch(batch_size=args_opt.batch_size, drop_remainder=True)
|
||||
|
||||
return data_set
|
||||
|
||||
|
||||
class CrossEntropyLoss(nn.Cell):
|
||||
def __init__(self):
|
||||
super(CrossEntropyLoss, self).__init__()
|
||||
self.cross_entropy = P.SoftmaxCrossEntropyWithLogits()
|
||||
self.mean = P.ReduceMean()
|
||||
self.one_hot = P.OneHot()
|
||||
self.one = Tensor(1.0, mstype.float32)
|
||||
self.zero = Tensor(0.0, mstype.float32)
|
||||
|
||||
def construct(self, logits, label):
|
||||
label = self.one_hot(label, F.shape(logits)[1], self.one, self.zero)
|
||||
loss_func = self.cross_entropy(logits, label)[0]
|
||||
loss_func = self.mean(loss_func, (-1,))
|
||||
return loss_func
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if not args_opt.do_eval and args_opt.run_distribute:
|
||||
context.set_auto_parallel_context(device_num=args_opt.device_num, parallel_mode=ParallelMode.DATA_PARALLEL,
|
||||
all_reduce_fusion_config=[140])
|
||||
init()
|
||||
|
||||
context.set_context(mode=context.GRAPH_MODE)
|
||||
epoch_size = args_opt.epoch_size
|
||||
net = resnet50(args_opt.batch_size, args_opt.num_classes)
|
||||
loss = CrossEntropyLoss()
|
||||
opt = Momentum(filter(lambda x: x.requires_grad, net.get_parameters()), 0.01, 0.9)
|
||||
|
||||
model = Model(net, loss_fn=loss, optimizer=opt, metrics={'acc'})
|
||||
|
||||
if args_opt.do_train:
|
||||
dataset = create_dataset(1)
|
||||
batch_num = dataset.get_dataset_size()
|
||||
config_ck = CheckpointConfig(save_checkpoint_steps=batch_num * 5, keep_checkpoint_max=10)
|
||||
ckpoint_cb = ModelCheckpoint(prefix="train_resnet_cifar10", directory="./", config=config_ck)
|
||||
time_cb = TimeMonitor(data_size=batch_num)
|
||||
loss_cb = LossMonitor()
|
||||
model.train(epoch_size, dataset, callbacks=[ckpoint_cb, loss_cb, time_cb])
|
||||
|
||||
if args_opt.do_eval:
|
||||
if args_opt.checkpoint_path:
|
||||
param_dict = load_checkpoint(args_opt.checkpoint_path)
|
||||
load_param_into_net(net, param_dict)
|
||||
net.set_train(False)
|
||||
eval_dataset = create_dataset(1, training=False)
|
||||
res = model.eval(eval_dataset)
|
||||
print("result: ", res)
|
|
@ -1,37 +0,0 @@
|
|||
#!/bin/bash
|
||||
# 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.
|
||||
# ============================================================================
|
||||
set -e
|
||||
BASE_PATH=$(cd "$(dirname $0)"; pwd)
|
||||
export DEVICE_NUM=2
|
||||
export RANK_SIZE=$DEVICE_NUM
|
||||
ulimit -n 65535
|
||||
export DISTRIBUTION_FILE=$BASE_PATH/tdt${DEVICE_NUM}p/tdt_
|
||||
export MINDSPORE_HCCL_CONFIG_PATH=$BASE_PATH/hccl_${DEVICE_NUM}p.json
|
||||
|
||||
for((i=0;i<$DEVICE_NUM;i++))
|
||||
do
|
||||
rm -rf ./dataparallel$i
|
||||
mkdir ./dataparallel$i
|
||||
cp *.py ./dataparallel$i
|
||||
cp -r kernel_meta ./dataparallel$i
|
||||
cd ./dataparallel$i
|
||||
export RANK_ID=$i
|
||||
export DEVICE_ID=$i
|
||||
echo "start training for device $i"
|
||||
env > env$i.log
|
||||
python resnet_cifar.py --run_distribute=1 --device_num=$DEVICE_NUM --epoch_size=10 >log 2>&1 &
|
||||
cd ../
|
||||
done
|
|
@ -1,7 +0,0 @@
|
|||
{
|
||||
"deviceNum":2,
|
||||
"deviceId":0,
|
||||
"shardConfig":"RANDOM",
|
||||
"shuffle":"ON",
|
||||
"seed": 0
|
||||
}
|
|
@ -1,7 +0,0 @@
|
|||
{
|
||||
"deviceNum":2,
|
||||
"deviceId":1,
|
||||
"shardConfig":"RANDOM",
|
||||
"shuffle":"ON",
|
||||
"seed": 0
|
||||
}
|
|
@ -1,161 +0,0 @@
|
|||
# Copyright 2020-2022 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.
|
||||
# ============================================================================
|
||||
|
||||
import os
|
||||
import random
|
||||
import numpy as np
|
||||
from resnet import resnet50
|
||||
|
||||
import mindspore.common.dtype as mstype
|
||||
import mindspore.dataset as ds
|
||||
import mindspore.dataset.transforms as C
|
||||
import mindspore.dataset.vision as vision
|
||||
import mindspore.nn as nn
|
||||
import mindspore.ops.functional as F
|
||||
from mindspore import Tensor
|
||||
from mindspore import context
|
||||
from mindspore.nn.optim.momentum import Momentum
|
||||
from mindspore.ops import operations as P
|
||||
from mindspore.train import Callback
|
||||
from mindspore.train import Model
|
||||
|
||||
random.seed(1)
|
||||
np.random.seed(1)
|
||||
ds.config.set_seed(1)
|
||||
|
||||
data_home = "/home/workspace/mindspore_dataset"
|
||||
|
||||
|
||||
def create_dataset(repeat_num=1, training=True, batch_size=32):
|
||||
data_dir = data_home + "/cifar-10-batches-bin"
|
||||
if not training:
|
||||
data_dir = data_home + "/cifar-10-verify-bin"
|
||||
data_set = ds.Cifar10Dataset(data_dir)
|
||||
|
||||
resize_height = 224
|
||||
resize_width = 224
|
||||
rescale = 1.0 / 255.0
|
||||
shift = 0.0
|
||||
|
||||
# define map operations
|
||||
random_crop_op = vision.RandomCrop(
|
||||
(32, 32), (4, 4, 4, 4)) # padding_mode default CONSTANT
|
||||
random_horizontal_op = vision.RandomHorizontalFlip()
|
||||
# interpolation default BILINEAR
|
||||
resize_op = vision.Resize((resize_height, resize_width))
|
||||
rescale_op = vision.Rescale(rescale, shift)
|
||||
normalize_op = vision.Normalize(
|
||||
(0.4465, 0.4822, 0.4914), (0.2010, 0.1994, 0.2023))
|
||||
changeswap_op = vision.HWC2CHW()
|
||||
type_cast_op = C.TypeCast(mstype.int32)
|
||||
|
||||
c_trans = []
|
||||
if training:
|
||||
c_trans = [random_crop_op, random_horizontal_op]
|
||||
c_trans += [resize_op, rescale_op, normalize_op,
|
||||
changeswap_op]
|
||||
|
||||
# apply map operations on images
|
||||
data_set = data_set.map(operations=type_cast_op, input_columns="label")
|
||||
data_set = data_set.map(operations=c_trans, input_columns="image")
|
||||
|
||||
# apply shuffle operations
|
||||
data_set = data_set.shuffle(buffer_size=1000)
|
||||
|
||||
# apply batch operations
|
||||
data_set = data_set.batch(batch_size=batch_size, drop_remainder=True)
|
||||
|
||||
# apply repeat operations
|
||||
data_set = data_set.repeat(repeat_num)
|
||||
|
||||
return data_set
|
||||
|
||||
|
||||
class CrossEntropyLoss(nn.Cell):
|
||||
def __init__(self):
|
||||
super(CrossEntropyLoss, self).__init__()
|
||||
self.cross_entropy = P.SoftmaxCrossEntropyWithLogits()
|
||||
self.mean = P.ReduceMean()
|
||||
self.one_hot = P.OneHot()
|
||||
self.one = Tensor(1.0, mstype.float32)
|
||||
self.zero = Tensor(0.0, mstype.float32)
|
||||
|
||||
def construct(self, logits, label):
|
||||
label = self.one_hot(label, F.shape(logits)[1], self.one, self.zero)
|
||||
loss = self.cross_entropy(logits, label)[0]
|
||||
loss = self.mean(loss, (-1,))
|
||||
return loss
|
||||
|
||||
|
||||
class LossGet(Callback):
|
||||
def __init__(self, per_print_times=1):
|
||||
super(LossGet, self).__init__()
|
||||
if not isinstance(per_print_times, int) or per_print_times < 0:
|
||||
raise ValueError("print_step must be int and >= 0.")
|
||||
self._per_print_times = per_print_times
|
||||
self._loss = 0.0
|
||||
|
||||
def step_end(self, run_context):
|
||||
cb_params = run_context.original_args()
|
||||
loss = cb_params.net_outputs
|
||||
|
||||
if isinstance(loss, (tuple, list)):
|
||||
if isinstance(loss[0], Tensor) and isinstance(loss[0].asnumpy(), np.ndarray):
|
||||
loss = loss[0]
|
||||
|
||||
if isinstance(loss, Tensor) and isinstance(loss.asnumpy(), np.ndarray):
|
||||
loss = np.mean(loss.asnumpy())
|
||||
|
||||
cur_step_in_epoch = (cb_params.cur_step_num - 1) % cb_params.batch_num + 1
|
||||
|
||||
if isinstance(loss, float) and (np.isnan(loss) or np.isinf(loss)):
|
||||
raise ValueError("epoch: {} step: {}. Invalid loss, terminating training."
|
||||
.format(cb_params.cur_epoch_num, cur_step_in_epoch))
|
||||
if self._per_print_times != 0 and cb_params.cur_step_num % self._per_print_times == 0:
|
||||
self._loss = loss
|
||||
print("epoch: %s step: %s, loss is %s" % (cb_params.cur_epoch_num, cur_step_in_epoch, loss))
|
||||
|
||||
def get_loss(self):
|
||||
return self._loss
|
||||
|
||||
|
||||
def train_process(epoch_size, num_classes, batch_size):
|
||||
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
|
||||
net = resnet50(batch_size, num_classes)
|
||||
loss = CrossEntropyLoss()
|
||||
opt = Momentum(filter(lambda x: x.requires_grad,
|
||||
net.get_parameters()), 0.01, 0.9)
|
||||
|
||||
model = Model(net, loss_fn=loss, optimizer=opt, metrics={'acc'})
|
||||
|
||||
dataset = create_dataset(1, training=True, batch_size=batch_size)
|
||||
loss_cb = LossGet()
|
||||
model.train(epoch_size, dataset, callbacks=[loss_cb])
|
||||
|
||||
net.set_train(False)
|
||||
eval_dataset = create_dataset(1, training=False)
|
||||
res = model.eval(eval_dataset)
|
||||
print("result: ", res)
|
||||
return res
|
||||
|
||||
|
||||
def test_resnet_cifar_1p():
|
||||
epoch_size = 1
|
||||
num_classes = 10
|
||||
batch_size = 32
|
||||
acc = train_process(epoch_size, num_classes, batch_size)
|
||||
os.system("rm -rf kernel_meta")
|
||||
print("End training...")
|
||||
assert acc['acc'] > 0.20
|
|
@ -1,202 +0,0 @@
|
|||
# Copyright 2020-2022 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.
|
||||
# ============================================================================
|
||||
|
||||
import os
|
||||
import random
|
||||
from multiprocessing import Process, Queue
|
||||
import numpy as np
|
||||
|
||||
from resnet import resnet50
|
||||
import mindspore.common.dtype as mstype
|
||||
import mindspore.dataset as ds
|
||||
import mindspore.dataset.transforms as C
|
||||
import mindspore.dataset.vision as vision
|
||||
import mindspore.nn as nn
|
||||
import mindspore.ops.functional as F
|
||||
from mindspore import Tensor
|
||||
from mindspore import context
|
||||
from mindspore.communication.management import init
|
||||
from mindspore.nn.optim.momentum import Momentum
|
||||
from mindspore.ops import operations as P
|
||||
from mindspore.train import Callback, Model
|
||||
from mindspore.context import ParallelMode
|
||||
|
||||
random.seed(1)
|
||||
np.random.seed(1)
|
||||
ds.config.set_seed(1)
|
||||
|
||||
MINDSPORE_HCCL_CONFIG_PATH = "/home/workspace/mindspore_config/hccl/rank_table_8p.json"
|
||||
data_home = "/home/workspace/mindspore_dataset"
|
||||
|
||||
|
||||
def create_dataset(repeat_num=1, training=True, batch_size=32, rank_id=0, rank_size=1,
|
||||
enable_hccl=False):
|
||||
data_dir = data_home + "/cifar-10-batches-bin"
|
||||
if not training:
|
||||
data_dir = data_home + "/cifar-10-verify-bin"
|
||||
data_set = ds.Cifar10Dataset(data_dir)
|
||||
|
||||
if enable_hccl:
|
||||
rank_id = rank_id
|
||||
rank_size = rank_size
|
||||
data_set = ds.Cifar10Dataset(
|
||||
data_dir, num_shards=rank_size, shard_id=rank_id)
|
||||
|
||||
resize_height = 224
|
||||
resize_width = 224
|
||||
rescale = 1.0 / 255.0
|
||||
shift = 0.0
|
||||
|
||||
# define map operations
|
||||
random_crop_op = vision.RandomCrop(
|
||||
(32, 32), (4, 4, 4, 4)) # padding_mode default CONSTANT
|
||||
random_horizontal_op = vision.RandomHorizontalFlip()
|
||||
# interpolation default BILINEAR
|
||||
resize_op = vision.Resize((resize_height, resize_width))
|
||||
rescale_op = vision.Rescale(rescale, shift)
|
||||
normalize_op = vision.Normalize(
|
||||
(0.4465, 0.4822, 0.4914), (0.2010, 0.1994, 0.2023))
|
||||
changeswap_op = vision.HWC2CHW()
|
||||
type_cast_op = C.TypeCast(mstype.int32)
|
||||
|
||||
c_trans = []
|
||||
if training:
|
||||
c_trans = [random_crop_op, random_horizontal_op]
|
||||
c_trans += [resize_op, rescale_op, normalize_op,
|
||||
changeswap_op]
|
||||
|
||||
# apply map operations on images
|
||||
data_set = data_set.map(operations=type_cast_op, input_columns="label")
|
||||
data_set = data_set.map(operations=c_trans, input_columns="image")
|
||||
|
||||
# apply shuffle operations
|
||||
data_set = data_set.shuffle(buffer_size=1000)
|
||||
|
||||
# apply batch operations
|
||||
data_set = data_set.batch(batch_size=batch_size, drop_remainder=True)
|
||||
|
||||
# apply repeat operations
|
||||
data_set = data_set.repeat(repeat_num)
|
||||
|
||||
return data_set
|
||||
|
||||
|
||||
class CrossEntropyLoss(nn.Cell):
|
||||
def __init__(self):
|
||||
super(CrossEntropyLoss, self).__init__()
|
||||
self.cross_entropy = P.SoftmaxCrossEntropyWithLogits()
|
||||
self.mean = P.ReduceMean()
|
||||
self.one_hot = P.OneHot()
|
||||
self.one = Tensor(1.0, mstype.float32)
|
||||
self.zero = Tensor(0.0, mstype.float32)
|
||||
|
||||
def construct(self, logits, label):
|
||||
label = self.one_hot(label, F.shape(logits)[1], self.one, self.zero)
|
||||
loss = self.cross_entropy(logits, label)[0]
|
||||
loss = self.mean(loss, (-1,))
|
||||
return loss
|
||||
|
||||
|
||||
class LossGet(Callback):
|
||||
def __init__(self, per_print_times=1):
|
||||
super(LossGet, self).__init__()
|
||||
if not isinstance(per_print_times, int) or per_print_times < 0:
|
||||
raise ValueError("print_step must be int and >= 0.")
|
||||
self._per_print_times = per_print_times
|
||||
self._loss = 0.0
|
||||
|
||||
def step_end(self, run_context):
|
||||
cb_params = run_context.original_args()
|
||||
loss = cb_params.net_outputs
|
||||
|
||||
if isinstance(loss, (tuple, list)):
|
||||
if isinstance(loss[0], Tensor) and isinstance(loss[0].asnumpy(), np.ndarray):
|
||||
loss = loss[0]
|
||||
|
||||
if isinstance(loss, Tensor) and isinstance(loss.asnumpy(), np.ndarray):
|
||||
loss = np.mean(loss.asnumpy())
|
||||
|
||||
cur_step_in_epoch = (cb_params.cur_step_num - 1) % cb_params.batch_num + 1
|
||||
|
||||
if isinstance(loss, float) and (np.isnan(loss) or np.isinf(loss)):
|
||||
raise ValueError("epoch: {} step: {}. Invalid loss, terminating training."
|
||||
.format(cb_params.cur_epoch_num, cur_step_in_epoch))
|
||||
if self._per_print_times != 0 and cb_params.cur_step_num % self._per_print_times == 0:
|
||||
self._loss = loss
|
||||
print("epoch: %s step: %s, loss is %s" % (cb_params.cur_epoch_num, cur_step_in_epoch, loss))
|
||||
|
||||
def get_loss(self):
|
||||
return self._loss
|
||||
|
||||
|
||||
def train_process(q, device_id, epoch_size, num_classes, device_num, batch_size, enable_hccl):
|
||||
os.system("mkdir " + str(device_id))
|
||||
os.chdir(str(device_id))
|
||||
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
|
||||
context.set_context(device_id=device_id)
|
||||
os.environ['MINDSPORE_HCCL_CONFIG_PATH'] = MINDSPORE_HCCL_CONFIG_PATH
|
||||
os.environ['RANK_ID'] = str(device_id)
|
||||
os.environ['RANK_SIZE'] = str(device_num)
|
||||
if enable_hccl:
|
||||
context.set_auto_parallel_context(
|
||||
device_num=device_num, parallel_mode=ParallelMode.DATA_PARALLEL, all_reduce_fusion_config=[140])
|
||||
init()
|
||||
context.set_context(mode=context.GRAPH_MODE)
|
||||
net = resnet50(batch_size, num_classes)
|
||||
loss = CrossEntropyLoss()
|
||||
opt = Momentum(filter(lambda x: x.requires_grad,
|
||||
net.get_parameters()), 0.01, 0.9)
|
||||
|
||||
model = Model(net, loss_fn=loss, optimizer=opt, metrics={'acc'})
|
||||
|
||||
dataset = create_dataset(1, training=True,
|
||||
batch_size=batch_size, rank_id=device_id, rank_size=device_num,
|
||||
enable_hccl=enable_hccl)
|
||||
|
||||
loss_cb = LossGet()
|
||||
model.train(epoch_size, dataset, callbacks=[loss_cb])
|
||||
q.put(loss_cb.get_loss())
|
||||
|
||||
|
||||
def test_resnet_cifar_8p():
|
||||
q = Queue()
|
||||
device_num = 8
|
||||
epoch_size = 1
|
||||
num_classes = 10
|
||||
batch_size = 32
|
||||
enable_hccl = True
|
||||
process = []
|
||||
for i in range(device_num):
|
||||
device_id = i
|
||||
process.append(Process(target=train_process,
|
||||
args=(q, device_id, epoch_size, num_classes, device_num, batch_size, enable_hccl)))
|
||||
|
||||
for i in range(device_num):
|
||||
process[i].start()
|
||||
|
||||
print("Waiting for all subprocesses done...")
|
||||
|
||||
for i in range(device_num):
|
||||
process[i].join()
|
||||
|
||||
loss = 0.0
|
||||
for i in range(device_num):
|
||||
loss += q.get()
|
||||
loss = loss / device_num
|
||||
|
||||
for i in range(device_num):
|
||||
os.system("rm -rf " + str(i))
|
||||
print("End training...")
|
||||
assert loss < 2.0
|
Loading…
Reference in New Issue