Co-authored-by: chenhaozhe<chenhaozhe1@huawei.com> # message auto-generated for no-merge-commit merge: !47662 merge fxrt-port-squash into master fxrt: port upstream runtime changes made since the in-tree integration Created-by: c_34 Commit-by: chenhaozhe Merged-by: ascend-robot Description: <!-- PR描述模板更新日期:20260203 --> # 【合入来源】 > <font color="red">**如有社区issue,请关联issue链接**</font>\ > <font color="red">**请勿携带内部流程信息(需求链接、问题单、内部issue等)**</font> - [ ] 需求 - [ ] 问题单 - [ ] issue/工单 - [x] 重构优化 https://gitcode.com/Ascend/pytorch/issues/5278 - [ ] 资料更新 同步进行in-tree合入过程中修改的原始仓库内容。 ## 背景 fxrt 整改合入 master 之后,上游(pta 分支)又积累了一批运行时改动。本 PR 把这些改动移植进 in-tree 的 `torch_npu.fxrt`,使两边重新对齐。 **`e64806be`** (08-31) 按生成时的 stride 寻址 compiled-kernel 参数 融合后端是针对参数在 trace 时的 stride 生成 kernel 的。记录这些 layout 并在调用前恢复,使期间被重新 layout 的参数仍按 kernel 预期的方式寻址。 → `compiled_kernel_adapter.py`、`fx_backend.py` **`81a537aa`** (09-02) DVM op 通过 pybind 头文件已授予 `fxrt::ops::OpDvmCallV2` 的 friendship 访问 torch_npu 的 DVM kernel 状态,而不是它从未存在过的公有 accessor;helper 折进 op 类以使 friendship 生效。动态与静态 kernel 合并为一个入口,由 kernel 的 `F_DYN` 标志区分。附带效果见下方「需要注意」。 → `src/ops/ascend/dvm/`、`dvm_adapter.py` **`ad4c84cc`** (09-02) AutoFuse 新增 `autofuse_call` op:dlopen AutoFuse codegen 产出的 wrapper stub 并直接 launch,外加生成该 stub 的 Python codegen。下降位置在 DVM 与通用 compiled-kernel 路径之间,由 `FXRT_AUTOFUSE_NATIVE=1` 门控。算子注册无需登记表,`LoadOpLib` 按 `libops_ascend*` 前缀发现。 → `src/ops/ascend/autofuse/`、`autofuse_codegen.py`、`ops.list` **`bbf45634`** (09-07) custom / torch / python call 的 ref、view 零拷贝 仅重新解释输入 storage 的输出不再拷贝。`OpRunner` 里通用的 ref 元数据同步会跳过输出 shape 与输入不同的算子,因此 view 类自定义算子自行计算元数据;`AclnnCustomViewOperator` 是它们的新基类,经 custom op API 暴露。 → `op_custom_call`、`op_torch_call`、`op_python_call`、`aclnn_custom_view_operator.h`、`data_convert` **`0ae75226`** (09-12) FXRT 自定义集合通信 用 arg-mapping hook 把 vLLM 四参数的 all_gather ABI 适配到 FXRT 原生三参数 HCCL ABI;从 FX 图收集通信信息时,把 vLLM 的逻辑 group 名解析为对应的 torch device ProcessGroup。 → `fx_backend.py`、`utils.py` **`c7e3a2e0`** (09-12) FX 操作数分类,自定义调用加固 一个 FX 参数能否走标量算子 ABI,改由 FXRT 运行时实际可表示的值决定,而不是看 node target —— `item` 节点不再仅凭名字就被当成标量。自定义调用侧在转换前校验操作数种类,并报出出错的那一个,而不是在转换深处失败。 → `fx_backend.py`、`symbolic_shape.py`、`op_torch_call`、`data_convert` **`37fcc1bb`** (09-22) 修复多输出 ref 算子的输出数据校验 ref alias 输出与输入共享 storage,且两侧都不持有 deleter,清掉共享的 data pointer 会让第一个之后的每个输出都校验失败。改为读取 storage 地址而不取走它,仅在持有所有权的路径上移出 data pointer。影响 split、unbind、chunk。 → `op_torch_call.cpp` **`6051b291`** (09-22) `Tensor[]` 自定义调用输出保留 ref alias 返回 `Tensor[]` 的自定义调用丢失了单 Tensor 输出能保留的 ref aliasing,导致列表里每个元素各自声明 storage。让 alias 穿过列表输出,并把 `c7e3a2e0` 引入的操作数预校验换成转换点上的检查。 → `fx_backend.py`、`op_torch_call`、`data_convert` ## 内容 建议按三组顺序审阅: 1. **compiled-kernel 路径**(`compiled_kernel_adapter.py`、`fx_backend.py`、`autofuse_codegen.py`、`src/ops/ascend/autofuse/`)——按 inductor 生成的 stride 寻址 kernel 参数而不再假定连续;FX 操作数分类,标量不再走张量路径;新增 `OP(autofuse_call)` 与 `FXRT_AUTOFUSE_NATIVE` 门控的原生 AutoFuse 下降。vLLM 集合通信 hook 在同一路径上,自成一体。 2. **零拷贝 view**(`op_custom_call`、`op_torch_call`、`op_python_call`、`aclnn_custom_view_operator.h`)——改为交出 ref 输出而非拷贝,`data_convert` 删掉只为喂拷贝而存在的转换。多输出校验修复与 `Tensor[]` alias 修复是对同一段代码的纠正。 3. **DVM v2**(`src/ops/ascend/dvm/`)——CMake 探测改为匹配 `torch_npu/csrc/inductor/dvm/pybind_api.h` 实际携带的 friend 声明,并通过该 friendship 访问 kernel 私有成员。 ## 需要 reviewer 注意 **两处对上游的有意偏离,均在 CMake**:autofuse 的 `CMakeLists.txt` 采用 in-tree 同级算子模块的写法,而非上游的 glob + 自带 `install()` 形式(上游还链接了 in-tree 不存在的 `fxrt::securec`);`ops_ascend_autofuse` 加入 `FXRT_OP_LIBS` 与 `install_fxrt` 拷贝列表,使 wheel 携带该库。 **一处告警屏蔽**: 为了处理autofuse的自动生成算子函数签名,实现中使用了一个长f-string字符串进行代码生成,这个长字符串触发了超长行的告警,参考之前cpp_wapper中类似情况的处理方式增加的`.flak8`的告警屏蔽。 ## 验证 Ascend 910B3 实测: - `test/fxrt` 7 项全部通过 - `libops_ascend_autofuse.so` / `libops_ascend_dvm_v2.so` 均构建产出、随 wheel 安装,运行时经 `/proc/self/maps` 确认已 dlopen - ref/view 定向用例 10/10 与 eager 一致:split(均分/不均分)、unbind、chunk、`split->add`、broadcast_tensors、select/permute、flatten/reshape、`item()` 标量、标量乘 - AutoFuse 门控双向验证:默认关闭与 `FXRT_AUTOFUSE_NATIVE=1` 结果均正确 - eta 推荐模型(inductor + fx_wrapper -> fxrt 运行时,batch 128)精度校验通过,QPS 8929;IR dump 显示 146 个 `compiled_kernel_mutation`,即本 PR 的 stride 与操作数分类改动在真实负载下被覆盖 # 【资料变更】 > 请确认是否涉及资料变更。如涉及,需要在PR中体现,并简要说明修改内容。如不涉及,需填写“不涉及” 不涉及 # 【接口变更】 > 请确认是否涉及跨代码仓或者客户面可见的接口变更。如涉及,需要详细说明接口以及对应的变更内容,同时需要在资料中体现。如不涉及,需填写“不涉及” 不涉及 # 【CheckList】 > PR提交人对以下CheckList自检项进行全量自检,自检通过或不涉及,均修改 [ ] 为 [x] - [x] 代码注释完备,正确记录错误日志 - [x] 代码实现进行了返回值、空指针等校验 - [x] PR标题正确使用类型标签,如:feat、fix、refactor、docs、test等 - [x] PR持续集成流水线(CI)执行通过,代码检查无异常 See merge request: Ascend/pytorch!47662
FXRT
FXRT is a lightweight, high-performance runtime for the inference phase. It lowers PyTorch graphs (via torch.compile) onto its own IR and runtime, executing them on the Ascend backend. It focuses on inference latency and memory reuse, with native support for zero-copy view operators.
中文文档:README_CN.md
Table of Contents
Build and Install
FXRT is built and shipped as part of torch_npu; it is not packaged on its own.
Build torch_npu from source as usual and the wheel carries FXRT under
torch_npu/fxrt:
bash ci/build.sh --python=3.10
Pass --disable_fxrt to build torch_npu without FXRT. Its only extra build
dependency, nanobind, is declared in torch_npu's requirements*-build.txt, and
it is needed at build time only. See the
build-from-source guide.
FXRT is imported as torch_npu.fxrt. import torch_npu alone does not load it;
once torch_npu.fxrt is imported, the same package is also reachable as fxrt.
Run Your First Example
FXRT plugs in as a torch.compile backend. Once a model or function is compiled with backend, it runs on the FXRT runtime:
# quickstart.py
import torch
from torch_npu.fxrt import backend
def model(x, y):
z = x + y
z = z.view(4, -1) # view operators take the zero-copy path
return torch.relu(z)
# Move the inputs to npu
x = torch.randn(2, 8).npu()
y = torch.randn(2, 8).npu()
compiled = torch.compile(model, backend=backend)
out = compiled(x, y)
print(out.shape)
Key points:
from torch_npu.fxrt import backendprovides the compile backend.- Call the function as usual after
torch.compile(fn, backend=backend). view/reshape/permute/sliceand similar operators take the zero-copy view path by default.
Environment Variables
| Variable | Purpose |
|---|---|
FXRT_DISABLE_VIEW_OPS |
Disable the zero-copy implementation of specific view operators and fall back to the non-view path, for debugging and comparison. Accepts a comma-separated list drawn from permute, transpose, t, movedim, split and chunk, or all to cover them in one go. |
FXRT_DEV_DUMP_IR |
When set to 1, dumps the compiled IR for debugging |