From 7dea6b5949c5ab5f46119de5fd2cbff3116e3466 Mon Sep 17 00:00:00 2001 From: Changming Sun Date: Fri, 11 Feb 2022 20:46:59 -0800 Subject: [PATCH 1/2] Return a Status instead of throw an exception in GetAttrs --- .../core/framework/op_node_proto_helper.cc | 60 ++++++++++--------- 1 file changed, 32 insertions(+), 28 deletions(-) diff --git a/onnxruntime/core/framework/op_node_proto_helper.cc b/onnxruntime/core/framework/op_node_proto_helper.cc index 960c9971244aa..cbe2e66c070b9 100644 --- a/onnxruntime/core/framework/op_node_proto_helper.cc +++ b/onnxruntime/core/framework/op_node_proto_helper.cc @@ -90,34 +90,38 @@ inline constexpr int ArrayTypeToAttributeType() { } \ } -#define ORT_DEFINE_GET_ATTRS(IMPL_T, T, list) \ - template <> \ - template <> \ - Status OpNodeProtoHelper::GetAttrs( \ - const std::string& name, std::vector& values) const { \ - const AttributeProto* attr = TryGetAttribute(name); \ - if (!attr) { \ - return Status(ONNXRUNTIME, FAIL, "No attribute with this name is defined."); \ - } \ - values.reserve(attr->list##_size()); \ - for (int i = 0; i < attr->list##_size(); ++i) { \ - values.push_back(static_cast(attr->list(i))); \ - } \ - return Status::OK(); \ - } \ - template <> \ - template <> \ - Status OpNodeProtoHelper::GetAttrs( \ - const std::string& name, gsl::span values) const { \ - const AttributeProto* attr = TryGetAttribute(name); \ - if (!attr) { \ - return Status(ONNXRUNTIME, FAIL, "No attribute with this name is defined."); \ - } \ - ORT_ENFORCE(values.size() == static_cast(attr->list##_size())); \ - for (int i = 0; i < attr->list##_size(); ++i) { \ - values[i] = static_cast(attr->list(i)); \ - } \ - return Status::OK(); \ +#define ORT_DEFINE_GET_ATTRS(IMPL_T, T, list) \ + template <> \ + template <> \ + Status OpNodeProtoHelper::GetAttrs( \ + const std::string& name, std::vector& values) const { \ + const AttributeProto* attr = TryGetAttribute(name); \ + if (!attr) { \ + return Status(ONNXRUNTIME, FAIL, "No attribute with this name is defined."); \ + } \ + values.reserve(attr->list##_size()); \ + for (int i = 0; i < attr->list##_size(); ++i) { \ + values.push_back(static_cast(attr->list(i))); \ + } \ + return Status::OK(); \ + } \ + template <> \ + template <> \ + Status OpNodeProtoHelper::GetAttrs( \ + const std::string& name, gsl::span values) const { \ + const AttributeProto* attr = TryGetAttribute(name); \ + if (!attr) { \ + return Status(ONNXRUNTIME, FAIL, "No attribute with this name is defined."); \ + } \ + if (values.size() != static_cast(attr->list##_size())) { \ + std::ostringstream oss; \ + oss << "GetAttrs failed. Expect values.size()=" << (attr->list##_size()) << ", got " << values.size(); \ + return Status(ONNXRUNTIME, FAIL, oss.str()); \ + } \ + for (int i = 0; i < attr->list##_size(); ++i) { \ + values[i] = static_cast(attr->list(i)); \ + } \ + return Status::OK(); \ } // Will not work for std::strings From c6b1fd3aee469de4407dd3a1194b064597f2e862 Mon Sep 17 00:00:00 2001 From: Changming Sun Date: Mon, 14 Feb 2022 09:12:10 -0800 Subject: [PATCH 2/2] update --- onnxruntime/core/framework/op_node_proto_helper.cc | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/onnxruntime/core/framework/op_node_proto_helper.cc b/onnxruntime/core/framework/op_node_proto_helper.cc index cbe2e66c070b9..478a80e20ca5c 100644 --- a/onnxruntime/core/framework/op_node_proto_helper.cc +++ b/onnxruntime/core/framework/op_node_proto_helper.cc @@ -113,11 +113,8 @@ inline constexpr int ArrayTypeToAttributeType() { if (!attr) { \ return Status(ONNXRUNTIME, FAIL, "No attribute with this name is defined."); \ } \ - if (values.size() != static_cast(attr->list##_size())) { \ - std::ostringstream oss; \ - oss << "GetAttrs failed. Expect values.size()=" << (attr->list##_size()) << ", got " << values.size(); \ - return Status(ONNXRUNTIME, FAIL, oss.str()); \ - } \ + ORT_RETURN_IF(values.size() != static_cast(attr->list##_size()), \ + "GetAttrs failed. Expect values.size()=" , (attr->list##_size()) , ", got " , values.size()); \ for (int i = 0; i < attr->list##_size(); ++i) { \ values[i] = static_cast(attr->list(i)); \ } \