Skip to content

Commit

Permalink
[VitisAI][Fix] ShapeInferContext GetAttrxxxs support empty value (#21471
Browse files Browse the repository at this point in the history
)

### 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 <[email protected]>
Co-authored-by: Liu Minyue <[email protected]>
  • Loading branch information
3 people authored Aug 18, 2024
1 parent c018ba4 commit d1d40fb
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions include/onnxruntime/core/session/onnxruntime_cxx_inline.h
Original file line number Diff line number Diff line change
Expand Up @@ -2044,13 +2044,19 @@ inline ShapeInferContext::Ints ShapeInferContext::GetAttrInts(const char* attr_n
int64_t i = {};
size_t out = {};
// first call to get the bytes needed
// 1. A status == nullptr means that ReadOpAttr was successful. A status != nullptr means failure.
// 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).
// 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}.
auto status = ort_api_->ReadOpAttr(attr, ORT_OP_ATTR_INTS, &i, sizeof(i), &out);
if (status) {
size_t num_i = out / sizeof(int64_t);
ShapeInferContext::Ints ints(num_i, 0);
Ort::ThrowOnError(ort_api_->ReadOpAttr(attr, ORT_OP_ATTR_INTS, ints.data(), out, &out));
return ints;
} else {
if (out == 0u) {
return {};
}
return {i};
}
}
Expand All @@ -2068,13 +2074,19 @@ inline ShapeInferContext::Floats ShapeInferContext::GetAttrFloats(const char* at
float f = {};
size_t out = {};
// first call to get the bytes needed
// 1. A status == nullptr means that ReadOpAttr was successful. A status != nullptr means failure.
// 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).
// 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}.
auto status = ort_api_->ReadOpAttr(attr, ORT_OP_ATTR_FLOATS, &f, sizeof(f), &out);
if (status) {
size_t num_f = out / sizeof(float);
ShapeInferContext::Floats floats(num_f, 0);
Ort::ThrowOnError(ort_api_->ReadOpAttr(attr, ORT_OP_ATTR_FLOATS, floats.data(), out, &out));
return floats;
} else {
if (out == 0u) {
return {};
}
return {f};
}
}
Expand All @@ -2099,6 +2111,9 @@ inline ShapeInferContext::Strings ShapeInferContext::GetAttrStrings(const char*
char c = {};
size_t out = {};
// first call to get the bytes needed
// 1. A status == nullptr means that ReadOpAttr was successful. A status != nullptr means failure.
// 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).
// 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}.
auto status = ort_api_->ReadOpAttr(attr, ORT_OP_ATTR_STRINGS, &c, sizeof(char), &out);
if (status) {
std::vector<char> chars(out, '\0');
Expand All @@ -2115,6 +2130,9 @@ inline ShapeInferContext::Strings ShapeInferContext::GetAttrStrings(const char*
}
return strings;
} else {
if (out == 0u) {
return {};
}
return {std::string{c}};
}
}
Expand Down

0 comments on commit d1d40fb

Please sign in to comment.