Skip to content

Commit 62e7e24

Browse files
danyue333GenMing Zhonggenmingz
authored
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 <[email protected]> Co-authored-by: genmingz <[email protected]>
1 parent 2a36fd4 commit 62e7e24

File tree

7 files changed

+17
-1
lines changed

7 files changed

+17
-1
lines changed

onnxruntime/core/providers/shared_library/provider_interfaces.h

+1
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,7 @@ struct ProviderHost {
390390
virtual void AttributeProto__set_name(ONNX_NAMESPACE::AttributeProto* p, const ::std::string& value) = 0;
391391
virtual void AttributeProto__set_type(ONNX_NAMESPACE::AttributeProto* p, ONNX_NAMESPACE::AttributeProto_AttributeType value) = 0;
392392
virtual ONNX_NAMESPACE::TensorProto* AttributeProto__add_tensors(ONNX_NAMESPACE::AttributeProto* p) = 0;
393+
virtual std::string* AttributeProto__release_s(ONNX_NAMESPACE::AttributeProto* p) = 0;
393394

394395
// GraphProto
395396
virtual std::unique_ptr<ONNX_NAMESPACE::GraphProto> GraphProto__construct() = 0;

onnxruntime/core/providers/shared_library/provider_wrappedtypes.h

+1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ struct AttributeProto final {
122122
void set_name(const ::std::string& value) { return g_host->AttributeProto__set_name(this, value); }
123123
void set_type(AttributeProto_AttributeType value) { return g_host->AttributeProto__set_type(this, value); }
124124
TensorProto* add_tensors() { return g_host->AttributeProto__add_tensors(this); }
125+
std::string* release_s() { return g_host->AttributeProto__release_s(this); }
125126

126127
typedef AttributeProto_AttributeType AttributeType;
127128
static constexpr AttributeType UNDEFINED = AttributeProto_AttributeType_UNDEFINED;

onnxruntime/core/providers/vitisai/imp/attr_proto.cc

+4
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,8 @@ std::vector<std::string> attr_proto_get_strings(const ONNX_NAMESPACE::AttributeP
104104
}
105105
return ret;
106106
}
107+
std::string* attr_proto_release_string(ONNX_NAMESPACE::AttributeProto* attr) {
108+
vai_assert(attr->type() == ONNX_NAMESPACE::AttributeProto_AttributeType_STRING, attr->name());
109+
return attr->release_s();
110+
}
107111
} // namespace vaip

onnxruntime/core/providers/vitisai/imp/attr_proto.h

+1
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@ const ONNX_NAMESPACE::TensorProto& attr_proto_get_tensor(const ONNX_NAMESPACE::A
2323
gsl::span<const int64_t> attr_proto_get_ints(const ONNX_NAMESPACE::AttributeProto& attr);
2424
gsl::span<const float> attr_proto_get_floats(const ONNX_NAMESPACE::AttributeProto& attr);
2525
std::vector<std::string> attr_proto_get_strings(const ONNX_NAMESPACE::AttributeProto& attr);
26+
std::string* attr_proto_release_string(ONNX_NAMESPACE::AttributeProto* attr);
2627

2728
} // namespace vaip

onnxruntime/core/providers/vitisai/imp/global_api.cc

+7
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,13 @@ vaip_core::OrtApiForVaip* create_org_api_hook() {
449449
return vaip_core::DllSafe(model_proto.SerializeAsString());
450450
};
451451
the_global_api.model_proto_delete = [](ONNX_NAMESPACE::ModelProto* p) { delete p; };
452+
the_global_api.attr_proto_release_string = [](ONNX_NAMESPACE::AttributeProto* attr) -> vaip_core::DllSafe<std::string> {
453+
auto pstr = vaip::attr_proto_release_string(attr);
454+
std::string local_str = std::move(*pstr);
455+
pstr = nullptr;
456+
return vaip_core::DllSafe<std::string>(std::move(local_str));
457+
};
458+
452459
if (!s_library_vitisaiep.vaip_get_version) {
453460
return reinterpret_cast<vaip_core::OrtApiForVaip*>(&(the_global_api.host_));
454461
} else {

onnxruntime/core/providers/vitisai/include/vaip/vaip_ort_api.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ struct OrtApi;
1313

1414
namespace vaip_core {
1515

16-
#define VAIP_ORT_API_MAJOR (11u)
16+
#define VAIP_ORT_API_MAJOR (12u)
1717
#define VAIP_ORT_API_MINOR (0u)
1818
#define VAIP_ORT_API_PATCH (0u)
1919
struct OrtApiForVaip {
@@ -234,6 +234,7 @@ struct OrtApiForVaip {
234234
ModelProto* (*model_to_proto)(Model& model); // [95]
235235
DllSafe<std::string> (*model_proto_serialize_as_string)(ModelProto& model_proto); // [96]
236236
void (*model_proto_delete)(ModelProto* p); // [97]
237+
DllSafe<std::string> (*attr_proto_release_string)(AttributeProto* attr); // [98]
237238
};
238239

239240
#ifndef USE_VITISAI

onnxruntime/core/session/provider_bridge_ort.cc

+1
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,7 @@ struct ProviderHostImpl : ProviderHost {
497497
void AttributeProto__set_name(ONNX_NAMESPACE::AttributeProto* p, const ::std::string& value) override { return p->set_name(value); }
498498
void AttributeProto__set_type(ONNX_NAMESPACE::AttributeProto* p, ONNX_NAMESPACE::AttributeProto_AttributeType value) override { return p->set_type(value); }
499499
ONNX_NAMESPACE::TensorProto* AttributeProto__add_tensors(ONNX_NAMESPACE::AttributeProto* p) override { return p->add_tensors(); }
500+
std::string* AttributeProto__release_s(ONNX_NAMESPACE::AttributeProto* p) override { return p->release_s(); }
500501

501502
// GraphProto (wrapped)
502503
std::unique_ptr<ONNX_NAMESPACE::GraphProto> GraphProto__construct() override { return std::make_unique<ONNX_NAMESPACE::GraphProto>(); }

0 commit comments

Comments
 (0)