separate py from debug and utils
This commit is contained in:
parent
04398cf88e
commit
a9e30a96db
|
@ -6,6 +6,7 @@ set(_DEBUG_SRC_LIST
|
|||
"${CMAKE_CURRENT_SOURCE_DIR}/info.cc"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/label.cc"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/trace_info.cc"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/trace_base.cc"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/trace.cc"
|
||||
)
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/**
|
||||
* Copyright 2019 Huawei Technologies Co., Ltd
|
||||
* Copyright 2019-2020 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -20,7 +20,7 @@
|
|||
#include <sstream>
|
||||
#include <climits>
|
||||
#include "ir/anf.h"
|
||||
#include "utils/convert_utils.h"
|
||||
#include "utils/convert_utils_base.h"
|
||||
|
||||
namespace mindspore {
|
||||
std::string HighLightLine(const std::string &line, int col_begin, int col_end, SourceLineTip tip) {
|
||||
|
|
|
@ -54,91 +54,6 @@ std::string GetAbstractStr(const abstract::AbstractBasePtr &abs) {
|
|||
return oss.str();
|
||||
}
|
||||
|
||||
std::vector<DebugInfoPtr> GetSourceCodeDebugInfoVec(DebugInfoPtr debug_info) {
|
||||
std::vector<DebugInfoPtr> debug_with_loc_vec;
|
||||
while (debug_info != nullptr) {
|
||||
if (debug_info->location() != nullptr) {
|
||||
debug_with_loc_vec.push_back(debug_info);
|
||||
}
|
||||
if (debug_info->trace_info() != nullptr) {
|
||||
debug_info = debug_info->trace_info()->debug_info();
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return debug_with_loc_vec;
|
||||
}
|
||||
|
||||
DebugInfoPtr GetSourceCodeDebugInfo(const DebugInfoPtr &info) {
|
||||
auto debug_with_loc_vec = GetSourceCodeDebugInfoVec(info);
|
||||
if (debug_with_loc_vec.size() > 0) {
|
||||
return debug_with_loc_vec[0];
|
||||
} else {
|
||||
return info;
|
||||
}
|
||||
}
|
||||
|
||||
std::string GetDebugInfo(const DebugInfoPtr &info, SourceLineTip tip) {
|
||||
if (info == nullptr) {
|
||||
return "";
|
||||
}
|
||||
auto src_info = GetSourceCodeDebugInfo(info);
|
||||
if (src_info->location() != nullptr) {
|
||||
return src_info->location()->ToString(tip);
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
// a trace info identifies a node transform, so we can trace the node transform through
|
||||
// a link of trace info and debug info
|
||||
std::string GetInfoWithAction(const std::vector<DebugInfoPtr> &info_vec, SourceLineTip tip) {
|
||||
if (info_vec.size() < 1) {
|
||||
return "";
|
||||
}
|
||||
if (info_vec.size() == 1) {
|
||||
return info_vec[0]->location()->ToString(tip);
|
||||
}
|
||||
std::string traced_info = info_vec[0]->location()->ToString(tip);
|
||||
for (size_t i = 1; i < info_vec.size(); i++) {
|
||||
auto action_name = info_vec[i - 1]->trace_info()->GetActionBetweenNode(info_vec[i]);
|
||||
if (action_name == "") {
|
||||
break;
|
||||
}
|
||||
traced_info = traced_info + action_name + info_vec[i]->location()->ToString(tip);
|
||||
}
|
||||
return traced_info;
|
||||
}
|
||||
|
||||
std::string GetTracedDebugInfo(const DebugInfoPtr &info, SourceLineTip tip) {
|
||||
if (info == nullptr) {
|
||||
return "";
|
||||
}
|
||||
auto info_vec = GetSourceCodeDebugInfoVec(info);
|
||||
if (info_vec.size() == 0) {
|
||||
return "";
|
||||
} else if (info_vec.size() == 1) {
|
||||
return info_vec[0]->location()->ToString(tip);
|
||||
} else if (info_vec.size() > 1) {
|
||||
return GetInfoWithAction(info_vec, tip);
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
std::string GetDebugInfo(const DebugInfoPtr &info, const std::string &prefix, SourceLineTip tip) {
|
||||
std::ostringstream oss;
|
||||
if (info == nullptr) {
|
||||
return "";
|
||||
}
|
||||
|
||||
auto debug_info = GetTracedDebugInfo(info, tip);
|
||||
if (tip == kSourceLineTipDiscard) {
|
||||
std::replace(debug_info.begin(), debug_info.end(), '\r', '/');
|
||||
std::replace(debug_info.begin(), debug_info.end(), '\n', '/');
|
||||
}
|
||||
oss << prefix << debug_info;
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
std::string GetGraphParamString(const FuncGraphPtr &graph, abstract::AbstractBasePtrList args_spec_list) {
|
||||
std::ostringstream oss;
|
||||
oss << "graph:" << graph->ToString() << " with args[";
|
||||
|
|
|
@ -23,17 +23,15 @@
|
|||
#include <utility>
|
||||
#include <stack>
|
||||
|
||||
#include "ir/anf.h"
|
||||
#include "utils/any.h"
|
||||
#include "ir/func_graph.h"
|
||||
#include "debug/trace_base.h"
|
||||
#include "debug/info.h"
|
||||
#include "ir/anf.h"
|
||||
#include "ir/func_graph.h"
|
||||
#include "pipeline/static_analysis/static_analysis.h"
|
||||
#include "utils/any.h"
|
||||
|
||||
namespace mindspore {
|
||||
namespace trace {
|
||||
std::string GetDebugInfo(const DebugInfoPtr &info, SourceLineTip tip = kSourceLineTipNextLine);
|
||||
std::string GetDebugInfo(const DebugInfoPtr &info, const std::string &prefix,
|
||||
SourceLineTip tip = kSourceLineTipNextLine);
|
||||
DebugInfoPtr GetSourceCodeDebugInfo(const DebugInfoPtr &info);
|
||||
void TraceGraphEval();
|
||||
void GetEvalStackInfo(std::ostringstream &oss);
|
||||
|
|
|
@ -0,0 +1,120 @@
|
|||
/**
|
||||
* Copyright 2020 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include "debug/trace_base.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <map>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <utility>
|
||||
#include <stack>
|
||||
#include <algorithm>
|
||||
|
||||
#include "utils/graph_utils.h"
|
||||
|
||||
namespace mindspore {
|
||||
// namespace to support debug trace infomation
|
||||
namespace trace {
|
||||
std::vector<DebugInfoPtr> GetSourceCodeDebugInfoVec(DebugInfoPtr debug_info) {
|
||||
std::vector<DebugInfoPtr> debug_with_loc_vec;
|
||||
while (debug_info != nullptr) {
|
||||
if (debug_info->location() != nullptr) {
|
||||
debug_with_loc_vec.push_back(debug_info);
|
||||
}
|
||||
if (debug_info->trace_info() != nullptr) {
|
||||
debug_info = debug_info->trace_info()->debug_info();
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return debug_with_loc_vec;
|
||||
}
|
||||
|
||||
DebugInfoPtr GetSourceCodeDebugInfo(const DebugInfoPtr &info) {
|
||||
auto debug_with_loc_vec = GetSourceCodeDebugInfoVec(info);
|
||||
if (debug_with_loc_vec.size() > 0) {
|
||||
return debug_with_loc_vec[0];
|
||||
} else {
|
||||
return info;
|
||||
}
|
||||
}
|
||||
|
||||
std::string GetDebugInfo(const DebugInfoPtr &info, SourceLineTip tip) {
|
||||
if (info == nullptr) {
|
||||
return "";
|
||||
}
|
||||
auto src_info = GetSourceCodeDebugInfo(info);
|
||||
if (src_info->location() != nullptr) {
|
||||
return src_info->location()->ToString(tip);
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
// a trace info identifies a node transform, so we can trace the node transform through
|
||||
// a link of trace info and debug info
|
||||
std::string GetInfoWithAction(const std::vector<DebugInfoPtr> &info_vec, SourceLineTip tip) {
|
||||
if (info_vec.size() < 1) {
|
||||
return "";
|
||||
}
|
||||
if (info_vec.size() == 1) {
|
||||
return info_vec[0]->location()->ToString(tip);
|
||||
}
|
||||
std::string traced_info = info_vec[0]->location()->ToString(tip);
|
||||
for (size_t i = 1; i < info_vec.size(); i++) {
|
||||
auto action_name = info_vec[i - 1]->trace_info()->GetActionBetweenNode(info_vec[i]);
|
||||
if (action_name == "") {
|
||||
break;
|
||||
}
|
||||
traced_info = traced_info + action_name + info_vec[i]->location()->ToString(tip);
|
||||
}
|
||||
return traced_info;
|
||||
}
|
||||
|
||||
std::string GetTracedDebugInfo(const DebugInfoPtr &info, SourceLineTip tip) {
|
||||
if (info == nullptr) {
|
||||
return "";
|
||||
}
|
||||
auto info_vec = GetSourceCodeDebugInfoVec(info);
|
||||
if (info_vec.size() == 0) {
|
||||
return "";
|
||||
} else if (info_vec.size() == 1) {
|
||||
return info_vec[0]->location()->ToString(tip);
|
||||
} else if (info_vec.size() > 1) {
|
||||
return GetInfoWithAction(info_vec, tip);
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
std::string GetDebugInfo(const DebugInfoPtr &info, const std::string &prefix, SourceLineTip tip) {
|
||||
std::ostringstream oss;
|
||||
if (info == nullptr) {
|
||||
return "";
|
||||
}
|
||||
|
||||
auto debug_info = GetTracedDebugInfo(info, tip);
|
||||
if (tip == kSourceLineTipDiscard) {
|
||||
std::replace(debug_info.begin(), debug_info.end(), '\r', '/');
|
||||
std::replace(debug_info.begin(), debug_info.end(), '\n', '/');
|
||||
}
|
||||
oss << prefix << debug_info;
|
||||
return oss.str();
|
||||
}
|
||||
} // namespace trace
|
||||
} // namespace mindspore
|
|
@ -0,0 +1,39 @@
|
|||
/**
|
||||
* Copyright 2020 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MINDSPORE_CCSRC_DEBUG_TRACE_BASE_H_
|
||||
#define MINDSPORE_CCSRC_DEBUG_TRACE_BASE_H_
|
||||
|
||||
#include <fstream>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <utility>
|
||||
#include <stack>
|
||||
|
||||
#include "debug/info.h"
|
||||
#include "ir/anf.h"
|
||||
#include "ir/func_graph.h"
|
||||
#include "utils/any.h"
|
||||
|
||||
namespace mindspore {
|
||||
namespace trace {
|
||||
std::string GetDebugInfo(const DebugInfoPtr &info, SourceLineTip tip = kSourceLineTipNextLine);
|
||||
std::string GetDebugInfo(const DebugInfoPtr &info, const std::string &prefix,
|
||||
SourceLineTip tip = kSourceLineTipNextLine);
|
||||
} // namespace trace
|
||||
} // namespace mindspore
|
||||
|
||||
#endif // MINDSPORE_CCSRC_DEBUG_TRACE_BASE_H_
|
|
@ -1,7 +1,7 @@
|
|||
/**
|
||||
* This is the C++ adaptation and derivative work of Myia (https://github.com/mila-iqia/myia/).
|
||||
*
|
||||
* Copyright 2019 Huawei Technologies Co., Ltd
|
||||
* Copyright 2019-2020 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -17,12 +17,14 @@
|
|||
*/
|
||||
|
||||
#include "ir/dtype/type.h"
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
|
||||
#include <algorithm>
|
||||
#include "utils/log_adapter.h"
|
||||
#include <cstdlib>
|
||||
#include <string>
|
||||
|
||||
#include "ir/dtype/number.h"
|
||||
#include "utils/convert_utils.h"
|
||||
#include "utils/log_adapter.h"
|
||||
#include "utils/convert_utils_base.h"
|
||||
|
||||
namespace mindspore {
|
||||
TypeId IntBitsToTypeId(const int nbits) {
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
/**
|
||||
* This is the C++ adaptation and derivative work of Myia (https://github.com/mila-iqia/myia/).
|
||||
*
|
||||
* Copyright 2019 Huawei Technologies Co., Ltd
|
||||
* Copyright 2019-2020 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -31,6 +31,7 @@
|
|||
#include <type_traits>
|
||||
#include <unordered_map>
|
||||
#include <algorithm>
|
||||
|
||||
#include "ir/base.h"
|
||||
#include "ir/named.h"
|
||||
|
||||
|
|
|
@ -27,7 +27,7 @@
|
|||
#include "operator/ops.h"
|
||||
#include "pybind_api/export_flags.h"
|
||||
#include "utils/ordered_set.h"
|
||||
#include "utils/convert_utils.h"
|
||||
#include "utils/convert_utils_base.h"
|
||||
|
||||
namespace mindspore {
|
||||
/*
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/**
|
||||
* Copyright 2019 Huawei Technologies Co., Ltd
|
||||
* Copyright 2019-2020 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -21,6 +21,7 @@
|
|||
#include "ir/manager.h"
|
||||
#include "ir/param_value_py.h"
|
||||
#include "operator/ops.h"
|
||||
#include "utils/convert_utils_base.h"
|
||||
#include "utils/log_adapter.h"
|
||||
#include "utils/profile.h"
|
||||
#include "utils/context/ms_context.h"
|
||||
|
|
|
@ -22,11 +22,11 @@
|
|||
#include <numeric>
|
||||
#include <list>
|
||||
|
||||
#include "debug/trace_base.h"
|
||||
#include "ir/func_graph.h"
|
||||
#include "utils/profile.h"
|
||||
#include "utils/convert_utils.h"
|
||||
#include "utils/convert_utils_base.h"
|
||||
#include "operator/ops.h"
|
||||
#include "debug/trace.h"
|
||||
|
||||
namespace mindspore {
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/**
|
||||
* Copyright 2019 Huawei Technologies Co., Ltd
|
||||
* Copyright 2019-2020 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -23,7 +23,7 @@
|
|||
#include "pipeline/parse/python_adapter.h"
|
||||
#include "pipeline/parse/data_converter.h"
|
||||
#include "pybind11/pytypes.h"
|
||||
#include "utils/convert_utils.h"
|
||||
#include "utils/convert_utils_base.h"
|
||||
#include "utils/primitive_utils.h"
|
||||
|
||||
#include "pybind_api/api_register.h"
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/**
|
||||
* Copyright 2019 Huawei Technologies Co., Ltd
|
||||
* Copyright 2019-2020 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -15,11 +15,13 @@
|
|||
*/
|
||||
|
||||
#include "ir/value.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <memory>
|
||||
#include <cmath>
|
||||
#include <cfloat>
|
||||
#include "utils/convert_utils.h"
|
||||
|
||||
#include "utils/convert_utils_base.h"
|
||||
|
||||
namespace mindspore {
|
||||
const ValuePtr ValueSequeue::operator[](const std::size_t &dim) const {
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
/**
|
||||
* This is the C++ adaptation and derivative work of Myia (https://github.com/mila-iqia/myia/).
|
||||
*
|
||||
* Copyright 2019 Huawei Technologies Co., Ltd
|
||||
* Copyright 2019-2020 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -33,7 +33,6 @@
|
|||
#include "utils/config_manager.h"
|
||||
#include "utils/convert_utils.h"
|
||||
#include "utils/utils.h"
|
||||
#include "utils/base_ref.h"
|
||||
#include "vm/segment_runner.h"
|
||||
#include "parallel/context.h"
|
||||
#include "parallel/graph_util/get_parallel_info.h"
|
||||
|
|
|
@ -24,6 +24,8 @@
|
|||
#include <unordered_map>
|
||||
#include <map>
|
||||
#include <mutex>
|
||||
|
||||
#include "utils/base_ref_extends.h"
|
||||
#include "debug/draw.h"
|
||||
#include "ir/anf.h"
|
||||
#include "ir/tensor.h"
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
/**
|
||||
* This is the C++ adaptation and derivative work of Myia (https://github.com/mila-iqia/myia/).
|
||||
*
|
||||
* Copyright 2019 Huawei Technologies Co., Ltd
|
||||
* Copyright 2019-2020 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -26,6 +26,7 @@
|
|||
#include "optimizer/opt.h"
|
||||
|
||||
#include "ir/anf.h"
|
||||
#include "utils/convert_utils_base.h"
|
||||
#include "utils/overload.h"
|
||||
|
||||
namespace mindspore {
|
||||
|
|
|
@ -42,6 +42,7 @@
|
|||
#include "device/kernel_runtime_manager.h"
|
||||
#include "kernel/tbe/tbe_python_funcs.h"
|
||||
#include "utils/config_manager.h"
|
||||
#include "utils/base_ref_extends.h"
|
||||
|
||||
namespace mindspore {
|
||||
namespace session {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/**
|
||||
* Copyright 2019 Huawei Technologies Co., Ltd
|
||||
* Copyright 2019-2020 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -27,6 +27,7 @@
|
|||
#include "common/utils.h"
|
||||
#include "common/trans.h"
|
||||
#include "utils/context/ms_context.h"
|
||||
#include "utils/base_ref_extends.h"
|
||||
|
||||
namespace mindspore {
|
||||
namespace session {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/**
|
||||
* Copyright 2019 Huawei Technologies Co., Ltd
|
||||
* Copyright 2019-2020 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
|
|
@ -22,12 +22,13 @@
|
|||
#include <utility>
|
||||
#include <memory>
|
||||
#include <map>
|
||||
|
||||
#include "utils/base_ref_extends.h"
|
||||
#include "session/session_context.h"
|
||||
#include "session/kernel_graph.h"
|
||||
#include "ir/anf.h"
|
||||
#include "ir/tensor.h"
|
||||
#include "utils/any.h"
|
||||
#include "utils/base_ref.h"
|
||||
#include "utils/contract.h"
|
||||
#include "pynative/pynative_execute.h"
|
||||
#include "device/kernel_info.h"
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/**
|
||||
* Copyright 2019 Huawei Technologies Co., Ltd
|
||||
* Copyright 2019-2020 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -30,11 +30,6 @@ bool AnyIsLiteral(const Any &any) {
|
|||
return typeid_int == typeid_any || typeid_float == typeid_any || typeid_bool == typeid_any;
|
||||
}
|
||||
|
||||
std::ostream &operator<<(std::ostream &os, const pybind11::object &obj) {
|
||||
os << "[py::object]";
|
||||
return os;
|
||||
}
|
||||
|
||||
Any &Any::operator=(const Any &other) {
|
||||
if (m_ptr == other.m_ptr || &other == this) {
|
||||
return *this;
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/**
|
||||
* Copyright 2019 Huawei Technologies Co., Ltd
|
||||
* Copyright 2019-2020 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -26,8 +26,6 @@
|
|||
#include <vector>
|
||||
#include <utility>
|
||||
|
||||
#include "pybind11/pybind11.h"
|
||||
|
||||
#include "utils/overload.h"
|
||||
#include "utils/log_adapter.h"
|
||||
#include "utils/misc.h"
|
||||
|
@ -39,8 +37,6 @@ std::string type(const T &t) {
|
|||
return demangle(typeid(t).name());
|
||||
}
|
||||
|
||||
std::ostream &operator<<(std::ostream &os, const pybind11::object &obj);
|
||||
|
||||
class Any {
|
||||
public:
|
||||
// constructors
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/**
|
||||
* Copyright 2019 Huawei Technologies Co., Ltd
|
||||
* Copyright 2019-2020 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -185,13 +185,4 @@ bool RunFunctionRef::operator==(const BaseRef &other) const {
|
|||
}
|
||||
|
||||
bool RunFunctionRef::operator==(const RunFunctionRef &other) const { return func_ == other.func_; }
|
||||
|
||||
bool PyObjectRef::operator==(const BaseRef &other) const {
|
||||
if (!utils::isa<PyObjectRef>(other)) {
|
||||
return false;
|
||||
}
|
||||
return *this == utils::cast<PyObjectRef>(other);
|
||||
}
|
||||
|
||||
bool PyObjectRef::operator==(const PyObjectRef &other) const { return object_ == other.object_; }
|
||||
} // namespace mindspore
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/**
|
||||
* Copyright 2019 Huawei Technologies Co., Ltd
|
||||
* Copyright 2019-2020 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -25,20 +25,16 @@
|
|||
#include <sstream>
|
||||
#include <utility>
|
||||
#include <iterator>
|
||||
#include "pybind11/pybind11.h"
|
||||
#include "ir/value.h"
|
||||
|
||||
namespace py = pybind11;
|
||||
#include "ir/value.h"
|
||||
|
||||
namespace mindspore {
|
||||
class BaseRef;
|
||||
class VectorRef;
|
||||
class SetRef;
|
||||
class PyObjectRef;
|
||||
class RunFunctionRef;
|
||||
|
||||
using iterator = std::vector<BaseRef>::iterator;
|
||||
|
||||
using const_iterator = std::vector<BaseRef>::const_iterator;
|
||||
using const_reverse_iterator = std::vector<BaseRef>::const_reverse_iterator;
|
||||
|
||||
|
@ -88,8 +84,6 @@ inline std::shared_ptr<VectorRef> MakeNode(const AnfNodePtrList &a) {
|
|||
}
|
||||
inline std::shared_ptr<SetRef> MakeNode(const SetRef &a) { return std::make_shared<SetRef>(std::move(a)); }
|
||||
inline std::shared_ptr<RunFunctionRef> MakeNode(const RunFuncPtr &a) { return std::make_shared<RunFunctionRef>(a); }
|
||||
inline std::shared_ptr<PyObjectRef> MakeNode(const py::object &a) { return std::make_shared<PyObjectRef>(a); }
|
||||
inline std::shared_ptr<PyObjectRef> MakeNode(const py::tuple &a) { return std::make_shared<PyObjectRef>(a); }
|
||||
|
||||
class BaseRef : public Base {
|
||||
public:
|
||||
|
@ -367,24 +361,6 @@ class SetRef : public BaseRef {
|
|||
|
||||
using SetRefPtr = std::shared_ptr<SetRef>;
|
||||
|
||||
class PyObjectRef : public BaseRef {
|
||||
public:
|
||||
explicit PyObjectRef(const py::object &py_object) : object_(py_object) {}
|
||||
explicit PyObjectRef(const py::tuple &tuple_obj) : object_(tuple_obj) {}
|
||||
|
||||
~PyObjectRef() override = default;
|
||||
|
||||
std::shared_ptr<Base> copy() const override { return std::make_shared<PyObjectRef>(object_); }
|
||||
MS_DECLARE_PARENT(PyObjectRef, BaseRef)
|
||||
|
||||
uint32_t type() const override { return tid(); }
|
||||
std::string ToString() const override { return py::str(object_); }
|
||||
bool operator==(const BaseRef &other) const override;
|
||||
bool operator==(const PyObjectRef &other) const;
|
||||
|
||||
py::object object_;
|
||||
};
|
||||
|
||||
class RunFunctionRef : public BaseRef {
|
||||
public:
|
||||
RunFunctionRef() {}
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
/**
|
||||
* Copyright 2020 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include "utils/base_ref_extends.h"
|
||||
|
||||
namespace mindspore {
|
||||
bool PyObjectRef::operator==(const BaseRef &other) const {
|
||||
if (!utils::isa<PyObjectRef>(other)) {
|
||||
return false;
|
||||
}
|
||||
return *this == utils::cast<PyObjectRef>(other);
|
||||
}
|
||||
|
||||
bool PyObjectRef::operator==(const PyObjectRef &other) const { return object_ == other.object_; }
|
||||
} // namespace mindspore
|
|
@ -0,0 +1,45 @@
|
|||
/**
|
||||
* Copyright 2020 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MINDSPORE_CCSRC_UTILS_BASE_REF_EXTENDS_H_
|
||||
#define MINDSPORE_CCSRC_UTILS_BASE_REF_EXTENDS_H_
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "utils/base_ref_py.h"
|
||||
#include "utils/base_ref.h"
|
||||
|
||||
namespace mindspore {
|
||||
class PyObjectRef : public BaseRef {
|
||||
public:
|
||||
explicit PyObjectRef(const py::object &py_object) : object_(py_object) {}
|
||||
explicit PyObjectRef(const py::tuple &tuple_obj) : object_(tuple_obj) {}
|
||||
|
||||
~PyObjectRef() override = default;
|
||||
|
||||
std::shared_ptr<Base> copy() const override { return std::make_shared<PyObjectRef>(object_); }
|
||||
MS_DECLARE_PARENT(PyObjectRef, BaseRef)
|
||||
|
||||
uint32_t type() const override { return tid(); }
|
||||
std::string ToString() const override { return py::str(object_); }
|
||||
bool operator==(const BaseRef &other) const override;
|
||||
bool operator==(const PyObjectRef &other) const;
|
||||
|
||||
py::object object_;
|
||||
};
|
||||
} // namespace mindspore
|
||||
|
||||
#endif // MINDSPORE_CCSRC_UTILS_BASE_REF_EXTENDS_H_
|
|
@ -0,0 +1,31 @@
|
|||
/**
|
||||
* Copyright 2020 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MINDSPORE_CCSRC_UTILS_BASE_REF_PY_H_
|
||||
#define MINDSPORE_CCSRC_UTILS_BASE_REF_PY_H_
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "pybind11/pybind11.h"
|
||||
|
||||
namespace py = pybind11;
|
||||
|
||||
namespace mindspore {
|
||||
class PyObjectRef;
|
||||
inline std::shared_ptr<PyObjectRef> MakeNode(const py::object &a) { return std::make_shared<PyObjectRef>(a); }
|
||||
inline std::shared_ptr<PyObjectRef> MakeNode(const py::tuple &a) { return std::make_shared<PyObjectRef>(a); }
|
||||
} // namespace mindspore
|
||||
|
||||
#endif // MINDSPORE_CCSRC_UTILS_BASE_REF_PY_H_
|
|
@ -25,11 +25,12 @@
|
|||
#include <cfloat>
|
||||
|
||||
#include "pybind11/pybind11.h"
|
||||
|
||||
#include "ir/tensor.h"
|
||||
#include "pipeline/static_analysis/abstract_value.h"
|
||||
#include "pipeline/parse/parse.h"
|
||||
#include "pipeline/parse/parse_base.h"
|
||||
#include "ir/value.h"
|
||||
#include "ir/tensor.h"
|
||||
#include "utils/base_ref_extends.h"
|
||||
|
||||
namespace mindspore {
|
||||
py::object BuiltinsToPyData(const Any &value);
|
||||
|
@ -429,4 +430,154 @@ bool IsGraphOutputValueNodeOrParameter(const AnfNodePtr &output, const py::tuple
|
|||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Isomorphism
|
||||
static bool SameNodeShallow(const AnfNodePtr &node1, const AnfNodePtr &node2, FuncGraphPairMapEquiv *equiv_func_graph,
|
||||
NodeMapEquiv *const equiv_node) {
|
||||
if (equiv_node == nullptr) {
|
||||
MS_LOG(ERROR) << "Invalid equiv_node";
|
||||
return false;
|
||||
}
|
||||
if (equiv_node->count(node1) > 0 && (*equiv_node)[node1] == node2) {
|
||||
return true;
|
||||
}
|
||||
if (IsValueNode<FuncGraph>(node1) && IsValueNode<FuncGraph>(node2)) {
|
||||
return Isomorphic(GetValueNode<FuncGraphPtr>(node1), GetValueNode<FuncGraphPtr>(node2), equiv_func_graph,
|
||||
equiv_node);
|
||||
}
|
||||
if (node1->isa<ValueNode>() && node2->isa<ValueNode>()) {
|
||||
auto a1 = GetValueNode(node1);
|
||||
auto a2 = GetValueNode(node2);
|
||||
if (a1->isa<Primitive>() && a2->isa<Primitive>()) {
|
||||
return a1->cast<PrimitivePtr>()->name() == a2->cast<PrimitivePtr>()->name();
|
||||
} else if (a1->isa<tensor::Tensor>() && a2->isa<tensor::Tensor>()) {
|
||||
return a1->cast<tensor::TensorPtr>()->ValueEqual(*(a2->cast<tensor::TensorPtr>()));
|
||||
} else {
|
||||
return *a1 == *a2;
|
||||
}
|
||||
}
|
||||
if (node1->isa<Parameter>() && node2->isa<Parameter>()) {
|
||||
auto para1 = node1->cast<ParameterPtr>();
|
||||
auto para2 = node2->cast<ParameterPtr>();
|
||||
if (para1->name() == para2->name()) {
|
||||
return true;
|
||||
}
|
||||
MS_LOG(DEBUG) << "two parameters are not equal.";
|
||||
return false;
|
||||
}
|
||||
MS_LOG(ERROR) << "type error";
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool SameNode(const AnfNodePtr &node1, const AnfNodePtr &node2, FuncGraphPairMapEquiv *equiv_func_graph,
|
||||
NodeMapEquiv *const equiv_node) {
|
||||
MS_EXCEPTION_IF_NULL(node1);
|
||||
MS_EXCEPTION_IF_NULL(node2);
|
||||
if (node1->isa<CNode>() && node2->isa<CNode>()) {
|
||||
auto &inputs1 = node1->cast<CNodePtr>()->inputs();
|
||||
auto &inputs2 = node2->cast<CNodePtr>()->inputs();
|
||||
for (std::size_t i = 0; i < inputs1.size(); ++i) {
|
||||
if (!SameNodeShallow(inputs1[i], inputs2[i], equiv_func_graph, equiv_node)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return SameNodeShallow(node1, node2, equiv_func_graph, equiv_node);
|
||||
}
|
||||
|
||||
static bool SameSubgraph(AnfNodePtr root1, AnfNodePtr root2, FuncGraphPairMapEquiv *equiv_func_graph,
|
||||
NodeMapEquiv *const equiv_node) {
|
||||
std::unordered_set<AnfNodePtr> done;
|
||||
std::stack<std::pair<AnfNodePtr, AnfNodePtr>> todo;
|
||||
|
||||
todo.push(std::make_pair(root1, root2));
|
||||
while (todo.size() > 0) {
|
||||
AnfNodePtr node1 = todo.top().first;
|
||||
if (done.count(node1) > 0) {
|
||||
todo.pop();
|
||||
continue;
|
||||
}
|
||||
AnfNodePtr node2 = todo.top().second;
|
||||
|
||||
bool condition = false;
|
||||
std::vector<AnfNodePtr> s1 = SuccIncoming(node1);
|
||||
std::vector<AnfNodePtr> s2 = SuccIncoming(node2);
|
||||
|
||||
if (s1.size() != s2.size()) {
|
||||
return false;
|
||||
}
|
||||
for (std::size_t i = 0; i < s1.size(); ++i) {
|
||||
if (done.count(s1[i]) == 0) {
|
||||
todo.push(std::make_pair(s1[i], s2[i]));
|
||||
condition = true;
|
||||
}
|
||||
}
|
||||
if (condition) {
|
||||
continue;
|
||||
}
|
||||
(void)done.insert(node1);
|
||||
|
||||
auto res = SameNode(node1, node2, equiv_func_graph, equiv_node);
|
||||
if (res) {
|
||||
(*equiv_node)[node1] = node2;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
todo.pop();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Isomorphic(FuncGraphPtr fg1, FuncGraphPtr fg2, FuncGraphPairMapEquiv *equiv_func_graph,
|
||||
NodeMapEquiv *const equiv_node) {
|
||||
auto fg1_fg2 = std::make_pair(fg1, fg2);
|
||||
if (equiv_func_graph == nullptr) {
|
||||
MS_LOG(ERROR) << "equiv_func_graph not init";
|
||||
return false;
|
||||
}
|
||||
if (equiv_func_graph->find(fg1_fg2) != equiv_func_graph->end()) {
|
||||
return (*equiv_func_graph)[fg1_fg2] != kNotEquiv;
|
||||
}
|
||||
if (fg1 == nullptr || fg2 == nullptr) {
|
||||
MS_LOG(ERROR) << "Invalid function graph";
|
||||
return false;
|
||||
}
|
||||
if (fg1->parameters().size() != fg2->parameters().size()) {
|
||||
MS_LOG(DEBUG) << "parameters size not match";
|
||||
return false;
|
||||
}
|
||||
if (equiv_node != nullptr) {
|
||||
for (std::size_t i = 0; i < fg1->parameters().size(); ++i) {
|
||||
(*equiv_node)[fg1->parameters()[i]] = fg2->parameters()[i];
|
||||
}
|
||||
(*equiv_func_graph)[fg1_fg2] = kPending;
|
||||
auto result = SameSubgraph(fg1->get_return(), fg2->get_return(), equiv_func_graph, equiv_node);
|
||||
(*equiv_func_graph)[fg1_fg2] = EquivState(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
MS_LOG(ERROR) << "equiv_node not init";
|
||||
return false;
|
||||
}
|
||||
|
||||
tensor::TensorPtr ScalarToTensor(const ScalarPtr &scalar) {
|
||||
if (scalar == nullptr) {
|
||||
MS_EXCEPTION(ArgumentError) << "Nullptr Error!";
|
||||
}
|
||||
tensor::TensorPtr tensor = nullptr;
|
||||
if (scalar->isa<FloatImm>()) {
|
||||
tensor = std::make_shared<tensor::Tensor>(py::float_(GetValue<float>(scalar)), kFloat32);
|
||||
} else if (scalar->isa<IntergerImm>()) {
|
||||
tensor = std::make_shared<tensor::Tensor>(py::int_(GetValue<int>(scalar)), kInt32);
|
||||
} else if (scalar->isa<BoolImm>()) {
|
||||
tensor = std::make_shared<tensor::Tensor>(py::array(py::bool_(GetValue<bool>(scalar))), kBool);
|
||||
} else {
|
||||
auto type = scalar->type();
|
||||
auto type_str = (type == nullptr) ? "nullptr" : type->ToString();
|
||||
MS_LOG(EXCEPTION) << "Invalid scalar type: " << type_str;
|
||||
}
|
||||
MS_EXCEPTION_IF_NULL(tensor);
|
||||
return tensor;
|
||||
}
|
||||
} // namespace mindspore
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/**
|
||||
* Copyright 2019 Huawei Technologies Co., Ltd
|
||||
* Copyright 2019-2020 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -19,16 +19,25 @@
|
|||
|
||||
#include <limits>
|
||||
#include <memory>
|
||||
#include "pybind11/pybind11.h"
|
||||
#include <utility>
|
||||
#include <stack>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
|
||||
#include "pybind11/pybind11.h"
|
||||
#include "utils/convert_utils_base.h"
|
||||
#include "utils/any.h"
|
||||
#include "utils/base_ref.h"
|
||||
#include "ir/base.h"
|
||||
#include "ir/anf.h"
|
||||
#include "utils/base_ref.h"
|
||||
|
||||
namespace py = pybind11;
|
||||
|
||||
namespace mindspore {
|
||||
namespace tensor {
|
||||
class Tensor;
|
||||
using TensorPtr = std::shared_ptr<Tensor>;
|
||||
} // namespace tensor
|
||||
|
||||
py::object AnyToPyData(const Any &value);
|
||||
py::object BaseRefToPyData(const BaseRef &value);
|
||||
|
@ -36,95 +45,29 @@ bool BaseRefToBool(const BaseRef &in, bool *out);
|
|||
bool ValueToBool(const ValuePtr &in, bool *out);
|
||||
py::object ValuePtrToPyData(const ValuePtr &value);
|
||||
|
||||
inline int SizeToInt(size_t u) {
|
||||
if (u > static_cast<size_t>((std::numeric_limits<int>::max)())) {
|
||||
MS_LOG(EXCEPTION) << "The size_t value(" << u << ") exceeds the maximum value of int.";
|
||||
}
|
||||
return static_cast<int>(u);
|
||||
}
|
||||
|
||||
inline uint32_t SizeToUint(size_t u) {
|
||||
if (u > static_cast<size_t>((std::numeric_limits<uint32_t>::max)())) {
|
||||
MS_LOG(EXCEPTION) << "The size_t value(" << u << ") exceeds the maximum value of uint32_t.";
|
||||
}
|
||||
return static_cast<uint32_t>(u);
|
||||
}
|
||||
|
||||
inline int64_t SizeToLong(size_t u) {
|
||||
if (u > static_cast<size_t>((std::numeric_limits<int64_t>::max)())) {
|
||||
MS_LOG(EXCEPTION) << "The size_t value(" << u << ") exceeds the maximum value of int64_t.";
|
||||
}
|
||||
return static_cast<int64_t>(u);
|
||||
}
|
||||
|
||||
inline size_t IntToSize(int u) {
|
||||
if (u < 0) {
|
||||
MS_LOG(EXCEPTION) << "The int value(" << u << ") is less than 0.";
|
||||
}
|
||||
return static_cast<size_t>(u);
|
||||
}
|
||||
|
||||
inline size_t LongToSize(int64_t u) {
|
||||
if (u < 0) {
|
||||
MS_LOG(EXCEPTION) << "The int64_t value(" << u << ") is less than 0.";
|
||||
}
|
||||
return static_cast<size_t>(u);
|
||||
}
|
||||
|
||||
inline size_t FloatToSize(float u) {
|
||||
if (u < 0) {
|
||||
MS_LOG(EXCEPTION) << "The float value(" << u << ") is less than 0.";
|
||||
}
|
||||
|
||||
if (u > static_cast<float>((std::numeric_limits<size_t>::max)())) {
|
||||
MS_LOG(EXCEPTION) << "The float value(" << u << ") exceeds the maximum value of size_t.";
|
||||
}
|
||||
return static_cast<size_t>(u);
|
||||
}
|
||||
inline float IntToFloat(int32_t v) { return static_cast<float>(v); }
|
||||
|
||||
inline uint32_t IntToUint(int32_t u) {
|
||||
if (u < 0) {
|
||||
MS_LOG(EXCEPTION) << "The int32_t value(" << u << ") is less than 0.";
|
||||
}
|
||||
return static_cast<uint32_t>(u);
|
||||
}
|
||||
|
||||
inline int32_t UintToInt(uint32_t u) {
|
||||
if (u > static_cast<uint32_t>((std::numeric_limits<int32_t>::max)())) {
|
||||
MS_LOG(EXCEPTION) << "The uint32_t value(" << u << ") exceeds the maximum value of int32_t.";
|
||||
}
|
||||
return static_cast<int32_t>(u);
|
||||
}
|
||||
|
||||
inline unsigned int UlongToUint(size_t u) {
|
||||
if (u > static_cast<size_t>((std::numeric_limits<unsigned int>::max)())) {
|
||||
MS_LOG(EXCEPTION) << "The size_t value(" << u << ") exceeds the maximum value of unsigned int.";
|
||||
}
|
||||
return static_cast<unsigned int>(u);
|
||||
}
|
||||
|
||||
inline void IntMulWithOverflowCheck(int a, int b, int *c) {
|
||||
int out = a * b;
|
||||
if (a != 0) {
|
||||
bool ok = ((out / a) != b);
|
||||
if (ok) {
|
||||
MS_LOG(EXCEPTION) << "Mul: a(" << a << ") * b(" << b << ") result is overflow";
|
||||
}
|
||||
}
|
||||
*c = out;
|
||||
}
|
||||
|
||||
inline uint8_t *AddressOffset(void *address, size_t offset) {
|
||||
MS_EXCEPTION_IF_NULL(address);
|
||||
return static_cast<uint8_t *>(address) + offset;
|
||||
}
|
||||
|
||||
AbstractBasePtr PyListDtype2AbstractTensor(const py::object &shape_obj, const py::object &type_obj);
|
||||
|
||||
bool IsGraphOutputValueNodeOrParameter(const AnfNodePtr &output, const py::tuple &args,
|
||||
const std::shared_ptr<py::object> &ret_val);
|
||||
|
||||
// Isomorphism
|
||||
struct PairHasher {
|
||||
template <class T1, class T2>
|
||||
std::size_t operator()(const std::pair<T1, T2> &p) const {
|
||||
auto h1 = std::hash<T1>{}(p.first);
|
||||
auto h2 = std::hash<T2>{}(p.second);
|
||||
return h1 ^ h2;
|
||||
}
|
||||
};
|
||||
|
||||
enum EquivState { kNotEquiv = 0, kEquiv = 1, kPending = 2 };
|
||||
|
||||
using FuncGraphPairMapEquiv = std::unordered_map<std::pair<FuncGraphPtr, FuncGraphPtr>, EquivState, PairHasher>;
|
||||
using NodeMapEquiv = std::unordered_map<AnfNodePtr, AnfNodePtr>;
|
||||
|
||||
bool Isomorphic(FuncGraphPtr g1, FuncGraphPtr g2, FuncGraphPairMapEquiv *equiv_func_graph, NodeMapEquiv *equiv_node);
|
||||
|
||||
tensor::TensorPtr ScalarToTensor(const ScalarPtr &scalar);
|
||||
} // namespace mindspore
|
||||
|
||||
#endif // MINDSPORE_CCSRC_UTILS_CONVERT_UTILS_H_
|
||||
|
|
|
@ -0,0 +1,111 @@
|
|||
/**
|
||||
* Copyright 2020 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef MINDSPORE_CCSRC_UTILS_CONVERT_UTILS_BASE_H_
|
||||
#define MINDSPORE_CCSRC_UTILS_CONVERT_UTILS_BASE_H_
|
||||
|
||||
#include <limits>
|
||||
#include <memory>
|
||||
|
||||
#include "utils/log_adapter.h"
|
||||
|
||||
namespace mindspore {
|
||||
inline int SizeToInt(size_t u) {
|
||||
if (u > static_cast<size_t>((std::numeric_limits<int>::max)())) {
|
||||
MS_LOG(EXCEPTION) << "The size_t value(" << u << ") exceeds the maximum value of int.";
|
||||
}
|
||||
return static_cast<int>(u);
|
||||
}
|
||||
|
||||
inline uint32_t SizeToUint(size_t u) {
|
||||
if (u > static_cast<size_t>((std::numeric_limits<uint32_t>::max)())) {
|
||||
MS_LOG(EXCEPTION) << "The size_t value(" << u << ") exceeds the maximum value of uint32_t.";
|
||||
}
|
||||
return static_cast<uint32_t>(u);
|
||||
}
|
||||
|
||||
inline int64_t SizeToLong(size_t u) {
|
||||
if (u > static_cast<size_t>((std::numeric_limits<int64_t>::max)())) {
|
||||
MS_LOG(EXCEPTION) << "The size_t value(" << u << ") exceeds the maximum value of int64_t.";
|
||||
}
|
||||
return static_cast<int64_t>(u);
|
||||
}
|
||||
|
||||
inline size_t IntToSize(int u) {
|
||||
if (u < 0) {
|
||||
MS_LOG(EXCEPTION) << "The int value(" << u << ") is less than 0.";
|
||||
}
|
||||
return static_cast<size_t>(u);
|
||||
}
|
||||
|
||||
inline size_t LongToSize(int64_t u) {
|
||||
if (u < 0) {
|
||||
MS_LOG(EXCEPTION) << "The int64_t value(" << u << ") is less than 0.";
|
||||
}
|
||||
return static_cast<size_t>(u);
|
||||
}
|
||||
|
||||
inline size_t FloatToSize(float u) {
|
||||
if (u < 0) {
|
||||
MS_LOG(EXCEPTION) << "The float value(" << u << ") is less than 0.";
|
||||
}
|
||||
|
||||
if (u > static_cast<float>((std::numeric_limits<size_t>::max)())) {
|
||||
MS_LOG(EXCEPTION) << "The float value(" << u << ") exceeds the maximum value of size_t.";
|
||||
}
|
||||
return static_cast<size_t>(u);
|
||||
}
|
||||
inline float IntToFloat(int32_t v) { return static_cast<float>(v); }
|
||||
|
||||
inline uint32_t IntToUint(int32_t u) {
|
||||
if (u < 0) {
|
||||
MS_LOG(EXCEPTION) << "The int32_t value(" << u << ") is less than 0.";
|
||||
}
|
||||
return static_cast<uint32_t>(u);
|
||||
}
|
||||
|
||||
inline int32_t UintToInt(uint32_t u) {
|
||||
if (u > static_cast<uint32_t>((std::numeric_limits<int32_t>::max)())) {
|
||||
MS_LOG(EXCEPTION) << "The uint32_t value(" << u << ") exceeds the maximum value of int32_t.";
|
||||
}
|
||||
return static_cast<int32_t>(u);
|
||||
}
|
||||
|
||||
inline unsigned int UlongToUint(size_t u) {
|
||||
if (u > static_cast<size_t>((std::numeric_limits<unsigned int>::max)())) {
|
||||
MS_LOG(EXCEPTION) << "The size_t value(" << u << ") exceeds the maximum value of unsigned int.";
|
||||
}
|
||||
return static_cast<unsigned int>(u);
|
||||
}
|
||||
|
||||
inline void IntMulWithOverflowCheck(int a, int b, int *c) {
|
||||
int out = a * b;
|
||||
if (a != 0) {
|
||||
bool ok = ((out / a) != b);
|
||||
if (ok) {
|
||||
MS_LOG(EXCEPTION) << "Mul: a(" << a << ") * b(" << b << ") result is overflow";
|
||||
}
|
||||
}
|
||||
*c = out;
|
||||
}
|
||||
|
||||
inline uint8_t *AddressOffset(void *address, size_t offset) {
|
||||
MS_EXCEPTION_IF_NULL(address);
|
||||
return static_cast<uint8_t *>(address) + offset;
|
||||
}
|
||||
} // namespace mindspore
|
||||
|
||||
#endif // MINDSPORE_CCSRC_UTILS_CONVERT_UTILS_BASE_H_
|
|
@ -1,7 +1,7 @@
|
|||
/**
|
||||
* This is the C++ adaptation and derivative work of Myia (https://github.com/mila-iqia/myia/).
|
||||
*
|
||||
* Copyright 2019 Huawei Technologies Co., Ltd
|
||||
* Copyright 2019-2020 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -29,153 +29,12 @@
|
|||
#include <queue>
|
||||
#include <set>
|
||||
|
||||
#include "ir/visitor.h"
|
||||
#include "utils/log_adapter.h"
|
||||
#include "common/utils.h"
|
||||
#include "pipeline/parse/function_block.h"
|
||||
#include "pipeline/parse/python_adapter.h"
|
||||
#include "debug/label.h"
|
||||
#include "ir/func_graph.h"
|
||||
#include "utils/log_adapter.h"
|
||||
|
||||
namespace mindspore {
|
||||
using SymbolicKeyTypePtr = std::shared_ptr<SymbolicKeyType>;
|
||||
|
||||
namespace {
|
||||
class DeepFirstSearcher : public AnfVisitor {
|
||||
public:
|
||||
explicit DeepFirstSearcher(const IncludeFunc &include) : include_(include) {}
|
||||
~DeepFirstSearcher() override = default;
|
||||
|
||||
std::vector<AnfNodePtr> Search(const AnfNodePtr &root) {
|
||||
if (root == nullptr) {
|
||||
return res_;
|
||||
}
|
||||
seen_ = NewSeenGeneration();
|
||||
Visit(root);
|
||||
return res_;
|
||||
}
|
||||
|
||||
void Visit(const AnfNodePtr &node) override {
|
||||
MS_EXCEPTION_IF_NULL(node);
|
||||
if (node->seen_ == seen_) {
|
||||
return;
|
||||
}
|
||||
|
||||
node->seen_ = seen_;
|
||||
|
||||
auto incl = include_(node);
|
||||
if (incl == EXCLUDE) {
|
||||
return;
|
||||
}
|
||||
|
||||
res_.push_back(node);
|
||||
if (incl == FOLLOW) {
|
||||
AnfVisitor::Visit(node);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
size_t seen_{0};
|
||||
IncludeFunc include_;
|
||||
std::vector<AnfNodePtr> res_{};
|
||||
};
|
||||
|
||||
class DeepScopedGraphSearcher : public DeepFirstSearcher {
|
||||
public:
|
||||
explicit DeepScopedGraphSearcher(const IncludeFunc &include) : DeepFirstSearcher(include) {}
|
||||
~DeepScopedGraphSearcher() override = default;
|
||||
|
||||
void Visit(const CNodePtr &cnode) override {
|
||||
if (cnode->func_graph() == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
AnfNodePtr ret = cnode->func_graph()->get_return();
|
||||
if (ret != nullptr) {
|
||||
DeepFirstSearcher::Visit(ret);
|
||||
}
|
||||
|
||||
auto &inputs = cnode->inputs();
|
||||
for (auto iter = inputs.rbegin(); iter != inputs.rend(); ++iter) {
|
||||
DeepFirstSearcher::Visit(*iter);
|
||||
}
|
||||
}
|
||||
|
||||
void Visit(const ValueNodePtr &vnode) override {
|
||||
if (!IsValueNode<FuncGraph>(vnode)) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto graph = GetValueNode<FuncGraphPtr>(vnode);
|
||||
AnfNodePtr ret = graph->get_return();
|
||||
if (ret != nullptr) {
|
||||
DeepFirstSearcher::Visit(ret);
|
||||
}
|
||||
}
|
||||
|
||||
void Visit(const ParameterPtr ¶m) override {
|
||||
if (param->func_graph() == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
AnfNodePtr ret = param->func_graph()->get_return();
|
||||
if (ret != nullptr) {
|
||||
DeepFirstSearcher::Visit(ret);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
class DeepUsedGraphSearcher : public DeepFirstSearcher {
|
||||
public:
|
||||
explicit DeepUsedGraphSearcher(const IncludeFunc &include) : DeepFirstSearcher(include) {}
|
||||
~DeepUsedGraphSearcher() override = default;
|
||||
|
||||
void Visit(const CNodePtr &cnode) override {
|
||||
auto &inputs = cnode->inputs();
|
||||
for (auto iter = inputs.rbegin(); iter != inputs.rend(); ++iter) {
|
||||
DeepFirstSearcher::Visit(*iter);
|
||||
}
|
||||
}
|
||||
|
||||
void Visit(const ValueNodePtr &vnode) override {
|
||||
if (!IsValueNode<FuncGraph>(vnode)) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto graph = GetValueNode<FuncGraphPtr>(vnode);
|
||||
AnfNodePtr ret = graph->get_return();
|
||||
if (ret != nullptr) {
|
||||
DeepFirstSearcher::Visit(ret);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
class DeepLinkedGraphSearcher : public DeepFirstSearcher {
|
||||
public:
|
||||
explicit DeepLinkedGraphSearcher(const IncludeFunc &include) : DeepFirstSearcher(include) {}
|
||||
~DeepLinkedGraphSearcher() override = default;
|
||||
|
||||
void Visit(const CNodePtr &cnode) override {
|
||||
auto &inputs = cnode->inputs();
|
||||
for (auto iter = inputs.rbegin(); iter != inputs.rend(); ++iter) {
|
||||
DeepFirstSearcher::Visit(*iter);
|
||||
}
|
||||
}
|
||||
|
||||
void Visit(const ValueNodePtr &) override {}
|
||||
};
|
||||
} // namespace
|
||||
|
||||
std::vector<AnfNodePtr> DeepScopedGraphSearch(const AnfNodePtr &root, const IncludeFunc &include) {
|
||||
return DeepScopedGraphSearcher(include).Search(root);
|
||||
}
|
||||
|
||||
std::vector<AnfNodePtr> DeepUsedGraphSearch(const AnfNodePtr &root, const IncludeFunc &include) {
|
||||
return DeepUsedGraphSearcher(include).Search(root);
|
||||
}
|
||||
|
||||
std::vector<AnfNodePtr> DeepLinkedGraphSearch(const AnfNodePtr &root, const IncludeFunc &include) {
|
||||
return DeepLinkedGraphSearcher(include).Search(root);
|
||||
}
|
||||
|
||||
std::vector<AnfNodePtr> TopoSort(const AnfNodePtr &root, const SuccFunc &succ, const IncludeFunc &include) {
|
||||
size_t seen = NewSeenGeneration();
|
||||
std::list<AnfNodePtr> todo(1, root);
|
||||
|
@ -408,154 +267,4 @@ void FuncGraphIndex::Acquire(const AnfNodePtr &key) {
|
|||
(void)index_node_[name].insert(key);
|
||||
}
|
||||
}
|
||||
|
||||
// Isomorphism
|
||||
static bool SameNodeShallow(const AnfNodePtr &node1, const AnfNodePtr &node2, FuncGraphPairMapEquiv *equiv_func_graph,
|
||||
NodeMapEquiv *const equiv_node) {
|
||||
if (equiv_node == nullptr) {
|
||||
MS_LOG(ERROR) << "Invalid equiv_node";
|
||||
return false;
|
||||
}
|
||||
if (equiv_node->count(node1) > 0 && (*equiv_node)[node1] == node2) {
|
||||
return true;
|
||||
}
|
||||
if (IsValueNode<FuncGraph>(node1) && IsValueNode<FuncGraph>(node2)) {
|
||||
return Isomorphic(GetValueNode<FuncGraphPtr>(node1), GetValueNode<FuncGraphPtr>(node2), equiv_func_graph,
|
||||
equiv_node);
|
||||
}
|
||||
if (node1->isa<ValueNode>() && node2->isa<ValueNode>()) {
|
||||
auto a1 = GetValueNode(node1);
|
||||
auto a2 = GetValueNode(node2);
|
||||
if (a1->isa<Primitive>() && a2->isa<Primitive>()) {
|
||||
return a1->cast<PrimitivePtr>()->name() == a2->cast<PrimitivePtr>()->name();
|
||||
} else if (a1->isa<tensor::Tensor>() && a2->isa<tensor::Tensor>()) {
|
||||
return a1->cast<tensor::TensorPtr>()->ValueEqual(*(a2->cast<tensor::TensorPtr>()));
|
||||
} else {
|
||||
return *a1 == *a2;
|
||||
}
|
||||
}
|
||||
if (node1->isa<Parameter>() && node2->isa<Parameter>()) {
|
||||
auto para1 = node1->cast<ParameterPtr>();
|
||||
auto para2 = node2->cast<ParameterPtr>();
|
||||
if (para1->name() == para2->name()) {
|
||||
return true;
|
||||
}
|
||||
MS_LOG(DEBUG) << "two parameters are not equal.";
|
||||
return false;
|
||||
}
|
||||
MS_LOG(ERROR) << "type error";
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool SameNode(const AnfNodePtr &node1, const AnfNodePtr &node2, FuncGraphPairMapEquiv *equiv_func_graph,
|
||||
NodeMapEquiv *const equiv_node) {
|
||||
MS_EXCEPTION_IF_NULL(node1);
|
||||
MS_EXCEPTION_IF_NULL(node2);
|
||||
if (node1->isa<CNode>() && node2->isa<CNode>()) {
|
||||
auto &inputs1 = node1->cast<CNodePtr>()->inputs();
|
||||
auto &inputs2 = node2->cast<CNodePtr>()->inputs();
|
||||
for (std::size_t i = 0; i < inputs1.size(); ++i) {
|
||||
if (!SameNodeShallow(inputs1[i], inputs2[i], equiv_func_graph, equiv_node)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return SameNodeShallow(node1, node2, equiv_func_graph, equiv_node);
|
||||
}
|
||||
|
||||
static bool SameSubgraph(AnfNodePtr root1, AnfNodePtr root2, FuncGraphPairMapEquiv *equiv_func_graph,
|
||||
NodeMapEquiv *const equiv_node) {
|
||||
std::unordered_set<AnfNodePtr> done;
|
||||
std::stack<std::pair<AnfNodePtr, AnfNodePtr>> todo;
|
||||
|
||||
todo.push(std::make_pair(root1, root2));
|
||||
while (todo.size() > 0) {
|
||||
AnfNodePtr node1 = todo.top().first;
|
||||
if (done.count(node1) > 0) {
|
||||
todo.pop();
|
||||
continue;
|
||||
}
|
||||
AnfNodePtr node2 = todo.top().second;
|
||||
|
||||
bool condition = false;
|
||||
std::vector<AnfNodePtr> s1 = SuccIncoming(node1);
|
||||
std::vector<AnfNodePtr> s2 = SuccIncoming(node2);
|
||||
|
||||
if (s1.size() != s2.size()) {
|
||||
return false;
|
||||
}
|
||||
for (std::size_t i = 0; i < s1.size(); ++i) {
|
||||
if (done.count(s1[i]) == 0) {
|
||||
todo.push(std::make_pair(s1[i], s2[i]));
|
||||
condition = true;
|
||||
}
|
||||
}
|
||||
if (condition) {
|
||||
continue;
|
||||
}
|
||||
(void)done.insert(node1);
|
||||
|
||||
auto res = SameNode(node1, node2, equiv_func_graph, equiv_node);
|
||||
if (res) {
|
||||
(*equiv_node)[node1] = node2;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
todo.pop();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Isomorphic(FuncGraphPtr fg1, FuncGraphPtr fg2, FuncGraphPairMapEquiv *equiv_func_graph,
|
||||
NodeMapEquiv *const equiv_node) {
|
||||
auto fg1_fg2 = std::make_pair(fg1, fg2);
|
||||
if (equiv_func_graph == nullptr) {
|
||||
MS_LOG(ERROR) << "equiv_func_graph not init";
|
||||
return false;
|
||||
}
|
||||
if (equiv_func_graph->find(fg1_fg2) != equiv_func_graph->end()) {
|
||||
return (*equiv_func_graph)[fg1_fg2] != kNotEquiv;
|
||||
}
|
||||
if (fg1 == nullptr || fg2 == nullptr) {
|
||||
MS_LOG(ERROR) << "Invalid function graph";
|
||||
return false;
|
||||
}
|
||||
if (fg1->parameters().size() != fg2->parameters().size()) {
|
||||
MS_LOG(DEBUG) << "parameters size not match";
|
||||
return false;
|
||||
}
|
||||
if (equiv_node != nullptr) {
|
||||
for (std::size_t i = 0; i < fg1->parameters().size(); ++i) {
|
||||
(*equiv_node)[fg1->parameters()[i]] = fg2->parameters()[i];
|
||||
}
|
||||
(*equiv_func_graph)[fg1_fg2] = kPending;
|
||||
auto result = SameSubgraph(fg1->get_return(), fg2->get_return(), equiv_func_graph, equiv_node);
|
||||
(*equiv_func_graph)[fg1_fg2] = EquivState(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
MS_LOG(ERROR) << "equiv_node not init";
|
||||
return false;
|
||||
}
|
||||
|
||||
tensor::TensorPtr ScalarToTensor(const ScalarPtr &scalar) {
|
||||
if (scalar == nullptr) {
|
||||
MS_EXCEPTION(ArgumentError) << "Nullptr Error!";
|
||||
}
|
||||
tensor::TensorPtr tensor = nullptr;
|
||||
if (scalar->isa<FloatImm>()) {
|
||||
tensor = std::make_shared<tensor::Tensor>(py::float_(GetValue<float>(scalar)), kFloat32);
|
||||
} else if (scalar->isa<IntergerImm>()) {
|
||||
tensor = std::make_shared<tensor::Tensor>(py::int_(GetValue<int>(scalar)), kInt32);
|
||||
} else if (scalar->isa<BoolImm>()) {
|
||||
tensor = std::make_shared<tensor::Tensor>(py::array(py::bool_(GetValue<bool>(scalar))), kBool);
|
||||
} else {
|
||||
auto type = scalar->type();
|
||||
auto type_str = (type == nullptr) ? "nullptr" : type->ToString();
|
||||
MS_LOG(EXCEPTION) << "Invalid scalar type: " << type_str;
|
||||
}
|
||||
MS_EXCEPTION_IF_NULL(tensor);
|
||||
return tensor;
|
||||
}
|
||||
} // namespace mindspore
|
||||
|
|
|
@ -29,7 +29,7 @@
|
|||
#include <string>
|
||||
|
||||
#include "ir/anf.h"
|
||||
#include "ir/primitive.h"
|
||||
#include "ir/primitive_base.h"
|
||||
#include "ir/scalar.h"
|
||||
#include "ir/tensor.h"
|
||||
#include "debug/label.h"
|
||||
|
@ -42,6 +42,10 @@ using IncludeFunc = std::function<IncludeType(const AnfNodePtr &)>;
|
|||
using SuccFunc = std::function<std::vector<AnfNodePtr>(AnfNodePtr)>;
|
||||
using SearchFunc = std::function<std::vector<AnfNodePtr>(const AnfNodePtr &, const IncludeFunc &)>;
|
||||
|
||||
std::vector<AnfNodePtr> DeepScopedGraphSearch(const AnfNodePtr &root, const IncludeFunc &include);
|
||||
std::vector<AnfNodePtr> DeepUsedGraphSearch(const AnfNodePtr &root, const IncludeFunc &include);
|
||||
std::vector<AnfNodePtr> DeepLinkedGraphSearch(const AnfNodePtr &root, const IncludeFunc &include);
|
||||
|
||||
std::vector<AnfNodePtr> SuccDeeper(const AnfNodePtr &node);
|
||||
std::vector<AnfNodePtr> SuccDeeperSimple(const AnfNodePtr &node);
|
||||
std::vector<AnfNodePtr> SuccIncoming(const AnfNodePtr &node);
|
||||
|
@ -79,26 +83,6 @@ class FuncGraphIndex {
|
|||
std::map<std::string, std::set<FuncGraphPtr>> index_func_graph_;
|
||||
std::map<std::string, std::set<AnfNodePtr>> index_node_;
|
||||
};
|
||||
|
||||
// Isomorphism
|
||||
|
||||
struct PairHasher {
|
||||
template <class T1, class T2>
|
||||
std::size_t operator()(const std::pair<T1, T2> &p) const {
|
||||
auto h1 = std::hash<T1>{}(p.first);
|
||||
auto h2 = std::hash<T2>{}(p.second);
|
||||
return h1 ^ h2;
|
||||
}
|
||||
};
|
||||
|
||||
enum EquivState { kNotEquiv = 0, kEquiv = 1, kPending = 2 };
|
||||
|
||||
using FuncGraphPairMapEquiv = std::unordered_map<std::pair<FuncGraphPtr, FuncGraphPtr>, EquivState, PairHasher>;
|
||||
using NodeMapEquiv = std::unordered_map<AnfNodePtr, AnfNodePtr>;
|
||||
|
||||
bool Isomorphic(FuncGraphPtr g1, FuncGraphPtr g2, FuncGraphPairMapEquiv *equiv_func_graph, NodeMapEquiv *equiv_node);
|
||||
|
||||
tensor::TensorPtr ScalarToTensor(const ScalarPtr &scalar);
|
||||
} // namespace mindspore
|
||||
|
||||
#endif // MINDSPORE_CCSRC_UTILS_GRAPH_UTILS_H_
|
||||
|
|
|
@ -0,0 +1,174 @@
|
|||
/**
|
||||
* Copyright 2020 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include "utils/graph_utils.h"
|
||||
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
#include <utility>
|
||||
#include <stack>
|
||||
#include <vector>
|
||||
#include <list>
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
|
||||
#include "ir/visitor.h"
|
||||
#include "ir/func_graph.h"
|
||||
#include "debug/label.h"
|
||||
#include "utils/log_adapter.h"
|
||||
#include "common/utils.h"
|
||||
#include "pipeline/parse/function_block.h"
|
||||
#include "pipeline/parse/python_adapter.h"
|
||||
|
||||
namespace mindspore {
|
||||
namespace {
|
||||
class DeepFirstSearcher : public AnfVisitor {
|
||||
public:
|
||||
explicit DeepFirstSearcher(const IncludeFunc &include) : include_(include) {}
|
||||
~DeepFirstSearcher() override = default;
|
||||
|
||||
std::vector<AnfNodePtr> Search(const AnfNodePtr &root) {
|
||||
if (root == nullptr) {
|
||||
return res_;
|
||||
}
|
||||
seen_ = NewSeenGeneration();
|
||||
Visit(root);
|
||||
return res_;
|
||||
}
|
||||
|
||||
void Visit(const AnfNodePtr &node) override {
|
||||
MS_EXCEPTION_IF_NULL(node);
|
||||
if (node->seen_ == seen_) {
|
||||
return;
|
||||
}
|
||||
|
||||
node->seen_ = seen_;
|
||||
|
||||
auto incl = include_(node);
|
||||
if (incl == EXCLUDE) {
|
||||
return;
|
||||
}
|
||||
|
||||
res_.push_back(node);
|
||||
if (incl == FOLLOW) {
|
||||
AnfVisitor::Visit(node);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
size_t seen_{0};
|
||||
IncludeFunc include_;
|
||||
std::vector<AnfNodePtr> res_{};
|
||||
};
|
||||
|
||||
class DeepScopedGraphSearcher : public DeepFirstSearcher {
|
||||
public:
|
||||
explicit DeepScopedGraphSearcher(const IncludeFunc &include) : DeepFirstSearcher(include) {}
|
||||
~DeepScopedGraphSearcher() override = default;
|
||||
|
||||
void Visit(const CNodePtr &cnode) override {
|
||||
if (cnode->func_graph() == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
AnfNodePtr ret = cnode->func_graph()->get_return();
|
||||
if (ret != nullptr) {
|
||||
DeepFirstSearcher::Visit(ret);
|
||||
}
|
||||
|
||||
auto &inputs = cnode->inputs();
|
||||
for (auto iter = inputs.rbegin(); iter != inputs.rend(); ++iter) {
|
||||
DeepFirstSearcher::Visit(*iter);
|
||||
}
|
||||
}
|
||||
|
||||
void Visit(const ValueNodePtr &vnode) override {
|
||||
if (!IsValueNode<FuncGraph>(vnode)) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto graph = GetValueNode<FuncGraphPtr>(vnode);
|
||||
AnfNodePtr ret = graph->get_return();
|
||||
if (ret != nullptr) {
|
||||
DeepFirstSearcher::Visit(ret);
|
||||
}
|
||||
}
|
||||
|
||||
void Visit(const ParameterPtr ¶m) override {
|
||||
if (param->func_graph() == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
AnfNodePtr ret = param->func_graph()->get_return();
|
||||
if (ret != nullptr) {
|
||||
DeepFirstSearcher::Visit(ret);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
class DeepUsedGraphSearcher : public DeepFirstSearcher {
|
||||
public:
|
||||
explicit DeepUsedGraphSearcher(const IncludeFunc &include) : DeepFirstSearcher(include) {}
|
||||
~DeepUsedGraphSearcher() override = default;
|
||||
|
||||
void Visit(const CNodePtr &cnode) override {
|
||||
auto &inputs = cnode->inputs();
|
||||
for (auto iter = inputs.rbegin(); iter != inputs.rend(); ++iter) {
|
||||
DeepFirstSearcher::Visit(*iter);
|
||||
}
|
||||
}
|
||||
|
||||
void Visit(const ValueNodePtr &vnode) override {
|
||||
if (!IsValueNode<FuncGraph>(vnode)) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto graph = GetValueNode<FuncGraphPtr>(vnode);
|
||||
AnfNodePtr ret = graph->get_return();
|
||||
if (ret != nullptr) {
|
||||
DeepFirstSearcher::Visit(ret);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
class DeepLinkedGraphSearcher : public DeepFirstSearcher {
|
||||
public:
|
||||
explicit DeepLinkedGraphSearcher(const IncludeFunc &include) : DeepFirstSearcher(include) {}
|
||||
~DeepLinkedGraphSearcher() override = default;
|
||||
|
||||
void Visit(const CNodePtr &cnode) override {
|
||||
auto &inputs = cnode->inputs();
|
||||
for (auto iter = inputs.rbegin(); iter != inputs.rend(); ++iter) {
|
||||
DeepFirstSearcher::Visit(*iter);
|
||||
}
|
||||
}
|
||||
|
||||
void Visit(const ValueNodePtr &) override {}
|
||||
};
|
||||
} // namespace
|
||||
|
||||
std::vector<AnfNodePtr> DeepScopedGraphSearch(const AnfNodePtr &root, const IncludeFunc &include) {
|
||||
return DeepScopedGraphSearcher(include).Search(root);
|
||||
}
|
||||
|
||||
std::vector<AnfNodePtr> DeepUsedGraphSearch(const AnfNodePtr &root, const IncludeFunc &include) {
|
||||
return DeepUsedGraphSearcher(include).Search(root);
|
||||
}
|
||||
|
||||
std::vector<AnfNodePtr> DeepLinkedGraphSearch(const AnfNodePtr &root, const IncludeFunc &include) {
|
||||
return DeepLinkedGraphSearcher(include).Search(root);
|
||||
}
|
||||
} // namespace mindspore
|
|
@ -1,5 +1,5 @@
|
|||
/**
|
||||
* Copyright 2019 Huawei Technologies Co., Ltd
|
||||
* Copyright 2019-2020 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -22,6 +22,7 @@
|
|||
#include "ir/anf.h"
|
||||
#include "utils/callbacks.h"
|
||||
#include "utils/graph_utils.h"
|
||||
#include "utils/base_ref_extends.h"
|
||||
#include "session/session_factory.h"
|
||||
#include "common/utils.h"
|
||||
#ifdef ENABLE_GE
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
/**
|
||||
* This is the C++ adaptation and derivative work of Myia (https://github.com/mila-iqia/myia/).
|
||||
*
|
||||
* Copyright 2019 Huawei Technologies Co., Ltd
|
||||
* Copyright 2019-2020 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -24,6 +24,7 @@
|
|||
#include "vm/backend.h"
|
||||
#include "vm/transform.h"
|
||||
#include "pipeline/parse/data_converter.h"
|
||||
#include "utils/base_ref_extends.h"
|
||||
|
||||
namespace mindspore {
|
||||
namespace compile {
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
/**
|
||||
* This is the C++ adaptation and derivative work of Myia (https://github.com/mila-iqia/myia/).
|
||||
*
|
||||
* Copyright 2019 Huawei Technologies Co., Ltd
|
||||
* Copyright 2019-2020 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -30,7 +30,7 @@
|
|||
#include <unordered_map>
|
||||
|
||||
#include "ir/anf.h"
|
||||
#include "utils/base_ref.h"
|
||||
#include "utils/base_ref_extends.h"
|
||||
|
||||
namespace mindspore {
|
||||
namespace compile {
|
||||
|
|
|
@ -24,10 +24,10 @@
|
|||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "utils/base_ref_extends.h"
|
||||
#include "ir/anf.h"
|
||||
#include "ir/manager.h"
|
||||
#include "ir/tensor.h"
|
||||
#include "utils/base_ref.h"
|
||||
|
||||
namespace mindspore {
|
||||
namespace compile {
|
||||
|
|
Loading…
Reference in New Issue