Files
sglang/python/sglang/srt/layers/zero_copy_context.py
T
+26 abddb1c7e9 [Kimi] Support kimi-k3 (#32541)
Co-authored-by: DarkSharpness <76582120+DarkSharpness@users.noreply.github.com>
Co-authored-by: Xiaoyu Zhang <1182563586@qq.com>
Co-authored-by: Mick <mickjagger19@icloud.com>
Co-authored-by: Yuhao Yang <47235274+yhyang201@users.noreply.github.com>
Co-authored-by: Cheng Wan <54331508+ch-wan@users.noreply.github.com>
Co-authored-by: Ke Bao <ispobaoke@gmail.com>
Co-authored-by: Baizhou Zhang <sobereddiezhang@gmail.com>
Co-authored-by: Chunan Zeng <zcnrex@gmail.com>
Co-authored-by: Khoa Pham <khoa.pham@radixark.ai>
Co-authored-by: Ziyi Xu <ziyi.xu@radixark.ai>
Co-authored-by: Zijie Xia <37504505+zijiexia@users.noreply.github.com>
Co-authored-by: Yuwei An <ayw.sirius19@gmail.com>
Co-authored-by: zhangxiaohao <1024393531@qq.com>
Co-authored-by: Yangmin Li <yangminl@nvidia.com>
Co-authored-by: Julien Lin <jullin@nvidia.com>
Co-authored-by: Hao Phan <htphan@nvidia.com>
Co-authored-by: Thomas Wang <1am9trash@gmail.com>
Co-authored-by: RolaoDenthu <xinyisong0111@gmail.com>
Co-authored-by: pigeonsoup <32922982+pigeonsoup@users.noreply.github.com>
Co-authored-by: HaiShaw <hixiao@gmail.com>
Co-authored-by: Xinyuan Tong <115166877+JustinTong0323@users.noreply.github.com>
Co-authored-by: Pranjal Shankhdhar <pranjal.ssh@gmail.com>
Co-authored-by: Lee Nau <lee.nau@gmail.com>
Co-authored-by: HMING <126185151+Hearum@users.noreply.github.com>
Co-authored-by: elvischenv <219235043+elvischenv@users.noreply.github.com>
Co-authored-by: Byron Hsu <byronhsu1230@gmail.com>
Co-authored-by: Byron Hsu <byron+per@periodiclabs.ai>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
Co-authored-by: Thomas Wang <thomawan@amd.com>
Co-authored-by: Xinyi Song <86638975+RolaoDenthu@users.noreply.github.com>
Co-authored-by: Mohammad Miadh Angkad <176301910+mmangkad@users.noreply.github.com>
Co-authored-by: Cheng Wan <cheng.wan@radixark.ai>
Co-authored-by: BBuf <xiaoyu.zhang@radixark.ai>
Co-authored-by: Hanming Lu <hanminglu@meta.com>
Co-authored-by: Xinyi Song <xinyis10@illinois.edu>
2026-08-04 13:22:49 -07:00

57 lines
1.5 KiB
Python

import contextlib
from typing import Iterator, Optional
import msgspec
import torch
class ZeroCopyContext(msgspec.Struct, frozen=True):
moe_output: Optional[torch.Tensor] = None
ctx = ZeroCopyContext()
@contextlib.contextmanager
def set_moe_output(out: torch.Tensor) -> Iterator[None]:
"""Publish `out` as the MoE runner's output destination for the block."""
old_output = ctx.moe_output
msgspec.structs.force_setattr(ctx, "moe_output", out)
try:
yield
finally:
msgspec.structs.force_setattr(ctx, "moe_output", old_output)
def get_moe_output(ref: torch.Tensor) -> Optional[torch.Tensor]:
"""The published destination iff it can stand in for empty_like(ref)."""
return get_moe_output_spec(ref.shape, ref.dtype, ref.device)
def get_moe_output_spec(
shape: torch.Size, dtype: torch.dtype, device: torch.device
) -> Optional[torch.Tensor]:
"""Spec form of get_moe_output for callers that would otherwise have to
materialize a reference tensor just for the match (e.g. the trtllm-gen
runner, whose activation input is fp4-packed and shaped differently
from its output)."""
out = ctx.moe_output
if (
out is not None
and out.shape == shape
and out.dtype == dtype
and out.device == device
and out.is_contiguous()
):
return out
return None
__all__ = [
"ZeroCopyContext",
"ctx",
"set_moe_output",
"get_moe_output",
"get_moe_output_spec",
]