!45174 [MS][LITE] converter api support dual ABI

Merge pull request !45174 from XianglongZeng/myms_2
This commit is contained in:
i-robot 2022-11-09 02:48:07 +00:00 committed by Gitee
commit d0b4a09987
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
7 changed files with 151 additions and 104 deletions

View File

@ -67,18 +67,20 @@ inline std::set<std::string> SetCharToString(const std::set<std::vector<char>> &
return ret;
}
inline std::map<std::vector<char>, int32_t> MapStringToChar(const std::map<std::string, int32_t> &s) {
std::map<std::vector<char>, int32_t> ret;
template <class T>
inline std::map<std::vector<char>, T> MapStringToChar(const std::map<std::string, T> &s) {
std::map<std::vector<char>, T> ret;
std::transform(s.begin(), s.end(), std::inserter(ret, ret.begin()), [](auto str) {
return std::pair<std::vector<char>, int32_t>(std::vector<char>(str.first.begin(), str.first.end()), str.second);
return std::pair<std::vector<char>, T>(std::vector<char>(str.first.begin(), str.first.end()), str.second);
});
return ret;
}
inline std::map<std::string, int32_t> MapCharToString(const std::map<std::vector<char>, int32_t> &c) {
std::map<std::string, int32_t> ret;
template <class T>
inline std::map<std::string, T> MapCharToString(const std::map<std::vector<char>, T> &c) {
std::map<std::string, T> ret;
std::transform(c.begin(), c.end(), std::inserter(ret, ret.begin()), [](auto ch) {
return std::pair<std::string, int32_t>(std::string(ch.first.begin(), ch.first.end()), ch.second);
return std::pair<std::string, T>(std::string(ch.first.begin(), ch.first.end()), ch.second);
});
return ret;
}
@ -151,24 +153,6 @@ inline std::vector<std::pair<std::vector<char>, int64_t>> PairStringInt64ToPairC
return ret;
}
template <class T>
inline std::map<std::vector<char>, T> PadInfoStringToChar(const std::map<std::string, T> &s_pad_info) {
std::map<std::vector<char>, T> ret;
std::transform(s_pad_info.begin(), s_pad_info.end(), std::inserter(ret, ret.begin()), [](auto str) {
return std::pair<std::vector<char>, T>(std::vector<char>(str.first.begin(), str.first.end()), str.second);
});
return ret;
}
template <class T>
inline std::map<std::string, T> PadInfoCharToString(const std::map<std::vector<char>, T> &c_pad_info) {
std::map<std::string, T> ret;
std::transform(c_pad_info.begin(), c_pad_info.end(), std::inserter(ret, ret.begin()), [](auto ch) {
return std::pair<std::string, T>(std::string(ch.first.begin(), ch.first.end()), ch.second);
});
return ret;
}
template <class T>
inline void TensorMapCharToString(const std::map<std::vector<char>, T> *c, std::unordered_map<std::string, T> *s) {
if (c == nullptr || s == nullptr) {

View File

@ -129,7 +129,7 @@ Status Serialization::Load(const std::vector<std::string> &files, ModelType mode
}
Status Serialization::SetParameters(const std::map<std::string, Buffer> &parameters, Model *model) {
return SetParameters(PadInfoStringToChar<Buffer>(parameters), model);
return SetParameters(MapStringToChar<Buffer>(parameters), model);
}
Status Serialization::ExportModel(const Model &model, ModelType model_type, const std::string &model_file,

View File

@ -446,9 +446,9 @@ BucketBatchByLengthDataset::BucketBatchByLengthDataset(
if (input == nullptr) {
ir_node_ = nullptr;
} else {
auto ds = std::make_shared<BucketBatchByLengthNode>(
input->IRNode(), VectorCharToString(column_names), bucket_boundaries, bucket_batch_sizes, c_func,
PadInfoCharToString(map), pad_to_bucket_boundary, drop_remainder);
auto ds = std::make_shared<BucketBatchByLengthNode>(input->IRNode(), VectorCharToString(column_names),
bucket_boundaries, bucket_batch_sizes, c_func,
MapCharToString(map), pad_to_bucket_boundary, drop_remainder);
ir_node_ = std::static_pointer_cast<DatasetNode>(ds);
}

View File

@ -262,7 +262,7 @@ class DATASET_API Dataset : public std::enable_shared_from_this<Dataset> {
bool pad_to_bucket_boundary = false, bool drop_remainder = false) {
return std::make_shared<BucketBatchByLengthDataset>(
shared_from_this(), VectorStringToChar(column_names), bucket_boundaries, bucket_batch_sizes,
element_length_function, PadInfoStringToChar(pad_info), pad_to_bucket_boundary, drop_remainder);
element_length_function, MapStringToChar(pad_info), pad_to_bucket_boundary, drop_remainder);
}
/// \brief Function to create a SentencePieceVocab from source dataset.

View File

@ -23,26 +23,27 @@
#include "include/api/format.h"
#include "include/api/status.h"
#include "include/registry/converter_context.h"
#include "include/api/dual_abi_helper.h"
namespace mindspore {
struct ConverterPara;
class MS_API Converter {
public:
Converter(converter::FmkType fmk_type, const std::string &model_file, const std::string &output_file = "",
const std::string &weight_file = "");
inline Converter(converter::FmkType fmk_type, const std::string &model_file, const std::string &output_file = "",
const std::string &weight_file = "");
~Converter() = default;
void SetConfigFile(const std::string &config_file);
std::string GetConfigFile() const;
inline void SetConfigFile(const std::string &config_file);
inline std::string GetConfigFile() const;
void SetConfigInfo(const std::string &section, const std::map<std::string, std::string> &config);
std::map<std::string, std::map<std::string, std::string>> GetConfigInfo() const;
inline void SetConfigInfo(const std::string &section, const std::map<std::string, std::string> &config);
inline std::map<std::string, std::map<std::string, std::string>> GetConfigInfo() const;
void SetWeightFp16(bool weight_fp16);
bool GetWeightFp16() const;
void SetInputShape(const std::map<std::string, std::vector<int64_t>> &input_shape);
std::map<std::string, std::vector<int64_t>> GetInputShape() const;
inline void SetInputShape(const std::map<std::string, std::vector<int64_t>> &input_shape);
inline std::map<std::string, std::vector<int64_t>> GetInputShape() const;
void SetInputFormat(Format format);
Format GetInputFormat() const;
@ -56,17 +57,17 @@ class MS_API Converter {
void SetExportMindIR(ModelType export_mindir);
ModelType GetExportMindIR() const;
void SetDecryptKey(const std::string &key);
std::string GetDecryptKey() const;
inline void SetDecryptKey(const std::string &key);
inline std::string GetDecryptKey() const;
void SetDecryptMode(const std::string &mode);
std::string GetDecryptMode() const;
inline void SetDecryptMode(const std::string &mode);
inline std::string GetDecryptMode() const;
void SetEnableEncryption(bool encryption);
bool GetEnableEncryption() const;
void SetEncryptKey(const std::string &key);
std::string GetEncryptKey() const;
inline void SetEncryptKey(const std::string &key);
inline std::string GetEncryptKey() const;
void SetInfer(bool infer);
bool GetInfer() const;
@ -77,14 +78,70 @@ class MS_API Converter {
void SetNoFusion(bool no_fusion);
bool GetNoFusion();
void SetDevice(const std::string &device);
std::string GetDevice();
inline void SetDevice(const std::string &device);
inline std::string GetDevice();
Status Convert();
void *Convert(size_t *data_size);
private:
Converter(converter::FmkType fmk_type, const std::vector<char> &model_file, const std::vector<char> &output_file,
const std::vector<char> &weight_file);
void SetConfigFile(const std::vector<char> &config_file);
std::vector<char> GetConfigFileChar() const;
void SetConfigInfo(const std::vector<char> &section, const std::map<std::vector<char>, std::vector<char>> &config);
std::map<std::vector<char>, std::map<std::vector<char>, std::vector<char>>> GetConfigInfoChar() const;
void SetInputShape(const std::map<std::vector<char>, std::vector<int64_t>> &input_shape);
std::map<std::vector<char>, std::vector<int64_t>> GetInputShapeChar() const;
void SetDecryptKey(const std::vector<char> &key);
std::vector<char> GetDecryptKeyChar() const;
void SetDecryptMode(const std::vector<char> &mode);
std::vector<char> GetDecryptModeChar() const;
void SetEncryptKey(const std::vector<char> &key);
std::vector<char> GetEncryptKeyChar() const;
void SetDevice(const std::vector<char> &device);
std::vector<char> GetDeviceChar();
std::shared_ptr<ConverterPara> data_;
};
Converter::Converter(converter::FmkType fmk_type, const std::string &model_file, const std::string &output_file,
const std::string &weight_file)
: Converter(fmk_type, StringToChar(model_file), StringToChar(output_file), StringToChar(weight_file)) {}
void Converter::SetConfigFile(const std::string &config_file) { SetConfigFile(StringToChar(config_file)); }
std::string Converter::GetConfigFile() const { return CharToString(GetConfigFileChar()); }
void Converter::SetConfigInfo(const std::string &section, const std::map<std::string, std::string> &config) {
SetConfigInfo(StringToChar(section), MapStringToVectorChar(config));
}
std::map<std::string, std::map<std::string, std::string>> Converter::GetConfigInfo() const {
return MapMapCharToString(GetConfigInfoChar());
}
void Converter::SetInputShape(const std::map<std::string, std::vector<int64_t>> &input_shape) {
SetInputShape(MapStringToChar(input_shape));
}
std::map<std::string, std::vector<int64_t>> Converter::GetInputShape() const {
return MapCharToString(GetInputShapeChar());
}
void Converter::SetDecryptKey(const std::string &key) { SetDecryptKey(StringToChar(key)); }
std::string Converter::GetDecryptKey() const { return CharToString(GetDecryptKeyChar()); }
void Converter::SetDecryptMode(const std::string &mode) { SetDecryptMode(StringToChar(mode)); }
std::string Converter::GetDecryptMode() const { return CharToString(GetDecryptModeChar()); }
void Converter::SetEncryptKey(const std::string &key) { SetEncryptKey(StringToChar(key)); }
std::string Converter::GetEncryptKey() const { return CharToString(GetEncryptKeyChar()); }
void Converter::SetDevice(const std::string &device) { SetDevice(StringToChar(device)); }
std::string Converter::GetDevice() { return CharToString(GetDeviceChar()); }
} // namespace mindspore
#endif // MINDSPORE_LITE_INCLUDE_CONVERTER_H_

View File

@ -32,13 +32,15 @@ void ConverterPyBind(const py::module &m) {
py::class_<Converter, std::shared_ptr<Converter>>(m, "ConverterBind")
.def(py::init<converter::FmkType, const std::string &, const std::string &, const std::string &>())
.def("set_config_file", &Converter::SetConfigFile)
.def("set_config_file", py::overload_cast<const std::string &>(&Converter::SetConfigFile))
.def("get_config_file", &Converter::GetConfigFile)
.def("set_config_info", &Converter::SetConfigInfo)
.def("set_config_info",
py::overload_cast<const std::string &, const std::map<std::string, std::string> &>(&Converter::SetConfigInfo))
.def("get_config_info", &Converter::GetConfigInfo)
.def("set_weight_fp16", &Converter::SetWeightFp16)
.def("get_weight_fp16", &Converter::GetWeightFp16)
.def("set_input_shape", &Converter::SetInputShape)
.def("set_input_shape",
py::overload_cast<const std::map<std::string, std::vector<int64_t>> &>(&Converter::SetInputShape))
.def("get_input_shape", &Converter::GetInputShape)
.def("set_input_format", &Converter::SetInputFormat)
.def("get_input_format", &Converter::GetInputFormat)
@ -48,13 +50,13 @@ void ConverterPyBind(const py::module &m) {
.def("get_output_data_type", &Converter::GetOutputDataType)
.def("set_export_mindir", &Converter::SetExportMindIR)
.def("get_export_mindir", &Converter::GetExportMindIR)
.def("set_decrypt_key", &Converter::SetDecryptKey)
.def("set_decrypt_key", py::overload_cast<const std::string &>(&Converter::SetDecryptKey))
.def("get_decrypt_key", &Converter::GetDecryptKey)
.def("set_decrypt_mode", &Converter::SetDecryptMode)
.def("set_decrypt_mode", py::overload_cast<const std::string &>(&Converter::SetDecryptMode))
.def("get_decrypt_mode", &Converter::GetDecryptMode)
.def("set_enable_encryption", &Converter::SetEnableEncryption)
.def("get_enable_encryption", &Converter::GetEnableEncryption)
.def("set_encrypt_key", &Converter::SetEncryptKey)
.def("set_encrypt_key", py::overload_cast<const std::string &>(&Converter::SetEncryptKey))
.def("get_encrypt_key", &Converter::GetEncryptKey)
.def("set_infer", &Converter::SetInfer)
.def("get_infer", &Converter::GetInfer)

View File

@ -28,54 +28,57 @@ constexpr size_t kMaxConfigNumPerSection = 1000;
namespace lite {
int RunConverter(const std::shared_ptr<ConverterPara> &data_);
}
Converter::Converter(converter::FmkType fmk_type, const std::string &model_file, const std::string &output_file,
const std::string &weight_file) {
Converter::Converter(converter::FmkType fmk_type, const std::vector<char> &model_file,
const std::vector<char> &output_file, const std::vector<char> &weight_file) {
data_ = std::make_shared<ConverterPara>();
if (data_ != nullptr) {
data_->fmk_type = fmk_type;
data_->model_file = model_file;
data_->output_file = output_file;
data_->weight_file = weight_file;
data_->model_file = CharToString(model_file);
data_->output_file = CharToString(output_file);
data_->weight_file = CharToString(weight_file);
} else {
MS_LOG(ERROR) << "Create ConverterPara failed";
}
}
void Converter::SetConfigFile(const std::string &config_file) {
void Converter::SetConfigFile(const std::vector<char> &config_file) {
if (data_ != nullptr) {
data_->config_file = config_file;
data_->config_file = CharToString(config_file);
}
}
std::string Converter::GetConfigFile() const {
std::vector<char> Converter::GetConfigFileChar() const {
std::string cfg_file = "";
if (data_ != nullptr) {
return data_->config_file;
} else {
return "";
cfg_file = data_->config_file;
}
return StringToChar(cfg_file);
}
void Converter::SetConfigInfo(const std::string &section, const std::map<std::string, std::string> &config) {
void Converter::SetConfigInfo(const std::vector<char> &section,
const std::map<std::vector<char>, std::vector<char>> &config) {
auto section_str = CharToString(section);
auto config_str = MapVectorCharToString(config);
if (data_ != nullptr) {
if (data_->config_param.size() > kMaxSectionNum) {
MS_LOG(ERROR) << "Section num " << data_->config_param.size() << "exceeds max num " << kMaxSectionNum;
return;
}
if (data_->config_param.find(section) != data_->config_param.end()) {
MS_LOG(WARNING) << "Section " << section << "already exists, "
if (data_->config_param.find(section_str) != data_->config_param.end()) {
MS_LOG(WARNING) << "Section " << section_str << "already exists, "
<< "value will be overwrite.";
}
if (config.size() > kMaxConfigNumPerSection) {
MS_LOG(ERROR) << "Config num " << config.size() << " exceeds max num " << kMaxConfigNumPerSection << " in "
<< section;
<< section_str;
return;
}
data_->config_param[section] = config;
data_->config_param[section_str] = config_str;
}
}
std::map<std::string, std::map<std::string, std::string>> Converter::GetConfigInfo() const {
return data_->config_param;
std::map<std::vector<char>, std::map<std::vector<char>, std::vector<char>>> Converter::GetConfigInfoChar() const {
return MapMapStringToChar(data_->config_param);
}
void Converter::SetWeightFp16(bool weight_fp16) {
@ -92,21 +95,22 @@ bool Converter::GetWeightFp16() const {
}
}
void Converter::SetInputShape(const std::map<std::string, std::vector<int64_t>> &input_shape) {
void Converter::SetInputShape(const std::map<std::vector<char>, std::vector<int64_t>> &input_shape) {
auto input_shape_str = MapCharToString(input_shape);
if (data_ != nullptr) {
for (auto &it : input_shape) {
for (auto &it : input_shape_str) {
lite::ConverterInnerContext::GetInstance()->UpdateGraphInputTensorShape(it.first, it.second);
}
data_->input_shape = input_shape;
data_->input_shape = input_shape_str;
}
}
std::map<std::string, std::vector<int64_t>> Converter::GetInputShape() const {
std::map<std::vector<char>, std::vector<int64_t>> Converter::GetInputShapeChar() const {
std::map<std::string, std::vector<int64_t>> input_shape = {};
if (data_ != nullptr) {
return data_->input_shape;
} else {
return {};
input_shape = data_->input_shape;
}
return MapStringToChar(input_shape);
}
void Converter::SetInputFormat(Format format) {
@ -168,32 +172,32 @@ ModelType Converter::GetExportMindIR() const {
}
}
void Converter::SetDecryptKey(const std::string &key) {
void Converter::SetDecryptKey(const std::vector<char> &key) {
if (data_ != nullptr) {
data_->decrypt_key = key;
data_->decrypt_key = CharToString(key);
}
}
std::string Converter::GetDecryptKey() const {
std::vector<char> Converter::GetDecryptKeyChar() const {
std::string decrypt_key = "";
if (data_ != nullptr) {
return data_->decrypt_key;
} else {
return "";
decrypt_key = data_->decrypt_key;
}
return StringToChar(decrypt_key);
}
void Converter::SetDecryptMode(const std::vector<char> &mode) {
if (data_ != nullptr) {
data_->decrypt_mode = CharToString(mode);
}
}
void Converter::SetDecryptMode(const std::string &mode) {
std::vector<char> Converter::GetDecryptModeChar() const {
std::string decrypt_mode = "";
if (data_ != nullptr) {
data_->decrypt_mode = mode;
}
}
std::string Converter::GetDecryptMode() const {
if (data_ != nullptr) {
return data_->decrypt_mode;
} else {
return "";
decrypt_mode = data_->decrypt_mode;
}
return StringToChar(decrypt_mode);
}
void Converter::SetEnableEncryption(bool encryption) {
@ -210,18 +214,18 @@ bool Converter::GetEnableEncryption() const {
}
}
void Converter::SetEncryptKey(const std::string &key) {
void Converter::SetEncryptKey(const std::vector<char> &key) {
if (data_ != nullptr) {
data_->encrypt_key = key;
data_->encrypt_key = CharToString(key);
}
}
std::string Converter::GetEncryptKey() const {
std::vector<char> Converter::GetEncryptKeyChar() const {
std::string encrypt_key = "";
if (data_ != nullptr) {
return data_->encrypt_key;
} else {
return "";
encrypt_key = data_->encrypt_key;
}
return StringToChar(encrypt_key);
}
void Converter::SetInfer(bool infer) {
@ -266,18 +270,18 @@ bool Converter::GetNoFusion() {
}
}
void Converter::SetDevice(const std::string &device) {
void Converter::SetDevice(const std::vector<char> &device) {
if (data_ != nullptr) {
data_->device = device;
data_->device = CharToString(device);
}
}
std::string Converter::GetDevice() {
std::vector<char> Converter::GetDeviceChar() {
std::string device = "";
if (data_ != nullptr) {
return data_->device;
} else {
return "";
device = data_->device;
}
return StringToChar(device);
}
Status Converter::Convert() {