[RFC] Native Ascend NPU Support for Liger Kernel
Maintainers usually reply within 3 days
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 25/100
- Issue type
- Feature
- Clarity
- Needs clarification
- Activity status
- Stale
- Domain
- machine-learning
Research direction
Begin with the infer_device() entry point and the geglu and fused_neighborhood_attention operators described in the proposal. Review how device-specific BLOCK_SIZE handling and NPU CI would fit the existing project, then define validation for native-run and adjusted operators; the RFC does not name files or tests, so scope and completion criteria need maintainer agreement.
Written by the indexing model from the issue text.
Description
1. Background & Motivation
Ascend NPU is a default PyTorch device backend, natively compatible with ecosystems like Transformers, FlagGems, and Llama Factory. We’re also enabling Triton support (repo: triton-ascend).
Liger Kernel’s wide adoption has led to growing user requests for NPU support. This proposal aims to advance native adaptation, with community input welcome.
2. Proposed Implementation Steps
Adaptation proceeds in phases, with no breaking changes to existing devices.
2.1 Device Support Integration
Extend device detection logic to include NPU:
def infer_device():
"""Get current device name based on available devices"""
if torch.cuda.is_available(): # Works for Nvidia/AMD
return "cuda"
elif is_npu_available(): # Ascend NPU check
return "npu"
elif torch.xpu.is_available():
return "xpu"
else:
return "cpu"
2.2 Operator Compatibility Guarantee
- Native-run Operators: Some (e.g.,
geglu,fused_neighborhood_attention) work on NPU via basic device adaptation. - NPU-specific Adjustments: Partial operators need
BLOCK_SIZEtweaks to avoid Unified Buffer (UB) overflow.
Example: geglu Operator Modification
Original code:
@triton.jit
def _geglu_tanh_forward_kernel(a, b, c, stride, n_cols: tl.constexpr, BLOCK_SIZE: tl.constexpr):
program_id = tl.program_id(0).to(tl.int64)
a += program_id * stride
b += program_id * stride
c += program_id * stride
col_offsets = tl.arange(0, BLOCK_SIZE)
mask = col_offsets < n_cols
a_row = tl.load(a + col_offsets, mask=mask, other=0).to(tl.float32)
b_row = tl.load(b + col_offsets, mask=mask, other=0)
# GELU tanh approximation
sqrt_2_over_pi = 0.7978845608028654
a_cubed = a_row ** 3
tanh_arg = sqrt_2_over_pi * (a_row + 0.044715 * a_cubed)
c_row = 0.5 * a_row * (1 + tanh(tanh_arg)) * b_row
tl.store(c + col_offsets, c_row, mask=mask)
NPU-adapted code (add BLOCK_SIZE_SUB for UB overflow prevention):
@triton.jit
def _geglu_tanh_forward_kernel(a, b, c, stride, n_cols: tl.constexpr,
BLOCK_SIZE: tl.constexpr, BLOCK_SIZE_SUB: tl.constexpr):
program_id = tl.program_id(0).to(tl.int64)
base_offset = program_id * stride
num_sub_blocks = tl.cdiv(BLOCK_SIZE, BLOCK_SIZE_SUB)
for sub_block_idx in range(num_sub_blocks):
col_offsets = tl.arange(0, BLOCK_SIZE_SUB) + sub_block_idx * BLOCK_SIZE_SUB
mask = col_offsets < n_cols
a_row = tl.load(a + base_offset + col_offsets, mask=mask, other=0).to(tl.float32)
b_row = tl.load(b + base_offset + col_offsets, mask=mask, other=0)
# GELU tanh approximation (same as original)
sqrt_2_over_pi = 0.7978845608028654
a_cubed = a_row ** 3
tanh_arg = sqrt_2_over_pi * (a_row + 0.044715 * a_cubed)
c_row = 0.5 * a_row * (1 + tanh(tanh_arg)) * b_row
tl.store(c + base_offset + col_offsets, c_row, mask=mask)
Note: BLOCK_SIZE_SUB = BLOCK_SIZE for non-NPU devices.
2.3 Performance Optimization
- Tune operator parameters for NPU performance.
- Future plan: Add NPU-affine APIs to
triton-ascend.
2.4 CI Integration
We’re applying for NPU devices to integrate into Liger Kernel’s native CI for continuous validation.
3. Code Organization Proposal
NPU modifications won’t break existing code. To improve readability, we consider organizing device-specific operators into dedicated directories (referencing FlagGems):
Reference: FlagGems Muti backend
Seeking community feedback on this approach.
4. Conclusion
We aim to extend Liger Kernel to Ascend NPU while ensuring compatibility/performance. Your input will help refine this initiative.
- Dominant language
- Python
- Stars
- 6.7k
- Forks
- 673
- Avg merge
- 4d 20h
- Merged PRs (30d)
- 19
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from linkedin/Liger-Kernel
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
linkedin/Liger-Kernel#1693 ·
Maintainers usually reply within 3 days
-
[Bug] GroupNorm kernels overflow int32 sample offsets (`batch_idx * X_row_stride`) past 2^31 elementsPossibly taken A pull request linked to this issue is open or already merged. Open
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
linkedin/Liger-Kernel#1692 ·
Maintainers usually reply within 3 days
-
[Bug] LigerGroupNormFunction.backward crashes on any 4D (N, C, H, W) tensor (H > 1) and hardcodes tl.bfloat16 for float16 inputsPossibly taken @shotsan claimed this 2 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
linkedin/Liger-Kernel#1676 · 2 comments ·
Maintainers usually reply within 3 days
-
[Bug] LigerFusedGateUpSiLUMulFunction.backward crashes with shape mismatch on any 3D (B, T, 2*H) input tensorPossibly taken @shotsan claimed this 2 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
linkedin/Liger-Kernel#1675 · 1 comment ·
Maintainers usually reply within 3 days
-
[Bug] Silent backward gradient corruption in LigerCrossEntropy on non-contiguous logits (returns raw input logits as dL/dlogits)Possibly taken @shotsan claimed this 5 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
linkedin/Liger-Kernel#1674 ·
Maintainers usually reply within 3 days
All issues in linkedin/Liger-Kernel
Similar issues
-
ui
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
ARTPARK-SAHAI-ORG/calibrate-frontend#919 ·
Maintainers usually reply within 1 day
-
CI: MSYS2 jobs fail — mingw python3 package renamed upstream to pythonPossibly taken @ronaldtse claimed this today. Open
Difficulty 1/5 Under an hour Newbie friendliness 95/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
pydantic/pytest-examples#102 · 1 comment ·