Skip to content
Merged
Changes from all commits
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
31 changes: 19 additions & 12 deletions lmdeploy/pytorch/backends/dlinfer/ascend/op_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ class AscendOpsBackend(DlinferOpsBackend):
total_slots = None
max_batches = None
dist_meta: DistMeta = None
fia_causal_masks: dict[torch.device, torch.Tensor] = {}

@staticmethod
def get_name() -> str:
Expand Down Expand Up @@ -189,6 +190,14 @@ def get_total_slots():
cls.total_slots = cls.total_slots.view(block_num, block_size)
return cls.total_slots

def get_fia_causal_mask():
device = step_context.block_offsets.device
mask = cls.fia_causal_masks.get(device)
if mask is None:
mask = torch.triu(torch.ones(2048, 2048, dtype=torch.int8, device=device), diagonal=1)
cls.fia_causal_masks[device] = mask
return mask
Comment thread
wanfengcxz marked this conversation as resolved.
Comment on lines +193 to +199

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

kernel不是按绝对位置直接访问,而是计算当前 Q/KV tile 相对于 causal 对角线的位置。


def get_cpu_seqlens(is_decoding, is_prefill_no_cache):
"""Get sequence lengths on CPU.

Expand Down Expand Up @@ -255,17 +264,10 @@ def get_kv_start_indices_and_attention_mask(is_decoding, is_prefill_no_cache, q_
slots = slot_tables[history_length:kv_seq_len]
kv_start_indices.append(slots)

if is_prefill_no_cache:
attention_mask.append(
torch.triu(torch.ones(max_q_seq_len,
max_kv_seq_len,
dtype=step_context.kv_caches[0][0].dtype,
device=step_context.block_offsets.device),
diagonal=max_kv_seq_len - max_q_seq_len + 1))
else:
attention_mask.append(
torch.triu(torch.ones(2048, 2048, dtype=torch.bool, device=step_context.block_offsets.device),
diagonal=1))
# Standard GQA/MHA, MLA and paged prefill all use FIA sparse
# mode 3. Reuse the fixed split-fuse causal-mask template
# across all steps, following vllm-ascend.
attention_mask.append(get_fia_causal_mask())

kv_start_indices = torch.cat(kv_start_indices)

Expand Down Expand Up @@ -516,7 +518,8 @@ def build_graph_runner(model: torch.nn.Module, model_config: ModelConfig, cache_
AscendOpsBackend.enable_graph = not backend_config.eager_mode
AscendOpsBackend.max_batches = cache_config.max_batches
from dlinfer.framework.lmdeploy_ext.cudagraph.ascend_cudagraph import AscendGraphRunner
return AscendGraphRunner(model, model_config, cache_config, backend_config, device)
is_mla = model_config.k_head_dim != model_config.v_head_dim
return AscendGraphRunner(model, model_config, cache_config, backend_config, device, is_mla=is_mla)

@staticmethod
def init():
Expand All @@ -527,6 +530,10 @@ def init():
is emitted but non-linear-attention models are unaffected.
"""

from dlinfer.vendor.ascend.version import ensure_ascend_runtime

ensure_ascend_runtime()

try:
from torch_npu.contrib import transfer_to_npu # noqa: F401
except ImportError:
Expand Down
Loading