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
1 change: 1 addition & 0 deletions doc/testing_matrix.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ Pick targeted rows based on the touched surface:
| LiteRT-LM web / Gemma 4 web bundle | `gemma4-litert-web` |
| Chat app model cache/download/projector | `chat-app-device-cache` |
| LiteRT-LM engine reload memory on a device | `chat-app-litert-reload-memory` |
| Android Vulkan prompt cap, Vulkan device facts or the Vulkan 1.2 load refusal on a device | `chat-app-micro-batch-sweep` |
| Isolate shutdown releases, exit teardown, a Flutter pin bump, or app-exit guidance | `chat-app-macos-quit` |
| Speech-to-text API or adapter | `speech-to-text-smoke`, `web-speech-to-text-smoke`, plus `litert-lm-asr-smoke` for the dedicated LiteRT-LM streaming engine |
| Text-to-speech API or adapter | `text-to-speech-smoke`, plus `web-text-to-speech-smoke` for browser synthesis/playback/export |
Expand Down
92 changes: 92 additions & 0 deletions example/chat_app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -807,3 +807,95 @@ temperature 0, seed 1). The matching build-time defaults are
`LITERT_RELOAD_TEMPERATURE` and `LITERT_RELOAD_SEED`. An argument passed as an
empty string keeps its default. Fewer than three iterations record the steps
without the memory check.

#### Micro-batch sweep and the Android Vulkan prompt cap

`integration_test/micro_batch_sweep_e2e_test.dart` loads the locked
`chat-gguf-vulkan` model at several micro-batch sizes in one process and sends
a short history prompt, a long history prompt and a tool request with
`ToolChoice.auto` and `ToolChoice.required` at each. Size `0` is the library
default and the only one the test judges, so the sizes must include it: a
default attempt must load, answer both history prompts with the code and
nothing else, and end both tool requests as the `get_weather` call.

Every other size is a control that is only recorded. An explicit size, even
`512` (the default `n_ubatch`), makes the library leave the Android Vulkan
prompt cap off, so `0+512` compares the capped default with the same context
uncapped. On a Vulkan device with the small matmul tile defect the control is
expected to answer wrongly (a Pixel 9 Pro's Mali-G715, subgroup size 16,
answered `8888...`); a correct control there says the cap is no longer needed.

Every repeat of the default size runs first, then the controls repeat by
repeat, and a control sends the tool requests only in its last repeat, so
nothing a control does to the process costs a judged repeat.

```sh
dart run tool/testing/run_local_e2e.dart \
--scenario chat-app-micro-batch-sweep --device <device>
```

Only an Android device whose GPU ggml-vulkan registers makes the library
decide about the cap. Elsewhere the run checks the default path and the
harness.

For Test Lab, build both APKs from that target as above, then add:

```sh
--environment-variables=microBatchArms=0+512,microBatchRepeats=6,microBatchBudgetSeconds=450,microBatchNativeLog=debug \
--directories-to-pull=/sdcard/Android/data/com.example.llamadart_chat_example/files/micro_batch_sweep
```

The pulled directory holds `journal.jsonl`, `native_stderr.log`, `vkjson.json`
(`cmd gpu vkjson`, saved before Flutter starts: the driver's API version and
subgroup size) and `app_apk.sha256`. Every journal record is written through
before the next step, and the same records appear in logcat as
`MICRO_BATCH_SWEEP` lines. In the journal:

- `attempt_verdict` is the verdict of one attempt: `passed`, the judged cases
one by one, the failures and the cap state. `judged` is true for the default
size. A run that dies keeps the verdicts of the attempts before it.
- `prompt_cap` is what is known about the cap of an attempt's context, as
`state`:
- `capped` (with `cap_tokens`) or `not_capped`: the library's own debug
line, with the Vulkan devices it decided from (`name`, `api_version`,
`loader_api_version`, `subgroup_size`). The library logs it only when it
has a Vulkan device to decide from.
- `no_vulkan_device`: the load asked Vulkan for GPU layers but ggml-vulkan
registered no device. The library logs nothing and applies the cap all the
same, although the context runs on the CPU (an emulator, or a load that
fell back).
- `explicit_size`: an explicit size, which leaves the cap off.
- `vulkan_not_requested`, `not_android`: no cap.
- `not_logged`: a Vulkan device is registered and the cap was to be decided,
but no decision reached the log. Treat the record as missing.
- `load` holds the backend the model runs on, its GPU layer count and the GPU
devices the runtime lists.
- `log` is every `LlamaLogger` record, among them the warning of a load that
fell back to the CPU because the Vulkan driver is below 1.2.
- `load_refused` is a typed exception from a load.
- `case` and `case_error` are each answer (with `passed` for a judged case)
and each typed exception from a request; `summary` counts the passing
answers by size.

`microBatchDevice=gpu` asks each load for `ComputeDevice.gpu` first, records a
typed refusal as `load_refused` and then loads as the profile does
(`ComputeDevice.auto`), so one run shows whether a device that cannot run
Vulkan gets a typed error and a CPU fallback. The other arguments are
`microBatchProfile` (a validation profile id) and, at build time,
`--dart-define=MICRO_BATCH_SWEEP_ARMS`, `MICRO_BATCH_SWEEP_REPEATS`,
`MICRO_BATCH_SWEEP_BUDGET_SECONDS`, `MICRO_BATCH_SWEEP_NATIVE_LOG`,
`MICRO_BATCH_SWEEP_DEVICE`, `MICRO_BATCH_SWEEP_PROFILE` and
`MICRO_BATCH_SWEEP_MODEL`.

The budget counts from the end of the model download and check. Past it no
attempt and no request starts and the test fails, which keeps a slow device
inside the Test Lab timeout; give the timeout the download on top (it took 1.5
to 4 minutes for this model on Test Lab) and the length of one request.

`--dart-define=MICRO_BATCH_SWEEP_NEGATIVE_CONTROL=true` checks the judge: it
expects a code the prompts do not hold, so the test must fail, with failures
of the default size only.

When a native crash ends the app, Test Lab reports `Test failed to run`. It
still pulls the directory: the last `attempt_started` or `case_started` record
without its result names the step that was running.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public MainActivityTest() throws IOException {
Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
if ("true".equals(arguments.getString("stageModel"))) stageModel(instrumentation);
forwardReloadMemoryArguments(arguments, instrumentation);
forwardMicroBatchSweepArguments(arguments, instrumentation);
}

private static void stageModel(Instrumentation instrumentation) throws IOException {
Expand Down Expand Up @@ -85,6 +86,33 @@ private static void forwardReloadMemoryArguments(Bundle arguments, Instrumentati
if (snapshots) startDumpsysResponder(instrumentation, cache);
}

// Hands micro_batch_sweep_e2e_test.dart its instrumentation arguments, and
// with any of them saves the device facts next to its journal.
private static void forwardMicroBatchSweepArguments(Bundle arguments, Instrumentation instrumentation)
throws IOException {
File file = new File(instrumentation.getTargetContext().getCacheDir(), "micro_batch_sweep_args.json");
JSONObject json = new JSONObject();
try {
json.put("arms", optional(arguments, "microBatchArms"));
json.put("repeats", optional(arguments, "microBatchRepeats"));
json.put("budget_seconds", optional(arguments, "microBatchBudgetSeconds"));
json.put("profile", optional(arguments, "microBatchProfile"));
json.put("native_log", optional(arguments, "microBatchNativeLog"));
json.put("device", optional(arguments, "microBatchDevice"));
if (json.length() == 0) {
file.delete();
return;
}
json.put("pid", Process.myPid());
} catch (JSONException error) {
throw new IOException(error);
}
write(file, json.toString());
// Connect UiAutomation before Flutter starts, as the dumpsys responder does.
instrumentation.getUiAutomation();
saveDeviceFacts(instrumentation, "micro_batch_sweep");
}

// Null for a blank value: a runner that passes an optional argument as an
// empty string means the default, and JSONObject.put drops a null value.
private static String optional(Bundle arguments, String name) {
Expand All @@ -95,10 +123,10 @@ private static String optional(Bundle arguments, String name) {
// Next to the journal: the Vulkan driver's own limits, from which a WebGPU
// limit that a run trips over is derived, and the hash of the installed
// app APK, which names the runtime libraries the run loaded.
private static void saveDeviceFacts(Instrumentation instrumentation) {
private static void saveDeviceFacts(Instrumentation instrumentation, String output) {
try {
Context context = instrumentation.getTargetContext();
File directory = new File(context.getExternalFilesDir(null), "litert_reload_memory");
File directory = new File(context.getExternalFilesDir(null), output);
if (!directory.isDirectory() && !directory.mkdirs()) {
throw new IOException("Unable to create " + directory);
}
Expand All @@ -122,7 +150,7 @@ private static void startDumpsysResponder(Instrumentation instrumentation, File
// semantics. Connect before Flutter starts: a semantics handle that
// appears during the test fails it at the end.
instrumentation.getUiAutomation();
saveDeviceFacts(instrumentation);
saveDeviceFacts(instrumentation, "litert_reload_memory");
Thread responder = new Thread(() -> {
while (true) {
try {
Expand Down
Loading
Loading