!23078 Get bprop from python when the bprop mindir dir does not exist

Merge pull request !23078 from YuJianfeng/bprop_mindir
This commit is contained in:
i-robot 2021-09-09 09:51:14 +00:00 committed by Gitee
commit 5c72e9d7c6
2 changed files with 46 additions and 15 deletions

View File

@ -16,6 +16,9 @@
* limitations under the License.
*/
#ifndef _WIN32
#include <dirent.h>
#endif
#include <memory>
#include <string>
#include <utility>
@ -52,9 +55,35 @@ constexpr char serializable_bprop_ops[] = "serializable_bprop_ops";
constexpr char bprop_mindir_module[] = "mindspore.ops.bprop_mindir";
#ifndef _WIN32
std::string GetBpropDir() {
static std::string bprop_dir;
if (bprop_dir.empty()) {
py::module mod = py::module::import("mindspore.ops._grad");
auto grad_file_path = mod.attr("__file__").cast<std::string>();
bprop_dir = grad_file_path.substr(0, grad_file_path.find_last_of('/'));
}
return bprop_dir;
}
bool BpropMindirDirExists() {
auto bprop_mindir_dir = GetBpropDir() + kBpropMindIRDir;
DIR *dir = opendir(bprop_mindir_dir.c_str());
if (dir != nullptr) {
if (closedir(dir) == -1) {
MS_LOG(WARNING) << "The bprop mindir dir \"" << bprop_mindir_dir << "\" close failed!";
}
return true;
}
MS_LOG(INFO) << "The bprop mindir dir \"" << bprop_mindir_dir << "\" doesn't exists.";
return false;
}
// Get the serializable bprop list from the module mindspore.ops.bprop_mindir in python.
std::unordered_set<std::string> GetSerializableBpropList() {
std::unordered_set<std::string> serializable_bprop_list;
if (!BpropMindirDirExists()) {
return serializable_bprop_list;
}
py::module mod = py::module::import(bprop_mindir_module);
py::object serializable_bprop_ops_attr = mod.attr(serializable_bprop_ops);
if (!py::isinstance<py::list>(serializable_bprop_ops_attr)) {
@ -78,24 +107,10 @@ bool IsSerializableBprop(const std::string &prim_name) {
static std::unordered_set<std::string> serializable_bprop_list = GetSerializableBpropList();
return std::any_of(serializable_bprop_list.begin(), serializable_bprop_list.end(),
[&prim_name](const std::string &serializable_bprop_prim_name) {
auto str1 = prim_name;
auto str2 = serializable_bprop_prim_name;
(void)transform(str1.begin(), str1.end(), str1.begin(), ::tolower);
(void)transform(str2.begin(), str2.end(), str2.begin(), ::tolower);
return str1 == str2;
return prim_name == serializable_bprop_prim_name;
});
}
std::string GetBpropDir() {
static std::string bprop_dir;
if (bprop_dir.empty()) {
py::module mod = py::module::import("mindspore.ops._grad");
auto grad_file_path = mod.attr("__file__").cast<std::string>();
bprop_dir = grad_file_path.substr(0, grad_file_path.find_last_of('/'));
}
return bprop_dir;
}
std::string GetBpropHash() {
static std::string bprop_hash;
if (bprop_hash.empty()) {

View File

@ -13,6 +13,7 @@
# limitations under the License.
# ============================================================================
"""Generate the mindir for bprop"""
import os
import numpy as np
import mindspore.nn as nn
@ -22,6 +23,7 @@ import mindspore.ops as ops
from mindspore.ops.operations import _inner_ops as inner
import mindspore.common.dtype as mstype
from mindspore.common.initializer import initializer
import mindspore.ops._grad as g
class Net(nn.Cell):
@ -54,6 +56,20 @@ class GradNet(nn.Cell):
return gout
def test_remove_mindir_dir():
bprop_path = g.__file__
bprop_installed_dir = bprop_path[: bprop_path.rindex('/')]
bprop_mindir_export_dir = bprop_installed_dir + "/../bprop_mindir"
os.rename(bprop_mindir_export_dir, bprop_mindir_export_dir + "_bak")
x = Tensor(np.array([[[[-1, 1, 10],
[1, -1, 1],
[10, 1, -1]]]]).astype(np.float32))
relu = Net(P.ReLU())
grad = GradNet(relu)
grad.compile(x)
os.rename(bprop_mindir_export_dir + "_bak", bprop_mindir_export_dir)
def test_relu():
x = Tensor(np.array([[[[-1, 1, 10],
[1, -1, 1],