| 65 | } |
| 66 | |
| 67 | TfLiteStatus RunKernelTest(const kernel_test::TestOptions& options, |
| 68 | TestRunner* runner) { |
| 69 | InputGenerator input_generator; |
| 70 | |
| 71 | if (options.read_input_from_file.empty()) { |
| 72 | TF_LITE_ENSURE_STATUS(input_generator.LoadModel(options.tflite_model)); |
| 73 | TF_LITE_ENSURE_STATUS( |
| 74 | input_generator.GenerateInput(options.input_distribution)); |
| 75 | } else { |
| 76 | TF_LITE_ENSURE_STATUS( |
| 77 | input_generator.ReadInputsFromFile(options.read_input_from_file)); |
| 78 | } |
| 79 | |
| 80 | runner->LoadModel(options.tflite_model); |
| 81 | runner->AllocateTensors(); |
| 82 | if (!runner->IsValid()) return kTfLiteError; |
| 83 | auto input_tensor_ids = runner->GetInputs(); |
| 84 | auto inputs = input_generator.GetInputs(); |
| 85 | if (inputs.size() != input_tensor_ids.size()) { |
| 86 | fprintf(stderr, |
| 87 | "Number of input tensors generated doesn't match what the model " |
| 88 | "asks for."); |
| 89 | } |
| 90 | for (int i = 0; i < inputs.size(); i++) { |
| 91 | runner->SetInput(input_tensor_ids[i], inputs[i]); |
| 92 | } |
| 93 | |
| 94 | runner->Invoke(); |
| 95 | |
| 96 | if (!options.dump_input_to_file.empty()) { |
| 97 | TF_LITE_ENSURE_STATUS( |
| 98 | input_generator.WriteInputsToFile(options.dump_input_to_file)); |
| 99 | } |
| 100 | |
| 101 | if (!options.dump_output_to_file.empty()) { |
| 102 | std::ofstream output_file; |
| 103 | output_file.open(options.dump_output_to_file, |
| 104 | std::fstream::out | std::fstream::trunc); |
| 105 | if (!output_file) { |
| 106 | return kTfLiteError; |
| 107 | } |
| 108 | |
| 109 | for (auto id : runner->GetOutputs()) { |
| 110 | output_file << runner->ReadOutput(id) << "\n"; |
| 111 | } |
| 112 | output_file.close(); |
| 113 | } |
| 114 | |
| 115 | return kTfLiteOk; |
| 116 | } |
| 117 | |
| 118 | } // namespace kernel_test |
| 119 | } // namespace testing |