mindspore-ai/akg/cuda-c-patterns
Claude Code and agent skill by mindspore-ai/akg
Trust tier 3
- community
Source check pending
Checking that the SKILL.md file is still in the repository.
CUDA C 三大编程模式:向量操作、归约、矩阵乘法
Install
npx vskill@latest install mindspore-ai/akg/cuda-c-patternsSKILL.md
The same SKILL.md was published earlier as mindspore-ai/akg/cuda-c-patterns.
CUDA C 编程模式
1. 向量操作模式
适用于元素级运算:加法、乘法、激活函数等。
标准代码结构
适用算子
- 算术运算: add, mul, sub, div
- 激活函数: relu, sigmoid, tanh, gelu, silu
- 数学函数: exp, log, sqrt, pow, abs
- 类型转换: cast
- 广播操作: broadcast
关键要点
- 使用一维索引
blockIdx.x * blockDim.x + threadIdx.x - 边界检查
if (idx < n_elements) - 简单直接的数据流:加载 → 计算 → 存储
- 推荐块大小: 256 或 512
ReLU 示例
GELU 示例
多输入逐元素操作
2. 归约模式
适用于求和、最大值、最小值等聚合操作。
标准代码结构(共享内存归约)
适用算子
- 基础归约: sum, mean, max, min, prod
- 归一化: softmax, logsoftmax, layernorm, batchnorm, rmsnorm
- 统计: variance, std
- 搜索: argmax, argmin
关键要点
- 使用
extern __shared__声明动态共享内存 - 树形归约(每次折半)
__syncthreads()确保数据一致性atomicAdd用于跨 block 的全局归约- ⚠️ 同步点必须所有线程都能到达
Softmax 示例(数值稳定版)
LayerNorm 示例
3. 矩阵乘法模式
适用于矩阵乘法等多维块计算。
标准代码结构(朴素版)
共享内存优化版
适用算子
- 矩阵运算: matmul, bmm (batch matmul), linear
- 卷积: conv2d, conv3d(im2col 变换后)
- 注意力机制: attention
关键要点
- 2D Grid: 使用
dim3配置二维网格和线程块 - 共享内存优化: 分块加载减少全局内存访问
- 同步: 每次分块加载后
__syncthreads() - 边界处理: 越界位置填充 0
- 分块大小: 常用 16x16 或 32x32
Host 侧启动
模式选择指南
| 算子类型 | 推荐模式 | 关键特征 | 块大小 | |
|---|---|---|---|---|
| Element-wise | 向量操作 | 逐元素独立计算 | 256/512 | |
| Reduction | 归约模式 | 需要聚合多个值 | 256 | |
| MatMul/Conv | 矩阵乘法 | 多维块计算,2D Grid | 16x16/32x32 | |
| Softmax/Norm | 归约+元素操作 | 行级归约+逐元素 | 256 | |
| Attention | 组合模式 | MatMul + Softmax | 视情况而定 |
最佳实践
- 选择合适的模式: 根据算子特性选择基础模式
- 优化块大小: 平衡并行度和资源占用
- 注意边界: 使用
if (idx < n)处理不规则形状 - 数值稳定性: 归约类算子先减最大值防溢出
- 内存访问: 保证合并访问,使用共享内存缓存
- 同步安全:
__syncthreads()必须所有线程到达 - 减少原子操作: 先块内归约再全局写回
Security
- Security scanPassed, 95 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingakg_agents/python/akg_agents/op/resources/skills/cuda-c/guides/cuda-c-patterns/SKILL.md
Popularity
- Repository stars
- 259
- Repository forks
- 48
- 7-day trend score
- 28
Stars and forks are for the whole GitHub repository, not this skill alone. Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/mindspore-ai/akg/cuda-c-patterns)