1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 #include "tensorflow/lite/testing/tflite_diff_util.h"
16 
17 #include <cstdarg>
18 #include <cstdio>
19 #include <cstdlib>
20 #include <sstream>
21 
22 #include "tensorflow/lite/testing/generate_testspec.h"
23 #include "tensorflow/lite/testing/parse_testdata.h"
24 #include "tensorflow/lite/testing/tflite_driver.h"
25 
26 namespace tflite {
27 namespace testing {
28 namespace {
SingleRunDiffTestWithProvidedRunner(::tflite::testing::DiffOptions options,int num_invocations,TestRunner * (* runner_factory)())29 bool SingleRunDiffTestWithProvidedRunner(::tflite::testing::DiffOptions options,
30                                          int num_invocations,
31                                          TestRunner* (*runner_factory)()) {
32   std::stringstream tflite_stream;
33   std::string reference_tflite_model = options.reference_tflite_model.empty()
34                                            ? options.tflite_model
35                                            : options.reference_tflite_model;
36   if (!GenerateTestSpecFromTFLiteModel(
37           tflite_stream, reference_tflite_model, num_invocations,
38           options.input_layer, options.input_layer_type,
39           options.input_layer_shape, options.output_layer)) {
40     return false;
41   }
42 
43   std::unique_ptr<TestRunner> runner(runner_factory());
44   runner->LoadModel(options.tflite_model);
45   return ParseAndRunTests(&tflite_stream, runner.get());
46 }
47 }  // namespace
48 
RunDiffTest(const DiffOptions & options,int num_invocations)49 bool RunDiffTest(const DiffOptions& options, int num_invocations) {
50   std::stringstream tflite_stream;
51   if (!GenerateTestSpecFromTensorflowModel(
52           tflite_stream, options.tensorflow_model, options.tflite_model,
53           num_invocations, options.input_layer, options.input_layer_type,
54           options.input_layer_shape, options.output_layer)) {
55     return false;
56   }
57   TfLiteDriver tflite_driver(options.delegate);
58   tflite_driver.LoadModel(options.tflite_model);
59   return ParseAndRunTests(&tflite_stream, &tflite_driver);
60 }
61 
RunDiffTestWithProvidedRunner(const tflite::testing::DiffOptions & options,TestRunner * (* runner_factory)())62 bool RunDiffTestWithProvidedRunner(const tflite::testing::DiffOptions& options,
63                                    TestRunner* (*runner_factory)()) {
64   int failure_count = 0;
65   for (int i = 0; i < options.num_runs_per_pass; i++) {
66     if (!SingleRunDiffTestWithProvidedRunner(options,
67                                              /*num_invocations=*/1,
68                                              runner_factory)) {
69       ++failure_count;
70     }
71   }
72   int failures_in_first_pass = failure_count;
73 
74   if (failure_count == 0) {
75     // Let's try again with num_invocations > 1 to make sure we can do multiple
76     // invocations without resetting the interpreter.
77     for (int i = 0; i < options.num_runs_per_pass; i++) {
78       if (!SingleRunDiffTestWithProvidedRunner(options,
79                                                /*num_invocations=*/2,
80                                                runner_factory)) {
81         ++failure_count;
82       }
83     }
84   }
85 
86   fprintf(stderr, "Num errors in single-inference pass: %d\n",
87           failures_in_first_pass);
88   fprintf(stderr, "Num errors in multi-inference pass : %d\n",
89           failure_count - failures_in_first_pass);
90 
91   return failure_count == 0;
92 }
93 }  // namespace testing
94 
95 }  // namespace tflite
96