Skip to content

[perf] Optimize controller status queries - #245

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

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

Conversation

@ji-huazhong

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

Copy link
Copy Markdown
Collaborator

Problem

Python Controller status queries do unnecessary work before returning their results. Consumption queries call Python max() on an already sorted index tensor, comparing one scalar tensor at a time. Masked production queries first copy every allocated row for the requested columns and then select the partition's rows, which is expensive for small partitions with high global indexes. KV readiness queries also convert scalar tensors to Python booleans separately for each field.

Changes

  • Use the final sorted index for the consumption query's capacity check.
  • Select the requested production rows and columns together, preserving column registration order, duplicate-field elimination, and preallocated rows.
  • Convert readiness masks to Python lists once per mask. For kv_exists, convert each integer status row directly and check == 1 in Python, avoiding both per-field scalar Tensor conversions and a temporary boolean Tensor per key.

Public APIs, return ordering, readiness semantics, and result isolation are preserved. This adds no cache or invalidation state.

Validation

Strengthen the existing production-status test with different per-row and per-field states, reordered and repeated fields, sparse indexes, reserved rows, and result mutation. Add two assertions to the existing finalize end-to-end test to verify that kv_exists hides unfinalized fields and exposes them after finalization. No test functions or mocks are added.

  • python -m compileall -q transfer_queue tutorial tests: passed.
  • Formatting, type checking, license, and docstring checks: passed.
  • The final production-status test: passed.
  • Existing KV visibility and existence tests (synchronous and asynchronous finalization, missing and linked keys, and clearing): 4 passed in the full suite.
  • Full local suite: 1004 passed, 16 skipped, 1 failed, 6 setup errors. The failing test names and error messages match the previously recorded baseline: test_opt_out_keeps_the_process_alive_for_inspection fails in the full-suite order, and six Yuanrong fixtures lack the optional datasystem SDK. All tests affected by this change pass.

Performance

Compared with main at eb39cc4, using real local ZMQ requests, one PyTorch CPU thread, five warmup calls, and nine alternating blocks of 20 calls per implementation. Values are medians of the block-average latencies.

Query Before After Latency change
Consumption status, 4,096 rows 5.386 ms 1.168 ms -78.3%
Production status, 4,096 rows, 8 fields 1.465 ms 1.461 ms -0.3%
Production status, 64 rows at high global indexes, 8 fields 2.110 ms 0.381 ms -82.0%
kv_exists, 256 keys, 16 fields 12.148 ms 1.657 ms -86.4%
KV metadata, 32 keys, 16 fields 0.372 ms 0.355 ms -4.5%
KV metadata, 4,096 keys, 16 fields 3.840 ms 3.815 ms -0.7%

Direct kv_exists calls for the same 256 keys and 16 fields take 10.454 ms on main and 0.497 ms with this change (95.2% lower latency), measured with nine alternating blocks of 50 calls. This excludes RPC overhead; the table above includes it.

For the sparse production query, the status tensor has 139,266 allocated rows. CPU profiling shows the old indexing path allocates a 1,114,128-byte intermediate result and a 512-byte selected result; the new path allocates only the 512-byte selected result.

The dense production query shows no speedup. These measurements establish improvements in the listed query workloads, not a training-throughput gain.

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-30T06:12:31.623408Z b520bd3 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 1939 to 1942
row = (partition.production_status[global_index] == 1).tolist()
ready[key] = sorted(
name for name, col in partition.field_name_mapping.items() if col < len(row) and row[col] == 1
name for name, col in partition.field_name_mapping.items() if col < len(row) and row[col]
)

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.

Nit: we can convert the row directly with .tolist() and keep the existing row[col] == 1 check:

Suggested change
row = (partition.production_status[global_index] == 1).tolist()
ready[key] = sorted(
name for name, col in partition.field_name_mapping.items() if col < len(row) and row[col] == 1
name for name, col in partition.field_name_mapping.items() if col < len(row) and row[col]
)
row = partition.production_status[global_index].tolist()
ready[key] = sorted(
name for name, col in partition.field_name_mapping.items() if col < len(row) and row[col] == 1
)

This avoids allocating a temporary bool tensor per key for == 1 (roughly 2x faster in the Python loop for 256+ keys) while keeping the diff smaller and preserving the exact == 1 readiness check.

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