paddle-static-graph
Paddle 静态图模式
Paddle 静态图采用先编译、后执行的范式:Python 端构建 Program 描述计算逻辑,再由 Executor 将 Program 翻译为可调度的算子序列并执行。
Program 结构速查
ProgramDesc
└─ blocks_: vector<BlockDesc>
├─ BlockDesc [0] (global block)
│ ├─ ops_: vector<OpDesc> ← 前向 + 反向 + 优化器算子
│ └─ vars_: map<string, VarDesc> ← 参数、中间变量、梯度
└─ BlockDesc [1..N] (sub-blocks, 用于 while_op / cond 等控制流)
- ProgramDesc:顶层容器,持有 protobuf 描述和 BlockDesc 列表。
- BlockDesc:逻辑作用域,包含 OpDesc 序列和 VarDesc 字典。
- OpDesc:一个算子描述,记录类型、输入输出名称、属性。
- VarDesc:一个变量描述,记录名称、dtype、shape、持久化标记等。
执行器生命周期概览
[Graph Construction] [Build Phase (once)] [Scheduling / Execution]
┌─────────────────────┐
Python API ──► ProgramDesc │ 1. BuildVariableScope│ work queue
nn.Linear / optimizer │ VarDesc → Variable │ ┌───────────┐
append_op → OpDesc │ 2. BuildOpFuncList │──────────►│ async │
│ OpDesc → OpFuncNode│ │ dispatch │
│ (kernel select, │ │ (thread │
│ transfer insert) │ │ pool) │
│ 3. Convert │ └───────────┘
│ dependency DAG │ dep_count=0
│ stream schedule │ → push queue
│ var lifetime / GC │ → execute
└─────────────────────┘ → decrement deps
什么场景看什么文件
| 调试场景 | 应关注的文件 / 模块 |
|---|---|
| Program 构建逻辑、append_op 流程 | python/paddle/base/framework.py (Block, Program) |
| 参数初始化、startup_program | python/paddle/base/framework.py + python/paddle/nn/initializer/ |
| Executor Python 入口 | python/paddle/base/executor.py |
| StandaloneExecutor / InterpreterCore | paddle/fluid/framework/new_executor/standalone_executor.cc |
| Build 阶段(变量创建、算子构建、依赖分析) | paddle/fluid/framework/new_executor/program_interpreter.cc |
| 算子依赖 DAG 与流调度 | paddle/fluid/framework/new_executor/stream_analyzer.cc |
| Scope 与变量生命周期管理 | paddle/fluid/framework/scope.cc, paddle/fluid/framework/variable.h |
| OpDesc / VarDesc protobuf 定义 | paddle/fluid/framework/framework.proto |
| 反向算子自动生成 | python/paddle/autograd/backward_utils.py, paddle/fluid/operators/ |
社区源码参考 (L3)
- Program / Block / Op / Var 描述:
paddle/fluid/framework/program_desc.cc,block_desc.cc,op_desc.cc,var_desc.cc - 新执行器入口:
paddle/fluid/framework/new_executor/standalone_executor.cc - InterpreterCore 实现:
paddle/fluid/framework/new_executor/program_interpreter.cc - StreamAnalyzer 流调度:
paddle/fluid/framework/new_executor/stream_analyzer.cc
更多实现细节请参考 references/ 目录下的专题文档。
More from pfcclab/paddle-skills
paddle-pull-request
|
30paddle-debug
专注于在 Paddle 代码库中定位问题并输出高质量调试报告的调试流程与技巧;代码修复是在结论充分后的后续步骤。遇到 Paddle 框架、算子、训练脚本或分布式训练相关问题时,优先使用本 skill 规划调试与报告输出。
24fastdeploy-pull-request
|
20paddle-pir-cinn
Use when working with Paddle's new IR system (PIR) or CINN compiler: understanding SSA-based Program structure, Dialect/Type/Attribute design, writing or debugging Passes, tracing the CINN compilation pipeline from GroupOp to CUDA kernel, or translating legacy ProgramDesc to PIR.
5paddle-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.
5paddle-eager-graph
Use when navigating Paddle eager-mode (dynamic graph) source code, tracing forward/backward execution, debugging autograd issues, understanding PyLayer, or investigating complex-valued gradient computation. Covers Python API to C++ kernel call chain, backward graph topology sort, and inplace version tracking.
5