tensorflow/lite/kernels/bidirectional_sequence_lstm_test.cc
// Same as the no cifg no peephole no projection no clipping test, but have an
// aux input (without aux input weights), this is the case when stacking but no
// cross-links.
Same as the no cifg no peephole no projection no clipping test, but have an
aux input (without aux input weights), this is the case when stacking but no
cross-links.
Same as the no cifg no peephole no projection no clipping test but have an aux input ( without aux input weights ) this is the case when stacking but no cross - links .
TEST_P(LSTMOpTest, BlackBoxTestWithAuxInputZeroAuxWeight) {
  const int n_batch = 1;
  const int n_input = 2;
  const int n_cell = 4;
  const int n_output = 4;
  const int sequence_length = 3;
  auto params = GetParam();
  const bool quantize_weights = std::get<0>(params);
  const bool asymmetric_quantize_inputs = std::get<1>(params);
  BidirectionalLSTMOpModel lstm(
      n_batch, n_input, n_cell, n_output, sequence_length, false,
      false, false,
      false, false,
      true, 0.0,
      0.0, quantize_weights, true,
      {
          {sequence_length, n_batch, n_input},  
          {n_cell, n_input},  
          {n_cell, n_input},  
          {n_cell, n_input},  
          {n_cell, n_input},  
          {n_cell, n_output},  
          {n_cell, n_output},  
          {n_cell, n_output},  
          {n_cell, n_output},  
          {0},  
          {0},  
          {0},  
          {n_cell},  
          {n_cell},  
          {n_cell},  
          {n_cell},  
          {0, 0},  
          {0},     
          {n_cell, n_input},  
          {n_cell, n_input},  
          {n_cell, n_input},  
          {n_cell, n_input},  
          {n_cell, n_output},  
          {n_cell, n_output},  
          {n_cell, n_output},  
          {n_cell, n_output},  
          {0},  
          {0},  
          {0},  
          {n_cell},  
          {n_cell},  
          {n_cell},  
          {n_cell},  
          {0, 0},  
          {0},     
          {n_batch, n_output},  
          {n_batch, n_cell},    
          {n_batch, n_output},  
          {n_batch, n_cell},    
          {sequence_length, n_batch, n_input},  
          {n_cell, n_input},                    
          {n_cell, n_input},                    
          {n_cell, n_input},                    
          {n_cell, n_input},                    
          {n_cell, n_input},                    
          {n_cell, n_input},                    
          {n_cell, n_input},                    
          {n_cell, n_input},                    
      },
      asymmetric_quantize_inputs);
  lstm.SetInputToInputWeights({-0.45018822, -0.02338299, -0.0870589,
                               -0.34550029, 0.04266912, -0.15680569,
                               -0.34856534, 0.43890524});
  lstm.SetInputToCellWeights({-0.50013041, 0.1370284, 0.11810488, 0.2013163,
                              -0.20583314, 0.44344562, 0.22077113,
                              -0.29909778});
  lstm.SetInputToForgetWeights({0.09701663, 0.20334584, -0.50592935,
                                -0.31343272, -0.40032279, 0.44781327,
                                0.01387155, -0.35593212});
  lstm.SetInputToOutputWeights({-0.25065863, -0.28290087, 0.04613829,
                                0.40525138, 0.44272184, 0.03897077, -0.1556896,
                                0.19487578});
  lstm.SetInputGateBias({0., 0., 0., 0.});
  lstm.SetCellBias({0., 0., 0., 0.});
  lstm.SetForgetGateBias({1., 1., 1., 1.});
  lstm.SetOutputGateBias({0., 0., 0., 0.});
  lstm.SetRecurrentToInputWeights(
      {-0.0063535, -0.2042388, 0.31454784, -0.35746509, 0.28902304, 0.08183324,
       -0.16555229, 0.02286911, -0.13566875, 0.03034258, 0.48091322,
       -0.12528998, 0.24077177, -0.51332325, -0.33502164, 0.10629296});
  lstm.SetRecurrentToCellWeights(
      {-0.3407414, 0.24443203, -0.2078532, 0.26320225, 0.05695659, -0.00123841,
       -0.4744786, -0.35869038, -0.06418842, -0.13502428, -0.501764, 0.22830659,
       -0.46367589, 0.26016325, -0.03894562, -0.16368064});
  lstm.SetRecurrentToForgetWeights(
      {-0.48684245, -0.06655136, 0.42224967, 0.2112639, 0.27654213, 0.20864892,
       -0.07646349, 0.45877004, 0.00141793, -0.14609534, 0.36447752, 0.09196436,
       0.28053468, 0.01560611, -0.20127171, -0.01140004});
  lstm.SetRecurrentToOutputWeights(
      {0.43385774, -0.17194885, 0.2718237, 0.09215671, 0.24107647, -0.39835793,
       0.18212086, 0.01301402, 0.48572797, -0.50656658, 0.20047462, -0.20607421,
       -0.51818722, -0.15390486, 0.0468148, 0.39922136});
  static float lstm_input[] = {2., 3., 3., 4., 1., 1.};
  static float lstm_fw_golden_output[] = {
      -0.02973187, 0.1229473,  0.20885126, -0.15358765,
      -0.03716109, 0.12507336, 0.41193449, -0.20860538,
      -0.15053082, 0.09120187, 0.24278517, -0.12222792};
  static float lstm_bw_golden_output[] = {
      -0.0806187, 0.139077, 0.400476,   -0.197842, -0.0332076, 0.123838,
      0.309777,   -0.17621, -0.0490733, 0.0739237, 0.067706,   -0.0208124};
  float* batch0_start = lstm_input;
  float* batch0_end = batch0_start + lstm.num_inputs() * lstm.sequence_length();
  lstm.SetInput(0, batch0_start, batch0_end);
  lstm.SetAuxInput(0, batch0_start, batch0_end);
  std::vector<float> dummy_weights(n_cell * n_input, 0.0f);
  lstm.SetAuxInputToInputWeights(dummy_weights);
  lstm.SetAuxInputToForgetWeights(dummy_weights);
  lstm.SetAuxInputToCellWeights(dummy_weights);
  lstm.SetAuxInputToOutputWeights(dummy_weights);
  lstm.Invoke();
  float* fw_golden_start = lstm_fw_golden_output;
  float* fw_golden_end =
      fw_golden_start + lstm.num_fw_outputs() * lstm.sequence_length();
  std::vector<float> fw_expected;
  fw_expected.insert(fw_expected.end(), fw_golden_start, fw_golden_end);
  EXPECT_THAT(lstm.GetFwOutput(),
              ElementsAreArray(
                  ArrayFloatNear(fw_expected, quantize_weights ? 1e-2 : 1e-5)));
  float* bw_golden_start = lstm_bw_golden_output;
  float* bw_golden_end =
      bw_golden_start + lstm.num_bw_outputs() * lstm.sequence_length();
  std::vector<float> bw_expected;
  bw_expected.insert(bw_expected.end(), bw_golden_start, bw_golden_end);
  EXPECT_THAT(lstm.GetBwOutput(),
              ElementsAreArray(
                  ArrayFloatNear(bw_expected, quantize_weights ? 1e-2 : 1e-5)));
}
TEST_P ( LSTMOpTest , BlackBoxTestWithAuxInputZeroAuxWeight ) { const int n_batch = 1 ; const int n_input = 2 ; const int n_cell = 4 ; const int n_output = 4 ; const int sequence_length = 3 ; auto params = GetParam ( ) ; const bool quantize_weights = std :: get < 0 > ( params ) ; const bool asymmetric_quantize_inputs = std :: get < 1 > ( params ) ; BidirectionalLSTMOpModel lstm ( n_batch , n_input , n_cell , n_output , sequence_length , false , false , false , false , false , true , 0.0 , 0.0 , quantize_weights , true , { { sequence_length , n_batch , n_input } , { n_cell , n_input } , { n_cell , n_input } , { n_cell , n_input } , { n_cell , n_input } , { n_cell , n_output } , { n_cell , n_output } , { n_cell , n_output } , { n_cell , n_output } , { 0 } , { 0 } , { 0 } , { n_cell } , { n_cell } , { n_cell } , { n_cell } , { 0 , 0 } , { 0 } , { n_cell , n_input } , { n_cell , n_input } , { n_cell , n_input } , { n_cell , n_input } , { n_cell , n_output } , { n_cell , n_output } , { n_cell , n_output } , { n_cell , n_output } , { 0 } , { 0 } , { 0 } , { n_cell } , { n_cell } , { n_cell } , { n_cell } , { 0 , 0 } , { 0 } , { n_batch , n_output } , { n_batch , n_cell } , { n_batch , n_output } , { n_batch , n_cell } , { sequence_length , n_batch , n_input } , { n_cell , n_input } , { n_cell , n_input } , { n_cell , n_input } , { n_cell , n_input } , { n_cell , n_input } , { n_cell , n_input } , { n_cell , n_input } , { n_cell , n_input } , } , asymmetric_quantize_inputs ) ; lstm . SetInputToInputWeights ( { -0.45018822 , -0.02338299 , -0.0870589 , -0.34550029 , 0.04266912 , -0.15680569 , -0.34856534 , 0.43890524 } ) ; lstm . SetInputToCellWeights ( { -0.50013041 , 0.1370284 , 0.11810488 , 0.2013163 , -0.20583314 , 0.44344562 , 0.22077113 , -0.29909778 } ) ; lstm . SetInputToForgetWeights ( { 0.09701663 , 0.20334584 , -0.50592935 , -0.31343272 , -0.40032279 , 0.44781327 , 0.01387155 , -0.35593212 } ) ; lstm . SetInputToOutputWeights ( { -0.25065863 , -0.28290087 , 0.04613829 , 0.40525138 , 0.44272184 , 0.03897077 , -0.1556896 , 0.19487578 } ) ; lstm . SetInputGateBias ( { 0. , 0. , 0. , 0. } ) ; lstm . SetCellBias ( { 0. , 0. , 0. , 0. } ) ; lstm . SetForgetGateBias ( { 1. , 1. , 1. , 1. } ) ; lstm . SetOutputGateBias ( { 0. , 0. , 0. , 0. } ) ; lstm . SetRecurrentToInputWeights ( { -0.0063535 , -0.2042388 , 0.31454784 , -0.35746509 , 0.28902304 , 0.08183324 , -0.16555229 , 0.02286911 , -0.13566875 , 0.03034258 , 0.48091322 , -0.12528998 , 0.24077177 , -0.51332325 , -0.33502164 , 0.10629296 } ) ; lstm . SetRecurrentToCellWeights ( { -0.3407414 , 0.24443203 , -0.2078532 , 0.26320225 , 0.05695659 , -0.00123841 , -0.4744786 , -0.35869038 , -0.06418842 , -0.13502428 , -0.501764 , 0.22830659 , -0.46367589 , 0.26016325 , -0.03894562 , -0.16368064 } ) ; lstm . SetRecurrentToForgetWeights ( { -0.48684245 , -0.06655136 , 0.42224967 , 0.2112639 , 0.27654213 , 0.20864892 , -0.07646349 , 0.45877004 , 0.00141793 , -0.14609534 , 0.36447752 , 0.09196436 , 0.28053468 , 0.01560611 , -0.20127171 , -0.01140004 } ) ; lstm . SetRecurrentToOutputWeights ( { 0.43385774 , -0.17194885 , 0.2718237 , 0.09215671 , 0.24107647 , -0.39835793 , 0.18212086 , 0.01301402 , 0.48572797 , -0.50656658 , 0.20047462 , -0.20607421 , -0.51818722 , -0.15390486 , 0.0468148 , 0.39922136 } ) ; static float lstm_input [ ] = { 2. , 3. , 3. , 4. , 1. , 1. } ; static float lstm_fw_golden_output [ ] = { -0.02973187 , 0.1229473 , 0.20885126 , -0.15358765 , -0.03716109 , 0.12507336 , 0.41193449 , -0.20860538 , -0.15053082 , 0.09120187 , 0.24278517 , -0.12222792 } ; static float lstm_bw_golden_output [ ] = { -0.0806187 , 0.139077 , 0.400476 , -0.197842 , -0.0332076 , 0.123838 , 0.309777 , -0.17621 , -0.0490733 , 0.0739237 , 0.067706 , -0.0208124 } ; float * batch0_start = lstm_input ; float * batch0_end = batch0_start + lstm . num_inputs ( ) * lstm . sequence_length ( ) ; lstm . SetInput ( 0 , batch0_start , batch0_end ) ; lstm . SetAuxInput ( 0 , batch0_start , batch0_end ) ; std :: vector < float > dummy_weights ( n_cell * n_input , 0.0f ) ; lstm . SetAuxInputToInputWeights ( dummy_weights ) ; lstm . SetAuxInputToForgetWeights ( dummy_weights ) ; lstm . SetAuxInputToCellWeights ( dummy_weights ) ; lstm . SetAuxInputToOutputWeights ( dummy_weights ) ; lstm . Invoke ( ) ; float * fw_golden_start = lstm_fw_golden_output ; float * fw_golden_end = fw_golden_start + lstm . num_fw_outputs ( ) * lstm . sequence_length ( ) ; std :: vector < float > fw_expected ; fw_expected . insert ( fw_expected . end ( ) , fw_golden_start , fw_golden_end ) ; EXPECT_THAT ( lstm . GetFwOutput ( ) , ElementsAreArray ( ArrayFloatNear ( fw_expected , quantize_weights ? 1e-2 : 1e-5 ) ) ) ; float * bw_golden_start = lstm_bw_golden_output ; float * bw_golden_end = bw_golden_start + lstm . num_bw_outputs ( ) * lstm . sequence_length ( ) ; std :: vector < float > bw_expected ; bw_expected . insert ( bw_expected . end ( ) , bw_golden_start , bw_golden_end ) ; EXPECT_THAT ( lstm . GetBwOutput ( ) , ElementsAreArray ( ArrayFloatNear ( bw_expected , quantize_weights ? 1e-2 : 1e-5 ) ) ) ; }
Same as the no cifg no peephole no projection no clipping test, but have an
aux input (without aux input weights), this is the case when stacking but no
cross-links.
Same as the no cifg no peephole no projection no clipping test but have an aux input ( without aux input weights ) this is the case when stacking but no cross - links .
// n_cell and n_output have the same size when there is no projection. /*use_cifg=*/ /*use_peephole=*/ /*use_projection_weights=*/ /*use_projection_bias=*/ /*merge_outputs=*/ /*use_aux_input=*/ /*cell_clip=*/ /*proj_clip=*/ /*time_major=*/ // input tensor // Forward cell // input_to_input_weight tensor // input_to_forget_weight tensor // input_to_cell_weight tensor // input_to_output_weight tensor // recurrent_to_input_weight tensor // recurrent_to_forget_weight tensor // recurrent_to_cell_weight tensor // recurrent_to_output_weight tensor // cell_to_input_weight tensor // cell_to_forget_weight tensor // cell_to_output_weight tensor // input_gate_bias tensor // forget_gate_bias tensor // cell_gate_bias tensor // output_gate_bias tensor // projection_weight tensor // projection_bias tensor // Backward cell // input_to_input_weight tensor // input_to_forget_weight tensor // input_to_cell_weight tensor // input_to_output_weight tensor // recurrent_to_input_weight tensor // recurrent_to_forget_weight tensor // recurrent_to_cell_weight tensor // recurrent_to_output_weight tensor // cell_to_input_weight tensor // cell_to_forget_weight tensor // cell_to_output_weight tensor // input_gate_bias tensor // forget_gate_bias tensor // cell_gate_bias tensor // output_gate_bias tensor // projection_weight tensor // projection_bias tensor // activation_state tensor // cell_state tensor // activation_state tensor // cell_state tensor // aux_input tensor // aux_fw_input_to_input tensor // aux_fw_input_to_forget tensor // aux_fw_input_to_cell tensor // aux_fw_input_to_output tensor // aux_bw_input_to_input tensor // aux_bw_input_to_forget tensor // aux_bw_input_to_cell tensor // aux_bw_input_to_output tensor // Input should have n_input * sequence_length many values. // Aux input and input are the same, so we should observe the same outputs // as there's no aux input.