| 52 | } |
| 53 | |
| 54 | TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) { |
| 55 | // Check we have all the inputs and outputs we need. |
| 56 | TF_LITE_ENSURE_EQ(context, node->inputs->size, 5); |
| 57 | TF_LITE_ENSURE_EQ(context, node->outputs->size, 1); |
| 58 | |
| 59 | const TfLiteTensor* input = GetInput(context, node, kInputTensor); |
| 60 | const TfLiteTensor* input_weights = GetInput(context, node, kWeightsTensor); |
| 61 | const TfLiteTensor* recurrent_weights = |
| 62 | GetInput(context, node, kRecurrentWeightsTensor); |
| 63 | const TfLiteTensor* bias = GetInput(context, node, kBiasTensor); |
| 64 | const TfLiteTensor* hidden_state = |
| 65 | GetInput(context, node, kHiddenStateTensor); |
| 66 | |
| 67 | // Check all the parameters of tensor match within themselves and match the |
| 68 | // input configuration. |
| 69 | auto* params = reinterpret_cast<TfLiteSequenceRNNParams*>(node->builtin_data); |
| 70 | const bool time_major = params->time_major; |
| 71 | const int batch_size = |
| 72 | (time_major) ? input->dims->data[1] : input->dims->data[0]; |
| 73 | const int max_time = |
| 74 | (time_major) ? input->dims->data[0] : input->dims->data[1]; |
| 75 | const int num_units = input_weights->dims->data[0]; |
| 76 | TF_LITE_ENSURE_EQ(context, input->dims->data[2], |
| 77 | input_weights->dims->data[1]); |
| 78 | TF_LITE_ENSURE_EQ(context, input_weights->dims->data[0], bias->dims->data[0]); |
| 79 | TF_LITE_ENSURE_EQ(context, recurrent_weights->dims->data[0], |
| 80 | bias->dims->data[0]); |
| 81 | TF_LITE_ENSURE_EQ(context, recurrent_weights->dims->data[1], |
| 82 | bias->dims->data[0]); |
| 83 | TF_LITE_ENSURE_EQ(context, input->type, kTfLiteFloat32); |
| 84 | TF_LITE_ENSURE_EQ(context, input_weights->type, recurrent_weights->type); |
| 85 | TF_LITE_ENSURE_EQ(context, NumDimensions(hidden_state), 2); |
| 86 | TF_LITE_ENSURE_EQ(context, hidden_state->dims->data[0], batch_size); |
| 87 | TF_LITE_ENSURE_EQ(context, hidden_state->dims->data[1], num_units); |
| 88 | |
| 89 | TfLiteTensor* output = GetOutput(context, node, kOutputTensor); |
| 90 | |
| 91 | // Resize output. |
| 92 | TfLiteIntArray* output_size_array = TfLiteIntArrayCreate(3); |
| 93 | output_size_array->data[0] = (time_major) ? max_time : batch_size; |
| 94 | output_size_array->data[1] = (time_major) ? batch_size : max_time; |
| 95 | output_size_array->data[2] = num_units; |
| 96 | TF_LITE_ENSURE_OK(context, |
| 97 | context->ResizeTensor(context, output, output_size_array)); |
| 98 | |
| 99 | const bool is_hybrid = IsHybridOp(input, input_weights); |
| 100 | |
| 101 | // Allocate temporary tensors to store quantized values of input and |
| 102 | // hidden_state tensors. |
| 103 | if (is_hybrid) { |
| 104 | int* scratch_tensor_index = reinterpret_cast<int*>(node->user_data); |
| 105 | TfLiteIntArrayFree(node->temporaries); |
| 106 | node->temporaries = TfLiteIntArrayCreate(3); |
| 107 | node->temporaries->data[0] = *scratch_tensor_index; |
| 108 | TfLiteTensor* input_quantized = GetTemporary(context, node, /*index=*/0); |
| 109 | input_quantized->type = input_weights->type; |
| 110 | input_quantized->allocation_type = kTfLiteArenaRw; |
| 111 | if (!TfLiteIntArrayEqual(input_quantized->dims, input->dims)) { |
nothing calls this directly
no test coverage detected