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
This commit is contained in:
Hector Li 2019-07-22 11:42:57 -07:00 committed by GitHub
parent 227734139a
commit f938a6e53a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 84 additions and 5 deletions

View file

@ -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<std::string> activations = {"sigmoid", "tanh", "sigmoid", "tanh"};
DeepCpuGruOpTestContext ctx(direction, activations);
const int batch_size = 3;
const int seq_length = 2;
std::vector<float> 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<int> sequence_length = {2, 1, 2};
std::vector<float> 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<float> 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<float> 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<std::string> activations = {"sigmoid", "tanh", "sigmoid", "tanh"};

View file

@ -45,7 +45,8 @@ static void RunLstmTest(const std::vector<float>& X_data,
// copy the following vectors as we may modify them
std::vector<string> activations = {},
std::vector<float> activation_alphas = {},
std::vector<float> activation_betas = {}) {
std::vector<float> 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<float>& X_data,
test.AddAttribute("hidden_size", hidden_size);
// test.AddAttribute<int64_t>("output_sequence", output_sequence);
test.AddAttribute<int64_t>("input_forget", input_forget);
test.AddAttribute<float>("clip", clip);
if (hasClip) {
test.AddAttribute<float>("clip", clip);
}
std::vector<int64_t> X_dims = {seq_length, batch_size, input_size};
std::vector<int64_t> 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<std::string> activations = {"sigmoid", "tanh", "tanh", "sigmoid", "tanh", "tanh"};
bool use_bias = true;
bool use_peepholes = false;
std::vector<float> 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<int> sequence_length = {2, 1, 2};
std::vector<float> 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<float> 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<float> 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