[Developer] #657 모델 파일명 .litertlm fix + 레거시 마이그레이션 + 에뮬레이터 dev 인프라

- ModelConfig.filename 기본값 gemma4_e2b_q4.bin → gemma-4-E2B-it.litertlm (#342 근본 원인)
- _migrateLegacyPath: 기존 .bin 설치본 rename + meta 갱신 (재다운로드 2.4GB 방지, 멱등)
- maxTokens 2048 유지 + 1024 회귀 방지 주석 (KV cache < prefill signature 시 추론 실패)
- LLM_BACKEND dart-define: 에뮬레이터 CPU 강제 (SwiftShader GPU 네이티브 SIGABRT 회피)
- GEMMA_MODEL_URL dart-define: 호스트 로컬 모델 서버 주입 (기본값 = HF URL 불변)
- debug 전용 cleartext manifest (10.0.2.2 모델 서버용, release 불변)
- 마이그레이션 테스트 4건 신규 (AC-2/3/4). 171 passed, analyze clean

2026-07-14 에뮬레이터 E2E 검증: 다운로드→로드→tool call 왕복 전 구간 성공.

Refs #657, #342

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-14 16:15:29 +09:00
parent b8e74d7dd9
commit 8b11d77c97
5 changed files with 142 additions and 6 deletions

View File

@@ -13,6 +13,20 @@ import 'llm_service.dart';
/// local file path generally does not require the token.
const String _hfToken = String.fromEnvironment('HF_TOKEN', defaultValue: '');
/// Backend override for dev builds. The Android emulator advertises a
/// software Vulkan adapter (SwiftShader) that SIGABRTs LiteRT-LM's GPU
/// init natively — the Dart-level gpu→cpu fallback can't catch a native
/// abort, so emulator runs must force CPU: `--dart-define=LLM_BACKEND=cpu`.
/// Unset (default) keeps the SDK's gpu→cpu fallback for real devices.
const String _backendOverride =
String.fromEnvironment('LLM_BACKEND', defaultValue: '');
PreferredBackend? get _preferredBackend => switch (_backendOverride) {
'cpu' => PreferredBackend.cpu,
'gpu' => PreferredBackend.gpu,
_ => null,
};
/// One-shot guard so [FlutterGemma.initialize] runs at most once per
/// isolate. Re-init is unsupported by the underlying plugin.
bool _initialized = false;
@@ -70,7 +84,15 @@ class GemmaLlmService implements LlmService {
modelType: ModelType.gemma4,
fileType: ModelFileType.litertlm,
).fromFile(modelPath).install();
final model = await FlutterGemma.getActiveModel(maxTokens: 2048);
// #342 root cause was the model *filename* (.bin — LiteRT-LM rejects it;
// must be .litertlm), NOT the KV cache size. maxTokens stays 2048: the
// Gemma 4 E2B compiled graph requires a cache ≥ its prefill signature —
// 1024 fails tensor allocation (DYNAMIC_UPDATE_SLICE prepare, verified
// on-emulator 2026-07-09).
final model = await FlutterGemma.getActiveModel(
maxTokens: 2048,
preferredBackend: _preferredBackend,
);
_model = model;
_loaded = true;
}