Skip to content
Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Unreleased

- Exclude Android Adreno 750 driver `2150604839` from llama.cpp Vulkan loads: automatic selection falls back to CPU with a warning, and an explicit GPU request throws instead of crashing or producing incorrect text.

- 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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ Android deployments, NPU. See the
[support matrix](https://llamadart.leehack.com/docs/platforms/support-matrix)
for each target.

Android llama.cpp excludes the known unsafe Adreno 750 driver `2150604839`:
explicit GPU requests throw, while automatic selection with a Vulkan
preference falls back to CPU with a warning. See the
[device limitations](https://llamadart.leehack.com/docs/platforms/support-matrix#known-limitations).

## Start Here

| Need | Link |
Expand Down
2 changes: 1 addition & 1 deletion example/basic_app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ A clean, organized CLI application demonstrating the capabilities of the `llamad
- **Single Response Mode**: Pass a prompt as an argument for quick tasks.
- **Automatic Model Management**: Accepts local paths, HTTP(S) URLs, and `hf://` Hugging Face model sources, then resolves remote sources through the package-managed cache.
- **Platform Cache Defaults**: Remote model URLs use `DefaultModelDownloadManager`, so desktop/server runs share the per-user `llamadart` model cache while explicit local paths are loaded directly.
- **Backend Optimization**: Defaults to GPU acceleration (Metal/Vulkan) when available.
- **Backend Optimization**: Defaults to GPU acceleration (Metal/Vulkan) when available on desktop and CPU for Android llama.cpp. Android Adreno 750 driver `2150604839` is excluded from Vulkan: automatic selection with a Vulkan preference uses CPU with a warning; explicit GPU requests throw.
- **LoRA Adapters**: Load one or more LoRA adapters with repeated `--lora` flags.
- **Structured Output**: Pass `--grammar` for GBNF-constrained generation.
- **Tool Calling Test Mode**: Enable `--tool-test` to exercise function-calling flow.
Expand Down
3 changes: 2 additions & 1 deletion lib/src/backends/llama_cpp/llama_cpp_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2104,12 +2104,13 @@ class LlamaCppService {
required int splitMode,
required int mainGpu,
}) => resolveVulkanLoadDecision(
usesGpu: gpuLayers > 0 && backend != GpuBackend.cpu,
usesGpu: gpuLayers != 0 && backend != GpuBackend.cpu,
backendRegistry: ggmlGpuRegistryName(backend),
splitMode: splitMode,
mainGpu: mainGpu,
registered: _registeredDevices,
probe: () => _vulkanDeviceProbe,
isAndroid: _isAndroid,
);

/// The registered ggml devices, in registry order.
Expand Down
39 changes: 31 additions & 8 deletions lib/src/backends/llama_cpp/load_device_selection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ final class VulkanLoadDecision {

/// The usable devices to list for the load in place of the selected ones;
/// `null` when no listed load can avoid the unusable devices, which is when
/// every selected device is one, and when nothing changes.
/// every selected device is one, or an unsafe Android driver is registered,
/// and when nothing changes.
final List<GgmlDeviceEntry>? devices;

/// Whether no usable device is left, so the load cannot run on the GPU.
Expand Down Expand Up @@ -152,25 +153,26 @@ VulkanDeviceFacts? vulkanFactsOf(
return named.first;
}

/// Decides what a model load does about Vulkan devices below Vulkan 1.2.
/// Decides what a model load does about Vulkan devices below Vulkan 1.2 or,
/// on [isAndroid], the known unsafe Adreno 750 driver.
///
/// [usesGpu] is false for a load that offloads nothing, which never starts a
/// device. [backendRegistry] is the ggml registry of an explicit GPU backend,
/// whose devices the load lists (`llama_model_params.devices`) when it has
/// any, or `null` when llama.cpp selects among every registered device.
///
/// Only the devices the load would use count: a registered device that is
/// not selected is never initialized (`ggml_backend_vk_reg_get_device` only
/// describes it). [registered] is read only for a load that [usesGpu], and
/// [probe] only when a Vulkan device is selected. A device whose facts
/// [vulkanFactsOf] cannot tell is taken as usable.
/// Vulkan API floors apply to selected model devices. On Android the driver
/// guard also covers registered devices that auxiliary GPU selectors could
/// choose independently. [registered] is read only for a load that [usesGpu].
/// A device whose facts cannot be matched is taken as usable.
VulkanLoadDecision resolveVulkanLoadDecision({
required bool usesGpu,
required String? backendRegistry,
required int splitMode,
required int mainGpu,
required List<GgmlDeviceEntry> Function() registered,
required VulkanDeviceProbe Function() probe,
bool isAndroid = false,
}) {
if (!usesGpu) return VulkanLoadDecision.unchanged;
final devices = registered();
Expand All @@ -186,12 +188,33 @@ VulkanLoadDecision resolveVulkanLoadDecision({
mainGpu: mainGpu,
);

if (!selected.any((device) => device.isVulkan)) {
final inspected = isAndroid ? devices : selected;
if (!inspected.any((device) => device.isVulkan)) {
return VulkanLoadDecision.unchanged;
}
final facts = probe().devices;
if (facts == null) return VulkanLoadDecision.unchanged;

// Projector and decision-head GPU selection is independent of the model's
// list. Refuse GPU work if any registered Android device has this driver.
if (isAndroid) {
for (final device in devices.where((device) => device.isVulkan)) {
if (facts.any(
(fact) =>
fact.name == device.description &&
fact.isIntegratedGpu == (device.type == _typeIgpu) &&
fact.hasAdreno750DriverDefect,
)) {
return VulkanLoadDecision._(
unsupported:
'"${device.description}" (${device.name}) on Android driver '
'2150604839 has known llama.cpp Vulkan shader-compiler crashes '
'and incorrect quantized results. GPU work is excluded until '
'a native workaround and the model are qualified',
);
}
}
}
String? unsupported;
final usable = <GgmlDeviceEntry>[];
for (final device in selected) {
Expand Down
21 changes: 21 additions & 0 deletions lib/src/backends/llama_cpp/vulkan_device_probe.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ final class VulkanDeviceFacts {
required this.instanceApiVersion,
required this.apiVersion,
required this.subgroupSize,
this.vendorId = 0,
this.driverVersion = 0,
});

/// Vulkan 1.2 as `VK_MAKE_API_VERSION` encodes it.
Expand All @@ -46,6 +48,23 @@ final class VulkanDeviceFacts {
/// or the driver is older than Vulkan 1.1 and cannot report it.
final int subgroupSize;

/// `VkPhysicalDeviceProperties.vendorID`, or 0 when not reported.
final int vendorId;

/// The vendor-specific `VkPhysicalDeviceProperties.driverVersion`.
final int driverVersion;

/// The Android Adreno 750 driver with shader-compiler crashes and
/// incorrect quantized matrix-vector results (llamadart-native #79).
///
/// A crash workaround alone does not qualify this driver for inference;
/// common K-quants remain incorrect. Other driver versions are unaffected
/// by this narrowly matched guard.
bool get hasAdreno750DriverDefect =>
vendorId == 0x5143 &&
driverVersion == 2150604839 &&
name == 'Adreno (TM) 750';

/// Whether ggml-vulkan can drive the device.
///
/// ggml-vulkan registers no device when the loader is below Vulkan 1.2
Expand Down Expand Up @@ -196,6 +215,8 @@ final class VulkanDeviceInfoApi {
instanceApiVersion: info.ref.instance_api_version,
apiVersion: info.ref.api_version,
subgroupSize: info.ref.subgroup_size,
vendorId: info.ref.vendor_id,
driverVersion: info.ref.driver_version,
),
);
}
Expand Down
3 changes: 3 additions & 0 deletions lib/src/core/models/inference/model_params.dart
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ class ModelParams {
/// a GPU or NPU that fails to start throws `LlamaUnsupportedException`
/// there. [validate] rejects a device that contradicts [preferredBackend],
/// [gpuLayers], [mainGpu] or [liteRtLmBackend].
/// Android llama.cpp excludes the known unsafe Adreno 750 driver
/// `2150604839`: [ComputeDevice.gpu] throws, while automatic selection
/// with a Vulkan preference falls back to CPU with a warning.
final ComputeDevice device;

/// Number of model layers to offload to the GPU (n_gpu_layers).
Expand Down
5 changes: 4 additions & 1 deletion skills/llamadart-flutter-app/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,10 @@ or shipping runtimes the app does not use.
- Android: llama.cpp ships `cpu` and `vulkan` by default; `opencl` is opt-in
through `llamadart_native_backends`. llama.cpp `ComputeDevice.auto` stays
on the CPU there; Vulkan is experimental and device-dependent, so request
`ComputeDevice.gpu` only on devices the app has validated. Leave
`ComputeDevice.gpu` only on devices the app has validated. Adreno 750
driver `2150604839` is excluded: an explicit GPU request throws; automatic
selection with a Vulkan preference falls back to CPU with a warning.
A shader-compiler workaround alone does not qualify quantized output. Leave
`ModelParams.microBatchSize` unset on Android Vulkan: on a GPU whose
subgroup size is 16, below 8 or unreadable a text prompt is then decoded at
most 8 tokens at a time, and a value above 32 returns wrong text there. The
Expand Down
76 changes: 76 additions & 0 deletions test/unit/backends/llama_cpp/llama_cpp_service_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5616,9 +5616,11 @@ void main() {
List<GgmlDeviceEntry> registered,
List<VulkanDeviceFacts>? facts, {
LlamaCppObjectCalls? objectCalls,
bool isAndroid = false,
}) {
reads = 0;
final service = LlamaCppService(
isAndroid: isAndroid,
deviceTypes: (_) => [gpuType],
objectCalls: objectCalls,
registeredDevices: () => registered,
Expand All @@ -5633,6 +5635,80 @@ void main() {
return service;
}

for (final gpuLayers in [ModelParams.maxGpuLayers, -1]) {
test(
'Android Adreno guard refuses explicit GPU and loads auto on CPU '
'before a native model call can use the unsafe device ($gpuLayers layers)',
() {
final adreno = entry('Vulkan0', description: 'Adreno (TM) 750');
const bad = VulkanDeviceFacts(
name: 'Adreno (TM) 750',
deviceType: 2,
instanceApiVersion: vulkan13,
apiVersion: vulkan13,
subgroupSize: 64,
vendorId: 0x5143,
driverVersion: 2150604839,
);
// Metal supplies the host's real GPU module; the injected registry
// and facts supply Android's selected Vulkan device. No driver is
// initialized by the probe and the guarded load must use the CPU.
final explicit = serviceWith([adreno], [bad], isAndroid: true);
expect(
() => explicit.loadModel(
modelPath,
ModelParams(
gpuLayers: gpuLayers,
contextSize: 64,
device: ComputeDevice.gpu,
preferredBackend: GpuBackend.metal,
),
),
throwsA(
isA<LlamaUnsupportedException>().having(
(e) => e.message,
'message',
contains('driver 2150604839'),
),
),
);
expect(_readPrivateForTesting<Map>(explicit, '_models'), isEmpty);
final automatic = serviceWith([adreno], [bad], isAndroid: true);
final model = automatic.loadModel(
modelPath,
ModelParams(
gpuLayers: gpuLayers,
contextSize: 64,
preferredBackend: GpuBackend.metal,
),
);
expect(automatic.getResolvedGpuLayers(), 0);
expect(automatic.getActiveBackendName(), 'CPU');
expect(
warnings,
contains(
allOf(
contains('driver 2150604839'),
contains('so the model loaded on CPU with 0 GPU layers'),
),
),
);
final context = automatic.createContext(
model,
const ModelParams(contextSize: 64),
);
final params =
_readPrivateForTesting<Map<int, llama_context_params>>(
automatic,
'_contextParams',
)[context]!;
expect(params.op_offload, isFalse);
expect(params.offload_kqv, isFalse);
},
testOn: 'mac-os',
);
}

Matcher noVulkanWarning() => isNot(contains(contains('Vulkan 1.2')));

test('ComputeDevice.gpu throws and loads nothing when the only '
Expand Down
63 changes: 63 additions & 0 deletions test/unit/backends/llama_cpp/load_device_selection_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,10 @@ void main() {
bool usesGpu = true,
int splitMode = _layer,
int mainGpu = 0,
bool isAndroid = false,
}) => resolveVulkanLoadDecision(
usesGpu: usesGpu,
isAndroid: isAndroid,
backendRegistry: backend,
splitMode: splitMode,
mainGpu: mainGpu,
Expand Down Expand Up @@ -301,6 +303,67 @@ void main() {
final maliOld = _fact('Mali-G68', _vulkan11);
final mali = _device('Vulkan0', description: 'Mali-G68');

test(
'refuses Android GPU loads when the affected driver is registered',
() {
final adreno = _device('Vulkan0', description: 'Adreno (TM) 750');
const bad = VulkanDeviceFacts(
name: 'Adreno (TM) 750',
deviceType: 2,
instanceApiVersion: _vulkan13,
apiVersion: _vulkan13,
subgroupSize: 64,
vendorId: 0x5143,
driverVersion: 2150604839,
);
expect(
decide([adreno], _facts([bad]), isAndroid: true),
refused('driver 2150604839'),
);
expect(decide([adreno], _facts([bad])), unchanged());
expect(
decide([adreno], _facts([bad]), isAndroid: true, usesGpu: false),
unchanged(),
);
expect(
decide([adreno, cuda], _facts([bad]), isAndroid: true),
refused('driver 2150604839'),
);
expect(
decide(
[adreno, rtx],
_facts([bad, rtxOk]),
isAndroid: true,
splitMode: _none,
mainGpu: 1,
),
refused('driver 2150604839'),
);
final newer = VulkanDeviceFacts(
name: bad.name,
deviceType: bad.deviceType,
instanceApiVersion: bad.instanceApiVersion,
apiVersion: bad.apiVersion,
subgroupSize: bad.subgroupSize,
vendorId: bad.vendorId,
driverVersion: bad.driverVersion + 1,
);
expect(decide([adreno], _facts([newer]), isAndroid: true), unchanged());
expect(
decide([adreno], _facts([bad, newer]), isAndroid: true),
refused('driver 2150604839'),
);
expect(
decide(
[adreno],
() => const VulkanDeviceProbe.unavailable('missing'),
isAndroid: true,
),
unchanged(),
);
},
);

test('a load that offloads nothing reads neither the registry nor the '
'facts', () {
expect(decide([mali], _facts([maliOld]), usesGpu: false), unchanged());
Expand Down
Loading
Loading