From be16eac8aed00634ebfdff22d1fcd66983ac1dfb Mon Sep 17 00:00:00 2001 From: zhenggf Date: Mon, 22 Jun 2026 17:20:16 +0800 Subject: [PATCH 1/3] fix: honor SLA topk environment setting (cherry picked from commit e8ee93a79bd20dce2d084e992a8e140710f2c9b6) --- lightx2v_platform/ops/attn/hygon_dcu/flash_attn.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lightx2v_platform/ops/attn/hygon_dcu/flash_attn.py b/lightx2v_platform/ops/attn/hygon_dcu/flash_attn.py index d1fff4fe2..a497dc421 100644 --- a/lightx2v_platform/ops/attn/hygon_dcu/flash_attn.py +++ b/lightx2v_platform/ops/attn/hygon_dcu/flash_attn.py @@ -114,7 +114,7 @@ def half(x): softmax_scale = 1.0 / math.sqrt(q.shape[-1]) # Use Flash Attention 2.6.1 (ROCm version) with varlen interface if SAPRDE_LINEAR_ATTN and int(os.getenv("USE_SLA", 0)) and q.shape[1] == k.shape[1]: - topk_value = float(os.getenv("SPARSE_ATTN_TOPK", "0.5")) + topk_value = float(os.getenv("SPARSE_ATTN_TOPK", "0.4")) q = q_flat.unsqueeze(0) k = k_flat.unsqueeze(0) @@ -124,7 +124,7 @@ def half(x): q, k, v, - topk=0.4, + topk=topk_value, ) else: output = flash_attn_varlen_func( From a89f1b7fddbd002e8ad87cd90a6fbf41456894c4 Mon Sep 17 00:00:00 2001 From: zhenggf Date: Tue, 30 Jun 2026 14:24:17 +0800 Subject: [PATCH 2/3] fix: validate Hygon SLA topk environment value --- .../ops/attn/hygon_dcu/flash_attn.py | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/lightx2v_platform/ops/attn/hygon_dcu/flash_attn.py b/lightx2v_platform/ops/attn/hygon_dcu/flash_attn.py index a497dc421..4ac599fc1 100644 --- a/lightx2v_platform/ops/attn/hygon_dcu/flash_attn.py +++ b/lightx2v_platform/ops/attn/hygon_dcu/flash_attn.py @@ -25,6 +25,26 @@ FLASH_ATTN_AVAILABLE = False +def _get_sparse_attn_topk(default: float = 0.4) -> float: + raw_topk = os.getenv("SPARSE_ATTN_TOPK", str(default)) + try: + topk_value = float(raw_topk) + except (TypeError, ValueError): + logger.warning( + "Invalid SPARSE_ATTN_TOPK={!r}; falling back to {:.3f}", raw_topk, default + ) + return default + + if not 0.0 < topk_value <= 1.0: + logger.warning( + "SPARSE_ATTN_TOPK={!r} is outside (0.0, 1.0]; falling back to {:.3f}", + raw_topk, + default, + ) + return default + return topk_value + + @PLATFORM_ATTN_WEIGHT_REGISTER("flash_attn_hygon_dcu") class FlashAttnHygonDcu(AttnWeightTemplate): """ @@ -114,7 +134,7 @@ def half(x): softmax_scale = 1.0 / math.sqrt(q.shape[-1]) # Use Flash Attention 2.6.1 (ROCm version) with varlen interface if SAPRDE_LINEAR_ATTN and int(os.getenv("USE_SLA", 0)) and q.shape[1] == k.shape[1]: - topk_value = float(os.getenv("SPARSE_ATTN_TOPK", "0.4")) + topk_value = _get_sparse_attn_topk() q = q_flat.unsqueeze(0) k = k_flat.unsqueeze(0) From 5999f502ff448221b9316b80697a01ce5ca641a8 Mon Sep 17 00:00:00 2001 From: zhenggf Date: Wed, 1 Jul 2026 14:59:32 +0800 Subject: [PATCH 3/3] style: format Hygon SLA topk changes --- lightx2v_platform/ops/attn/hygon_dcu/flash_attn.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lightx2v_platform/ops/attn/hygon_dcu/flash_attn.py b/lightx2v_platform/ops/attn/hygon_dcu/flash_attn.py index 4ac599fc1..c9a4d0cfe 100644 --- a/lightx2v_platform/ops/attn/hygon_dcu/flash_attn.py +++ b/lightx2v_platform/ops/attn/hygon_dcu/flash_attn.py @@ -30,9 +30,7 @@ def _get_sparse_attn_topk(default: float = 0.4) -> float: try: topk_value = float(raw_topk) except (TypeError, ValueError): - logger.warning( - "Invalid SPARSE_ATTN_TOPK={!r}; falling back to {:.3f}", raw_topk, default - ) + logger.warning("Invalid SPARSE_ATTN_TOPK={!r}; falling back to {:.3f}", raw_topk, default) return default if not 0.0 < topk_value <= 1.0: