|
| 1 | +/* Copyright 2025 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 "litert/tools/benchmark_litert_model.h" |
| 17 | + |
| 18 | +#include <fcntl.h> |
| 19 | +#include <sys/stat.h> |
| 20 | + |
| 21 | + |
| 22 | +#include <string> |
| 23 | +#include <utility> |
| 24 | + |
| 25 | +#include <gtest/gtest.h> |
| 26 | +#include "tflite/core/c/c_api_types.h" |
| 27 | +#include "tflite/tools/benchmark/benchmark_model.h" |
| 28 | +#include "tflite/tools/benchmark/benchmark_params.h" |
| 29 | + |
| 30 | +namespace litert { |
| 31 | +namespace benchmark { |
| 32 | +namespace { |
| 33 | +using ::litert::benchmark::BenchmarkLiteRtModel; |
| 34 | +using ::tflite::benchmark::BenchmarkListener; |
| 35 | +using ::tflite::benchmark::BenchmarkParams; |
| 36 | +using ::tflite::benchmark::BenchmarkResults; |
| 37 | + |
| 38 | +static constexpr char kModelPath[] = |
| 39 | + "third_party/odml/litert/litert/test/testdata/" |
| 40 | + "mobilenet_v2_1.0_224.tflite"; |
| 41 | +static constexpr char kSignatureToRunFor[] = "<placeholder signature>"; |
| 42 | + |
| 43 | +class TestBenchmarkListener : public BenchmarkListener { |
| 44 | + public: |
| 45 | + void OnBenchmarkEnd(const BenchmarkResults& results) override { |
| 46 | + results_ = results; |
| 47 | + } |
| 48 | + |
| 49 | + BenchmarkResults results_; |
| 50 | +}; |
| 51 | + |
| 52 | +TEST(BenchmarkLiteRtModelTest, GPUAcceleration) { |
| 53 | + // MSAN does not support GPU tests. |
| 54 | +#if defined(MEMORY_SANITIZER) || defined(THREAD_SANITIZER) |
| 55 | + GTEST_SKIP() << "GPU tests are not supported In msan"; |
| 56 | +#endif |
| 57 | + BenchmarkParams params = BenchmarkLiteRtModel::DefaultParams(); |
| 58 | + params.Set<std::string>("graph", kModelPath); |
| 59 | + params.Set<std::string>("signature_to_run_for", kSignatureToRunFor); |
| 60 | + params.Set<bool>("use_cpu", false); |
| 61 | + params.Set<bool>("use_gpu", true); |
| 62 | + params.Set<bool>("require_full_delegation", true); |
| 63 | + |
| 64 | + BenchmarkLiteRtModel benchmark = BenchmarkLiteRtModel(std::move(params)); |
| 65 | + |
| 66 | + EXPECT_EQ(benchmark.Run(), kTfLiteOk); |
| 67 | +} |
| 68 | + |
| 69 | +TEST(BenchmarkLiteRtModelTest, GPUAccelerationWithProfiler) { |
| 70 | + // MSAN does not support GPU tests. |
| 71 | +#if defined(MEMORY_SANITIZER) || defined(THREAD_SANITIZER) |
| 72 | + GTEST_SKIP() << "GPU tests are not supported In msan"; |
| 73 | +#endif |
| 74 | + BenchmarkParams params = BenchmarkLiteRtModel::DefaultParams(); |
| 75 | + params.Set<std::string>("graph", kModelPath); |
| 76 | + params.Set<std::string>("signature_to_run_for", kSignatureToRunFor); |
| 77 | + params.Set<bool>("use_cpu", false); |
| 78 | + params.Set<bool>("use_gpu", true); |
| 79 | + params.Set<bool>("require_full_delegation", true); |
| 80 | + params.Set<bool>("use_profiler", true); |
| 81 | + |
| 82 | + BenchmarkLiteRtModel benchmark = BenchmarkLiteRtModel(std::move(params)); |
| 83 | + |
| 84 | + EXPECT_EQ(benchmark.Run(), kTfLiteOk); |
| 85 | +} |
| 86 | + |
| 87 | +} // namespace |
| 88 | +} // namespace benchmark |
| 89 | +} // namespace litert |
0 commit comments