ascendc-api-best-practices

Guides correct usage of Ascend C kernel APIs including arithmetic, reduce, data copy, buffer, and pipeline synchronization.

6|1|Updated Apr 19, 2026
One-click install
npx skills add https://github.com/xchang1121/op-autoresearch --skill ascendc-api-best-practices-xchang1121
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: ascendc-api-best-practices
Source: https://github.com/xchang1121/op-autoresearch/tree/main/skills/ascendc/fundamentals/ascendc-api-best-practices
Command: npx skills add https://github.com/xchang1121/op-autoresearch --skill ascendc-api-best-practices-xchang1121

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Writing Ascend C kernels for Ascend NPUs involves many API pitfalls: misaligned DataCopy causing out-of-bounds access, repeatTime overflow past 255, missing EnQue/DeQue synchronization, FP16 precision loss, and banned APIs like GlobalTensor::SetValue. This Skill provides the correct usage patterns, parameter limits, and blacklists so kernel code compiles and runs correctly the first time. ## Core Features & Use Cases - API Category Guides: Detailed references for arithmetic (Adds/Muls), reduction (ReduceMax/ReduceSum, AR/RA patterns), data movement (DataCopy/DataCopyPad), buffer management (TBuf/TQue, Double Buffer), precision conversion (Cast RoundMode), pipeline synchronization (EnQue/DeQue), transpose (TransDataTo5HD + Gather), and host runtime initialization. - Blacklist & Anti-Patterns: Explicitly forbidden APIs (SetValue/GetValue in production, non-aligned DataCopy) and performance anti-patterns (per-element GM access, identity casts, redundant SetFlag/WaitFlag) with recommended replacements. - Constraint Reference: Hard limits such as repeatTime ≤ 255, Compare 256-byte alignment, DataCopyPad blockCount ≤ 4095, UB 32-byte alignment, and Reduce dst 8-byte alignment, each with fix strategies. - Use Case: While writing a Softmax kernel, you hit wrong results with cols=5. The Skill tells you to use DataCopyPad for both CopyIn and CopyOut, use rLengthAlign for UB row offsets but rLength for Reduce counts, and replace Duplicate+Div with Muls(1/sum). ## Quick Start Ask the assistant to check the correct usage and restrictions of an Ascend C API, for example how to handle non-aligned data movement or why a Reduce call returns wrong results.

Frequently Asked Questions about ascendc-api-best-practices

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I handle non-32-byte-aligned data movement in Ascend C?▼

Use DataCopyPad instead of DataCopy for both CopyIn and CopyOut when data is not 32-byte aligned. DataCopyPad automatically handles padding on GM-to-UB copies and discards padding on write-back, avoiding out-of-bounds UB access.

Why does my Ascend C kernel produce wrong results when repeatTime exceeds 255?▼

The repeatTime parameter is uint8_t on most Vector APIs, so values above 255 overflow and truncate, silently skipping computation. Fix it by capping tile rows in host-side tiling or batching the API call in the kernel with loops of at most 255 repeats.

What is the difference between TBuf and TQue in Ascend C?▼

TQue is used for buffers involved in MTE2/MTE3 data movement and supports EnQue/DeQue synchronization and Double Buffer via InitBuffer(que, 2, size). TBuf is for pure Vector computation scratch space, obtained with Get<T>() and requiring no manual free.

Why does my Ascend C kernel read garbage after DataCopyPad?▼

DataCopy and DataCopyPad are asynchronous DMA operations, so computing immediately after them reads unfinished data. Insert EnQue after the copy and DeQue before compute, or temporarily add PipeBarrier<PIPE_ALL> to confirm the issue is synchronization.

Should FP16 addition in Ascend C be computed directly in half precision?▼

By default no: FP16 and BF16 addition lose the smaller operand when magnitudes differ beyond roughly 1024x or 128x. Cast to FP32, perform Add/Sub in-place, then cast back, unless the spec guarantees same-magnitude inputs.

Which Ascend C APIs are forbidden in production kernel code?▼

GlobalTensor::SetValue and GetValue are banned in production because per-element GM access is extremely slow; use DataCopyPad with UB staging instead. Non-32B-aligned DataCopy between GM and UB is also forbidden due to out-of-bounds risk.