tests.rs 11.6 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

//! Integration tests for layout serialization.
//!
//! These tests verify the complete serialization and deserialization flow,
//! ensuring that layouts can be transmitted to remote nodes and reconstructed
//! with all necessary metadata intact.

use crate::layout::physical::PhysicalLayout;
use crate::layout::{BlockDimension, LayoutConfig, LayoutDescriptor};
use dynamo_memory::nixl::{MemType, NixlAgent, NixlDescriptor};
use dynamo_memory::{Buffer, MemoryDescriptor, MemoryRegion, StorageKind};
use std::any::Any;
use std::sync::Arc;

// Simple mock implementation for testing
#[derive(Debug)]
pub struct MockMemory {
    addr: usize,
    size: usize,
}

impl MockMemory {
    pub fn new(addr: usize, size: usize) -> Arc<Self> {
        Arc::new(Self { addr, size })
    }
}

impl MemoryDescriptor for MockMemory {
    fn addr(&self) -> usize {
        self.addr
    }
    fn size(&self) -> usize {
        self.size
    }
    fn storage_kind(&self) -> StorageKind {
        StorageKind::System
    }
    fn as_any(&self) -> &dyn Any {
        self
    }
    fn nixl_descriptor(&self) -> Option<NixlDescriptor> {
        None
    }
}

/// Mock memory region for testing serialization
#[derive(Debug)]
struct TestMemoryRegion {
    addr: usize,
    size: usize,
    kind: StorageKind,
    descriptor: NixlDescriptor,
}

impl TestMemoryRegion {
    fn new(addr: usize, size: usize, kind: StorageKind) -> Arc<Self> {
        Arc::new(Self {
            addr,
            size,
            kind,
            descriptor: NixlDescriptor {
                addr: addr as u64,
                size,
                mem_type: MemType::Dram,
                device_id: 0,
            },
        })
    }
}

impl MemoryDescriptor for TestMemoryRegion {
    fn addr(&self) -> usize {
        self.addr
    }

    fn size(&self) -> usize {
        self.size
    }

    fn storage_kind(&self) -> StorageKind {
        self.kind
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn nixl_descriptor(&self) -> Option<NixlDescriptor> {
        Some(self.descriptor.clone())
    }
}

fn make_test_config() -> LayoutConfig {
    LayoutConfig::builder()
        .num_blocks(10)
        .num_layers(4)
        .outer_dim(2)
        .page_size(16)
        .inner_dim(128)
        .dtype_width_bytes(2)
        .build()
        .unwrap()
}

#[test]
fn test_fully_contiguous_layout_serialization_roundtrip() {
    let agent = NixlAgent::new("test-fc-serialize").expect("failed to create agent");
    let config = make_test_config();

    // Calculate required size
    let required_size = config.num_blocks
        * config.num_layers
        * config.outer_dim
        * config.page_size
        * config.inner_dim
        * config.dtype_width_bytes;

    // Create test memory region
    let memory = TestMemoryRegion::new(0x10000, required_size, StorageKind::System);
    let regions = vec![Buffer::from_arc(memory as Arc<dyn MemoryDescriptor>)];

    // Build physical layout
    let original_layout = PhysicalLayout::builder(agent)
        .with_config(config.clone())
        .fully_contiguous()
        .with_registered_regions(regions)
        .expect("failed to provide regions")
        .build()
        .expect("failed to build layout");

    // Serialize to LayoutDescriptor
    let serialized = original_layout
        .to_descriptor()
        .expect("failed to serialize layout");

    // Verify serialized data
    assert_eq!(serialized.version, LayoutDescriptor::CURRENT_VERSION);
    assert_eq!(serialized.layout_config, config);
    assert_eq!(serialized.location, StorageKind::System);
    assert_eq!(serialized.memory_descriptors.len(), 1);
    assert_eq!(serialized.memory_descriptors[0].addr, 0x10000);
    assert_eq!(serialized.memory_descriptors[0].size, required_size);

    // Serialize to JSON
    let json = serialized.to_json().expect("failed to serialize to JSON");
    assert!(json.contains("\"version\":1"));
    assert!(json.contains("\"num_blocks\":10"));

    // Deserialize from JSON
    let deserialized = LayoutDescriptor::from_json(&json).expect("failed to deserialize from JSON");

    // Verify deserialized matches original
    assert_eq!(deserialized.version, serialized.version);
    assert_eq!(deserialized.layout_config, serialized.layout_config);
    assert_eq!(deserialized.location, serialized.location);
    assert_eq!(
        deserialized.memory_descriptors.len(),
        serialized.memory_descriptors.len()
    );

    // Reconstruct layout from serialized data
    let reconstructed =
        PhysicalLayout::from_descriptor(deserialized).expect("failed to reconstruct layout");

    // Verify reconstructed layout has same configuration
    assert_eq!(reconstructed.layout().config(), &config);
    assert_eq!(reconstructed.location(), StorageKind::System);
    assert_eq!(reconstructed.layout().num_blocks(), 10);
    assert_eq!(reconstructed.layout().num_layers(), 4);
    assert!(reconstructed.layout().is_fully_contiguous());
}

#[test]
fn test_layer_separate_layout_serialization_roundtrip() {
    let agent = NixlAgent::new("test-ls-serialize").expect("failed to create agent");
    let config = make_test_config();

    // Calculate per-layer size
    let per_layer_size = config.num_blocks
        * config.outer_dim
        * config.page_size
        * config.inner_dim
        * config.dtype_width_bytes;

    // Create memory regions (one per layer)
    let regions: Vec<Buffer> = (0..config.num_layers)
        .map(|i| {
            Buffer::from_arc(TestMemoryRegion::new(
                0x10000 + i * per_layer_size,
                per_layer_size,
                StorageKind::System,
            ) as Arc<dyn MemoryDescriptor>)
        })
        .collect();

    // Build physical layout
    let original_layout = PhysicalLayout::builder(agent)
        .with_config(config.clone())
        .layer_separate(BlockDimension::BlockIsFirstDim)
        .with_registered_regions(regions)
        .expect("failed to provide regions")
        .build()
        .expect("failed to build layout");

    // Serialize to LayoutDescriptor
    let serialized = original_layout
        .to_descriptor()
        .expect("failed to serialize layout");

    // Verify serialized data
    assert_eq!(serialized.version, LayoutDescriptor::CURRENT_VERSION);
    assert_eq!(serialized.layout_config, config);
    assert_eq!(serialized.memory_descriptors.len(), 4); // One per layer

    // Verify memory descriptors
    for (i, desc) in serialized.memory_descriptors.iter().enumerate() {
        assert_eq!(desc.addr, 0x10000 + i * per_layer_size);
        assert_eq!(desc.size, per_layer_size);
    }

    // Serialize to JSON bytes
    let json_bytes = serialized
        .to_json_bytes()
        .expect("failed to serialize to JSON bytes");

    // Deserialize from JSON bytes
    let deserialized = LayoutDescriptor::from_json_bytes(&json_bytes)
        .expect("failed to deserialize from JSON bytes");

    // Verify deserialized matches original
    assert_eq!(deserialized.version, serialized.version);
    assert_eq!(deserialized.layout_config, serialized.layout_config);
    assert_eq!(
        deserialized.memory_descriptors.len(),
        serialized.memory_descriptors.len()
    );

    // Reconstruct layout from serialized data
    let reconstructed =
        PhysicalLayout::from_descriptor(deserialized).expect("failed to reconstruct layout");

    // Verify reconstructed layout has same configuration
    assert_eq!(reconstructed.layout().config(), &config);
    assert_eq!(reconstructed.location(), StorageKind::System);
    assert_eq!(reconstructed.layout().num_blocks(), 10);
    assert_eq!(reconstructed.layout().num_layers(), 4);
    assert!(!reconstructed.layout().is_fully_contiguous());
}

#[test]
fn test_memory_region_calculation_after_deserialization() {
    let agent = NixlAgent::new("test-memory-calc").expect("failed to create agent");
    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;

    let memory = TestMemoryRegion::new(0x1000, required_size, StorageKind::System);
    let regions = vec![Buffer::from_arc(memory as Arc<dyn MemoryDescriptor>)];

    let original_layout = PhysicalLayout::builder(agent)
        .with_config(config.clone())
        .fully_contiguous()
        .with_registered_regions(regions)
        .expect("failed to provide regions")
        .build()
        .expect("failed to build layout");

    // Serialize and deserialize
    let serialized = original_layout
        .to_descriptor()
        .expect("failed to serialize");
    let reconstructed = PhysicalLayout::from_descriptor(serialized).expect("failed to reconstruct");

    // Verify memory region calculations
    let region = reconstructed
        .memory_region(0, 0, 0)
        .expect("failed to get memory region");
    assert_eq!(region.addr, 0x1000);

    let region_size = config.page_size * config.inner_dim * config.dtype_width_bytes;
    assert_eq!(region.size, region_size);

    // Test different block/layer/outer indices
    let region = reconstructed
        .memory_region(1, 1, 1)
        .expect("failed to get memory region");
    // Address should be: base + block_stride + layer_stride + outer_stride
    let layer_stride = config.outer_dim * region_size;
    let block_stride = config.num_layers * layer_stride;
    let expected_addr = 0x1000 + block_stride + layer_stride + region_size;
    assert_eq!(region.addr, expected_addr);
}

#[test]
fn test_version_check_on_deserialization() {
    let config = make_test_config();

    // Calculate required size for fully contiguous layout
    let required_size = config.num_blocks
        * config.num_layers
        * config.outer_dim
        * config.page_size
        * config.inner_dim
        * config.dtype_width_bytes;

    let mut serialized = LayoutDescriptor {
        version: 999, // Future version
        layout_config: config.clone(),
        location: StorageKind::System,
        nixl_metadata: crate::layout::physical::NixlMetadata::new(
            "test".to_string(),
            MemType::Dram,
            0,
        ),
        memory_descriptors: vec![],
        layout_type_details: crate::layout::LayoutTypeDetails::FullyContiguous(
            crate::layout::FullyContiguousDetails {
                block_format: crate::layout::BlockFormat::Operational,
                kv_block_layout: crate::layout::KvBlockLayout::OperationalNHD,
            },
        ),
    };

    // Should fail with unsupported version
    let result = PhysicalLayout::from_descriptor(serialized.clone());
    assert!(result.is_err());
    assert!(
        result
            .unwrap_err()
            .to_string()
            .contains("Unsupported serialization version")
    );

    // Should succeed with supported version
    serialized.version = LayoutDescriptor::CURRENT_VERSION;
    serialized.memory_descriptors = vec![MemoryRegion {
        addr: 0x1000,
        size: required_size,
    }];
    let result = PhysicalLayout::from_descriptor(serialized);
    if let Err(ref e) = result {
        eprintln!("Error during deserialization: {}", e);
    }
    assert!(
        result.is_ok(),
        "Expected successful deserialization, got error: {:?}",
        result.err()
    );

    let layout = result.unwrap();
    assert_eq!(
        layout.layout().block_layout(),
        crate::layout::KvBlockLayout::OperationalNHD
    );
}