/* Any copyright is dedicated to the Public Domain. http://creativecommons.org/publicdomain/zero/1.0/ */ "use strict"; // One task per failure mode of the HWInference text generation path, and what // each records in firefox.ai.runtime. Creation failures record the error name, // run failures the message, and each mode must come out as its own value. // // Not covered, for lack of an injection point: the process refusing to launch, // the process dying before the model load reports, and the backend refusing a // model (llama.cpp aborts on an unreadable file instead of reporting). const HWI_PREFS = [["browser.ml.llama.hwInference", true]]; const RUN_REQUEST = { prompt: TINYSTORIES_STORYTELLER_PROMPT, samplers: TINYSTORIES_GREEDY_SAMPLERS, nPredict: 16, }; function events(name) { return Glean.firefoxAiRuntime[name].testGetValue() ?? []; } function only(name) { const all = events(name); Assert.equal(all.length, 1, `Exactly one ${name} event`); return all[0].extra; } function failedCreations() { return events("engineCreation") .map(event => event.extra) .filter(creation => creation.outcome === "error"); } function unsuccessfulRuns() { return events("engineRun").filter(event => event.extra.outcome !== "success"); } // crash-me.gguf takes the process down mid-generation. The harness counts // the minidump as a failure unless the test claims it, as browser_ml_native // does for the same model. async function removeCrashDump(shutdown) { const [subject] = await shutdown; const props = subject.QueryInterface(Ci.nsIPropertyBag2); if (!props.hasKey("dumpID")) { return; } const dumpID = props.getPropertyAsAString("dumpID"); await Services.crashmanager.ensureCrashIsPresent(dumpID); const minidumps = Services.dirsvc.get("ProfD", Ci.nsIFile); minidumps.append("minidumps"); for (const suffix of [".dmp", ".extra"]) { const file = minidumps.clone(); file.append(dumpID + suffix); if (file.exists()) { file.remove(false); } } } add_task(async function test_download_failure() { const { cleanup } = await setup({ prefs: HWI_PREFS }); try { let thrown; await createEngine({ ...TINYSTORIES_ENGINE_OPTIONS, engineId: "hwi-tel-download", featureId: "link-preview", modelId: "acme-not-found/bert", modelFile: "config.json", }).catch(e => (thrown = e)); Assert.ok(thrown, "A missing model fails engine creation"); Assert.equal(failedCreations().length, 1); Assert.equal( failedCreations().at(-1).error, thrown.name, "The creation failure records the download error" ); Assert.equal(failedCreations().at(-1).host_process, "hwinference"); Assert.equal( failedCreations().at(-1).failed_step, "model-download", "The creation failed while downloading the model" ); const download = events("modelDownload").at(-1).extra; Assert.equal(download.outcome, "error", "The download is recorded failed"); Assert.equal(Number(download.failed_files), 1, "One file failed"); Assert.equal(unsuccessfulRuns().length, 0); } finally { await cleanup(); } }); add_task(async function test_aborted_creation() { const { cleanup } = await setup({ prefs: HWI_PREFS }); try { const controller = new AbortController(); controller.abort(); await Assert.rejects( createEngine( { ...TINYSTORIES_ENGINE_OPTIONS, engineId: "hwi-tel-abort", featureId: "link-preview", }, null, controller.signal ), error => error.name === "AbortError" ); Assert.equal( failedCreations().length, 0, "An aborted creation is not counted as failed" ); const creation = only("engineCreation"); Assert.equal( creation.outcome, "aborted", "A creation its caller gave up is recorded as aborted" ); Assert.equal(creation.error, undefined, "and names no error"); } finally { await cleanup(); } }); add_task(async function test_process_crash_mid_generation() { // Sanitizer builds have no crash reporter, so their signal handler turns // the deliberate crash into a sanitizer report that fails the run. if (AppConstants.ASAN || AppConstants.TSAN) { ok(true, "Skipping the deliberate crash on a sanitizer build"); return; } SimpleTest.expectChildProcessCrash(); const { cleanup } = await setup({ prefs: HWI_PREFS }); try { const engine = await createEngine({ ...TINYSTORIES_ENGINE_OPTIONS, engineId: "hwi-tel-crash", featureId: "link-preview", modelFile: "crash-me.gguf", }); const { pid } = await getInferenceProcessInfo("hwInference"); const shutdown = TestUtils.topicObserved( "ipc:utility-shutdown", (subject, data) => parseInt(data, 10) === pid ); await Assert.rejects( collectGeneratedText( engine.runWithGenerator({ ...RUN_REQUEST, nPredict: 64 }) ), error => error.name === "AbortError" ); await removeCrashDump(shutdown); noteIntentionalUtilityCrash(pid); Assert.equal(unsuccessfulRuns().length, 1); const failed = unsuccessfulRuns()[0].extra; Assert.equal(failed.outcome, "error", "A dead process is a failed run"); Assert.equal( failed.error, "InferenceProcessCrash", "The run is recorded as a process crash" ); Assert.equal(failed.host_process, "hwinference"); await engine.terminate(); } finally { await cleanup(); } }); add_task(async function test_cancel_mid_generation() { const { cleanup } = await setup({ prefs: HWI_PREFS }); try { const engine = await createEngine({ ...TINYSTORIES_ENGINE_OPTIONS, engineId: "hwi-tel-cancel", featureId: "link-preview", }); const pending = engine.run({ prompt: TINYSTORIES_STORYTELLER_PROMPT, samplers: TINYSTORIES_GREEDY_SAMPLERS, nPredict: 4096, stopOnEndOfGenerationTokens: false, }); engine.cancel(); const result = await pending; await engine.terminate(); Assert.equal( result.finishReason, "cancelled", "The generation was stopped before its end" ); const run = only("engineRun"); Assert.equal( run.outcome, "aborted", "A generation its caller cancelled is aborted" ); Assert.equal(run.finish_reason, "cancelled"); } finally { await cleanup(); } }); add_task(async function test_terminate_mid_generation() { const { cleanup } = await setup({ prefs: HWI_PREFS }); try { const engine = await createEngine({ ...TINYSTORIES_ENGINE_OPTIONS, engineId: "hwi-tel-terminate", featureId: "link-preview", }); await Assert.rejects( (async () => { for await (const chunk of engine.runWithGenerator({ prompt: TINYSTORIES_STORYTELLER_PROMPT, samplers: TINYSTORIES_GREEDY_SAMPLERS, nPredict: 4096, minOutputBufferSize: 1, stopOnEndOfGenerationTokens: false, })) { void chunk; await engine.terminate(); } })(), error => error.name === "AbortError" ); Assert.equal(unsuccessfulRuns().length, 1); Assert.equal( unsuccessfulRuns()[0].extra.outcome, "aborted", "A run whose engine the caller tore down is aborted, not failed" ); } finally { await cleanup(); } });