!18898 Add bprop directory arg to the the bprop mindir generator

Merge pull request !18898 from YuJianfeng/bprop_mindir
This commit is contained in:
i-robot 2021-06-28 01:15:23 +00:00 committed by Gitee
commit 19e95c5c8e
1 changed files with 62 additions and 7 deletions

View File

@ -14,6 +14,8 @@
# ============================================================================
"""Generate the mindir for bprop"""
import os
import shutil
import argparse
import numpy as np
import mindspore.context as context
@ -72,10 +74,63 @@ def test_identity():
grad(x)
test_relu()
test_identity()
# mindspore/ops/_grad/__init__.py
bprop_path = g.__file__
bprop_mindir_path = bprop_path[: bprop_path.rindex('/')] + "/../bprop_mindir/"
print("The new bprop mindir files has been generated in the path \"" + bprop_mindir_path +
"\", copy the *.mindir to your PYTHONPATH if necessary.")
def run_generate():
test_relu()
test_identity()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Bprop generator")
parser.add_argument('--mindspore_path', type=str, default=None,
help="The absolute path of the mindspore root directory where the bprop source files has been \
modified. If not specified, it will find the bprop source files in your mindspore installed \
path. Default: None.")
args_opt = parser.parse_args()
# mindspore/ops/_grad/__init__.py
bprop_path = g.__file__
bprop_installed_dir = bprop_path[: bprop_path.rindex('/')]
bprop_mindir_export_dir = bprop_installed_dir + "/../bprop_mindir"
mindspore_path = args_opt.mindspore_path
bprop_src_dir = None
bprop_mindir_src_dir = None
if not mindspore_path is None:
mindspore_path = mindspore_path.rstrip('/')
bprop_src_dir = mindspore_path + "/mindspore/ops/_grad"
bprop_mindir_src_dir = mindspore_path + "/mindspore/ops/bprop_mindir"
copy_flag = not bprop_src_dir is None and bprop_src_dir != bprop_installed_dir
# If the specified bprop source directory is not on the mindspore installed path,
# copy the bprop source files to the installed path.
backup_suffix = "_generate_bak"
if copy_flag is True:
shutil.rmtree(bprop_installed_dir + backup_suffix)
os.rename(bprop_installed_dir, bprop_installed_dir + backup_suffix)
os.mkdir(bprop_installed_dir)
ls = os.listdir(bprop_src_dir)
for line in ls:
file_path = os.path.join(bprop_src_dir, line)
if os.path.isfile(file_path):
print("copy: " + file_path)
shutil.copy(file_path, bprop_installed_dir)
run_generate()
# If the specified bprop source directory is not on the mindspore installed path,
# copy the generated mindir files to the mindir directory relative to the specified path.
if copy_flag is True:
shutil.rmtree(bprop_installed_dir)
os.rename(bprop_installed_dir + backup_suffix, bprop_installed_dir)
ls = os.listdir(bprop_mindir_export_dir)
for line in ls:
file_path = os.path.join(bprop_mindir_export_dir, line)
if file_path.endswith(".mindir") and os.path.isfile(file_path):
print("copy: " + file_path)
shutil.copy(file_path, bprop_mindir_src_dir)
print("The new bprop mindir files has been generated in the path \"" + bprop_mindir_src_dir +
"\".")
else:
print("The new bprop mindir files has been generated in the path \"" + bprop_mindir_export_dir +
"\", copy the *.mindir to your PYTHONPATH if necessary.")