ADD file via upload

This commit is contained in:
zyf1234 2023-08-27 15:01:06 +08:00
parent 011ed9ffa5
commit 9cc0e3064e
1 changed files with 245 additions and 0 deletions

245
df_graph_manager.cc Normal file
View File

@ -0,0 +1,245 @@
/**
* Copyright 2019 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 "include/transform/graph_ir/df_graph_manager.h"
#include <sstream>
#ifndef ENABLE_LITE_ACL
#include "include/common/utils/python_adapter.h"
#include "pipeline/jit/pipeline.h"
#endif
#ifndef NO_DLIB
#include "tdt/tsd_client.h"
#endif
namespace mindspore {
namespace transform {
// 此构造函数用于初始化 DfGraphWrapper 类的一个新实例。
// 它接受四个参数:'name'、'id'、'graph_ptr' 和 'options'
DfGraphWrapper::DfGraphWrapper(const std::string &name, const int &id, const DfGraphPtr &graph_ptr,
const OptionMap &options)
: name_(name), id_(id), graph_ptr_(graph_ptr), options_(options) {}
DfGraphManager::DfGraphManager() { //构造函数
graph_id_ = 0;
graph_runner_ptr_ = nullptr;
sess_ptr_ = nullptr;
}
DfGraphManager::~DfGraphManager() { //析构函数
// in python first destroy after atexit but in c++ destoy before atexit
DeleteGraphRunner();
DeleteGeSession();
ClearGraph();
#ifndef ENABLE_LITE_ACL
python_adapter::set_python_env_flag(false);
#endif
}
DfGraphManager &DfGraphManager::GetInstance() {
static DfGraphManager instance;
return instance;
}
// 该函数用于生成图形ID。
int DfGraphManager::GenerateId() {
graph_id_++; // 递增图形ID
if (graph_id_ <= 0) { // 如果图形ID小于等于0则将其设置为1确保ID不为负数
graph_id_ = 1;
}
MS_LOG(INFO) << "Generate graph Id : " << graph_id_; // 打印生成的图形ID仅用于日志记录
return graph_id_; // 返回生成的图形ID
}
// 该函数用于向图形管理器中添加一个图形。
// 参数 'name' 表示图形的名称,'graph_ptr' 表示图形的指针,'options' 表示图形的选项。
Status DfGraphManager::AddGraph(const std::string &name, const DfGraphPtr &graph_ptr, const OptionMap &options) {
std::lock_guard<std::mutex> lg(lock_); // 使用互斥锁,确保添加图形的操作是线程安全的
if (name.empty()) { // 如果图形名称为空,返回无效参数错误
MS_LOG(ERROR) << "The graph name is null, add graph failed";
return Status::INVALID_ARGUMENT;
}
if (graph_ptr == nullptr) { // 如果图形指针为空,返回无效参数错误
MS_LOG(INFO) << "The new graph {" << name << "}'s pointer is null, add graph failed";
return Status::INVALID_ARGUMENT;
}
int id = GenerateId(); // 生成一个新的图形ID
// 创建一个 DfGraphWrapperPtr 对象,用于包装图形信息,并将图形添加到图形管理器中
DfGraphWrapperPtr wrap_ptr = std::make_shared<DfGraphWrapper>(name, id, graph_ptr, options);
// 将图形添加到图形管理器中
auto ret = graphs_.emplace(name, wrap_ptr);
if (ret.second == false) { // 如果图形名称已经存在,将旧的图形覆盖
MS_LOG(WARNING) << "The graph name:{ " << name << " }is already exists! The old graph will be overwritten!!";
ret.first->second = wrap_ptr;
}
MS_LOG(INFO) << "Add graph " << name << " to GraphManager success!"; // 成功添加图形后记录日志
return Status::SUCCESS; // 返回成功状态
}
// 该函数用于获取图形管理器中的所有图形,并以一个 DfGraphWrapperPtr 类型的向量返回这些图形。
std::vector<DfGraphWrapperPtr> DfGraphManager::GetAllGraphs() {
std::lock_guard<std::mutex> lg(lock_); // 使用互斥锁,确保获取图形的操作是线程安全的
std::vector<DfGraphWrapperPtr> ret; // 创建用于存储图形的向量
std::stringstream ss;
ss << "{ ";
for (auto it = graphs_.begin(); it != graphs_.end(); ++it) { // 遍历图形管理器中的所有图形
ss << it->first << ", "; // 将图形名称添加到日志记录字符串中
ret.emplace_back(it->second); // 将图形指针添加到返回向量中
}
ss << "}";
MS_LOG(INFO) << "Return graphs: " << ss.str(); // 记录获取的图形名称到日志
return ret; // 返回存储所有图形的向量
}
// 该函数用于获取已保存的图形名称集合。
// 返回类型为 std::set<string>,表示一个存储唯一图形名称的集合。
std::set<string> DfGraphManager::GetSavedGraphs() { return saved_graphs_; } // 直接返回保存的图形名称集合
// 该函数用于向已保存的图形名称集合中添加新的图形名称。
// 参数 'id' 表示要添加的图形名称。
void DfGraphManager::AddSavedGraphs(const std::string &id) { saved_graphs_.insert(id); } // 将新的图形名称 'id' 插入已保存的图形名称集合中
// 该函数用于根据图形名称获取对应的 DfGraphWrapperPtr 对象。
// 参数 'name' 表示要获取的图形名称。
DfGraphWrapperPtr DfGraphManager::GetGraphByName(const std::string &name) {
std::lock_guard<std::mutex> lg(lock_); // 使用互斥锁,确保获取图形的操作是线程安全的
if (name.empty()) {
MS_LOG(ERROR) << "The graph name is null";
return nullptr; // 如果图形名称为空,返回空指针
}
auto it = graphs_.find(name);
if (it == graphs_.end()) {
MS_LOG(INFO) << "Can't found graph name: " << name;
return nullptr; // 如果图形名称在图形管理器中找不到,返回空指针
}
MS_LOG(INFO) << "Return graph: " << name; // 记录获取的图形名称到日志
return it->second; // 返回找到的图形的 DfGraphWrapperPtr 对象
}
// 该函数用于清空图形管理器中的所有图形,并释放相关资源。
void DfGraphManager::ClearGraph() noexcept {
std::lock_guard<std::mutex> lg(lock_); // 使用互斥锁,确保清空图形的操作是线程安全的
graphs_.clear(); // 清空图形管理器中的所有图形
anf_graphs_.clear(); // 清空图形管理器中的所有 ANF 图形(可能是另一种图形表示)
MS_LOG(INFO) << "Remove all graphs in GraphManager"; // 记录已清空所有图形的日志
}
// 该函数用于将给定的 ANF 图形指针与特定的图形名称相关联。
// 参数 'name' 表示图形的名称,'anf_graph_ptr' 表示要关联的 ANF 图形指针。
void DfGraphManager::SetAnfGraph(const std::string &name, const AnfGraphPtr &anf_graph_ptr) {
DfGraphWrapperPtr df_graph = GetGraphByName(name); // 获取给定名称的图形包装器对象
if (df_graph == nullptr) {
MS_LOG(ERROR) << "Can't found graph name: " << name;
return; // 如果找不到给定名称的图形,则返回错误并退出函数
}
std::lock_guard<std::mutex> lg(lock_); // 使用互斥锁,确保设置 ANF 图形的操作是线程安全的
anf_graphs_[df_graph->id_] = anf_graph_ptr; // 将给定的 ANF 图形指针与图形包装器的 ID 相关联,并存储在 anf_graphs_ 容器中
}
// 该函数用于根据给定的图形 ID 获取对应的 ANF 图形指针。
// 参数 'graph_id' 表示要获取的图形 ID。
AnfGraphPtr DfGraphManager::GetAnfGraph(uint32_t graph_id) {
std::lock_guard<std::mutex> lg(lock_); // 使用互斥锁,确保获取 ANF 图形的操作是线程安全的
auto iter = anf_graphs_.find(graph_id);
if (iter == anf_graphs_.end()) {
MS_LOG(ERROR) << "Can't found anf graph, graph_id = " << graph_id;
return nullptr; // 如果找不到给定图形 ID 对应的 ANF 图形,则记录错误日志并返回空指针
}
return iter->second; // 返回找到的图形 ID 对应的 ANF 图形指针
}
// 该函数用于清空 ANF 图形容器,即移除所有已关联的 ANF 图形。
void DfGraphManager::EraseAnfGraph() {
std::lock_guard<std::mutex> lg(lock_); // 使用互斥锁,确保清空 ANF 图形容器的操作是线程安全的
anf_graphs_.clear(); // 清空 ANF 图形容器,移除所有已关联的 ANF 图形
}
// 该函数用于设置与图形管理器关联的 GEGraphEngine会话指针。
// 参数 'sess_ptr' 表示要设置的 GE 会话指针。
void DfGraphManager::SetGeSession(const std::shared_ptr<ge::Session> &sess_ptr) {
std::lock_guard<std::mutex> lg(lock_); // 使用互斥锁,确保设置 GE 会话的操作是线程安全的
if (sess_ptr == nullptr) {
MS_LOG(WARNING) << "You are adding a empty Ge Session"; // 如果传入的 GE 会话指针为空,记录警告日志
}
if (sess_ptr_ == nullptr) {
MS_LOG(INFO) << "Add a new Ge Session success"; // 如果之前未设置过 GE 会话,记录设置成功的日志
} else { // 如果之前已经设置过 GE 会话,记录设置成功的日志,并提示之前的 GE 会话将被覆盖
MS_LOG(INFO) << "Add a new Ge Session success, the old Ge Session will be overwritten!!";
}
sess_ptr_ = sess_ptr; // 将传入的 GE 会话指针设置为图形管理器关联的 GE 会话指针
}
// 该函数用于获取图形管理器关联的 GEGraphEngine会话指针。
// 返回类型为 std::shared_ptr<ge::Session>,表示 GE 会话指针。
std::shared_ptr<ge::Session> DfGraphManager::GetGeSession() {
std::lock_guard<std::mutex> lg(lock_); // 使用互斥锁,确保获取 GE 会话指针的操作是线程安全的
return sess_ptr_; // 返回图形管理器关联的 GE 会话指针
}
// 该函数用于删除图形管理器关联的 GEGraphEngine会话并清除与该会话相关的数据。
void DfGraphManager::DeleteGeSession() noexcept {
std::lock_guard<std::mutex> lg(lock_); // 使用互斥锁,确保删除 GE 会话的操作是线程安全的
if (sess_ptr_ == nullptr) {
MS_LOG(INFO) << "Ge Session is not exist"; // 如果当前没有关联的 GE 会话,记录日志并直接返回
} else {
sess_ptr_ = nullptr; // 将关联的 GE 会话指针设置为空指针,表示删除 GE 会话
saved_graphs_.clear(); // 清空已保存的图形名称集合,即移除所有已保存的图形信息
MS_LOG(INFO) << "Delete Ge Session success"; // 记录删除成功的日志
}
}
// 该函数用于设置与图形管理器关联的图形运行器GraphRunner指针。
// 参数 'graph_runner_ptr' 表示要设置的图形运行器指针。
void DfGraphManager::SetGraphRunner(const std::shared_ptr<transform::GraphRunner> &graph_runner_ptr) noexcept {
std::lock_guard<std::mutex> lg(lock_); // 使用互斥锁,确保设置图形运行器的操作是线程安全的
if (graph_runner_ptr == nullptr) { // 如果传入的图形运行器指针为空,记录警告日志
MS_LOG(WARNING) << "You are adding a empty GraphRunner";
}
if (graph_runner_ptr_ == nullptr) { // 如果之前未设置过图形运行器,记录设置成功的日志
MS_LOG(INFO) << "Add a new GraphRunner success";
} else { // 如果之前已经设置过图形运行器,记录设置成功的日志,并提示之前的图形运行器将被覆盖
MS_LOG(INFO) << "Add a new GraphRunner success, the old GraphRunner will be overwritten!!";
}
graph_runner_ptr_ = graph_runner_ptr; // 将传入的图形运行器指针设置为图形管理器关联的图形运行器指针
}
// 该函数用于获取图形管理器关联的图形运行器GraphRunner指针。
// 返回类型为 std::shared_ptr<transform::GraphRunner>,表示图形运行器指针
std::shared_ptr<transform::GraphRunner> DfGraphManager::GetGraphRunner() {
std::lock_guard<std::mutex> lg(lock_); // 使用互斥锁,确保获取图形运行器指针的操作是线程安全的
return graph_runner_ptr_; // 返回图形管理器关联的图形运行器指针
}
// 该函数用于删除图形管理器关联的图形运行器GraphRunner
void DfGraphManager::DeleteGraphRunner() noexcept {
std::lock_guard<std::mutex> lg(lock_); // 使用互斥锁,确保删除图形运行器的操作是线程安全的
if (graph_runner_ptr_ == nullptr) {
MS_LOG(INFO) << "GraphRunner is not exist"; // 如果当前没有关联的图形运行器,记录日志并直接返回
} else {
graph_runner_ptr_ = nullptr; // 将关联的图形运行器指针设置为空指针,表示删除图形运行器
MS_LOG(INFO) << "Delete GraphRunner success"; // 记录删除成功的日志
}
}
} // namespace transform
} // namespace mindspore