Repository navigation
[perf, refactor] Optimize controller metadata processing - #246
Conversation
Signed-off-by: ji-huazhong <hzji210@gmail.com>
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
| if not self.global_indexes or not self.per_sample_shapes: | ||
| return |
There was a problem hiding this comment.
Suggestion: returning early here when not self.global_indexes leaves FieldMeta in an inconsistent state (per_sample_shapes, dtype, shape, is_nested, and is_non_tensor still populated), relying on an implicit contract that callers immediately discard the object.
Notice that DataPartitionStatus.clear_data() currently still loops over all fields and calls field_meta.remove_samples(indexes_to_release) even when len(self.global_indexes) == 0, right before throwing the whole dict away with self.field_metadata.clear(). If we instead:
- Short-circuit
if not self.global_indexes: self.field_metadata.clear()before the loop inclear_data(), full-partition clears skip the per-fielddifference_update()work altogether (faster than the current path). - Reset the attributes and call
self.per_sample_shapes.clear()whennot self.global_indexeshere inremove_samples()(a single C-level clear, no per-indexpoploop), keepingFieldMetaself-consistent even when a single field becomes empty or whenFieldMetais used directly.
Signed-off-by: ji-huazhong <hzji210@gmail.com>
Problem
The Python Controller does redundant work when maintaining and returning metadata. FieldMeta has separate shape-promotion paths, and whole-partition clearing repeats per-field work before discarding all field metadata. Marking a sampled batch consumed queries and sorts the whole partition again. Ordinary insertion and KV key creation repeat index-allocation bookkeeping. BatchMeta construction builds temporary index-keyed dictionaries before converting them into request-ordered lists.
Changes
Public interfaces, wire formats, sampling and readiness semantics remain unchanged.
Validation
Extend the existing real Controller metadata test with the request order
[2, 0, 2, 1], a field subset, missing tags and missing backend metadata. Extend two existing partition tests to verify that full clearing removes row metadata and key mappings while honoringclear_consumption=False, and that clearing regular, nested and non-tensor fields resets their metadata before a subsequent write with a different dtype and shape. The empty-state assertion fails on the preceding PR commit. No new test function or mock-based test is added.python -m compileall -q transfer_queue tutorial tests: passed.datasystemSDK. Their names and messages match the pre-change baseline exactly.Performance
Local CPU measurements use one PyTorch thread, five warmups and nine alternating blocks. Results are medians of block-average times. Cleanup compares the PR base with the final implementation. Consumption and packing each isolate their respective change against its preceding implementation; the consumption comparison predates the packing change. These measurements exclude RPC, payload transfer and model training.
For a partition with 4,096 rows and 16 fields,
clear_datatakes:Each block contains 10 clears; setup copies are outside the timed region, and metadata cleanup/object release are included.
Fetching 4,096 rows in 64 batches, with eight fields and the sequential sampler, takes 24.122 -> 16.312 ms per complete sequence (32.4% lower; 10 sequences per block). The sequence performs 0 rather than 64 full consumption-status queries from
mark_consumed.For
generate_batch_metawith 4,096 rows, selecting eight of 16 fields (30 calls per block):Memory is measured separately with
tracemalloc, taking medians of five alternating repeats. Extra peak means newly traced peak allocation minus the returned BatchMeta's newly retained allocation, measured with collection before and after releasing it. Inputs are prepared before tracing; payloads and native Torch/GPU allocations are excluded. Returned-object allocation is equal in these 4,096-row comparisons. For 64-row batches, the first three cases save only about 3.7-7.8 KiB of extra peak; the sparse case uses about 1 KiB more.