Ascend平台提供了TensorFlow 1.15网络迁移工具,该工具适用于原生的TensorFlow训练脚本迁移场景,AI算法工程师通过该工具分析原生的TensorFlow Python API和Horovod Python API在昇腾AI处理器上的支持度情况,同时将原生的TensorFlow训练脚本自动迁移成昇腾AI处理器支持的脚本。对于无法自动迁移的API,您可以参考工具输出的迁移报告,对训练脚本进行相应的适配修改。
import tensorflow as tf import tensorflow.compat.v1 as tf import horovod.tensorflow as hvd
在昇腾AI处理器进行模型迁移之前,建议用户事先准备好基于TensorFlow 1.15开发的训练模型以及配套的数据集,并要求在GPU或CPU上跑通,精度收敛,且达到预期精度和性能要求。同时记录相关精度和性能指标,用于后续在昇腾AI处理器进行精度和性能对比。
pip3 install pandas
pip3 install xlrd==1.2.0
pip3 install openpyxl
pip3 install tkintertable
pip3 install google_pasta
进入迁移工具所在目录,例如“tfplugin安装目录/tfplugin/latest/python/site-packages/npu_bridge/convert_tf2npu/”,执行命令可同时完成脚本扫描和自动迁移,例如:
python3 main.py -i /root/models/official/resnet
其中main.py为工具入口脚本,参数说明如下所示。
参数名 |
参数说明 |
可选/必选 |
---|---|---|
-i |
被迁移的原始脚本路径,当前该路径仅支持配置为文件夹,不支持单个文件。 说明:
|
必选 |
-o |
指定迁移后的脚本路径,该路径不能为原始脚本路径的子目录。 该参数可选,如果不指定,默认生成在当前路径下,例如output_npu_20210401150929/xxx_npu_20210401150929。 |
可选 |
-r |
指定生成的迁移报告路径,该路径不能为原始脚本路径的子目录。 该参数可选,如果不指定,默认生成在当前路径下,例如report_npu_20210401150929。 |
可选 |
-m |
Python执行入口文件。 如果原始脚本使用了tf.keras/hvd接口,且脚本中没有main函数,由于迁移工具无法识别入口函数,因此无法进行NPU资源初始化,以及NPU训练相关配置。 对于以上场景,需要通过-m参数指定Python执行的入口文件,以便工具可以将用户脚本进行彻底迁移,保证后续训练的顺利执行。 配置示例:-m /root/models/xxx.py |
可选 |
-d |
如果原始脚本支持分布式训练,需要指定原始脚本使用的分布式策略,便于工具对分布式脚本进行自动迁移。取值:
目前session run分布式脚本无法彻底进行自动迁移,使用工具自动迁移完后,需要参考sess.run分布式脚本迁移进行后续的手工改造。 |
分布式必选 |
通过python3 main.py -h可以获取迁移工具使用帮助。
python3 main_win.py
在弹出的窗口根据界面提示进行操作。
# 表示resnet_run_loop.py第91行的map_and_batch()的drop_remainder参数配置为True /root/models/models/official/resnet/resnet_run_loop.py:91 change map_and_batch(xxx) to map_and_batch(xxx, drop_remainder=True) # 表示resnet_run_loop.py第4行新增“from npu_bridge.npu_init import *”头文件引用,用于导入NPU相关库 /root/models/models/official/resnet/resnet_run_loop.py:4 from npu_bridge.npu_init import * Finish conver file: /root/models/models/official/resnet/resnet_run_loop.py
/root/ast_test/hvd/model_lib.py:704 is horovod api. The '-d' argument conflicts with the Horovod distributed strategy in your script Finish conver file: /root/ast_test/hvd/model_lib.py /root/ast_test/hvd/test.py:3, NPU Unsupport API: hvd.allreduce
工具迁移API支持度 |
说明 |
---|---|
支持(无需迁移) |
此类API在昇腾AI处理器上绝对支持,无需适配修改。 例如"tf.abs"等接口,在昇腾AI处理器上能够完全支持,不需要迁移。 |
工具迁移后API功能支持 |
工具迁移后,该API在昇腾AI处理器上可以支持。 |
工具迁移后训练功能打通 |
工具迁移后,能够保证在昇腾AI处理器训练执行成功,但原有API功能可能不完全支持。 例如"tf.config.experimental.set_memory_growth"接口,它的作用一般是将所有GPU设置为仅在需要时申请显存空间,在昇腾AI处理器上训练时,该接口实际并不生效。 因此,迁移工具会直接return返回None,从而保证在昇腾AI处理器上训练正常执行。 "tf.config.experimental.set_memory_growth" --> 直接return返回None |
不支持(不影响迁移,无需干预) |
此类API在昇腾AI处理器上不支持,但不影响脚本执行,无需用户干预。 例如"tf.config.experimental.get_memory_growth"接口,由于工具会将"tf.config.experimental.set_memory_growth"直接return返回None,因此对应的get接口也不会影响脚本在昇腾AI处理器上的执行,即便出现这个接口,用户也无需干预。 |
不支持(无迁移方案,建议不使用) |
此类API在昇腾AI处理器上不支持,且当前暂无具体迁移方案,建议您不要使用,否则会引起训练失败。 例如"tf.distribute.experimental.TPUStrategy"等TPU相关接口,需通过Google TPU设备执行,昇腾AI处理器上不支持,建议用户不要使用。 另外,"tf.enable_eager_execution"等eager功能相关接口,在当前版本也不支持,建议用户不要使用。 |
废弃类 |
此类API在TensorFlow 1.15版本已经废弃,建议用户使用TensorFlow官网推荐的API,否则可能会引起训练失败。 例如"tf.colocate_with"等接口。 |
兼容类 |
此类API主要作为TensorFlow 2.X版本中支持TensorFlow 1.X功能所使用,在TensorFlow 1.X脚本中不会使用到。 例如"tf.compat.v1.abs"和"tf.compat.v2.abs"等接口。 |
# 未去重的统计结果,分类和API支持度表中的一致 1.In brief: Total API: 231, in which Support: 222, API support after migration: 5, Network training support after migration: 0, Not support but no impact on migration: 0, Not support or recommended: 2, Compatible: 0, Deprecated: 2, Analysing: 0 # 去重后的统计结果,分类和API支持度表中的一致 2.After eliminate duplicate: Total API: 98, in which Support: 92, API support after migration: 4, Network training support after migration: 0, Not support but no impact on migration: 0, Not support or recommended: 1, Compatible: 0, Deprecated: 1, Analysing: 0
if __name__ == '__main__': session_config = tf.ConfigProto(allow_soft_placement=True) custom_op = session_config.graph_options.rewrite_options.custom_optimizers.add() custom_op.name = "NpuOptimizer" custom_op.parameter_map["profiling_mode"].b = True ... ... (npu_sess, npu_shutdown) = init_resource(config=session_config) tf.app.run() shutdown_resource(npu_sess, npu_shutdown) close_session(npu_sess)
session_config = tf.ConfigProto(allow_soft_placement=True) custom_op = session_config.graph_options.rewrite_options.custom_optimizers.add() custom_op.name = 'NpuOptimizer' custom_op.parameter_map["aoe_mode"].s = tf.compat.as_bytes("2") run_config = tf.estimator.RunConfig( train_distribute=distribution_strategy, session_config=session_config, save_checkpoints_secs=60*60*24) classifier = tf.estimator.Estimator( model_fn=model_function, model_dir=flags_obj.model_dir, config=npu_run_config_init(run_config=run_config))
session_config = tf.ConfigProto(allow_soft_placement=True) custom_op = session_config.graph_options.rewrite_options.custom_optimizers.add() custom_op.name = 'NpuOptimizer' custom_op.parameter_map["aoe_mode"].s = tf.compat.as_bytes("2") config = npu_config_proto(config_proto=session_config) with tf.Session(config=config) as sess: sess.run(tf.global_variables_initializer()) interaction_table.init.run()
import tensorflow as tf import tensorflow.python.keras as keras from tensorflow.python.keras import backend as K from npu_bridge.npu_init import * config_proto = tf.ConfigProto(allow_soft_placement=True) custom_op = config_proto.graph_options.rewrite_options.custom_optimizers.add() custom_op.name = 'NpuOptimizer' custom_op.parameter_map["aoe_mode"].s = tf.compat.as_bytes("2") npu_keras_sess = set_keras_session_npu_config(config=config_proto) #数据预处理... #模型搭建... #模型编译... #模型训练...