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
5 changes: 3 additions & 2 deletions doc/testing_matrix.md
Original file line number Diff line number Diff line change
Expand Up @@ -777,8 +777,9 @@ audio bytes.

`gemma4-mtp-smoke` takes the Gemma 4 GGUF and its external drafter via
`--draft-model-path`. Inspect the JSON for plain output and either MTP output
or an MTP error. The command can exit successfully after reporting an MTP
error, so use it for manual inspection. For MTP and n-gram performance
or an MTP error. Omit the drafter path for a target with embedded MTP.
The smoke exits nonzero with a typed JSON error when the MTP turn fails
or the drafter runs and proposes zero tokens. For MTP and n-gram performance
numbers, use `llama-cpp-speculative-benchmark` with
`--speculative-cases draft-mtp,ngram-simple`, which supersedes the older
env-var-driven benchmark script. Like every `local-only` row, neither runs in CI.
Expand Down
22 changes: 14 additions & 8 deletions test/unit/backends/llama_cpp/library_probe_call_sites_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -604,14 +604,20 @@ void main() {
expect(
() => _invoke<Object?>(service, '_resolveSpeculativeApi', const []),
throwsA(
isA<LlamaUnsupportedException>().having(
(error) => error.message,
'message',
'llama.cpp speculative decoding is unavailable in this native '
'runtime bundle (missing llama_dart_speculative_* wrapper '
'symbols). Use the package-pinned native runtime or an '
'ABI-compatible build.',
),
isA<LlamaUnsupportedException>()
.having(
(error) => error.runtimeType,
'runtimeType',
LlamaUnsupportedException,
)
.having(
(error) => error.message,
'message',
'llama.cpp speculative decoding is unavailable in this native '
'runtime bundle (missing llama_dart_speculative_* wrapper '
'symbols). Use the package-pinned native runtime or an '
'ABI-compatible build.',
),
),
);
});
Expand Down
14 changes: 9 additions & 5 deletions test/unit/backends/llama_cpp/llama_cpp_backend_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1899,11 +1899,15 @@ void main() {
throwsA(
allOf(
isA<LlamaUnsupportedException>(),
isA<LlamaSpeculativeDecodingUnsupportedException>().having(
(error) => error.strategies,
'strategies',
[strategy],
),
isA<LlamaSpeculativeDecodingUnsupportedException>()
.having(
(error) => error.runtimeType,
'runtimeType',
LlamaSpeculativeDecodingUnsupportedException,
)
.having((error) => error.strategies, 'strategies', [
strategy,
]),
),
),
);
Expand Down
24 changes: 18 additions & 6 deletions test/unit/backends/llama_cpp/llama_cpp_service_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1379,7 +1379,13 @@ void main() {
speculativeDecodingConfig: SpeculativeDecodingConfig.ngramMod(),
),
),
throwsA(isA<LlamaUnsupportedException>()),
throwsA(
isA<LlamaUnsupportedException>().having(
(error) => error.runtimeType,
'runtimeType',
LlamaUnsupportedException,
),
),
);
});

Expand Down Expand Up @@ -1621,11 +1627,17 @@ void main() {
const ModelParams(),
),
throwsA(
isA<LlamaUnsupportedException>().having(
(error) => error.message,
'message',
contains('ModelParams(loadMtp: true)'),
),
isA<LlamaUnsupportedException>()
.having(
(error) => error.runtimeType,
'runtimeType',
LlamaUnsupportedException,
)
.having(
(error) => error.message,
'message',
contains('ModelParams(loadMtp: true)'),
),
),
);
expect(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,11 +296,17 @@ void main() {
),
),
throwsA(
isA<LlamaUnsupportedException>().having(
(e) => e.message,
'message',
allOf(contains('topLogprobs'), contains('speculative')),
),
isA<LlamaUnsupportedException>()
.having(
(error) => error.runtimeType,
'runtimeType',
LlamaUnsupportedException,
)
.having(
(e) => e.message,
'message',
allOf(contains('topLogprobs'), contains('speculative')),
),
),
);
});
Expand Down
49 changes: 47 additions & 2 deletions test/unit/tooling/gemma4_mtp_smoke_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,48 @@ void main() {
});
}

for (final (attempts, proposals, expectedCode) in [
(1, 0, 1),
(0, 0, 0),
(1, 2, 0),
]) {
test(
'MTP exits $expectedCode after $attempts attempts and $proposals proposals',
() async {
final engine = _SmokeEngine(
perf: BackendPerfContextData(
loadMs: 0,
promptEvalMs: 0,
evalMs: 0,
sampleMs: 0,
promptEvalTokens: 0,
evalTokens: 0,
sampleCount: 0,
reusedGraphs: 0,
speculativeDraftAttempts: attempts,
speculativeDraftTokens: proposals,
speculativeAcceptedDraftTokens: 0,
),
);
final (code, report) = await _runSmoke([
'target.gguf',
'draft.gguf',
], engine);

expect(code, expectedCode);
final mtp = report['mtp'] as Map<String, dynamic>;
expect(mtp['perf'], containsPair('speculativeDraftTokens', proposals));
if (expectedCode == 1) {
expect(mtp['errorType'], 'NoDraftTokensProposed');
expect(mtp['error'], contains('proposed zero tokens'));
} else {
expect(mtp, isNot(contains('errorType')));
}
expect(engine.disposed, isTrue);
},
);
}

test('reports a typed MTP error and exits nonzero', () async {
final engine = _SmokeEngine(failMtp: true);
final (code, report) = await _runSmoke(['target.gguf'], engine);
Expand Down Expand Up @@ -69,7 +111,9 @@ Future<(int, Map<String, dynamic>)> _runSmoke(
}

class _SmokeEngine implements LlamaEngine {
_SmokeEngine({this.failMtp = false});
_SmokeEngine({this.failMtp = false, this.perf});

final BackendPerfContextData? perf;

final bool failMtp;
late ModelParams modelParams;
Expand All @@ -93,7 +137,8 @@ class _SmokeEngine implements LlamaEngine {
}

@override
Future<BackendPerfContextData?> getPerformanceContext() async => null;
Future<BackendPerfContextData?> getPerformanceContext() async =>
turns.last == null ? null : perf;

@override
Stream<LlamaCompletionChunk> create(
Expand Down
12 changes: 9 additions & 3 deletions tool/testing/gemma4_mtp_smoke.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ Future<int> runGemma4MtpSmoke(
),
);

var result = 0;
Map<String, Object?> mtp;
try {
mtp = await _run(
Expand All @@ -87,7 +86,6 @@ Future<int> runGemma4MtpSmoke(
),
);
} catch (error, stackTrace) {
result = 1;
mtp = {
'error': error.toString(),
'errorType': error.runtimeType.toString(),
Expand All @@ -106,7 +104,7 @@ Future<int> runGemma4MtpSmoke(
'mtp': mtp,
}),
);
return result;
return mtp.containsKey('errorType') ? 1 : 0;
} finally {
await engine.dispose();
}
Expand Down Expand Up @@ -138,7 +136,15 @@ Future<Map<String, Object?>> _run(

final text = output.toString();
final perf = await engine.getPerformanceContext();
final noDraftTokensProposed =
params.isSpeculativeDecodingEnabled &&
(perf?.speculativeDraftAttempts ?? 0) > 0 &&
perf?.speculativeDraftTokens == 0;
return {
if (noDraftTokensProposed) ...{
'errorType': 'NoDraftTokensProposed',
'error': 'The MTP drafter ran but proposed zero tokens.',
},
'elapsedMs': stopwatch.elapsedMilliseconds,
'firstTokenLatencyMs': firstTokenLatencyMs,
'outputChars': text.length,
Expand Down
3 changes: 2 additions & 1 deletion tool/testing/test_matrix.dart
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,8 @@ const List<TestMatrixRow> testMatrixRows = <TestMatrixRow>[
'--benchmark-max-tokens 32 --draft-token-max 1',
useWhen:
'Omit --draft-model-path for a target with embedded MTP. The smoke '
'exits nonzero and reports the error in JSON if the MTP turn fails. '
'exits nonzero and reports a typed JSON error if the MTP turn fails '
'or the drafter runs and proposes zero tokens. '
'Run locally before the llama-cpp-speculative-benchmark row.',
),
TestMatrixRow(
Expand Down
3 changes: 2 additions & 1 deletion website/docs/guides/performance-tuning.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ chain then selects and accepts the token, preserving greedy output and seeded
sampling. The benefit depends on how often the model's shortlist passes.

Older runtimes and requests with lazy grammar, a thinking budget, DRY,
top-n-sigma, Mirostat or requested token logprobs keep their existing paths.
top-n-sigma, Mirostat, requested token logprobs, or speculative decoding keep
their existing paths.

## Speculative decoding

Expand Down
Loading