Add attrProto.release_s interface (#22977)

### Description
Add AttributeProto.release_s interface, which is used to obtain the
string in the attribute using move semantics instead of copying it



### Motivation and Context
The ep_context node stores a lot of information in attributes, which may
cause the memory usage to increase. Use this interface to avoid memory
waste

---------

Co-authored-by: GenMing Zhong <genmingz@xlnx.xilinx.com>
Co-authored-by: genmingz <genmingz@amd.com>
This commit is contained in:
genmingz@AMD 2024-12-13 13:13:43 +08:00 committed by GitHub
parent 2a36fd4f6e
commit 62e7e24f17
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 17 additions and 1 deletions

View file

@ -390,6 +390,7 @@ struct ProviderHost {
virtual void AttributeProto__set_name(ONNX_NAMESPACE::AttributeProto* p, const ::std::string& value) = 0;
virtual void AttributeProto__set_type(ONNX_NAMESPACE::AttributeProto* p, ONNX_NAMESPACE::AttributeProto_AttributeType value) = 0;
virtual ONNX_NAMESPACE::TensorProto* AttributeProto__add_tensors(ONNX_NAMESPACE::AttributeProto* p) = 0;
virtual std::string* AttributeProto__release_s(ONNX_NAMESPACE::AttributeProto* p) = 0;
// GraphProto
virtual std::unique_ptr<ONNX_NAMESPACE::GraphProto> GraphProto__construct() = 0;

View file

@ -122,6 +122,7 @@ struct AttributeProto final {
void set_name(const ::std::string& value) { return g_host->AttributeProto__set_name(this, value); }
void set_type(AttributeProto_AttributeType value) { return g_host->AttributeProto__set_type(this, value); }
TensorProto* add_tensors() { return g_host->AttributeProto__add_tensors(this); }
std::string* release_s() { return g_host->AttributeProto__release_s(this); }
typedef AttributeProto_AttributeType AttributeType;
static constexpr AttributeType UNDEFINED = AttributeProto_AttributeType_UNDEFINED;

View file

@ -104,4 +104,8 @@ std::vector<std::string> attr_proto_get_strings(const ONNX_NAMESPACE::AttributeP
}
return ret;
}
std::string* attr_proto_release_string(ONNX_NAMESPACE::AttributeProto* attr) {
vai_assert(attr->type() == ONNX_NAMESPACE::AttributeProto_AttributeType_STRING, attr->name());
return attr->release_s();
}
} // namespace vaip

View file

@ -23,5 +23,6 @@ const ONNX_NAMESPACE::TensorProto& attr_proto_get_tensor(const ONNX_NAMESPACE::A
gsl::span<const int64_t> attr_proto_get_ints(const ONNX_NAMESPACE::AttributeProto& attr);
gsl::span<const float> attr_proto_get_floats(const ONNX_NAMESPACE::AttributeProto& attr);
std::vector<std::string> attr_proto_get_strings(const ONNX_NAMESPACE::AttributeProto& attr);
std::string* attr_proto_release_string(ONNX_NAMESPACE::AttributeProto* attr);
} // namespace vaip

View file

@ -449,6 +449,13 @@ vaip_core::OrtApiForVaip* create_org_api_hook() {
return vaip_core::DllSafe(model_proto.SerializeAsString());
};
the_global_api.model_proto_delete = [](ONNX_NAMESPACE::ModelProto* p) { delete p; };
the_global_api.attr_proto_release_string = [](ONNX_NAMESPACE::AttributeProto* attr) -> vaip_core::DllSafe<std::string> {
auto pstr = vaip::attr_proto_release_string(attr);
std::string local_str = std::move(*pstr);
pstr = nullptr;
return vaip_core::DllSafe<std::string>(std::move(local_str));
};
if (!s_library_vitisaiep.vaip_get_version) {
return reinterpret_cast<vaip_core::OrtApiForVaip*>(&(the_global_api.host_));
} else {

View file

@ -13,7 +13,7 @@ struct OrtApi;
namespace vaip_core {
#define VAIP_ORT_API_MAJOR (11u)
#define VAIP_ORT_API_MAJOR (12u)
#define VAIP_ORT_API_MINOR (0u)
#define VAIP_ORT_API_PATCH (0u)
struct OrtApiForVaip {
@ -234,6 +234,7 @@ struct OrtApiForVaip {
ModelProto* (*model_to_proto)(Model& model); // [95]
DllSafe<std::string> (*model_proto_serialize_as_string)(ModelProto& model_proto); // [96]
void (*model_proto_delete)(ModelProto* p); // [97]
DllSafe<std::string> (*attr_proto_release_string)(AttributeProto* attr); // [98]
};
#ifndef USE_VITISAI

View file

@ -497,6 +497,7 @@ struct ProviderHostImpl : ProviderHost {
void AttributeProto__set_name(ONNX_NAMESPACE::AttributeProto* p, const ::std::string& value) override { return p->set_name(value); }
void AttributeProto__set_type(ONNX_NAMESPACE::AttributeProto* p, ONNX_NAMESPACE::AttributeProto_AttributeType value) override { return p->set_type(value); }
ONNX_NAMESPACE::TensorProto* AttributeProto__add_tensors(ONNX_NAMESPACE::AttributeProto* p) override { return p->add_tensors(); }
std::string* AttributeProto__release_s(ONNX_NAMESPACE::AttributeProto* p) override { return p->release_s(); }
// GraphProto (wrapped)
std::unique_ptr<ONNX_NAMESPACE::GraphProto> GraphProto__construct() override { return std::make_unique<ONNX_NAMESPACE::GraphProto>(); }