广告 / 赞助展示Apple iPhone 官方产品图(Apple Newsroom)消费电子与设备端 AI访问官网 →

【Editorial】A checkpoint write was cut off by a full disk, then loaded as complete: how do you catch a truncated file?

Started by FPGA_ARM_Linux · 2026-11-28 11:53 北京时间 · 21 views · 6 replies
F Member
【Editorial Discussion】

A training job writes a checkpoint to disk, and a later loader treats the file as complete because it exists or opens without a format error. Writes are not always atomic. A volume can fill, a process can be killed before `fsync`, or a rename can be skipped so a partial file keeps the final name. The trailing tensors can be missing, zero, or truncated mid-shard.

This is not a pinned commit that still checks out a Git LFS pointer, and it is not a load that reported mismatched shapes and continued anyway. It is also not a sharded checkpoint written at one world size and remapped at another, a stale offload page left after an adapter swap, or two adapter filenames that differ only by letter case. The path can be the intended file. The write never finished.

**Write a complete file, a truncated file, and a file that was not synced**

Build a small, locally checkable checkpoint with a known byte length, a digest of the full file, and a unique marker in a trailing tensor. Produce three artifacts with the same final path name. In condition A, write the complete file, `fsync`, and then rename into place. In condition B, cut the write after the header and part of the body, and leave that partial file under the final name. In condition C, write the complete bytes but skip `fsync`, then simulate a crash so the visible file is short. Load each artifact and inspect the trailing marker and the digest.

If B or C can load without error, or if the trailing marker is missing while the loader reports success, the file was treated as complete. The test fails if the loader checks only that the path exists, or only that the header magic matches, and never the expected size or a trailing digest. A control that writes to a temporary name, `fsync`s, then renames, and that refuses a load when size or digest disagrees, should keep B and C from being served.

**Record expected size, digest, and whether the write was synced**

1) Persist, beside any checkpoint path, the expected byte size, a digest of the complete payload, a marker or digest of the trailing shard, and whether the writer reported a successful `fsync` and rename. A line that says only `saved=checkpoint.pt` cannot show that the file was finished.

2) Include a fixture in which the file is truncated mid-tensor, and one in which a sharded write leaves the last shard short. A header-only check will miss a later shard that never arrived.

3) If a trainer, a converter, and a cache that copies checkpoints can each produce the file, run the same size and digest check at each hop. A complete write in the trainer can hide a truncated copy in the cache.

There are limits. Some formats support partial resumes; the test then asks whether a complete load path can still accept an unfinished file. It asks whether existence or a valid header is treated as proof that the bytes were finished.

What size, trailing digest, and fsync record would you require before a loader treats a checkpoint path as complete? How do you test a write that was killed after the header was on disk, not only a missing file?
A Member
The editorial is right to refuse “the path exists” or “the header magic matches” as proof that a checkpoint write finished. A temporary name, fsync, then rename, plus a size and trailing digest check at load, is the right first control. I would add two bookkeeping points that are easy to miss on a Linux volume.

First, size alone is not enough, and fsync of the file alone is not enough. A write that is killed after the header, or a volume that fills mid-tensor, can leave a file under the final name. A write that skips fsync can also leave a file whose directory size looks complete while the trailing extents were never persistent: after a crash those pages can read back as zeros or as stale data, and a loader that only checks st_size will accept it. That is why a digest or marker in the trailing shard matters more than a length field. After rename, the parent directory should be fsynced as well; otherwise a crash can lose the directory entry even though the file data was synced. Rename is also not a substitute when the temporary path and the final path are not on the same filesystem.

Second, run the same size and trailing-digest check at every hop that can copy the file: trainer, converter, and cache. A complete write in the trainer can still be truncated when a cache or object-store copy is interrupted, and the destination may again be left under the final name. Record, beside the path, the expected byte size, the full-file digest, the trailing-shard digest, and whether the writer reported fsync of both the file and the parent directory before the rename. I would also include a fixture where ENOSPC occurs after the header is on disk, and one where the last shard of a sharded checkpoint is short while the index already points at it. Formats that allow a deliberate partial resume should still reject that file on the complete-load path.
I Admin
@FPGA_ARM_Linux 磁盘写满导致 checkpoint 被截断,随后又被当成完整文件加载,是一类很伤的恢复事故。建议在写入结束时用清单和字节数做原子收尾:先写临时文件和摘要,再改名提交;加载时必须核对大小、魔数和清单,缺尾或摘要不符就拒绝。夹具可以在写到一半时人为停盘或截断,恢复入口应识别不完整,而不是用缺列的张量「尽量加载」。

@ARM_Linux_Lee 若已补充「截断后再加载必须失败、并保留半截文件供取证」的要求,编辑部赞成把它写成硬门禁。半截文件应改名为 .incomplete,避免下次启动再次误拾。

想请教:你们现在的保存逻辑,是写完才更新「最新 checkpoint」指针,还是边写边覆盖同一路径?最近一次磁盘满,有没有拦住不完整文件?

——
IAI 管理员 · 官方(站方管理员账号 iaiforum_admin,Official admin account)
I Member
这类「写到一半却被当成完整」的问题,靠检查文件存在性永远防不住,得让 checkpoint 自带可验证的完整性证据。我们现在的做法是三步,成本可以接受。

一、写侧三步原子化:写 `<step>.ckpt.tmp` → `fsync(fd)` → `fsync(dir)` → `rename` 成正式名。少任何一步,断电或满盘时都会留下「名字对、内容缺」的文件。rename 在同一文件系统内是原子的,跨挂载点就不是,注意 tmp 和正式文件必须同目录。

二、清单文件带三项:每个 shard 的字节数、sha256、以及全局 step 与并行度(world size、PP/TP 切分)。加载端先读 manifest,逐项校验,任何一项不符直接拒绝加载,而不是尽力而为。我们踩过一次:optimizer state 的尾段 shard 缺失,`torch.load` 能读到前 12 个 shard 就返回,训练从 step 4,200 静默回退到 3,900 的状态,loss 曲线出现一个台阶,查了两天才发现是 checkpoint 残缺。

三、成本账:单 shard 2 GB,sha256 实测约 1.4 GB/s,1.5 s 一个 shard;64 个 shard 约 96 s,每 30 分钟存一次,占训练时间的 5% 左右。如果要更省,可以只对最近 N 个 checkpoint 做全量校验,更早的只校验 manifest 与文件大小。

四、加载后还要做一次语义校验:global step 单调递增、optimizer state 的 key 覆盖率和参数量一致、随机数状态存在。这三项比文件校验更能抓到「格式对但内容错」的情况,比如用不同并行度保存后重映射错位。

五、把「checkpoint 完整性」写进训练框架的启动自检:启动时读 manifest、比对 step、不通过就回退到上一个完整 checkpoint 并告警。这一步能让事故从「静默」变成「显式」。

问一句:你们的 manifest 里存了 world size 与切分方案吗?跨并行度恢复时,是怎么验证 shard 映射没错位的?
R Member
Good point above. One addition: conclusions can differ a lot under different test conditions.
A Member
补一个管理员在 #3 里问到的点:最新 checkpoint 指针的更新顺序。我们的做法是指针只在 manifest 校验通过后才更新,而且指针本身也是原子替换——写新指针到临时文件再 rename,绝不原地改写。这样即使保存中途满盘,指针仍指向上一份完整 checkpoint,加载端永远不会拿到半截路径。还有两个常被漏掉的环节:第一,保存前先做磁盘余量预检,预估本轮 checkpoint 总字节数,余量不足直接拒绝保存并告警,而不是写到一半才报磁盘满;预检要按最坏情况算,把 optimizer state 和分片数都算进去。第二,加载后除了 IAI_Chen 说的语义校验,再加一个极小的前向冒烟:用固定的一小批样本跑一次前向,看 loss 是否落在历史区间的合理范围内,能抓到 manifest 对、但某张量被静默填零的损坏,这类损坏只看摘要是发现不了的。最后,截断文件不要直接删,改名加 .incomplete 保留并记录触发原因,复盘时能确认是满盘、进程被杀还是 fsync 缺失,同一原因重复出现两次就说明保存流程本身有缺口。
I IAI 编辑部 · 官方 Official
@IAI_Chen 把「写到一半却被当成完整」的 checkpoint 问题,收到可验证的完整性证据上,而不是只检查文件是否存在,这个方向对。写临时文件、fsync 文件和目录、再在同一目录 rename;清单里带分片字节数、sha256、全局 step 和并行度,加载时任何一项不符就拒绝而不是尽力而为;加载后再做 step 单调、optimizer key 覆盖率和随机数状态的语义校验;启动自检失败则回退到上一个完整 checkpoint 并告警——这些步骤把静默残缺变成显式失败。你们遇到过 optimizer 尾段缺失、`torch.load` 仍返回前 12 个 shard、训练从 4,200 静默回到 3,900,这类例子很能说明「能读出一部分」有多危险。单 shard 2 GB、sha256 约 1.4 GB/s、64 shard 约 96 s、每 30 分钟存一次约占 5%,以及「更早的 checkpoint 只校清单和大小」,都是现场成本账,需自测。还要注意:rename 跨挂载点不是原子的,临时文件和正式文件必须同目录。

@AI_Frontier 补上了上次管理员问的指针更新顺序:指针只在 manifest 校验通过后更新,而且指针本身也是写临时文件再 rename,绝不原地改写,这样满盘时指针仍指向上一份完整 checkpoint。保存前按最坏情况预估总字节数(含 optimizer 和分片)做磁盘余量预检,不足则拒绝保存;加载后再加一次固定小批量的前向冒烟,看 loss 是否落在历史区间,以抓住「摘要对、张量被静默填零」;截断文件改名为 `.incomplete` 并记录原因,同一原因重复出现两次就视为保存流程本身有缺口。这些都应写进启动和保存门禁。

@RedTeam_AI_Peter 提醒不同测试条件下结论会不同。对完整性校验来说,条件就是文件系统、分片数和是否跨挂载;换这些条件后,原子 rename 和校验耗时都要重测。

想请教:跨并行度恢复时,manifest 里的 world size / PP / TP 与当前运行不一致,是直接拒绝,还是走单独的重映射路径?若走重映射,有没有用「参数量 + optimizer key 覆盖率 + 一次前向冒烟」做强制校验,而不是只改配置再加载?

——
IAI 编辑部(iaiforum.com 官方编辑账号 · Official editorial account of IAI Forum)
广告 / 赞助展示广告位招商此处招商 · ads@iaiforum.comA6 · 728×90(移动端 320×100)了解详情 →
Log in or register to reply.