forked from mindspore-Ecosystem/mindspore
!17607 fix static check warning in SOMAS
From: @laiyongqiang Reviewed-by: @yuchaojie,@zhoufeng54 Signed-off-by: @zhoufeng54
This commit is contained in:
commit
183b1f105c
|
@ -108,7 +108,8 @@ void MemSwapManager::RetreatSwapThreshold() {
|
||||||
if (update_one_decay_step) {
|
if (update_one_decay_step) {
|
||||||
distance_threshold_ -= distance_decay_step_;
|
distance_threshold_ -= distance_decay_step_;
|
||||||
} else if (distance_threshold_ >= kDistanceLowerBound) {
|
} else if (distance_threshold_ >= kDistanceLowerBound) {
|
||||||
size_t new_distance_decay_step = (distance_threshold_ - kDistanceLowerBound) / 4;
|
static constexpr size_t kDistanceDecayStepFactor = 4;
|
||||||
|
size_t new_distance_decay_step = (distance_threshold_ - kDistanceLowerBound) / kDistanceDecayStepFactor;
|
||||||
if (new_distance_decay_step < 1) {
|
if (new_distance_decay_step < 1) {
|
||||||
new_distance_decay_step = 1;
|
new_distance_decay_step = 1;
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,7 +22,6 @@
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <numeric>
|
#include <numeric>
|
||||||
#include <set>
|
#include <set>
|
||||||
#include <sstream>
|
|
||||||
|
|
||||||
#include "backend/optimizer/somas/somas_node.h"
|
#include "backend/optimizer/somas/somas_node.h"
|
||||||
#include "backend/optimizer/somas/somas_solver_pre.h"
|
#include "backend/optimizer/somas/somas_solver_pre.h"
|
||||||
|
@ -47,6 +46,8 @@ using mindspore::profiler::TensorMemory;
|
||||||
namespace mindspore {
|
namespace mindspore {
|
||||||
namespace somas {
|
namespace somas {
|
||||||
constexpr auto kGapSize = 512;
|
constexpr auto kGapSize = 512;
|
||||||
|
constexpr auto kRetryIntervalSeconds = 500;
|
||||||
|
constexpr size_t kRefNodeTensorNum = 2;
|
||||||
|
|
||||||
constexpr auto kGraphId = "graph_id";
|
constexpr auto kGraphId = "graph_id";
|
||||||
constexpr auto kHashId = "hash_id";
|
constexpr auto kHashId = "hash_id";
|
||||||
|
@ -130,31 +131,12 @@ bool Somas::CalcSomasModelHash(const session::KernelGraph *graph) {
|
||||||
MS_LOG(INFO) << "Graph " << graph->graph_id() << "'s SOMAS Model hash id is " << hash_id_;
|
MS_LOG(INFO) << "Graph " << graph->graph_id() << "'s SOMAS Model hash id is " << hash_id_;
|
||||||
std::string filename =
|
std::string filename =
|
||||||
save_graphs_path_ + "/somas_meta/" + "somas_graph" + std::to_string(graph->graph_id()) + "_" + hash_id_ + ".info";
|
save_graphs_path_ + "/somas_meta/" + "somas_graph" + std::to_string(graph->graph_id()) + "_" + hash_id_ + ".info";
|
||||||
if (filename.size() > PATH_MAX) {
|
std::ofstream ofs;
|
||||||
MS_LOG(WARNING) << "File path " << filename << " is too long.";
|
if (Common::OpenFile(filename, ofs)) {
|
||||||
return false;
|
ofs << model_str << std::endl;
|
||||||
}
|
ofs.close();
|
||||||
auto real_path = Common::GetRealPath(filename);
|
|
||||||
if (!real_path.has_value()) {
|
|
||||||
MS_LOG(WARNING) << "Get real path failed. path=" << filename;
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::ifstream ifs(real_path.value());
|
|
||||||
if (ifs) {
|
|
||||||
MS_LOG(INFO) << "Graph " << graph->graph_id() << "'s SOMAS Model file " << real_path.value() << " is exist.";
|
|
||||||
ifs.close();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
ChangeFileMode(real_path.value(), S_IRWXU);
|
|
||||||
std::ofstream ofs(real_path.value());
|
|
||||||
|
|
||||||
if (!ofs.is_open()) {
|
|
||||||
MS_LOG(WARNING) << "Open file '" << real_path.value() << "' failed!";
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
ofs << model_str << std::endl;
|
|
||||||
ofs.close();
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -185,26 +167,11 @@ bool Somas::SaveSomasResult(const session::KernelGraph *graph) {
|
||||||
|
|
||||||
std::string filename =
|
std::string filename =
|
||||||
save_graphs_path_ + "/somas_meta/" + "somas_graph" + std::to_string(graph->graph_id()) + "_" + hash_id_ + ".json";
|
save_graphs_path_ + "/somas_meta/" + "somas_graph" + std::to_string(graph->graph_id()) + "_" + hash_id_ + ".json";
|
||||||
if (filename.size() > PATH_MAX) {
|
std::ofstream ofs;
|
||||||
MS_LOG(WARNING) << "File path " << filename << " is too long.";
|
if (Common::OpenFile(filename, ofs)) {
|
||||||
return false;
|
ofs << somas_json.dump() << std::endl;
|
||||||
|
ofs.close();
|
||||||
}
|
}
|
||||||
auto real_path = Common::GetRealPath(filename);
|
|
||||||
if (!real_path.has_value()) {
|
|
||||||
MS_LOG(WARNING) << "Get real path failed. path=" << filename;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
ChangeFileMode(real_path.value(), S_IRWXU);
|
|
||||||
std::ofstream ofs(real_path.value());
|
|
||||||
|
|
||||||
if (!ofs.is_open()) {
|
|
||||||
MS_LOG(WARNING) << "Open file '" << real_path.value() << "' failed!";
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
ofs << somas_json.dump() << std::endl;
|
|
||||||
ofs.close();
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -225,7 +192,7 @@ bool Somas::LoadSomasResult(const session::KernelGraph *graph, const string file
|
||||||
} catch (std::exception &e) {
|
} catch (std::exception &e) {
|
||||||
MS_LOG(WARNING) << "Parse json file error: " << filename << ", sleep 500ms and retry again.";
|
MS_LOG(WARNING) << "Parse json file error: " << filename << ", sleep 500ms and retry again.";
|
||||||
somas_json_fs.close();
|
somas_json_fs.close();
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
std::this_thread::sleep_for(std::chrono::milliseconds(kRetryIntervalSeconds));
|
||||||
std::ifstream retry_tmp(filename);
|
std::ifstream retry_tmp(filename);
|
||||||
if (!retry_tmp.is_open()) {
|
if (!retry_tmp.is_open()) {
|
||||||
MS_LOG(INFO) << "Open json file: " << filename << " error, please check kernel_meta.";
|
MS_LOG(INFO) << "Open json file: " << filename << " error, please check kernel_meta.";
|
||||||
|
@ -544,7 +511,7 @@ void Somas::InitCommonNodeInputs(bool is_all_nop_node, const CNodePtr &kernel) {
|
||||||
if ((op_name == kDynamicRNNOpName || op_name == kDynamicGRUV2OpName) && input_origin_type == kMetaTypeNone) {
|
if ((op_name == kDynamicRNNOpName || op_name == kDynamicGRUV2OpName) && input_origin_type == kMetaTypeNone) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
auto parameter = GetSomasParameters(prenode_index.first, prenode_index.second);
|
auto parameter = GetSomasParameter(prenode_index.first, prenode_index.second);
|
||||||
node->input_parameters_map_[real_input_index] = parameter;
|
node->input_parameters_map_[real_input_index] = parameter;
|
||||||
real_input_index++;
|
real_input_index++;
|
||||||
MS_LOG(DEBUG) << "Input [" << prenode_index.first->fullname_with_scope() << "] is not a real cnode kernel.";
|
MS_LOG(DEBUG) << "Input [" << prenode_index.first->fullname_with_scope() << "] is not a real cnode kernel.";
|
||||||
|
@ -644,7 +611,7 @@ void Somas::InitAtomicCleanInputs(bool enable_fusion_clear, const CNodePtr &kern
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SomasParameterPtr Somas::CreateSomasParameters(AnfNodePtr node, size_t index) {
|
SomasParameterPtr Somas::CreateSomasParameter(AnfNodePtr node, size_t index) {
|
||||||
auto id = parameters_list_.size();
|
auto id = parameters_list_.size();
|
||||||
auto device_addr = AnfAlgo::GetOutputAddr(node, index);
|
auto device_addr = AnfAlgo::GetOutputAddr(node, index);
|
||||||
if (device_addr == nullptr) {
|
if (device_addr == nullptr) {
|
||||||
|
@ -656,7 +623,7 @@ SomasParameterPtr Somas::CreateSomasParameters(AnfNodePtr node, size_t index) {
|
||||||
return param;
|
return param;
|
||||||
}
|
}
|
||||||
|
|
||||||
SomasParameterPtr Somas::GetSomasParameters(AnfNodePtr node, size_t index) {
|
SomasParameterPtr Somas::GetSomasParameter(AnfNodePtr node, size_t index) {
|
||||||
auto key = node.get();
|
auto key = node.get();
|
||||||
auto iter = parameters_map_.find(key);
|
auto iter = parameters_map_.find(key);
|
||||||
if (iter != parameters_map_.end()) {
|
if (iter != parameters_map_.end()) {
|
||||||
|
@ -665,14 +632,14 @@ SomasParameterPtr Somas::GetSomasParameters(AnfNodePtr node, size_t index) {
|
||||||
if (it != iter->second.end()) {
|
if (it != iter->second.end()) {
|
||||||
return *it;
|
return *it;
|
||||||
} else {
|
} else {
|
||||||
auto new_param = CreateSomasParameters(node, index);
|
auto new_param = CreateSomasParameter(node, index);
|
||||||
iter->second.push_back(new_param);
|
iter->second.push_back(new_param);
|
||||||
return new_param;
|
return new_param;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
auto new_param = CreateSomasParameters(node, index);
|
auto param = CreateSomasParameter(node, index);
|
||||||
parameters_map_[key].push_back(new_param);
|
parameters_map_[key].push_back(param);
|
||||||
return new_param;
|
return param;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1304,15 +1271,15 @@ std::map<size_t, size_t> Somas::GetRefTensorsInContiguousList() {
|
||||||
size_t contiguous_in_ref_list = std::count_if(ref_node_list.begin(), ref_node_list.end(),
|
size_t contiguous_in_ref_list = std::count_if(ref_node_list.begin(), ref_node_list.end(),
|
||||||
[this](size_t tid) { return tensors_map_[tid]->contiguous_; });
|
[this](size_t tid) { return tensors_map_[tid]->contiguous_; });
|
||||||
// Keep info about contiguous and check for errors
|
// Keep info about contiguous and check for errors
|
||||||
if (ref_node_list.size() > 2 && contiguous_in_ref_list > 0) {
|
if (ref_node_list.size() > kRefNodeTensorNum && contiguous_in_ref_list > 0) {
|
||||||
MS_LOG(WARNING) << "Ref node of size greater than two with at least one contiguous tensor in";
|
MS_LOG(WARNING) << "Ref node of size greater than two with at least one contiguous tensor in";
|
||||||
}
|
}
|
||||||
if (ref_node_list.size() == 2 && contiguous_in_ref_list == 1) {
|
if (ref_node_list.size() == kRefNodeTensorNum && contiguous_in_ref_list == 1) {
|
||||||
MS_LOG(WARNING) << "Ref node of size two with only one contiguous tensor" << ref_node_list[0] << ":"
|
MS_LOG(WARNING) << "Ref node of size two with only one contiguous tensor" << ref_node_list[0] << ":"
|
||||||
<< tensors_map_[ref_node_list[0]]->contiguous_ << ", " << ref_node_list[1] << ":"
|
<< tensors_map_[ref_node_list[0]]->contiguous_ << ", " << ref_node_list[1] << ":"
|
||||||
<< tensors_map_[ref_node_list[1]]->contiguous_;
|
<< tensors_map_[ref_node_list[1]]->contiguous_;
|
||||||
}
|
}
|
||||||
if (ref_node_list.size() == 2 && contiguous_in_ref_list == 2) {
|
if (ref_node_list.size() == kRefNodeTensorNum && contiguous_in_ref_list == kRefNodeTensorNum) {
|
||||||
ref_tensors_in_contiguous_map[ref_node_list[0]] = ref_node_list[1];
|
ref_tensors_in_contiguous_map[ref_node_list[0]] = ref_node_list[1];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1389,12 +1356,12 @@ void Somas::UpdateRefTensorsConflict() {
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Somas::GetSplitName(const std::string &scope_name) const {
|
std::string Somas::GetSplitName(const std::string &scope_name) const {
|
||||||
auto indx = scope_name.rfind('/');
|
auto index = scope_name.rfind('/');
|
||||||
if (indx == std::string::npos) {
|
if (index == std::string::npos) {
|
||||||
return scope_name;
|
return scope_name;
|
||||||
} else {
|
} else {
|
||||||
if (indx < scope_name.size() - 1) {
|
if (index < scope_name.size() - 1) {
|
||||||
auto split_name = scope_name.substr(indx + 1);
|
auto split_name = scope_name.substr(index + 1);
|
||||||
return split_name;
|
return split_name;
|
||||||
}
|
}
|
||||||
return scope_name;
|
return scope_name;
|
||||||
|
@ -1517,26 +1484,11 @@ void Somas::DumpParameters(std::ostringstream &oss) const {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Somas::DumpSomasInfoIR(const string filename) const {
|
void Somas::DumpSomasInfoIR(const string filename) const {
|
||||||
if (filename.size() > PATH_MAX) {
|
std::ofstream ofs;
|
||||||
MS_LOG(ERROR) << "File path " << filename << " is too long.";
|
if (Common::OpenFile(filename, ofs)) {
|
||||||
return;
|
ofs << SomasInfo();
|
||||||
|
ofs.close();
|
||||||
}
|
}
|
||||||
auto real_path = Common::GetRealPath(filename);
|
|
||||||
if (!real_path.has_value()) {
|
|
||||||
MS_LOG(ERROR) << "Get real path failed. path=" << filename;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
ChangeFileMode(real_path.value(), S_IRWXU);
|
|
||||||
std::ofstream ofs(real_path.value());
|
|
||||||
|
|
||||||
if (!ofs.is_open()) {
|
|
||||||
MS_LOG(ERROR) << "Open dump file '" << real_path.value() << "' failed!";
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
ofs << SomasInfo();
|
|
||||||
ofs.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Somas::Offline() const {
|
std::string Somas::Offline() const {
|
||||||
|
@ -1585,27 +1537,11 @@ std::string Somas::Offline() const {
|
||||||
|
|
||||||
void Somas::DumpOfflineIR(const string filename) const {
|
void Somas::DumpOfflineIR(const string filename) const {
|
||||||
MS_LOG(INFO) << "Printing somas-log-from-graph log: " << filename;
|
MS_LOG(INFO) << "Printing somas-log-from-graph log: " << filename;
|
||||||
if (filename.size() > PATH_MAX) {
|
std::ofstream ofs;
|
||||||
MS_LOG(ERROR) << "File path " << filename << " is too long.";
|
if (Common::OpenFile(filename, ofs)) {
|
||||||
return;
|
ofs << Offline();
|
||||||
|
ofs.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
auto real_path = Common::GetRealPath(filename);
|
|
||||||
if (!real_path.has_value()) {
|
|
||||||
MS_LOG(ERROR) << "Get real path failed. path=" << filename;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
ChangeFileMode(real_path.value(), S_IRWXU);
|
|
||||||
std::ofstream ofs(real_path.value());
|
|
||||||
|
|
||||||
if (!ofs.is_open()) {
|
|
||||||
MS_LOG(ERROR) << "Open dump file '" << real_path.value() << "' failed!";
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
ofs << Offline();
|
|
||||||
ofs.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Somas::SomasMemory() const {
|
std::string Somas::SomasMemory() const {
|
||||||
|
@ -1623,15 +1559,15 @@ std::string Somas::SomasMemory() const {
|
||||||
|
|
||||||
std::map<size_t, std::map<size_t, SomasTensorPtr>> mem_list;
|
std::map<size_t, std::map<size_t, SomasTensorPtr>> mem_list;
|
||||||
|
|
||||||
for (const auto &tensor : tensors_list_) {
|
for (const auto &output_tensor : tensors_list_) {
|
||||||
size_t key = tensor->offset_;
|
size_t key = output_tensor->offset_;
|
||||||
auto iter = mem_list.find(key);
|
auto iter = mem_list.find(key);
|
||||||
if (iter == mem_list.end()) {
|
if (iter == mem_list.end()) {
|
||||||
std::map<size_t, SomasTensorPtr> id_tensor_map;
|
std::map<size_t, SomasTensorPtr> id_tensor_map;
|
||||||
id_tensor_map[tensor->GetId()] = tensor;
|
id_tensor_map[output_tensor->GetId()] = output_tensor;
|
||||||
mem_list[key] = id_tensor_map;
|
mem_list[key] = id_tensor_map;
|
||||||
} else {
|
} else {
|
||||||
iter->second[tensor->GetId()] = tensor;
|
iter->second[output_tensor->GetId()] = output_tensor;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1652,51 +1588,35 @@ std::string Somas::SomasMemory() const {
|
||||||
for (const auto &mem : mem_list) {
|
for (const auto &mem : mem_list) {
|
||||||
auto id_tensor_map = mem.second;
|
auto id_tensor_map = mem.second;
|
||||||
for (const auto &id_tensor : id_tensor_map) {
|
for (const auto &id_tensor : id_tensor_map) {
|
||||||
auto tensor = id_tensor.second;
|
auto place_tensor = id_tensor.second;
|
||||||
std::string scope_name;
|
std::string scope_name;
|
||||||
size_t src_stm_id = 0xffff;
|
size_t src_stm_id = 0xffff;
|
||||||
if (tensor->GetSourceNode() != nullptr) {
|
if (place_tensor->GetSourceNode() != nullptr) {
|
||||||
scope_name = tensor->GetSourceNode()->scope_full_name_;
|
scope_name = place_tensor->GetSourceNode()->scope_full_name_;
|
||||||
src_stm_id = tensor->GetSourceNode()->GetStream()->GetId();
|
src_stm_id = place_tensor->GetSourceNode()->GetStream()->GetId();
|
||||||
} else {
|
} else {
|
||||||
scope_name = "Somas Tensor";
|
scope_name = "Somas Tensor";
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string split_name = GetSplitName(scope_name);
|
std::string split_name = GetSplitName(scope_name);
|
||||||
oss << "#" << mem_map[tensor->GetOffset()] << "\t" << tensor->GetOffset() << "\t"
|
oss << "#" << mem_map[place_tensor->GetOffset()] << "\t" << place_tensor->GetOffset() << "\t"
|
||||||
<< tensor->GetOffset() + tensor->GetAlignedSize() << "\t%" << tensor->GetId() << "T\t"
|
<< place_tensor->GetOffset() + place_tensor->GetAlignedSize() << "\t%" << place_tensor->GetId() << "T\t"
|
||||||
<< tensor->GetOriginalSize() << "\t" << tensor->GetAlignedSize() << "\t&"
|
<< place_tensor->GetOriginalSize() << "\t" << place_tensor->GetAlignedSize() << "\t&"
|
||||||
<< static_cast<void *>(tensor->GetOffset() + mem_base_addr_) << "\t&"
|
<< static_cast<void *>(place_tensor->GetOffset() + mem_base_addr_) << "\t&"
|
||||||
<< static_cast<void *>(tensor->GetOffset() + mem_base_addr_ + tensor->GetAlignedSize()) << "\t"
|
<< static_cast<void *>(place_tensor->GetOffset() + mem_base_addr_ + place_tensor->GetAlignedSize()) << "\t"
|
||||||
<< tensor_type_name_map[tensor->type_] << "\t" << split_name << "\tstm" << src_stm_id << "\t"
|
<< tensor_type_name_map[place_tensor->type_] << "\t" << split_name << "\tstm" << src_stm_id << "\t"
|
||||||
<< tensor->lifetime_.start_ << "\t" << tensor->lifetime_.end_ << "\n";
|
<< place_tensor->lifetime_.start_ << "\t" << place_tensor->lifetime_.end_ << "\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return oss.str();
|
return oss.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Somas::DumpSomasMemoryIR(const string filename) const {
|
void Somas::DumpSomasMemoryIR(const string filename) const {
|
||||||
if (filename.size() > PATH_MAX) {
|
std::ofstream ofs;
|
||||||
MS_LOG(ERROR) << "File path " << filename << " is too long.";
|
if (Common::OpenFile(filename, ofs)) {
|
||||||
return;
|
ofs << SomasMemory();
|
||||||
|
ofs.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
auto real_path = Common::GetRealPath(filename);
|
|
||||||
if (!real_path.has_value()) {
|
|
||||||
MS_LOG(ERROR) << "Get real path failed. path=" << filename;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
ChangeFileMode(real_path.value(), S_IRWXU);
|
|
||||||
std::ofstream ofs(real_path.value());
|
|
||||||
|
|
||||||
if (!ofs.is_open()) {
|
|
||||||
MS_LOG(ERROR) << "Open dump file '" << real_path.value() << "' failed!";
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
ofs << SomasMemory();
|
|
||||||
ofs.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t Somas::CalcLowerBound() const {
|
size_t Somas::CalcLowerBound() const {
|
||||||
|
@ -1822,14 +1742,14 @@ void Somas::ConvertToProfilingNode(uint32_t graph_id) {
|
||||||
std::string name = GetSplitName(node->scope_full_name_);
|
std::string name = GetSplitName(node->scope_full_name_);
|
||||||
node_memory.SetNodeName(name);
|
node_memory.SetNodeName(name);
|
||||||
node_memory.SetNodeId(node->GetId());
|
node_memory.SetNodeId(node->GetId());
|
||||||
for (const auto &tensor : node->input_tensors_) {
|
for (const auto &input_tensor : node->input_tensors_) {
|
||||||
node_memory.AddInputTensorId(tensor->GetId());
|
node_memory.AddInputTensorId(input_tensor->GetId());
|
||||||
}
|
}
|
||||||
for (const auto &tensor : node->output_tensors_) {
|
for (const auto &output_tensor : node->output_tensors_) {
|
||||||
node_memory.AddOutputTensorId(tensor->GetId());
|
node_memory.AddOutputTensorId(output_tensor->GetId());
|
||||||
}
|
}
|
||||||
for (const auto &tensor : node->workspace_tensors_) {
|
for (const auto &workspace_tensor : node->workspace_tensors_) {
|
||||||
node_memory.AddWorkSpaceTensorId(tensor->GetId());
|
node_memory.AddWorkSpaceTensorId(workspace_tensor->GetId());
|
||||||
}
|
}
|
||||||
graph_node->AddNodeMemory(node_memory);
|
graph_node->AddNodeMemory(node_memory);
|
||||||
}
|
}
|
||||||
|
|
|
@ -129,8 +129,8 @@ class Somas {
|
||||||
std::string GetSplitName(const string &scope_name) const;
|
std::string GetSplitName(const string &scope_name) const;
|
||||||
size_t CalcLowerBound() const;
|
size_t CalcLowerBound() const;
|
||||||
void GenGraphStatisticInfo();
|
void GenGraphStatisticInfo();
|
||||||
SomasParameterPtr GetSomasParameters(AnfNodePtr node, size_t index);
|
SomasParameterPtr GetSomasParameter(AnfNodePtr node, size_t index);
|
||||||
SomasParameterPtr CreateSomasParameters(AnfNodePtr node, size_t index);
|
SomasParameterPtr CreateSomasParameter(AnfNodePtr node, size_t index);
|
||||||
void InitCommonNodeInputs(bool is_all_nop_node, const CNodePtr &kernel);
|
void InitCommonNodeInputs(bool is_all_nop_node, const CNodePtr &kernel);
|
||||||
void InitAtomicCleanInputs(bool is_all_nop_node, const CNodePtr &kernel);
|
void InitAtomicCleanInputs(bool is_all_nop_node, const CNodePtr &kernel);
|
||||||
void ComputeOneTensorConflicts(const std::shared_ptr<SomasTensor> &calc_tensor,
|
void ComputeOneTensorConflicts(const std::shared_ptr<SomasTensor> &calc_tensor,
|
||||||
|
@ -156,6 +156,7 @@ class Somas {
|
||||||
bool LoadSomasResult(const session::KernelGraph *graph, const string filename);
|
bool LoadSomasResult(const session::KernelGraph *graph, const string filename);
|
||||||
bool UpdateTensorsOffset(const std::vector<nlohmann::json> &tensors_json);
|
bool UpdateTensorsOffset(const std::vector<nlohmann::json> &tensors_json);
|
||||||
bool CalcSomasModelHash(const session::KernelGraph *graph);
|
bool CalcSomasModelHash(const session::KernelGraph *graph);
|
||||||
|
void UpdateInputTensor(SomasNodePtr node, SomasNodePtr pre_somas_node, SomasTensorPtr input_somas_tensor) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
using SomasPtr = std::shared_ptr<Somas>;
|
using SomasPtr = std::shared_ptr<Somas>;
|
||||||
|
|
|
@ -51,7 +51,7 @@ size_t FootPrint::Result() {
|
||||||
std::shared_ptr<FootPrint> foot_print = shared_from_this();
|
std::shared_ptr<FootPrint> foot_print = shared_from_this();
|
||||||
size_t upperbound = 0;
|
size_t upperbound = 0;
|
||||||
uint32_t total_footprints = 0;
|
uint32_t total_footprints = 0;
|
||||||
while (NULL != foot_print) {
|
while (foot_print != NULL) {
|
||||||
foot_print->printStats();
|
foot_print->printStats();
|
||||||
|
|
||||||
upperbound = foot_print->getOffset();
|
upperbound = foot_print->getOffset();
|
||||||
|
@ -249,7 +249,7 @@ bool FastHeuristic::Eval(vector<BlockTensor> *block_tensors_v, std::shared_ptr<F
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
// go to the next footprint slot
|
// go to the next footprint slot
|
||||||
if (NULL != p->Next()) {
|
if (p->Next() != NULL) {
|
||||||
p = p->Next();
|
p = p->Next();
|
||||||
} else if (bpushed == false) { // something went wrong
|
} else if (bpushed == false) { // something went wrong
|
||||||
MS_LOG(WARNING) << "Could not allocate memory for tensor: " << tensor->index_;
|
MS_LOG(WARNING) << "Could not allocate memory for tensor: " << tensor->index_;
|
||||||
|
|
|
@ -206,7 +206,7 @@ void SomasSolverCore::BuildBlocks() {
|
||||||
pTensor->blocked_ = true;
|
pTensor->blocked_ = true;
|
||||||
pTensor = pTensor->right_;
|
pTensor = pTensor->right_;
|
||||||
tensors_block_count++;
|
tensors_block_count++;
|
||||||
} while (NULL != pTensor);
|
} while (pTensor != NULL);
|
||||||
|
|
||||||
// add to the list
|
// add to the list
|
||||||
this->block_tensors_.emplace_back(bTensor);
|
this->block_tensors_.emplace_back(bTensor);
|
||||||
|
@ -355,14 +355,12 @@ size_t SomasSolverCore::FindSolutions() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void SomasSolverCore::Destroy(std::shared_ptr<FootPrint> &pFootprint) {
|
void SomasSolverCore::Destroy(std::shared_ptr<FootPrint> &pFootprint) {
|
||||||
while (NULL != pFootprint) {
|
while (pFootprint != NULL) {
|
||||||
if (NULL != pFootprint->Next()) {
|
if (pFootprint->Next() != NULL) {
|
||||||
std::shared_ptr<FootPrint> &p = pFootprint;
|
std::shared_ptr<FootPrint> &p = pFootprint;
|
||||||
pFootprint = pFootprint->Next();
|
pFootprint = pFootprint->Next();
|
||||||
// delete p;
|
|
||||||
p = NULL;
|
p = NULL;
|
||||||
} else {
|
} else {
|
||||||
// delete pFootprint;
|
|
||||||
pFootprint = NULL;
|
pFootprint = NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,11 +30,11 @@ namespace somas {
|
||||||
constexpr auto kSolNumThresholdMultiThread = 8;
|
constexpr auto kSolNumThresholdMultiThread = 8;
|
||||||
Status SomasSolverPre::CheckTensors(const TensorsDescMap *pTensors, uint32_t index1, uint32_t index2) {
|
Status SomasSolverPre::CheckTensors(const TensorsDescMap *pTensors, uint32_t index1, uint32_t index2) {
|
||||||
auto tensors = *pTensors;
|
auto tensors = *pTensors;
|
||||||
if (nullptr == tensors[index1]) {
|
if (tensors[index1] == nullptr) {
|
||||||
MS_LOG(WARNING) << "NULL tensor received in continuous constraint (tensor index " << index1 << ")";
|
MS_LOG(WARNING) << "NULL tensor received in continuous constraint (tensor index " << index1 << ")";
|
||||||
return FAILED;
|
return FAILED;
|
||||||
}
|
}
|
||||||
if (nullptr == tensors[index2]) {
|
if (tensors[index2] == nullptr) {
|
||||||
MS_LOG(WARNING) << "NULL tensor received in continuous constraint (tensor index " << index2 << ")";
|
MS_LOG(WARNING) << "NULL tensor received in continuous constraint (tensor index " << index2 << ")";
|
||||||
return FAILED;
|
return FAILED;
|
||||||
}
|
}
|
||||||
|
@ -99,7 +99,7 @@ Status SomasSolverPre::Solving(const session::KernelGraph *graph, TensorsDescMap
|
||||||
const std::vector<DynamicBitSet> *pConstraints,
|
const std::vector<DynamicBitSet> *pConstraints,
|
||||||
const vector<vector<size_t>> &continuous_v, bool bVerifySolution, bool ball,
|
const vector<vector<size_t>> &continuous_v, bool bVerifySolution, bool ball,
|
||||||
SortingType sorting, FittingType fitting, AlgorithmType algorithm) {
|
SortingType sorting, FittingType fitting, AlgorithmType algorithm) {
|
||||||
Status retval = SUCCESS;
|
Status ret = SUCCESS;
|
||||||
try {
|
try {
|
||||||
TensorsDescMap &tensors = *ptensors;
|
TensorsDescMap &tensors = *ptensors;
|
||||||
size_t total_sol = kNumSortingTypes * kNumFittingTypes * kNumAlgorithmTypes;
|
size_t total_sol = kNumSortingTypes * kNumFittingTypes * kNumAlgorithmTypes;
|
||||||
|
@ -156,6 +156,7 @@ Status SomasSolverPre::Solving(const session::KernelGraph *graph, TensorsDescMap
|
||||||
*(tensor.second.get()) = *(vecTensorsMap[best_sol][tensor.first]);
|
*(tensor.second.get()) = *(vecTensorsMap[best_sol][tensor.first]);
|
||||||
}
|
}
|
||||||
max_offset_ = best_solver->GetUpperbound();
|
max_offset_ = best_solver->GetUpperbound();
|
||||||
|
constexpr float kFloatPresent = 100.0;
|
||||||
MS_LOG(INFO) << "SOMAS SOLVER RESUME:";
|
MS_LOG(INFO) << "SOMAS SOLVER RESUME:";
|
||||||
MS_LOG(INFO) << "Best Solution:[" << 1 + best_sol << "/" << total_sol << "] ";
|
MS_LOG(INFO) << "Best Solution:[" << 1 + best_sol << "/" << total_sol << "] ";
|
||||||
MS_LOG(INFO) << "Best result:" << best << " Bytes " << (best) / (giga) << " GB ("
|
MS_LOG(INFO) << "Best result:" << best << " Bytes " << (best) / (giga) << " GB ("
|
||||||
|
@ -166,7 +167,8 @@ Status SomasSolverPre::Solving(const session::KernelGraph *graph, TensorsDescMap
|
||||||
MS_LOG(INFO) << "Best sorting strategy: " << sortingNames[best_solver->sort_strategy_];
|
MS_LOG(INFO) << "Best sorting strategy: " << sortingNames[best_solver->sort_strategy_];
|
||||||
MS_LOG(INFO) << "Best offset strategy: " << branchingNames[best_solver->branching_strategy_];
|
MS_LOG(INFO) << "Best offset strategy: " << branchingNames[best_solver->branching_strategy_];
|
||||||
MS_LOG(INFO) << "Time elapsed: " << total_time << " ms";
|
MS_LOG(INFO) << "Time elapsed: " << total_time << " ms";
|
||||||
MS_LOG(INFO) << "Spread:" << static_cast<double>((worst - best) / static_cast<double>(best * 100.0)) << " %%";
|
MS_LOG(INFO) << "Spread:" << static_cast<double>((worst - best) / static_cast<double>(best * kFloatPresent))
|
||||||
|
<< " %%";
|
||||||
} else {
|
} else {
|
||||||
if (AddContiguousInfoInMap(continuous_v, ptensors) == FAILED) {
|
if (AddContiguousInfoInMap(continuous_v, ptensors) == FAILED) {
|
||||||
return FAILED;
|
return FAILED;
|
||||||
|
@ -183,21 +185,22 @@ Status SomasSolverPre::Solving(const session::KernelGraph *graph, TensorsDescMap
|
||||||
MS_LOG(INFO) << "SomasSolver::Solving RESULT: " << max_offset_ << " (" << max_offset_ / (giga) << " GB)";
|
MS_LOG(INFO) << "SomasSolver::Solving RESULT: " << max_offset_ << " (" << max_offset_ / (giga) << " GB)";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
auto context_ptr = MsContext::GetInstance();
|
Log(graph, tensors, pConstraints, continuous_v);
|
||||||
MS_EXCEPTION_IF_NULL(context_ptr);
|
|
||||||
bool save_graphs = context_ptr->get_param<bool>(MS_CTX_SAVE_GRAPHS_FLAG);
|
|
||||||
if (save_graphs) {
|
|
||||||
Log(graph, tensors, pConstraints, continuous_v);
|
|
||||||
}
|
|
||||||
} catch (const std::exception &e) {
|
} catch (const std::exception &e) {
|
||||||
MS_LOG(EXCEPTION) << "SomasSolver::Solving FAILED: " << e.what();
|
MS_LOG(EXCEPTION) << "SomasSolver::Solving FAILED: " << e.what();
|
||||||
retval = FAILED;
|
ret = FAILED;
|
||||||
}
|
}
|
||||||
return retval;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SomasSolverPre::Log(const session::KernelGraph *graph, const TensorsDescMap &tensors,
|
void SomasSolverPre::Log(const session::KernelGraph *graph, const TensorsDescMap &tensors,
|
||||||
const std::vector<DynamicBitSet> *pConstraints, const vector<vector<size_t>> &continuous_v) {
|
const std::vector<DynamicBitSet> *pConstraints, const vector<vector<size_t>> &continuous_v) {
|
||||||
|
auto context_ptr = MsContext::GetInstance();
|
||||||
|
MS_EXCEPTION_IF_NULL(context_ptr);
|
||||||
|
bool save_graphs = context_ptr->get_param<bool>(MS_CTX_SAVE_GRAPHS_FLAG);
|
||||||
|
if (!save_graphs) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
SolverInputLog(graph, tensors, pConstraints, continuous_v);
|
SolverInputLog(graph, tensors, pConstraints, continuous_v);
|
||||||
SolverOutputLog(graph, tensors);
|
SolverOutputLog(graph, tensors);
|
||||||
TensorRelationLog(pConstraints, graph);
|
TensorRelationLog(pConstraints, graph);
|
||||||
|
@ -247,46 +250,30 @@ void SomasSolverPre::SolverInputLog(const session::KernelGraph *graph, const Ten
|
||||||
MS_EXCEPTION_IF_NULL(context_ptr);
|
MS_EXCEPTION_IF_NULL(context_ptr);
|
||||||
auto save_graphs_path = context_ptr->get_param<std::string>(MS_CTX_SAVE_GRAPHS_PATH);
|
auto save_graphs_path = context_ptr->get_param<std::string>(MS_CTX_SAVE_GRAPHS_PATH);
|
||||||
std::string filename = save_graphs_path + "/" + "somas_solver_input_" + std::to_string(graph->graph_id()) + ".ir";
|
std::string filename = save_graphs_path + "/" + "somas_solver_input_" + std::to_string(graph->graph_id()) + ".ir";
|
||||||
if (filename.size() > PATH_MAX) {
|
std::ofstream ofs;
|
||||||
MS_LOG(ERROR) << "File path " << filename << " is too long.";
|
if (Common::OpenFile(filename, ofs)) {
|
||||||
return;
|
for (auto &t : tensors) {
|
||||||
}
|
ofs << "T " << t.second->index_ << " " << t.second->size_ << " " << t.second->lifelong_ << std::endl;
|
||||||
auto real_path = Common::GetRealPath(filename);
|
}
|
||||||
if (!real_path.has_value()) {
|
|
||||||
MS_LOG(ERROR) << "Get real path failed. path=" << filename;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
ChangeFileMode(real_path.value(), S_IRWXU);
|
for (auto &t1 : tensors) {
|
||||||
std::ofstream ofs(real_path.value());
|
for (auto &t2 : tensors) {
|
||||||
|
size_t idx1 = t1.first;
|
||||||
if (!ofs.is_open()) {
|
size_t idx2 = t2.first;
|
||||||
MS_LOG(ERROR) << "Open log file '" << real_path.value() << "' failed!";
|
if ((idx1 != idx2) && (*pConstraints)[idx1].IsBitTrue(idx2) == false) {
|
||||||
return;
|
ofs << "C " << idx1 << " " << idx2 << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto &t : tensors) {
|
|
||||||
ofs << "T " << t.second->index_ << " " << t.second->size_ << " " << t.second->lifelong_ << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (auto &t1 : tensors) {
|
|
||||||
for (auto &t2 : tensors) {
|
|
||||||
size_t idx1 = t1.first;
|
|
||||||
size_t idx2 = t2.first;
|
|
||||||
if ((idx1 != idx2) && (*pConstraints)[idx1].IsBitTrue(idx2) == false) {
|
|
||||||
ofs << "C " << idx1 << " " << idx2 << std::endl;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
for (auto &s : continuous_v) {
|
||||||
for (auto &s : continuous_v) {
|
ofs << "S";
|
||||||
ofs << "S";
|
for (auto idx : s) {
|
||||||
for (auto idx : s) {
|
ofs << " " << idx;
|
||||||
ofs << " " << idx;
|
}
|
||||||
|
ofs << std::endl;
|
||||||
}
|
}
|
||||||
ofs << std::endl;
|
ofs.close();
|
||||||
}
|
}
|
||||||
ofs.close();
|
|
||||||
|
|
||||||
MS_LOG(INFO) << "SomasSolver input Log done";
|
MS_LOG(INFO) << "SomasSolver input Log done";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -315,15 +302,18 @@ void SomasSolverPre::SolverOutputLog(const session::KernelGraph *graph, const Te
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
constexpr size_t contiguous_left = 1;
|
||||||
|
constexpr size_t contiguous_mid = 2;
|
||||||
|
constexpr size_t contiguous_right = 3;
|
||||||
for (auto &t : tensors) {
|
for (auto &t : tensors) {
|
||||||
SomasSolverTensorDescPtr tensor = t.second;
|
SomasSolverTensorDescPtr tensor = t.second;
|
||||||
int continuous = 0;
|
int continuous = 0;
|
||||||
if (tensor->left_ == nullptr && tensor->right_ != nullptr)
|
if (tensor->left_ == nullptr && tensor->right_ != nullptr)
|
||||||
continuous = 1;
|
continuous = contiguous_left;
|
||||||
else if (tensor->left_ != nullptr && tensor->right_ != nullptr)
|
else if (tensor->left_ != nullptr && tensor->right_ != nullptr)
|
||||||
continuous = 2;
|
continuous = contiguous_mid;
|
||||||
else if (tensor->left_ != nullptr && tensor->right_ == nullptr)
|
else if (tensor->left_ != nullptr && tensor->right_ == nullptr)
|
||||||
continuous = 3;
|
continuous = contiguous_right;
|
||||||
const size_t alignment = 512;
|
const size_t alignment = 512;
|
||||||
bool size_aligned = tensor->size_ % alignment == 0;
|
bool size_aligned = tensor->size_ % alignment == 0;
|
||||||
bool offset_aligned = tensor->offset_ % alignment == 0;
|
bool offset_aligned = tensor->offset_ % alignment == 0;
|
||||||
|
|
|
@ -19,10 +19,12 @@
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
|
#include <fstream>
|
||||||
#include "utils/ms_context.h"
|
#include "utils/ms_context.h"
|
||||||
#include "utils/system/env.h"
|
#include "utils/system/env.h"
|
||||||
#include "utils/system/file_system.h"
|
#include "utils/system/file_system.h"
|
||||||
#include "utils/log_adapter.h"
|
#include "utils/log_adapter.h"
|
||||||
|
#include "utils/utils.h"
|
||||||
|
|
||||||
namespace mindspore {
|
namespace mindspore {
|
||||||
std::optional<std::string> Common::GetRealPath(const std::string &input_path) {
|
std::optional<std::string> Common::GetRealPath(const std::string &input_path) {
|
||||||
|
@ -287,4 +289,25 @@ std::string Common::AddId(const std::string &filename, const std::string &suffix
|
||||||
g_id++;
|
g_id++;
|
||||||
return s.str();
|
return s.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Common::OpenFile(const std::string filename, std::ofstream &ofs) {
|
||||||
|
if (filename.size() > PATH_MAX) {
|
||||||
|
MS_LOG(ERROR) << "File path " << filename << " is too long.";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
auto real_path = GetRealPath(filename);
|
||||||
|
if (!real_path.has_value()) {
|
||||||
|
MS_LOG(ERROR) << "Get real path failed. path=" << filename;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
ChangeFileMode(real_path.value(), S_IRUSR | S_IWUSR);
|
||||||
|
ofs.open(real_path.value());
|
||||||
|
|
||||||
|
if (!ofs.is_open()) {
|
||||||
|
MS_LOG(ERROR) << "Open dump file '" << real_path.value() << "' failed!";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
} // namespace mindspore
|
} // namespace mindspore
|
||||||
|
|
|
@ -38,6 +38,7 @@ class Common {
|
||||||
static bool CreateNotExistDirs(const std::string &path);
|
static bool CreateNotExistDirs(const std::string &path);
|
||||||
|
|
||||||
static std::string AddId(const std::string &filename, const std::string &suffix);
|
static std::string AddId(const std::string &filename, const std::string &suffix);
|
||||||
|
static bool OpenFile(const std::string filename, std::ofstream &ofs);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static bool IsEveryFilenameValid(const std::string &path, size_t length_limit, const std::string &error_message);
|
static bool IsEveryFilenameValid(const std::string &path, size_t length_limit, const std::string &error_message);
|
||||||
|
|
Loading…
Reference in New Issue