From 442c7300eb30d57ded968480bdcaf55a6d853f72 Mon Sep 17 00:00:00 2001 From: Ashwini Khade Date: Fri, 14 May 2021 05:52:54 -0700 Subject: [PATCH] add opset14 rnn ops (#7687) * add opset14 rnn ops * update kernel hashes --- .../InferenceTest.cs | 15 +++------------ .../providers/cpu/cpu_execution_provider.cc | 18 ++++++++++++------ .../core/providers/cpu/rnn/deep_cpu_gru.cc | 11 ++++++++++- .../core/providers/cpu/rnn/deep_cpu_gru.h | 5 +++++ .../core/providers/cpu/rnn/deep_cpu_lstm.cc | 9 ++++++++- onnxruntime/core/providers/cpu/rnn/lstm_base.h | 7 ++++++- onnxruntime/core/providers/cpu/rnn/rnn.cc | 11 ++++++++++- onnxruntime/core/providers/cpu/rnn/rnn.h | 9 ++++++++- .../testdata/kernel_def_hashes/onnx.cpu.json | 12 ++++++++++++ .../onnx_backend_test_series_filters.jsonc | 9 --------- 10 files changed, 74 insertions(+), 32 deletions(-) diff --git a/csharp/test/Microsoft.ML.OnnxRuntime.Tests/InferenceTest.cs b/csharp/test/Microsoft.ML.OnnxRuntime.Tests/InferenceTest.cs index 5bb6759ab8..edf2a299dd 100644 --- a/csharp/test/Microsoft.ML.OnnxRuntime.Tests/InferenceTest.cs +++ b/csharp/test/Microsoft.ML.OnnxRuntime.Tests/InferenceTest.cs @@ -825,18 +825,9 @@ namespace Microsoft.ML.OnnxRuntime.Tests { "test_min_int8", "node test error"}, { "test_cast_FLOAT_to_STRING", "node test error"}, { "test_identity_sequence", "data type not supported"}, - { "test_gru_batchwise", "opset14 version not implemented yet"}, - { "test_gru_defaults", "opset14 version not implemented yet"}, - { "test_gru_seq_length", "opset14 version not implemented yet"}, - { "test_gru_with_initial_bias", "opset14 version not implemented yet"}, - { "test_lstm_batchwise", "opset14 version not implemented yet"}, - { "test_lstm_defaults", "opset14 version not implemented yet"}, - { "test_lstm_with_initial_bias", "opset14 version not implemented yet"}, - { "test_lstm_with_peepholes", "opset14 version not implemented yet"}, - { "test_rnn_seq_length", "opset14 version not implemented yet"}, - { "test_simple_rnn_batchwise", "opset14 version not implemented yet"}, - { "test_simple_rnn_defaults", "opset14 version not implemented yet"}, - { "test_simple_rnn_with_initial_bias", "opset14 version not implemented yet"}, + { "test_gru_batchwise", "batchwise operations not supported"}, + { "test_lstm_batchwise", "batchwise operations not supported"}, + { "test_simple_rnn_batchwise", "batchwise operations not supported"}, { "test_sub_uint8", "data type not supported"}, { "test_mul_uint8", "data type not supported"}, { "test_add_uint8", "data type not supported"}, diff --git a/onnxruntime/core/providers/cpu/cpu_execution_provider.cc b/onnxruntime/core/providers/cpu/cpu_execution_provider.cc index 910bdc117d..11ec495b17 100644 --- a/onnxruntime/core/providers/cpu/cpu_execution_provider.cc +++ b/onnxruntime/core/providers/cpu/cpu_execution_provider.cc @@ -189,9 +189,9 @@ class ONNX_OPERATOR_VERSIONED_TYPED_KERNEL_CLASS_NAME(kCpuExecutionProvider, kOn class ONNX_OPERATOR_VERSIONED_TYPED_KERNEL_CLASS_NAME(kCpuExecutionProvider, kOnnxDomain, 1, 10, int32_t, ArgMax); class ONNX_OPERATOR_VERSIONED_TYPED_KERNEL_CLASS_NAME(kCpuExecutionProvider, kOnnxDomain, 1, 10, float, ArgMin); class ONNX_OPERATOR_VERSIONED_TYPED_KERNEL_CLASS_NAME(kCpuExecutionProvider, kOnnxDomain, 1, 10, int32_t, ArgMin); -class ONNX_OPERATOR_KERNEL_CLASS_NAME(kCpuExecutionProvider, kOnnxDomain, 7, GRU); -class ONNX_OPERATOR_KERNEL_CLASS_NAME(kCpuExecutionProvider, kOnnxDomain, 7, LSTM); -class ONNX_OPERATOR_KERNEL_CLASS_NAME(kCpuExecutionProvider, kOnnxDomain, 7, RNN); +class ONNX_OPERATOR_VERSIONED_KERNEL_CLASS_NAME(kCpuExecutionProvider, kOnnxDomain, 7, 13, GRU); +class ONNX_OPERATOR_VERSIONED_KERNEL_CLASS_NAME(kCpuExecutionProvider, kOnnxDomain, 7, 13, LSTM); +class ONNX_OPERATOR_VERSIONED_KERNEL_CLASS_NAME(kCpuExecutionProvider, kOnnxDomain, 7, 13, RNN); class ONNX_OPERATOR_VERSIONED_KERNEL_CLASS_NAME(kCpuExecutionProvider, kOnnxDomain, 6, 12, Cast); class ONNX_OPERATOR_VERSIONED_KERNEL_CLASS_NAME(kCpuExecutionProvider, kOnnxDomain, 4, 10, Concat); class ONNX_OPERATOR_VERSIONED_KERNEL_CLASS_NAME(kCpuExecutionProvider, kOnnxDomain, 1, 10, Gather); @@ -686,6 +686,9 @@ class ONNX_OPERATOR_KERNEL_CLASS_NAME(kCpuExecutionProvider, kOnnxDomain, 14, Re class ONNX_OPERATOR_KERNEL_CLASS_NAME(kCpuExecutionProvider, kOnnxDomain, 14, Identity); class ONNX_OPERATOR_TYPED_KERNEL_CLASS_NAME(kCpuExecutionProvider, kOnnxDomain, 14, float, BatchNormalization); class ONNX_OPERATOR_TYPED_KERNEL_CLASS_NAME(kCpuExecutionProvider, kOnnxDomain, 14, double, BatchNormalization); +class ONNX_OPERATOR_KERNEL_CLASS_NAME(kCpuExecutionProvider, kOnnxDomain, 14, GRU); +class ONNX_OPERATOR_KERNEL_CLASS_NAME(kCpuExecutionProvider, kOnnxDomain, 14, LSTM); +class ONNX_OPERATOR_KERNEL_CLASS_NAME(kCpuExecutionProvider, kOnnxDomain, 14, RNN); // !!PLEASE READ BELOW!! Following that, add new entries above this comment @@ -976,9 +979,9 @@ Status RegisterOnnxOperatorKernels(KernelRegistry& kernel_registry) { float, ArgMin)>, BuildKernelCreateInfo, - BuildKernelCreateInfo, - BuildKernelCreateInfo, - BuildKernelCreateInfo, + BuildKernelCreateInfo, + BuildKernelCreateInfo, + BuildKernelCreateInfo, BuildKernelCreateInfo, BuildKernelCreateInfo, @@ -1805,6 +1808,9 @@ Status RegisterOnnxOperatorKernels(KernelRegistry& kernel_registry) { BatchNormalization)>, BuildKernelCreateInfo, + BuildKernelCreateInfo, + BuildKernelCreateInfo, + BuildKernelCreateInfo, }; for (auto& function_table_entry : function_table) { diff --git a/onnxruntime/core/providers/cpu/rnn/deep_cpu_gru.cc b/onnxruntime/core/providers/cpu/rnn/deep_cpu_gru.cc index 59e788a4cb..fce8c8a172 100644 --- a/onnxruntime/core/providers/cpu/rnn/deep_cpu_gru.cc +++ b/onnxruntime/core/providers/cpu/rnn/deep_cpu_gru.cc @@ -138,9 +138,18 @@ Equations (Default: f=Sigmoid, g=Tanh): namespace onnxruntime { -ONNX_CPU_OPERATOR_KERNEL( +ONNX_CPU_OPERATOR_VERSIONED_KERNEL( GRU, 7, + 13, + KernelDefBuilder().TypeConstraint("T", {DataTypeImpl::GetTensorType(), + DataTypeImpl::GetTensorType()}) + .TypeConstraint("T1", DataTypeImpl::GetTensorType()), + DeepCpuGruOp); + +ONNX_CPU_OPERATOR_KERNEL( + GRU, + 14, KernelDefBuilder().TypeConstraint("T", {DataTypeImpl::GetTensorType(), DataTypeImpl::GetTensorType()}) .TypeConstraint("T1", DataTypeImpl::GetTensorType()), diff --git a/onnxruntime/core/providers/cpu/rnn/deep_cpu_gru.h b/onnxruntime/core/providers/cpu/rnn/deep_cpu_gru.h index 0a34697b30..885675f3b7 100644 --- a/onnxruntime/core/providers/cpu/rnn/deep_cpu_gru.h +++ b/onnxruntime/core/providers/cpu/rnn/deep_cpu_gru.h @@ -49,6 +49,10 @@ class DeepCpuGruOp final : public OpKernel { activation_funcs_ = rnn::detail::ActivationFuncs(activation_func_names, activation_func_alphas, activation_func_betas); + + layout_ = info.GetAttrOrDefault("layout", static_cast(0)); + ORT_ENFORCE(layout_ == 0, + "Batchwise recurrent operations (layout == 1) are not supported. If you need support create a github issue with justification."); } Status Compute(OpKernelContext* context) const override; @@ -62,6 +66,7 @@ class DeepCpuGruOp final : public OpKernel { int hidden_size_ {}; float clip_; int linear_before_reset_ {}; + int64_t layout_; rnn::detail::ActivationFuncs activation_funcs_; diff --git a/onnxruntime/core/providers/cpu/rnn/deep_cpu_lstm.cc b/onnxruntime/core/providers/cpu/rnn/deep_cpu_lstm.cc index b0bc166032..cb11858536 100644 --- a/onnxruntime/core/providers/cpu/rnn/deep_cpu_lstm.cc +++ b/onnxruntime/core/providers/cpu/rnn/deep_cpu_lstm.cc @@ -156,7 +156,14 @@ Equations (Default: f=Sigmoid, g=Tanh, h=Tanh): namespace onnxruntime { /* LSTM operator */ -ONNX_CPU_OPERATOR_KERNEL(LSTM, 7, +ONNX_CPU_OPERATOR_VERSIONED_KERNEL(LSTM, 7, 13, + KernelDefBuilder() + .TypeConstraint("T", {DataTypeImpl::GetTensorType(), + DataTypeImpl::GetTensorType()}) + .TypeConstraint("T1", DataTypeImpl::GetTensorType()), + DeepCpuLstmOp); + +ONNX_CPU_OPERATOR_KERNEL(LSTM, 14, KernelDefBuilder() .TypeConstraint("T", {DataTypeImpl::GetTensorType(), DataTypeImpl::GetTensorType()}) diff --git a/onnxruntime/core/providers/cpu/rnn/lstm_base.h b/onnxruntime/core/providers/cpu/rnn/lstm_base.h index 61d2197aa0..2d9061fb1e 100644 --- a/onnxruntime/core/providers/cpu/rnn/lstm_base.h +++ b/onnxruntime/core/providers/cpu/rnn/lstm_base.h @@ -13,7 +13,8 @@ namespace onnxruntime { class LSTMBase { protected: LSTMBase(const OpKernelInfo& info) - : clip_(info.GetAttrOrDefault("clip", std::numeric_limits::max())) { + : clip_(info.GetAttrOrDefault("clip", std::numeric_limits::max())), + layout_(info.GetAttrOrDefault("layout", static_cast(0))) { std::string direction; ORT_ENFORCE(info.GetAttr("direction", &direction).IsOK()); @@ -46,6 +47,9 @@ class LSTMBase { activation_funcs_ = rnn::detail::ActivationFuncs(activation_func_names, activation_func_alphas, activation_func_betas); + + ORT_ENFORCE(layout_ == 0, + "Batchwise recurrent operations (layout == 1) are not supported. If you need support create a github issue with justification."); } ~LSTMBase() = default; @@ -70,6 +74,7 @@ class LSTMBase { int hidden_size_ = 0; float clip_; bool input_forget_ = false; + int64_t layout_; rnn::detail::ActivationFuncs activation_funcs_; }; diff --git a/onnxruntime/core/providers/cpu/rnn/rnn.cc b/onnxruntime/core/providers/cpu/rnn/rnn.cc index c6fa145090..1f0e0610da 100644 --- a/onnxruntime/core/providers/cpu/rnn/rnn.cc +++ b/onnxruntime/core/providers/cpu/rnn/rnn.cc @@ -11,9 +11,18 @@ #include "core/util/math_cpuonly.h" namespace onnxruntime { -ONNX_CPU_OPERATOR_KERNEL( +ONNX_CPU_OPERATOR_VERSIONED_KERNEL( RNN, 7, + 13, + KernelDefBuilder() + .TypeConstraint("T", DataTypeImpl::GetTensorType()) + .TypeConstraint("T1", DataTypeImpl::GetTensorType()), + RNN); + +ONNX_CPU_OPERATOR_KERNEL( + RNN, + 14, KernelDefBuilder() .TypeConstraint("T", DataTypeImpl::GetTensorType()) .TypeConstraint("T1", DataTypeImpl::GetTensorType()), diff --git a/onnxruntime/core/providers/cpu/rnn/rnn.h b/onnxruntime/core/providers/cpu/rnn/rnn.h index 3dfd708c40..725282bd13 100644 --- a/onnxruntime/core/providers/cpu/rnn/rnn.h +++ b/onnxruntime/core/providers/cpu/rnn/rnn.h @@ -17,7 +17,8 @@ class RNN : public OpKernel { public: RNN(const OpKernelInfo& info) : OpKernel(info), - clip_(info.GetAttrOrDefault("clip", -1.0f)) { + clip_(info.GetAttrOrDefault("clip", -1.0f)), + layout_(info.GetAttrOrDefault("layout", static_cast(0))) { ORT_ENFORCE(info.GetAttr("direction", &direction_).IsOK()); ORT_ENFORCE(allowed_directions.find(direction_) != allowed_directions.end()); const int num_directions = direction_ == "bidirectional" ? 2 : 1; @@ -39,6 +40,9 @@ class RNN : public OpKernel { ORT_ENFORCE(allowed_activations.find(activations_[direction]) != allowed_activations.end(), "RNN op: Invalid activation attribute - ", activations_[direction]); } + + ORT_ENFORCE(layout_ == 0, + "Batchwise recurrent operations (layout == 1) are not supported. If you need support create a github issue with justification."); } Status Compute(OpKernelContext* context) const override; @@ -63,6 +67,9 @@ class RNN : public OpKernel { int64_t hidden_size_; // const std::string default_activation = "Tanh"; + + // added since opset 14. Default value 0 matches the behavior prior to opset14 + int64_t layout_; }; } // namespace onnxruntime diff --git a/onnxruntime/test/testdata/kernel_def_hashes/onnx.cpu.json b/onnxruntime/test/testdata/kernel_def_hashes/onnx.cpu.json index 077dc7ca9e..2aa13288f8 100644 --- a/onnxruntime/test/testdata/kernel_def_hashes/onnx.cpu.json +++ b/onnxruntime/test/testdata/kernel_def_hashes/onnx.cpu.json @@ -879,6 +879,10 @@ "GreaterOrEqual ai.onnx CPUExecutionProvider", 17416867432093505280 ], + [ + "GRU ai.onnx CPUExecutionProvider", + 5656895183302764352 + ], [ "GRU ai.onnx CPUExecutionProvider", 2706165712066264784 @@ -1083,6 +1087,10 @@ "LRN ai.onnx CPUExecutionProvider", 15047083116516536152 ], + [ + "LSTM ai.onnx CPUExecutionProvider", + 4392316607181063320 + ], [ "LSTM ai.onnx CPUExecutionProvider", 5860801277476873352 @@ -2047,6 +2055,10 @@ "ReverseSequence ai.onnx CPUExecutionProvider", 777686786711298232 ], + [ + "RNN ai.onnx CPUExecutionProvider", + 1524012948315159160 + ], [ "RNN ai.onnx CPUExecutionProvider", 7150196822928564664 diff --git a/onnxruntime/test/testdata/onnx_backend_test_series_filters.jsonc b/onnxruntime/test/testdata/onnx_backend_test_series_filters.jsonc index a172e24b8c..eb77ffce72 100644 --- a/onnxruntime/test/testdata/onnx_backend_test_series_filters.jsonc +++ b/onnxruntime/test/testdata/onnx_backend_test_series_filters.jsonc @@ -54,18 +54,9 @@ "^test_resize_upsample_sizes_nearest_round_prefer_ceil_asymmetric_cpu", // Following tests are for opset 14 ops and are not yet implemented in ORT "^test_gru_batchwise_cpu", - "^test_gru_defaults_cpu", - "^test_gru_seq_length_cpu", - "^test_gru_with_initial_bias_cpu", "^test_identity_sequence_cpu", "^test_lstm_batchwise_cpu", - "^test_lstm_defaults_cpu", - "^test_lstm_with_initial_bias_cpu", - "^test_lstm_with_peepholes_cpu", - "^test_rnn_seq_length_cpu", "^test_simple_rnn_batchwise_cpu", - "^test_simple_rnn_defaults_cpu", - "^test_simple_rnn_with_initial_bias_cpu", "^test_sub_uint8_cpu", "^test_mul_uint8_cpu", "^test_add_uint8_cpu",