* Copyright (c) 2025 Huawei Technologies Co., Ltd.
* This program is free software, you can redistribute it and/or modify it under the terms and conditions of
* CANN Open Software License Agreement Version 2.0 (the "License").
* Please refer to the License for details. You may not use this file except in compliance with the License.
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
* See LICENSE in the root of the software repository for the full text of the License.
*/
#ifndef INC_FRAMEWORK_COMMON_FILE_CONSTANT_UTILS_H
#define INC_FRAMEWORK_COMMON_FILE_CONSTANT_UTILS_H
#include <map>
#include <string>
#include <vector>
#include "ge/ge_api_error_codes.h"
#include "graph/op_desc.h"
#include "graph/ge_tensor.h"
#include "graph/node.h"
#include "common/ge_common/ge_types.h"
#include "graph/manager/graph_external_weight_manager.h"
#include "nlohmann/json.hpp"
namespace ge {
constexpr const char *kExternalWeightDisabled = "0";
constexpr const char *kExternalWeightEnabled = "1";
constexpr const char *kExternalWeightCombined = "2";
using ConstNodeWeightHashList = std::vector<std::pair<NodePtr, std::pair<GeTensorPtr, std::string>>>;
struct FileIdToFilePath {
std::string value_bin_file_id;
std::string value_bin_file_path;
};
struct OptionInfo {
std::vector<FileIdToFilePath> info;
};
void from_json(const nlohmann::json &j, FileIdToFilePath &info);
void from_json(const nlohmann::json &j, OptionInfo &option_info);
* 1.通过可选IR属性file_path直接设置或者获取外置权重文件的路径;
* 2.通过可选IR属性file_id设置外置权重文件的唯一标识,并通过option ge.exec.value_bins设置file id到file path的映射;
* 3.通过私有属性location获取外置权重路径,该属性存在于两种场景:
* ① parser模块解析onnx模型的外置权重时,权重路径会被写在节点的location属性上;
* ② 开启权重外置功能(ge.externalWeight)时,生成的外置权重文件的路径会被写在location属性上。
*/
class FileConstantUtils {
public:
static Status GetFileIdToPathMapFromOption(std::map<std::string, std::string> &file_id_to_path_map);
static Status CopyOneWeightFromFileWithFilehandler(const void *const curr_dev_ptr, const std::string &file_path,
const size_t offset, const size_t file_constant_size,
size_t &left_size, std::ifstream &ifs);
static Status CopyOneWeightFromFile(const void *const curr_dev_ptr, const std::string &file_path, const size_t offset,
const size_t file_constant_size, size_t &left_size);
static Status GetFilePath(const OpDescPtr &op_desc, const std::map<std::string, std::string> &file_id_to_path_map,
std::string &file_path, size_t &offset, size_t &length);
static FileConstantInfo GetFileConstantInfo(const OpDescPtr &op_desc);
static Status GetExternalWeightDirFromOmPath(const std::string &om_path, string &file_constant_weight_dir);
static Status GetExternalWeightDir(const ge::ModelData &model_data, string &file_constant_weight_dir);
static Status SetExternalPath(const OpDescPtr &op_desc, const std::string &weight_dir);
static Status SetExternalPath(const ComputeGraphPtr &compute_graph, const std::string &weight_dir);
static Status ReadExternalWeightFromFile(const std::string &file_path, const size_t offset, const size_t file_length,
char_t *const bin_buff);
static Status ConvertFileConstToConst(const ComputeGraphPtr &compute_graph);
static Status ConvertFileConstToConst(const NodePtr &node);
static Status ConvertConstToFileConst(const ComputeGraphPtr &compute_graph, bool all_in_one = false);
static Status ConvertConstToFileConst(const NodePtr &node);
static ConstNodeWeightHashList GetAllConstNodesAndWeightHash(const ComputeGraphPtr &compute_graph);
static Status ChangeFilePath(const ComputeGraphPtr &compute_graph, const std::string &om_path);
static Status MoveExternalWeightFiles(const std::map<std::string, std::string> &old_file_to_new_file);
static std::string GetTmpWeightDir(const int32_t pid, const uint64_t session_id);
static void SetFileConstantPath(const OpDescPtr &op_desc, const std::string &file_path, const int64_t offset = 0,
const int64_t length = 0);
static Status RefreshRelativePath(const ComputeGraphPtr &compute_graph);
private:
friend class ExternalWeightManager;
static Status ConvertToFileConstants(const ConstNodeWeightHashList &const_to_weight_hash_list,
const std::string &weight_dir, FileConstantMeta &meta, const bool all_in_one=false);
static Status SaveWeightToFileWithReuse(const ConstNodeWeightHashList &const_to_weight_hash_list,
const std::string &weight_dir, FileConstantMeta &meta);
static Status SaveWeightToOneFileWithReuse(const ConstNodeWeightHashList &const_to_weight_hash_list,
const std::string &weight_dir, FileConstantMeta &meta);
static Status SaveWeightsAndMetadata(const ConstNodeWeightHashList &const_to_weight_hash_list,
FileConstantMeta &meta, std::ofstream &ofs,
const std::string &weight_path, size_t &offset,
size_t &new_weights_written, size_t &reused_weights_count);
static Status WriteWeightWithPadding(std::ofstream &ofs, const uint8_t *weight_data_ptr,
const size_t size, size_t &offset);
static Status ChangeFilePathAttr(const ComputeGraphPtr &compute_graph, const std::string &om_path,
std::map<std::string, std::string> &old_file_to_new_file);
static Status MoveFilePath(const std::map<std::string, std::string> &old_file_to_new_file);
static void GetValidFullPath(const std::string &dir_name, const std::string &file_name, std::string &full_name);
};
}
#endif