Skip to content

feat(runtime): add copy_stacked_from D2H read-back for StackedDeviceTensor#1954

Merged
lyfne123 merged 1 commit into
hw-native-sys:mainfrom
YunjiQin:feat/copy-stacked-from
Jul 6, 2026
Merged

feat(runtime): add copy_stacked_from D2H read-back for StackedDeviceTensor#1954
lyfne123 merged 1 commit into
hw-native-sys:mainfrom
YunjiQin:feat/copy-stacked-from

Conversation

@YunjiQin

@YunjiQin YunjiQin commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds DistributedWorker.copy_stacked_from(stacked, host) — the read-back symmetric to alloc_stacked_tensor. It reads every resident shard of a StackedDeviceTensor back to the host in one call, filling host[i] from shard i's owning worker (D2H). This lets callers recover the current device contents of a resident stack (e.g. a KV cache at the end of an L3 step), which the per-dispatch path otherwise skips.

Like the upload path, the D2H copy runs inside the forked chip worker, so host must be a CPU, contiguous, shared-memory [B, *tail] tensor allocated before prepare() (matching the stack's shape/dtype); this is validated up front.

Changes

  • copy_stacked_from: per-shard D2H read-back over stacked.worker_ids, mirroring the existing alloc_stacked_tensor / free_stacked_tensor per-shard loops.
  • Refactor: extracted the shared forked-worker host-buffer validation into _require_forked_host_buffer, now used by both the upload (read) and read-back (write) paths — removes the duplicated is_shared()/is_contiguous()/cpu check.
  • Docs: documented the API in the en and zh-cn getting-started guides (kept in sync).

Testing

  • tests/ut/runtime/test_distributed_worker.py — new TestCopyStackedFrom (mocked worker): happy path, permuted worker_ids, and shape/dtype/type/non-shared/non-contiguous/after-close rejections. 80 passed.
  • tests/st/distributed/test_l3_stacked_device_tensor.py — read-back proof: host source is zeroed after upload, so a correct 10+i read-back confirms the D2H path reads device memory, not stale host state.
  • Code review completed
  • Documentation updated (en + zh-cn)

…ensor

Symmetric to alloc_stacked_tensor: reads every resident shard of a
StackedDeviceTensor back to a host [B, *tail] buffer, filling host[i]
from shard i's owning worker. Like the upload path, the D2H copy runs
in the forked chip worker, so the host buffer must be a CPU, contiguous,
shared-memory tensor allocated before prepare().

Extract the shared forked-worker host-buffer validation into
_require_forked_host_buffer, now used by both the upload (read) and
read-back (write) paths. Add ut coverage (mocked worker) and an st
read-back proof, and document the API in the en/zh-cn guides.
@coderabbitai

coderabbitai Bot commented Jul 6, 2026

Copy link
Copy Markdown

Review Change Stack

Caution

Review failed

An error occurred during the review process. Please try again later.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request introduces the copy_stacked_from API to read back device contents of a StackedDeviceTensor to a host tensor (D2H copy). It includes documentation updates in both English and Chinese, a helper method _require_forked_host_buffer to validate host buffers, and comprehensive system and unit tests for the new functionality. There are no review comments to address, and the changes look solid.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

@lyfne123 lyfne123 merged commit e7b33bc into hw-native-sys:main Jul 6, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

2 participants