File size: 4,951 Bytes
5f923cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright 2025 The ODML Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "runtime/util/litert_lm_loader.h"

#include <filesystem>  // NOLINT: Required for path manipulation.
#include <memory>
#include <utility>

#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "absl/status/status.h"  // from @com_google_absl
#include "runtime/components/model_resources.h"
#include "runtime/util/memory_mapped_file.h"
#include "runtime/util/scoped_file.h"
#include "runtime/util/status_macros.h"  // IWYU pragma: keep
#include "schema/core/litertlm_header_schema_generated.h"

namespace litert::lm {

namespace {

using ::testing::status::StatusIs;

TEST(LitertLmLoaderTest, GetSectionLocationNotFound) {
  const auto model_path =
      std::filesystem::path(::testing::SrcDir()) /
      "litert_lm/runtime/testdata/test_lm.litertlm";
  ASSERT_OK_AND_ASSIGN(std::unique_ptr<MemoryMappedFile> mapped_file,
                       MemoryMappedFile::Create(model_path.string()));
  LitertLmLoader loader(std::move(mapped_file));

  BufferKey embedder_key(schema::AnySectionDataType_TFLiteModel,
                         ModelType::kTfLiteEmbedder);
  EXPECT_THAT(loader.GetSectionLocation(embedder_key),
              StatusIs(absl::StatusCode::kNotFound));
}

TEST(LitertLmLoaderTest, InitializeWithSentencePieceFile) {
  const auto model_path =
      std::filesystem::path(::testing::SrcDir()) /
      "litert_lm/runtime/testdata/test_lm.litertlm";
  auto model_file = ScopedFile::Open(model_path.string());
  EXPECT_TRUE(model_file.ok());
  LitertLmLoader loader(std::move(model_file.value()));
  EXPECT_FALSE(loader.GetHuggingFaceTokenizer());
  EXPECT_GT(loader.GetSentencePieceTokenizer()->Size(), 0);
  EXPECT_GT(loader.GetTFLiteModel(ModelType::kTfLitePrefillDecode).Size(), 0);
  EXPECT_GT(loader.GetLlmMetadata().Size(), 0);
  // Try to get non-existent TFLite model.
  EXPECT_EQ(loader.GetTFLiteModel(ModelType::kTfLiteEmbedder).Size(), 0);
}

TEST(LitertLmLoaderTest, InitializeWithHuggingFaceFile) {
  const auto model_path =
      std::filesystem::path(::testing::SrcDir()) /
      "litert_lm/runtime/testdata/test_hf_tokenizer.litertlm";
  auto model_file = ScopedFile::Open(model_path.string());
  ASSERT_TRUE(model_file.ok());
  LitertLmLoader loader(std::move(model_file.value()));
  ASSERT_GT(loader.GetHuggingFaceTokenizer()->Size(), 0);
  ASSERT_FALSE(loader.GetSentencePieceTokenizer());
}

TEST(LitertLmLoaderTest, InitializeWithMemoryMappedFile) {
  const auto model_path =
      std::filesystem::path(::testing::SrcDir()) /
      "litert_lm/runtime/testdata/test_lm.litertlm";
  ASSERT_OK_AND_ASSIGN(std::unique_ptr<MemoryMappedFile> mapped_file,
                       MemoryMappedFile::Create(model_path.string()));
  LitertLmLoader loader(std::move(mapped_file));
  EXPECT_FALSE(loader.GetHuggingFaceTokenizer());
  EXPECT_GT(loader.GetSentencePieceTokenizer()->Size(), 0);
  EXPECT_GT(loader.GetTFLiteModel(ModelType::kTfLitePrefillDecode).Size(), 0);
  EXPECT_GT(loader.GetLlmMetadata().Size(), 0);
  EXPECT_EQ(loader.GetTFLiteModel(ModelType::kTfLiteEmbedder).Size(), 0);
}

TEST(LitertLmLoaderTest, GetSectionLocationSizeMatch) {
  const auto model_path =
      std::filesystem::path(::testing::SrcDir()) /
      "litert_lm/runtime/testdata/test_lm.litertlm";
  ASSERT_OK_AND_ASSIGN(std::unique_ptr<MemoryMappedFile> mapped_file,
                       MemoryMappedFile::Create(model_path.string()));
  LitertLmLoader loader(std::move(mapped_file));

  BufferKey sp_key(schema::AnySectionDataType_SP_Tokenizer);
  ASSERT_OK_AND_ASSIGN(auto sp_location, loader.GetSectionLocation(sp_key));
  EXPECT_EQ(sp_location.second - sp_location.first,
            loader.GetSentencePieceTokenizer()->Size());

  BufferKey model_key(schema::AnySectionDataType_TFLiteModel,
                      ModelType::kTfLitePrefillDecode);
  ASSERT_OK_AND_ASSIGN(auto model_location,
                       loader.GetSectionLocation(model_key));
  EXPECT_EQ(model_location.second - model_location.first,
            loader.GetTFLiteModel(ModelType::kTfLitePrefillDecode).Size());

  BufferKey metadata_key(schema::AnySectionDataType_LlmMetadataProto);
  ASSERT_OK_AND_ASSIGN(auto metadata_location,
                       loader.GetSectionLocation(metadata_key));
  EXPECT_EQ(metadata_location.second - metadata_location.first,
            loader.GetLlmMetadata().Size());
}

}  // namespace
}  // namespace litert::lm