Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## Unreleased

- Add the `llamadart_extra_runtimes` build-hook key.
`llamadart_extra_runtimes: [stable_diffusion]` opts into image generation
and keeps the default `llama_cpp` and `litert_lm` runtimes, which naming
`stable_diffusion` in `llamadart_native_runtimes` drops
([#856](https://github.com/leehack/llamadart/issues/856)).
- Update the default LiteRT-LM runtime to
`leehack/litert-lm-native@v0.18.0` (upstream LiteRT-LM `v0.18.0`). On the
macOS GPU backend the program cache of Qwen3 0.6B and Gemma 4 E2B no longer
Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,13 @@ experimental and may change, and the runtime is opt-in.
hooks:
user_defines:
llamadart:
llamadart_native_runtimes: [llama_cpp, stable_diffusion]
llamadart_extra_runtimes: [stable_diffusion]
```

Keep `litert_lm` in the list if the app also loads `.litertlm` models.
The key adds to what `llamadart_native_runtimes` selects, so the default
`llama_cpp` and `litert_lm` runtimes stay. Naming `stable_diffusion` in
`llamadart_native_runtimes` bundles it too, but that list replaces the
defaults.

- **Flutter iOS/macOS apps** should add the
`llamadart_stable_diffusion_flutter` companion (see [Install](#install)),
Expand Down
4 changes: 4 additions & 0 deletions example/basic_app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,10 @@ hooks:
llamadart_native_runtimes: [llama_cpp, stable_diffusion]
```

That list also leaves out `litert_lm`, which no example here loads. An app that
keeps the default runtimes sets `llamadart_extra_runtimes: [stable_diffusion]`
instead.

The first run downloads the runtime (40 to 70 MB) through the build hook.
`ImageGenerationEngine.load` then downloads the files this example pins for
the preset in `lib/services/image_cli_options.dart`, unless a flag names a
Expand Down
5 changes: 4 additions & 1 deletion example/basic_app/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,8 @@ dev_dependencies:
hooks:
user_defines:
llamadart:
# The image example needs the opt-in stable_diffusion runtime.
# The image example needs the opt-in stable_diffusion runtime. Every
# example here loads GGUF, so this list also leaves out litert_lm; an
# app that keeps the default runtimes sets
# `llamadart_extra_runtimes: [stable_diffusion]` instead.
llamadart_native_runtimes: [llama_cpp, stable_diffusion]
7 changes: 4 additions & 3 deletions example/chat_app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,12 @@ macOS `14.0` or newer first.
This app bundles the `llama_cpp` and `litert_lm` native runtimes and declares
both Apple companion packages so GGUF and `.litertlm` presets work on supported
targets. It also opts into the experimental `stable_diffusion` runtime for the
image screen, which adds about 37 MB per iOS, macOS and Android arm64 target
image screen with `llamadart_extra_runtimes`, which keeps those defaults. The
runtime adds about 37 MB per iOS, macOS and Android arm64 target
and 38 to 72 MB on Linux and Windows (CPU or Vulkan build; Vulkan by default),
and declares `llamadart_stable_diffusion_flutter` so iOS and macOS link it
through SwiftPM. Android x64 has no `stable_diffusion` build and skips it with
a build warning; the Windows arm64 entry leaves it out. If you copy
through SwiftPM. Android x64 and Windows arm64 have no `stable_diffusion` build
and skip it with a build warning. If you copy
this app and only ship GGUF models, set `llamadart_native_runtimes` to
`[llama_cpp]` and keep only `llamadart_llama_cpp_flutter` to reduce bundle
size. Flutter iOS/macOS apps should declare the companion package for every
Expand Down
20 changes: 7 additions & 13 deletions example/chat_app/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,10 @@ hooks:
user_defines:
llamadart:
# The chat example intentionally demonstrates both GGUF and .litertlm
# model flows, plus the opt-in stable_diffusion runtime for its image
# screen. On iOS and macOS the companion packages above link these
# runtimes through Swift Package Manager instead. android-x64 has no
# stable_diffusion build and skips it with a build warning; the entries
# below leave out what is not published. Smaller examples rely on the
# package defaults.
llamadart_native_runtimes:
runtimes: [llama_cpp, litert_lm, stable_diffusion]
platforms:
# No LiteRT-LM bundle for these targets, nor stable_diffusion for
# windows-arm64.
ios-x86_64-sim: [llama_cpp, stable_diffusion]
windows-arm64: [llama_cpp]
# model flows, which the package defaults bundle, plus the opt-in
# stable_diffusion runtime for its image screen. On iOS and macOS the
# companion packages above link these runtimes through Swift Package
# Manager instead. A target without a stable_diffusion or LiteRT-LM
# build (android-x64, windows-arm64) skips that runtime with a build
# warning.
llamadart_extra_runtimes: [stable_diffusion]
12 changes: 8 additions & 4 deletions hook/build.dart
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ void main(List<String> args) async {
final pkgRoot = input.packageRoot.toFilePath();
final rawNativeRuntimeConfig =
input.userDefines[nativeRuntimesUserDefineKey];
final rawExtraRuntimeConfig = input.userDefines[extraRuntimesUserDefineKey];
final appleCompanions = _flutterAppleCompanions(
input: input,
code: code,
Expand All @@ -171,6 +172,7 @@ void main(List<String> args) async {
final configuredRuntimes = selectNativeRuntimesForBundle(
bundle: spec.bundle,
rawUserConfig: rawNativeRuntimeConfig,
rawExtraConfig: rawExtraRuntimeConfig,
warn: log.warning,
);
var selectedRuntimes = appleSpmRuntimes == null
Expand Down Expand Up @@ -217,10 +219,12 @@ void main(List<String> args) async {
)];
if (selectedRuntimes.contains(nativeRuntimeStableDiffusion) &&
stableDiffusionBundleSpec == null) {
if (nativeRuntimeNamedForExactBundle(
bundle: spec.bundle,
rawUserConfig: rawNativeRuntimeConfig,
runtime: nativeRuntimeStableDiffusion,
if ([rawNativeRuntimeConfig, rawExtraRuntimeConfig].any(
(rawConfig) => nativeRuntimeNamedForExactBundle(
bundle: spec.bundle,
rawUserConfig: rawConfig,
runtime: nativeRuntimeStableDiffusion,
),
)) {
throw Exception(
'stable_diffusion runtime is not available for ${spec.bundle}.',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ LlamaUnsupportedException stableDiffusionLoadFailure({
return LlamaUnsupportedException(
'stable_diffusion runtime is not bundled for $platform; add '
'stable_diffusion to hooks.user_defines.llamadart.'
'llamadart_native_runtimes$companionAdvice and rebuild.',
'llamadart_extra_runtimes$companionAdvice and rebuild.',
);
}
if (_vulkanLoaderNames.any(detail.toLowerCase().contains)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ StableDiffusionRuntimeStatus probeStableDiffusionRuntime() {
LlamaUnsupportedException(
'stable_diffusion runtime is not available on the web: it needs a '
'native platform (Android arm64, iOS, macOS, Linux or Windows x64) with '
'stable_diffusion in llamadart_native_runtimes.',
'stable_diffusion in llamadart_extra_runtimes.',
),
);
}
2 changes: 1 addition & 1 deletion lib/src/core/image/image_generation_engine.dart
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ class ImageGenerationTask {
/// stable_diffusion runtime (stable-diffusion.cpp).
///
/// The app must bundle the runtime by adding `stable_diffusion` to
/// `hooks.user_defines.llamadart.llamadart_native_runtimes`. It runs on
/// `hooks.user_defines.llamadart.llamadart_extra_runtimes`. It runs on
/// Android arm64 (CPU), iOS and macOS (Metal), and Linux and Windows (CPU or
/// Vulkan). On the web and other platforms [load] throws
/// [LlamaUnsupportedException].
Expand Down
85 changes: 69 additions & 16 deletions lib/src/hook/native_bundle_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
/// `hook/build.dart`. Pure decisions over already-discovered paths and
/// already-decoded config: nothing here downloads, extracts, or emits assets.
///
/// [nativeRuntimesUserDefineKey] picks the runtime families to build;
/// [nativeRuntimesUserDefineKey] picks the runtime families to build and
/// [extraRuntimesUserDefineKey] adds opt-in families to that pick;
/// [nativeBackendUserDefineKey] filters the split llama.cpp backend modules
/// within `llama_cpp`. Both scope by platform through a `platforms` map, or —
/// within `llama_cpp`. All scope by platform through a `platforms` map, or —
/// kept for backward compatibility — a bare map already keyed by platform.
///
/// Entry-point roles, deliberately not a call order `hook/build.dart` may
Expand Down Expand Up @@ -57,6 +58,11 @@ const String nativePathUserDefineKey = 'llamadart_native_path';
/// [selectNativeRuntimesForBundle].
const String nativeRuntimesUserDefineKey = 'llamadart_native_runtimes';

/// User-define key adding opt-in runtime families to whatever
/// [nativeRuntimesUserDefineKey] selects, so the defaults stay without being
/// restated. Decoded by [selectNativeRuntimesForBundle].
const String extraRuntimesUserDefineKey = 'llamadart_extra_runtimes';

/// The llama.cpp/GGUF runtime family; the only one with configurable backends.
const String nativeRuntimeLlamaCpp = 'llama_cpp';

Expand All @@ -76,10 +82,14 @@ const List<String> allNativeRuntimes = [
nativeRuntimeLiteRtLm,
];

/// The opt-in runtime families, the only ones [extraRuntimesUserDefineKey]
/// accepts.
const List<String> optInNativeRuntimes = [nativeRuntimeStableDiffusion];

/// Every runtime family a config may name.
const List<String> supportedNativeRuntimes = [
...allNativeRuntimes,
nativeRuntimeStableDiffusion,
...optInNativeRuntimes,
];

/// Fallback families for config that names no runtimes. An explicit `none`
Expand Down Expand Up @@ -562,28 +572,62 @@ List<String>? parseRequestedBackends({
/// at all — an unsupported scalar at the *root* included — the fallback to
/// [defaultNativeRuntimes] is silent when the key is unset or the config is
/// platform-scoped, and a [warn] otherwise.
///
/// [rawExtraConfig] (`llamadart_extra_runtimes`) then appends the
/// [optInNativeRuntimes] it names for [bundle] that the selection lacks. It
/// takes the same shapes, platform precedence and aliases, and `none` clears
/// it; unset, empty, or without an entry for [bundle] it adds nothing. Unlike
/// [rawUserConfig] it is strict: any other value, `all` and the default
/// families included, throws a [FormatException], because a dropped name there
/// would leave the app without a runtime it asked for.
List<String> selectNativeRuntimesForBundle({
required String bundle,
required Object? rawUserConfig,
Object? rawExtraConfig,
required void Function(String message) warn,
}) {
final extras = _extraNativeRuntimesForBundle(
bundle: bundle,
rawExtraConfig: rawExtraConfig,
);
final parsed = _parseNativeRuntimeConfigForBundle(
bundle: bundle,
rawUserConfig: rawUserConfig,
);
if (parsed == null) {
return defaultNativeRuntimes;
}

final invalid = parsed.invalid;
final invalid = parsed?.invalid ?? const <String>[];
if (invalid.isNotEmpty) {
warn(
'Ignoring unknown native runtime(s) for $bundle: ${invalid.join(', ')}. '
'Supported runtimes: ${supportedNativeRuntimes.join(', ')}.',
);
}

return parsed.runtimes;
final selected = parsed?.runtimes ?? defaultNativeRuntimes;
final added = extras.where((runtime) => !selected.contains(runtime));
return added.isEmpty ? selected : [...selected, ...added];
}

List<String> _extraNativeRuntimesForBundle({
required String bundle,
required Object? rawExtraConfig,
}) {
final parsed = _parseNativeRuntimeConfigForBundle(
bundle: bundle,
rawUserConfig: rawExtraConfig,
optInOnly: true,
);
if (parsed == null) {
return const [];
}
if (parsed.invalid.isNotEmpty) {
throw FormatException(
'hooks.user_defines.llamadart.$extraRuntimesUserDefineKey names '
'${parsed.invalid.join(', ')} for $bundle; it accepts only the opt-in '
'runtimes: ${optInNativeRuntimes.join(', ')}. Select '
'${allNativeRuntimes.join(' and ')} with $nativeRuntimesUserDefineKey.',
);
}
return parsed.explicit.toList(growable: false);
}

/// Whether [runtime] was named for [bundle], not implied by a default or by
Expand Down Expand Up @@ -795,6 +839,7 @@ List<String> selectBackendsForBundle({
_parseNativeRuntimeConfigForBundle({
required String bundle,
required Object? rawUserConfig,
bool optInOnly = false,
}) {
if (rawUserConfig == null) {
return null;
Expand All @@ -803,7 +848,7 @@ _parseNativeRuntimeConfigForBundle({
if (rawUserConfig is String ||
rawUserConfig is List<Object?> ||
rawUserConfig == false) {
return _parseRuntimeList(rawUserConfig);
return _parseRuntimeList(rawUserConfig, optInOnly: optInOnly);
}

final root = _toStringMap(rawUserConfig);
Expand All @@ -829,15 +874,15 @@ _parseNativeRuntimeConfigForBundle({
if (platformValue is Map<Object?, Object?>) {
final platformMap = _toStringMap(platformValue);
if (platformMap != null && platformMap.containsKey('runtimes')) {
return _parseRuntimeList(platformMap['runtimes']);
return _parseRuntimeList(platformMap['runtimes'], optInOnly: optInOnly);
}
return _parseRuntimeList(platformValue);
return _parseRuntimeList(platformValue, optInOnly: optInOnly);
}
return _parseRuntimeList(platformValue);
return _parseRuntimeList(platformValue, optInOnly: optInOnly);
}

if (globalValue != null) {
return _parseRuntimeList(globalValue);
return _parseRuntimeList(globalValue, optInOnly: optInOnly);
}

final hasRuntimeShape =
Expand Down Expand Up @@ -1226,8 +1271,10 @@ List<String> _parseBackendList(Object? value) {
return result;
}

/// With [optInOnly], a token naming anything but `none` or one of
/// [optInNativeRuntimes] is reported as invalid.
({List<String> runtimes, List<String> invalid, Set<String> explicit})
_parseRuntimeList(Object? value) {
_parseRuntimeList(Object? value, {bool optInOnly = false}) {
final result = <String>[];
final invalid = <String>[];
final explicit = <String>{};
Expand All @@ -1243,6 +1290,12 @@ _parseRuntimeList(Object? value) {
}
return;
}
if (optInOnly &&
normalized != 'none' &&
!optInNativeRuntimes.contains(normalized)) {
invalid.add(token.trim());
return;
}
if (normalized == 'all') {
for (final runtime in allNativeRuntimes) {
if (!result.contains(runtime)) {
Expand Down Expand Up @@ -1299,7 +1352,7 @@ _parseRuntimeList(Object? value) {
if (value is Map<Object?, Object?>) {
final mapped = _toStringMap(value);
if (mapped != null && mapped.containsKey('runtimes')) {
return _parseRuntimeList(mapped['runtimes']);
return _parseRuntimeList(mapped['runtimes'], optInOnly: optInOnly);
}
}

Expand Down
5 changes: 3 additions & 2 deletions skills/llamadart-flutter-app/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,9 @@ or shipping runtimes the app does not use.
user-defines do not change SwiftPM binaries either. The stable_diffusion
companion is independent: it selects image generation on iOS and macOS and
leaves llama.cpp and LiteRT-LM where they were.
- Image generation (`ImageGenerationEngine`) needs `stable_diffusion` added
to `llamadart_native_runtimes`; it is never bundled by default. On iOS and
- Image generation (`ImageGenerationEngine`) needs
`llamadart_extra_runtimes: [stable_diffusion]`, which keeps the default
runtimes; it is never bundled by default. On iOS and
macOS add `llamadart_stable_diffusion_flutter` instead, since App Store
Connect rejects the iOS framework the hook bundles. Details are in the
llamadart-image-generation skill.
Expand Down
6 changes: 4 additions & 2 deletions skills/llamadart-image-generation/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ description: >-
hooks:
user_defines:
llamadart:
llamadart_native_runtimes: [llama_cpp, stable_diffusion]
llamadart_extra_runtimes: [stable_diffusion]
```

Keep `litert_lm` in the list if the app also loads `.litertlm` models. Run
The key adds to what `llamadart_native_runtimes` selects, so the default
`llama_cpp` and `litert_lm` stay; naming `stable_diffusion` in
`llamadart_native_runtimes` instead replaces the defaults. Run
`flutter clean` once after changing it. Linux and Windows bundle the Vulkan
build when `llamadart_native_backends` selects Vulkan (the default);
`llamadart_stable_diffusion_backends: [cpu]` picks the CPU build.
Expand Down
Loading
Loading