[{"data":1,"prerenderedAt":346},["ShallowReactive",2],{"content-query-5dDviXusUD":3},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"date":10,"cover":11,"type":12,"category":13,"body":14,"_type":340,"_id":341,"_source":342,"_file":343,"_stem":344,"_extension":345},"/technology-blogs/zh/1832","zh",false,"","【MindSpore易点通】性能调试经验总结下篇","MindSpore使用model.train接口训练时，默认同步保存模型，可能影响训练性能。","2022-09-14","https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/09/26/3e40f8575a6d4673bcc1fab49344a90b.png","technology-blogs","基础知识",{"type":15,"children":16,"toc":325},"root",[17,25,30,36,41,46,51,61,66,71,76,81,86,92,97,105,110,118,123,128,133,139,144,152,157,162,167,172,180,185,193,198,203,211,216,223,229,234,239,244,249,257,263,268,273,278,283,291,297,302,307,312,317],{"type":18,"tag":19,"props":20,"children":22},"element","h1",{"id":21},"mindspore易点通性能调试经验总结下篇",[23],{"type":24,"value":8},"text",{"type":18,"tag":19,"props":26,"children":28},{"id":27},"异步保存模型",[29],{"type":24,"value":27},{"type":18,"tag":31,"props":32,"children":34},"h3",{"id":33},"背景信息",[35],{"type":24,"value":33},{"type":18,"tag":37,"props":38,"children":39},"p",{},[40],{"type":24,"value":9},{"type":18,"tag":31,"props":42,"children":44},{"id":43},"经验总结内容",[45],{"type":24,"value":43},{"type":18,"tag":37,"props":47,"children":48},{},[49],{"type":24,"value":50},"在模型定义中配置async_save参数为True，即可实现异步保存模型，提升训练性能。",{"type":18,"tag":52,"props":53,"children":55},"pre",{"code":54},"config_ck = CheckpointConfig(save_checkpoint_steps=steps_per_epoch_train,\n\n                                keep_checkpoint_max=epoch_max, async_save=True)\n\nckpoint_cb = ModelCheckpoint(prefix=\"train_resnet_cifar10\",\n\n                                directory=\"./\", config=config_ck)\n\nmodel.train(..., callbacks=[ckpoint_cb, ...], ...)\n",[56],{"type":18,"tag":57,"props":58,"children":59},"code",{"__ignoreMap":7},[60],{"type":24,"value":54},{"type":18,"tag":37,"props":62,"children":63},{},[64],{"type":24,"value":65},"性能对比：异步保存模型：2450 imgs/sec；同步保存模型：2200 imgs/sec",{"type":18,"tag":19,"props":67,"children":69},{"id":68},"自动数据加速",[70],{"type":24,"value":68},{"type":18,"tag":31,"props":72,"children":74},{"id":73},"背景信息-1",[75],{"type":24,"value":33},{"type":18,"tag":37,"props":77,"children":78},{},[79],{"type":24,"value":80},"MindSpore提供了一种自动数据调优的工具——Dataset AutoTune，用于在训练过程中根据环境资源的情况自动调整数据处理管道的并行度， 最大化利用系统资源加速数据处理管道的处理速度。",{"type":18,"tag":37,"props":82,"children":83},{},[84],{"type":24,"value":85},"在整个训练的过程中，Dataset AutoTune模块会持续检测当前训练性能瓶颈处于数据侧还是网络侧。如果检测到瓶颈在数据侧，则将 进一步对数据处理管道中的各个算子（如GeneratorDataset、map、batch此类数据算子）进行参数调整。",{"type":18,"tag":31,"props":87,"children":89},{"id":88},"_1示例代码",[90],{"type":24,"value":91},"1、示例代码",{"type":18,"tag":37,"props":93,"children":94},{},[95],{"type":24,"value":96},"示例代码如下：",{"type":18,"tag":52,"props":98,"children":100},{"code":99},"import mindspore.dataset as ds\n\ndef create_dataset(...)\n\n    \"\"\"\n\n    create dataset for train or test\n\n    \"\"\"\n\n    # 使能自动数据加速\n\n    ds.config.set_enable_autotune(True)\n\n\n\n    # 其他数据集代码无需变更\n\n    data_set = ds.Cifar10Dataset(data_path)\n",[101],{"type":18,"tag":57,"props":102,"children":103},{"__ignoreMap":7},[104],{"type":24,"value":99},{"type":18,"tag":37,"props":106,"children":107},{},[108],{"type":24,"value":109},"自动数据加速关键代码：",{"type":18,"tag":52,"props":111,"children":113},{"code":112},"import mindspore.dataset as ds\n\nds.config.set_enable_autotune(True)\n",[114],{"type":18,"tag":57,"props":115,"children":116},{"__ignoreMap":7},[117],{"type":24,"value":112},{"type":18,"tag":37,"props":119,"children":120},{},[121],{"type":24,"value":122},"注：",{"type":18,"tag":37,"props":124,"children":125},{},[126],{"type":24,"value":127},"自动数据加速目前仅可用于下沉模式（dataset_sink_mode=True），在非下沉模式（dataset_sink_mode=False）下，Dataset AutoTune将不会生效，但不会影响网络正常训练;",{"type":18,"tag":37,"props":129,"children":130},{},[131],{"type":24,"value":132},"Profiling性能分析和自动数据加速无法同时开启，否则会导致Profiling或Dataset AutoTune不生效。如果这样同时开启此两个功能，则会有一条警告信息提示用户检查是否为误操作。因此在使用Dataset AutoTune时，用户需要确保关闭Profiling功能。",{"type":18,"tag":31,"props":134,"children":136},{"id":135},"_2运行结果分析",[137],{"type":24,"value":138},"2、运行结果分析",{"type":18,"tag":37,"props":140,"children":141},{},[142],{"type":24,"value":143},"以示例代码为例使能数据自动加速功能，随后自动数据加速模块会通过打屏log的形式展示其对于性能瓶颈的分析情况：",{"type":18,"tag":52,"props":145,"children":147},{"code":146},"[WARNING] ME(15918,fffde27fc1e0,python):2022-01-13-09:33:16.061.926 [mindspore/ccsrc/minddata/dataset/engine/perf/auto_tune.cc:425] Analyse] Op (BatchOp(ID:2)) getting low average worker cpu utilization 1.25694% \u003C 35% threshold.\n\n\n\n[WARNING] ME(15918,fffde27fc1e0,python):2022-01-13-09:33:16.061.941\n\n[mindspore/ccsrc/minddata/dataset/engine/perf/auto_tune.cc:359]\n\nRequestConnectorCapacityChange] Added request to change \"prefetch_size\" of Operator: BatchOp(ID:2)From old value: [32] to new value: [36].\n",[148],{"type":18,"tag":57,"props":149,"children":150},{"__ignoreMap":7},[151],{"type":24,"value":146},{"type":18,"tag":37,"props":153,"children":154},{},[155],{"type":24,"value":156},"Epoch:8, 2703.92imgs/sec",{"type":18,"tag":37,"props":158,"children":159},{},[160],{"type":24,"value":161},"epoch time: 22154.670 ms, per step time: 47.339 ms",{"type":18,"tag":37,"props":163,"children":164},{},[165],{"type":24,"value":166},"数据管道的性能分析和调整过程可以通过上述的log体现：",{"type":18,"tag":37,"props":168,"children":169},{},[170],{"type":24,"value":171},"当检测到数据侧为瓶颈且需要发生参数变更时，则会通过WARNING级别的LOG信息提醒用户正在调整的参数。",{"type":18,"tag":52,"props":173,"children":175},{"code":174},"BatchOp(ID:2)From old value: [32] to new value: [36]\n",[176],{"type":18,"tag":57,"props":177,"children":178},{"__ignoreMap":7},[179],{"type":24,"value":174},{"type":18,"tag":37,"props":181,"children":182},{},[183],{"type":24,"value":184},"在数据处理管道的初始配置下，Device Connector队列（数据管道与计算网络之间的缓冲队列）的利用率较低。",{"type":18,"tag":52,"props":186,"children":188},{"code":187},"[mindspore/ccsrc/minddata/dataset/engine/perf/auto_tune.cc:425] Analyse] Op (BatchOp(ID:2)) getting low average worker cpu utilization 1.25694% \u003C 35% threshold.\n",[189],{"type":18,"tag":57,"props":190,"children":191},{"__ignoreMap":7},[192],{"type":24,"value":187},{"type":18,"tag":37,"props":194,"children":195},{},[196],{"type":24,"value":197},"在使用示例代码运行20epoch加速前与加速后运行时间对比：",{"type":18,"tag":37,"props":199,"children":200},{},[201],{"type":24,"value":202},"自动数据加速后效果：",{"type":18,"tag":37,"props":204,"children":205},{},[206],{"type":18,"tag":207,"props":208,"children":210},"img",{"alt":7,"src":209},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/09/26/3d865672632c44248e3a2d8fdc4cd547.png",[],{"type":18,"tag":37,"props":212,"children":213},{},[214],{"type":24,"value":215},"未使用自动数据加速效果：",{"type":18,"tag":37,"props":217,"children":218},{},[219],{"type":18,"tag":207,"props":220,"children":222},{"alt":7,"src":221},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/09/26/570186b81c1e4b3b90d536cbe6f5e407.png",[],{"type":18,"tag":19,"props":224,"children":226},{"id":225},"matmultilediv等算子最好把输入做16倍数对齐",[227],{"type":24,"value":228},"MatMul、Tile、Div等算子最好把输入做16倍数对齐",{"type":18,"tag":31,"props":230,"children":232},{"id":231},"背景信息-2",[233],{"type":24,"value":33},{"type":18,"tag":37,"props":235,"children":236},{},[237],{"type":24,"value":238},"因为D芯片底层数据搬运按照16字节对齐，所以部分算子的输入在非16倍数的情况下无法使能多核，导致性能变差。",{"type":18,"tag":31,"props":240,"children":242},{"id":241},"经验总结内容-1",[243],{"type":24,"value":43},{"type":18,"tag":37,"props":245,"children":246},{},[247],{"type":24,"value":248},"MatMul、Tile以及Div算子的输入最好先向上进行16字节的对齐，以MatMul举例如下：",{"type":18,"tag":52,"props":250,"children":252},{"code":251},"import mindspore.nn as nn\n\nclass CustomMatMul(nn.Cell):\n\n    def __init__(self, transpose_a=False, transpose_b=False):\n\n        super(CustomMatMul, self).__init__()\n\n        self.fc = P.MatMul(transpose_a=transpose_a, transpose_b=transpose_b)\n\n\n\n    def construct(self, x1, x2):\n\n        out = self.fc(x1, x2)\n\n        return out\n\nclass MatMul_assignto16(nn.cell):\n\n    def __init__(self, args):\n\n        super(MatMul_assignto16, self).__init__()\n\n        self.num_classes = (args.num_classes // 16 + 1) * 16    #输入的类别数做向上16字节对齐\n\n        self.weight = Parameter(Tensor(np.ones((self.num_classes, args.emb_size)), mstype.float32), name=\"w\", requires_grad=True)\n\n        self.fc = CustomMatMul(transpose_b=True).add_flags_recursive(fp16=True)\n\n\n\n    def construct(self, feature):\n\n        out = self.fc(feature, self.weight)\n\n        return out\n",[253],{"type":18,"tag":57,"props":254,"children":255},{"__ignoreMap":7},[256],{"type":24,"value":251},{"type":18,"tag":19,"props":258,"children":260},{"id":259},"matmul算子性能差",[261],{"type":24,"value":262},"MatMul算子性能差",{"type":18,"tag":31,"props":264,"children":266},{"id":265},"背景信息-3",[267],{"type":24,"value":33},{"type":18,"tag":37,"props":269,"children":270},{},[271],{"type":24,"value":272},"D芯片中矩阵运算是用cube做的，只能支持fp16的计算类型",{"type":18,"tag":31,"props":274,"children":276},{"id":275},"经验总结内容-2",[277],{"type":24,"value":43},{"type":18,"tag":37,"props":279,"children":280},{},[281],{"type":24,"value":282},"MatMul算子尽量使用fp16的计算类型，fp32的MatMul是在aicpu上计算的，性能会比fp16差很多。 可以在me脚本层面手动指定算子的计算类型，如下：",{"type":18,"tag":52,"props":284,"children":286},{"code":285},"class CustomMatMul(nn.Cell):\n\n    def __init__(self, transpose_a=False, transpose_b=False):\n\n        super(CustomMatMul, self).__init__()\n\n        self.fc = P.MatMul(transpose_a=transpose_a, transpose_b=transpose_b)\n\n\n\n    def construct(self, x1, x2):\n\n        out = self.fc(x1, x2)\n\n        return out\n\n\n\nself.fc = CustomMatMul(transpose_b=True).add_flags_recursive(fp16=True)\n",[287],{"type":18,"tag":57,"props":288,"children":289},{"__ignoreMap":7},[290],{"type":24,"value":285},{"type":18,"tag":19,"props":292,"children":294},{"id":293},"resnet50跑imagenet类别数设定",[295],{"type":24,"value":296},"ResNet50跑ImageNet类别数设定",{"type":18,"tag":31,"props":298,"children":300},{"id":299},"背景信息-4",[301],{"type":24,"value":33},{"type":18,"tag":37,"props":303,"children":304},{},[305],{"type":24,"value":306},"ResNet50跑ImageNet在PyTorch上面的类别数设定是1000，而TensorFlow的类别数设定是1001。",{"type":18,"tag":31,"props":308,"children":310},{"id":309},"经验总结内容-3",[311],{"type":24,"value":43},{"type":18,"tag":37,"props":313,"children":314},{},[315],{"type":24,"value":316},"Ascend环境下针对TensorFlow做了更深度的优化，因此在跑MindSpore+Ascend的时候，也建议把类别数设置为1001以获取更加极致的性能。 可以在me脚本层面手动指定类别数，如下：",{"type":18,"tag":52,"props":318,"children":320},{"code":319},"class CommonHead(nn.Cell):\n\n    def __init__(self, num_classes=1001, out_channels):  #将类别数默认置为1001\n\n        super(CommonHead, self).__init__()\n\n        self.avgpool = GlobalAvgPooling()\n\n        self.fc = nn.Dense(out_channels, num_classes, has_bias=True)\n\n \n\n    def construct(self, x):\n\n        x = self.avgpool(x)\n\n        x = self.fc(x)\n\n        return x\n",[321],{"type":18,"tag":57,"props":322,"children":323},{"__ignoreMap":7},[324],{"type":24,"value":319},{"title":7,"searchDepth":326,"depth":326,"links":327},4,[328,330,331,332,333,334,335,336,337,338,339],{"id":33,"depth":329,"text":33},3,{"id":43,"depth":329,"text":43},{"id":73,"depth":329,"text":33},{"id":88,"depth":329,"text":91},{"id":135,"depth":329,"text":138},{"id":231,"depth":329,"text":33},{"id":241,"depth":329,"text":43},{"id":265,"depth":329,"text":33},{"id":275,"depth":329,"text":43},{"id":299,"depth":329,"text":33},{"id":309,"depth":329,"text":43},"markdown","content:technology-blogs:zh:1832.md","content","technology-blogs/zh/1832.md","technology-blogs/zh/1832","md",1776506116083]