{"id":24771073,"url":"https://github.com/pfcclab/paddleviz","last_synced_at":"2025-10-11T20:31:23.485Z","repository":{"id":185438041,"uuid":"663387277","full_name":"PFCCLab/paddleviz","owner":"PFCCLab","description":null,"archived":false,"fork":false,"pushed_at":"2023-09-26T07:25:11.000Z","size":2163,"stargazers_count":4,"open_issues_count":0,"forks_count":2,"subscribers_count":4,"default_branch":"main","last_synced_at":"2024-11-30T18:36:08.556Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/PFCCLab.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2023-07-07T07:19:57.000Z","updated_at":"2024-11-20T16:52:12.000Z","dependencies_parsed_at":null,"dependency_job_id":"2e6d71ea-ecef-4823-8586-35fab2bc4667","html_url":"https://github.com/PFCCLab/paddleviz","commit_stats":null,"previous_names":["pfcclab/paddleviz"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PFCCLab%2Fpaddleviz","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PFCCLab%2Fpaddleviz/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PFCCLab%2Fpaddleviz/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PFCCLab%2Fpaddleviz/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/PFCCLab","download_url":"https://codeload.github.com/PFCCLab/paddleviz/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":236126453,"owners_count":19098952,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-01-29T03:57:50.139Z","updated_at":"2025-10-11T20:31:23.113Z","avatar_url":"https://github.com/PFCCLab.png","language":"Python","readme":"# paddleviz\n一款适用于`PaddlePaddle`框架的反向图可视化工具。\n\n## 概述\n飞桨深度学习框架提供了动态图编程的模式来开发深度学习模型，但动态图的反向图调试能力仍存在不足。为了更好地追踪反向图执行流程，本项目为飞桨框架提供了反向图可视化能力。\n\n## 安装\n### paddle 版本\n由于是新特性，运行本项目需要从[官网](https://www.paddlepaddle.org.cn/install/quick?docurl=/documentation/docs/zh/develop/install/pip/linux-pip.html)安装最新版本的`paddlepaddle`。\n\n安装命令\n```shell\npython -m pip install paddlepaddle==0.0.0 -f https://www.paddlepaddle.org.cn/whl/linux/cpu-mkl/develop.html\n```\n\n### 其他依赖包\n运行本项目需要提前安装第三方可视化工具包`graphviz`。\n\n安装命令\n```shell\npip install graphviz\n```\n\n## 使用\n1. 首先需要定义网络模型以及可视化代码，下面是一个基本的使用例子。\n\n在该示例`example.py`中，我们进行了以下步骤：\n* 1. 将系统日志输出级别设为`6`。\n* 2. 定义网络，调用网络前向推理过程，获取动态图网络的最终输出。\n* 3. 调用`backward()`进行一次反向推理。\n* 4. 调用本项目`paddleviz.viz`包下的`make_graph()`函数获取反向图可视化结果，函数返回值是一个`Digraph`类型的实例。\n* 5. 调用`Digraph`实例的`render()`方法绘制并保存反向图可视化结果。\n\n```python\nimport os\n\n# 将日志级别设置为6\nos.environ['GLOG_v'] = '6'\n\nimport paddle\nimport paddle.nn as nn\n\nfrom paddleviz.viz import make_graph\n\nclass Model(nn.Layer):\n    def __init__(self):\n        super(Model, self).__init__()\n        self.conv = nn.Sequential(\n            nn.Conv2D(3, 6, 3, 1), # in_channels, out_channels, kernel_size\n            nn.Sigmoid(),\n            nn.MaxPool2D(2, 2), # kernel_size, stride\n            nn.Conv2D(6, 16, 3, 1),\n            nn.Sigmoid(),\n            nn.MaxPool2D(2, 2)\n        )\n        self.fc = nn.Sequential(\n            nn.Linear(16*6*6, 120),\n            nn.Sigmoid(),\n            nn.Linear(120, 84),\n            nn.Sigmoid(),\n            nn.Linear(84, 10)\n        )\n\n    def forward(self, img):\n        feature = self.conv(img)\n        output = self.fc(feature.reshape([img.shape[0], -1]))\n        return output\n\nif __name__ == '__main__':\n    \n    # 定义网络\n    model = Model()\n    x = paddle.randn([1, 3, 32, 32])\n\n    # 正向推理\n    y = model(x)\n\n    # 反向推理\n    y.sum().backward()\n\n    # 可视化网络反向图，dpi 代表分辨率，默认为600，如果网络较大，可以改为更大的分辨率\n    dot = make_graph(y, dpi=\"600\")\n\n    # 绘制保存反向图\n    dot.render('viz-result.gv', format='png', view=False)\n```\n2. 定义好模型文件后，运行如下命令：\n```\npython paddleviz/run.py [模型调用文件路径，如example.py]\n```\n\n示例中网络的反向图可视化结果为：\n\n![image-result](./images/result.png)\n\n## 原理\n动态图在执行前向推理逻辑的同时，也会创建对应的反向图。反向图的数据结构如下图所示，其中每个节点都表示一个`算子(xx_OP)`，该算子节点会将`梯度信息(xx_G)`和`Tensor信息(xx_)`作为输入，计算并输出最终的反向梯度信息，然后输出的反向梯度信息作为下一个节点的输入，反复向上递归这一过程，直到到达不需要计算梯度的节点为止。\n\n![forward](./images/forward.png)\n\n对于反向图中的每个节点通过调用其`next_functions()`函数来获取与他相连的反向结点，然后递归调用`add_nodes()`函数从而完成整个反向图的可视化。\n\n模型反向推理时会输出日志，日志中包含每个算子输入输出的反向梯度信息，通过读取日志补充算子间边上详细信息，得到最终的反向图。","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpfcclab%2Fpaddleviz","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpfcclab%2Fpaddleviz","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpfcclab%2Fpaddleviz/lists"}