-
Notifications
You must be signed in to change notification settings - Fork 19
fix: resolve prefix caching crashes with MTP speculative decoding #234
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
Draft
valarLip
wants to merge
1
commit into
main
Choose a base branch
from
ds_prefix_cache
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,14 @@ | ||
| # SPDX-License-Identifier: MIT | ||
| # Copyright (C) 2024-2025, Advanced Micro Devices, Inc. All rights reserved. | ||
|
|
||
| import logging | ||
| from abc import ABC, abstractmethod | ||
| from typing import Any, Dict, Generic, Optional, Type, TypeVar | ||
|
|
||
| import torch | ||
| from atom.model_engine.scheduler import ScheduledBatch | ||
|
|
||
| logger = logging.getLogger("atom") | ||
| from atom.model_ops.attention_mla import MLAModules | ||
| from atom.utils import CpuGpuBuffer | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| from atom.utils.block_convert import block_table_convert_triton | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
@@ -141,18 +144,23 @@ def prepare_prefill(self, batch: ScheduledBatch): | |
| sum_scheduled_tokens = batch.total_tokens_num_prefill | ||
| var = self.model_runner.forward_vars | ||
| positions = [] | ||
| cu_seqlens_q = [0] | ||
| cu_seqlens_k = [0] | ||
| max_seqlen_q = 0 | ||
| max_seqlen_k = 0 | ||
| slot_mapping = [] | ||
| has_cached = False | ||
| # seqs = list(batch.seqs.values()) | ||
| # seqs = seqs[:bs] | ||
| for i in range(bs): | ||
| seqlen = batch.context_lens[i] | ||
| cached_seqlen = batch.num_cached_tokens[i] | ||
| if cached_seqlen > 0: | ||
| has_cached = True | ||
| positions.extend(list(range(cached_seqlen, seqlen))) | ||
| seqlen_q = seqlen - cached_seqlen | ||
| seqlen_k = seqlen | ||
| cu_seqlens_q.append(cu_seqlens_q[-1] + seqlen_q) | ||
| cu_seqlens_k.append(cu_seqlens_k[-1] + seqlen_k) | ||
| max_seqlen_q = max(seqlen_q, max_seqlen_q) | ||
| max_seqlen_k = max(seqlen_k, max_seqlen_k) | ||
|
|
@@ -166,17 +174,29 @@ def prepare_prefill(self, batch: ScheduledBatch): | |
| ) // self.model_runner.block_size | ||
| last_block_tokens = batch.last_block_num_tokens[i] | ||
| block_table = batch.block_tables[i] | ||
| for i in range(num_cached_blocks, num_blocks): | ||
| start = block_table[i] * self.model_runner.block_size | ||
| if i != num_blocks - 1: | ||
| for blk_idx in range(num_cached_blocks, num_blocks): | ||
| start = block_table[blk_idx] * self.model_runner.block_size | ||
| if blk_idx != num_blocks - 1: | ||
| end = start + self.model_runner.block_size | ||
| else: | ||
| end = start + last_block_tokens | ||
| slot_mapping.extend(list(range(start, end))) | ||
| if cu_seqlens_k[-1] > batch.total_tokens_num: # prefix cache | ||
| if has_cached: | ||
| self.prepare_block_tables(batch) | ||
| # Validate metadata consistency | ||
| assert ( | ||
| len(positions) == sum_scheduled_tokens | ||
| ), f"positions length {len(positions)} != sum_scheduled_tokens {sum_scheduled_tokens}" | ||
| if batch.block_tables: | ||
| assert ( | ||
| len(slot_mapping) == sum_scheduled_tokens | ||
| ), f"slot_mapping length {len(slot_mapping)} != sum_scheduled_tokens {sum_scheduled_tokens}" | ||
| assert ( | ||
| cu_seqlens_q[-1] == sum_scheduled_tokens | ||
| ), f"cu_seqlens_q[-1]={cu_seqlens_q[-1]} != sum_scheduled_tokens={sum_scheduled_tokens}" | ||
| var["positions"].np[:sum_scheduled_tokens] = positions | ||
| var["slot_mapping"].np[: len(slot_mapping)] = slot_mapping | ||
| var["cu_seqlens_q"].np[: bs + 1] = cu_seqlens_q | ||
| cu_seqlens_k = torch.tensor(cu_seqlens_k, dtype=torch.int32, pin_memory=True) | ||
| var["context_lens"].np[:bs] = batch.context_lens[:bs] | ||
| min_seqlen_q = 0 | ||
|
|
@@ -186,6 +206,8 @@ def prepare_prefill(self, batch: ScheduledBatch): | |
| ("slot_mapping", len(slot_mapping)), | ||
| ("context_lens", bs), | ||
| ] | ||
| if has_cached: | ||
| vars_used.append(("block_tables", bs)) | ||
|
|
||
| ctx = {el: var[el].copy_to_gpu(num) for el, num in vars_used} | ||
| if self.block_ratio > 1 and "block_tables" in ctx: | ||
|
|
@@ -202,6 +224,7 @@ def prepare_prefill(self, batch: ScheduledBatch): | |
| max_seqlen_k=max_seqlen_k, | ||
| min_seqlen_q=min_seqlen_q, | ||
| dropout_p=dropout_p, | ||
| has_cached=has_cached, | ||
| **ctx, | ||
| ) | ||
| positions = var["positions"].copy_to_gpu(sum_scheduled_tokens) | ||
|
|
||
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
Oops, something went wrong.
Oops, something went wrong.
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.
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.
Module level import not at top of file