From 70ec1f7de703c9677cedfe89aef1fde255b724a0 Mon Sep 17 00:00:00 2001 From: Han Yin Date: Fri, 27 Jun 2025 11:58:29 -0700 Subject: [PATCH] di: add a stub TierDetection; provide both actual impl and stub in AppModule --- .../java/com/example/llama/di/AppModule.kt | 11 +++++++++++ .../example/llama/engine/StubTierDetection.kt | 19 +++++++++++++++++++ .../llama/viewmodel/SettingsViewModel.kt | 6 ++++++ 3 files changed, 36 insertions(+) create mode 100644 examples/llama.android/app/src/main/java/com/example/llama/engine/StubTierDetection.kt diff --git a/examples/llama.android/app/src/main/java/com/example/llama/di/AppModule.kt b/examples/llama.android/app/src/main/java/com/example/llama/di/AppModule.kt index 658096617e..ff53da9d62 100644 --- a/examples/llama.android/app/src/main/java/com/example/llama/di/AppModule.kt +++ b/examples/llama.android/app/src/main/java/com/example/llama/di/AppModule.kt @@ -3,6 +3,7 @@ package com.example.llama.di import android.content.Context import android.llama.cpp.InferenceEngine import android.llama.cpp.KleidiLlama +import android.llama.cpp.TierDetection import android.llama.cpp.gguf.GgufMetadataReader import com.example.llama.data.local.AppDatabase import com.example.llama.data.remote.HuggingFaceApiService @@ -18,6 +19,7 @@ import com.example.llama.engine.InferenceService import com.example.llama.engine.InferenceServiceImpl import com.example.llama.engine.ModelLoadingService import com.example.llama.engine.StubInferenceEngine +import com.example.llama.engine.StubTierDetection import com.example.llama.monitoring.PerformanceMonitor import dagger.Binds import dagger.Module @@ -71,6 +73,15 @@ internal abstract class AppModule { } } + @Provides + fun provideTierDetection(@ApplicationContext context: Context): TierDetection { + return if (USE_STUB_ENGINE) { + StubTierDetection + } else { + KleidiLlama.getTierDetection(context) + } + } + @Provides fun providePerformanceMonitor(@ApplicationContext context: Context) = PerformanceMonitor(context) diff --git a/examples/llama.android/app/src/main/java/com/example/llama/engine/StubTierDetection.kt b/examples/llama.android/app/src/main/java/com/example/llama/engine/StubTierDetection.kt new file mode 100644 index 0000000000..b120ad2c41 --- /dev/null +++ b/examples/llama.android/app/src/main/java/com/example/llama/engine/StubTierDetection.kt @@ -0,0 +1,19 @@ +package com.example.llama.engine + +import android.llama.cpp.LLamaTier +import android.llama.cpp.TierDetection +import android.util.Log + +/** + * A stub [TierDetection] for agile development & testing + */ +object StubTierDetection : TierDetection { + private val tag = StubTierDetection::class.java.simpleName + + override val detectedTier: LLamaTier? + get() = LLamaTier.T2 + + override fun clearCache() { + Log.d(tag, "Cache cleared") + } +} diff --git a/examples/llama.android/app/src/main/java/com/example/llama/viewmodel/SettingsViewModel.kt b/examples/llama.android/app/src/main/java/com/example/llama/viewmodel/SettingsViewModel.kt index 42011d5162..7105d57c80 100644 --- a/examples/llama.android/app/src/main/java/com/example/llama/viewmodel/SettingsViewModel.kt +++ b/examples/llama.android/app/src/main/java/com/example/llama/viewmodel/SettingsViewModel.kt @@ -1,5 +1,7 @@ package com.example.llama.viewmodel +import android.llama.cpp.LLamaTier +import android.llama.cpp.TierDetection import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.example.llama.data.preferences.UserPreferences @@ -26,6 +28,7 @@ class SettingsViewModel @Inject constructor( private val userPreferences: UserPreferences, private val performanceMonitor: PerformanceMonitor, private val modelRepository: ModelRepository, + private val tierDetection: TierDetection, ) : ViewModel() { // Storage usage metrics @@ -57,6 +60,9 @@ class SettingsViewModel @Inject constructor( private val _themeMode = MutableStateFlow(UserPreferences.THEME_MODE_AUTO) val themeMode: StateFlow = _themeMode.asStateFlow() + val detectedTier: LLamaTier? + get() = tierDetection.detectedTier + init { viewModelScope.launch { // Load user preferences