@@ -59,10 +59,48 @@ absl::StatusOr<std::unique_ptr<MemoryMappedFile>> CreateMemoryMapFromScopedFile(
5959 " whole" );
6060}
6161
62- constexpr uint64_t kLitertLmHeaderMaxSize = 16 * 1024 ;
63-
6462} // namespace
6563
64+ absl::StatusOr<std::pair<BufferKey, std::optional<std::string>>>
65+ ExtractBufferKeyAndBackendConstraint (const schema::SectionObject* section) {
66+ auto items = section->items ();
67+ BufferKey buffer_key (section->data_type ());
68+ std::optional<std::string> backend_constraint;
69+ // Extract the specific model type from the section items KeyValuePairs.
70+ if (section->data_type () == schema::AnySectionDataType_TFLiteModel ||
71+ section->data_type () == schema::AnySectionDataType_TFLiteWeights) {
72+ bool found_model_type = false ;
73+ std::string model_type;
74+ for (size_t j = 0 ; j < items->size (); ++j) {
75+ auto item = items->Get (j);
76+ if (item->key () &&
77+ absl::AsciiStrToLower (item->key ()->str ()) == " model_type" &&
78+ item->value ()) {
79+ found_model_type = true ;
80+ model_type = *(item->value_as_StringValue ()->value ());
81+ }
82+ if (item->key () &&
83+ absl::AsciiStrToLower (item->key ()->str ()) == " backend_constraint" &&
84+ item->value ()) {
85+ backend_constraint = *(item->value_as_StringValue ()->value ());
86+ }
87+ }
88+ if (found_model_type) {
89+ ABSL_LOG (INFO) << " model_type: " << model_type;
90+ ASSIGN_OR_RETURN (ModelType model_type_enum,
91+ StringToModelType (model_type));
92+ buffer_key = BufferKey (section->data_type (), model_type_enum);
93+ } else {
94+ ABSL_LOG (WARNING) << " model_type not found, use kTfLitePrefillDecode" ;
95+ // For backward compatibility, we will use the default model type if
96+ // model_type is not found.
97+ buffer_key =
98+ BufferKey (section->data_type (), ModelType::kTfLitePrefillDecode );
99+ }
100+ }
101+ return std::make_pair (buffer_key, backend_constraint);
102+ }
103+
66104absl::Status LitertLmLoader::MapSection (BufferKey buffer_key,
67105 uint64_t begin_offset,
68106 uint64_t end_offset) {
@@ -149,7 +187,6 @@ absl::Status LitertLmLoader::Initialize() {
149187 ABSL_LOG (INFO) << " mmap_status is ok" ;
150188
151189 // Read the header information.
152- schema::LitertlmHeader header;
153190 absl::Status status =
154191 ReadHeaderFromLiteRTLM (header_data, header_size, &header_);
155192 ABSL_LOG (INFO) << " status: " << status;
@@ -164,44 +201,14 @@ absl::Status LitertLmLoader::Initialize() {
164201 auto sections = header_.metadata ->section_metadata ()->objects ();
165202 for (size_t i = 0 ; i < sections->size (); ++i) {
166203 const schema::SectionObject* section = sections->Get (i);
167- auto items = section->items ();
168- BufferKey buffer_key (section->data_type ());
169- // Extract the specific model type from the section items KeyValuePairs.
170- if (section->data_type () == schema::AnySectionDataType_TFLiteModel ||
171- section->data_type () == schema::AnySectionDataType_TFLiteWeights) {
172- bool found_model_type = false ;
173- std::string model_type;
174- std::string backend_constraint;
175- for (size_t j = 0 ; j < items->size (); ++j) {
176- auto item = items->Get (j);
177- if (item->key () &&
178- absl::AsciiStrToLower (item->key ()->str ()) == " model_type" &&
179- item->value ()) {
180- found_model_type = true ;
181- model_type = *(item->value_as_StringValue ()->value ());
182- }
183- if (item->key () &&
184- absl::AsciiStrToLower (item->key ()->str ()) == " backend_constraint" &&
185- item->value ()) {
186- backend_constraint = *(item->value_as_StringValue ()->value ());
187- }
188- }
189- if (found_model_type) {
190- ABSL_LOG (INFO) << " model_type: " << model_type;
191- ASSIGN_OR_RETURN (ModelType model_type_enum,
192- StringToModelType (model_type));
193- buffer_key = BufferKey (section->data_type (), model_type_enum);
194- } else {
195- ABSL_LOG (WARNING) << " model_type not found, use kTfLitePrefillDecode" ;
196- // For backward compatibility, we will use the default model type if
197- // model_type is not found.
198- buffer_key =
199- BufferKey (section->data_type (), ModelType::kTfLitePrefillDecode );
200- }
201- if (!backend_constraint.empty ()) {
202- section_backend_constraint_[buffer_key] = backend_constraint;
203- ABSL_LOG (INFO) << " section_backend_constraint: " << backend_constraint;
204- }
204+ ASSIGN_OR_RETURN (auto key_and_constraint,
205+ ExtractBufferKeyAndBackendConstraint (section));
206+ BufferKey buffer_key = key_and_constraint.first ;
207+ if (key_and_constraint.second .has_value () &&
208+ !key_and_constraint.second ->empty ()) {
209+ section_backend_constraint_[buffer_key] = *key_and_constraint.second ;
210+ ABSL_LOG (INFO) << " section_backend_constraint: "
211+ << *key_and_constraint.second ;
205212 }
206213 section_locations_[buffer_key] =
207214 std::make_pair (section->begin_offset (), section->end_offset ());
0 commit comments