Skip to content

fix: load OOM traces that omit addr during snapshot import - #100

Merged
codeasier merged 2 commits into
mainfrom
issue-97-oom-trace-load
Aug 19, 2026
Merged

codeasier merged 2 commits into
mainfrom
issue-97-oom-trace-load

Conversation

@codeasier

@codeasier codeasier commented Aug 19, 2026 •

Copy link
Copy Markdown
Owner

Summary

  • Allow TraceEntry.from_dict to load PyTorch OOM events that omit addr/stream, using dataclass defaults (addr=-1, size=0, stream=0) and reading device_free.
  • Map "oom" to action value 8 so snapshot import writes an integer instead of a string.
  • Add unit coverage for OOM from_dict, replay skip, and snapshot2db dump of addr-less OOM events.

Fixes #97

Test plan

  • Reproduce OOM from_dict without addr
  • Snapshot/replay/dump unit tests for addr-less OOM events
  • Confirm pt-snap import succeeds on a snapshot whose device_traces include an OOM event

Snapshot provenance decision: updated

@codeasier
codeasier force-pushed the issue-97-oom-trace-load branch from 5faa942 to e94365d Compare August 19, 2026 15:51

@codeasier codeasier left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

检视结论:可以合并

改动小而聚焦,方向正确。TraceEntry dataclass 本来就声明了 addr: int = -1 # not present for OOM 和 device_free,但 from_dict 用硬取键的方式读取,注释与实现自相矛盾——改成复用 dataclass 默认值是正确修法,而不是打补丁。"oom": 8 追加在映射末尾、没有重排既有的 0–7,因此不破坏已生成的数据库;回放侧无需改动,ReplayExecutor.execute 对未识别 action 走末尾的 skip 分支并 return True,OOM 天然被跳过,PR 只是补测试锁定了这个行为。PROVENANCE 已按仓库门禁要求记录为一方 bugfix。4 个 CI 任务全绿。

下面留了几条行内意见,都不阻塞合并。另外 PR 描述里 test plan 的第三项——用真实含 OOM device_traces 的快照跑通 pt-snap import——仍未勾选。单测覆盖了 pickle 构造的等价路径,但真机验证一次更稳妥。

size=int(trace_dict["size"]),
stream=int(trace_dict["stream"]),
addr=int(trace_dict.get("addr", -1)),
size=int(trace_dict.get("size", 0)),

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

size 的默认值放宽得过头了。

addr/stream 缺失是 OOM 的已知特征,放宽合理;但 size 对所有 action 都是必需字段——OOM 事件里它恰恰是最关键的信息(请求了多少字节却失败了)。改成 .get("size", 0) 之后,一条畸形的 alloc(漏了 size)会静默变成 0 字节而不再抛 KeyError,掩盖数据损坏。

建议只对 OOM 放宽,或至少保留 size 为必需键:

is_oom = trace_dict["action"] == "oom"
...
addr=int(trace_dict["addr"]) if not is_oom else int(trace_dict.get("addr", -1)),
size=int(trace_dict["size"]),

addr=int(trace_dict.get("addr", -1)),
size=int(trace_dict.get("size", 0)),
stream=int(trace_dict.get("stream", 0)),
device_free=int(trace_dict.get("device_free", -1)),

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

device_free 解析出来后目前无人消费:全仓库除本行和新增测试外没有任何引用,snapshot2db 没有对应列,to_dict 的非 _origin 分支也不输出它。所以这个值只存在于内存对象里,测试断言的是一个暂时不可观测的字段。

本身不算错(补上了之前完全没解析的缺口),但如果后续要做 OOM 分析,值得单独开 issue 跟踪落库 + to_dict 输出。

"free_requested": 5,
"free_completed": 6,
"workspace_snapshot": 7,
"oom": 8,

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

映射表仍缺 snapshot。

TraceEntry 的 docstring 列出了 snapshot action,但这张表里没有它。sqlite_meta.py 的转换是 value_map.get(value, value),未命中就把原始字符串直接写进 INTEGER 列——也就是本 PR 想消除的"写字符串而不是整数"的问题,在 snapshot 上依然存在,属于同一个 bug 的另一个入口。

建议在本 PR 顺手补一个值;或者更好的做法是给未命中 value_map 的 action 加一条显式告警/异常,避免下一次同类 issue 再以线上 import 失败的形式暴露。

self.assertEqual(snapshot.trace_entries[0].idx, 0)
self.assertEqual(snapshot.trace_entries[1].idx, 1)

self.assertTrue(

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:这里删掉了一个与本 PR 无关的空行,建议还原以保持 diff 干净。

@codeasier codeasier left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

检视结论:✅ 可以合并

改动小而聚焦(85 增 4 删,7 个文件),4 个 CI 任务全绿(Lint、Provenance、Python 3.10–3.13),修复方向正确。

核心改动评估

  • from_dict 使用 .get() 复用 dataclass 默认值:正确修法。addr: int = -1 # not present for OOM 的注释早已表达意图,但原来 trace_dict["addr"] 硬取键与之自相矛盾,现在终于对齐。
  • device_free 补全解析:顺带填补了真实缺口,之前完全未被解析。
  • 回放路径无需改动:ReplayExecutor.execute 对未识别 action 走 Skip event... during replay 分支并 return True,OOM 天然被跳过;PR 补的测试锁定了此行为,足够。
  • "oom": 8 追加在映射末尾,未重排 0–7,不破坏已生成数据库;test_database_schema_and_action_state_mapping_are_exact 两处同步修改,仍成立。
  • PROVENANCE.md:记录明确,标注为一方 bugfix、不影响上游映射与许可证据,符合仓库 provenance 门禁要求。

三点建议(不阻塞合并)

1. size 的默认值放宽得过头了

# 现在
size=int(trace_dict.get("size", 0))

addr/stream 缺失是 OOM 的已知特征,但 size 对所有 action 都是必需字段。当前写法会让一条畸形的 alloc(漏了 size)静默变成 0 字节而不再报错,掩盖数据损坏。建议只对 OOM 放宽:

size=int(trace_dict.get("size", 0) if trace_dict.get("action") == "oom" else trace_dict["size"])

或至少保留 size 为必需键,不提供默认值。

2. 映射表仍缺 snapshot 等 action

TraceEntry docstring 列出了 snapshot action,但 TRACE_ENTRY_ACTION_VALUE_MAP 里没有。sqlite_meta.py 的转换是 value_map.get(value, value),未命中时把原始字符串写入 INTEGER 列——正是本 PR 想消除的问题,在 snapshot 上依然存在。可以在本 PR 顺手补值,或者更好:给未知 action 加显式告警,避免下次同类 issue 再出现。

3. device_free 解析后无人消费

全仓库除 entities.py 自身和新增测试外无任何引用;snapshot2db 无对应列,to_dict 的非 _origin 分支也不输出它。值目前只存在于内存对象中。若 OOM 分析是后续目标,建议单独开 issue 跟踪落库;否则测试断言的是一个暂时不可观测的字段。


小瑕疵

  • tests/snapshot/test_entities.py 第 410 行附近删掉了一个与本 PR 无关的空行,属于噪声改动。
  • PR 描述 test plan 第三项(用真实含 OOM device_traces 的快照跑通 pt-snap import)仍未勾选。单测覆盖了 pickle 构造的等价路径,但有条件的话真机跑一下更稳妥。

codeasier and others added 2 commits August 20, 2026 01:15
PyTorch OOM events lack addr/stream, so TraceEntry.from_dict raised
KeyError and aborted import before replay could skip them.

Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
@codeasier
codeasier force-pushed the issue-97-oom-trace-load branch from 82a5dfe to afdca7b Compare August 19, 2026 17:16
@codeasier
codeasier merged commit aa43deb into main Aug 19, 2026
6 checks passed
@codeasier
codeasier deleted the issue-97-oom-trace-load branch October 3, 2026 19:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] OOM 事件缺少 addr 导致 snapshot import 加载失败

1 participant