Skip to content

prepare: publish a validated split without replacing existing shards - #708

Merged
juliankang4 merged 2 commits into
midagedev:mainfrom
juliankang4:fix-prepare-atomic
Oct 23, 2026
Merged

juliankang4 merged 2 commits into
midagedev:mainfrom
juliankang4:fix-prepare-atomic

Conversation

@juliankang4

Copy link
Copy Markdown
Collaborator

prepare가 분할 파일을 만들다 실패하거나 두 번 동시에 돌아도 기존 결과를 망가뜨리지 않도록, 완성하고 검증한 뒤에만 공개하게 고칩니다.

Part of #565. Tracked in #699.

What and why

prepare writes a converted checkpoint as a split of GGUF shards, and two things could go wrong with it:

  • A later shard that fails to write left the output prefix holding a mix of new and old shards. The first shard of an earlier run was already overwritten by then.
  • Two prepare runs on the same output prefix shared one temporary .part name per shard. A run could report success and the split on disk could still change afterwards, because the other run was writing the same file.

Both come from #540, which added the shard writer. The split is now written by one private function, write_split. It takes an OS lock on a <prefix>.prepare.lock file, which stays beside the output, writes every shard into a private temporary directory, checks the whole split with the reader the server uses (gguf::Split::open and model::arch::spec), and only then moves the shards into place with the existing rename_noreplace. The last shards go first and the first shard, which readers open, goes last. On an error it removes only its own temporary directory and the shards it already published.

An output shard that already exists, whether a file, a directory or a dangling symlink, is now an error instead of being overwritten. A split cannot be swapped in one step, so keeping the old result and asking for a new prefix is the safe choice. This changes behavior for anyone who reran prepare over the same prefix, and the module documentation says so. Tensor conversion, GGUF layout and output names are unchanged. There is no new public item, option or dependency, and no GPU path is touched. The change does not add fsync or crash durability.

Release notes: Fixed: prepare no longer overwrites existing shards, and a failed or concurrent run no longer leaves a mixed split.

Code shape

  • No second copy. write_split is the one owner of writing, validating and publishing a split. It reuses Writer, gguf::Split, model::arch::spec and model::fileio::rename_noreplace.
  • Model code holds only architecture facts. No model-specific branch was added.
  • Dead code is gone. The old inline loop and the fixed .part naming are removed.
  • No silent failure. An existing output, a busy lock, and write, validation, publish, rollback, cleanup and unlock errors are all returned.
  • Comments state what is true now. The module documentation describes the current output contract.
  • No relaxed gate. No band, tolerance or lint moved.
  • cargo fmt and cargo clippy are clean on what I touched; the lint count did not go up. rustfmt ran clean on the pinned nightly; cargo clippy has not run on this head.

Duplication found or removed (path:line): none. split_dir in crates/model-hf/src/bin/prepare.rs:373 is not shared with crates/model-hf/src/lib.rs:16, because that library's test support is cfg(test) and pub(crate), so the separately compiled prepare binary cannot reach it.

What I ran

The tests ran on a Linux machine with RUSTFLAGS='-C target-cpu=native' and CARGO_BUILD_JOBS=4. The change is in one host-side file and no CUDA path is involved, so no GPU gates ran.

  • FAIL-first, on a tree with the original writer and only the new tests: cargo test -p bloomery-model-hf --bin prepare split_ -- --nocapture exits 101 with test result: FAILED. 0 passed; 2 failed; 0 ignored; 0 measured; 5 filtered out; finished in 0.00s. Both new tests fail on an assertion, not at compile time. split_failure_preserves_existing_shards shows the first existing shard's bytes changed after a later shard failed. split_concurrent_writers_cannot_change_a_successful_output shows a successful writer's output changed afterwards.
  • With the fix: cargo test -p bloomery-model-hf --bin prepare -- --nocapture exits 0 with test result: ok. 8 passed; 0 failed; 1 ignored; 0 measured; 0 filtered out; finished in 1.18s. Four of the eight are new regression tests. They cover shard counts of 1, 2 and 4 against the bytes an independent Writer produces, the first shard absent until the whole split validates, and cleanup that touches only the run's own files after a write, validation or publish failure.
  • The ignored test needs a real Qwen3.8 tokenizer folder and was already ignored. The split change is checked with synthetic GGUF fixtures. I did not run a full model conversion.
  • git diff upstream/main...HEAD --check exits 0.

Not run: cargo clippy, the contribution checks and the GPU gates.

Reproduction

cargo test -p bloomery-model-hf --bin prepare split_ -- --nocapture
cargo test -p bloomery-model-hf --bin prepare -- --nocapture

To see the failure first, run the first command with crates/model-hf/src/bin/prepare.rs restored to its upstream version and only the test module and its fixtures kept.

What I ran into

This does not block the PR. The F32 scale packing in prepare still builds a temporary Vec per tensor. I did not measure it and left it alone.

AI help

AI 코딩 에이전트가 코드를 작성한 후 설명을 썼고, 테스트를 마쳤습니다. diff 리뷰와 PR 초안 리뷰는 제가 직접 했습니다.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Verdict: CLEAN: The changed writer preserves existing shards and validates the full split before publication.

Non-blocking

⚠ Cover publication order: crates/model-hf/src/bin/prepare.rs:475. The assertions observe the output before publication and after completion. Replacing paths.iter().rev() at line 258 with paths.iter() leaves them green, while readers can see the first shard before its siblings. Extend this test with a deterministic observation that the first shard is published last.

⚠ Explain the debuggability layer: crates/model-hf/src/bin/prepare.rs:261. The PR supplies the shared fix and regression tests, but does not explain its debugging route. Document how the existing path-qualified publication and cleanup errors diagnose failures, or identify the existing probe that does so.

Checked

Duplicates: 7 new functions checked; no reusable duplicate found. Dryer reported 0 pairs.
Complexity: crapper flagged new write_split at 28. Its stages are staging/validation, publication/rollback, and cleanup; no existing decision tree gained branches.
Silent failure: 5 sites followed: conversion iterator errors, metadata errors, rollback, temporary-directory cleanup, and unlock. Failures reach the caller and exit status 1.
Tests, mutation by inspection: returning success for an existing output (243 → 414) and omitting rollback tracking (262 → 539) fail assertions. Bypassing busy rejection (224 → 422) and publishing forwards (258 → 459) survive.
Gates: no pins, bands, thresholds, or existing tests changed.
Escape catalog: C1–C4 do not match.
Adversarial checks: overlapping writers, a late publication conflict, and a pre-existing dangling symlink are blocked by locking, atomic no-replace rename, and symlink metadata checks.
Hot path: none; this is checkpoint conversion.

Notes

Tests were not rerun during this static review. The PR reports eight passing host tests and one ignored test.

🔨 Shingler, bloomery's review bot: approves a clean pull request and requests changes on a must-fix (how it works)

@juliankang4

Copy link
Copy Markdown
Collaborator Author

The review is done and the contribution check passed, so I'm merging this.

@juliankang4
juliankang4 merged commit 489ae66 into midagedev:main Oct 23, 2026
7 checks passed
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.

1 participant