|
| 1 | +// Copyright 2025 Google LLC. |
| 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 <cstring> |
| 16 | +#include <tuple> |
| 17 | +#include <utility> |
| 18 | + |
| 19 | +#include <gmock/gmock.h> |
| 20 | +#include <gtest/gtest.h> |
| 21 | +#include "absl/debugging/leak_check.h" // from @com_google_absl |
| 22 | +#include "absl/log/absl_log.h" // from @com_google_absl |
| 23 | +#include "absl/strings/string_view.h" // from @com_google_absl |
| 24 | +#include "absl/types/span.h" // from @com_google_absl |
| 25 | +#include "litert/c/litert_common.h" |
| 26 | +#include "litert/c/litert_tensor_buffer_types.h" |
| 27 | +#include "litert/cc/litert_compiled_model.h" |
| 28 | +#include "litert/cc/litert_environment.h" |
| 29 | +#include "litert/cc/litert_expected.h" |
| 30 | +#include "litert/cc/litert_macros.h" |
| 31 | +#include "litert/cc/litert_model.h" |
| 32 | +#include "litert/cc/litert_options.h" |
| 33 | +#include "litert/cc/litert_tensor_buffer.h" |
| 34 | +#include "litert/cc/options/litert_gpu_options.h" |
| 35 | +#include "litert/test/common.h" |
| 36 | +#include "litert/test/matchers.h" |
| 37 | +#include "litert/test/testdata/simple_model_test_vectors.h" |
| 38 | + |
| 39 | +using testing::FloatNear; |
| 40 | +using testing::Pointwise; |
| 41 | + |
| 42 | +namespace litert { |
| 43 | +namespace { |
| 44 | + |
| 45 | +using TestParams = std::tuple<LiteRtDelegatePrecision>; |
| 46 | + |
| 47 | +Expected<Options> CreateGpuOptions(const TestParams& params) { |
| 48 | + LITERT_ASSIGN_OR_RETURN(auto gpu_options, GpuOptions::Create()); |
| 49 | + LITERT_RETURN_IF_ERROR(gpu_options.SetDelegatePrecision(std::get<0>(params))); |
| 50 | + |
| 51 | + LITERT_ASSIGN_OR_RETURN(litert::Options options, Options::Create()); |
| 52 | + options.SetHardwareAccelerators(kLiteRtHwAcceleratorGpu); |
| 53 | + options.AddOpaqueOptions(std::move(gpu_options)); |
| 54 | + return std::move(options); |
| 55 | +} |
| 56 | + |
| 57 | +class ParameterizedTest : public ::testing::TestWithParam<TestParams> {}; |
| 58 | + |
| 59 | +TEST_P(ParameterizedTest, Basic) { |
| 60 | + // To workaround the memory leak in Nvidia's driver |
| 61 | + absl::LeakCheckDisabler disable_leak_check; |
| 62 | + |
| 63 | + LITERT_ASSERT_OK_AND_ASSIGN( |
| 64 | + auto model, |
| 65 | + Model::CreateFromFile(testing::GetTestFilePath(kModelFileName))); |
| 66 | + |
| 67 | + auto env = litert::Environment::Create({}); |
| 68 | + ASSERT_TRUE(env); |
| 69 | + |
| 70 | + LITERT_ASSERT_OK_AND_ASSIGN(auto options, CreateGpuOptions(GetParam())); |
| 71 | + LITERT_ASSERT_OK_AND_ASSIGN(auto compiled_model, |
| 72 | + CompiledModel::Create(*env, model, options)); |
| 73 | + |
| 74 | + LITERT_ASSERT_OK_AND_ASSIGN(auto signatures, model.GetSignatures()); |
| 75 | + EXPECT_EQ(signatures.size(), 1); |
| 76 | + |
| 77 | + auto signature_key = signatures[0].Key(); |
| 78 | + EXPECT_EQ(signature_key, Model::DefaultSignatureKey()); |
| 79 | + size_t signature_index = 0; |
| 80 | + |
| 81 | + LITERT_ASSERT_OK_AND_ASSIGN( |
| 82 | + auto input_buffers, compiled_model.CreateInputBuffers(signature_index)); |
| 83 | + |
| 84 | + LITERT_ASSERT_OK_AND_ASSIGN( |
| 85 | + auto output_buffers, compiled_model.CreateOutputBuffers(signature_index)); |
| 86 | + |
| 87 | + // Fill model inputs. |
| 88 | + auto input_names = signatures[0].InputNames(); |
| 89 | + EXPECT_EQ(input_names.size(), 2); |
| 90 | + EXPECT_EQ(input_names.at(0), "arg0"); |
| 91 | + EXPECT_EQ(input_names.at(1), "arg1"); |
| 92 | + LITERT_ASSERT_OK_AND_ASSIGN(auto buffer0_type, input_buffers[0].BufferType()); |
| 93 | + EXPECT_EQ(buffer0_type, kLiteRtTensorBufferTypeHostMemory); |
| 94 | + ASSERT_TRUE(input_buffers[0].Write<float>( |
| 95 | + absl::MakeConstSpan(kTestInput0Tensor, kTestInput0Size))); |
| 96 | + LITERT_ASSERT_OK_AND_ASSIGN(auto buffer1_type, input_buffers[0].BufferType()); |
| 97 | + EXPECT_EQ(buffer1_type, kLiteRtTensorBufferTypeHostMemory); |
| 98 | + ASSERT_TRUE(input_buffers[1].Write<float>( |
| 99 | + absl::MakeConstSpan(kTestInput1Tensor, kTestInput1Size))); |
| 100 | + |
| 101 | + // Execute model. |
| 102 | + compiled_model.Run(signature_index, input_buffers, output_buffers); |
| 103 | + |
| 104 | + // Check model output. |
| 105 | + auto output_names = signatures[0].OutputNames(); |
| 106 | + EXPECT_EQ(output_names.size(), 1); |
| 107 | + EXPECT_EQ(output_names.at(0), "tfl.add"); |
| 108 | + LITERT_ASSERT_OK_AND_ASSIGN(auto output_type, input_buffers[0].BufferType()); |
| 109 | + EXPECT_EQ(output_type, kLiteRtTensorBufferTypeHostMemory); |
| 110 | + { |
| 111 | + auto lock_and_addr = litert::TensorBufferScopedLock::Create<const float>( |
| 112 | + output_buffers[0], TensorBuffer::LockMode::kRead); |
| 113 | + ASSERT_TRUE(lock_and_addr); |
| 114 | + auto output = absl::MakeSpan(lock_and_addr->second, kTestOutputSize); |
| 115 | + for (auto i = 0; i < kTestOutputSize; ++i) { |
| 116 | + ABSL_LOG(INFO) << "Result: " << output[i] << "\t" << kTestOutputTensor[i]; |
| 117 | + } |
| 118 | + EXPECT_THAT(output, Pointwise(FloatNear(1e-5), kTestOutputTensor)); |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +INSTANTIATE_TEST_SUITE_P( |
| 123 | + CompiledModelWebGpuTest, ParameterizedTest, |
| 124 | + ::testing::Combine(::testing::ValuesIn<LiteRtDelegatePrecision>({ |
| 125 | + kLiteRtDelegatePrecisionDefault, |
| 126 | + kLiteRtDelegatePrecisionFp16, |
| 127 | + kLiteRtDelegatePrecisionFp32, |
| 128 | + }))); |
| 129 | + |
| 130 | +} // namespace |
| 131 | +} // namespace litert |
0 commit comments