1 /* Copyright 2018 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
16 #include <jni.h>
17
18 #include <sstream>
19 #include <string>
20
21 #include "tensorflow/lite/tools/benchmark/benchmark_tflite_model.h"
22
23 #ifdef __ANDROID__
24 #include <android/log.h>
25 #endif
26
27 namespace tflite {
28 namespace benchmark {
29 namespace {
30
Run(int argc,char ** argv)31 void Run(int argc, char** argv) {
32 BenchmarkTfLiteModel benchmark;
33 benchmark.Run(argc, argv);
34 }
35
36 } // namespace
37 } // namespace benchmark
38 } // namespace tflite
39
40 extern "C" {
41
42 JNIEXPORT void JNICALL
Java_org_tensorflow_lite_benchmark_BenchmarkModel_nativeRun(JNIEnv * env,jclass clazz,jstring args_obj)43 Java_org_tensorflow_lite_benchmark_BenchmarkModel_nativeRun(JNIEnv* env,
44 jclass clazz,
45 jstring args_obj) {
46 const char* args_chars = env->GetStringUTFChars(args_obj, nullptr);
47
48 // Split the args string into individual arg tokens.
49 std::istringstream iss(args_chars);
50 std::vector<std::string> args_split{std::istream_iterator<std::string>(iss),
51 {}};
52
53 // Construct a fake argv command-line object for the benchmark.
54 std::vector<char*> argv;
55 std::string arg0 = "(BenchmarkModelAndroid)";
56 argv.push_back(const_cast<char*>(arg0.data()));
57 for (auto& arg : args_split) {
58 argv.push_back(const_cast<char*>(arg.data()));
59 }
60
61 tflite::benchmark::Run(static_cast<int>(argv.size()), argv.data());
62
63 env->ReleaseStringUTFChars(args_obj, args_chars);
64 }
65
66 } // extern "C"
67