Skip to content

Commit

Permalink
Add attrProto.release_s interface (#22977)
Browse files Browse the repository at this point in the history
### 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]>
  • Loading branch information
3 people authored Dec 13, 2024
1 parent 2a36fd4 commit 62e7e24
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions onnxruntime/core/providers/vitisai/imp/attr_proto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions onnxruntime/core/providers/vitisai/imp/attr_proto.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 7 additions & 0 deletions onnxruntime/core/providers/vitisai/imp/global_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions onnxruntime/core/session/provider_bridge_ort.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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>(); }
Expand Down

0 comments on commit 62e7e24

Please sign in to comment.