Skip to content

Add bounded batch parsing to HermitCrab - #582

Merged
johnml1135 merged 6 commits into
masterfrom
feat/hc-parse-words-batch
Nov 20, 2026
Merged

johnml1135 merged 6 commits into
masterfrom
feat/hc-parse-words-batch

Conversation

@johnml1135

@johnml1135 johnml1135 commented Oct 29, 2026 •

Copy link
Copy Markdown
Collaborator

Start here: src/SIL.Machine.Morphology.HermitCrab/Morpher.cs, ParseWords: the whole public surface, plus the guard that requires a degree-1 Morpher.

Status: ready for review; all checks green. #546 (shared template optimization, still open) is not required; this PR is independent of it.

FieldWorks consumer: FLEx's parser worker adopts this through ParseWords on a degree-1 Morpher, tracked in LT-22885.

Morpher.ParseWords(words, maxWordConcurrency = 4, guessRoot = false) parses a batch of words concurrently and returns one WordParseResult per input position, in input order, duplicates kept. Each result carries materialized analyses, a trace, and any per-word exception, so an invalid shape or resource-limit failure on one word does not discard the others. The single-word API and the constructor default (maxDegreeOfParallelism = 0) are unchanged.

What could break existing callers? Nothing opts in implicitly. ParseWords throws unless the Morpher was built with maxDegreeOfParallelism: 1, which keeps template slots, analysis cascades and synthesis sequential inside each word. One breaking change: MaxAlternatives is now a constructor argument with no setter, fixed for the Morpher's lifetime, so the rules read it directly and parsing writes no shared compiled rule state (AnalysisStratumRule.cs, AnalysisLanguageRule.cs). Callers that set it must move it to the constructor and recompile; FieldWorks' HCParser.cs becomes new Morpher(m_traceManager, m_language, 0, maxAlternatives). ParseWords also takes an optional progress callback, called on the parsing threads when each word starts and finishes.

var morpher = new Morpher(traceManager, language, maxDegreeOfParallelism: 1);
IReadOnlyList<WordParseResult> results = morpher.ParseWords(pendingWords);

Where to look:

  • Per-invocation limits replacing the old _maxAlternatives field: AnalysisStratumRule.cs. Pinned by ParseWords_CapturesMaxAlternativesPerWord.
  • Results match single-word parsing, in order: ParseWords_PreservesInputOrderAndSingleWordAnalysisSets, ParseWords_MatchesSingleWordAnalysisSetsWithCompounding.
  • Degree-0 Morphers are refused: ParseWords_RequiresSequentialMorpher.
  • Tracing runs the whole batch serially on the calling thread: ParseWords_TracingRunsOnCallingThreadAndRetainsPerWordTraces.

Deliberately not here: FieldWorks adoption (it needs a caller change; the default stays 0); a change to the single-word constructor default; hard or capped words are unmeasured for parity and throughput.

Speed figures are measured wall timings used only as motivation, not a guarantee for this implementation or every Language (details below).

Verification: offline Release build passes with zero warnings or errors; the full HermitCrab NUnit project passes, including the new ParseWords_* cases; CSharpier checks the five changed files; comment hygiene is clean. Repository-wide ./local_check.sh and coverage were not run locally.

Next: review the ParseWords signature and the degree-1 requirement; say if you want the requirement relaxed.


Reading this a year from now -- start here

ParseWords exists so a caller with a queue of pending words can spend parallelism across words instead of inside one word. The measurements, FieldWorks adoption notes and the PanGloss comparison below are the reasoning behind the API shape; the tree only shows the API.

Evidence: measured speedups (wall time, secondary)

A local benchmark harness, not part of this PR, measured four concurrent degree-1 words at 3.05x on Sena and 2.20x on Mbugwe versus the degree-0 serial-word default. Those figures include the separate #546 shared template optimization. The same experiment with sharing disabled measured 2.42x and 1.47x.

Caveats: three-pass median wall timings on a shared machine, excluding construction, over small completed cohorts (62 Sena positions; 15 of 42 sampled Mbugwe positions). The harness used one Language/Morpher per worker, whereas this API shares one fixed Morpher. Hard or capped words remain unmeasured; the larger Sena run was unstable under its watchdog. Twenty concurrent words performed worse than four on both primary real-input cohorts, hence the default of 4.

Decisions and why
  • Degree-1 Morpher required. Bounded parallelism across words with sequential work inside each word. The evidence does not justify silently changing the existing single-word constructor default.
  • Differs from PanGloss on purpose. Its memory architecture differs, so the C# API puts parallel work across words and sequential paths within each word.
  • Tracing is serial. Traced calls sharing a trace manager must not overlap, so a traced batch runs on the calling thread.
Deferred: what FieldWorks adoption needs

FieldWorks's parser worker currently submits one word at a time and constructs Morpher without a degree argument (Src/LexText/ParserCore/HCParser.cs:195 in the FieldWorks repo; not checked here). It would need to: construct a degree-1 Morpher; gather a finite batch from its pending queue; call ParseWords; map each indexed result, error and trace back to the original request. Language reloads and option changes must wait for the batch to finish; selectors and custom rules must support concurrent calls. FieldWorks is not changed here.

🤖 Generated with Claude Code


This change is Reviewable

@codecov-commenter

codecov-commenter commented Oct 29, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 75.64%. Comparing base (e7422c1) to head (e02b1bd).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #582      +/-   ##
==========================================
+ Coverage   75.53%   75.64%   +0.11%     
==========================================
  Files         455      456       +1     
  Lines       38933    38985      +52     
  Branches     5428     5435       +7     
==========================================
+ Hits        29407    29492      +85     
+ Misses       8376     8347      -29     
+ Partials     1150     1146       -4     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@claude

This comment has been minimized.

@claude

This comment has been minimized.

@johnml1135
johnml1135 requested a review from jtmaxwell3 November 3, 2026 02:11
@jtmaxwell3

Copy link
Copy Markdown
Collaborator

FieldWorks currently displays what word is being parsed and the number of words remaining in the lower left corner of the display.  This is very useful feedback for a long running process. Seeing what word is being parsed lets you see which words are taking a long time. Maybe ParseWords could take an optional callback function that is called when a word starts parsing and when it stops? The set of words currently being parsed could be displayed in the lower left corner.

@jtmaxwell3

Copy link
Copy Markdown
Collaborator

An alternative to passing limits like MaxAlternatives in as an argument to internal functions is to make them be readonly once Morpher has been initialized. In FieldWorks, the Hermit Crab grammar gets recompiled whenever any of the parameters are changed. Are there situations where we want to change MaxAlternatives on the fly? If so, maybe we should make it be an argument to ParseWord(s) rather than a variable in Morpher.

@johnml1135

Copy link
Copy Markdown
Collaborator Author

FieldWorks currently displays what word is being parsed and the number of words remaining…

Done in 4adfd57. ParseWords takes an optional callback that is called when each word starts and finishes, with its input index, surface form and (on finish) its WordParseResult. Callbacks run on the parsing threads and may overlap, so FieldWorks needs to marshal to the UI thread and keep them quick. If a callback fails, every word still parses and gets its finish notification, but ParseWords then throws instead of returning its result list.

@johnml1135

Copy link
Copy Markdown
Collaborator Author

An alternative to passing limits like MaxAlternatives in as an argument to internal functions is to make them be readonly once Morpher has been initialized…

Agreed, and done in 4adfd57. MaxAlternatives is now a constructor argument and stays fixed for the Morpher's lifetime. The rules read it directly instead of receiving it on every call. The public setter is intentionally removed, so callers that set it must move to the constructor and recompile. For FieldWorks (C# 8, so no init-only setters) that means new Morpher(m_traceManager, m_language, 0, maxAlternatives) in HCParser.cs in place of the initializer entry. Degree 0 keeps today's single-word behaviour; adopting ParseWords later (LT-22885) uses degree 1.

@claude

This comment has been minimized.

@claude

This comment has been minimized.

@jtmaxwell3

Copy link
Copy Markdown
Collaborator

I don't think that you need to throw WordParseProgressException at the end of ParseWords. Just return ordered. ordered has the exceptions stored in WordParseResults. Throwing WordParseProgressException just complicates the call to ParseWords.

@johnml1135

Copy link
Copy Markdown
Collaborator Author

I don't think that you need to throw WordParseProgressException at the end of ParseWords. Just return ordered.

Agreed, removed in 852c422. One distinction: ordered holds the parse failures (WordParseResult.Error), but errors thrown by the progress callback were never stored there, so returning ordered alone would have hidden them. The catch is now gone too: a throwing callback ends the batch and reaches the caller directly (as AggregateException when words run concurrently, the usual Parallel.For behaviour). In FieldWorks that lands on the parser thread's UnhandledException, which ParserListener already sends to ErrorReporter, so callback bugs show up as crash reports. Parse failures still come back per word in the results.

@claude

This comment has been minimized.

@jtmaxwell3

Copy link
Copy Markdown
Collaborator

Since MaxAlternatives is now readonly, I don't think that you need to make any changes to AnalysisLanguageRule or AnalysisStratumRule. Can we just revert those changes?

johnml1135 and others added 6 commits November 20, 2026 09:22
Require degree-1 compilation for batch parsing, preserve ordered per-word results and errors, and serialize traced batches. Keep alternative budgets on invocation-local rule wrappers and retain the existing constructor default.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ed progress callbacks.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…pping them

Remove WordParseProgressException. A throwing callback is the caller's bug, so it now
ends the batch like any delegate passed to Parallel.For; parse failures still land in
WordParseResult.Error.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…at MaxAlternatives is fixed at construction.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@johnml1135
johnml1135 force-pushed the feat/hc-parse-words-batch branch from 852c422 to e02b1bd Compare November 20, 2026 14:46
@johnml1135

Copy link
Copy Markdown
Collaborator Author

Since MaxAlternatives is now readonly, I don't think that you need to make any changes to AnalysisLanguageRule or AnalysisStratumRule. Can we just revert those changes?

Done in e02b1bd. Both files are back to master, so the PR no longer touches them; they still set the rules' MaxAlternatives from the now-fixed Morpher value on each application, which is the same value every time. The diff is now Morpher.cs, WordParseResult.cs and MorpherTests.cs. The branch is also rebased onto current master. The MorpherTests (51) pass, the HermitCrab project passes and the comment-hygiene scan is clean. local_check.sh stops at the native SentencePiece build because cmake is missing locally, so that step is not verified here.

@claude

claude Bot commented Nov 20, 2026

Copy link
Copy Markdown
Contributor
  1. Verdict: approve. e02b1bd restores AnalysisLanguageRule.cs and AnalysisStratumRule.cs to master, as jtmaxwell3 asked, and adds no new findings.
  2. Most important: the PR body is now out of date. It still says "parsing writes no shared compiled rule state (AnalysisStratumRule.cs, AnalysisLanguageRule.cs)", and it still points at ParseWords_CapturesMaxAlternativesPerWord in AnalysisStratumRule.cs. On master, AnalysisLanguageRule.cs:37 writes _rules[i].MaxAlternatives on every application. Under ParseWords the concurrent words all write that value into the same rule objects. Since 4adfd57 fixes the value at construction, every write stores the same number, so I found no defect, but the body should say this instead.
  3. Counts: Critical 0, Important 0, Low 0 open (F3 withdrawn).
  4. Ran: dotnet test on the HermitCrab tests filtered to MorpherTests, Release, net10.0 locally: 51/51 passed. I also ran ParseWords_UsesBoundedWordConcurrency with -e DOTNET_PROCESSOR_COUNT=1: 2/2 passed, which settles the starved-runner question from earlier rounds. gh pr checks 582: Comment hygiene (advisory) passed on both OSes; Build on ubuntu-22.04, Build on windows-latest and claude-review are pending.
  5. Not verified: history was rewritten since 852c422 (compare reports diverged), so I re-reviewed the whole PR against master. I did not check the FieldWorks HCParser.cs change, which lives outside this repo.

Public API: adds Morpher.ParseWords, WordParseResult and the Morpher(ITraceManager, Language, int, int) constructor. It removes the public Morpher.MaxAlternatives setter, which breaks source and binary compatibility for consumers of the netstandard2.0 package; the PR body states this. Target framework, package, and sillsdev/machine.py parity: none verified.

Findings: F1 addressed, F2 withdrawn, F3 withdrawn (its code left the PR in e02b1bd), F4 withdrawn.

Reviewed at e02b1bd

@johnml1135
johnml1135 merged commit c73ca66 into master Nov 20, 2026
11 checks passed
@johnml1135
johnml1135 deleted the feat/hc-parse-words-batch branch November 20, 2026 18:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants