python将xml格式文件转成png或者pdf格式

本文主要介绍运行NCCL代码时输出的xml文件该如何转成更加容易观看的图格式

如下是举例,服务器上的PCIE相关的topo xml 文件

bash 复制代码
<system version="1">
  <cpu numaid="1" affinity="ffffff00,0000ffff,ff000000" arch="x86_64" vendor="GenuineIntel" familyid="6" modelid="106">
    <pci busid="0000:98:00.0" class="0x060400" vendor="0x1bd4" device="0x3004" subsystem_vendor="0x0000" subsystem_device="0x0000" link_speed="16.0 GT/s PCIe" link_width="16">
      <pci busid="0000:a6:00.0" class="0x030200" vendor="0x10de" device="0x2235" subsystem_vendor="0x10de" subsystem_device="0x145a" link_speed="16.0 GT/s PCIe" link_width="16">
        <gpu dev="0" sm="86" rank="0" gdr="1"/>
      </pci>
      <pci busid="0000:a5:00.0" class="0x020000" vendor="0x15b3" device="0x1021" subsystem_vendor="0x15b3" subsystem_device="0x0023" link_speed="16.0 GT/s PCIe" link_width="16">
        <nic>
          <net name="mlx5_0" dev="0" speed="200000" port="1" latency="0.000000" guid="0xbe8cb30003ae6d94" maxconn="131072" gdr="1"/>
        </nic>
      </pci>
    </pci>
  </cpu>
</system>

将该文件转换成图格式

python 复制代码
import os
import sys
import logging
import argparse
import xml.etree.ElementTree as ET
from graphviz import Graph


def build_topology(graph, parent_element, parent_node=None):
    dev = parent_element.tag
    res = None
    if dev == 'cpu':
        res = parent_element.get('numaid').replace(':', '_')
    elif dev == 'pci':
        res = parent_element.get('busid').replace(':', '_')
    elif dev == 'gpu':
        res = parent_element.get('dev').replace(':', '_')
    elif dev == 'nvlink':
        res = parent_element.get('target').replace(':', '_')
    elif dev == 'net':
        res = parent_element.get('name').replace(':', '_')
    
    node = dev
    if res is not None:
        node = dev + '_' + res
    
    all_attributes = {attr: parent_element.get(attr) for attr in parent_element.attrib}
    str_attr = node + '\n\n' + str(all_attributes).replace(',', '\n')
    graph.node(node, shape='box', label=str_attr)
    
    if parent_node is not None:
        graph.edge(parent_node, node)
    
    for child_element in parent_element:
        build_topology(graph, child_element, node)


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Parse xml file to pdf or png.')
    parser.add_argument('-i', '--input', required=True, help='Input file path')
    parser.add_argument('-o', '--output', default='xml', help='Output file name')
    args = parser.parse_args()

    input_file_path = args.input
    output_file_name = args.output


	# 解析XML数据
	tree = ET.parse(input_file_path)
	# 获取 XML 文档对象的根结点 Element
	root_element = tree.getroot()
	
	graph = Graph()
	build_topology(graph, root_element)
	
	# 绘制拓扑图
	graph.render(output_file_name, format='png')
	# graph.view()
	print('Finished')

得出结果为

相关推荐
wj3055853783 小时前
课程 9:模型测试记录与 Prompt 策略
linux·人工智能·python·comfyui
星寂樱易李3 小时前
iperf3 + Python-- 网络带宽、网速、网络稳定性
开发语言·网络·python
qingfeng154153 小时前
企业微信机器人开发:如何实现自动化与智能运营?
人工智能·python·机器人·自动化·企业微信
彦为君6 小时前
Agent 安全:从权限提示到沙箱隔离
python·ai·ai编程
PILIPALAPENG7 小时前
Python 语法速成指南:前端开发者视角(JS 类比版)
前端·人工智能·python
用户8356290780518 小时前
Python 操作 PowerPoint 页眉与页脚指南
后端·python
枫叶林FYL8 小时前
项目九:异步高性能爬虫与数据采集中枢 —— 基于 Crawl<sub>4</sub>AI 与 Playwright 的现代化数据采集平台 项目总览
爬虫·python·深度学习·wpf
猫猫的小茶馆9 小时前
【Python】函数与模块化编程
linux·开发语言·arm开发·驱动开发·python·stm32
驯龙高手_追风9 小时前
Adobe Acrobat PDF阅读器设置默认滚动翻页
adobe·pdf·adobe acrobat reader·adobe reader