[Spec][Ngram] 1/N: Reference based Speculative Decoding refactor#20393
Merged
hnyls2002 merged 12 commits intosgl-project:mainfrom Mar 22, 2026
Merged
[Spec][Ngram] 1/N: Reference based Speculative Decoding refactor#20393hnyls2002 merged 12 commits intosgl-project:mainfrom
hnyls2002 merged 12 commits intosgl-project:mainfrom
Conversation
Contributor
|
Warning You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again! |
edc131f to
e35bd8b
Compare
209d17d to
4ef7157
Compare
Collaborator
Author
|
/tag-and-rerun-ci |
kpham-sgl
commented
Mar 13, 2026
| | `--speculative-ngram-min-bfs-breadth` | The minimum breadth for BFS (Breadth-First Search) in ngram speculative decoding. | `1` | Type: int | | ||
| | `--speculative-ngram-max-bfs-breadth` | The maximum breadth for BFS (Breadth-First Search) in ngram speculative decoding. | `10` | Type: int | | ||
| | `--speculative-ngram-match-type` | The match type for cache tree. | `BFS` | `BFS`, `PROB` | | ||
| | `--speculative-ngram-match-type` | Ngram tree-building mode. `BFS` selects recency-based expansion and `PROB` selects frequency-based expansion. This setting is forwarded to the ngram cache implementation. | `BFS` | `BFS`, `PROB` | |
Collaborator
Author
There was a problem hiding this comment.
Open to put these doc change in a separate PR
hnyls2002
requested changes
Mar 20, 2026
Collaborator
|
@kpham-sgl This PR generally looks good, only does some migration and cleanup. For the next PRs:
|
19 tasks
- Rename module/extension (ngram_corpus_cpp), sources, and TrieCache→Trie - Update ngram_worker and registered tests Made-with: Cursor
Align pybind name and Python attribute with actual semantics: Ngram is a concurrency wrapper, not a trie.
hnyls2002
approved these changes
Mar 22, 2026
Collaborator
|
/rerun-ut test_ngram_corpus.py |
Collaborator
|
/rerun-ut test_ngram_speculative_decoding.py |
Contributor
|
❌ No test file found matching |
Contributor
|
✅ Triggered |
Contributor
OrangeRedeng
pushed a commit
to OrangeRedeng/sglang
that referenced
this pull request
Mar 22, 2026
0-693
pushed a commit
to 0-693/sglang
that referenced
this pull request
Mar 25, 2026
dutsc
pushed a commit
to dutsc/sglang
that referenced
this pull request
Mar 30, 2026
JustinTong0323
pushed a commit
to JustinTong0323/sglang
that referenced
this pull request
Apr 7, 2026
yhyang201
pushed a commit
to yhyang201/sglang
that referenced
this pull request
Apr 22, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
1/N: Refactor the monolithic C++
Ngramclass into a template-based architecture to enable pluggable cache backends (e.g., Suffix Automaton in 2+/N), no behavioural changes (except [NEW] tag).Modifications
Extract
Result,Node,fillResult()intoresult.h/.cpp(shared across backends)Extract
TrieNode+Trieintotrie.h/.cpp(trie data structure + tree building:insert,buildRecency,buildFrequency,squeeze,reset)Convert
Ngramintoclass Ngram— thin concurrency wrapper that holds both SAM (in later PRs) - for corpus and prefix and running Trie - for decoded tokensAdd comprehensive test suite (
test_ngram_corpus.py): golden-output tests for BFS/PROB modes, reset, squeeze/eviction, batch consistency, mask invariants, frequency boosting, recency ordering, etc.[NEW] Wire
match_typeparameter throughngram_worker.py[NEW] Update docs for the above parameter
Future plans
#21052
Accuracy Tests
Pass
python -m pytest test/registered/spec/utils/test_ngram_cache.py -vpython -m pytest test/registered/spec/test_ngram_speculative_decoding.py::TestNgramSpeculativeDecodingTriton -xvsBenchmarking and Profiling
Checklist
Review Process
/tag-run-ci-label,/rerun-failed-ci,/tag-and-rerun-ci