Repository navigation
[腾讯犀牛鸟2026] pnnx for torch exported program (.pt2) #6995
wyyazjjl
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
大家好,这是犀牛鸟实战 ncnn 第 2 题的总结。
题目是给 pnnx 加
torch.export的.pt2支持。Issue 在这里:#6366PR:#6994
一开始
.pt2和 TorchScript 一样都是 zip,pnnx 会直接拿去load_torchscript,然后就挂了。后来看了归档结构才知道,里面是 JSON 图 + 裸权重,其实不需要 libtorch 的export.load,自己读就行。题目也要求别引入第三方 JSON 库,所以 JSON 解析是自己写的。具体就是这几件事:
.pt2先认出来再走新路径,靠 zip 里的archive_format或者models/*.json,别再误判成 TorchScript。图和权重读进来之后,把aten.relu.default这种 FX 名字改成原来的aten::relu,这样几百个现成 rewriter 还能接着用,不用再抄一套。torch.export经常省略默认参数,pooling、norm、slice、attention 这些如果不补上,后面生成的 python 会直接语法错误。生成脚本里加了export_exported_program()。单测在tests/pt2/,从现有算子测抄过来,改成torch.export.export+save。测试我跑了两边,都是:
Windows 10 + PyTorch 2.13 CPU,149 个全过。WSL Ubuntu 上也是 149 个全过。没有 Mac,macOS 没本地测。
代码主要在
tools/pnnx:新增load_pt2.cpp、json.cpp,zip 的 DEFLATE 用了puff.c。main.cpp、ir.cpp、storezip.cpp有改,测试都在tests/pt2/。All reactions