Skip to content
Skills Library
Programming · Building & coding

paddle-phi-kernel

/paddle-phi-kernel

Use when working with Paddle's PHI kernel system: registering new kernels, debugging kernel selection/dispatch, understanding code auto-generation from YAML, or implementing operator decomposition via the combination mechanism.

Repository
paddle ↗
Licence
Apache-2.0
Last repository change
26.10.2026

Assessment and security

95/ 100Class: good
Format20 / 25
Frontmatter with name and description, valid name, description up to 1,024 characters, name matching the folder.
Discoverability25 / 25
Description explains when to use the skill, has a reasonable length and uses third person.
Content35 / 35
At least 150 words and at most 500 lines, headings, step lists, examples or code.
Hygiene15 / 15
No placeholders such as TODO, no Windows paths, with a licence.
Riskno signals detected in the scan
Files in this package
5
Scripts
0
HTML comments
0
URLs in SKILL.md
0
Deprecation

No signals detected. Repository changed on 26.10.2026, without references to retired models or APIs.

Licence

Apache-2.0, otwarta: można używać i zmieniać, z zachowaniem licencji i informacji o zmianach.

What it does and when to use it

Use when working with Paddle's PHI kernel system: registering new kernels, debugging kernel selection/dispatch, understanding code auto-generation from YAML, or implementing operator decomposition via the combination mechanism.

Original author description

Use when working with Paddle's PHI kernel system: registering new kernels, debugging kernel selection/dispatch, understanding code auto-generation from YAML, or implementing operator decomposition via the combination mechanism.

How it works

  1. Kernel 两级 map 结构
  2. KernelKey 哈希结构
  3. 代码生成管线概览
  4. 组合算子机制
  5. 什么场景看什么文件
  6. 社区源码链接(L3)
Original SKILL.md headings · 70 lines, 260 words
SKILL.md preview
---
name: paddle-phi-kernel
description: "Use when working with Paddle's PHI kernel system: registering new kernels, debugging kernel selection/dispatch, understanding code auto-generation from YAML, or implementing operator decomposition via the combination mechanism."
---

# PHI Kernel 体系总览

## Kernel 两级 map 结构

PHI kernel 在 `KernelFactory` 单例中以两级 `flat_hash_map` 存储:

```
api_name (string)  →  KernelKeyMap
                        ├── KernelKey_1  →  Kernel_1
                        ├── KernelKey_2  →  Kernel_2
                        └── ...
```

- 第一级:算子名称(如 `"add"`, `"matmul"`)→ `KernelKeyMap`
- 第二级:`KernelKey` → `Kernel` 对象(包含函数指针、参数元信息)

## KernelKey 哈希结构

`KernelKey` 由 Backend、DataLayout、DataType 三要素组成,哈希编码为 32-bit 整数:

```
bit:  31 ─────── 20 | 19 ─── 12 | 11 ── 8 | 7 ── 0
      Extended       DataType    Layout    Backend
```

- `Backend`(bit 0-7): CPU / GPU / GPUDNN / XPU / OneDNN 等
- `DataLayout`(bit 8-11): NCHW / NHWC / ANY 等
- `DataType`(bit 12-19): FLOAT32 / FLOAT16 / BFLOAT16 / INT64 等
- `Extended`(bit 20-31): 保留位,用于未来扩展

## 代码生成管线概览

PHI 体系通过 YAML 驱动代码生成,覆盖三个子系统:

| 子系统 | 生成器 | 产出物 |
|--------|--------|--------|
| C++ API 层 | `api_gen.py` | `paddle/phi/api/lib/api.cc` |
| 动态图函数层 | `eager_gen.py` | `dygraph_functions.cc`, `nodes.cc` |
| Python-C 映射层 | `python_c_gen.py` | `eager_op_function.cc` |

输入 YAML:`ops.yaml`, `backward.yaml`, `fused_ops.yaml`, `sparse_ops.yaml`, `op_compat.yaml`

## 组合算子机制

将 1061 个原生算子分解为约 200 个基础算子(primitive operators),降低分布式 / 编译器 / 新硬件适配成本。

- **前向分解**:`DecompInterface` → `call_decomp_rule()` → `composite.h` 实现
- **反向分解**(VJP):`VjpInterface` / `DecompVjpInterface` → `call_decomp_vjp()` → `details.h` 实现
- **CustomVJP**:为 sigmoid、log_softmax 等数值敏感算子提供手写反向

## 什么场景看什么文件

| 场景 | 参考文档 |
|------|----------|
| 注册新 kernel / 理解宏展开 | [references/kernel-registration.md](references/kernel-registration.md) |
| 调试 kernel 选择 / 分派失败 | [references/kernel-selection.md](references/kernel-selection.md) |
| 理解 YAML → 代码生成流程 | [references/codegen-pipeline.md](references/codegen-pipeline.md) |
| 开发组合算子 / VJP | [references/combination-mechanism.md](references/combination-mechanism.md) |

## 社区源码链接(L3)

- PHI kernels 根目录: `paddle/phi/kernels/`
- Kernel 注册宏: `paddle/phi/core/kernel_registry.h`
- KernelFactory: `paddle/phi/core/kernel_factory.h`, `kernel_factory.cc`
- YAML 定义: `paddle/phi/ops/yaml/ops.yaml`, `backward.yaml`
- 代码生成脚本: `paddle/phi/api/generator/` — `api_gen.py`, `backward_api_gen.py`, `tensor_operants_gen.py`
- 组合算子前向: `paddle/fluid/primitive/decomp_rule/decomp_rule/composite.h`
- 组合算子反向: `paddle/fluid/primitive/decomp_rule/decomp_vjp/details.h`
Files in this package (5)
  • SKILL.md 3 KB
  • references/codegen-pipeline.md 7 KB
  • references/combination-mechanism.md 8 KB
  • references/kernel-registration.md 6 KB
  • references/kernel-selection.md 6 KB

Versions

The database has no other skills named paddle-phi-kernel.

Database snapshot: 13.11.2026 · package version df183059e43b (SHA-256 digest of the entire package)

Downloads in the last 30 days: 2 · ranking

Report a problem with this skill: harmful content, incorrect descriptions or an author’s removal request.

Similar skills

  • 100paddle-op-dev
    PaddlePaddle (飞桨) C++ 算子开发指南。提供从 YAML 配置、InferMeta 函数、Kernel 实现、Python API 封装、单元测试到编译验证的完整算子开发流程指导。在以下场景使用此 skill:(1) 为 Paddle 框架新增 C++ 算子 (2) 修改或调试已有 Paddle 算子 (3) 编写算子的 YAML 配置、InferMeta、Kernel、Python API 或单元测试 (4) 理解 Paddle 算子开发架构和流程 (5) 编译 Paddle 并验证算子正确性
  • 96paddle-pull-request
    自动创建或更新 GitHub Pull Request。 当需要为 Paddle 相关仓库创建 PR 时,优先使用本 skill。
  • 100paddle-webhooks
    Receive and verify Paddle webhooks. Use when setting up Paddle webhook handlers, debugging signature verification, or handling subscription events like subscription.created, subscription.canceled, or transaction.completed.
  • 100paddle
    Paddle is a merchant-of-record payments platform that sells software on a company's behalf and handles checkout, subscriptions, sales tax, VAT and invoicing. Use when a user asks to add subscription billing without handling tax compliance, accept international payments,…
  • 95metal-kernel
    Write Metal/MPS kernels for PyTorch operators. Use when adding MPS device support to operators, implementing Metal shaders, or porting CUDA kernels to Apple Silicon. Covers native_functions.yaml dispatch, host-side operators, and Metal kernel implementation.
  • 100laravel-billing
    Use when implementing subscriptions, invoices, payment methods, webhooks, or billing portals with Laravel Cashier (Stripe or Paddle).

Feedback and reports

Your message goes to Bartosz Sokoliński. An email address is needed only for a reply. See the privacy policy.