• 图算融合使能不同优化等级尝试网络性能调优


    1.功能描述

    使能图算融合尝试网络性能调优

    2.功能简介

    当前网络性能要进行调优的时候,可以尝试打开图算融合开关。而单纯设置图算融合为True的时候,并不一定挖掘了其所有的优化空间,在图算融合内部,优化的幅度是分等级的,一般分为以下4个等级。

    • 0: Disable GraphKernel
    • 1: Enable GraphKernel with basic features only.
    • 2: Enable GraphKernel with all stable features.
    • 3: Enable GraphKernel with all experimental features. 对于当前的GPU网络使能图算融合而言,一般都是设置了enable_graph_kernel=True,而这个设置实际上只优化到了第二个等级,也即把基本的融合优化,和一些稳定的优化使能了。

    3.解决方法

    正如前文提及的,图算融合的优化幅度是有等级的,具体的等级设置可以用一个flag opt_level来进行控制,具体的操作,则是可以在正式训练前使用context进行设置,如在run_train()一类的函数前加一行使能图算最高等级的优化:

    context.set_context(enable_graph_kernel=True,graph_kernel_flags="--opt_level=3")

    这样便可将图算尚且还在实验中的一些优化也给加上了,通过这样的设置可以方便观察性能是否有了进一步的提升。 如下代码所示,分别是默认开图算和开图算且设置了最高阶优化的形式:

    1. # Copyright 2021 Huawei Technologies Co., Ltd
    2. #
    3. # Licensed under the Apache License, Version 2.0 (the "License");
    4. # you may not use this file except in compliance with the License.
    5. # You may obtain a copy of the License at
    6. #
    7. # http://www.apache.org/licenses/LICENSE-2.0
    8. #
    9. # Unless required by applicable law or agreed to in writing, software
    10. # distributed under the License is distributed on an "AS IS" BASIS,
    11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12. # See the License for the specific language governing permissions and
    13. # limitations under the License.
    14. # ============================================================================
    15. import numpy as np
    16. import mindspore.context as context
    17. from mindspore import Tensor
    18. import mindspore.nn as nn
    19. from mindspore.nn import Cell
    20. from mindspore.ops import operations as P
    21. import mindspore.ops.functional as F
    22. import pytest
    23. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
    24. # enable graph kernel optimization.
    25. context.set_context(enable_graph_kernel=True)
    26. class BertAttentionPiece(Cell):
    27. def __init__(self):
    28. super(BertAttentionPiece, self).__init__()
    29. self.add = P.Add()
    30. self.dropout = nn.Dropout(1 - 0.1)
    31. self.softmax = nn.Softmax()
    32. self.multiply_data = -10000.0
    33. self.sub = P.Sub()
    34. self.multiply = P.Mul()
    35. self.get_dtype = P.DType()
    36. self.cast = P.Cast()
    37. def construct(self, attention_mask, attention_scores):
    38. multiply_out = self.sub(self.cast(F.tuple_to_array((1.0,)), self.get_dtype(attention_scores)),
    39. self.cast(attention_mask, self.get_dtype(attention_scores)))
    40. adder = self.multiply(multiply_out, self.multiply_data)
    41. attention_scores = self.add(adder, attention_scores)
    42. attention_probs = self.softmax(attention_scores)
    43. attention_probs = self.dropout(attention_probs)
    44. return attention_probs
    45. def get_rtol_atol(dtype):
    46. if dtype == np.float16:
    47. return 1.e-3, 1.e-3
    48. return 1.e-4, 1.e-4
    49. def compare_result(expect, output, dtype):
    50. rtol, atol = get_rtol_atol(dtype)
    51. if isinstance(expect, (list, tuple)):
    52. assert isinstance(output, (list, tuple)) and len(expect) == len(output)
    53. expect_list = list(expect)
    54. output_list = list(output)
    55. for e, o in zip(expect_list, output_list):
    56. assert np.allclose(e.asnumpy(), o.asnumpy(), rtol, atol, equal_nan=True)
    57. else:
    58. assert np.allclose(expect.asnumpy(), output.asnumpy(), rtol, atol, equal_nan=True)
    59. def get_softmax_output(x, y, use_experimental_features):
    60. # use experimental features such as stitch fusion.
    61. if use_experimental_features:
    62. context.set_context(graph_kernel_flags="--opt_level=3")
    63. net = BertAttentionPiece()
    64. result = net(x, y)
    65. return result
    66. def test_softmax(shape, dtype):
    67. np.random.seed(0)
    68. x = Tensor(np.random.normal(0, 1, shape).astype(dtype))
    69. y = Tensor(np.random.normal(0, 1, shape).astype(dtype))
    70. expect = get_softmax_output(x, y, False)
    71. output = get_softmax_output(x, y, True)
    72. compare_result(expect, output, dtype)
    73. def test_softmax_gpu():
    74. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
    75. test_softmax([64, 12, 128, 128], np.float16)
    76. if __name__ == '__main__':
    77. test_softmax_gpu()

    在均能正确跑出结果的同时,使能了opt_level=3的话,会对这个用例进行一个stitch fusion的优化,以便获取更优性能。 

     上图为单纯使能图算会生成的融合pattern,为3个,而下图为使能了高阶优化后,图算融合会多生成一个融合pattern,增加了融合机会,从而能够获取更优性能。 

    4.建议与总结

    对于GPU后端而言,使能图算融合,一般可以直接打开进行优化尝试,也即优化等级开到2。而在Ascend后端上使能图算融合,可能优化等级开到2会有潜在Bug,如果想尝试图算优化,却遇到Bug的情况,请将opt_level设置为1,也即把最基本的融合优化打开试验性能的优劣情况。最后,尽管GPU后端可以尽可能的使能高的优化层级,但毕竟最高阶的优化在上面也有提及是属于内部实验阶段,可能打开后不一定会有优化,还需用户自己多进行尝试。

    5.相关参考文档

    对于图算融合的内部flag设置,详情可以参考图算flag定义文档 下的说明,可以参照这个说明更灵活的使用图算融合,以尝试获取更优的网络性能。

     

  • 相关阅读:
    学习打卡HTML
    【C语言从0到1之数据类型】
    应用开发平台业务支撑功能——集成阿里EasyExcel组件实现Excel导入导出功能
    快速排序及优化
    【Eye-tracking】DIDEC: The Dutch Image Description and Eye-tracking Corpus
    Go 言 Go 语,一文看懂 Go 语言文件操作
    SQL注入漏洞(类型篇)
    深度学习基础知识 最近邻插值法、双线性插值法、双三次插值算法
    【Linux】分布式版本控制工具git
    Android 桌面小组件使用
  • 原文地址:https://blog.csdn.net/Kenji_Shinji/article/details/126990721