diff --git a/configure.py b/configure.py index 843220b0603228028225f956f4426790f43dd239..30fdd70c357d474a1b2deda06ec1918283610b72 100755 --- a/configure.py +++ b/configure.py @@ -126,7 +126,7 @@ def setup_python(): def setup_ascend(env_path): """Get ascend install path.""" - default_ascend_path = "/usr/local/Ascend" + default_ascend_path = os.path.realpath("/usr/local/Ascend") custom_ascend_path = env_path while True: if not custom_ascend_path: diff --git a/convert_tf2npu/file_op.py b/convert_tf2npu/file_op.py index b1f39505ab42acac26710bd997ead454388a36a3..98a7e02db98b166625d909be723a2f2a48744bde 100644 --- a/convert_tf2npu/file_op.py +++ b/convert_tf2npu/file_op.py @@ -39,15 +39,13 @@ def mkdir_and_copyfile(srcfile, dstpath, file_name): shutil.copyfile(os.path.join(srcfile, file_name), os.path.join(dstpath, file_name)) def write_output_after_conver(out_file, dst_content): - file = open(out_file, 'w') - file.write(dst_content) - file.close() + with open(out_file, 'w') as file: + file.write(dst_content) def write_report_after_conver(new_file_path, report_file, dst_content): mkdir(new_file_path) - file = open(os.path.join(new_file_path, report_file), 'w') - file.write(dst_content) - file.close() + with open(os.path.join(new_file_path, report_file), 'w') as file: + file.write(dst_content) def get_bit_val(value, index): if value & (1 << index): @@ -63,29 +61,26 @@ def write_report_terminator(content): if get_bit_val(value, times - 1): file = util_global.get_value('report_file')[times - 1] if os.path.exists(os.path.join(report_path, file)): - file = open(os.path.join(report_path, file), 'a') - file.write(content) - file.write("\r\n") - file.write("\r\n") - file.close() + with open(os.path.join(report_path, file), 'a') as file: + file.write(content) + file.write("\r\n") + file.write("\r\n") times = times - 1 util_global.set_value('report_file_status', 0) def write_conver_report(content, file): report_path = util_global.get_value('report') mkdir(report_path) - file = open(os.path.join(report_path, file), 'a') - file.write(content) - file.write("\r\n") - file.close() + with open(os.path.join(report_path, file), 'a') as file: + file.write(content) + file.write("\r\n") def write_analysis_report(content, file): report_path = util_global.get_value('report') mkdir(report_path) - file = open(os.path.join(report_path, file), 'a') - file.write(content) - file.write("\r\n") - file.close() + with open(os.path.join(report_path, file), 'a') as file: + file.write(content) + file.write("\r\n") def abs_join(abs1, abs2): abs2 = os.fspath(abs2) diff --git a/script/run_tfplugin_install.sh b/script/run_tfplugin_install.sh index e7bfa213daec70052c8c61ddde5f5881826ac7ea..2fd0652f3a6fc5c5887be0390584a83f07249354 100644 --- a/script/run_tfplugin_install.sh +++ b/script/run_tfplugin_install.sh @@ -156,13 +156,13 @@ install_package() { } PYTHONDIR="${common_parse_dir}/tfplugin" -WHL_INSTALL_DIR_PATH="${PYTHONDIR}/python/site-packages" +WHL_INSTALL_DIR_PATH="${common_parse_dir}/tfplugin/python/site-packages" SOURCE_PATH="${sourcedir}/bin" PYTHON_NPU_BRIDGE_WHL="npu_bridge-1.15.0-py3-none-any.whl" PYTHON_NPU_BRIDGE_NAME="npu_bridge" new_install() { - if [ ! -d ${sourcedir} ]; then + if [ ! -d "${sourcedir}" ]; then log "INFO" "no need to install tfplugin files." return 0 fi diff --git a/script/run_tfplugin_uninstall.sh b/script/run_tfplugin_uninstall.sh index 41dc705bf053aa1320da47e401cd82f70dc67bb3..1b92033e035c80fb919395464b592ad9870d309d 100644 --- a/script/run_tfplugin_uninstall.sh +++ b/script/run_tfplugin_uninstall.sh @@ -119,7 +119,7 @@ uninstall_package() { } PYTHONDIR="${common_parse_dir}""/tfplugin" -WHL_INSTALL_DIR_PATH="${PYTHONDIR}/python/site-packages" +WHL_INSTALL_DIR_PATH="${common_parse_dir}/tfplugin/python/site-packages" NPU_BRIDGE_NAME="npu_bridge" new_uninstall() { diff --git a/script/run_tfplugin_upgrade.sh b/script/run_tfplugin_upgrade.sh index 4ba4add0b1bfcfb2a68f694dbcf693cab4b57c50..e44b6866843b6fdb4d22cb4937e7fab4f7179491 100644 --- a/script/run_tfplugin_upgrade.sh +++ b/script/run_tfplugin_upgrade.sh @@ -102,8 +102,8 @@ if [ -f "$install_info" ]; then usergroup="$tfplugin_user_group" elif [ -f "$install_info_old" ] && [ $(grep -c -i "Acllib_Install_Path_Param" $install_info_old) -ne 0]; then . $install_info_old - username=$UserName - usergroup=$UserGroup + username="$DEFAULT_USERNAME" + usergroup="$DEFAULT_USERGROUP" else echo "ERR_NO:0x0080;ERR_DES:Installation information no longer exists,please complete "${install_info}" or ${install_info_old}" exit 1 @@ -153,7 +153,7 @@ install_package() { } PYTHONDIR="${common_parse_dir}""/tfplugin" -WHL_INSTALL_DIR_PATH="${PYTHONDIR}/python/site-packages" +WHL_INSTALL_DIR_PATH="${common_parse_dir}/tfplugin/python/site-packages" SOURCE_PATH="${sourcedir}/bin" PYTHON_NPU_BRIDGE_WHL="npu_bridge-1.15.0-py3-none-any.whl" PYTHON_NPU_BRIDGE_NAME="npu_bridge" diff --git a/tf_adapter/interface_checker/check_interface.py b/tf_adapter/interface_checker/check_interface.py index 9b8adaa780cb0d3af01cad7173a31b0420a1d055..43d90d58b8b3e11dc8b123a0e40c173e0e4f49b6 100644 --- a/tf_adapter/interface_checker/check_interface.py +++ b/tf_adapter/interface_checker/check_interface.py @@ -1,5 +1,19 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- +# Copyright (C) 2021. Huawei Technologies Co., Ltd. All rights reserved. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at + +# http://www.apache.org/licenses/LICENSE-2.0 + +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================== + """ check interface """ diff --git a/tf_adapter/python/npu_bridge/estimator/npu/mnist_softmax_npu.py b/tf_adapter/python/npu_bridge/estimator/npu/mnist_softmax_npu.py index da00912f282e04915b97d7e7b6d58a206caca321..4d185bf87b37b4cc455ed3d9b5dd4f7b4cebaf55 100644 --- a/tf_adapter/python/npu_bridge/estimator/npu/mnist_softmax_npu.py +++ b/tf_adapter/python/npu_bridge/estimator/npu/mnist_softmax_npu.py @@ -24,6 +24,7 @@ from __future__ import division from __future__ import print_function import argparse +import os import sys import tensorflow as tf @@ -101,7 +102,7 @@ if __name__ == '__main__': parser.add_argument( '--data_dir', type=str, - default='/tmp/tensorflow/mnist/input_data', + default=os.path.realpath('/tmp/tensorflow/mnist/input_data'), help='Directory for storing input data') parser.add_argument( '--npu', type=bool, default=True, help='Turn npu on') diff --git a/tf_adapter_2.x/configure.py b/tf_adapter_2.x/configure.py index 5d93bfa819b240e21da7500507b52fd217e4fc3c..890ea6e83ed2eda47bf53db01428662942acc9ff 100644 --- a/tf_adapter_2.x/configure.py +++ b/tf_adapter_2.x/configure.py @@ -107,7 +107,7 @@ def setup_python(env_path): def setup_ascend(env_path): """Get ascend install path.""" - default_ascend_path = "/usr/local/Ascend" + default_ascend_path = os.path.realpath("/usr/local/Ascend") ask_ascend_path = ('Please specify the location of ascend. [Default is ' '%s]\n(You can make this quiet by set env [ASCEND_INSTALLED_PATH]): ') % default_ascend_path custom_ascend_path = env_path diff --git a/tf_adapter_2.x/examples/basic_tests.py b/tf_adapter_2.x/examples/basic_tests.py index 52cb9d0230b413eeebcc7b45d320eb990e510d1c..4e9cd68a393ef7738bb3bffd366472b4b71abf5e 100644 --- a/tf_adapter_2.x/examples/basic_tests.py +++ b/tf_adapter_2.x/examples/basic_tests.py @@ -1,5 +1,19 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- +# Copyright (C) 2021. Huawei Technologies Co., Ltd. All rights reserved. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at + +# http://www.apache.org/licenses/LICENSE-2.0 + +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================== + import unittest import os import tensorflow as tf diff --git a/tools/gen_sc_makefile_bazel.py b/tools/gen_sc_makefile_bazel.py index 3f1307699ef0f15114eb07a4883ef8d93e5db410..7f7c9e566fa5358018919842b31eb7ba57375ecf 100644 --- a/tools/gen_sc_makefile_bazel.py +++ b/tools/gen_sc_makefile_bazel.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python3 #-*- coding: UTF-8 -*- # Copyright (C) 2021. Huawei Technologies Co., Ltd. All rights reserved. # Licensed under the Apache License, Version 2.0 (the "License"); @@ -26,9 +26,9 @@ source_suffix = ["c","cpp","cc","cce"] def get_gcc_cmd(input_file, cpp_file_list, code_top_dir, custom_code_top_dir=""): gcc_cmd_set = {} lint_cmd_set = {} - file = open(input_file, "r") - lines = file.readlines() - file.close() + lines = [] + with open(input_file, "r") as file: + lines = file.readlines() compile_cmd_regex = re.compile('(gcc|g\+\+|\"aarch64-linux-gnu-gcc\"|ccec)\s+[^ ].*\s-o\s+[^ ]+\.(o|s|cpp|obj)(\s|$)') if(custom_code_top_dir != "") : custom_code_top_dir = custom_code_top_dir.replace(code_top_dir + "/", "") @@ -144,9 +144,9 @@ def walkDir(top_dir,directory): def get_cpp_file_list(top_dir,input_file, custom_code_top_dir=""): cpp_file_list = [] - file = open(input_file, "r") - lines = file.readlines() - file.close() + lines = [] + with open(input_file, "r") as file: + lines = file.readlines() for line in lines: line = line.strip() if custom_code_top_dir != "" : @@ -182,33 +182,32 @@ def main(): print "Error: can not get lint gcc cmd " sys.exit(-1) - fd = open(output_file, "w") - content = "" - content = "".join([content, ".PHONY: all\n"]) - content = "".join([content, "\n"]) - content = "".join([content, "ifeq ($(PCLINT_ENABLE),true)\n"]) - for (obj_file,wine_cmd) in lint_cmd_set.items(): - obj_file = "".join([obj_file , ".lint"]) - content = "".join([content, "all:", obj_file, "\n"]) - content = "".join([content, obj_file, ": FORCE\n"]) - content = "".join([content, "\tmkdir -p $(dir $@)\n"]) - content = "".join([content, "\t", wine_cmd, " > $@ \n"]) + with open(output_file, "w") as fd: + content = "" + content = "".join([content, ".PHONY: all\n"]) content = "".join([content, "\n"]) - content = "".join([content, "else\n"]) - - for (obj_file,gcc_cmd) in gcc_cmd_set.items(): - content = "".join([content, "all:" , obj_file, "\n"]) - content = "".join([content, obj_file, ": FORCE\n"]) - content = "".join([content, "\trm -rf $@\n"]) - content = "".join([content, "\t $(SOURCEANALYZER) ", gcc_cmd, "\n"]) + content = "".join([content, "ifeq ($(PCLINT_ENABLE),true)\n"]) + for (obj_file,wine_cmd) in lint_cmd_set.items(): + obj_file = "".join([obj_file , ".lint"]) + content = "".join([content, "all:", obj_file, "\n"]) + content = "".join([content, obj_file, ": FORCE\n"]) + content = "".join([content, "\tmkdir -p $(dir $@)\n"]) + content = "".join([content, "\t", wine_cmd, " > $@ \n"]) + content = "".join([content, "\n"]) + content = "".join([content, "else\n"]) + + for (obj_file,gcc_cmd) in gcc_cmd_set.items(): + content = "".join([content, "all:" , obj_file, "\n"]) + content = "".join([content, obj_file, ": FORCE\n"]) + content = "".join([content, "\trm -rf $@\n"]) + content = "".join([content, "\t $(SOURCEANALYZER) ", gcc_cmd, "\n"]) + content = "".join([content, "\n"]) + + content = "".join([content, "endif\n"]) content = "".join([content, "\n"]) - - content = "".join([content, "endif\n"]) - content = "".join([content, "\n"]) - content = "".join([content, ".PHONY: FORCE\n"]) - content = "".join([content, "FORCE: \n"]) - fd.write(content) - fd.close() + content = "".join([content, ".PHONY: FORCE\n"]) + content = "".join([content, "FORCE: \n"]) + fd.write(content) if __name__ == '__main__': main()