hex/android/app/src/main/cpp/hexspeech.cpp
Ryan Vogel 34bc8e4862
Android diagnostics: say where dictation's threads are kept, and name the speed test's rows by what they do (#2761)
* Android diagnostics: say where dictation's threads are kept, and name the speed test's rows by what they do

What:
- The report's Speech line adds "dictation threads: 4 kept ready on cpu
  4-7" (or "on any core", "started for every graph"), from the loaded
  model's pool, or before a load from the same check the pool will make.
- Each speed-test run and "Last dictation" say where that decode's threads
  were kept: hexspeech.cpp's timings gain an eighth value, read back from
  the decoding thread after the decode (-1 no pool, 0 any core, else a bit
  per core), so a refused sched_setaffinity reads as any core.
- pool_mask now also asks the kernel: it pins the calling thread to the
  mask and puts it back, and gives 0 (any core) if that is refused, so a
  pool is pinned to what the report says or not at all. New JNI:
  Whisper.usableCores, Whisper.poolCores.
- The speed test's rows are named by what they do, the same in every build
  from 2412: "new threads every graph", "threads kept ready, any core",
  "threads kept ready, fast cores" (only where Hex can tell the cores
  apart), "threads kept ready, 6"; the row this build's dictation uses is
  marked "(what dictation uses)". The faster-English row is unchanged.
- Debug builds only: am start --es cpu_capacity 300,300,1024,1024 (after a
  force-stop, before any dictation) stands in for the kernel's core
  ratings, so the emulator can take the pinned branch.

Why: after #2753, "as now" meant new threads per graph in 2409 reports and
a pinned pool afterwards, and nothing in a report said whether pinning
applied (it doesn't on an 8 Elite, where every core counts as fast), so
testers' reports couldn't be read.

Removed: the "as now" and "6 threads kept ready" labels.

Verified on basebook: ./gradlew testDebugUnitTest assembleDebug green (237
tests; StripTextTest's report cases now cover a pinned run, a refused one
reading as any core, the row marker, the Last dictation line and the words
for each pool state; hexspeech rebuilt). Not run on a device or emulator.

* Android diagnostics: no thread the speed test pins outlives its run; debug core ratings say so

What:
- SpeechModel.measure makes a fresh pool for every speed-test run on the
  test's own thread and frees it afterwards (Whisper.threadPool(handle, 0,
  0) before and after). Before, its finally put dictation's pool back from
  the test's thread: that pool's worker inherited the "Hex speed test"
  name and stayed pinned to the fast cores (the thread the gate emulator
  found on cpu 2-3 after the app speed test), and later rows could reuse a
  pool made elsewhere, whose workers the core sampler can't see.
- Dictation makes its pool again on the transcription thread when it next
  starts (ensurePool in prepare and before a standard decode; a no-op when
  the pool is there). dictationCores falls back to the cores a new pool
  would use when the model has none.
- The Processor line ends "(debug core ratings: ...)" under the debug
  override, so an emulator report can't pass for a phone's.

Checked, unchanged: the measuring thread itself (and the keyboard test's,
the same worker), a decode stopped with ✕ and a dictation's decode all get
their affinity back in hexspeech.cpp's decode, after the pool is paused;
freeing or replacing a pool (unload, release, another size or mask) joins
its workers, and ggml applies no affinity to the freeing thread; a paused
pool's creation applies none to its creator.

Removed: measure() no longer restores dictation's pool itself.

Verified on basebook: ./gradlew testDebugUnitTest assembleDebug green (237
tests; StripTextTest covers the Processor line with and without debug
ratings). The thread-affinity behaviour needs the emulator check.
2026-11-24 17:22:21 -05:00

314 lines
17 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <android/asset_manager_jni.h>
#include <dlfcn.h>
#include <jni.h>
#ifndef NDEBUG
#include <android/log.h>
#endif
#include <ggml-cpu.h>
#include <whisper.h>
#include <sched.h>
#include <algorithm>
#include <atomic>
#include <chrono>
#include <cmath>
#include <cstring>
#include <iterator>
#include <memory>
#include <mutex>
#include <string>
#include <unordered_map>
#include <vector>
// The model stays loaded between dictations (SpeechModel.kt owns it and frees it when idle), so a
// dictation pays only for its own audio. Load, decode, threadPool and free are called under one lock.
// A model's thread pool (Whisper.threadPool): ggml's worker threads made once and reused for every graph, where by
// default ggml starts and joins a new pool for each one, every encoder pass and every decoded token. Dictation keeps one
// with its model (SpeechModel.open); Labs › Diagnostics' speed test makes its own. A pool lives with its model and is
// freed with it.
struct Pool { ggml_threadpool *threads; int count; jlong mask; };
static std::mutex pools_lock;
static std::unordered_map<whisper_context *, Pool> pools;
static void drop_pool(whisper_context *ctx) {
std::lock_guard<std::mutex> hold(pools_lock);
auto found = pools.find(ctx);
if (found == pools.end()) return;
ggml_threadpool_free(found->second.threads);
pools.erase(found);
}
extern "C" JNIEXPORT jlong JNICALL
Java_ly_anoma_hex_speech_Whisper_load(JNIEnv *env, jobject, jobject assets, jstring file) {
#ifdef NDEBUG
// whisper.cpp also installs this callback for ggml. Set it before either engine starts.
static const bool quiet = [] {
whisper_log_set([](ggml_log_level, const char *, void *) {}, nullptr);
return true;
}();
(void)quiet;
#endif
auto options = whisper_context_default_params();
options.use_gpu = false;
whisper_context *ctx = nullptr;
if (file) {
const char *path = env->GetStringUTFChars(file, nullptr);
if (!path) return 0;
ctx = whisper_init_from_file_with_params(path, options);
env->ReleaseStringUTFChars(file, path);
} else {
auto manager = AAssetManager_fromJava(env, assets);
if (!manager) return 0;
std::unique_ptr<AAsset, decltype(&AAsset_close)> model(
AAssetManager_open(manager, "whisper-tiny.bin", AASSET_MODE_BUFFER), AAsset_close);
if (!model) return 0;
auto data = const_cast<void *>(AAsset_getBuffer(model.get()));
if (!data) return 0;
// The weights are copied into the context, so the asset can close afterwards.
ctx = whisper_init_from_buffer_with_params(data, AAsset_getLength(model.get()), options);
}
#ifndef NDEBUG
__android_log_print(ANDROID_LOG_DEBUG, "HexSpeech", "%s model %s", file ? "small" : "tiny", ctx ? "loaded" : "failed");
#endif
return reinterpret_cast<jlong>(ctx);
}
extern "C" JNIEXPORT void JNICALL
Java_ly_anoma_hex_speech_Whisper_free(JNIEnv *, jobject, jlong ctx) {
drop_pool(reinterpret_cast<whisper_context *>(ctx));
whisper_free(reinterpret_cast<whisper_context *>(ctx));
#ifndef NDEBUG
__android_log_print(ANDROID_LOG_DEBUG, "HexSpeech", "model freed");
#endif
}
// The dictation the person discarded (✕, or Home's Cancel), by MicCapture's job number. whisper.cpp asks after each
// encoder pass and each decoded token, so its decode ends within one of those and the next dictation's load and
// decode no longer wait for the whole abandoned one. Set from any thread, outside the model's lock.
static std::atomic<jlong> abandoned{0};
extern "C" JNIEXPORT void JNICALL
Java_ly_anoma_hex_speech_Whisper_abort(JNIEnv *, jobject, jlong job) { abandoned.store(job); }
// [fast]'s bits this thread may run on, or 0 when that leaves fewer cores than the pool has threads: sharing what is
// left is slower than letting the kernel place them, as before. Also 0 when the kernel refuses to pin this thread to
// them (tried and put back here), so a pool is either pinned to what Labs › Diagnostics reports or not pinned at all.
static jlong pool_mask(jlong fast, int threads) {
if (fast == 0) return 0;
cpu_set_t allowed;
if (sched_getaffinity(0, sizeof(allowed), &allowed) != 0) return 0;
jlong usable = 0;
int cores = 0;
cpu_set_t wanted;
CPU_ZERO(&wanted);
for (int core = 0; core < GGML_MAX_N_THREADS && core < 64; ++core) {
const jlong bit = static_cast<jlong>(1) << core;
if ((fast & bit) && CPU_ISSET(core, &allowed)) { usable |= bit; ++cores; CPU_SET(core, &wanted); }
}
if (cores < threads) return 0;
const bool pinned = sched_setaffinity(0, sizeof(wanted), &wanted) == 0;
sched_setaffinity(0, sizeof(allowed), &allowed);
return pinned ? usable : 0;
}
extern "C" JNIEXPORT jlong JNICALL
Java_ly_anoma_hex_speech_Whisper_usableCores(JNIEnv *, jobject, jlong fast, jint threads) { return pool_mask(fast, threads); }
// The cores the model's pool is kept on: -1 for no pool, 0 for any core.
extern "C" JNIEXPORT jlong JNICALL
Java_ly_anoma_hex_speech_Whisper_poolCores(JNIEnv *, jobject, jlong handle) {
std::lock_guard<std::mutex> hold(pools_lock);
auto found = pools.find(reinterpret_cast<whisper_context *>(handle));
return found == pools.end() ? -1 : found->second.mask;
}
// Keeps a pool of [threads] worker threads with the model for its decodes, or none (0), ggml's default. A pool of
// another size or cores is replaced. ggml's default pool parameters, as its per-graph pools use: only reuse, the cores
// and the paused start differ. [fast] has a bit per core to keep them on (0: no restriction); a short encode then stays
// off the little ones instead of being one of them.
extern "C" JNIEXPORT jboolean JNICALL
Java_ly_anoma_hex_speech_Whisper_threadPool(JNIEnv *, jobject, jlong handle, jint threads, jlong fast) {
auto ctx = reinterpret_cast<whisper_context *>(handle);
if (!ctx) return false;
const jlong mask = threads > 0 ? pool_mask(fast, threads) : 0;
{
std::lock_guard<std::mutex> hold(pools_lock);
auto found = pools.find(ctx);
if (found != pools.end() && found->second.count == threads && found->second.mask == mask) return true;
}
drop_pool(ctx);
if (threads <= 0) return true;
auto params = ggml_threadpool_params_default(threads);
// Started paused: the workers sleep as soon as they start instead of polling for a graph, and the first decode's
// kickoff is where ggml applies the mask to the thread that runs it (ggml-cpu.c: kickoff resumes a paused pool), then
// hexspeech's decode puts that thread's own affinity back. Not strict, as ggml's per-graph pools are: every worker
// may use every core of the mask.
params.paused = true;
for (int core = 0; core < GGML_MAX_N_THREADS && core < 64; ++core)
if (mask & (static_cast<jlong>(1) << core)) params.cpumask[core] = true;
auto pool = ggml_threadpool_new(&params);
if (!pool) return false;
std::lock_guard<std::mutex> hold(pools_lock);
pools[ctx] = {pool, threads, mask};
return true;
}
// The last decode's timings, for Labs › Diagnostics: microseconds spent sampling, in the encoder, decoding one token at a
// time, decoding in batches and on the prompt, then the encoder window (0: the full 30 s), the thread count, and the cores
// its threads were kept on (-1: no pool, new threads every graph; 0: kept on any core; else a bit per core, read back from
// the decoding thread, so a refused pinning reads as any core). Written by decode and read by timings, both under
// SpeechModel's lock.
static jlong last[8];
extern "C" JNIEXPORT jlongArray JNICALL
Java_ly_anoma_hex_speech_Whisper_timings(JNIEnv *env, jobject) {
auto result = env->NewLongArray(8);
if (result) env->SetLongArrayRegion(result, 0, 8, last);
return result;
}
// The name of the file this code was loaded from (libhexspeech.so, or another build of it), for Labs › Diagnostics.
extern "C" JNIEXPORT jstring JNICALL
Java_ly_anoma_hex_speech_Whisper_library(JNIEnv *env, jobject) {
Dl_info info{};
if (!dladdr(reinterpret_cast<void *>(&Java_ly_anoma_hex_speech_Whisper_library), &info) || !info.dli_fname) return nullptr;
const char *name = std::strrchr(info.dli_fname, '/');
return env->NewStringUTF(name ? name + 1 : info.dli_fname);
}
extern "C" JNIEXPORT jbyteArray JNICALL
Java_ly_anoma_hex_speech_Whisper_decode(JNIEnv *env, jobject, jlong handle, jboolean small, jshortArray samples,
jint count, jint threads, jstring language, jboolean translate, jstring prompt,
jlong job) {
auto ctx = reinterpret_cast<whisper_context *>(handle);
if (!ctx || count < 1 || count > env->GetArrayLength(samples)) return nullptr;
std::vector<float> pcm(count);
jshort page[2048];
for (int i = 0; i < count; i += 2048) {
const int n = std::min(2048, count - i);
env->GetShortArrayRegion(samples, i, n, page);
if (env->ExceptionCheck()) return nullptr;
for (int j = 0; j < n; ++j) pcm[i + j] = page[j] / 32768.0f;
}
auto params = whisper_full_default_params(WHISPER_SAMPLING_GREEDY);
// Dictation asks for at most 4; Labs › Diagnostics also tries 6.
params.n_threads = std::clamp<int>(threads, 1, 8);
const char *code = env->GetStringUTFChars(language, nullptr);
if (!code) return nullptr;
std::string lang(code);
env->ReleaseStringUTFChars(language, code);
params.language = lang.c_str();
params.translate = translate;
// Custom vocabulary as Whisper's initial prompt biases its spelling of those names.
std::string hint;
if (prompt) {
const char *chars = env->GetStringUTFChars(prompt, nullptr);
if (!chars) return nullptr;
hint = chars;
env->ReleaseStringUTFChars(prompt, chars);
}
if (!hint.empty()) params.initial_prompt = hint.c_str();
// The encoder's window, in 20 ms frames: up to 8 s of speech, the speech plus 3.84 s (192 frames),
// at most the measured 512; the 40-clip benchmark kept the same word errors at 27% less time.
// Longer, the speech plus 12.8 s (640 frames), rounded up to 64, until that reaches Whisper's full
// 1500 (about 16.7 s and more). On 53 long clips in six languages (8.5-30 s) that margin made no clip
// worse than the full window; at 576 and less clips start to lose words, so don't lower it without
// rerunning the long-clip check (benchmark/README.md). The bundled tiny model keeps the full window;
// shortened, it repeats itself.
if (small) {
const int speech = static_cast<int>(std::ceil(count * 50.0 / 16000));
const int frames = count <= 8 * 16000 ? std::min(512, (speech + 192 + 63) / 64 * 64) : (speech + 640 + 63) / 64 * 64;
if (frames < 1500) params.audio_ctx = frames;
}
// A language the model handles badly (Kazakh told English) repeats itself to Whisper's 220-token limit, then
// retries with five sampled decoders at each of up to five more temperatures: 2–5× the time, for nonsense.
// Speech measured at most 10 tokens a second (Kazakh in Kazakh; Swedish 6, English 3.6), so each 30 s window
// may have 15 a second plus 16, and a decode that fails Whisper's checks is retried once, with one decoder,
// at 0.2. whisper.cpp tries params.temperature, then each step up to 1.0, and decodes greedily at any
// temperature <= 0: from -0.8 in steps of 1.0 that is greedy, then 0.2. A single retry any hotter (0.6, 1.0)
// invented fluent English or mixed scripts instead. The 40 benchmark clips and 15 long ones never fall back,
// and keep the same text.
params.max_tokens = std::min(220, static_cast<int>(std::ceil(std::min(count, 30 * 16000) * 15.0 / 16000)) + 16);
params.greedy.best_of = 1;
params.temperature = -0.8f;
params.temperature_inc = 1.0f;
params.abort_callback = [](void *mine) { return abandoned.load() == *static_cast<const jlong *>(mine); };
params.abort_callback_user_data = &job;
params.no_context = true;
// Audio longer than Whisper's 30 s window is decoded with timestamps, as Whisper decodes long audio: each window
// then ends at the last sentence it finished, and the next starts there. Without them whisper.cpp moves on a
// whole 30 s at a time, cutting mid-sentence: the words across the cut were lost, and a window full of speech to
// its edge left out whole sentences (5 of 14 in a 36 s Spanish dictation). On 32 clips of 36-58 s in the ten
// dictation languages that lost 27 of 457 sentences; with timestamps, one: a sentence said twice in a row,
// which every setting drops. The time is the same. Up to 30 s nothing changes.
params.no_timestamps = count <= 30 * 16000;
params.print_progress = params.print_realtime = params.print_timestamps = params.print_special = false;
params.suppress_nst = true;
// A fresh decoding state for every dictation (3 ms), the weights staying loaded: a state kept from an
// earlier, longer dictation leaves encoder output past a shorter window's end, which its cross-attention
// still reads, so a short dictation after a long one could come out differently (even in another language).
std::unique_ptr<whisper_state, decltype(&whisper_free_state)> state(whisper_init_state(ctx), whisper_free_state);
if (!state) return nullptr;
ggml_threadpool *pool = nullptr;
jlong mask = 0;
{
std::lock_guard<std::mutex> hold(pools_lock);
auto found = pools.find(ctx);
if (found != pools.end()) {
pool = found->second.threads;
mask = found->second.mask;
params.n_threads = std::min(params.n_threads, found->second.count);
}
}
// ggml gives the thread that computes a graph the pool's own mask too (ggml-cpu.c: ggml_graph_compute_kickoff
// applies workers[0]'s). That is this thread, which for dictation also loads models and runs Parakeet's graphs,
// and for the speed test also times the no-pool run: keep what it had and put it back once the decode ends. A
// masked pool runs only when that is saved, else this decode keeps ggml's own threads and no pinning.
cpu_set_t mine;
const bool attach = pool && (!mask || sched_getaffinity(0, sizeof(mine), &mine) == 0);
// The pool outlives the state, which never frees it. A decode stopped by abort ends between graphs, so the pool is
// idle and the next graph resets its abort flag.
if (attach) hex_whisper_state_set_threadpool(state.get(), pool);
#ifndef NDEBUG
const auto started = std::chrono::steady_clock::now();
#endif
const int status = whisper_full_with_state(ctx, state.get(), params, pcm.data(), count);
// Paused workers wait on a condition variable until the next graph instead of polling.
if (attach) ggml_threadpool_pause(pool);
// Where this decode's threads were kept, for Labs › Diagnostics: read back from this thread, which ggml pinned to the
// pool's mask at the first graph unless the kernel refused (then it reads as any core).
jlong kept = attach ? 0 : -1;
if (attach && mask) {
cpu_set_t now_on;
if (sched_getaffinity(0, sizeof(now_on), &now_on) == 0) {
jlong on = 0;
for (int core = 0; core < 64; ++core) if (CPU_ISSET(core, &now_on)) on |= static_cast<jlong>(1) << core;
if (on != 0 && (on & ~mask) == 0) kept = on;
}
sched_setaffinity(0, sizeof(mine), &mine);
}
whisper_timings spent{};
hex_whisper_state_timings(state.get(), &spent);
const jlong now[8] = {static_cast<jlong>(spent.sample_ms * 1000), static_cast<jlong>(spent.encode_ms * 1000),
static_cast<jlong>(spent.decode_ms * 1000), static_cast<jlong>(spent.batchd_ms * 1000),
static_cast<jlong>(spent.prompt_ms * 1000), params.audio_ctx, params.n_threads, kept};
std::copy(std::begin(now), std::end(now), std::begin(last));
#ifndef NDEBUG
// Diagnostics identify the engine and task only; no audio or recognized text is logged.
const auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - started).count();
__android_log_print(ANDROID_LOG_DEBUG, "HexSpeech", "%s %s %s in %lld ms, %d samples, %d threads, window %d, max %d tokens",
small ? "small" : "tiny", translate ? "translate" : "transcribe",
status == 0 ? "complete" : abandoned.load() == job ? "abandoned" : "failed", static_cast<long long>(ms),
count, params.n_threads, params.audio_ctx, params.max_tokens);
#endif
if (status != 0) return nullptr;
std::string text;
for (int i = 0; i < whisper_full_n_segments_from_state(state.get()); ++i)
text += whisper_full_get_segment_text_from_state(state.get(), i);
// JNI's modified UTF-8 is not standard UTF-8 (notably for supplementary code points).
auto result = env->NewByteArray(static_cast<jsize>(text.size()));
if (result) env->SetByteArrayRegion(result, 0, static_cast<jsize>(text.size()),
reinterpret_cast<const jbyte *>(text.data()));
return result;
}