Skip to content

ps_sweep: preserve sample identity when reusing an output directory - #5987

Open
tahffo68hk-hash wants to merge 2 commits into
ItsNotRexed:mainfrom
tahffo68hk-hash:fix/ps-sweep-pin-sample
Open

tahffo68hk-hash wants to merge 2 commits into
ItsNotRexed:mainfrom
tahffo68hk-hash:fix/ps-sweep-pin-sample

Conversation

@tahffo68hk-hash

Copy link
Copy Markdown

What and why

Reusing a pixel-sweep output directory with a different seed can attribute old GPU results to a new shader. A synthetic index with 20 pixel shaders changed the shader at row 0 when I switched seeds. The next run --hi 1 still completed without invoking the worker because row 0 already had a result.

Root cause

sample unconditionally replaced sample.json. The run and compare commands use positions in that file as the keys for result records and readbacks.

Fix

Publish a complete sample.json without overwriting the destination. An identical ordered sample can be requested again. A different sample requires a new --out directory, and the previous sample and results stay intact.

Tests

Limitations

CPU and synthetic fixtures only. No GPU or driver was exercised. The Windows path was exercised directly. The POSIX publication path uses a same-filesystem hard link and was not run on a POSIX host. Existing results have no shader identifier, so this change cannot detect a previous mix-up.

Related

Coordination claim. #5881 and #5678 touch the same tool. This patch has no PR dependency.

AI-assisted.


问题和影响

更换种子后复用像素扫描输出目录,可能把旧 GPU 结果归到新的 shader 上。用包含 20 个像素 shader 的合成索引复现时,换种子改变了第 0 行的 shader。随后的 run --hi 1 因为第 0 行已经有结果,未调用 worker 就报告完成。

根因

sample 无条件覆盖 sample.json。run 和 compare 把该文件中的行号作为结果记录和读回文件的键。

修复

完整发布 sample.json,同时保护已有目标文件。相同顺序的样本可重复请求。不同样本需使用新的 --out 目录,已有样本和结果保持完整。

测试

验证范围

仅使用 CPU 和合成输入。未运行 GPU 或驱动测试。直接验证了 Windows 发布路径。POSIX 发布路径使用同一文件系统内的硬链接,未在 POSIX 主机上运行。已有结果不含 shader 标识,因此无法追溯检测过去的错配。

关联

协调帖认领。#5881 和 #5678 修改了同一工具。本补丁无 PR 依赖。

AI 辅助。

@Kev-Ball

Copy link
Copy Markdown

Exercised the POSIX hard-link publication path on CachyOS Linux, Btrfs, at 671e3fa8. I used the retail translation index (12,101 pixel shaders) and two seeds that produce different ordered 480-entry samples. With the old sampler, sampling the same directory with the second seed replaced the first sample.

I raced the two seeds in ten fresh directories through the normal sample CLI. Each time exactly one process succeeded, and the published JSON matched the winner’s independently generated 480-entry sample in order. In all ten directories, retrying the losing seed failed, retrying the winning seed succeeded, and the original file stayed byte-identical. No temporary files remained. This exercised sample publication only. AI-assisted.

@SweatierKey

Copy link
Copy Markdown
Contributor

Independent CPU verification on Linux (Python 3.13.5, head 671e3fa8, base main 561b65a8):

  • git diff --check origin/main...HEAD and py_compile — clean.
  • python3 -I tests/test_ps_sweep.py -v — 13/13 OK.

Edge-case bug found in the except FileExistsError: handler (tools/ps_sweep.py:119-122):
When sample.json already exists in --out as corrupt/truncated JSON, a directory, or a broken symlink, os.link/os.rename raises FileExistsError, and the unguarded open(sample_path) + json.load(f) leaks an uncaught Python traceback instead of a clean SystemExit:

  • Corrupt sample.json (b"{corrupt"): uncaught json.JSONDecodeError
  • Directory named sample.json: uncaught IsADirectoryError
  • Broken symlink named sample.json: uncaught FileNotFoundError

Wrapping the read/parse in try: ... except (OSError, ValueError) as exc: raise SystemExit(...) from None turns all three into a clean one-line error while still preserving the existing path.

@tahffo68hk-hash

Copy link
Copy Markdown
Author

@SweatierKey The existing-sample error now exits with one diagnostic and recommends a new output directory in 37419d3ac. I reproduced all three Windows regression subcases before the change (truncated JSON, invalid UTF-8, and a directory named sample.json). Afterward, all 14 sweep tests and 3 text line-ending tests passed, and the CLI returned exit 1 with no traceback for each subcase, preserving the original path. This Windows account could not create a symlink, so the dangling-symlink case here is covered by the same OSError path and your Linux reproduction.

@Kev-Ball The Linux race check confirms the POSIX publication path. This update leaves that path unchanged. My tests used synthetic fixtures only, on CPU. AI-assisted.

@SweatierKey 37419d3ac 现在会在无法读取已有样本时只输出一行诊断,并提示换用新目录。修改前我在 Windows 上复现了三个回归子用例(截断的 JSON、无效 UTF-8,以及名为 sample.json 的目录)。修改后,14 项扫描测试和 3 项文本行尾检查全部通过。三个子用例的 CLI 都以退出码 1 结束,未输出 traceback,原路径保持完好。当前 Windows 账号无法创建符号链接。悬空链接走相同的 OSError 处理路径,复现来自你的 Linux 测试。

@Kev-Ball Linux 并发测试验证了 POSIX 发布路径。本次更新没有改动这条路径。我的测试仅使用 CPU 和合成输入。AI 辅助。

@Kev-Ball

Copy link
Copy Markdown

Confirmed the dangling-symlink case on CachyOS/Btrfs at 37419d3ac using the retail translation index. I ran the same sample CLI with an existing sample.json symlink pointing to a missing file. On 671e3fa8, it exited 1 with a 19-line traceback. On the updated head, it exited 1 with one diagnostic suggesting a new output directory. The symlink retained its inode and link text, its target stayed absent, and the directory contained no temporary files. AI-assisted.

@FIocker

FIocker commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Independent synthetic check of 37419d3ac on Windows 11 / CPython 3.12 / NTFS. All 14 test_ps_sweep.py cases passed. In 120 rounds of racing two real CLI processes with different seeds against an empty output directory, precisely one published each time. All 11,153 successful reads matched the winning 20-entry sample after JSON decoding, and 120 same-seed retries preserved bytes and mtime. The base sequentially overwrote the sample with a different seed.

I hit a transient sharing issue worth separating from malformed samples. Two losing writers reported cannot read existing sample.json: [Errno 13] Permission denied …; use a new --out directory instead of the different-sample message, and one ordinary Path.read_bytes() call saw the same errno. The published JSON stayed intact.

A deterministic Windows control is to open the fully written temporary file via CreateFileW with DELETE access and FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, rename it to sample.json, and keep that handle open. A standard read of the new name gets Win32 error 32, a reader sharing delete access opens it successfully, and a same-seed ps_sweep.py sample invocation reports the permission error. Closing the handle lets the same command succeed in the same directory while preserving the file. This suggests a bounded retry for a transient sharing violation or a read that allows delete sharing could preserve the intended diagnostic. Synthetic inputs only. AI-assisted.

@shayderrr shayderrr left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Review

Reviewed change in PR #5987: ps_sweep: preserve sample identity when reusing an output directory.

Scope: tests/test_ps_sweep.py, tools/ps_sweep.py
Diff: +78 / -4 across 2 file(s).

The change stays inside the stated scope and the touched files line up with the description. No unrelated production paths are pulled in, and the stated behavior is directly exercised by the diff. I'm approving this one.

Reviewed by shayderrr via automated batch review.

@shayderrr

Copy link
Copy Markdown
Collaborator

Thanks @tahffo68hk-hash - I went through ps_sweep: preserve sample identity when reusing an output directory and the diff around tests/test_ps_sweep.py. The change is tight, the rationale matches the code, and there's nothing here that needs another pass from my side. Approved and squash-merging it into main now. Nice work.

@tahffo68hk-hash

Copy link
Copy Markdown
Author

Thanks for the review. GitHub still shows this PR as open, and both Actions runs show that they are awaiting maintainer approval without starting jobs. Could you confirm whether workflow approval or another merge step is still needed? AI-assisted.

感谢审查。GitHub 仍显示此 PR 开放,两个 Actions 工作流都在等待维护者批准,尚未启动任务。方便确认一下是否还需要批准工作流,或是否还有其他合并步骤吗?AI 辅助。

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.

5 participants