-
Notifications
You must be signed in to change notification settings - Fork 405
Add _TensorViewCodec for storage-deduplicated tensor serialization #1413
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
fzyzcjy
merged 31 commits into
main
from
tom/pr_chain/trainer_ft/dev_revert_reversed/add-tensorviewcodec-for-storage-deduplicated-tensor-serialization
Jul 10, 2026
Merged
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
96eecb4
Add a deterministic_random reward type
fzyzcjy 2c1d4a7
Add an inplace_modify_args context manager
fzyzcjy 7b00c75
Add fault-tolerance support tweaks to shared utilities
fzyzcjy 635aa50
Preserve the process-group backend across reload
fzyzcjy 7a33a63
Add fault-tolerance foundation utilities
fzyzcjy 8dc8ddd
Add structured logfmt logging helper
fzyzcjy 96d220f
Add a Clock abstraction with a fake clock for tests
fzyzcjy a005a2f
Add a fault injector test utility
fzyzcjy 8365971
Add control-server data models
fzyzcjy e308821
Add a cell health checker and heartbeat utilities
fzyzcjy 4a52fcf
Add the fault-tolerance dependency, CI label, and logger-config setup
fzyzcjy 2ed9fcf
Always reconnect rollout engines on weight-update setup
fzyzcjy a838a0b
Add a fault-injection RPC to train actors
fzyzcjy c7798b5
Delay splitting train data by DP until actor-side processing
fzyzcjy d0b41fc
Add a deterministic NCCL backend for order-stable collectives
fzyzcjy a48d03f
Add a per-process identity helper
fzyzcjy 3e85a86
Add structured event models keyed by per-process identity
fzyzcjy dc11be3
Add structured event logging keyed by per-process identity
fzyzcjy 691e4a7
Add event-log snapshot and restore checkpointing
fzyzcjy 5a91150
Log training metrics as MetricEvents through the event logger
fzyzcjy 34fb714
Add the witness id allocator
fzyzcjy fd3bc9c
Trace witness ids through the model via injected witness parameters
fzyzcjy 85f120b
Add event-log checksum-consistency analysis rules
fzyzcjy 5b5c797
Add an event-log witness-tracing analysis rule
fzyzcjy 493ef68
Add the event-log analyzer that applies analysis rules
fzyzcjy 81b95c4
Add dump and inference-engine-checksum comparison helpers for FT tests
fzyzcjy 71d38e2
Add metric comparison helpers for FT tests
fzyzcjy c043ccf
Add reconfiguration assertions for fault-tolerance tests
fzyzcjy 932660e
Relocate GroupInfo into shared process-group utilities
fzyzcjy 69c50b9
Add _TensorViewCodec for storage-deduplicated tensor serialization
fzyzcjy 956b4b1
Merge remote-tracking branch 'origin/main' into tom/pr_chain/trainer_…
fzyzcjy 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import torch | ||
|
|
||
|
|
||
| class _TensorViewCodec: | ||
| """Encode tensors as (unique_storages, view_metas) and decode back. | ||
|
|
||
| Many input tensors may share underlying storage (e.g. Megatron | ||
| distributed-optimizer grad buckets). `encode` dedups by storage data_ptr — | ||
| each unique storage is wrapped once as a uint8 tensor (no copy), plus a | ||
| per-input view_meta record (storage_id, dtype, shape, stride, | ||
| storage_offset). `decode` reconstructs the original views with | ||
| `as_strided` over the storage bytes reinterpreted at the original dtype. | ||
| """ | ||
|
|
||
| @staticmethod | ||
| def encode(tensors: list[torch.Tensor]) -> tuple[list[torch.Tensor], list[dict]]: | ||
| storage_id_by_key: dict[tuple[torch.device, int], int] = {} | ||
| unique_storages: list[torch.Tensor] = [] | ||
| view_metas: list[dict] = [] | ||
| for t in tensors: | ||
| storage = t.untyped_storage() | ||
| key = (t.device, storage.data_ptr()) | ||
| if key not in storage_id_by_key: | ||
| storage_id_by_key[key] = len(unique_storages) | ||
| # Wrap full storage as uint8 tensor (no copy, shares memory). | ||
| unique_storages.append(torch.tensor(storage, dtype=torch.uint8, device=t.device)) | ||
| view_metas.append( | ||
| { | ||
| "storage_id": storage_id_by_key[key], | ||
| "dtype": t.dtype, | ||
| "shape": tuple(t.shape), | ||
| "stride": tuple(t.stride()), | ||
| "storage_offset": t.storage_offset(), | ||
| } | ||
| ) | ||
| return unique_storages, view_metas | ||
|
|
||
| @staticmethod | ||
| def decode(unique_storages: list[torch.Tensor], view_metas: list[dict]) -> list[torch.Tensor]: | ||
| tensors: list[torch.Tensor] = [] | ||
| for vm in view_metas: | ||
| storage_t = unique_storages[vm["storage_id"]] # uint8 view of received storage | ||
| # Reinterpret bytes as the original dtype, then apply stride/offset. | ||
| dtype_view = storage_t.view(vm["dtype"]) | ||
| view = torch.as_strided( | ||
| dtype_view, | ||
| size=vm["shape"], | ||
| stride=vm["stride"], | ||
| storage_offset=vm["storage_offset"], | ||
| ) | ||
| tensors.append(view) | ||
| return tensors | ||
Oops, something went wrong.
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.
If the untyped storage size is not a multiple of the element size of
vm["dtype"](which can happen with mixed-dtype storages or padded storages), callingstorage_t.view(vm["dtype"])will raise aRuntimeError. To prevent this, slicestorage_tto a multiple of the element size before reinterpreting its dtype.