dynamo-llm 1.0.2

Dynamo LLM Library
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
// SPDX-FileCopyrightText: Copyright (c) 2024-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

//! Transfer strategy selection based on source and destination storage locations.

use crate::block_manager::v2::memory::StorageKind;

use super::TransferCapabilities;
use crate::block_manager::v2::physical::{layout::PhysicalLayout, transfer::TransferContext};

/// Transfer strategy to use for copying memory between locations.
///
/// The strategy is determined by the source and destination storage locations.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TransferStrategy {
    /// CPU memcpy (for host-to-host transfers)
    Memcpy,

    /// CUDA async host-to-device transfer
    CudaAsyncH2D,

    /// CUDA async device-to-host transfer
    CudaAsyncD2H,

    /// CUDA async device-to-device transfer
    CudaAsyncD2D,

    /// CUDA blocking host-to-device transfer
    CudaBlockingH2D,

    /// CUDA blocking device-to-host transfer
    CudaBlockingD2H,

    /// NIXL read operation (pull from remote)
    NixlRead,

    /// NIXL write operation (push to remote)
    NixlWrite,

    /// NIXL write (flipped local and remote order)
    /// This is needed for some NIXL backends.
    /// For example, the POSIX backend requires that host memory
    /// always be the "local" descriptor list, regardless of whether
    /// it's a read or write.
    NixlWriteFlipped,

    /// NIXL read (flipped local and remote order)
    NixlReadFlipped,

    /// Invalid/unsupported transfer
    Invalid,
}

/// Plan for executing a transfer, either direct or via bounce buffer.
///
/// Some transfers require staging through host memory when direct paths
/// are not enabled via capabilities.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TransferPlan {
    /// Direct single-hop transfer using the specified strategy.
    Direct(TransferStrategy),

    /// Two-hop transfer requiring a bounce buffer in host memory.
    ///
    /// This is used when:
    /// - Device → Remote (without GPU RDMA)
    /// - Disk → Remote
    /// - Device ↔ Disk (without GDS)
    TwoHop {
        /// First hop strategy (src → bounce)
        first: TransferStrategy,

        /// Bounce buffer location (always Pinned for best performance)
        bounce_location: StorageKind,

        /// Second hop strategy (bounce → dst)
        second: TransferStrategy,
    },
}

pub(crate) fn select_strategy(
    src: &PhysicalLayout,
    dst: &PhysicalLayout,
    ctx: &TransferContext,
) -> anyhow::Result<TransferPlan> {
    let is_src_local = src.nixl_metadata().agent_name() == ctx.nixl_agent().name();
    let is_dst_local = dst.nixl_metadata().agent_name() == ctx.nixl_agent().name();

    if !is_src_local && !is_dst_local {
        return Err(anyhow::anyhow!(
            "Both src and dst are remote - this is not supported."
        ));
    }

    if is_src_local && is_dst_local {
        return Ok(select_direct_strategy(
            src.location(),
            dst.location(),
            false,
            ctx.capabilities(),
        ));
    }

    select_remote_strategy_v2(
        src.location(),
        is_src_local,
        dst.location(),
        is_dst_local,
        ctx.capabilities(),
    )
}

/// Select the appropriate transfer plan based on source and destination locations.
///
/// # Arguments
/// * `src` - Source storage location (always local)
/// * `dst` - Destination storage location (can be local or remote)
/// * `dst_is_remote` - Whether destination is on a remote node
/// * `capabilities` - Transfer capability flags
///
/// # Returns
/// A transfer plan (direct or two-hop)
///
/// # Conservative Default Policy
///
/// With default capabilities (all disabled):
/// - Device can only transfer to/from Host
/// - Disk can only transfer to/from Host
/// - Host can transfer to Device, Disk, or Remote
/// - Device ↔ Device is allowed (native CUDA)
///
/// Transfers that would violate this policy are staged through host:
/// - Device → Remote: Device → Host → Remote (2 hops)
/// - Disk → Remote: Disk → Host → Remote (2 hops)
/// - Device ↔ Disk: Device → Host → Disk (2 hops)
///
/// # Optional Direct Paths
///
/// - `allow_gds`: Enables Disk ↔ Device direct transfers
/// - `allow_gpu_rdma`: Enables Device → Remote direct transfers
fn select_direct_strategy(
    src: StorageKind,
    dst: StorageKind,
    dst_is_remote: bool,
    capabilities: &TransferCapabilities,
) -> TransferPlan {
    use StorageKind::*;
    use TransferStrategy::*;

    // Handle remote destination
    if dst_is_remote {
        return select_remote_strategy(src, capabilities);
    }

    // Local-to-local transfers
    match (src, dst) {
        // Host ↔ Host - direct memcpy
        (System, System) | (System, Pinned) | (Pinned, System) | (Pinned, Pinned) => {
            TransferPlan::Direct(Memcpy)
        }

        // Host → Device - direct CUDA
        (System, Device(_)) => TransferPlan::Direct(CudaBlockingH2D),
        (Pinned, Device(_)) => TransferPlan::Direct(CudaAsyncH2D),

        // Device → Host - direct CUDA
        (Device(_), System) => TransferPlan::Direct(CudaBlockingD2H),
        (Device(_), Pinned) => TransferPlan::Direct(CudaAsyncD2H),

        // Device ↔ Device - direct CUDA
        (Device(_), Device(_)) => TransferPlan::Direct(CudaAsyncD2D),

        // Host ↔ Disk - direct NIXL
        (System, Disk(_)) | (Pinned, Disk(_)) => TransferPlan::Direct(NixlWrite),
        (Disk(_), System) | (Disk(_), Pinned) => TransferPlan::Direct(NixlReadFlipped),

        // Disk ↔ Disk - NIXL doesn't seem to support direct transfers here.
        // Leaving this as two-hop for now.
        (Disk(_), Disk(_)) => TransferPlan::TwoHop {
            first: NixlReadFlipped,
            bounce_location: Pinned,
            second: NixlWrite,
        },

        // Device ↔ Disk - check GDS capability
        (Device(_), Disk(_)) => {
            if capabilities.allows_device_disk_direct() {
                // Direct GDS transfer
                TransferPlan::Direct(NixlWrite)
            } else {
                // Stage through host: Device → Pinned → Disk
                TransferPlan::TwoHop {
                    first: CudaAsyncD2H,
                    bounce_location: Pinned,
                    second: NixlWrite,
                }
            }
        }
        (Disk(_), Device(_)) => {
            if capabilities.allows_device_disk_direct() {
                // Direct GDS transfer
                TransferPlan::Direct(NixlRead)
            } else {
                // Stage through host: Disk → Pinned → Device
                TransferPlan::TwoHop {
                    first: NixlReadFlipped,
                    bounce_location: Pinned,
                    second: CudaAsyncH2D,
                }
            }
        }
    }
}

/// Select transfer strategy for remote destination.
fn select_remote_strategy(src: StorageKind, capabilities: &TransferCapabilities) -> TransferPlan {
    use StorageKind::*;
    use TransferStrategy::*;

    match src {
        // Host → Remote - direct NIXL
        System | Pinned => TransferPlan::Direct(NixlWrite),

        // Device → Remote - check GPU RDMA capability
        Device(_) => {
            if capabilities.allows_device_remote_direct() {
                // Direct GPU RDMA transfer
                TransferPlan::Direct(NixlWrite)
            } else {
                // Stage through host: Device → Pinned → Remote
                TransferPlan::TwoHop {
                    first: CudaAsyncD2H,
                    bounce_location: Pinned,
                    second: NixlWrite,
                }
            }
        }

        // Disk → Remote - always stage through host
        Disk(_) => TransferPlan::TwoHop {
            first: NixlWrite,
            bounce_location: Pinned,
            second: NixlWrite,
        },
    }
}

fn select_remote_strategy_v2(
    src: StorageKind,
    is_src_local: bool,
    dst: StorageKind,
    is_dst_local: bool,
    capabilities: &TransferCapabilities,
) -> anyhow::Result<TransferPlan> {
    // We only support System, Pinned and Device for remote transfers.
    // Later we might support staged/bounce buffer transfers.

    if matches!(src, StorageKind::Disk(_)) | matches!(dst, StorageKind::Disk(_)) {
        return Err(anyhow::anyhow!(
            "Neither local nor remote disk transfers are supported over NIXL at this time."
        ));
    }

    if !capabilities.allow_gpu_rdma
        && (matches!(src, StorageKind::Device(_)) || matches!(dst, StorageKind::Device(_)))
    {
        return Err(anyhow::anyhow!(
            "GPU RDMA is disabled - this transfer requires GPU RDMA."
        ));
    }

    if is_src_local && !is_dst_local {
        return Ok(TransferPlan::Direct(TransferStrategy::NixlWrite));
    }

    if is_dst_local && !is_src_local {
        return Ok(TransferPlan::Direct(TransferStrategy::NixlReadFlipped));
    }

    unreachable!("Both src and dst are remote - this is not supported.");
}

#[cfg(test)]
mod tests {
    use super::*;

    fn default_caps() -> TransferCapabilities {
        TransferCapabilities::default()
    }

    #[test]
    fn test_host_to_host_transfers() {
        let caps = default_caps();
        assert_eq!(
            select_direct_strategy(StorageKind::System, StorageKind::System, false, &caps),
            TransferPlan::Direct(TransferStrategy::Memcpy)
        );
        assert_eq!(
            select_direct_strategy(StorageKind::System, StorageKind::Pinned, false, &caps),
            TransferPlan::Direct(TransferStrategy::Memcpy)
        );
        assert_eq!(
            select_direct_strategy(StorageKind::Pinned, StorageKind::System, false, &caps),
            TransferPlan::Direct(TransferStrategy::Memcpy)
        );
        assert_eq!(
            select_direct_strategy(StorageKind::Pinned, StorageKind::Pinned, false, &caps),
            TransferPlan::Direct(TransferStrategy::Memcpy)
        );
    }

    #[test]
    fn test_host_to_device_transfers() {
        let caps = default_caps();
        // System (unpinned) to device should be blocking
        assert_eq!(
            select_direct_strategy(StorageKind::System, StorageKind::Device(0), false, &caps),
            TransferPlan::Direct(TransferStrategy::CudaBlockingH2D)
        );

        // Pinned to device should be async
        assert_eq!(
            select_direct_strategy(StorageKind::Pinned, StorageKind::Device(0), false, &caps),
            TransferPlan::Direct(TransferStrategy::CudaAsyncH2D)
        );
    }

    #[test]
    fn test_device_to_host_transfers() {
        let caps = default_caps();
        // Device to system should be blocking
        assert_eq!(
            select_direct_strategy(StorageKind::Device(0), StorageKind::System, false, &caps),
            TransferPlan::Direct(TransferStrategy::CudaBlockingD2H)
        );

        // Device to pinned should be async
        assert_eq!(
            select_direct_strategy(StorageKind::Device(0), StorageKind::Pinned, false, &caps),
            TransferPlan::Direct(TransferStrategy::CudaAsyncD2H)
        );
    }

    #[test]
    fn test_device_to_device_transfers() {
        let caps = default_caps();
        assert_eq!(
            select_direct_strategy(StorageKind::Device(0), StorageKind::Device(1), false, &caps),
            TransferPlan::Direct(TransferStrategy::CudaAsyncD2D)
        );
        assert_eq!(
            select_direct_strategy(StorageKind::Device(3), StorageKind::Device(3), false, &caps),
            TransferPlan::Direct(TransferStrategy::CudaAsyncD2D)
        );
    }

    #[test]
    fn test_disk_to_host_transfers() {
        let caps = default_caps();
        // Disk to host - direct NIXL
        assert_eq!(
            select_direct_strategy(StorageKind::Disk(42), StorageKind::System, false, &caps),
            TransferPlan::Direct(TransferStrategy::NixlReadFlipped)
        );
        assert_eq!(
            select_direct_strategy(StorageKind::Disk(42), StorageKind::Pinned, false, &caps),
            TransferPlan::Direct(TransferStrategy::NixlReadFlipped)
        );
    }

    #[test]
    fn test_host_to_disk_transfers() {
        let caps = default_caps();
        // Host to disk - direct NIXL
        assert_eq!(
            select_direct_strategy(StorageKind::System, StorageKind::Disk(42), false, &caps),
            TransferPlan::Direct(TransferStrategy::NixlWrite)
        );
        assert_eq!(
            select_direct_strategy(StorageKind::Pinned, StorageKind::Disk(42), false, &caps),
            TransferPlan::Direct(TransferStrategy::NixlWrite)
        );
    }

    #[test]
    fn test_device_to_disk_without_gds() {
        let caps = default_caps(); // GDS disabled
        // Device → Disk should use bounce buffer
        let plan =
            select_direct_strategy(StorageKind::Device(0), StorageKind::Disk(42), false, &caps);
        match plan {
            TransferPlan::TwoHop {
                first,
                bounce_location,
                second,
            } => {
                assert_eq!(first, TransferStrategy::CudaAsyncD2H);
                assert_eq!(bounce_location, StorageKind::Pinned);
                assert_eq!(second, TransferStrategy::NixlWrite);
            }
            _ => panic!("Expected TwoHop plan"),
        }
    }

    #[test]
    fn test_disk_to_device_without_gds() {
        let caps = default_caps(); // GDS disabled
        // Disk → Device should use bounce buffer
        let plan =
            select_direct_strategy(StorageKind::Disk(42), StorageKind::Device(0), false, &caps);
        match plan {
            TransferPlan::TwoHop {
                first,
                bounce_location,
                second,
            } => {
                assert_eq!(first, TransferStrategy::NixlReadFlipped);
                assert_eq!(bounce_location, StorageKind::Pinned);
                assert_eq!(second, TransferStrategy::CudaAsyncH2D);
            }
            _ => panic!("Expected TwoHop plan"),
        }
    }

    #[test]
    fn test_device_to_disk_with_gds() {
        let caps = TransferCapabilities::default().with_gds(true);
        // Device → Disk should be direct with GDS
        assert_eq!(
            select_direct_strategy(StorageKind::Device(0), StorageKind::Disk(42), false, &caps),
            TransferPlan::Direct(TransferStrategy::NixlWrite)
        );
    }

    #[test]
    fn test_disk_to_device_with_gds() {
        let caps = TransferCapabilities::default().with_gds(true);
        // Disk → Device should be direct with GDS
        assert_eq!(
            select_direct_strategy(StorageKind::Disk(42), StorageKind::Device(0), false, &caps),
            TransferPlan::Direct(TransferStrategy::NixlRead)
        );
    }

    #[test]
    fn test_host_to_remote() {
        let caps = default_caps();
        // Host → Remote - always direct
        assert_eq!(
            select_direct_strategy(StorageKind::System, StorageKind::System, true, &caps),
            TransferPlan::Direct(TransferStrategy::NixlWrite)
        );
        assert_eq!(
            select_direct_strategy(StorageKind::Pinned, StorageKind::Pinned, true, &caps),
            TransferPlan::Direct(TransferStrategy::NixlWrite)
        );
    }

    #[test]
    fn test_device_to_remote_without_rdma() {
        let caps = default_caps(); // GPU RDMA disabled
        // Device → Remote should use bounce buffer
        let plan = select_direct_strategy(StorageKind::Device(0), StorageKind::System, true, &caps);
        match plan {
            TransferPlan::TwoHop {
                first,
                bounce_location,
                second,
            } => {
                assert_eq!(first, TransferStrategy::CudaAsyncD2H);
                assert_eq!(bounce_location, StorageKind::Pinned);
                assert_eq!(second, TransferStrategy::NixlWrite);
            }
            _ => panic!("Expected TwoHop plan"),
        }
    }

    #[test]
    fn test_device_to_remote_with_rdma() {
        let caps = TransferCapabilities::default().with_gpu_rdma(true);
        // Device → Remote should be direct with GPU RDMA
        assert_eq!(
            select_direct_strategy(StorageKind::Device(0), StorageKind::Device(0), true, &caps),
            TransferPlan::Direct(TransferStrategy::NixlWrite)
        );
    }

    #[test]
    fn test_disk_to_remote() {
        let caps = default_caps();
        // Disk → Remote always uses bounce buffer
        let plan = select_direct_strategy(StorageKind::Disk(42), StorageKind::System, true, &caps);
        match plan {
            TransferPlan::TwoHop {
                first,
                bounce_location,
                second,
            } => {
                assert_eq!(first, TransferStrategy::NixlWrite);
                assert_eq!(bounce_location, StorageKind::Pinned);
                assert_eq!(second, TransferStrategy::NixlWrite);
            }
            _ => panic!("Expected TwoHop plan"),
        }
    }
}