Skip to content

Export LabelLookAheadRelabeler - #314

Open
npuichigo wants to merge 1 commit into
garvys-org:mainfrom
npuichigo:export-label-lookahead-relabeler
Open

Export LabelLookAheadRelabeler#314
npuichigo wants to merge 1 commit into
garvys-org:mainfrom
npuichigo:export-label-lookahead-relabeler

Conversation

@npuichigo

Copy link
Copy Markdown

What

Widen one re-export from pub(super) to pub.

Why

LabelLookAheadRelabeler has pub fn init and pub fn relabel, but
lookahead_matchers/mod.rs re-exports the type as pub(super), so neither is
callable from outside the crate.

That blocks the natural way to use lookahead composition when one side is static
and the other changes per call:

  1. build the lookahead FST (and its LabelReachableData) once, then
  2. relabel each new FST into that index space before composing.

Step 2 needs LabelLookAheadRelabeler::relabel. Without it the only reachable
entry point is MatcherFst::new_with_relabeling, which fuses "build the
reachability tables" with "relabel the other FST", so the tables are rebuilt on
every composition.

Impact

Measured on a text-normalization workload: short linear input acceptors composed
against a static 187k-state grammar, then shortest path.

before (plain compose) after (lookahead)
per input 14.4 ms 3.8 ms
throughput 2.1 chars/ms 7.9 chars/ms

Building the tables moves to startup (~300 ms, one-off). Output is unchanged:
verified byte-for-byte against plain compose across a 184-case regression
corpus.

For reference, OpenFST (via pynini) on the same grammar and corpus measures
7.4 ms per input, so this path makes rustfst roughly 2x faster than OpenFST here
— but only if the relabeler is reachable.

Risk

Should be none: the type and both of its methods are already pub; only the
module re-export is widened, matching the other items in that module.

Note

Assembling lookahead composition currently requires hand-writing the nested
matcher/filter generics, since ComposeFilterEnum has no lookahead variant and
compose_static::create_matcher always builds a SortedMatcher. Happy to
follow up with a convenience entry point and/or docs if that would be welcome.

`LabelLookAheadRelabeler::{init, relabel}` are declared `pub`, but the
re-export in `lookahead_matchers/mod.rs` is `pub(super)`, so the type is
unreachable from outside the crate.

This makes it impossible to relabel an FST against an already-built
lookahead FST. The only external entry point left is
`MatcherFst::new_with_relabeling`, which builds the reachability tables
and relabels the other FST in a single call, so the tables have to be
rebuilt on every composition.

That is prohibitive whenever one FST is static and the other varies: in a
text-normalization workload, building the tables for a 187k-state grammar
takes ~300 ms while each input is a short linear acceptor. With the
relabeler exported, the tables are built once at startup and each input is
relabeled into the grammar's index space before composing, which measured
14.4 ms -> 3.8 ms per input with byte-identical output.

Widening the re-export to `pub` matches the other items in the module and
adds no new API surface beyond the already-`pub` type.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant