05 · 宏与静态注册:算子怎么出现在 torch.ops 下
结论:TORCH_LIBRARY 展开成静态对象,dlopen 执行 .init_array 时其构造函数向 Dispatcher 登记——调用者是加载器,不是用户代码。
%%{init: {"flowchart": {"wrappingWidth": 360}}}%%
flowchart TB
M["TORCH_LIBRARY(myops, m) { m.def(...); }"] --> S["展开:static TorchLibraryInit<br/>myops_init(&fn)"]
S --> SO["编进 .so 的 .init_array"]
SO -- "import myops_ext → dlopen" --> INIT["加载器跑构造函数"]
INIT --> D[("Dispatcher 登记<br/>myops::scale")]
D --> PY["torch.ops.myops.scale 可用"]
classDef hot fill:#fde68a,stroke:#b45309
class INIT hot
COMMENTS
评论存放在 GitHub Discussions, 用 GitHub 账号登录即可发表,支持 Markdown。 想针对正文某句话说?选中那段文字,点浮出的「评论」即可划线评论;觉得哪里写错了,发表时勾上「同时提交 Issue」。 有人回复你时 GitHub 会按你的通知设置发邮件,不用守在这里。