Skip to content

Publish the checkpoint cache after the shard merge completes - #236

Open
rpinho wants to merge 1 commit into
ArcInstitute:mainfrom
rpinho:fix-checkpoint-cache
Open

rpinho wants to merge 1 commit into
ArcInstitute:mainfrom
rpinho:fix-checkpoint-cache

Conversation

@rpinho

@rpinho rpinho commented Nov 16, 2026

Copy link
Copy Markdown

There's a bug in Evo2.load_evo2_model: it merges shards into the cache file that the next load accepts based on its existence.

If opening the second shard fails after writing the first shard's 12 bytes, retrying accepts those 12 bytes as the checkpoint instead of merging both shards into 25 bytes.

This merges into a temporary directory on the destination filesystem, closes the file, then publishes it with os.replace.

Test: test_failed_merge_is_not_reused_on_retry in tests/test_cpu.py fails on main and passes here. Both CPU checkpoint tests pass on Python 3.11 and 3.12; syntax checks and git diff --check pass. Model loading and downloads are mocked.

🤖 Generated with Claude Code

https://claude.ai/code/session_01LKxc9GKLwnyeV2jaZBxH9N

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request introduces atomic merging of model shards in evo2/models.py by writing to a temporary directory before replacing the final weights file, and adds CPU-only unit tests in tests/test_cpu.py to verify this behavior. The feedback suggests ensuring final_weights_path is converted to an absolute path to guarantee the temporary directory is created on the same filesystem for an atomic os.replace. Additionally, it is recommended to clear cached evo2 modules from sys.modules in the test setup to prevent cached imports from bypassing the mocked dependencies.

Comment thread evo2/models.py
Comment on lines +270 to +271
with tempfile.TemporaryDirectory(dir=os.path.dirname(final_weights_path)) as temp_dir:
temp_weights_path = os.path.join(temp_dir, filename)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

If final_weights_path is a relative path (or if os.path.dirname(final_weights_path) resolves to an empty string), tempfile.TemporaryDirectory(dir="") will fallback to the system's default temporary directory (e.g., /tmp). This can place the temporary file on a different filesystem/mount point than the final destination, which prevents os.replace from being an atomic operation and can cause performance issues or failures.

Using os.path.abspath ensures we always get a valid absolute directory path on the same filesystem.

                    final_weights_dir = os.path.dirname(os.path.abspath(final_weights_path))
                    with tempfile.TemporaryDirectory(dir=final_weights_dir) as temp_dir:
                        temp_weights_path = os.path.join(temp_dir, filename)

Comment thread tests/test_cpu.py
Comment on lines +37 to +38
with mock.patch.dict(sys.modules, {**modules, 'transformer_engine': None}):
loaded = importlib.import_module('evo2.models')

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

If evo2 or evo2.models was already imported by another test in the suite, importlib.import_module will return the cached module from sys.modules instead of performing a fresh import under the mock.patch.dict context. This would bypass the mocked vortex modules and potentially fail or use the real vortex library.

To ensure the test is robust and independent of other tests, we should remove any existing evo2 modules from sys.modules before importing.

Suggested change
with mock.patch.dict(sys.modules, {**modules, 'transformer_engine': None}):
loaded = importlib.import_module('evo2.models')
for k in list(sys.modules.keys()):
if k == 'evo2' or k.startswith('evo2.'):
sys.modules.pop(k, None)
with mock.patch.dict(sys.modules, {**modules, 'transformer_engine': None}):
loaded = importlib.import_module('evo2.models')

@rpinho

rpinho commented Nov 24, 2026

Copy link
Copy Markdown
Author

Let me know if you have any thoughts on this one.

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