扩展 core
组合已有操作时写 Block;需要原子求值或专用导数时注册用户算子;需要改变编译器语言时才新增 Rust 内建操作。先按职责选择扩展层。
先判断改哪一层
| 目标 | 需要改 | 首先验证 |
|---|---|---|
| 新增一个块(组合已有算子) | 只要 Python:写在 fly_ruler_std 或你自己的包里,用 @fr.block | just test-python-one tests/std/blocks 或对应包的测试 |
| 定义用户纯函数算子 | 应用中的 OperatorDefinition、BackendImplementation 与显式 operators.register | 跨后端值、导数、保存/新进程加载与修订失效 |
| 新增一个内建符号操作 | Rust IR 与编译器 + 每个后端的 lowering + 能力声明 + _core.pyi | just _test-rust、just test-python-one tests/core/codegen |
| 新增一个后端 | BackendGenerator.emit 与 BackendRuntime.bind/RuntimeOps 实现,注册成 Backend | just test |
| 新增一个编译目标(如新的 native 变体) | targets.py + compiler/_generation.py 的分派与产物草稿 | just test、just test-python-one tests/core/codegen |
| 新增显式 RK 方法 | integrator/_catalog.py 中的系数、身份与别名;数学约束见固定步长积分 | just test-python-one tests/core/test_integrator.py |
| 新增资源类型 | 既有 payload 编码 + _ResourceCatalog 的类型化布局 + 资源 adapter/模板 | just test、just test-python-one tests/core/codegen |
用户算子的通用接入
应用用 OperatorDefinition 定义数学契约,显式注册后端工厂,再通过 fr.math.call 连接输入;不需要为每个用户算子修改 Rust 内建操作表。原始求值和导数数学属于提供者,core 保持结构化 IR、类型检查、绑定快照与产物身份。
完整数据流、IR/SSA 不变量、四方向中间量复用和缓存恢复见用户算子实现机制。使用步骤和教学源见用户章节,精确签名见API。
新增一个内建符号操作
按这个顺序做,避免只在一个后端上通过:
- 定契约:参数、维度与 dtype、支持的 target/backend、数学依据、错误行为与错误文案。
- Rust IR 与编译器:新节点要能进入
ir/expr.rs、参与compiler/plan.rs的规划、并在compiler/check.rs里有诊断(维度不合法、后端不支持等)。 - 每个后端各实现一遍:
codegen/python/numpy_lowering.py(NumPy、JAX 共用其名字映射)、casadi_lowering.py与codegen/casadi/;语义必须一致,不允许「只支持 NumPy」的隐含前提。 - 登记能力:在
codegen/capabilities.py里写清各后端支持与否,让BackendCapabilityError指到具体路径而不是等生成失败。 - 跨 FFI 的同步:
rust/src/ffi/的包装、python/fly_ruler_core/_core.pyi、Python 门面(api/_math.py等)。 - 独立验证:用解析解或手推递推给出预期值,不要用实现自身的输出当基准。
新增一个后端
自定义后端只依赖三个协议,不需要改 core:
python
from fly_ruler_core import runtime
from fly_ruler_core import ModelSettings, artifacts, compiler, targets
from fly_ruler_core.backends import Backend
from fly_ruler_core.runtime.protocols import BackendRuntime
backend = Backend(
name="my-backend",
codegen_factory=lambda: MyGenerator(),
runtime_factory=lambda: BackendRuntime(
"my-backend", lambda: MyRuntimeOps, bind_saved_files
),
)
program = compiler.prepare(System.build("Root"), settings=ModelSettings())
target_program = compiler.compile(program, target=targets.PythonTarget(backend))
model = runtime.bind(target_program, backend=backend.runtime_factory())要点:
RuntimeOps的八个原语(array、zeros、flatten、reshape、slice、size、concatenate、to_numpy)必须自洽,VariableView完全依赖它们。BackendGenerator.emit(program, stage, *, options)只写ArtifactDraft;BackendRuntime.bind(target)只返回绑定侧BoundProgram。生成器通过draft.entries交付五类入口的参数、形状、时序与可用性,通过operator_requirements和block_decisions交付裁切后实际使用的算子和 Block 表示,执行 provider 不运行 lowering、补生成或发布文件。目标缓存与生成顺序由 compiler 编排;工程由 projects 编排;锁与文件事务由 artifacts 提供。name进产物键:改实现就要改名字或清产物目录,否则会命中旧产物。- 后端只能在计划允许的范围内工作;遇到不支持的算子应抛
BackendCapabilityError,不要静默降级。 examples/core/extensions.py是一份可运行的完整样例(NumPy 适配器包装内置后端)。
新增一个块
块的实现写在 fly_ruler_std 或下游包中,不碰 core:
- 配置用 Pydantic 模型并通过
@fr.block(config=...)传入,声明期就固定; - 状态用
fr.state+fr.dynamics(连续)或fr.next_step(离散),不要自己攒积分; - 参数用
fr.param,运行期可覆盖;派生量用fr.effect; - 只用 core 的 Signal 运算与 backend API,不直接引用 NumPy/CasADi/JAX 的实现。
完整写法见 std 的块实现。
文档与接口契约
python/fly_ruler_core/_core.pyi是公开面的类型契约,PyO3 改动必须同步它;just check会跑 mypy(配置在tests/core/mypy.toml)。- 公开面变化要同步
bindings、示例与docs/;just _drift检查 API 参考是否与源码注释一致,just _report列出缺摘要的公开对象。 - Rust 侧公开条目用中文
///注释,随cargo doc发布;cargo doc --manifest-path rust/Cargo.toml --document-private-items可本地生成含私有项的文档。 - 资源载荷留在 Python 侧:Rust 只接收元数据,FFI 不传大数组。
验证矩阵
在 packages/fly_ruler/ 下:
| 命令 | 覆盖 |
|---|---|
just check | 版本、Rust、core/std、示例与发行验收静态检查 |
just test | Rust、core/std、发行包结构和教学源回归 |
just _test-rust | Rust 库单元测试 |
just test-python-one tests/core/codegen | 生成代码行为(tests/core/codegen) |
cargo test --manifest-path rust/Cargo.toml --lib --all-features -- <name> / just test-python-one <path> | 单点复跑 |
cargo doc --manifest-path rust/Cargo.toml --document-private-items | 生成 Rust 文档 |
just pre-commit | 交付前完整门禁 |
在 docs/ 下:
| 命令 | 覆盖 |
|---|---|
just api | 重生成 docs/api/core 与 docs/api/std |
just _drift | 生成结果与源码注释是否一致 |
just _report | 缺摘要或小节注释的公开对象、docstring 风格问题 |
just check | 构建、静态检查、链接、锚点、正文与 API 注释 |
just test | 再加图示、真实示例与 API 漂移门禁 |
深入源码
诊断与开发验证 给出按修改面选择的回归和错误定位步骤。