Skip to content
Merged
4 changes: 2 additions & 2 deletions include/onnxruntime/core/providers/utils/ort_graph_to_proto.h
Original file line number Diff line number Diff line change
Expand Up @@ -665,11 +665,11 @@ static Ort::Status OrtOpAttrToProto(const OrtOpAttr& ort_attr, onnx::AttributePr
Ort::Status status{ort_api.ReadOpAttr(&ort_attr, attr_type, nullptr, 0, &total_attr_bytes)};
std::string* str = attr_proto.mutable_s();

str->resize(total_attr_bytes, '\0');
str->resize(total_attr_bytes);
ORT_EP_UTILS_C_RETURN_IF_ERROR(ort_api.ReadOpAttr(&ort_attr, attr_type, str->data(), total_attr_bytes,
&total_attr_bytes));

str->resize(total_attr_bytes - 1); // remove extra ending terminating '\0' character.
str->resize(total_attr_bytes);
break;
}
case OrtOpAttrType::ORT_OP_ATTR_STRINGS: {
Expand Down
3 changes: 2 additions & 1 deletion include/onnxruntime/core/session/onnxruntime_c_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -3667,7 +3667,8 @@ struct OrtApi {
*
* \param[in] name Name of the attribute
* \param[in] data Data content of the attribute
* \param[in] len Number of elements if data represents an array (e.g., ORT_OP_ATTR_INTS). Otherwise, set to 1.
* \param[in] len Number of bytes stored in data for ORT_OP_ATTR_STRING.
Number of elements if data represents an array (e.g., ORT_OP_ATTR_INTS). Otherwise, set to 1.
* \param[in] type Data type
* \param[out] op_attr Attribute that has been created, which must be released by OrtApi::ReleaseOpAttr
*
Expand Down
2 changes: 1 addition & 1 deletion include/onnxruntime/core/session/onnxruntime_cxx_inline.h
Original file line number Diff line number Diff line change
Expand Up @@ -2627,7 +2627,7 @@ inline std::string ShapeInferContext::GetAttrString(const char* attr_name) {
if (status) {
std::vector<char> chars(out, '\0');
Ort::ThrowOnError(ort_api_->ReadOpAttr(attr, ORT_OP_ATTR_STRING, chars.data(), out, &out));
return {chars.data()};
return std::string{chars.data(), out};
} else {
return {c};
}
Expand Down
9 changes: 3 additions & 6 deletions onnxruntime/core/session/custom_ops.cc
Original file line number Diff line number Diff line change
Expand Up @@ -436,17 +436,14 @@ ORT_API_STATUS_IMPL(OrtApis::ReadOpAttr, _In_ const OrtOpAttr* op_attr, _In_ Ort
}
case OrtOpAttrType::ORT_OP_ATTR_STRING: {
const auto& s = attr->s();
if (len < s.size() + 1) {
if (len < s.size()) {
ret = OrtApis::CreateStatus(OrtErrorCode::ORT_INVALID_ARGUMENT,
"Size of data not large enough to hold the string.");
} else {
char* output_c = reinterpret_cast<char*>(data);
for (char c : s) {
*output_c++ = c;
}
*output_c = '\0';
memcpy(output_c, s.data(), s.size());
}
*out = s.size() + 1;
*out = s.size();
break;
}
case OrtOpAttrType::ORT_OP_ATTR_STRINGS: {
Expand Down
2 changes: 1 addition & 1 deletion onnxruntime/core/session/standalone_op_invoker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ onnxruntime::Status CreateOpAttr(const char* name, const void* data, int len, Or
attr->set_type(ONNX_NAMESPACE::AttributeProto_AttributeType::AttributeProto_AttributeType_FLOATS);
break;
case OrtOpAttrType::ORT_OP_ATTR_STRING:
attr->set_s(std::string{str});
attr->set_s(std::string{str, static_cast<size_t>(len)});
attr->set_type(ONNX_NAMESPACE::AttributeProto_AttributeType::AttributeProto_AttributeType_STRING);
break;
case OrtOpAttrType::ORT_OP_ATTR_STRINGS:
Expand Down
10 changes: 7 additions & 3 deletions onnxruntime/test/autoep/library/ep.cc
Original file line number Diff line number Diff line change
Expand Up @@ -419,12 +419,16 @@ OrtStatus* ExampleEp::CreateEpContextNodes(gsl::span<const OrtNode*> fused_nodes
std::array<OrtOpAttr*, 6> attributes = {};
DeferOrtRelease<OrtOpAttr> defer_release_attrs(attributes.data(), attributes.size(), ort_api.ReleaseOpAttr);

RETURN_IF_ERROR(ort_api.CreateOpAttr("ep_cache_context", "binary_data", 1, ORT_OP_ATTR_STRING, &attributes[0]));
std::string ep_ctx = "binary_data";
RETURN_IF_ERROR(ort_api.CreateOpAttr("ep_cache_context", ep_ctx.c_str(), static_cast<int>(ep_ctx.length()),
ORT_OP_ATTR_STRING, &attributes[0]));
RETURN_IF_ERROR(ort_api.CreateOpAttr("main_context", &is_main_context, 1, ORT_OP_ATTR_INT, &attributes[1]));
RETURN_IF_ERROR(ort_api.CreateOpAttr("embed_mode", &embed_mode, 1, ORT_OP_ATTR_INT, &attributes[2]));
RETURN_IF_ERROR(ort_api.CreateOpAttr("ep_sdk_version", "1", 1, ORT_OP_ATTR_STRING, &attributes[3]));
RETURN_IF_ERROR(ort_api.CreateOpAttr("partition_name", fused_node_name, 1, ORT_OP_ATTR_STRING, &attributes[4]));
RETURN_IF_ERROR(ort_api.CreateOpAttr("source", this->name_.c_str(), 1, ORT_OP_ATTR_STRING, &attributes[5]));
RETURN_IF_ERROR(ort_api.CreateOpAttr("partition_name", fused_node_name, static_cast<int>(strlen(fused_node_name)),
ORT_OP_ATTR_STRING, &attributes[4]));
RETURN_IF_ERROR(ort_api.CreateOpAttr("source", this->name_.c_str(), static_cast<int>(this->name_.length()),
ORT_OP_ATTR_STRING, &attributes[5]));

RETURN_IF_ERROR(model_editor_api.CreateNode("EPContext", "com.microsoft", fused_node_name,
input_names.data(), input_names.size(),
Expand Down
5 changes: 3 additions & 2 deletions onnxruntime/test/shared_lib/custom_op_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -453,8 +453,9 @@ void StandaloneCustomKernel::InitGru() {
float betas[1] = {2.f};
Ort::OpAttr activation_beta = Ort::OpAttr("activation_beta ", betas, 1, OrtOpAttrType::ORT_OP_ATTR_FLOATS);

const char* direction_string = "bidirectional";
Ort::OpAttr direction = Ort::OpAttr("direction", direction_string, 1, OrtOpAttrType::ORT_OP_ATTR_STRING);
const std::string direction_string = "bidirectional";
Ort::OpAttr direction = Ort::OpAttr("direction", direction_string.c_str(), static_cast<int>(direction_string.length()),
OrtOpAttrType::ORT_OP_ATTR_STRING);

int64_t linear_before_reset_value = 0;
Ort::OpAttr linear_before_reset = Ort::OpAttr("linear_before_reset", &linear_before_reset_value, 1,
Expand Down
Loading