• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2021 Huawei Technologies Co., Ltd
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ============================================================================
15"""Generate the mindir for bprop"""
16import os
17import shutil
18import argparse
19
20from mindspore.ops import operations as P
21import mindspore.ops._grad as g
22from mindspore.ops.operations import _inner_ops as inner
23from mindspore._c_expression import _export_bprop_mindir
24
25serializable_bprop_ops = [P.ReLU(), P.Identity(), inner.Range(1.0), P.OnesLike(), P.ZerosLike(), P.Argmax(), P.Argmin(),
26                          P.Broadcast(1), P.AssignAdd(), P.AssignSub(), P.IsFinite(), P.ApproximateEqual(), P.Sign(),
27                          P.LogicalNot(), P.Round(), P.LinSpace(), P.DropoutGenMask(), P.OneHot(), P.Assign(), P.IOU(),
28                          P.BNTrainingReduce(), P.Equal(), P.NotEqual(), P.Greater(), P.GreaterEqual(), P.Less(),
29                          P.LessEqual(), P.LogicalAnd(), P.LogicalOr(), P.ReduceAll(), P.ReduceAny(), P.DropoutDoMask()]
30
31
32def run_generate():
33    for op in serializable_bprop_ops:
34        _export_bprop_mindir(op)
35
36
37if __name__ == "__main__":
38    parser = argparse.ArgumentParser(description="Bprop generator")
39    parser.add_argument('--mindspore_path', type=str, default=None,
40                        help="The absolute path of the mindspore root directory where the bprop source files has been \
41                        modified. If not specified, it will find the bprop source files in your mindspore installed \
42                        path. Default: None.")
43
44    args_opt = parser.parse_args()
45    # mindspore/ops/_grad/__init__.py
46    bprop_path = g.__file__
47    bprop_installed_dir = bprop_path[: bprop_path.rindex('/')]
48    bprop_mindir_export_dir = bprop_installed_dir + "/../bprop_mindir"
49
50    mindspore_path = args_opt.mindspore_path
51    bprop_src_dir = None
52    bprop_mindir_src_dir = None
53    if not mindspore_path is None:
54        mindspore_path = mindspore_path.rstrip('/')
55        bprop_src_dir = mindspore_path + "/mindspore/ops/_grad"
56        bprop_mindir_src_dir = mindspore_path + "/mindspore/ops/bprop_mindir"
57
58    copy_flag = not bprop_src_dir is None and bprop_src_dir != bprop_installed_dir
59    # If the specified bprop source directory is not on the mindspore installed path,
60    # copy the bprop source files to the installed path.
61    backup_suffix = "_generate_bak"
62    if copy_flag is True:
63        shutil.rmtree(bprop_installed_dir + backup_suffix, ignore_errors=True)
64        os.rename(bprop_installed_dir, bprop_installed_dir + backup_suffix)
65        os.mkdir(bprop_installed_dir)
66        ls = os.listdir(bprop_src_dir)
67        for line in ls:
68            file_path = os.path.join(bprop_src_dir, line)
69            if os.path.isfile(file_path):
70                print("copy: " + file_path)
71                shutil.copy(file_path, bprop_installed_dir)
72
73    run_generate()
74
75    # If the specified bprop source directory is not on the mindspore installed path,
76    # copy the generated mindir files to the mindir directory relative to the specified path.
77    if copy_flag is True:
78        shutil.rmtree(bprop_installed_dir)
79        os.rename(bprop_installed_dir + backup_suffix, bprop_installed_dir)
80        ls = os.listdir(bprop_mindir_export_dir)
81        for line in ls:
82            file_path = os.path.join(bprop_mindir_export_dir, line)
83            if file_path.endswith(".mindir") and os.path.isfile(file_path):
84                print("copy: " + file_path)
85                shutil.copy(file_path, bprop_mindir_src_dir)
86
87        print("The new bprop mindir files has been generated in the path \"" + bprop_mindir_src_dir +
88              "\".")
89    else:
90        print("The new bprop mindir files has been generated in the path \"" + bprop_mindir_export_dir +
91              "\", copy the *.mindir to your mindspore path or PYTHONPATH if necessary.")
92