def attribute(node): log_success_report(getattr(node, "lineno", "None"), node.attr) if node.attr == 'dropout': node.value = ast.Name(id=util_global.get_value(node.attr)[0], ctx=ast.Load()) else: node = ast.Name(id=util_global.get_value(node.attr)[0], ctx=ast.Load()) util_global.set_value('need_conver', True) return node
def ast_function_def(node): log_success_report(getattr(node, "lineno", "None"), node.name) node.body = [ast.Return(value=ast.Call( func=ast.Attribute(value=ast.Name(id=util_global.get_value(node.name)[0], ctx=ast.Load()), attr='gelu', ctx=ast.Load()), args=[ast.Name(id='x', ctx=ast.Load())], keywords=[]))] util_global.set_value('need_conver', True) return node
def ast_call(node): if isinstance(node.func, ast.Attribute): if len(node.args) > 0: if isinstance(node.args[0], ast.Call): if isinstance(node.args[0].func, ast.Attribute): if node.args[0].func.attr == 'BroadcastGlobalVariablesHook': log_success_report(getattr(node, "lineno", "None"), 'BroadcastGlobalVariablesHook') node.func = ast.Name(id=util_global.get_value('BroadcastGlobalVariablesHook')[0], ctx=ast.Load()) node.args = [] util_global.set_value('need_conver', True) if isinstance(node.func, ast.Attribute) and node.func.attr == 'shard': log_success_report(getattr(node, "lineno", "None"), 'shard') node.args = [ast.Call(func=ast.Name(id='get_rank_size', ctx=ast.Load()), args=[], keywords=[]), ast.Call(func=ast.Name(id='get_rank_id', ctx=ast.Load()), args=[], keywords=[])] util_global.set_value('need_conver', True) if isinstance(node.func, ast.Attribute) and (node.func.attr == 'batch' or node.func.attr == 'map_and_batch'): exist = False for keyword in node.keywords: if keyword.arg == 'drop_remainder': exist = True if ((isinstance(keyword.value, ast.NameConstant) and keyword.value.value != True) or (not isinstance(keyword.value, ast.NameConstant))): log_success_report(getattr(node, "lineno", "None"), node.func.attr) keyword.value = ast.NameConstant(value=True) util_global.set_value('need_conver', True) if not exist: log_success_report(getattr(node, "lineno", "None"), node.func.attr) keyword = ast.keyword(arg='drop_remainder', value=ast.NameConstant(value=True)) node.keywords.insert(0, keyword) util_global.set_value('need_conver', True) if (isinstance(node.func, ast.Attribute) and isinstance(node.func.value, ast.Name) and node.func.value.id == 'tf' and node.func.attr == 'device'): log_success_report(getattr(node, "lineno", "None"), node.func.attr) node.args = [ast.Str(s='/cpu:0')] util_global.set_value('need_conver', True) return node
def attribute(node): """Modify node attribute""" log_success_report(getattr(node, "lineno", "None"), node.attr) node = ast.Name(id=util_global.get_value(node.attr)[0], ctx=ast.Load()) util_global.set_value('need_conver', True) return node
def ast_call(node): """Visit and transform ast call node""" distributed_mode = util_global.get_value("distributed_mode", "") is_not_strategy = distributed_mode in ("horovod", "") is_not_horovod = distributed_mode in ("tf_strategy", "") convert_loss_scale_api(node) if _call_name_match(node.func, "set_experimental_options"): log_msg( getattr(node, 'lineno', 'None'), 'change set_experimental_options(*) to set_experimental_options(experimental_options)' ) node.args = [ast.Name(id='experimental_options', ctx=ast.Load())] node.keywords = [] util_global.set_value('need_conver', True) if isinstance(node.func, ast.Name) and node.func.id == 'check_available_gpus': log_msg(getattr(node, 'lineno', 'None'), "change check_available_gpus() to ['/device:CPU:0']") util_global.set_value('need_conver', True) return ast.List(elts=[ast.Str(s="/device:CPU:0")], ctx=ast.Load()) if ((isinstance(node.func, ast.Name) and node.func.id == 'GraphOptions') or (isinstance(node.func, ast.Attribute) and node.func.attr == 'GraphOptions')): log_success_report(getattr(node, 'lineno', 'None'), 'GraphOptions()') src = copy.deepcopy(node) node.func = ast.Name(id='npu_graph_options', ctx=ast.Load()) node.args = [] node.keywords = [] node.keywords.append(ast.keyword(arg='graph_options', value=src)) util_global.set_value('need_conver', True) return node if (isinstance(node.func, ast.Name) and node.func.id == 'OptimizerOptions') or \ (isinstance(node.func, ast.Attribute) and node.func.attr == 'OptimizerOptions'): log_success_report(getattr(node, 'lineno', 'None'), 'OptimizerOptions()') src = copy.deepcopy(node) node.func = ast.Name(id='npu_optimizer_options', ctx=ast.Load()) node.args = [] node.keywords = [] node.keywords.append(ast.keyword(arg='optimizer_options', value=src)) util_global.set_value('need_conver', True) return node if _call_name_match(node.func, "Session"): return convert_origin_func_to_npu(node, tf_func_map["tf.Session"], "tf.Session", ["config"]) if _call_name_match(node.func, "InteractiveSession"): return convert_origin_func_to_npu(node, tf_func_map["tf.InteractiveSession"], "tf.InteractiveSession", ["config"]) if isinstance(node.func, ast.Attribute ) and node.func.attr == "BroadcastGlobalVariablesHook": if isinstance(node.func.value, ast.Name) and node.func.value.id == "hvd": if is_not_horovod: log_strategy_distributed_mode_error(node) return node log_msg( getattr(node, "lineno", "None"), 'change hvd.BroadcastGlobalVariablesHook to NPUBroadcastGlobalVariablesHook' ) node = pasta.parse( "NPUBroadcastGlobalVariablesHook(0, int(os.getenv('RANK_ID', '0')))" ) util_global.set_value('need_conver', True) return node if isinstance(node.func, ast.Attribute ) and node.func.attr == "BroadcastGlobalVariablesCallback": if isinstance(node.func.value, ast.Attribute) and node.func.value.attr == "callbacks": if is_not_horovod: log_strategy_distributed_mode_error(node) return node log_msg( getattr(node, "lineno", "None"), 'change hvd.callbacks.BroadcastGlobalVariablesCallback to NPUBroadcastGlobalVariablesCallback' ) node = pasta.parse( "NPUBroadcastGlobalVariablesCallback(root_rank=0)") util_global.set_value('need_conver', True) return node if isinstance(node.func, ast.Attribute) and node.func.attr == "DistributedOptimizer": if isinstance(node.func.value, ast.Name) and node.func.value.id == "hvd": if is_not_horovod: log_strategy_distributed_mode_error(node) return node return convert_hvd_distributed_api(node) if isinstance(node.func, ast.Attribute) and node.func.attr == 'shard': log_success_report(getattr(node, "lineno", "None"), 'shard') node.args = [ pasta.parse("int(os.getenv('RANK_SIZE', '1'))"), pasta.parse("int(os.getenv('RANK_ID', '0'))") ] node.keywords.clear() util_global.set_value('need_conver', True) return node if isinstance(node.func, ast.Attribute) and node.func.attr == 'dropout': if isinstance(node.func.value, ast.Attribute) and node.func.value.attr == 'nn': for index, _ in enumerate(node.args): if index == 2: return node for keyword in node.keywords: if keyword.arg == "noise_shape": return node log_success_report(getattr(node, "lineno", "None"), 'dropout') node.func = ast.Attribute(value=ast.Name(id='npu_ops', ctx=ast.Load()), attr='dropout', ctx=ast.Load()) keywords_new = [] for keyword in node.keywords: if keyword.arg != 'rate': keywords_new.append(keyword) else: keywords_new.append( ast.keyword(arg='keep_prob', value=ast.BinOp(left=ast.Num(n=1), op=ast.Sub(), right=keyword.value))) node.keywords = keywords_new util_global.set_value('need_conver', True) return node if isinstance(node.func, ast.Attribute) and \ ((node.func.attr == 'map_and_batch') or (node.func.attr == 'batch' and (not isinstance(node.func.value, ast.Attribute) or ( isinstance(node.func.value, ast.Attribute) and node.func.value.attr != 'train')))): exist = False for keyword in node.keywords: if keyword.arg == 'drop_remainder': exist = True if ((isinstance(keyword.value, ast.NameConstant) and not keyword.value.value) or (not isinstance(keyword.value, ast.NameConstant))): log_success_report(getattr(node, "lineno", "None"), node.func.attr) keyword.value = pasta.parse('True') util_global.set_value('need_conver', True) if not exist: log_success_report(getattr(node, "lineno", "None"), node.func.attr) keyword = ast.keyword(arg='drop_remainder', value=pasta.parse('True')) node.keywords.insert(0, keyword) util_global.set_value('need_conver', True) return node if (isinstance(node.func, ast.Attribute) and isinstance(node.func.value, ast.Name) and node.func.value.id == 'tf' and node.func.attr == 'device'): log_success_report(getattr(node, "lineno", "None"), node.func.attr) node.args = [ast.Str(s='/cpu:0')] util_global.set_value('need_conver', True) return node if isinstance(node.func, ast.Attribute) and \ (node.func.attr == "get_distribution_strategy" or node.func.attr == "MirroredStrategy" or node.func.attr == "MultiWorkerMirroredStrategy"): if is_not_strategy: log_hvd_distributed_mode_error(node) return node log_success_report(getattr(node, "lineno", "None"), node.func.attr) new_func = ast.Attribute(value=ast.Name(id="npu_strategy", ctx=ast.Load()), attr="NPUStrategy", ctx=ast.Load()) ast.copy_location(new_func, node.func) node.func = new_func node.keywords = [] node.args = [] util_global.set_value('need_conver', True) return node if (isinstance(node.func, ast.Attribute) and (node.func.attr == 'RunConfig')) and \ (_call_name_match(node.func.value, 'estimator') or _call_name_match(node.func.value, 'tpu')): if node.keywords.count("train_distribute") or node.keywords.count( "eval_distribute"): if is_not_strategy: log_hvd_distributed_mode_error(node) save_summary_steps = None for keyword in node.keywords: if keyword.arg == 'save_summary_steps': save_summary_steps = keyword break if len(node.args) < 3 and not save_summary_steps: log_msg(getattr(node, 'lineno'), 'RunConfig() add save_summary_steps=0') util_global.set_value('need_conver', True) node.keywords.append( ast.keyword(arg='save_summary_steps', value=pasta.parse('0'))) return node if isinstance(node.func, ast.Attribute) and (node.func.attr == 'TPUEstimator') and \ ((isinstance(node.func.value, ast.Attribute) and (node.func.value.attr == 'tpu')) or (isinstance(node.func.value, ast.Name) and (node.func.value.id == 'tpu'))): add_eval_on_tpu = True add_use_tpu = True add_export_to_tpu = True for keyword in node.keywords: if (keyword.arg == 'eval_on_tpu') or ( keyword.arg == 'use_tpu') or (keyword.arg == 'export_to_tpu'): if (not isinstance(keyword.value, ast.NameConstant)) or \ (isinstance(keyword.value, ast.NameConstant) and (keyword.value.value)): log_success_report(getattr(node, 'lineno', 'None'), 'TPUEstimator(' + keyword.arg + '=*)') keyword.value = pasta.parse('False') util_global.set_value('need_conver', True) if add_eval_on_tpu and (keyword.arg == 'eval_on_tpu'): add_eval_on_tpu = False if add_use_tpu and (keyword.arg == 'use_tpu'): add_use_tpu = False if add_export_to_tpu and (keyword.arg == 'export_to_tpu'): add_export_to_tpu = False if add_eval_on_tpu: log_success_report(getattr(node, 'lineno', 'None'), 'TPUEstimator(eval_on_tpu=*)') node.keywords.append( ast.keyword(arg='eval_on_tpu', value=pasta.parse('False'))) util_global.set_value('need_conver', True) if add_use_tpu: log_success_report(getattr(node, 'lineno', 'None'), 'TPUEstimator(use_tpu=*)') node.keywords.append( ast.keyword(arg='use_tpu', value=pasta.parse('False'))) util_global.set_value('need_conver', True) if add_export_to_tpu: log_success_report(getattr(node, 'lineno', 'None'), 'TPUEstimator(export_to_tpu=*)') node.keywords.append( ast.keyword(arg='export_to_tpu', value=pasta.parse('False'))) util_global.set_value('need_conver', True) if isinstance(node.func, ast.Attribute) and (node.func.attr == 'VirtualDeviceConfiguration'): log_success_report(getattr(node, 'lineno', 'None'), 'VirtualDeviceConfiguration') util_global.set_value('need_conver', True) memory_limit = None for keyword in node.keywords: if keyword.arg == 'memory_limit': memory_limit = keyword break if memory_limit: memory_limit.value = ast.NameConstant(value=None) else: node.keywords.append( ast.keyword(arg='memory_limit', value=ast.NameConstant(value=None))) return node if isinstance(node.func, ast.Attribute) and (node.func.attr == 'set_soft_device_placement'): log_success_report(getattr(node, 'lineno', 'None'), 'set_soft_device_placement') util_global.set_value('need_conver', True) node.args = [] node.keywords = [ ast.keyword(arg='enabled', value=ast.NameConstant(value=True)) ] return node if isinstance(node.func, ast.Attribute) and (node.func.attr == 'set_memory_growth'): log_success_report(getattr(node, 'lineno', 'None'), 'set_memory_growth') util_global.set_value('need_conver', True) node = ast.NameConstant(value=None) return node if isinstance(node.func, ast.Attribute) and (node.func.attr == 'set_virtual_device_configuration'): log_success_report(getattr(node, 'lineno', 'None'), 'set_virtual_device_configuration') util_global.set_value('need_conver', True) node = ast.NameConstant(value=None) return node if isinstance(node.func, ast.Attribute) and (node.func.attr == 'jit_scope'): if isinstance(node.func.value, ast.Attribute) and (node.func.value.attr == 'experimental'): if isinstance(node.func.value.value, ast.Attribute) and (node.func.value.value.attr == 'xla'): log_success_report(getattr(node, 'lineno', 'None'), '*.xla.experimental.jit_scope') util_global.set_value('need_conver', True) compile_ops = None for keyword in node.keywords: if keyword.arg == 'compile_ops': compile_ops = keyword break if compile_ops: compile_ops.value = pasta.parse('False') else: node.keywords.append( ast.keyword(arg='compile_ops', value=pasta.parse('False'))) return node for estimator in util_global.get_value('Estimators', []): if (isinstance(node.func, ast.Attribute) and (node.func.attr == estimator)) \ or (isinstance(node.func, ast.Name) and (node.func.id == estimator)): log_msg( getattr(node, 'lineno'), "".join([estimator, '() add config=npu_run_config_init()'])) config = None for keyword in node.keywords: if keyword.arg == 'config': config = keyword break if config: new_value = ast.Call(func=ast.Name(id='npu_run_config_init', ctx=ast.Load()), args=[], keywords=[ ast.keyword(arg='run_config', value=config.value) ]) ast.copy_location(new_value, config.value) config.value = new_value else: node.keywords.append( ast.keyword(arg='config', value=pasta.parse('npu_run_config_init()'))) util_global.set_value('need_conver', True) return node if isinstance(node.func, ast.Attribute) and (node.func.attr == 'clear_session'): log_msg(getattr(node, 'lineno'), "change keras.clear_session() to npu_clear_session()") node = ast.Call(func=ast.Name(id='npu_clear_session', ctx=ast.Load()), args=[], keywords=[]) util_global.set_value('need_conver', True) if _call_name_match(node.func, "MonitoredTrainingSession"): return convert_origin_func_to_npu( node, tf_func_map["tf.train.MonitoredTrainingSession"], "MonitoredTrainingSession", ["config", "hooks"]) if isinstance(node.func, ast.Attribute) and node.func.attr == "managed_session": return convert_origin_func_to_npu( node, tf_func_map["tf.train.Supervisor.managed_session"], "managed_session", ["config"], True) if distributed_mode == "tf_strategy": # this cond should be placed at the end of the Call function. return convert_distributed_strategy_apis(node) return node
def ast_call(node): convert_loss_scale_api(node) if _call_name_match(node.func, "set_experimental_options"): log_msg( getattr(node, 'lineno', 'None'), 'change set_experimental_options(*) to set_experimental_options(experimental_options)' ) node.args = [ast.Name(id='experimental_options', ctx=ast.Load())] node.keywords = [] util_global.set_value('need_conver', True) if isinstance(node.func, ast.Name) and node.func.id == 'check_available_gpus': log_msg(getattr(node, 'lineno', 'None'), "change check_available_gpus() to ['/device:CPU:0']") util_global.set_value('need_conver', True) return ast.List(elts=[ast.Str(s="/device:CPU:0")], ctx=ast.Load()) if (isinstance(node.func, ast.Name) and node.func.id == 'ConfigProto') or \ (isinstance(node.func, ast.Attribute) and node.func.attr == 'ConfigProto'): log_success_report(getattr(node, 'lineno', 'None'), 'ConfigProto()') src = copy.deepcopy(node) node.func = ast.Name(id='npu_config_proto', ctx=ast.Load()) node.args = [] node.keywords = [] node.keywords.append(ast.keyword(arg='config_proto', value=src)) util_global.set_value('need_conver', True) return node if (isinstance(node.func, ast.Name) and node.func.id == 'GraphOptions') or \ (isinstance(node.func, ast.Attribute) and node.func.attr == 'GraphOptions'): log_success_report(getattr(node, 'lineno', 'None'), 'GraphOptions()') src = copy.deepcopy(node) node.func = ast.Name(id='npu_graph_options', ctx=ast.Load()) node.args = [] node.keywords = [] node.keywords.append(ast.keyword(arg='graph_options', value=src)) util_global.set_value('need_conver', True) return node if (isinstance(node.func, ast.Name) and node.func.id == 'OptimizerOptions') or \ (isinstance(node.func, ast.Attribute) and node.func.attr == 'OptimizerOptions'): log_success_report(getattr(node, 'lineno', 'None'), 'OptimizerOptions()') src = copy.deepcopy(node) node.func = ast.Name(id='npu_optimizer_options', ctx=ast.Load()) node.args = [] node.keywords = [] node.keywords.append(ast.keyword(arg='optimizer_options', value=src)) util_global.set_value('need_conver', True) return node if (isinstance(node.func, ast.Name) and node.func.id == 'Session') or \ (isinstance(node.func, ast.Attribute) and node.func.attr == 'Session'): log_success_report(getattr(node, 'lineno', 'None'), 'Session()') config = None for index, _ in enumerate(node.args): if index == 2: config = node.args.pop(2) break for keyword in node.keywords: if keyword.arg == 'config': config = keyword break if config: if isinstance(config, ast.keyword): config.value = ast.Call(func=ast.Name(id='npu_config_proto', ctx=ast.Load()), args=[], keywords=[ ast.keyword(arg='config_proto', value=config.value) ]) else: node.keywords.append( ast.keyword(arg='config', value=ast.Call( func=ast.Name(id='npu_config_proto', ctx=ast.Load()), args=[], keywords=[ ast.keyword(arg='config_proto', value=config) ]))) else: node.keywords.append( ast.keyword(arg='config', value=ast.Call(func=ast.Name(id='npu_config_proto', ctx=ast.Load()), args=[], keywords=[]))) util_global.set_value('need_conver', True) return node if isinstance(node.func, ast.Attribute ) and node.func.attr == "BroadcastGlobalVariablesHook": log_success_report(getattr(node, "lineno", "None"), 'BroadcastGlobalVariablesHook') node.func = ast.Name(id="NpuEmptyHook", ctx=ast.Load()) node.args = [] node.keywords = [] util_global.set_value('need_conver', True) return node if isinstance(node.func, ast.Attribute) and node.func.attr == "DistributedOptimizer": log_success_report(getattr(node, "lineno", "None"), 'DistributedOptimizer') return node.args[0] if isinstance(node.func, ast.Attribute) and node.func.attr == 'shard': log_success_report(getattr(node, "lineno", "None"), 'shard') node.args = [ ast.Call(func=ast.Name(id='get_rank_size', ctx=ast.Load()), args=[], keywords=[]), ast.Call(func=ast.Name(id='get_rank_id', ctx=ast.Load()), args=[], keywords=[]) ] util_global.set_value("has_hccl_api", True) util_global.set_value('need_conver', True) if isinstance(node.func, ast.Attribute) and node.func.attr == 'dropout': if isinstance(node.func.value, ast.Attribute) and node.func.value.attr == 'nn': log_success_report(getattr(node, "lineno", "None"), 'dropout') node.func = ast.Attribute(value=ast.Name(id='npu_ops', ctx=ast.Load()), attr='dropout', ctx=ast.Load()) keywords_new = [] for keyword in node.keywords: if keyword.arg != 'rate': keywords_new.append(keyword) node.keywords = keywords_new util_global.set_value('need_conver', True) if isinstance(node.func, ast.Attribute) and ((node.func.attr == 'map_and_batch') or (node.func.attr == 'batch' \ and (not isinstance(node.func.value, ast.Attribute) or (isinstance(node.func.value, ast.Attribute) and node.func.value.attr != 'train')))): exist = False for keyword in node.keywords: if keyword.arg == 'drop_remainder': exist = True if ((isinstance(keyword.value, ast.NameConstant) and keyword.value.value != True) or (not isinstance(keyword.value, ast.NameConstant))): log_success_report(getattr(node, "lineno", "None"), node.func.attr) keyword.value = pasta.parse('True') util_global.set_value('need_conver', True) if not exist: log_success_report(getattr(node, "lineno", "None"), node.func.attr) keyword = ast.keyword(arg='drop_remainder', value=pasta.parse('True')) node.keywords.insert(0, keyword) util_global.set_value('need_conver', True) if (isinstance(node.func, ast.Attribute) and isinstance(node.func.value, ast.Name) and node.func.value.id == 'tf' and node.func.attr == 'device'): log_success_report(getattr(node, "lineno", "None"), node.func.attr) node.args = [ast.Str(s='/cpu:0')] util_global.set_value('need_conver', True) if isinstance(node.func, ast.Attribute) and ( node.func.attr == "get_distribution_strategy" or node.func.attr == "MirroredStrategy" or node.func.attr == "MultiWorkerMirroredStrategy"): log_success_report(getattr(node, "lineno", "None"), node.func.attr) new_func = ast.Attribute(value=ast.Name(id="npu_strategy", ctx=ast.Load()), attr="NPUStrategy", ctx=ast.Load()) ast.copy_location(new_func, node.func) node.func = new_func node.keywords = [] node.args = [] util_global.set_value('need_conver', True) if isinstance(node.func, ast.Attribute) and (node.func.attr == 'AdviceProto'): log_success_report(getattr(node, 'lineno', 'None'), 'tf.profiler.AdviceProto') util_global.set_value('need_conver', True) node = ast.NameConstant(value=None) return node if isinstance(node.func, ast.Attribute) and (node.func.attr == 'Checker'): log_success_report(getattr(node, 'lineno', 'None'), 'tf.profiler.AdviceProto.Checker') util_global.set_value('need_conver', True) node = ast.NameConstant(value=None) return node if isinstance(node.func, ast.Attribute) and (node.func.attr == 'CheckersEntry'): log_success_report(getattr(node, 'lineno', 'None'), 'tf.profiler.AdviceProto.CheckersEntry') util_global.set_value('need_conver', True) node = ast.NameConstant(value=None) return node if isinstance(node.func, ast.Attribute) and (node.func.attr == 'GraphNodeProto'): log_success_report(getattr(node, 'lineno', 'None'), 'tf.profiler.GraphNodeProto') util_global.set_value('need_conver', True) node = ast.NameConstant(value=None) return node if isinstance(node.func, ast.Attribute) and (node.func.attr == 'InputShapesEntry'): log_success_report(getattr(node, 'lineno', 'None'), 'tf.profiler.GraphNodeProto.InputShapesEntry') util_global.set_value('need_conver', True) node = ast.NameConstant(value=None) return node if isinstance(node.func, ast.Attribute) and (node.func.attr == 'MultiGraphNodeProto'): log_success_report(getattr(node, 'lineno', 'None'), 'tf.profiler.MultiGraphNodeProto') util_global.set_value('need_conver', True) node = ast.NameConstant(value=None) return node if isinstance(node.func, ast.Attribute) and (node.func.attr == 'OpLogProto'): log_success_report(getattr(node, 'lineno', 'None'), 'tf.profiler.OpLogProto') util_global.set_value('need_conver', True) node = ast.NameConstant(value=None) return node if isinstance(node.func, ast.Attribute) and (node.func.attr == 'IdToStringEntry'): log_success_report(getattr(node, 'lineno', 'None'), 'tf.profiler.OpLogProto.IdToStringEntry') util_global.set_value('need_conver', True) node = ast.NameConstant(value=None) return node if isinstance(node.func, ast.Attribute) and (node.func.attr == 'ProfileOptionBuilder'): log_success_report(getattr(node, 'lineno', 'None'), 'tf.profiler.ProfileOptionBuilder') util_global.set_value('need_conver', True) node = ast.NameConstant(value=None) return node if isinstance(node.func, ast.Attribute) and (node.func.attr == 'advise'): log_success_report(getattr(node, 'lineno', 'None'), 'tf.profiler.advise') util_global.set_value('need_conver', True) node = ast.NameConstant(value=None) return node if isinstance(node.func, ast.Attribute) and (node.func.attr == 'profile'): log_success_report(getattr(node, 'lineno', 'None'), 'tf.profiler.profile') util_global.set_value('need_conver', True) node = ast.NameConstant(value=None) return node if isinstance(node.func, ast.Attribute) and (node.func.attr == 'write_op_log'): log_success_report(getattr(node, 'lineno', 'None'), 'tf.profiler.write_op_log') util_global.set_value('need_conver', True) node = ast.NameConstant(value=None) return node if isinstance(node.func, ast.Attribute) and (node.func.attr == 'TPUEstimator') and \ ((isinstance(node.func.value, ast.Attribute) and (node.func.value.attr == 'tpu')) or \ (isinstance(node.func.value, ast.Name) and (node.func.value.id == 'tpu'))): add_eval_on_tpu = True add_use_tpu = True add_export_to_tpu = True for keyword in node.keywords: if (keyword.arg == 'eval_on_tpu') or ( keyword.arg == 'use_tpu') or (keyword.arg == 'export_to_tpu'): if (not isinstance(keyword.value, ast.NameConstant)) or ( isinstance(keyword.value, ast.NameConstant) and (keyword.value.value != False)): log_success_report(getattr(node, 'lineno', 'None'), 'TPUEstimator(' + keyword.arg + '=*)') keyword.value = pasta.parse('False') util_global.set_value('need_conver', True) if add_eval_on_tpu and (keyword.arg == 'eval_on_tpu'): add_eval_on_tpu = False if add_use_tpu and (keyword.arg == 'use_tpu'): add_use_tpu = False if add_export_to_tpu and (keyword.arg == 'export_to_tpu'): add_export_to_tpu = False if add_eval_on_tpu: log_success_report(getattr(node, 'lineno', 'None'), 'TPUEstimator(eval_on_tpu=*)') node.keywords.append( ast.keyword(arg='eval_on_tpu', value=pasta.parse('False'))) util_global.set_value('need_conver', True) if add_use_tpu: log_success_report(getattr(node, 'lineno', 'None'), 'TPUEstimator(use_tpu=*)') node.keywords.append( ast.keyword(arg='use_tpu', value=pasta.parse('False'))) util_global.set_value('need_conver', True) if add_export_to_tpu: log_success_report(getattr(node, 'lineno', 'None'), 'TPUEstimator(export_to_tpu=*)') node.keywords.append( ast.keyword(arg='export_to_tpu', value=pasta.parse('False'))) util_global.set_value('need_conver', True) if isinstance(node.func, ast.Attribute) and (node.func.attr == 'VirtualDeviceConfiguration'): log_success_report(getattr(node, 'lineno', 'None'), 'VirtualDeviceConfiguration') util_global.set_value('need_conver', True) memory_limit = None for keyword in node.keywords: if keyword.arg == 'memory_limit': memory_limit = keyword break if memory_limit: memory_limit.value = ast.NameConstant(value=None) else: node.keywords.append( ast.keyword(arg='memory_limit', value=ast.NameConstant(value=None))) return node if isinstance(node.func, ast.Attribute) and (node.func.attr == 'set_soft_device_placement'): log_success_report(getattr(node, 'lineno', 'None'), 'set_soft_device_placement') util_global.set_value('need_conver', True) node.args = [] node.keywords = [ ast.keyword(arg='enabled', value=ast.NameConstant(value=True)) ] return node if isinstance(node.func, ast.Attribute) and (node.func.attr == 'set_memory_growth'): log_success_report(getattr(node, 'lineno', 'None'), 'set_memory_growth') util_global.set_value('need_conver', True) node = ast.NameConstant(value=None) return node if isinstance(node.func, ast.Attribute) and (node.func.attr == 'set_virtual_device_configuration'): log_success_report(getattr(node, 'lineno', 'None'), 'set_virtual_device_configuration') util_global.set_value('need_conver', True) node = ast.NameConstant(value=None) return node if isinstance(node.func, ast.Attribute) and (node.func.attr == 'jit_scope'): if isinstance(node.func.value, ast.Attribute) and (node.func.value.attr == 'experimental'): if isinstance(node.func.value.value, ast.Attribute) and (node.func.value.value.attr == 'xla'): log_success_report(getattr(node, 'lineno', 'None'), '*.xla.experimental.jit_scope') util_global.set_value('need_conver', True) compile_ops = None for keyword in node.keywords: if keyword.arg == 'compile_ops': compile_ops = keyword break if compile_ops: compile_ops.value = pasta.parse('False') else: node.keywords.append( ast.keyword(arg='compile_ops', value=pasta.parse('False'))) return node for estimator in util_global.get_value('Estimators', []): if (isinstance(node.func, ast.Attribute) and (node.func.attr == estimator)) \ or (isinstance(node.func, ast.Name) and (node.func.id == estimator)): log_msg(getattr(node, 'lineno'), estimator + '() add config=npu_run_config_init()') config = None for keyword in node.keywords: if keyword.arg == 'config': config = keyword break if config: new_value = ast.Call(func=ast.Name(id='npu_run_config_init', ctx=ast.Load()), args=[], keywords=[ ast.keyword(arg='run_config', value=config.value) ]) ast.copy_location(new_value, config.value) config.value = new_value else: node.keywords.append( ast.keyword(arg='config', value=pasta.parse('npu_run_config_init()'))) util_global.set_value('need_conver', True) return node for estimator_func in util_global.get_value('EstimatorFunc', []): if isinstance(node.func, ast.Attribute) and (node.func.attr == estimator_func): if isinstance( node.func.value, ast.Attribute) and node.func.value.attr == "learning": return node input_fn = None hooks = None for index, _ in enumerate(node.args): if index == 0: input_fn = node.args[0] elif index == 1: hooks = node.args.pop(1) for keyword in node.keywords: if keyword.arg == 'input_fn': input_fn = keyword elif keyword.arg == 'hooks': hooks = keyword if not input_fn: break if not hooks: node.keywords.append( ast.keyword(arg='hooks', value=pasta.parse('npu_hooks_append()'))) elif isinstance(hooks, ast.keyword): new_value = ast.Call(func=ast.Name(id='npu_hooks_append', ctx=ast.Load()), args=[], keywords=[ ast.keyword(arg='hooks_list', value=hooks.value) ]) ast.copy_location(new_value, hooks.value) hooks.value = new_value else: node.keywords.append( ast.keyword(arg='hooks', value=ast.Call( func=ast.Name(id='npu_hooks_append', ctx=ast.Load()), args=[], keywords=[ ast.keyword(arg='hooks_list', value=hooks) ]))) util_global.set_value('need_conver', True) return node if isinstance(node.func, ast.Attribute) and (node.func.attr == 'compile'): opt_map = { "adadelta": "tf.keras.optimizers.Adadelta()", "adagrad": "tf.keras.optimizers.Adagrad()", "adam": "tf.keras.optimizers.Adam()", "adamax": "tf.keras.optimizers.Adamax()", "ftrl": "tf.keras.optimizers.Ftrl()", "nadam": "tf.keras.optimizers.Nadam()", "rmsprop": "tf.keras.optimizers.RMSprop()", "sgd": "tf.keras.optimizers.SGD()" } for keyword in node.keywords: if keyword.arg == "optimizer": log_success_report(getattr(node, 'lineno', 'None'), 'KerasDistributeOptimizer') if isinstance(keyword.value, ast.Str): keras_opt = opt_map[keyword.value.s] npu_keras_opt = "npu_keras_optimizer(" + keras_opt + ")" keyword.value = pasta.parse(npu_keras_opt) util_global.set_value('need_conver', True) return node if isinstance(node.func, ast.Attribute) and isinstance( node.func.value, ast.Attribute): if (node.func.attr.find("Optimizer") != -1) and (node.func.attr != 'ScipyOptimizerInterface'): log_msg(getattr(node, "lineno", "None"), "add NPUDistributedOptimizer()") new_node = ast.Call(func=ast.Name(id="npu_tf_optimizer", ctx=ast.Load()), args=[node], keywords=[]) ast.copy_location(new_node, node) util_global.set_value('need_conver', True) return new_node if isinstance(node.func, ast.Attribute): opt_list = [ "Adadelta", "Adagrad", "Adam", "Adamax", "Ftrl", "Nadam", "RMSprop", "SGD" ] if node.func.attr in opt_list: log_success_report(getattr(node, "lineno", "None"), "KerasDistributeOptimizer") new_node = ast.Call(func=ast.Name(id="npu_keras_optimizer", ctx=ast.Load()), args=[node], keywords=[]) ast.copy_location(new_node, node) util_global.set_value('need_conver', True) return new_node if (isinstance(node.func, ast.Attribute) and (node.func.attr == 'MonitoredTrainingSession')) or \ (isinstance(node.func, ast.Name) and (node.func.id == 'MonitoredTrainingSession')): log_success_report(getattr(node, "lineno", "None"), 'MonitoredTrainingSession') hooks = None for index, _ in enumerate(node.args): if index == 4: hooks = node.args.pop(4) break for keyword in node.keywords: if keyword.arg == 'hooks': hooks = keyword break if not hooks: node.keywords.append( ast.keyword(arg='hooks', value=pasta.parse('npu_hooks_append()'))) elif isinstance(hooks, ast.keyword): new_value = ast.Call( func=ast.Name(id='npu_hooks_append', ctx=ast.Load()), args=[], keywords=[ast.keyword(arg='hooks_list', value=hooks.value)]) ast.copy_location(new_value, hooks.value) hooks.value = new_value else: node.keywords.append( ast.keyword(arg='hooks', value=ast.Call(func=ast.Name(id='npu_hooks_append', ctx=ast.Load()), args=[], keywords=[ ast.keyword(arg='hooks_list', value=hooks) ]))) util_global.set_value('need_conver', True) return node specs = {'TrainSpec': 2, 'EvalSpec': 3} for spec, hooks_index in specs.items(): if _call_name_match(node.func, spec): log_success_report(getattr(node, "lineno", "None"), spec) hooks = None for index, _ in enumerate(node.args): if index == hooks_index: hooks = node.args.pop(hooks_index) break for keyword in node.keywords: if keyword.arg == 'hooks': hooks = keyword break if not hooks: node.keywords.append( ast.keyword(arg='hooks', value=pasta.parse('npu_hooks_append()'))) elif isinstance(hooks, ast.keyword): new_value = ast.Call(func=ast.Name(id='npu_hooks_append', ctx=ast.Load()), args=[], keywords=[ ast.keyword(arg='hooks_list', value=hooks.value) ]) ast.copy_location(new_value, hooks.value) hooks.value = new_value else: node.keywords.append( ast.keyword(arg='hooks', value=ast.Call( func=ast.Name(id='npu_hooks_append', ctx=ast.Load()), args=[], keywords=[ ast.keyword(arg='hooks_list', value=hooks) ]))) util_global.set_value('need_conver', True) return node