SOC 職業分類に基づく
Codex または Claude でインストール この Prompt をコピーして Codex、Claude、または他のアシスタントに貼り付けると、Skill ページを確認してインストールできます。
直接コマンドでは確認用 Prompt が省略されます。実行前にソースを確認してください。
npx skills add https://github.com/mindspore-ai/akg --skill cuda-c-basicsコマンドは1行のまま表示されます。コピー前に横へスクロールして全体を確認してください。
ローカルで確認しますか?SkillsMP が現在取得できるファイルをダウンロードできます。
SKILL.md を表示中
矩阵乘法矩阵乘法 A[M, K] @ B[K, N] = C[M, N]中,大K维度矩阵乘法(K>>M,N)优化:针对M/N较小但K极大(如M=N=256,K=131072)的场景,Split-K切分K维度并行化、Workspace+Reduce替代全局同步,实现显著性能提升
Triton Ascend hard API restrictions and forbidden syntax. MUST-follow rules that apply to every kernel: forbidden control flow (return/break/continue/lambda/while), tensor slice/index restrictions, scalar conversion rules, BLOCK_SIZE upper bound. Violating any of these produces a compile or runtime error on Ascend.
Triton Ascend 性能优化通用策略: BLOCK_SIZE 选择 (1024-2048 for elementwise, must be <65536), grid configuration (use VEC_CORE_NUM / CUBE_CORE_NUM, 2D/3D grid for matmul / conv / reduce, 1D grid + inner loop for elementwise / pointwise), 256B alignment for memory transfers, autotune block-size patterns, fp16 / fp32 precision conversion. Bind via keywords like matmul, elementwise, reduce, block_size, grid, autotune, alignment, fp16, fp32, tile, interleaved-loop, cube-core, vec-core.
| name | cuda-c-basics |
| description | CUDA C 核心概念、内核结构和标准编程模式 |
| category | fundamental |
| version | 1.0.0 |
| metadata | {"backend":"cuda","dsl":"cuda_c","operator_patterns":"all"} |
__global__ 修饰的 C/C++ 函数,在 GPU 上并行执行<<<grid_size, block_size>>> 语法从主机代码启动(num_blocks_x, num_blocks_y)block_size = 256grid_size = ceil(total_elements / block_size)所有 CUDA C 内核都遵循相同的五步结构模式:
__global__ void standard_kernel(
float* output, float* input, int n_elements
) {
// 1. 计算全局线程索引
int idx = blockIdx.x * blockDim.x + threadIdx.x;
// 2. 边界检查
if (idx < n_elements) {
// 3. 加载数据
float data = input[idx];
// 4. 执行计算
float result = compute_function(data);
// 5. 存储结果
output[idx] = result;
}
}
void launch_kernel(float* input, float* output, int n_elements) {
const int block_size = 256;
const int num_blocks = (n_elements + block_size - 1) / block_size;
kernel<<<num_blocks, block_size>>>(output, input, n_elements);
}
int global_index = blockIdx.x * blockDim.x + threadIdx.x;
int row = blockIdx.y * blockDim.y + threadIdx.y;
int col = blockIdx.x * blockDim.x + threadIdx.x;
int x = blockIdx.x * blockDim.x + threadIdx.x;
int y = blockIdx.y * blockDim.y + threadIdx.y;
int z = blockIdx.z * blockDim.z + threadIdx.z;
// 一维网格
int block_size = 256;
int num_blocks = (n_elements + block_size - 1) / block_size;
kernel<<<num_blocks, block_size>>>(...);
// 二维网格(矩阵操作)
dim3 block_size(16, 16);
dim3 grid_size((N + 15) / 16, (M + 15) / 16);
kernel<<<grid_size, block_size>>>(...);
// 三维网格(体积数据)
dim3 block_size(8, 8, 8);
dim3 grid_size((X + 7) / 8, (Y + 7) / 8, (Z + 7) / 8);
kernel<<<grid_size, block_size>>>(...);
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if (idx < n_elements) {
// 安全访问 input[idx]
output[idx] = input[idx];
}
int row = blockIdx.y * blockDim.y + threadIdx.y;
int col = blockIdx.x * blockDim.x + threadIdx.x;
if (row < M && col < N) {
// 安全访问矩阵元素
output[row * N + col] = input[row * N + col];
}
// 分配设备内存
float* d_input, *d_output;
cudaMalloc(&d_input, size * sizeof(float));
cudaMalloc(&d_output, size * sizeof(float));
// 拷贝数据到设备
cudaMemcpy(d_input, h_input, size * sizeof(float), cudaMemcpyHostToDevice);
// 启动内核
kernel<<<grid, block>>>(d_output, d_input, size);
// 拷贝结果回主机
cudaMemcpy(h_output, d_output, size * sizeof(float), cudaMemcpyDeviceToHost);
// 释放内存
cudaFree(d_input);
cudaFree(d_output);
在生成算子时,必须在 Python 模块中内嵌 CUDA C 代码,使用 torch.utils.cpp_extension.load_inline 进行 JIT 编译:
import torch
from torch.utils.cpp_extension import load_inline
source = """
#include <torch/extension.h>
#include <cuda_runtime.h>
__global__ void my_kernel(const float* input, float* output, int size) {
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if (idx < size) {
output[idx] = input[idx] * 2.0f;
}
}
torch::Tensor my_kernel_call(torch::Tensor input) {
auto size = input.numel();
auto output = torch::zeros_like(input);
int block_size = 256;
int num_blocks = (size + block_size - 1) / block_size;
my_kernel<<<num_blocks, block_size>>>(
input.data_ptr<float>(), output.data_ptr<float>(), size);
return output;
}
"""
cpp_src = "torch::Tensor my_kernel_call(torch::Tensor input);"
kernel_module = load_inline(
name="my_cuda",
cpp_sources=cpp_src,
cuda_sources=source,
functions=["my_kernel_call"],
verbose=True,
extra_cflags=[""],
extra_ldflags=[""],
)
cudaFree 释放设备内存__syncthreads()printf()、throw std::runtime_error() 等打印/异常语句malloc / new 进行动态分配