From b2cddc5337d4e4c058b2f57fe4a65e539c3c491b Mon Sep 17 00:00:00 2001 From: Edward Chen <18449977+edgchen1@users.noreply.github.com> Date: Fri, 12 Feb 2021 13:26:56 -0800 Subject: [PATCH] Consolidate MLTypeCallDispatcher classes (#6651) --- .../core/framework/data_types_internal.h | 206 ++++++++++-------- onnxruntime/contrib_ops/cpu/inverse.cc | 4 +- onnxruntime/contrib_ops/cuda/inverse.cc | 5 +- .../contrib_ops/cuda/math/bias_softmax.cc | 10 +- .../contrib_ops/rocm/math/bias_softmax.cc | 11 +- .../core/framework/tensorprotoutils.cc | 10 +- .../core/optimizer/matmul_scale_fusion.cc | 6 +- .../core/providers/cpu/generator/range.cc | 5 +- onnxruntime/core/providers/cpu/math/clip.cc | 4 +- .../providers/cpu/math/element_wise_ops.cc | 14 +- onnxruntime/core/providers/cpu/math/sign.cc | 4 +- .../core/providers/cpu/ml/normalizer.cc | 4 +- onnxruntime/core/providers/cpu/nn/pool.cc | 4 +- onnxruntime/core/providers/cpu/nn/shrink.cc | 6 +- .../core/providers/cuda/generator/range.cc | 5 +- onnxruntime/core/providers/cuda/math/clip.cc | 4 +- .../cuda/math/variadic_elementwise_ops.cc | 29 +-- onnxruntime/core/providers/cuda/nn/dropout.h | 14 +- .../core/providers/cuda/tensor/gather_nd.cc | 6 +- .../providers/cuda/tensor/scatter_elements.cc | 11 +- onnxruntime/core/session/onnxruntime_c_api.cc | 14 +- .../cpu/cat_imputer_transformer.cc | 4 +- .../cpu/forecasting_pivot_transformer.cc | 13 +- .../cpu/from_string_transformer.cc | 4 +- .../hash_one_hot_vectorizer_transformer.cc | 4 +- .../cpu/imputation_marker_transformer.cc | 4 +- .../cpu/label_encoder_transformer.cc | 4 +- .../cpu/laglead_operator_transformer.cc | 5 +- .../cpu/max_abs_scaler_transformer.cc | 4 +- .../cpu/mean_imputer_transformer.cc | 4 +- .../cpu/median_imputer_transformer.cc | 4 +- .../cpu/min_max_imputer_transformer.cc | 4 +- .../cpu/min_max_scaler_transformer.cc | 4 +- .../cpu/missing_dummies_transformer.cc | 4 +- .../cpu/mode_imputer_transformer.cc | 4 +- .../cpu/normalize_transformer.cc | 4 +- .../cpu/numericalize_transformer.cc | 7 +- .../cpu/one_hot_encoder_transformer.cc | 4 +- .../featurizers_ops/cpu/pca_transformer.cc | 5 +- .../cpu/robust_scaler_transformer.cc | 4 +- .../cpu/rolling_window_transformer.cc | 10 +- .../cpu/short_grain_dropper_transformer.cc | 8 +- .../cpu/standard_scale_wrapper_transformer.cc | 4 +- .../featurizers_ops/cpu/string_transformer.cc | 4 +- .../cpu/time_series_imputer_transformer.cc | 8 +- .../cpu/truncated_svd_transformer.cc | 5 +- .../training_ops/cpu/tensor/gather_nd_grad.cc | 4 +- .../cuda/activation/bias_gelu_grad.cc | 7 +- .../training_ops/cuda/math/scale.cc | 4 +- .../training_ops/cuda/nn/dropout.cc | 17 +- .../cuda/tensor/gather_elements_grad.cc | 10 +- .../cuda/tensor/gather_nd_grad.cc | 6 +- 52 files changed, 277 insertions(+), 276 deletions(-) diff --git a/include/onnxruntime/core/framework/data_types_internal.h b/include/onnxruntime/core/framework/data_types_internal.h index 23e4166e20..8e1649c552 100644 --- a/include/onnxruntime/core/framework/data_types_internal.h +++ b/include/onnxruntime/core/framework/data_types_internal.h @@ -7,6 +7,7 @@ #include #include #include +#include #include #include "boost/mp11.hpp" @@ -16,11 +17,6 @@ #include "core/framework/data_types.h" #include "core/graph/onnx_protobuf.h" -#ifdef _MSC_VER -#pragma warning(push) -//TODO: fix the warning in CallableDispatchableRetHelper -#pragma warning(disable : 4702) -#endif namespace onnxruntime { namespace utils { @@ -223,6 +219,7 @@ inline bool IsPrimitiveDataType(const PrimitiveDataTypeBase* prim_type) { // This implementation contains a workaround for GCC bug https://gcc.gnu.org/bugzilla/show_bug.cgi?id=47226 // GCC until very recently does not support template parameter pack expansion within lambda context. namespace mltype_dispatcher_internal { + // T - type handled by this helper class CallableDispatchableHelper { int32_t dt_type_; // Type currently dispatched @@ -242,7 +239,6 @@ class CallableDispatchableHelper { } void CheckCalledOnce() { - ORT_ENFORCE(called_ < 2, "Check for duplicate types in MLTypeCallDispatcher"); ORT_ENFORCE(called_ == 1, "Unsupported data type: ", dt_type_); } }; @@ -256,7 +252,7 @@ struct UnsupportedTypeDefaultPolicy { }; // Helper with the result type -template > +template class CallableDispatchableRetHelper { int32_t dt_type_; // Type currently dispatched size_t called_; @@ -266,8 +262,6 @@ class CallableDispatchableRetHelper { explicit CallableDispatchableRetHelper(int32_t dt_type) noexcept : dt_type_(dt_type), called_(0), result_() {} Ret Get() { - // See if there were multiple invocations.It is a bug. - ORT_ENFORCE(called_ < 2, "Check for duplicate types in MLTypeCallDispatcherRet"); // No type was invoked if (called_ == 0) { result_ = UnsupportedPolicy()(dt_type_); @@ -286,103 +280,89 @@ class CallableDispatchableRetHelper { } }; +template +using TensorProtoElementTypeConstant = + std::integral_constant()>; + +using UndefinedTensorProtoElementTypeConstant = + std::integral_constant; + } // namespace mltype_dispatcher_internal -// This class helps to efficiently dispatch calls for templated -// kernel implementation functions that has no return value. -// If your implementation function must return a value such as Status -// Use MLTypeCallDispatcherRet class. -// -// The first template parameter is a template struct/class functor -// that must implement operator() with arbitrary number of arguments -// and void return turn. It must return Ret type if you are using MLTypeCallDispatcherRet. -// Fn must be default constructible. -// -// Types is a type list that are supported by this kernel implementation. -// There should be no duplicate types. An exception will be thrown if there -// a duplicate. -// -// The constructor accepts an enum that is obtained from -// input_tensor->DataType()->AsPrimitiveType()->GetDataType(). -// Fn will be called only once the type designated by dt_type value. -// If current dt_type is not handled, the Dispatcher will throw an exception. -// -template