-
Notifications
You must be signed in to change notification settings - Fork 110
Use shared ts.Transaction for metadata consolidation #4105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| # Copyright The Marin Authors | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| # Copyright The Levanter Authors | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| """Tests for consolidated metadata copy using a shared ts.Transaction (#4100).""" | ||
|
|
||
| import asyncio | ||
| import copy | ||
| import operator | ||
| import os | ||
| import tempfile | ||
|
|
||
| import jax | ||
| import numpy as np | ||
|
|
||
| from levanter.store.cache import CacheLedger, _consolidate_metadata, _expose_cache_rows, _extend_cache_with_other_cache | ||
| from levanter.store.tree_store import TreeStore | ||
|
|
||
| NUM_SHARDS = 8 | ||
| ROWS_PER_SHARD = 32 | ||
| ROW_WIDTH = 16 | ||
|
|
||
| # rank-1 only (no shapes metadata) | ||
| EXEMPLAR_FLAT = {"input_ids": np.array([0], dtype=np.int32)} | ||
|
|
||
| # multi-field with a rank-2 leaf (triggers shapes metadata) | ||
| EXEMPLAR_SHAPED = { | ||
| "input_ids": np.array([0], dtype=np.int32), | ||
| "spans": np.zeros((0, 2), dtype=np.int32), | ||
| } | ||
|
|
||
|
|
||
| def _build_and_consolidate(exemplar, make_row) -> TreeStore: | ||
| """Build shards, copy data + metadata, return the merged store.""" | ||
| with tempfile.TemporaryDirectory(prefix="levanter-test-consolidate-") as tmpdir: | ||
| shard_root = os.path.join(tmpdir, "shards") | ||
| os.makedirs(shard_root) | ||
|
|
||
| data_offset_tree = jax.tree.map(lambda _: 0, exemplar) | ||
| total_rows = 0 | ||
| shard_infos = [] | ||
|
|
||
| for i in range(NUM_SHARDS): | ||
| shard_path = os.path.join(shard_root, f"shard_{i}") | ||
| store = TreeStore.open(exemplar, shard_path, mode="w", cache_metadata=True) | ||
| store.extend([make_row(i) for _ in range(ROWS_PER_SHARD)]) | ||
|
|
||
| shard_infos.append( | ||
| { | ||
| "path": shard_path, | ||
| "row_offset": total_rows, | ||
| "data_offset_tree": copy.deepcopy(data_offset_tree), | ||
| "ledger": CacheLedger(total_num_rows=ROWS_PER_SHARD, shard_rows={}, is_finished=True), | ||
| } | ||
| ) | ||
| total_rows += ROWS_PER_SHARD | ||
|
|
||
| this_offsets = jax.tree.map(lambda x: x.data_size, store.tree) | ||
| data_offset_tree = jax.tree.map(operator.add, data_offset_tree, this_offsets) | ||
|
|
||
| dest_path = os.path.join(tmpdir, "dest") | ||
| TreeStore.open(exemplar, dest_path, mode="w", cache_metadata=True) | ||
|
|
||
| for info in shard_infos: | ||
| asyncio.run( | ||
| _extend_cache_with_other_cache( | ||
| dest_path, | ||
| info["path"], | ||
| exemplar, | ||
| info["data_offset_tree"], | ||
| info["row_offset"], | ||
| ) | ||
| ) | ||
| asyncio.run(_consolidate_metadata(dest_path, exemplar, shard_infos)) | ||
| _expose_cache_rows(dest_path, exemplar, total_rows) | ||
|
|
||
| merged = TreeStore.open(exemplar, dest_path, mode="r", cache_metadata=True) | ||
| assert len(merged) == NUM_SHARDS * ROWS_PER_SHARD | ||
|
|
||
| for i, info in enumerate(shard_infos): | ||
| row = merged[info["row_offset"]] | ||
| assert row["input_ids"][0] == i, f"shard {i} data mismatch" | ||
|
|
||
| return merged | ||
|
|
||
|
|
||
| def test_consolidate_metadata_flat(): | ||
| """Round-trip with a single rank-1 field (no shapes metadata).""" | ||
|
|
||
| def make_row(shard_index): | ||
| return {"input_ids": np.full((ROW_WIDTH,), shard_index, dtype=np.int32)} | ||
|
|
||
| _build_and_consolidate(EXEMPLAR_FLAT, make_row) | ||
|
|
||
|
|
||
| def test_consolidate_metadata_shaped(): | ||
| """Round-trip with multiple fields including rank-2 (exercises shapes metadata).""" | ||
|
|
||
| def make_row(shard_index): | ||
| return { | ||
| "input_ids": np.full((ROW_WIDTH,), shard_index, dtype=np.int32), | ||
| "spans": np.full((3, 2), shard_index, dtype=np.int32), | ||
| } | ||
|
|
||
| merged = _build_and_consolidate(EXEMPLAR_SHAPED, make_row) | ||
| row = merged[0] | ||
| assert row["spans"].shape == (3, 2) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_build_and_consolidatereturns a liveTreeStorehandle from inside awith tempfile.TemporaryDirectory(...)block, so the backing directory is deleted before callers use it. Intest_consolidate_metadata_shaped,merged[0]is read after return; this can fail or become flaky when TensorStore needs to fetch uncached data from disk. This makes the new test unreliable across environments and cache behavior.Useful? React with 👍 / 👎.