Co-authored-by: gongweibao <gognweibao@baidu.com>
This commit is contained in:
gongweibao
2026-03-04 21:55:31 +08:00
committed by GitHub
parent 5c8f5184d9
commit ddb06ff83f
306 changed files with 40627 additions and 34418 deletions
+83 -74
View File
@@ -22,87 +22,96 @@
#include <limits>
class ConfigManager {
public:
static ConfigManager& get_instance(const std::string& config_path = "fastdeploy_op_configs.json") {
static ConfigManager instance(config_path);
return instance;
public:
static ConfigManager& get_instance(
const std::string& config_path = "fastdeploy_op_configs.json") {
static ConfigManager instance(config_path);
return instance;
}
std::string get_best_config(const std::string& op_name,
const size_t m,
const size_t n,
const size_t k) {
initialize();
std::string mnk_string = op_name + "-" + std::to_string(update_m(m)) + "x" +
std::to_string(n) + "x" + std::to_string(k);
if (configs_.contains(mnk_string)) {
return configs_.at(mnk_string);
}
return "";
}
std::string get_best_config(const std::string& op_name, const size_t m, const size_t n, const size_t k) {
initialize();
std::string mnk_string = op_name + "-" +
std::to_string(update_m(m)) + "x" + std::to_string(n) + "x" + std::to_string(k);
if (configs_.contains(mnk_string)) {
return configs_.at(mnk_string);
}
return "";
int64_t update_m(const size_t m) {
size_t new_m = m;
if (m < 4) {
return m;
} else if (m < 16) {
return (m + 3) / 4 * 4;
} else if (m < 64) {
return (m + 15) / 16 * 16;
} else if (m < 256) {
return (m + 31) / 32 * 32;
} else if (m < 512) {
return (m + 63) / 64 * 64;
} else if (m < 1024) {
return (m + 127) / 128 * 128;
} else if (m < 8192) {
return (m + 1023) / 1024 * 1024;
} else if (m < 32768) {
return (m + 4095) / 4096 * 4096;
} else {
return 32768;
}
}
int64_t update_m(const size_t m) {
size_t new_m = m;
if (m < 4) {
return m;
} else if (m < 16) {
return (m + 3) / 4 * 4;
} else if (m < 64) {
return (m + 15) / 16 * 16;
} else if (m < 256) {
return (m + 31) / 32 * 32;
} else if (m < 512) {
return (m + 63) / 64 * 64;
} else if (m < 1024) {
return (m + 127) / 128 * 128;
} else if (m < 8192) {
return (m + 1023) / 1024 * 1024;
} else if (m < 32768) {
return (m + 4095) / 4096 * 4096;
} else {
return 32768;
}
void update(const std::string& op_name,
const size_t m,
const size_t n,
const size_t k,
const std::string& config) {
initialize();
std::string mnk_string = op_name + "-" + std::to_string(update_m(m)) + "x" +
std::to_string(n) + "x" + std::to_string(k);
configs_[mnk_string] = config;
}
void print() const {
std::cout << configs_.dump(4) << std::endl; // Pretty print with 4 spaces
}
~ConfigManager() {
std::ofstream file(config_path_);
if (file.is_open()) {
file << configs_.dump(4); // Pretty print with 4 spaces
file.close();
}
}
void update(const std::string& op_name, const size_t m, const size_t n, const size_t k, const std::string& config) {
initialize();
std::string mnk_string = op_name + "-" +
std::to_string(update_m(m)) + "x" + std::to_string(n) + "x" + std::to_string(k);
configs_[mnk_string] = config;
private:
void initialize() {
if (initialized_) return;
std::ifstream file(config_path_);
if (file.is_open()) {
try {
file >> configs_;
} catch (const std::exception& e) {
std::cerr << "Error reading configs from " << config_path_ << " : "
<< e.what() << std::endl;
configs_ = nlohmann::json::object(); // Create an empty JSON object
}
file.close();
} else {
configs_ = nlohmann::json::object(); // Create an empty JSON object
}
initialized_ = true;
}
void print() const {
std::cout << configs_.dump(4) << std::endl; // Pretty print with 4 spaces
}
ConfigManager(const std::string& config_path) : config_path_(config_path) {}
ConfigManager(const ConfigManager&) = delete;
ConfigManager& operator=(const ConfigManager&) = delete;
~ConfigManager() {
std::ofstream file(config_path_);
if (file.is_open()) {
file << configs_.dump(4); // Pretty print with 4 spaces
file.close();
}
}
private:
void initialize() {
if (initialized_) return;
std::ifstream file(config_path_);
if (file.is_open()) {
try {
file >> configs_;
} catch (const std::exception& e) {
std::cerr << "Error reading configs from " << config_path_ << " : " << e.what() << std::endl;
configs_ = nlohmann::json::object(); // Create an empty JSON object
}
file.close();
} else {
configs_ = nlohmann::json::object(); // Create an empty JSON object
}
initialized_ = true;
}
ConfigManager(const std::string& config_path) : config_path_(config_path) {}
ConfigManager(const ConfigManager&) = delete;
ConfigManager& operator=(const ConfigManager&) = delete;
nlohmann::json configs_;
std::string config_path_;
bool initialized_{false};
nlohmann::json configs_;
std::string config_path_;
bool initialized_{false};
};