Follow up on proto3 compatiblity. (#1799)

This provides additional has_*() methods abstraction/replacement for proto3 compatibility.
This commit is contained in:
Dmitri Smirnov 2019-09-11 11:36:13 -07:00 committed by GitHub
parent 3b7f047a49
commit a9e4de2cea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -8,30 +8,56 @@
#include "core/graph/op.h"
#include "onnx/defs/schema.h"
#include "core/framework/op_kernel.h"
#include "core/framework/tensorprotoutils.h"
#include "onnx/defs/schema.h"
#include "gsl/span"
using namespace ONNX_NAMESPACE;
using namespace ::onnxruntime::common;
namespace onnxruntime {
#define ORT_DEFINE_GET_ATTR(IMPL_T, T, type) \
template <> \
template <> \
Status OpNodeProtoHelper<IMPL_T>::GetAttr<T>( \
const std::string& name, T* value) const { \
const AttributeProto* attr = TryGetAttribute(name); \
if (!attr) { \
return ORT_MAKE_STATUS(ONNXRUNTIME, FAIL, "No attribute with name:'", name, "'is defined."); \
} \
if (!attr->has_##type()) { \
return Status(ONNXRUNTIME, FAIL, "Attibute name and type don't match"); \
} else { \
*value = static_cast<T>(attr->type()); \
return Status::OK(); \
} \
template <class T>
bool HasTyped(const AttributeProto*);
template <>
inline bool HasTyped<float>(const AttributeProto* attr) {
return utils::HasFloat(*attr);
}
template <>
inline bool HasTyped<int64_t>(const AttributeProto* attr) {
return utils::HasInt(*attr);
}
template <>
inline bool HasTyped<std::string>(const AttributeProto* attr) {
return utils::HasString(*attr);
}
template <>
inline bool HasTyped<TensorProto>(const AttributeProto* attr) {
return utils::HasTensor(*attr);
}
template <>
inline bool HasTyped<GraphProto>(const AttributeProto* attr) {
return utils::HasGraph(*attr);
}
#define ORT_DEFINE_GET_ATTR(IMPL_T, T, type) \
template <> \
template <> \
Status OpNodeProtoHelper<IMPL_T>::GetAttr<T>( \
const std::string& name, T* value) const { \
const AttributeProto* attr = TryGetAttribute(name); \
if (!attr) { \
return ORT_MAKE_STATUS(ONNXRUNTIME, FAIL, "No attribute with name:'", name, "'is defined."); \
} \
if (!HasTyped<T>(attr)) { \
return Status(ONNXRUNTIME, FAIL, "Attibute name and type don't match"); \
} else { \
*value = static_cast<T>(attr->type()); \
return Status::OK(); \
} \
}
#define ORT_DEFINE_GET_ATTRS(IMPL_T, T, list) \
#define ORT_DEFINE_GET_ATTRS(IMPL_T, T, list) \
template <> \
template <> \
Status OpNodeProtoHelper<IMPL_T>::GetAttrs<T>( \
@ -54,7 +80,7 @@ namespace onnxruntime {
if (!attr) { \
return Status(ONNXRUNTIME, FAIL, "No attribute with this name is defined."); \
} \
ORT_ENFORCE(values.size() == attr->list##_size()); \
ORT_ENFORCE(values.size() == attr->list##_size()); \
for (int i = 0; i < attr->list##_size(); ++i) { \
values[i] = static_cast<T>(attr->list(i)); \
} \