Skip to content

[TENT] Add a blocking waitTransfer entry point - #5415

Merged
alogfans merged 4 commits into
kvcache-ai:mainfrom
CAICAIIs:tent-sync-transfer
Nov 13, 2026
Merged

alogfans merged 4 commits into
kvcache-ai:mainfrom
CAICAIIs:tent-sync-transfer

Conversation

@CAICAIIs

@CAICAIIs CAICAIIs commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Callers that need to block on a batch currently replay TENT's poll loop themselves: backoff, cancellation and per-task status mapping all live outside the engine. This adds one blocking entry point on an already submitted batch:

  • waitTransfer(batch_id, status_list = nullptr, timeout_ms = 0) waits until every task in the batch terminates.
  • With status_list it receives one TransferStatus per request, in request order; a failed task is reported there instead of failing the call.
  • Without status_list the call fails if any task did not complete. The stage-buffer delegate path uses that mode.
  • timeout_ms bounds the wait (0 waits forever). On timeout the batch's queued tasks are canceled before any further polling, and the dispatch window is not refilled per cancellation, so the cleanup polls cannot pull more requests of the timed-out batch out of the queue. Tasks already in flight are then asked to stop, and the call returns one status per request; an entry that is still PENDING means the engine may still touch that request's memory.
  • A timeout without status_list returns TimedOut, because only the batch query can tell whether the engine still touches the buffers. Timeouts beyond a year saturate to an unbounded wait instead of wrapping the deadline.
  • Python: tent.TransferEngine.wait_transfer(batch_id, timeout_ms=0) returns the status list and releases the GIL while waiting. Submission stays asynchronous through allocateTransferBatch and submitTransfer.
  • The existing internal submit-and-wait helper is renamed submitAndWaitTransfer, so the two blocking paths stay distinguishable at the call site.

Testing

  • Built tentpy on Linux x86_64 (gcc 11.4, RDMA and CUDA enabled).
  • python mooncake-transfer-engine/tent/tests/test_python_wait_transfer.py -v — one test passes: a write round trip that submits a batch, waits on it and checks the returned status list, bytes and payload. The test is registered with ctest next to tent_python_guards_test.
  • tent_runtime_queue_dispatch_test covers the timeout path's dispatch ordering: with a one-owner dispatch window, the first request completes during the cleanup poll, the second stays CANCELED, and the batch issues no further transport submission. It also covers the status_list contract (a FAILED task is reported in the list while the call returns OK, and without status_list the same batch fails the call), a timeout without status_list (TimedOut), and a near-uint64 timeout that must not wrap the deadline.
  • Exercised through an RDMA-connected prefill/decode pair: GSM8K accuracy 0.4223, no failed transfers, and every submitted request reported a terminal state.

@github-actions github-actions Bot added run-ci Transfer Engine size/M 100-1000 changed lines (additions + deletions) labels Nov 11, 2026
@CAICAIIs
CAICAIIs force-pushed the tent-sync-transfer branch 5 times, most recently from 421633e to 592c93a Compare November 11, 2026 09:54
std::vector<TransferStatus>* status_list = nullptr,
uint64_t timeout_ms = 0);

Status transferSync(const std::vector<Request>& request_list,

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.

Recommend to use a better name e.g. submitAndWaitTransfer

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

thanks, done!

@CAICAIIs
CAICAIIs force-pushed the tent-sync-transfer branch 2 times, most recently from 6b8704b to fe82305 Compare November 11, 2026 13:54
CHECK_STATUS(getTransferStatus(batch_id, statuses));
for (size_t task_id = 0; task_id < statuses.size(); ++task_id)
if (statuses[task_id].s == PENDING)
(void)cancelTransfer(batch_id, task_id);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Could we stop dispatching this batch before starting timeout cleanup?

Cancelling a queued owner calls refillDispatchWindow(), which can submit another request from the same batch before this loop reaches it. The status collection before the loop and progressBatch() during settling can also refill the window.

Could we prevent new dispatches for this batch while letting requests already in flight drain? A regression test could assert that timeout cleanup submits no additional requests from this batch.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Right — cancelling a queued owner refills the window and could start another request of the same batch. waitTransfer now stops the batch's dispatches before it collects statuses:

  • new cancelQueuedOwners(batch) cancels the batch's queued owners and does not refill the window per owner;
  • the timeout path is cancelQueuedOwners -> getTransferStatus -> cancel the tasks still in flight -> one refillDispatchWindow(). By then the batch has no queued owners left, so the cleanup polls can only hand window slots to other batches;
  • CancelQueuedOwnersBeforePollingKeepsBatchIdle in tent_runtime_queue_dispatch_test pins this: one-owner dispatch window, the first request completes during the cleanup poll, the second stays CANCELED, and the batch issues no further transport submission.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Follow-up: the regression test now drives the public waitTransfer (1 ms timeout, the first post-deadline poll returns PENDING), so it checks that cleanup submits no second request; submit_calls stays 1.

@github-actions github-actions Bot added the documentation Improvements or additions to documentation label Nov 11, 2026
Add TransferEngine::waitTransfer(), which blocks until every task in a
submitted batch reaches a terminal state and reports one status per request.
On timeout it stops the batch from dispatching further requests: queued
owners are canceled without refilling the dispatch window, then the tasks
already in flight are canceled and the call waits briefly for the
cancellations to land. A PENDING status means the engine may still touch
that request's memory.

Bind it to Python as wait_transfer() and cover it with a test that submits a
batch, waits for it, and checks both transfer directions.

Rename the internal submit-and-wait helper to submitAndWaitTransfer so it is
not mistaken for the new blocking wait entry point.

Signed-off-by: CAICAIIs <3360776475@qq.com>
@CAICAIIs
CAICAIIs marked this pull request as ready for review November 11, 2026 17:56
Copilot AI balanced review requested due to automatic review settings November 11, 2026 17:56

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@staryxchen staryxchen left a comment

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.

Good job. The impl is good to me, but some parts could be further simplified I think.

self.assertEqual(write_status[0].bytes, len(payload))
self.assertEqual(ctypes.string_at(server_addr, len(payload)), payload)

ctypes.memset(client_addr + read_offset, 0, len(payload))

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.

The write round trip already shows the binding works: status list length, state, bytes, and GIL release. The READ half re-tests the TCP transport rather than any new wait_transfer path, so I'd trim it to keep the test lean.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks — trimmed to the write round trip; the READ half only re-tested the TCP transport.

Comment on lines +3967 to +3973
CHECK_STATUS(getTransferStatus(batch_id, statuses));
if (status_list) return Status::OK();
for (const auto& task_status : statuses)
if (task_status.s != COMPLETED)
return Status::InternalError(
"Transfer via stage buffer failed" LOC_MARK);
return Status::OK();

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.

When status_list is null, this re-reads every task status and loops over them, but the last progressBatch already gave us the aggregate: getBatchStatus only reports COMPLETED when every task succeeded. Could we keep the last overall_status and check overall_status.s == COMPLETED here instead? Then local_statuses can go, and the per-task fetch only happens when the caller asked for it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks — now uses the last progressBatch result and only fetches per-task statuses when status_list is set.

Comment on lines +3908 to +3910
if (request_list.empty())
return Status::InvalidArgument(
"submitAndWaitTransfer requires at least one request" LOC_MARK);

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.

This changes behavior: an empty list used to reach total_tasks == 0 and come back COMPLETED, and now it's an error. The only production caller, onDelegate, always passes one request, so the check never fires. I'd drop it, or move it to its own PR if we actually want to reject empty lists.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks — dropped the check; an empty list keeps the upstream path and comes back as completed.

}
for (const auto owner_id : owner_ids) {
auto queued_it = queued_owners_.find(owner_id);
if (queued_it == queued_owners_.end()) continue;

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.

owner_ids was just collected from queued_owners_ under the same progress_mutex_, and nothing in this loop erases entries other than the owner being canceled, so this find can't miss. The continue is dead code and can go (indexing via find is still fine).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks — collected the owners already filtered, so the second lookup is gone.

Comment on lines 3955 to 3965
if (canceled) {
const auto settle_deadline =
std::chrono::steady_clock::now() + kCancelSettleTimeout;
for (uint64_t poll_count = 0;
std::chrono::steady_clock::now() < settle_deadline; ++poll_count) {
TransferStatus overall_status;
CHECK_STATUS(progressBatch(batch_id, overall_status));
if (overall_status.s != PENDING) break;
waitBeforeNextPoll(poll_count);
}
waitBeforeNextPoll(poll_count);
}

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.

With this settle loop, a call can block for up to timeout_ms + 250ms, and the extra 250 ms isn't configurable. The contract already says a PENDING entry means the engine may still touch that request's memory, so a caller can keep polling on its own. Is there a concrete caller that needs this built in? If not, I'd return right after canceling: same semantics, simpler code, and the timeout means what it says.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks — removed the settle window; the call returns right after canceling and reporting, and PENDING keeps its documented meaning.

Comment on lines +3912 to +3914
if (!batch_id)
return Status::TooManyRequests(
"No free batch slot for a synchronous transfer" LOC_MARK);

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.

The clearer error is nice, but before this change submitTransfer already rejected a zero batch id, so this isn't needed for waitTransfer. Fine to keep if you want it, though it would sit better in a separate cleanup.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks — kept it; it only covers a failed allocateBatch, which submitTransfer cannot see.

engine.unregisterLocalMemory(buffer.data(), buffer.size()).ok());
}

TEST(RuntimeQueueDispatch, TimeoutDoesNotDispatchQueuedRequestsOfBatch) {

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.

Good targeted test. One gap: nothing covers the main status_list contract, where a failed task is reported in the list while the call itself returns OK, and the null-status_list mode fails instead. A small FakeTransport case returning FAILED for one request would cover both, and is worth more than the extra Python READ coverage.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks — added ReportsFailedTaskOnlyWhenStatusListIsRequested: a FAILED task is reported in the list with an OK call, and the call fails without status_list.

@staryxchen

Copy link
Copy Markdown
Collaborator

Minor: the title still says "Add a blocking transferSync entry point", but the new API is waitTransfer and transferSync is gone after the rename. Could you update it to something like "[TENT] Add a blocking waitTransfer entry point"? That keeps the squash message accurate.

@CAICAIIs CAICAIIs changed the title [TENT] Add a blocking transferSync entry point [TENT] Add a blocking waitTransfer entry point Nov 12, 2026
Cancel the batch's queued owners and ask its in-flight tasks to stop, then report the per-request statuses without a fixed settle window. Use the overall status from the last progressBatch() when status_list is null, restore the upstream path for an empty request list, and filter in-dispatch-window owners while collecting them in cancelQueuedOwners().

Trim the Python test to the write direction and add a dispatch test that a failed task is reported through status_list instead of failing the call.

Signed-off-by: CAICAIIs <3360776475@qq.com>
@CAICAIIs

Copy link
Copy Markdown
Contributor Author

Thanks — title updated to [TENT] Add a blocking waitTransfer entry point.

"No free batch slot for a synchronous transfer" LOC_MARK);
BatchGuard batch_guard(*this, batch_id);
CHECK_STATUS(submitTransfer(batch_id, request_list));
CHECK_STATUS(waitTransfer(batch_id));

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: before this change, the failure path did CHECK_STATUS(batch_guard.reset()), so a failed freeBatch was returned to the caller. Now when waitTransfer fails, the guard's destructor frees the batch and a freeBatch error only shows up as a WARNING log. Returning the transfer error is arguably more useful, so this may be intended. I'm just flagging the behavior change.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is intentional: the transfer outcome is what the caller acts on, so the guard logs a failed free instead of replacing it.

Comment on lines +3916 to +3917
const auto deadline = std::chrono::steady_clock::now() +
std::chrono::milliseconds(timeout_ms);

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.

std::chrono::milliseconds(timeout_ms) converts the uint64_t to a signed 64-bit count, and adding it to steady_clock::now() converts again to nanoseconds, so large values overflow. I checked locally: UINT64_MAX and 1 << 62 both give a deadline earlier than now. A caller that passes UINT64_MAX to mean "wait forever" (or wait_transfer(timeout_ms=2**64 - 1) from Python) times out after the first poll and gets the batch canceled. Could we clamp it, e.g. treat anything above some bound such as std::chrono::hours(24 * 365) like 0, or compute the deadline with saturation?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 36e522c: timeouts beyond a year now saturate to an unbounded wait, with a regression test. Thanks.

Comment on lines 3941 to 3943
if (!status_list)
return Status::InternalError(
"Transfer via stage buffer failed" LOC_MARK);

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.

With timeout_ms != 0 and no status_list, a timeout comes back as the same InternalError as a failed transfer. The caller can't tell the two apart, and can't tell whether the engine may still touch the buffers, since only a PENDING entry in status_list says that. The message "Transfer via stage buffer failed" also reads oddly now that this is a public API. No internal caller passes a timeout, so nothing hits this today. Could the timeout return its own status (e.g. a timeout code with a neutral message), or could the header say that callers using a timeout without status_list must call getTransferStatus before releasing memory?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 36e522c: a timeout without status_list now returns TimedOut, and the header tells callers to query the batch before reusing the buffers. Thanks.

waitTransfer() returned InternalError when a timeout expired without a
status_list, so callers could not tell a timeout from a transfer failure.
Return a dedicated TimedOut status instead and document that such callers
must query the batch before reusing the buffers.

Clamp timeouts beyond a year to an unbounded wait; converting the larger
unsigned value to the clock duration wrapped the deadline into the past and
canceled the batch on the first poll.

Signed-off-by: CAICAIIs <3360776475@qq.com>
The unknown-batch case only re-checked the generic InvalidArgument mapping,
which the C++ status contract already covers. Keep the round trip that
exercises the new binding end to end.

Signed-off-by: CAICAIIs <3360776475@qq.com>

@staryxchen staryxchen left a comment

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.

LGTM, thanks!

@alogfans
alogfans merged commit 0233e75 into kvcache-ai:main Nov 13, 2026
37 of 38 checks passed
jfeng18 added a commit to jfeng18/Mooncake that referenced this pull request Nov 14, 2026
* upstream/main: (95 commits)
  [Bugfix][TENT] Honor EFA protocol in classic shim (kvcache-ai#5280)
  [Doc] Add the AMD ATOM integration and homepage blog posts (kvcache-ai#5380)
  [Store-RS] Fence Store-RS eviction and delete by allocation identity (kvcache-ai#5243)
  fix: preserve integer map keys in structured non-tensor reads (kvcache-ai#4603)
  [Bugfix][TE][Store] Fix CXI transport so Mooncake Store works over Slingshot (kvcache-ai#5454)
  [Store] Coalesce removal KV events before publishing (kvcache-ai#5467)
  [CI/Build][Doc] Ship EFA in the default wheels only (kvcache-ai#5322)
  [CI/Build][Doc] Ship EFA in the default wheels only (kvcache-ai#5322)
  [Store] Shard LeaseEvictionIndex and parallelize memory eviction (kvcache-ai#5448)
  [TransferEngine] Open intra-node NVLink IPC mappings on the source buffer's device (kvcache-ai#5443)
  [Bugfix][TENT] Temporarily disable smart scheduling by default (kvcache-ai#5445)
  [TENT] Add a blocking waitTransfer entry point (kvcache-ai#5415)
  [EP] Add planned runtime scaling to ElasticBuffer (kvcache-ai#5376)
  [Store] Persist object upload and cleanup recovery records (kvcache-ai#5310)
  [TransferEngine] Add opt-in high-priority HIP copy streams (kvcache-ai#5005)
  [Store] feat: break master key count and value bytes down by configurable key groups (kvcache-ai#5393)
  [Store] Populate HugeTLB EFA segments in parallel (kvcache-ai#5457)
  [CI/Build] Prevent OOM in PR unit-tests build on GitHub-hosted runners (kvcache-ai#5420)
  [Store] Add automatic pipelined gather to ranged reads (kvcache-ai#4502)
  [Store] Add property tests for structured member byte-range plans (kvcache-ai#5328)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation run-ci size/M 100-1000 changed lines (additions + deletions) Transfer Engine

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants