"examples/backends/vllm/launch/agg_omni_video.sh" did not exist on "6bccf099f8372d17cae50f8050d42dd8a2c14d6d"
remote.rs 4.08 KB
Newer Older
Ryan Olson's avatar
Ryan Olson committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

//! Remote layout wrapper reconstructed from imported metadata.

use super::handle::LayoutHandle;
use crate::layout::PhysicalLayout;

/// A remote physical layout reconstructed from imported metadata.
///
/// This wraps a `PhysicalLayout` that was deserialized from another worker's
/// exported metadata. The layout's memory regions point to addresses on the
/// remote worker and are used for building NIXL RDMA transfer descriptors.
///
/// This type is cheap to clone as `PhysicalLayout` contains `Arc` internally.
#[derive(Debug, Clone)]
pub struct RemoteLayout {
    handle: LayoutHandle,
    layout: PhysicalLayout,
}

#[allow(dead_code)]
impl RemoteLayout {
    /// Create a new remote layout.
    ///
    /// # Arguments
    /// * `handle` - Unique handle for this layout (from remote worker)
    /// * `layout` - The reconstructed physical layout
    pub fn new(handle: LayoutHandle, layout: PhysicalLayout) -> Self {
        Self { handle, layout }
    }

    /// Get the handle for this layout.
    pub fn handle(&self) -> LayoutHandle {
        self.handle
    }

    /// Get a reference to the physical layout.
    pub fn layout(&self) -> &PhysicalLayout {
        &self.layout
    }

    /// Get the worker_id from the handle (identifies the remote worker).
    pub fn worker_id(&self) -> u64 {
        self.handle.worker_id()
    }

    /// Get the layout_id from the handle.
    pub fn layout_id(&self) -> u16 {
        self.handle.layout_id()
    }

    /// Consume this remote layout and return the physical layout.
    pub fn into_layout(self) -> PhysicalLayout {
        self.layout
    }
}

#[cfg(all(test, feature = "testing-kvbm"))]
mod tests {
    use super::*;
    use crate::layout::{LayoutConfig, LayoutDescriptor, NixlMetadata, PhysicalLayout};

    fn make_serialized_layout() -> LayoutDescriptor {
        use crate::layout::{BlockFormat, FullyContiguousDetails, LayoutTypeDetails};
        use dynamo_memory::{MemoryRegion, StorageKind, nixl};

        let config = LayoutConfig::builder()
            .num_blocks(2)
            .num_layers(2)
            .outer_dim(2)
            .page_size(4)
            .inner_dim(8)
            .dtype_width_bytes(2)
            .build()
            .unwrap();

        let required_size = config.num_blocks
            * config.num_layers
            * config.outer_dim
            * config.page_size
            * config.inner_dim
            * config.dtype_width_bytes;

        LayoutDescriptor {
            version: 1,
            layout_config: config,
            location: StorageKind::System,
            nixl_metadata: NixlMetadata::new("remote_agent".to_string(), nixl::MemType::Dram, 0),
            memory_descriptors: vec![MemoryRegion {
                addr: 0x1000,
                size: required_size,
            }],
            layout_type_details: LayoutTypeDetails::FullyContiguous(FullyContiguousDetails {
                block_format: BlockFormat::Operational,
                kv_block_layout: crate::layout::KvBlockLayout::OperationalNHD,
            }),
        }
    }

    #[test]
    fn test_remote_layout_creation() {
        let handle = LayoutHandle::new(999, 42);
        let serialized = make_serialized_layout();
        let layout = PhysicalLayout::from_descriptor(serialized).unwrap();
        let remote = RemoteLayout::new(handle, layout);

        assert_eq!(remote.handle(), handle);
        assert_eq!(remote.worker_id(), 999);
        assert_eq!(remote.layout_id(), 42);
        assert_eq!(
            remote.layout().layout().block_layout(),
            crate::layout::KvBlockLayout::OperationalNHD
        );
    }

    #[test]
    fn test_remote_layout_into_layout() {
        let handle = LayoutHandle::new(100, 200);
        let serialized = make_serialized_layout();
        let layout = PhysicalLayout::from_descriptor(serialized).unwrap();
        let remote = RemoteLayout::new(handle, layout);

        let _recovered = remote.into_layout();
        // Successfully consumed and returned the layout
    }
}