-
Notifications
You must be signed in to change notification settings - Fork 734
[KSM] support keep sampling mask #7146
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: release/2.5
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -432,6 +432,11 @@ async def chat_completion_stream_generator( | |
| delta=delta_message, | ||
| logprobs=logprobs_res, | ||
| draft_logprobs=draft_logprobs_res, | ||
| sampling_mask=( | ||
| self._make_sampling_mask_list(output["sampling_mask"]) | ||
| if output.get("sampling_mask") is not None | ||
| else None | ||
| ), | ||
|
Comment on lines
432
to
+439
|
||
| arrival_time=arrival_time, | ||
| speculate_metrics=output_speculate_metrics, | ||
| ) | ||
|
|
@@ -577,6 +582,7 @@ async def chat_completion_full_generator( | |
| decoder_base_url=self.tokenizer_base_url, | ||
| ) | ||
| prompt_logprobs_res_list = [[] for _ in range(num_choices)] | ||
| sampling_mask_list = [[] for _ in range(num_choices)] | ||
| speculate_metrics = [None for _ in range(num_choices)] | ||
| choices = [] | ||
| while num_choices > 0: | ||
|
|
@@ -656,6 +662,9 @@ async def chat_completion_full_generator( | |
| ) | ||
| if prompt_logprobs_res: | ||
| prompt_logprobs_res_list[idx].extend(clamp_prompt_logprobs(prompt_logprobs_res)) | ||
| output_sampling_mask = output.get("sampling_mask", None) | ||
| if output_sampling_mask is not None: | ||
| sampling_mask_list[idx].append(self._make_sampling_mask_list(output_sampling_mask)) | ||
| speculate_metrics[idx] = data["metrics"].get("speculate_metrics", None) | ||
| if data["finished"]: | ||
| trace_carrier = data.get("trace_carrier") | ||
|
|
@@ -691,6 +700,7 @@ async def chat_completion_full_generator( | |
| draft_logprob_contents=draft_logprob_contents, | ||
| response_processor=response_processor, | ||
| prompt_logprobs_res_list=prompt_logprobs_res_list, | ||
| sampling_mask_list=sampling_mask_list, | ||
| max_tokens=max_tokens, | ||
| speculate_metrics=speculate_metrics[idx], | ||
| ) | ||
|
|
@@ -745,6 +755,7 @@ async def _create_chat_completion_choice( | |
| logprob_contents: list, | ||
| draft_logprob_contents: list, | ||
| prompt_logprobs_res_list: list, | ||
| sampling_mask_list: list, | ||
| response_processor: ChatResponseProcessor, | ||
| max_tokens: int, | ||
| speculate_metrics: SpeculateMetrics | None, | ||
|
|
@@ -782,6 +793,10 @@ async def _create_chat_completion_choice( | |
| draft_logprobs_full_res = LogProbs(content=draft_logprob_contents[idx]) | ||
| if prompt_logprobs_res_list[idx]: | ||
| prompt_logprobs_full_res = prompt_logprobs_res_list[idx] | ||
| # Flatten per-step List[List[int]] into a single List[List[int]] over all tokens. | ||
| sampling_mask_full_res = None | ||
| if sampling_mask_list and sampling_mask_list[idx]: | ||
| sampling_mask_full_res = [mask for step in sampling_mask_list[idx] for mask in step] | ||
|
|
||
| num_cached_tokens[idx] = data.get("num_cached_tokens", 0) | ||
| num_input_image_tokens[idx] = data.get("num_input_image_tokens", 0) | ||
|
|
@@ -806,6 +821,7 @@ async def _create_chat_completion_choice( | |
| logprobs=logprobs_full_res, | ||
| draft_logprobs=draft_logprobs_full_res, | ||
| prompt_logprobs=prompt_logprobs_full_res, | ||
| sampling_mask=sampling_mask_full_res, | ||
| finish_reason=finish_reason, | ||
| speculate_metrics=speculate_metrics, | ||
| ) | ||
|
|
@@ -989,3 +1005,18 @@ def _make_logprob_dict( | |
| ) | ||
| for token_id, logprob, rank, token in zip(logprob_token_ids, logprobs, ranks, decoded_tokens) | ||
| } | ||
|
|
||
| @staticmethod | ||
| def _make_sampling_mask_list(sampling_mask) -> List[List[int]]: | ||
| """Wrap sampling_mask into a uniform List[List[int]] format. | ||
|
|
||
| sampling_mask is already in sparse-index form (no bool-to-index conversion needed): | ||
| Non-MTP: List[int] (indices for 1 token/step) → [[idx, ...]] | ||
| MTP: List[List[int]] (indices for N tokens/step) → [[idx, ...], ...] | ||
| """ | ||
| assert sampling_mask is not None | ||
| if sampling_mask and isinstance(sampling_mask[0], list): | ||
| # MTP: already List[List[int]], return as-is | ||
| return sampling_mask | ||
| # Non-MTP: already List[int], wrap in outer list for uniform format | ||
| return [sampling_mask] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -85,6 +85,11 @@ def top_k_top_p_sampling( | |
|
|
||
| _, ids = native_top_p_sampling(x, top_p) | ||
| else: | ||
| if top_k_list and any(x > 0 for x in top_k_list): | ||
| from fastdeploy.model_executor.ops.gpu import top_k_renorm_probs | ||
|
|
||
| x = top_k_renorm_probs(x, top_k) | ||
zeroRains marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
Comment on lines
+88
to
+92
|
||
| _, ids = paddle.tensor.top_p_sampling( | ||
| x, | ||
| top_p, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.