在模型训练阶段,可以通过Auto Tune工具全自动地完成模型中算子的性能自动优化,提升模型端到端的运行速度。Auto tune能够自动完成模型的性能优化,同时输出知识库文件。用户可以将知识库文件部署到新的环境上,同样的模型无需再次开启Auto tune即可获得之前调优的性能。
Auto Tune工具包含RL和GA两种调优模式:
RL调优模式主要支持elewise、broadcast、reduce类的算子,主要调优原理为:将Schedule过程抽象为基于蒙特卡洛树搜索(Monte Carlo tree search:MCTS,一种用于某些决策过程中的启发式搜索算法)的决策链,然后使用NN(Neural Networks)指导决策,其中NN基于RL进行训练生成。
GA调优模式主要支持cube类的算子,主要调优原理为:通过多级组合优化生成调优空间,加入人工经验进行剪枝,排序,提高调优的效率;利用遗传算法进行多轮的参数寻优,从而获得最优的tiling策略。
默认训练过程中进行AutoTune自动调优,如需使能,请参考本节内容修改训练脚本。
关于AutoTune功能的更多介绍,请参考《CANN 开发工具指南》中的“Auto Tune工具使用指南”章节。
from npu_bridge.npu_init import * npu_config=NPURunConfig( model_dir=FLAGS.model_dir, save_checkpoints_steps=FLAGS.save_checkpoints_steps, session_config=tf.ConfigProto(allow_soft_placement=True,log_device_placement=False), auto_tune_mode="RL,GA" )
import tensorflow as tf from npu_bridge.npu_init import * config = tf.ConfigProto() custom_op = config.graph_options.rewrite_options.custom_optimizers.add() custom_op.name = "NpuOptimizer" custom_op.parameter_map["use_off_line"].b = True custom_op.parameter_map["auto_tune_mode"].s = tf.compat.as_bytes("RL,GA") config.graph_options.rewrite_options.remapping = RewriterConfig.OFF config.graph_options.rewrite_options.memory_optimization = RewriterConfig.OFF with tf.Session(config=config) as sess: print(sess.run(cost))