-
Notifications
You must be signed in to change notification settings - Fork 630
fix: fix guided decoding state corruption in turbomind when tp>1 #4167
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
The sampled token are not broadcasted from tp_rank0, so all ranks should do the same sampling process to make sure the next token is same on all ranks. I think the problem may be the state of |
OK so we need to copy the |
I believe that a quick fix should be making |
All ranks should have the same next token and the next token are computed by dynamic decoding, so we should make sure the dynamic decoding process are same on all ranks. I think we can construct n_ranks of matchers here like and chose the correspond matcher here like |
8ada1ea to
e7a7055
Compare
e7a7055 to
adb0148
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Pass
h_tp_grouptoGuidedDecodeUpdateLayer h_tp_group->Sync()here so that all ranks completed filling their host mask buffer before any rank tries to update matcher state.AcceptTokenwhenh_tp_group->rank() == 0
In addition, a stream sync is required after the copy. need_apply as in GuidedDecodeMaskLayer is neede here to avoid the copy / sync cost when guided decoding is not needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sadly, not only AcceptToken but also FillNextTokenBitmask modify the shared matcher state. So if we need multiple times of sync and shared bit mask, that will kill the performance I believe.
So I take @irexyc 's advice to just dup the state for each thread.
|
Vote for making GuidedDecodeUpdateLayer only executed in rank 0 and calling GuidedDecodeUpdateLayer::Forward only when all ranks finish the sampling. |
Motivation
When serving models with tensor parallelism (
--tp >= 2), enabling guided decoding (e.g.,response_format: {"type": "json_schema"}) causes segmentation faults。Root Cause: The guided decoding state management layers (
GuidedDecodeMaskLayerandGuidedDecodeUpdateLayer) were being instantiated on all tensor parallelism (TP) ranks. Each rank independently modified shared decoding state, causing race conditions and memory corruption. In distributed inference, only rank 0 should orchestrate guided decoding logic while other ranks perform parallel computation.Modification
Eliminate shared mutable state by allocating an independent GrammarMatcher instance per TP rank. Enhance the decoding pipeline with rank awareness so each thread operates on its own isolated matcher, ensuring thread safety while maintaining deterministic behavior across ranks.
Fixes #4152