DSA 相关分布式算子(lightning_indexer、npu_dense_lightning_indexer_softmax_lse、npu_dense_lightning_indexer_grad_kl_loss、npu_sparse_lightning_indexer_grad_kl_loss、npu_sparse_flash_attention)在 preprocess 中对 actual_seq_len(q/kv 的累积序列长度)入参无条件调用 .to_local(),即要求它必须是 Replicate 的 DTensor。
lightning_indexer
npu_dense_lightning_indexer_softmax_lse
npu_dense_lightning_indexer_grad_kl_loss
npu_sparse_lightning_indexer_grad_kl_loss
npu_sparse_flash_attention
preprocess
actual_seq_len
.to_local()
但 actual_seq_len 是网络在前向过程中构造出来的普通 Tensor,用户调用分布式算子时无法保证把它包装成 DTensor。当传入普通 Tensor 时,.to_local() 抛 AttributeError。
AttributeError
各算子 preprocess 对 actual_seq_len 的处理存在两类问题:
if x is not None: x.to_local()
_to_local
两个矛盾点:
softmax_lse
isinstance(DTensor)
_to_local_seq_len
to_local
None
actual_seq_qlen
actual_seq_klen
npu_dense_lightning_indexer_softmax_lse(q, k, w, actual_seq_qlen=..., actual_seq_klen=..., layout='TND')
actual_seq_qlen.to_local()
AttributeError: 'Tensor' object has no attribute 'to_local'
mindspore/hyper-parallel#871
该问题是怎么引起的?
DSA 相关分布式算子(
lightning_indexer、npu_dense_lightning_indexer_softmax_lse、npu_dense_lightning_indexer_grad_kl_loss、npu_sparse_lightning_indexer_grad_kl_loss、npu_sparse_flash_attention)在preprocess中对actual_seq_len(q/kv 的累积序列长度)入参无条件调用.to_local(),即要求它必须是 Replicate 的 DTensor。但
actual_seq_len是网络在前向过程中构造出来的普通 Tensor,用户调用分布式算子时无法保证把它包装成 DTensor。当传入普通 Tensor 时,.to_local()抛AttributeError。现状(根因分析)
各算子
preprocess对actual_seq_len的处理存在两类问题:npu_dense_lightning_indexer_softmax_lseif x is not None: x.to_local()AttributeErrorlightning_indexerif x is not None: x.to_local()npu_sparse_flash_attention_to_local套在所有入参npu_dense_lightning_indexer_grad_kl_loss_to_localnpu_sparse_lightning_indexer_grad_kl_loss_to_local两个矛盾点:
softmax_lse/lightning_indexer对actual_seq_len直接.to_local(),不兼容网络构造的普通 Tensor。_to_local(isinstance(DTensor)判断)套在所有入参上,导致actual_seq_len之外的主张量(query/key/value/index/weights、rope 等)即使传普通 Tensor 也被静默透传,而这些入参必须是 DTensor 才能推导 layout。修复方案
_to_local_seq_len:仅当入参是 DTensor 时to_local,普通 Tensor /None原样透传。只用于actual_seq_len。_to_local:None透传(可选参数未提供),否则直接to_local,普通 Tensor 触发AttributeError(明确拒绝,避免静默放行)。actual_seq_len从 Replicate DTensor 改为普通 Tensor 传入;UT 增加「普通 Tensor 透传」「主张量拒绝普通 Tensor」的回归用例。重现步骤
actual_seq_qlen/actual_seq_klen用网络构造的普通 Tensor(非 DTensor)。npu_dense_lightning_indexer_softmax_lse(q, k, w, actual_seq_qlen=..., actual_seq_klen=..., layout='TND')。preprocess中actual_seq_qlen.to_local()触发报错。报错信息
关联 PR
mindspore/hyper-parallel#871