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
51 changes: 50 additions & 1 deletion packages/models/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,54 @@ The functions Pi Voice had keep their names and behaviour (`recommendModels`,
`createCatalog(data)` binds them all to a different catalog, e.g. one fetched
at runtime.

### How the picks are made

Every pick is judged on the benchmark machines, not the user's: error is the
FLEURS word (or character) error rate at the default quant, and wait is the
processing time after a 30-second dictation (`dictationSeconds`). For several
languages, error is the geometric mean over them, and the worst one is kept too.
The values below are `methodology` in `curation.yaml`.

1. **The pool.** Only models that speak every chosen language and have
benchmarks for them. Quality is a hard constraint: if any model stays under
`maxLanguageErrorPercent` (20%) on every chosen language, only those count,
and among them models that can switch between the languages automatically
come first.
2. **Best** balances accuracy and wait in proportion, among the pool within
`overallMaxWaitSeconds` (5 s). The lowest score wins:

```
accuracy = ½ × error + ½ × worst language's error (worstLanguageWeight 0.5)
× 0.95 for streaming models (streamingErrorDiscount)
score = ln(accuracy + 3) + β × ln(max(wait, 1.5 s)) (errorOffsetPercent, instantWaitSeconds)
β = ln(1 / (1 − 0.2)) / ln 3 (speedTradeErrorShare)
```

In words: a model three times as fast may make up to 20% more errors and
still be the better pick. Ratios rather than points or seconds, because
going from 3 s to 1 s feels like going from 1.5 s to 0.5 s, and 6% error
is to 3% what 12% is to 6%. Three points are added to every error first,
so small gaps between already low error rates (3.7% against 4.7%) count
for less than the same ratio between high ones. Waits under 1.5 s all
count as instant. For several languages the worst one weighs half, so a
model that is good at English cannot hide a weak second language.
Streaming models, which type while the user speaks, get a small edge.
When no usable model fits 5 s, the same score decides among those within
`accurateMaxWaitSeconds` (10 s), and past that the quickest usable model.
3. **Fast** is the most accurate model in the pool that finishes within
`fastCpuMaxWaitSeconds` (4 s) on the benchmark CPU, else the quickest one
there.
4. **Accurate** is the most accurate model in the pool within
`accurateMaxWaitSeconds` (10 s), else the most accurate one.
5. When no model is under the floor, the closest one carries every role: it
is `experimental` below `experimentalMaxErrorPercent` (30%), or when its
confidence interval reaches it, otherwise `unsupported`. A pick at or over
`noteMinErrorPercent` (15%) on some language is still recommended, with
the shortfall named.

`bestScore(methodology, model)` exposes the Best score, so an app that ranks
models itself (on another machine tier, say) orders them the same way.

### Grades and the speed meter

Grades are on measured error at the benchmarked quant: A under 5%, B under
Expand Down Expand Up @@ -161,4 +209,5 @@ One-time setup:

`test/fixtures/pi-voice-picks.json` and `pi-voice-data.json` record Pi
Voice's picks and data at the time of the move; `parity.test.ts` proves this
code reproduces them exactly. `picks.json` is this package's own snapshot.
code still reproduces their status, Fast and Accurate picks exactly (Best has
changed since, see above). `picks.json` is this package's own snapshot.
7 changes: 5 additions & 2 deletions packages/models/catalog.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
"transcribeCpp": "0.4.0",
"methodology": {
"dictationSeconds": 30,
"freeWaitSeconds": 3,
"waitWeight": 0.5,
"speedTradeErrorShare": 0.2,
"errorOffsetPercent": 3,
"instantWaitSeconds": 1.5,
"worstLanguageWeight": 0.5,
"streamingErrorDiscount": 0.05,
"overallMaxWaitSeconds": 5,
"maxLanguageErrorPercent": 20,
"noteMinErrorPercent": 15,
Expand Down
7 changes: 5 additions & 2 deletions packages/models/curation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ benchmarks:
# means. Waits are quoted after a dictation of `dictationSeconds`.
methodology:
dictationSeconds: 30
freeWaitSeconds: 3
waitWeight: 0.5
speedTradeErrorShare: 0.2
errorOffsetPercent: 3
instantWaitSeconds: 1.5
worstLanguageWeight: 0.5
streamingErrorDiscount: 0.05
overallMaxWaitSeconds: 5
maxLanguageErrorPercent: 20
noteMinErrorPercent: 15
Expand Down
4 changes: 2 additions & 2 deletions packages/models/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/models/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@handy-computer/models",
"version": "0.2.3",
"version": "0.3.0",
"description": "The curated transcribe.cpp model catalog and the recommendation method shared by Handy, Pi Voice, and models.handy.computer",
"type": "module",
"license": "MIT",
Expand Down
2 changes: 2 additions & 0 deletions packages/models/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export {
export {
accuracyGrade,
BENCHMARK_DICTATION_SECONDS,
bestScore,
type BestScoreInput,
frontierModelIds,
SPEED_METER_STEPS,
speedMeterLevel,
Expand Down
52 changes: 43 additions & 9 deletions packages/models/src/recommendations.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// The recommendation method, moved from Pi Voice (src/recommendations.ts at
// earendil-works/pi-voice@b5b61b1) with its behaviour unchanged. The only
// difference is that the benchmark data arrives as an argument instead of a
// module-level import, so callers can bind any catalog (see createCatalog).
// The recommendation method, moved from Pi Voice (earendil-works/pi-voice@b5b61b1)
// and bound to a catalog by createCatalog. Best has since changed (bestScore).

import { displayLanguage, modelMatchesLanguage, rankCatalogModels } from "./catalog.ts";
import { type DetectorCodes, undetectablePreferences } from "./language-detection.ts";
Expand Down Expand Up @@ -134,9 +132,35 @@ type ScoredModel = {
cpuWaitSeconds: number | undefined;
/** Needs the language set by hand for this set of languages. */
manual: boolean;
/** The Best pick's score (see bestScore): lower is better. */
score: number;
};

/** What bestScore reads about a model for the chosen languages. */
export type BestScoreInput = {
/** Geometric mean of the per-language error, in percent. */
readonly error: number;
/** The highest error on any chosen language, in percent. */
readonly worstError: number;
/** Seconds after the dictation on the benchmark GPU. */
readonly waitSeconds: number;
/** Whether the model transcribes while the user speaks. */
readonly streaming: boolean;
};

/** The Best pick's score, lowest wins: a model three times as fast may make
* `speedTradeErrorShare` more errors (README: How the picks are made). */
export function bestScore(methodology: Methodology, model: BestScoreInput): number {
const weight = methodology.worstLanguageWeight;
const blended = (1 - weight) * model.error + weight * model.worstError;
const accuracy = model.streaming ? blended * (1 - methodology.streamingErrorDiscount) : blended;
const beta = Math.log(1 / (1 - methodology.speedTradeErrorShare)) / Math.log(3);
return (
Math.log(accuracy + methodology.errorOffsetPercent) +
beta * Math.log(Math.max(model.waitSeconds, methodology.instantWaitSeconds))
);
}

function scoreModels(
context: RecommendationContext,
models: readonly CatalogModel[],
Expand Down Expand Up @@ -173,7 +197,12 @@ function scoreModels(
cpuWaitSeconds: cpuXrt ? method.dictationSeconds / cpuXrt : undefined,
manual: wanted.length > 1 &&
undetectablePreferences(context.detectorCodes, model, languages).length > 0,
score: error + method.waitWeight * Math.max(0, waitSeconds - method.freeWaitSeconds),
score: bestScore(method, {
error,
worstError,
waitSeconds,
streaming: model.capabilities.streaming,
}),
});
}
return scored;
Expand Down Expand Up @@ -296,14 +325,19 @@ export function recommendModels(
? withinFloor
: scored;

// Past the overall budget, the score decides within the Accurate budget,
// then the quickest wins (a proportional score understates long waits).
const byScore = (candidate: ScoredModel) => candidate.score;
const withinOverallBudget = pool.filter(
(candidate) => candidate.waitSeconds <= method.overallMaxWaitSeconds,
);
const withinAccurateBudget = pool.filter(
(candidate) => candidate.waitSeconds <= method.accurateMaxWaitSeconds,
);
const overall = hasUsableModel
? lowest(
withinOverallBudget.length ? withinOverallBudget : pool,
(candidate) => candidate.score,
)!
? lowest(withinOverallBudget, byScore) ??
lowest(withinAccurateBudget, byScore) ??
lowest(pool, (candidate) => candidate.waitSeconds + candidate.error / 1000)!
: lowest(pool, (candidate) => candidate.worstError + candidate.error / 1000)!;

// Fast means the best quality inside the CPU target. If every usable model
Expand Down
19 changes: 13 additions & 6 deletions packages/models/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,9 @@ export type CatalogModel = {
* usable manual model beats an automatic model over the error floor. Quality
* is a hard constraint whenever any model stays under
* `maxLanguageErrorPercent` on every chosen language. The overall pick has
* the lowest error plus wait penalty, where the first `freeWaitSeconds` cost
* nothing and each second beyond costs `waitWeight` points; its
* `overallMaxWaitSeconds` limit relaxes before quality does. The fast pick is
* judged on the benchmark CPU: the most accurate usable model within
* the lowest bestScore; its `overallMaxWaitSeconds` limit relaxes before
* quality does. The fast pick is judged on the benchmark CPU: the most
* accurate usable model within
* `fastCpuMaxWaitSeconds`, else the quickest usable model. The most accurate
* pick has the lowest error among usable models within
* `accurateMaxWaitSeconds`, else the most accurate usable model. When no
Expand All @@ -76,8 +75,16 @@ export type CatalogModel = {
*/
export type Methodology = {
readonly dictationSeconds: number;
readonly freeWaitSeconds: number;
readonly waitWeight: number;
/** A model three times as fast may make this share more errors (0.2 = 20%). */
readonly speedTradeErrorShare: number;
/** Added to every error before comparing, in percentage points. */
readonly errorOffsetPercent: number;
/** Waits up to this many seconds all count as the same. */
readonly instantWaitSeconds: number;
/** Weight of the worst chosen language against the geometric mean (0 to 1). */
readonly worstLanguageWeight: number;
/** Share taken off a streaming model's error (0.05 = 5%). */
readonly streamingErrorDiscount: number;
readonly overallMaxWaitSeconds: number;
readonly maxLanguageErrorPercent: number;
readonly noteMinErrorPercent: number;
Expand Down
Loading