笔者在服务器上进行pytorch profiler的实验,导出的json文件下载到本地之后通过edge://tracing打开时报错(edge和chrome使用的是同一套内核,笔者也测试了在chrome下同样报错):
SyntaxError: Unexpected token 'i', ..." per SM": inf,"... is not valid JSONat JSON.parse (<anonymous>)at new TraceEventImporter (edge://tracing/tracing.js:6342:19)at Import.createImporter_ (edge://tracing/tracing.js:2033:8)at edge://tracing/tracing.js:2025:167at Task.run (edge://tracing/tracing.js:2948:95)at runAnother (edge://tracing/tracing.js:2951:371)at runTask (edge://tracing/tracing.js:2927:57)at processIdleWork (edge://tracing/tracing.js:2932:116)at window.requestIdleCallback.timeout (edge://tracing/tracing.js:2925:81) pytorch profile
通过vscode打开之后发现有很多红色的报错信息,看到是inf,和报错信息也吻合了。
GPT给出的脚本,笔者进行了微调:
import json# 读取原始 JSON
with open('trace.json', 'r') as f:raw_data = f.read()# 替换不兼容的值
raw_data = raw_data.replace('inf', '1e308') # 替换 Infinity
raw_data = raw_data.replace('nan', 'null') # 替换 NaN# 解析为字典
data = json.loads(raw_data)# 写回 JSON 文件
with open('trace_fixed.json', 'w') as f:json.dump(data, f, indent=4)print("修复后的 JSON 已保存为 trace_fixed.json")