Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Dy2St] PyTorch 和 Paddle 仓库模型导出成功率对比任务 #58985

Open
2742195759 opened this issue Nov 14, 2023 · 29 comments
Open

[Dy2St] PyTorch 和 Paddle 仓库模型导出成功率对比任务 #58985

2742195759 opened this issue Nov 14, 2023 · 29 comments
Assignees

Comments

@2742195759
Copy link
Contributor

2742195759 commented Nov 14, 2023

背景

PyTorch 最近在 2.1.0 版本发布了 torch.export 功能,提供了以 PyTorch 2.x 的 dynamo 为核心的模型导出方法。其功能上与我们 Paddle 动转静模型导出 paddle.jit.save 相类似,因此我们想要建设一个 repo 能够自动对比 torch.exportpaddle.jit.save 的导出成功与否,PaddleJitLab/jit-exportable-models 就诞生了~

为了确保对比的公平性,需要两个模型使用相同/等价的组网 API,因此我们选取了一些 PaConvert 能够以较高成功率转换经典的 PyTorch 模型作为测试集合,通过 PaConvert 即可将 PyTorch 模型转换为等价的 Paddle 模型。

之后我们分别对转换前后的模型分别调用 torch.export.exportpaddle.jit.save 就可以了解两者模型导出是否成功啦~

任务详情

我们在下面的 #58985 (comment) 提供了详细的操作流程,你可以跟随下面的步骤来完成任务。

最终任务需要提交如下几部分内容:

  • Fork 后并添加 torch.export.export 代码的 PyTorch 模型 repo 1
  • 通过 PaConvert 转换并添加 paddle.jit.save 代码的 Paddle 模型 repo 2
  • PaddleJitLab/jit-exportable-models 提交 PR,增加可运行的自动化测试 shell 脚本

任务完成且通过后会邀请进入 PaddleJitLab 组织,并迁移 repo 1 和 repo 2 进组织~

参考资料

任务列表 (整体进展:12/80)

按 merge 的时间顺序,排名不分先后: @2742195759 (1)、@GreatV (11)

已完成部分
序号 模型名称 模型地址 难度 认领人 PR链接
✅1 TiSASRec.pytorch https://github.com/pmixer/TiSASRec.pytorch/tree/master @2742195759
✅4 pytorch-hed https://github.com/sniklaus/pytorch-hed @GreatV PaddleJitLab/jit-exportable-models#1
✅5 ConSinGAN https://github.com/tohinz/ConSinGAN @GreatV PaddleJitLab/jit-exportable-models#2
✅7 ScaledYOLOv4 https://github.com/WongKinYiu/ScaledYOLOv4 ⭐⭐ @GreatV PaddleJitLab/jit-exportable-models#11
✅11 DINO https://github.com/facebookresearch/dino ⭐⭐⭐ @GreatV PaddleJitLab/jit-exportable-models#7
✅26 EAST https://github.com/SakuraRiven/EAST @GreatV PaddleJitLab/jit-exportable-models#3
✅31 recurrent-visual-attention https://github.com/kevinzakka/recurrent-visual-attention @GreatV PaddleJitLab/jit-exportable-models#4
✅38 few-shot https://github.com/Shandilya21/Few-Shot @GreatV PaddleJitLab/jit-exportable-models#5
✅39 PyTorch-BayesianCNN https://github.com/kumar-shridhar/PyTorch-BayesianCNN ⭐⭐ @GreatV PaddleJitLab/jit-exportable-models#9
✅55 DeepRecommender https://github.com/NVIDIA/DeepRecommender ⭐⭐ @GreatV PaddleJitLab/jit-exportable-models#10
✅56 pren https://github.com/RuijieJ/pren @GreatV PaddleJitLab/jit-exportable-models#8
✅61 wide-resnet.pytorch https://github.com/meliketoy/wide-resnet.pytorch @GreatV PaddleJitLab/jit-exportable-models#6
序号 模型名称 模型地址 难度 认领人 PR链接
2 FastSAM https://github.com/CASIA-IVA-Lab/FastSAM ⭐⭐
3 caser_pytorch https://github.com/graytowne/caser_pytorch
6 Real-ESRGAN https://github.com/xinntao/Real-ESRGAN ⭐⭐
8 pytorch-segmentation https://github.com/qubvel/segmentation_models.pytorch ⭐⭐
9 tabnet https://github.com/dreamquark-ai/tabnet ⭐⭐
10 ContrastiveSeg https://github.com/tfzhou/ContrastiveSeg ⭐⭐⭐
12 DAMO-YOLO https://github.com/tinyvision/DAMO-YOLO ⭐⭐⭐
13 NCR https://github.com/rutgerswiselab/NCR
14 PAN https://github.com/WenmuZhou/PAN.pytorch ⭐⭐
15 monodepth2 https://github.com/OniroAI/MonoDepth-PyTorch
16 pspnet-pytorch https://github.com/Lextal/pspnet-pytorch
17 yolact https://github.com/dbolya/yolact/tree/master ⭐⭐
18 OV-DETR https://github.com/yuhangzang/OV-DETR ⭐⭐
19 yolov5 https://github.com/ultralytics/yolov5 ⭐⭐
20 deeplab-pytorch https://github.com/kazuto1011/deeplab-pytorch ⭐⭐
21 SimCC https://github.com/leeyegy/SimCC
22 MODNet https://github.com/ZHKKKe/MODNet/tree/master ⭐⭐
23 ESPNet https://github.com/espnet/espnet
24 CORA https://github.com/AkariAsai/CORA ⭐⭐⭐
25 AoANet https://github.com/husthuaan/AoANet ⭐⭐
27 detr https://github.com/facebookresearch/detr ⭐⭐
28 pytorch-YOLOv4 https://github.com/Tianxiaomo/pytorch-YOLOv4 ⭐⭐
29 pytorch_Realtime_Multi-Person_Pose_Estimation https://github.com/tensorboy/pytorch_Realtime_Multi-Person_Pose_Estimation ⭐⭐
30 CLIP https://github.com/openai/CLIP ⭐⭐
32 MVFNet https://github.com/Fanziapril/mvfnet ⭐⭐⭐
33 ADDS-DepthNet https://github.com/LINA-lln/ADDS-DepthNet
34 deep-image-prior https://github.com/DmitryUlyanov/deep-image-prior
35 vilbert-multi-task https://github.com/facebookresearch/vilbert-multi-task ⭐⭐
36 TorchSeg https://github.com/ycszen/TorchSeg ⭐⭐
37 CenterNet https://github.com/gakkiri/simple-centernet-pytorch ⭐⭐
40 ViTPose https://github.com/gpastal24/ViTPose-Pytorch ⭐⭐⭐
41 r2c https://github.com/rowanz/r2c
42 DeepCTR-Torch https://github.com/shenweichen/DeepCTR-Torch ⭐⭐
43 hacksc https://github.com/ZoneLikeWonderland/HACK-Model/ ⭐⭐
44 YOLOX https://github.com/Megvii-BaseDetection/YOLOX ⭐⭐
45 a-PyTorch-Tutorial-to-Image-Captioning https://github.com/sgrvinod/a-PyTorch-Tutorial-to-Image-Captioning ⭐⭐
46 prototypical-networks https://github.com/jakesnell/prototypical-networks
47 AdvSemiSeg https://github.com/hfslyc/AdvSemiSeg ⭐⭐
48 FastFCN https://github.com/wuhuikai/FastFCN ⭐⭐
49 STDC-Seg https://github.com/MichaelFan01/STDC-Seg ⭐⭐
50 AdaptSegNet https://github.com/wasidennis/AdaptSegNet
51 tiny-faces-pytorch https://github.com/varunagrawal/tiny-faces-pytorch
52 pytorch_GAN_zoo https://github.com/facebookresearch/pytorch_GAN_zoo
53 efficientdet-pytorch https://github.com/zylo117/Yet-Another-EfficientDet-Pytorch ⭐⭐⭐
54 mmocr https://github.com/open-mmlab/mmocr ⭐⭐⭐
57 VideoMAE https://github.com/MCG-NJU/VideoMAE ⭐⭐⭐
58 pixel2style2pixel https://github.com/eladrich/pixel2style2pixel ⭐⭐
59 dlrm https://github.com/facebookresearch/dlrm ⭐⭐ EmbeddingBag
暂不支持
60 torecsys https://github.com/p768lwy3/torecsys ⭐⭐⭐
62 pit https://github.com/naver-ai/pit ⭐⭐⭐
63 ESPNetv2 https://github.com/sacmehta/ESPNetv2
64 SupContrast https://github.com/HobbitLong/SupContrast
65 3D-ResNets-PyTorch https://github.com/kenshohara/3D-ResNets-PyTorch
66 beit2 https://github.com/sfatimakhan/BEIT ⭐⭐⭐
67 PyTorch-GAN https://github.com/eriklindernoren/PyTorch-GAN ⭐⭐
68 vnet.pytorch https://github.com/mattmacy/vnet.pytorch
69 mmaction2 https://github.com/open-mmlab/mmaction2 ⭐⭐⭐
70 FSPNet https://github.com/ZhouHuang23/FSPNet ⭐⭐⭐
71 CMT https://github.com/gnebehay/CMT ⭐⭐⭐
72 PseCo https://github.com/ligang-cs/PseCo ⭐⭐⭐
73 BackgroundMattingV2 https://github.com/PeterL1n/BackgroundMattingV2 ⭐⭐⭐
74 MapTR https://github.com/hustvl/MapTR ⭐⭐⭐
75 mae https://github.com/facebookresearch/mae ⭐⭐⭐
76 xcit https://github.com/facebookresearch/xcit ⭐⭐⭐
77 T2T-ViT https://github.com/yitu-opensource/T2T-ViT ⭐⭐⭐
78 ContextPrior https://github.com/ycszen/ContextPrior ⭐⭐⭐
79 moco https://github.com/facebookresearch/moco ⭐⭐
80 moco-v3 https://github.com/facebookresearch/moco-v3/tree/main ⭐⭐⭐

其他模型

序号 模型名称 模型地址 难度 认领人 PR链接
81 ??? ??? ⭐⭐⭐⭐
82 ??? ??? ⭐⭐⭐⭐
@2742195759
Copy link
Contributor Author

2742195759 commented Nov 14, 2023

一个小教程~

AI Studio 开发环境申请

由于本任务需要编写测试脚本,建议使用统一环境来进行开发,推荐使用 AI Studio 框架开发功能开发~

可以参考飞桨线上开发环境——AI Studio 申请 AI Studio 开发环境~

AI Studio 环境创建

进入AI Studio 主页,选择创建项目
image

选择框架开发任务
image

注意,这里选择环境 cuda11.2-cudnn8。大家保持一致,防止不必要的环境问题。
image

创建完毕之后,启动即可。

仓库Clone:

进入环境之后clone仓库:

git clone https://github.com/PaddleJitLab/jit-exportable-models
cd jit-exportable-models

这里的仓库是主要的脚本仓库,大家可以参看里面的样例进行编写,这里针对这个样例详细的讲解一下开发过程:
image

样例讲解:TiSASRec.pytorch/start_export.sh

首先这个样例里面只有一个文件:./start_export.sh 脚本:

image

我们的主要目的是编写一个 ./start_export.sh 脚本作为总启动命令,这个脚本文件需要做:

  • 为仓库准备好必要依赖,对应到 start_export.sh 中是如下的部分

    image
  • PaddleJitLab 中创建 PyTorch 和 Paddle 两个版本的仓库(clone 或则是创建新的都可以)。然后 clone 下来,运行对应的命令。
    image

最终的执行成功与否只需要在 export.py 中打印出下面两个log即可:

image

上述的 ./start_export.sh 的总体架构很简单,但是依赖了两个 PaddleJitLab 的仓库地址,以及对这两个仓库中的 export.py 脚本的调用。这个章节我们讲述如何创建这两个仓库。分别是 PyTorch 仓库和 Paddle 仓库的创建两个部分。

PyTorch 仓库创建:

首先对于 torch.export 的运行仓库。作为每个任务,我们都会获取一个初始的仓库地址,比如这里的 TiSASRec.pytorch 就是初始的地址。

用户获取了初始地址之后,阅读项目的README,首先尝试复现仓库的训练流程,比如这里我们发现README中提到了复现流程是:

image

所以我们查看 main.py ,查看 model 的创建位置:

image

我们发现在这里进行了model的forward调用,同时传入了需要的数据。因为我们的目的是测试 torch.export.export 接口是否可以导出成功,所以我们使用成本最小的方式来实现,直接拷贝一份main.py,命名为export.py(名字随意,只要 start_export.sh 可以启动即可),然后进行如下的修改:

image

可以看到,就是在forward后面插入 torch.export.export 的调用。所以我们的export.py 就会测试export,如果导出成功,那么就输出标志性的日志,表示成功。【注意这里的日志一定要一致,不要有任何的diff,后续数据统计会通过这个来统计】。

这样我们将新的 export.py 连同原来的仓库文件,一起提交到 JitLab 组织下。就完成了Torch仓库的构建。

Paddle仓库创建:

为了确保 Paddle 和 Torch 对比的权威性和公平性,我们希望 Torch 和 Paddle 的模型有同样的代码结构。为了获取到同样结构的两个模型。我们使用 PaConvert 工具将 Torch 仓库转化为 Paddle 仓库。 PaConvert 仓库地址如下:可以参考README来使用 https://github.com/PaddlePaddle/PaConvert 。这里使用下面的命令

python3.8 -m pip install -U paconvert
paconvert --in_dir TiSASRec.pytorch/ --out_dir TiSASRec.pytorch.paddle

然后我们可以看到输出:

image

发现有一些代码无法转写,大概率是Paddle中没有对应的Torch API,这类问题需要开发者手动解决,全局搜索 >>> 就可以知道哪些问题需要手动解决了:

image

比如 utils.py 中,这个 start() 不是 Torch的组网API,所以我们直接保留即可,后面的 zero_grad 通过查看Paddle的Adam的文档,可以知道对应的是 clear_grad ,所以我们换了即可。

最后我们就得到了 Paddle 的仓库:TiSASRec.pytorch.paddle。然后将加上 save 的调用即可:

paddle 仓库的 export.py 如下所示:

image

其他的和 torch 的 export.py 类似,然后我们运行命令,可以查看是否可以正确测试 save,一切没有问题之后,就可以给PaddleJitLab提交上述的仓库了。

总结:

到此,两个仓库的都构建完毕,而且 start_export.py 也写完了,这里总结一下完整的工作:

  • 提供给Torch原始仓库:TiSASRec.pytorch
  • 我们给Torch仓库添加一个export.py脚本,测试export功能,记得打印日志,然后创建新仓库并提交到 PaddleJitLab 组织中:https://github.com/PaddleJitLab/TiSASRec.pytorch
  • 我们使用PaConvert工具将Torch仓库转变为Paddle仓库,然后添加export.py,测试save功能。创建新仓库并提交到 PaddleJitLab 组织中:https://github.com/PaddleJitLab/TiSASRec.pytorch.paddle
  • 我们在 https://github.com/PaddleJitLab/jit-exportable-models 中创建一个新的目录TiSASRec.pytorch,并在目录下放置一个 start_export.sh 作为入口脚本(执行的word directory就是 TiSASRec.pytorch 目录中)然后提一个PR即可。
    一定要确保我们的start_export.sh 可以完成运行,自动测试torch+paddle的导出功能(torch.export.export和 paddle.jit.save)输出如下的【JIT】日志:

image

参考资料:

  1. https://github.com/PaddlePaddle/PaConvert 【PaConvert项目地址】

Note

  • 初次参与任务可能没有权限在 PaddleJitLab 创建 repo,可先在个人账户创建 repo,在首次任务通过后会邀请加入组织,之后可直接在组织中创建 repo
  • 如有模型 PyTorch 动态图就跑不通,直接反馈即可

@SigureMo SigureMo assigned 2742195759 and SigureMo and unassigned zhiboniu Nov 14, 2023
@SigureMo SigureMo changed the title 【PaddleJitLab】Torch 和 Paddle 仓库模型导出成功率对比任务 【PaddleJitLab】PyTorch 和 Paddle 仓库模型导出成功率对比任务 Nov 14, 2023
@GreatV
Copy link
Contributor

GreatV commented Nov 15, 2023

第一步的fork,怎么fork到组织仓库呀?

@SigureMo
Copy link
Member

第一步的fork,怎么fork到组织仓库呀?

  • Fork 后并添加 torch.export.export 代码的 PyTorch 模型 repo 1
  • 通过 PaConvert 转换并添加 paddle.jit.save 代码的 Paddle 模型 repo 2
  • PaddleJitLab/jit-exportable-models 提交 PR,增加可运行的自动化测试 shell 脚本

任务完成且通过后会邀请进入 PaddleJitLab 组织,并迁移 repo 1 和 repo 2 进组织~

以上面描述为准,先 fork 到个人账户,任务完成后统一迁移到组织账户,目前任务描述还有些地方没有修改~

@SigureMo SigureMo changed the title 【PaddleJitLab】PyTorch 和 Paddle 仓库模型导出成功率对比任务 [Draft][Dy2St] PyTorch 和 Paddle 仓库模型导出成功率对比任务 Nov 15, 2023
@GreatV
Copy link
Contributor

GreatV commented Nov 15, 2023

怎么解决不同仓库环境不一致的问题呀,有的年久失修pytorch版本会跑不起来

@SigureMo
Copy link
Member

怎么解决不同仓库环境不一致的问题呀,有的年久失修pytorch版本会跑不起来

跑不起来的模型可以反馈下即可~

@GreatV
Copy link
Contributor

GreatV commented Nov 15, 2023

认领 pytorch-hed

@ooooo-create
Copy link
Contributor

认领 vnet.pytorch

@GreatV
Copy link
Contributor

GreatV commented Nov 19, 2023

ConSinGAN 好像导不了

https://github.com/GreatV/ConSinGAN.paddle/blob/40988d58f9117c1efe5586e26a4223440b9a6107/export.py#L27-L41

ValueError: The feeded_var_names[1]: 'eager_tmp_1' doesn't exist in pruned inference program. Please check whether 'eager_tmp_1' is a valid feed_var name, or remove it from feeded_var_names if 'eager_tmp_1' is not involved in the target_vars calculation.

@SigureMo
Copy link
Member

ConSinGAN 好像导不了

无法导出的也要如实的记录,这些模型我们也没测过,所以并不能保证 Paddle 能成功导出,没有其他问题的话可以直接提交的

@SigureMo SigureMo changed the title [Draft][Dy2St] PyTorch 和 Paddle 仓库模型导出成功率对比任务 [Dy2St] PyTorch 和 Paddle 仓库模型导出成功率对比任务 Nov 20, 2023
@Liyulingyue
Copy link
Contributor

认领1 3

@sanbuphy
Copy link
Contributor

认领 2 FastSAM

@rhmaaa
Copy link
Contributor

rhmaaa commented Nov 20, 2023

认领 65

@enkilee
Copy link
Contributor

enkilee commented Nov 20, 2023

认领13,33

@ccsuzzh
Copy link
Contributor

ccsuzzh commented Nov 20, 2023

认领 51

@enkilee
Copy link
Contributor

enkilee commented Nov 20, 2023

认领33

@sunzhongkai588
Copy link
Contributor

认领16

@DrRyanHuang
Copy link
Member

DrRyanHuang commented Nov 20, 2023

认领 23,19,27,69

@longranger2
Copy link
Contributor

认领15,23

@xiaoyewww
Copy link
Contributor

认领26

@GreatV
Copy link
Contributor

GreatV commented Dec 16, 2023

54 mmocr 的模型非常多,每一个都要转吗
image

@GreatV
Copy link
Contributor

GreatV commented Dec 17, 2023

59 dlrm 需要用到 EmbeddingBag,paddle 暂不支持。

@2742195759
Copy link
Contributor Author

54 mmocr 的模型非常多,每一个都要转吗 image

可以先选一个转吧,不用全都转。

@2742195759
Copy link
Contributor Author

59 dlrm 需要用到 EmbeddingBag,paddle 暂不支持。

好的,麻烦再列表后面标记一下吧。也算是一个摸底了。

@Little-chick
Copy link
Contributor

认领52

@GreatV
Copy link
Contributor

GreatV commented Dec 31, 2023

6 Real-ESRGAN 依赖 BasicSR 依赖比较复杂,模型不好导出。

@GreatV
Copy link
Contributor

GreatV commented Jan 3, 2024

8 pytorch-segmentation 依赖 timm 依赖比较复杂,模型不好导出。
image

@Mister-TT
Copy link

认领63

@Mister-TT
Copy link

63 需要用到kaiming_normal_的fan_out模式,paddle不支持
1705408033028

@Little-chick
Copy link
Contributor

认领50AdaptSegNet

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: In Progress
Development

No branches or pull requests