Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions vllm_ascend/attention/attention_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,18 @@ def build(
query_start_loc_cpu = common_attn_metadata.query_start_loc_cpu[:
num_reqs
+ 1]

if common_attn_metadata.graph_pad_size > num_actual_tokens:
padded_num_tokens = common_attn_metadata.graph_pad_size - num_actual_tokens
seq_lens = torch.cat([seq_lens, torch.ones(padded_num_tokens, dtype=seq_lens.dtype, device=seq_lens.device)])
block_table_padding = torch.zeros(
(padded_num_tokens, ) + block_table.shape[1:],
dtype=block_table.dtype,
device=block_table.device)
block_table = torch.cat([block_table, block_table_padding],
dim=0)
query_start_loc_cpu = torch.cat([query_start_loc_cpu, torch.arange(query_start_loc_cpu[-1] + 1, query_start_loc_cpu[-1] + padded_num_tokens, dtype=query_start_loc_cpu.dtype, device=query_start_loc_cpu.device)])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

There are two issues in this padding logic that will cause problems during execution:

  1. query_start_loc_cpu padding is incorrect: The call to torch.arange has an off-by-one error. The end parameter is exclusive, so torch.arange(start, end) generates end - start elements. Your code generates padded_num_tokens - 1 elements, but you need to pad padded_num_tokens elements to match the padded seq_lens. This will cause a tensor size mismatch. The end argument should be query_start_loc_cpu[-1] + padded_num_tokens + 1.

  2. query_lens is not padded: You are padding seq_lens, block_table, and query_start_loc_cpu, but query_lens (calculated before this block) is not updated. This will lead to a mismatch in the number of sequences between query_lens and other padded tensors when creating AscendMetadata, causing errors downstream where query_lens is used. query_lens should also be padded with ones for the new sequences.

I've provided a suggestion to fix both issues.

            padded_num_tokens = common_attn_metadata.graph_pad_size - num_actual_tokens
            seq_lens = torch.cat([seq_lens, torch.ones(padded_num_tokens, dtype=seq_lens.dtype, device=seq_lens.device)])
            query_lens = torch.cat([query_lens, torch.ones(padded_num_tokens, dtype=query_lens.dtype, device=query_lens.device)])
            block_table_padding = torch.zeros(
                (padded_num_tokens, ) + block_table.shape[1:],
                dtype=block_table.dtype,
                device=block_table.device)
            block_table = torch.cat([block_table, block_table_padding],
                                    dim=0)
            new_query_locs = torch.arange(
                query_start_loc_cpu[-1] + 1,
                query_start_loc_cpu[-1] + padded_num_tokens + 1,
                dtype=query_start_loc_cpu.dtype,
                device=query_start_loc_cpu.device)
            query_start_loc_cpu = torch.cat([query_start_loc_cpu, new_query_locs])


query_start_loc = query_start_loc_cpu.to(self.device,
non_blocking=True)

Expand Down
5 changes: 4 additions & 1 deletion vllm_ascend/worker/model_runner_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -1619,7 +1619,10 @@ def _build_attn_state(self, num_reqs, num_scheduled_tokens,
return attn_state

def _update_graph_pad_size(self, with_prefill, graph_pad_size):
self.graph_pad_size = -1
if not with_prefill:
self.graph_pad_size = graph_pad_size
else:
self.graph_pad_size = -1

def _update_input_ids_and_positions(self, input_ids, positions,
num_input_tokens, with_prefill,
Expand Down
Loading