From f938a6e53a93d2071aaa30a06b4fea917214eee8 Mon Sep 17 00:00:00 2001 From: Hector Li Date: Mon, 22 Jul 2019 11:42:57 -0700 Subject: [PATCH] Add test for LSTM/GRU which has shorter sequence in the middle (#1437) Add test for LSTM/GRU which has shorter sequence in the middle --- .../providers/cpu/rnn/deep_cpu_gru_op_test.cc | 33 +++++++++++ .../cpu/rnn/deep_cpu_lstm_op_test.cc | 56 +++++++++++++++++-- 2 files changed, 84 insertions(+), 5 deletions(-) diff --git a/onnxruntime/test/providers/cpu/rnn/deep_cpu_gru_op_test.cc b/onnxruntime/test/providers/cpu/rnn/deep_cpu_gru_op_test.cc index 3314a8e627..fe9cf9a389 100644 --- a/onnxruntime/test/providers/cpu/rnn/deep_cpu_gru_op_test.cc +++ b/onnxruntime/test/providers/cpu/rnn/deep_cpu_gru_op_test.cc @@ -790,6 +790,39 @@ TEST(GRUTest, ONNXRuntime_TestGRUOpSequenceLengthWithBidirectionalLinearBeforeRe ctx.RunTest(X, batch_size, seq_length, sequence_length, &initial_h, expected_Y, expected_Y_h, true); } +TEST(GRUTest, ONNXRuntime_TestGRUOpShorterSeqInMiddle) { + const std::string direction = "bidirectional"; + const std::vector activations = {"sigmoid", "tanh", "sigmoid", "tanh"}; + + DeepCpuGruOpTestContext ctx(direction, activations); + + const int batch_size = 3; + const int seq_length = 2; + std::vector X = {-0.455351f, -0.276391f, + 0.855351f, 0.676391f, + -0.185934f, -0.269585f, + -0.585934f, 0.669585f, + -0.351455f, -0.391276f, + 0.670351f, 0.894676f}; + std::vector sequence_length = {2, 1, 2}; + std::vector initial_h = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f}; + std::vector expected_Y = {-0.0325528607f, 0.0774837881f, -0.275918573f, -0.00228558504f, -0.0456649921f, 0.0462125241f, + -0.108452908f, 0.15118938684f, -0.2759185731f, -0.0022855850f, -0.1950065642f, 0.0961040258f, + + -0.1671274304f, 0.1817691028f, 0.0f, 0.0f, -0.3073617219f, 0.0686715841f, + -0.1494070887f, 0.1356348693f, 0.0f, 0.0f, -0.2866500020f, 0.0448506586f}; + std::vector expected_Y_h = {-0.1671274304f, 0.18176910281f, + -0.2759185731f, -0.00228558504f, + -0.3073617219f, 0.0686715841f, + + -0.1084529086f, 0.15118938684f, + -0.2759185731f, -0.00228558504f, + -0.1950065642f, 0.0961040258f}; + + ctx.RunTest(X, batch_size, seq_length, sequence_length, &initial_h, expected_Y, expected_Y_h, true); +} + TEST(GRUTest, ONNXRuntime_TestGRUOpSequenceLengthWithPartialZero) { const std::string direction = "bidirectional"; const std::vector activations = {"sigmoid", "tanh", "sigmoid", "tanh"}; diff --git a/onnxruntime/test/providers/cpu/rnn/deep_cpu_lstm_op_test.cc b/onnxruntime/test/providers/cpu/rnn/deep_cpu_lstm_op_test.cc index 7c2d23de5c..34121981a7 100644 --- a/onnxruntime/test/providers/cpu/rnn/deep_cpu_lstm_op_test.cc +++ b/onnxruntime/test/providers/cpu/rnn/deep_cpu_lstm_op_test.cc @@ -45,7 +45,8 @@ static void RunLstmTest(const std::vector& X_data, // copy the following vectors as we may modify them std::vector activations = {}, std::vector activation_alphas = {}, - std::vector activation_betas = {}) { + std::vector activation_betas = {}, + bool hasClip = true) { OpTester test("LSTM"); int num_directions = (direction == "bidirectional") ? 2 : 1; @@ -68,7 +69,9 @@ static void RunLstmTest(const std::vector& X_data, test.AddAttribute("hidden_size", hidden_size); // test.AddAttribute("output_sequence", output_sequence); test.AddAttribute("input_forget", input_forget); - test.AddAttribute("clip", clip); + if (hasClip) { + test.AddAttribute("clip", clip); + } std::vector X_dims = {seq_length, batch_size, input_size}; std::vector W_dims = {num_directions, 4 * hidden_size, input_size}; @@ -606,7 +609,8 @@ class LstmOpContext2x1x2x2 { bool use_bias = true, bool use_peepholes = true, float clip = 9999.f, - bool input_forget = false) { + bool input_forget = false, + bool hasClip = true) { // run with and without output_sequence to test UniDirectionalLstm handling when Y isn't returned ::onnxruntime::test::RunLstmTest(X, input_weights_, recurrent_weights_, expected_Y, expected_Y_h, expected_Y_c, @@ -621,7 +625,8 @@ class LstmOpContext2x1x2x2 { input_forget, activation_func_names_, activation_alphas_, - activation_betas_); + activation_betas_, + hasClip); ::onnxruntime::test::RunLstmTest(X, input_weights_, recurrent_weights_, expected_Y, expected_Y_h, expected_Y_c, @@ -636,7 +641,8 @@ class LstmOpContext2x1x2x2 { input_forget, activation_func_names_, activation_alphas_, - activation_betas_); + activation_betas_, + hasClip); } private: @@ -1121,6 +1127,46 @@ TEST(LSTMTest, ONNXRuntime_TestLSTMSequenceLengthShorterThanInputSequenceLengthN // CUDA implementation doesn't support peephole context.RunTest(X_data, batch_size, seq_len, &initial_h, &initial_c, Y_data, Y_h_data, {}, &sequence_length, false); } + +TEST(LSTMTest, ONNXRuntime_TestLSTMShorterSeqInMiddle) { + const int seq_len = 2; + int batch_size = 3; + std::vector activations = {"sigmoid", "tanh", "tanh", "sigmoid", "tanh", "tanh"}; + + bool use_bias = true; + bool use_peepholes = false; + + std::vector X_data = {-0.455351f, -0.776391f, + 0.0f, 0.0f, + 0.348763f, 0.678345f, + + -0.185934f, -0.169585f, + 0.0f, 0.0f, + 0.078053f, 0.163457f}; + + std::vector sequence_length = {2, 1, 2}; + + std::vector Y_data = {0.02907280f, 0.01765226f, -0.06724346f, 0.02957184f, -0.15355367f, 0.04701351f, + + 0.01841230f, 0.04093486f, -0.06724346f, 0.02957184f, -0.17994503f, 0.07397783f, + + -0.02912546f, 0.04120104f, 0.0f, 0.0f, -0.12768818f, 0.07457943f, + + -0.04350187f, 0.03531464f, 0.0f, 0.0f, -0.08877515f, 0.03413615f}; + + std::vector Y_h_data = {-0.0291254f, 0.04120104f, -0.06724346f, 0.02957184f, -0.12768818f, 0.07457943f, + + 0.01841230f, 0.04093486f, -0.06724346f, 0.02957184f, -0.17994503f, 0.07397783f}; + + std::vector Y_c_data = {-0.06609819f, 0.06838701f, -0.14596788f, 0.04902556f, -0.26768601f, 0.12119407f, + + 0.04934450f, 0.07126625f, -0.14596788f, 0.04902556f, -0.34139895f, 0.11673255f}; + + std::string direction = "bidirectional"; + LstmOpContext2x1x2x2 context(direction, activations); + context.RunTest(X_data, batch_size, seq_len, nullptr, nullptr, Y_data, Y_h_data, Y_c_data, + &sequence_length, use_bias, use_peepholes, 0.0f, false, false); +} #endif // USE_NGRAPH } // namespace test