Skip to content

Commit ad4b8a2

Browse files
ai-edge-botcopybara-github
authored andcommitted
Move the logic of creating engine settings and session config to CreateEngineAndSessionConfigs.
LiteRT-LM-PiperOrigin-RevId: 816766300
1 parent 81940ef commit ad4b8a2

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

runtime/engine/litert_lm_lib.cc

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -278,19 +278,12 @@ void RunScoreText(litert::lm::Engine* llm, litert::lm::Engine::Session* session,
278278

279279
} // namespace
280280

281-
absl::Status RunLiteRtLm(const LiteRtLmSettings& settings) {
281+
absl::StatusOr<EngineAndSessionConfigs> CreateEngineAndSessionConfigs(
282+
const LiteRtLmSettings& settings) {
282283
const std::string model_path = settings.model_path;
283284
if (model_path.empty()) {
284285
return absl::InvalidArgumentError("Model path is empty.");
285286
}
286-
287-
std::unique_ptr<tflite::profiling::memory::MemoryUsageMonitor> mem_monitor;
288-
if (settings.report_peak_memory_footprint) {
289-
mem_monitor =
290-
std::make_unique<tflite::profiling::memory::MemoryUsageMonitor>(
291-
kMemoryCheckIntervalMs);
292-
mem_monitor->Start();
293-
}
294287
ABSL_LOG(INFO) << "Model path: " << model_path;
295288
ASSIGN_OR_RETURN(ModelAssets model_assets, // NOLINT
296289
ModelAssets::Create(model_path));
@@ -403,6 +396,28 @@ absl::Status RunLiteRtLm(const LiteRtLmSettings& settings) {
403396
engine_settings.GetMutableBenchmarkParams() = benchmark_params;
404397
}
405398

399+
return EngineAndSessionConfigs{
400+
.engine_settings = std::move(engine_settings),
401+
.session_config = std::move(session_config),
402+
};
403+
}
404+
405+
absl::Status RunLiteRtLm(const LiteRtLmSettings& settings) {
406+
407+
std::unique_ptr<tflite::profiling::memory::MemoryUsageMonitor> mem_monitor;
408+
if (settings.report_peak_memory_footprint) {
409+
mem_monitor =
410+
std::make_unique<tflite::profiling::memory::MemoryUsageMonitor>(
411+
kMemoryCheckIntervalMs);
412+
mem_monitor->Start();
413+
}
414+
415+
// Reuse the shared logic to get the engine settings and session config.
416+
ASSIGN_OR_RETURN(EngineAndSessionConfigs configs,
417+
CreateEngineSettings(settings));
418+
auto& engine_settings = configs.engine_settings;
419+
auto& session_config = configs.session_config;
420+
406421
ABSL_LOG(INFO) << "Creating engine";
407422
ASSIGN_OR_RETURN(auto engine,
408423
litert::lm::Engine::CreateEngine(std::move(engine_settings),

runtime/engine/litert_lm_lib.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,18 @@
2121
#include <vector>
2222

2323
#include "absl/status/status.h" // from @com_google_absl
24+
#include "absl/status/statusor.h" // from @com_google_absl
25+
#include "runtime/engine/engine_settings.h"
2426

2527
namespace litert {
2628
namespace lm {
2729

30+
// Configs for the engine and session.
31+
struct EngineAndSessionConfigs {
32+
EngineSettings engine_settings;
33+
SessionConfig session_config;
34+
};
35+
2836
struct LiteRtLmSettings {
2937
std::string backend = "gpu";
3038
std::optional<std::string> vision_backend = std::nullopt;
@@ -57,6 +65,10 @@ struct LiteRtLmSettings {
5765
bool disable_cache = false;
5866
};
5967

68+
// Creates the engine settings and session config from the given settings.
69+
absl::StatusOr<EngineAndSessionConfigs> CreateEngineAndSessionConfigs(
70+
const LiteRtLmSettings& settings);
71+
6072
absl::Status RunLiteRtLm(const LiteRtLmSettings& settings);
6173

6274
} // namespace lm

0 commit comments

Comments
 (0)