Skip to content

[perf, refactor] Optimize controller metadata processing - #246

Merged
ji-huazhong merged 2 commits into
Ascend:mainfrom
ji-huazhong:perf/optimize-controller-metadata
Nov 30, 2026
Merged

ji-huazhong merged 2 commits into
Ascend:mainfrom
ji-huazhong:perf/optimize-controller-metadata

Conversation

@ji-huazhong

@ji-huazhong ji-huazhong commented Nov 30, 2026 •

Copy link
Copy Markdown
Collaborator

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

  • Merge FieldMeta shape updates through one path. Keep ordinary tensors in their shared-shape representation until per-sample shapes are needed. Skip per-field cleanup when the whole partition becomes empty. For partial or field-only clearing, reset a FieldMeta when its last sample is removed; otherwise stop uniformity checks at the first different shape. Tags, backend metadata, links and key mappings are still cleaned up after the field metadata.
  • Share lazy consumption-tensor initialization across queries, scanning and marking. Mark selected rows directly without rebuilding the partition's index tensor.
  • Use one controller allocation routine for ordinary insertion and KV key creation: activate reserved indexes, allocate any remainder, register active rows and ensure status capacity. Keep key deduplication and key mappings in the KV path.
  • Build tags and filtered backend metadata directly in request order. Let BatchMeta initialize empty rows when the partition has no corresponding metadata. Preserve repeated rows, missing entries, field filtering and existing copy semantics.

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 honoring clear_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.

  • Partition metadata tests: 55 passed.
  • python -m compileall -q transfer_queue tutorial tests: passed.
  • Pre-commit formatting, lint, type, license and docstring checks: passed.
  • Full local suite: 1004 passed, 16 skipped, 1 failed and 6 setup errors in 647.50 s. The failure is the previously observed worker-exit opt-out assertion; the six errors are Yuanrong fixture setup errors without the optional datasystem SDK. 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_data takes:

Operation Before After
Clear all rows, regular fields 5.428 ms 1.012 ms
Clear all rows, nested fields 5.818 ms 1.254 ms
Clear one row while shapes remain varied 3.751 ms 0.089 ms
Clear the last row with a different shape 3.508 ms 2.160 ms

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_meta with 4,096 rows, selecting eight of 16 fields (30 calls per block):

Metadata Time before Time after Extra traced peak before / after
None 0.969 ms 0.378 ms 616.5 / 41.4 KiB
Tags only 1.215 ms 0.680 ms 504.5 / 73.6 KiB
Tags and backend metadata for every row 8.744 ms 8.270 ms 1832.8 / 1545.8 KiB
Tags on every 32nd row, backend metadata on every 64th row 1.030 ms 0.840 ms 633.9 / 628.3 KiB

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.

Signed-off-by: ji-huazhong <hzji210@gmail.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-30T08:44:08.589023Z 5a5f67b PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

Comment thread transfer_queue/controller.py Outdated
Comment on lines +290 to +291
if not self.global_indexes or not self.per_sample_shapes:
return

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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:

  1. Short-circuit if not self.global_indexes: self.field_metadata.clear() before the loop in clear_data(), full-partition clears skip the per-field difference_update() work altogether (faster than the current path).
  2. Reset the attributes and call self.per_sample_shapes.clear() when not self.global_indexes here in remove_samples() (a single C-level clear, no per-index pop loop), keeping FieldMeta self-consistent even when a single field becomes empty or when FieldMeta is used directly.

Signed-off-by: ji-huazhong <hzji210@gmail.com>
@ji-huazhong
ji-huazhong merged commit ffde754 into Ascend:main Nov 30, 2026
16 checks passed
@ji-huazhong
ji-huazhong deleted the perf/optimize-controller-metadata branch November 30, 2026 10:05
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.

2 participants