Skip to content

Commit d1d40fb

Browse files
mingyueliuhLiu Minyue
and
Liu Minyue
authored
[VitisAI][Fix] ShapeInferContext GetAttrxxxs support empty value (microsoft#21471)
### Description Bug fix for the ShapeInferContext GetAttrxxxs APIs. Node attribute maybe is empty. ### Motivation and Context If the attr value is empty, the expected result through the interface is empty , but currently, it returns a meaningless {0}. --------- Co-authored-by: mingyue <mingyue@amd.com> Co-authored-by: Liu Minyue <mingyue@xilinx.com>
1 parent c018ba4 commit d1d40fb

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

include/onnxruntime/core/session/onnxruntime_cxx_inline.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2044,13 +2044,19 @@ inline ShapeInferContext::Ints ShapeInferContext::GetAttrInts(const char* attr_n
20442044
int64_t i = {};
20452045
size_t out = {};
20462046
// first call to get the bytes needed
2047+
// 1. A status == nullptr means that ReadOpAttr was successful. A status != nullptr means failure.
2048+
// 2. The ReadOpAttr function should normally be called twice: once to get the needed buffer size (returns a status != nullptr), and a second time to actually read the ints (returns status == null on success).
2049+
// 3. This code tries a subtle optimization in the first call to ReadOpAttr. It passes in a buffer (&i) of size 1 just in case there is only 1 int. In this case, status == nullptr and we need to return {i}.
20472050
auto status = ort_api_->ReadOpAttr(attr, ORT_OP_ATTR_INTS, &i, sizeof(i), &out);
20482051
if (status) {
20492052
size_t num_i = out / sizeof(int64_t);
20502053
ShapeInferContext::Ints ints(num_i, 0);
20512054
Ort::ThrowOnError(ort_api_->ReadOpAttr(attr, ORT_OP_ATTR_INTS, ints.data(), out, &out));
20522055
return ints;
20532056
} else {
2057+
if (out == 0u) {
2058+
return {};
2059+
}
20542060
return {i};
20552061
}
20562062
}
@@ -2068,13 +2074,19 @@ inline ShapeInferContext::Floats ShapeInferContext::GetAttrFloats(const char* at
20682074
float f = {};
20692075
size_t out = {};
20702076
// first call to get the bytes needed
2077+
// 1. A status == nullptr means that ReadOpAttr was successful. A status != nullptr means failure.
2078+
// 2. The ReadOpAttr function should normally be called twice: once to get the needed buffer size (returns a status != nullptr), and a second time to actually read the ints (returns status == null on success).
2079+
// 3. This code tries a subtle optimization in the first call to ReadOpAttr. It passes in a buffer (&i) of size 1 just in case there is only 1 int. In this case, status == nullptr and we need to return {i}.
20712080
auto status = ort_api_->ReadOpAttr(attr, ORT_OP_ATTR_FLOATS, &f, sizeof(f), &out);
20722081
if (status) {
20732082
size_t num_f = out / sizeof(float);
20742083
ShapeInferContext::Floats floats(num_f, 0);
20752084
Ort::ThrowOnError(ort_api_->ReadOpAttr(attr, ORT_OP_ATTR_FLOATS, floats.data(), out, &out));
20762085
return floats;
20772086
} else {
2087+
if (out == 0u) {
2088+
return {};
2089+
}
20782090
return {f};
20792091
}
20802092
}
@@ -2099,6 +2111,9 @@ inline ShapeInferContext::Strings ShapeInferContext::GetAttrStrings(const char*
20992111
char c = {};
21002112
size_t out = {};
21012113
// first call to get the bytes needed
2114+
// 1. A status == nullptr means that ReadOpAttr was successful. A status != nullptr means failure.
2115+
// 2. The ReadOpAttr function should normally be called twice: once to get the needed buffer size (returns a status != nullptr), and a second time to actually read the ints (returns status == null on success).
2116+
// 3. This code tries a subtle optimization in the first call to ReadOpAttr. It passes in a buffer (&i) of size 1 just in case there is only 1 int. In this case, status == nullptr and we need to return {i}.
21022117
auto status = ort_api_->ReadOpAttr(attr, ORT_OP_ATTR_STRINGS, &c, sizeof(char), &out);
21032118
if (status) {
21042119
std::vector<char> chars(out, '\0');
@@ -2115,6 +2130,9 @@ inline ShapeInferContext::Strings ShapeInferContext::GetAttrStrings(const char*
21152130
}
21162131
return strings;
21172132
} else {
2133+
if (out == 0u) {
2134+
return {};
2135+
}
21182136
return {std::string{c}};
21192137
}
21202138
}

0 commit comments

Comments
 (0)