ferrotorch-distributed 0.5.5

Distributed training for ferrotorch — backends, collectives, and DDP
Documentation
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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
//! Remote Procedure Call (RPC) framework for distributed training.
//!
//! Provides a simple RPC mechanism built on top of the [`Backend`] transport
//! layer. Workers can register callable functions and invoke them on remote
//! ranks by name.
//!
//! # Architecture
//!
//! - [`RpcAgent`] wraps a [`Backend`] and adds a function registry, request
//!   routing, and response correlation.
//! - [`TcpRpcBackend`] is a thin wrapper around [`TcpBackend`](crate::backend::TcpBackend)
//!   that adds length-prefixed framing for variable-size RPC messages.
//!
//! # Limitations
//!
//! - **`rpc_async` spawns an unbounded number of threads.** Each async RPC
//!   call spawns a new OS thread. This is acceptable for the typical RPC use
//!   case (infrequent coordination calls), but is not suitable for
//!   high-frequency fire-and-forget patterns. A future version may use a
//!   thread pool or async runtime.

use std::collections::HashMap;
use std::sync::{Arc, Mutex};

use ferrotorch_core::{FerrotorchError, FerrotorchResult};

use crate::backend::Backend;

// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------

/// Maximum allowed RPC message size (1 GiB). Messages exceeding this limit
/// are rejected to prevent out-of-memory conditions from malicious or
/// corrupted length prefixes.
const MAX_RPC_MSG_SIZE: usize = 1 << 30;

// ---------------------------------------------------------------------------
// Error types
// ---------------------------------------------------------------------------

/// Errors specific to the RPC subsystem.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum RpcError {
    #[error("RPC function not found: {name}")]
    FunctionNotFound { name: String },

    #[error("invalid RPC message: {reason}")]
    InvalidMessage { reason: String },

    #[error("no connection to rank {rank} (star topology: non-zero ranks only connect to rank 0)")]
    NoConnection { rank: usize },

    #[error("RPC internal error: {0}")]
    Internal(String),

    #[error("RPC timeout")]
    Timeout,
}

impl From<RpcError> for FerrotorchError {
    fn from(e: RpcError) -> Self {
        FerrotorchError::InvalidArgument {
            message: e.to_string(),
        }
    }
}

// ---------------------------------------------------------------------------
// RPC message types
// ---------------------------------------------------------------------------

/// A serialized RPC request.
#[derive(Debug, Clone)]
struct RpcRequest {
    /// Unique identifier for correlating responses.
    request_id: u64,
    /// Name of the remote function to call.
    function_name: String,
    /// Serialized arguments (opaque bytes).
    payload: Vec<u8>,
}

/// A serialized RPC response.
#[derive(Debug, Clone)]
struct RpcResponse {
    /// The request_id this response is for.
    request_id: u64,
    /// Serialized return value (opaque bytes).
    payload: Vec<u8>,
    /// Error message, if any.
    error: Option<String>,
}

impl RpcRequest {
    fn serialize(&self) -> Vec<u8> {
        let mut buf = Vec::new();
        // Tag byte: 0x01 = request
        buf.push(0x01);
        buf.extend_from_slice(&self.request_id.to_le_bytes());
        let name_bytes = self.function_name.as_bytes();
        buf.extend_from_slice(&(name_bytes.len() as u32).to_le_bytes());
        buf.extend_from_slice(name_bytes);
        buf.extend_from_slice(&(self.payload.len() as u32).to_le_bytes());
        buf.extend_from_slice(&self.payload);
        buf
    }

    fn deserialize(data: &[u8]) -> Result<Self, RpcError> {
        if data.is_empty() || data[0] != 0x01 {
            return Err(RpcError::InvalidMessage {
                reason: "expected request tag 0x01".into(),
            });
        }
        let mut pos = 1;
        if data.len() < pos + 8 {
            return Err(RpcError::InvalidMessage {
                reason: "request too short for request_id".into(),
            });
        }
        let request_id = u64::from_le_bytes(data[pos..pos + 8].try_into().unwrap());
        pos += 8;

        if data.len() < pos + 4 {
            return Err(RpcError::InvalidMessage {
                reason: "request too short for name length".into(),
            });
        }
        let name_len = u32::from_le_bytes(data[pos..pos + 4].try_into().unwrap()) as usize;
        pos += 4;

        if data.len() < pos + name_len {
            return Err(RpcError::InvalidMessage {
                reason: "request too short for function name".into(),
            });
        }
        let function_name = String::from_utf8(data[pos..pos + name_len].to_vec()).map_err(|e| {
            RpcError::InvalidMessage {
                reason: format!("invalid UTF-8 in function name: {e}"),
            }
        })?;
        pos += name_len;

        if data.len() < pos + 4 {
            return Err(RpcError::InvalidMessage {
                reason: "request too short for payload length".into(),
            });
        }
        let payload_len = u32::from_le_bytes(data[pos..pos + 4].try_into().unwrap()) as usize;
        pos += 4;

        if data.len() < pos + payload_len {
            return Err(RpcError::InvalidMessage {
                reason: "request too short for payload".into(),
            });
        }
        let payload = data[pos..pos + payload_len].to_vec();

        Ok(Self {
            request_id,
            function_name,
            payload,
        })
    }
}

impl RpcResponse {
    fn serialize(&self) -> Vec<u8> {
        let mut buf = Vec::new();
        // Tag byte: 0x02 = response
        buf.push(0x02);
        buf.extend_from_slice(&self.request_id.to_le_bytes());
        if let Some(err) = &self.error {
            buf.push(0x01); // has error
            let err_bytes = err.as_bytes();
            buf.extend_from_slice(&(err_bytes.len() as u32).to_le_bytes());
            buf.extend_from_slice(err_bytes);
        } else {
            buf.push(0x00); // no error
            buf.extend_from_slice(&(self.payload.len() as u32).to_le_bytes());
            buf.extend_from_slice(&self.payload);
        }
        buf
    }

    fn deserialize(data: &[u8]) -> Result<Self, RpcError> {
        if data.is_empty() || data[0] != 0x02 {
            return Err(RpcError::InvalidMessage {
                reason: "expected response tag 0x02".into(),
            });
        }
        let mut pos = 1;
        if data.len() < pos + 8 {
            return Err(RpcError::InvalidMessage {
                reason: "response too short for request_id".into(),
            });
        }
        let request_id = u64::from_le_bytes(data[pos..pos + 8].try_into().unwrap());
        pos += 8;

        if data.len() < pos + 1 {
            return Err(RpcError::InvalidMessage {
                reason: "response too short for error flag".into(),
            });
        }
        let has_error = data[pos] == 0x01;
        pos += 1;

        if data.len() < pos + 4 {
            return Err(RpcError::InvalidMessage {
                reason: "response too short for payload/error length".into(),
            });
        }
        let len = u32::from_le_bytes(data[pos..pos + 4].try_into().unwrap()) as usize;
        pos += 4;

        if data.len() < pos + len {
            return Err(RpcError::InvalidMessage {
                reason: "response too short for payload/error data".into(),
            });
        }

        if has_error {
            let error_msg = String::from_utf8(data[pos..pos + len].to_vec()).map_err(|e| {
                RpcError::InvalidMessage {
                    reason: format!("invalid UTF-8 in error message: {e}"),
                }
            })?;
            Ok(Self {
                request_id,
                payload: Vec::new(),
                error: Some(error_msg),
            })
        } else {
            Ok(Self {
                request_id,
                payload: data[pos..pos + len].to_vec(),
                error: None,
            })
        }
    }
}

// ---------------------------------------------------------------------------
// TCP RPC Backend
// ---------------------------------------------------------------------------

/// TCP-based RPC transport built on [`TcpBackend`](crate::backend::TcpBackend).
///
/// # Topology limitation
///
/// `TcpRpcBackend` inherits the **star topology** from `TcpBackend`: non-zero
/// ranks only have a direct TCP connection to rank 0. This means:
///
/// - **Rank 0 can send/recv RPC messages to/from any rank.**
/// - **Non-zero ranks can only send/recv RPC messages to/from rank 0.**
/// - **Direct rank-to-rank RPC between two non-zero ranks (e.g., rank 1 to
///   rank 2) will fail** with [`RpcError::NoConnection`].
///
/// If rank-to-rank RPC is needed, implement a relay through rank 0 or use a
/// full-mesh backend. This is a known limitation of the current TCP transport.
pub struct TcpRpcBackend {
    backend: Arc<dyn Backend>,
}

impl TcpRpcBackend {
    /// Create a new TCP RPC backend wrapping an existing [`Backend`].
    pub fn new(backend: Arc<dyn Backend>) -> Self {
        Self { backend }
    }

    /// Send a raw RPC message to `dst_rank`.
    ///
    /// # Errors
    ///
    /// Returns [`RpcError::NoConnection`] if there is no direct connection
    /// to `dst_rank` (star topology: non-zero ranks can only reach rank 0).
    pub fn send(&self, data: &[u8], dst_rank: usize) -> FerrotorchResult<()> {
        self.backend.send(data, dst_rank).map_err(|e| {
            let msg = e.to_string();
            if msg.contains("no connection") || msg.contains("NoConnection") {
                RpcError::NoConnection { rank: dst_rank }.into()
            } else {
                e
            }
        })
    }

    /// Receive a raw RPC message from `src_rank`.
    ///
    /// Enforces [`MAX_RPC_MSG_SIZE`] to prevent OOM from malicious or
    /// corrupted length prefixes.
    ///
    /// # Errors
    ///
    /// Returns [`RpcError::NoConnection`] if there is no direct connection
    /// to `src_rank`.
    /// Returns [`RpcError::InvalidMessage`] if the message exceeds
    /// [`MAX_RPC_MSG_SIZE`].
    pub fn recv(&self, dst: &mut [u8], src_rank: usize) -> FerrotorchResult<()> {
        if dst.len() > MAX_RPC_MSG_SIZE {
            return Err(RpcError::InvalidMessage {
                reason: format!(
                    "RPC message size {} exceeds maximum allowed size {} (1 GiB)",
                    dst.len(),
                    MAX_RPC_MSG_SIZE
                ),
            }
            .into());
        }
        self.backend.recv(dst, src_rank).map_err(|e| {
            let msg = e.to_string();
            if msg.contains("no connection") || msg.contains("NoConnection") {
                RpcError::NoConnection { rank: src_rank }.into()
            } else {
                e
            }
        })
    }

    /// The rank of this backend.
    pub fn rank(&self) -> usize {
        self.backend.rank()
    }

    /// The world size.
    pub fn world_size(&self) -> usize {
        self.backend.world_size()
    }
}

// ---------------------------------------------------------------------------
// RPC Agent
// ---------------------------------------------------------------------------

/// Type-erased RPC handler function.
///
/// Takes serialized arguments and returns serialized result (or error).
type RpcHandler = Box<dyn Fn(&[u8]) -> Result<Vec<u8>, String> + Send + Sync>;

/// RPC agent that manages function registration and remote invocation.
///
/// Each rank creates an `RpcAgent` wrapping a [`Backend`]. Functions are
/// registered with [`register`] and invoked remotely with [`rpc_sync`].
///
/// # Response correlation
///
/// Concurrent `rpc_sync` calls are correlated by `request_id`. If a received
/// response has a different `request_id` than expected, it is buffered and
/// the agent retries the recv. Buffered responses are checked before
/// issuing new recv calls.
pub struct RpcAgent {
    backend: Arc<dyn Backend>,
    registry: Mutex<HashMap<String, Arc<RpcHandler>>>,
    next_request_id: Mutex<u64>,
    /// Buffered responses from out-of-order receives, keyed by request_id.
    buffered_responses: Mutex<HashMap<u64, RpcResponse>>,
}

impl RpcAgent {
    /// Create a new RPC agent.
    pub fn new(backend: Arc<dyn Backend>) -> Self {
        Self {
            backend,
            registry: Mutex::new(HashMap::new()),
            next_request_id: Mutex::new(1),
            buffered_responses: Mutex::new(HashMap::new()),
        }
    }

    /// Register a callable function.
    ///
    /// The handler receives serialized arguments and must return serialized
    /// results. If the registry lock is poisoned, this recovers the inner
    /// data and continues.
    pub fn register<F>(&self, name: &str, handler: F)
    where
        F: Fn(&[u8]) -> Result<Vec<u8>, String> + Send + Sync + 'static,
    {
        let mut reg = self.registry.lock().unwrap_or_else(|e| e.into_inner());
        reg.insert(name.to_string(), Arc::new(Box::new(handler)));
    }

    /// Look up a registered function by name.
    fn lookup(&self, name: &str) -> Option<Arc<RpcHandler>> {
        let reg = self.registry.lock().unwrap_or_else(|e| e.into_inner());
        reg.get(name).cloned()
    }

    /// Allocate a new unique request ID.
    fn next_id(&self) -> u64 {
        let mut id = self
            .next_request_id
            .lock()
            .unwrap_or_else(|e| e.into_inner());
        let current = *id;
        *id += 1;
        current
    }

    /// Invoke a function on a remote rank synchronously.
    ///
    /// Sends the request, then waits for a response with the matching
    /// `request_id`. If a response for a different request is received,
    /// it is buffered for later retrieval.
    ///
    /// # Errors
    ///
    /// Returns an error if the remote function is not found, if the remote
    /// handler returns an error, or if communication fails.
    pub fn rpc_sync(
        &self,
        dst_rank: usize,
        function_name: &str,
        args: &[u8],
    ) -> FerrotorchResult<Vec<u8>> {
        let request_id = self.next_id();
        let request = RpcRequest {
            request_id,
            function_name: function_name.to_string(),
            payload: args.to_vec(),
        };

        let serialized = request.serialize();
        self.backend.send(&serialized, dst_rank)?;

        // Wait for the response with the matching request_id.
        self.recv_response(dst_rank, request_id)
    }

    /// Receive a response matching the given `request_id` from `src_rank`.
    ///
    /// Checks the buffer first. If the response is not buffered, receives
    /// messages until the matching one arrives, buffering any non-matching
    /// responses along the way.
    fn recv_response(&self, src_rank: usize, expected_id: u64) -> FerrotorchResult<Vec<u8>> {
        // Check buffer first.
        {
            let mut buf = self
                .buffered_responses
                .lock()
                .unwrap_or_else(|e| e.into_inner());
            if let Some(resp) = buf.remove(&expected_id) {
                return self.process_response(resp);
            }
        }

        // Receive until we get the right response.
        loop {
            // Receive raw message. We allocate a maximum-size buffer for
            // the receive (we don't know the size ahead of time with the
            // Backend trait).
            let mut len_buf = [0u8; 8];
            // For simplicity with the Backend trait (which requires
            // pre-allocated buffers), we serialize the response with a
            // length prefix on the wire. But the Backend itself uses
            // length-prefixed messages too. So we receive the full
            // serialized response as a single message.
            //
            // For now, receive a reasonably-sized buffer. In practice the
            // send side sends the full serialized response as one Backend
            // message.
            let _ = len_buf; // unused — Backend handles framing

            // We need a different approach: the sender sends the full
            // serialized response via backend.send(), so we need to know
            // the size to allocate. Use a two-phase protocol:
            // Phase 1: receive 8-byte length prefix
            self.backend.recv(&mut len_buf, src_rank)?;
            let msg_len = u64::from_le_bytes(len_buf) as usize;

            if msg_len > MAX_RPC_MSG_SIZE {
                return Err(RpcError::InvalidMessage {
                    reason: format!(
                        "RPC response size {} exceeds maximum {} (1 GiB)",
                        msg_len, MAX_RPC_MSG_SIZE
                    ),
                }
                .into());
            }

            // Phase 2: receive the actual message
            let mut msg_buf = vec![0u8; msg_len];
            self.backend.recv(&mut msg_buf, src_rank)?;

            let response = RpcResponse::deserialize(&msg_buf).map_err(|e| {
                FerrotorchError::InvalidArgument {
                    message: format!("failed to deserialize RPC response: {e}"),
                }
            })?;

            if response.request_id == expected_id {
                return self.process_response(response);
            }

            // Buffer the non-matching response.
            let mut buf = self
                .buffered_responses
                .lock()
                .unwrap_or_else(|e| e.into_inner());
            buf.insert(response.request_id, response);
        }
    }

    /// Process a received response, converting errors.
    fn process_response(&self, response: RpcResponse) -> FerrotorchResult<Vec<u8>> {
        if let Some(err) = response.error {
            Err(FerrotorchError::InvalidArgument {
                message: format!("remote RPC error: {err}"),
            })
        } else {
            Ok(response.payload)
        }
    }

    /// Invoke a function on a remote rank asynchronously.
    ///
    /// Spawns a thread to perform the RPC call. Returns a join handle
    /// that can be used to retrieve the result.
    ///
    /// # Limitations
    ///
    /// **Spawns an unbounded number of OS threads.** Each call to `rpc_async`
    /// creates a new thread. This is acceptable for infrequent coordination
    /// RPCs but is not suitable for high-frequency patterns. A thread pool
    /// or async runtime would be needed for that use case.
    pub fn rpc_async(
        self: &Arc<Self>,
        dst_rank: usize,
        function_name: &str,
        args: &[u8],
    ) -> std::thread::JoinHandle<FerrotorchResult<Vec<u8>>> {
        let agent = Arc::clone(self);
        let name = function_name.to_string();
        let args = args.to_vec();
        std::thread::spawn(move || agent.rpc_sync(dst_rank, &name, &args))
    }

    /// Handle an incoming RPC request: look up the function, call it, and
    /// send the response back.
    pub fn handle_request(&self, src_rank: usize, request_data: &[u8]) -> FerrotorchResult<()> {
        let request = RpcRequest::deserialize(request_data).map_err(|e| {
            FerrotorchError::InvalidArgument {
                message: format!("failed to deserialize RPC request: {e}"),
            }
        })?;

        let response = match self.lookup(&request.function_name) {
            Some(handler) => match handler(&request.payload) {
                Ok(result) => RpcResponse {
                    request_id: request.request_id,
                    payload: result,
                    error: None,
                },
                Err(err) => RpcResponse {
                    request_id: request.request_id,
                    payload: Vec::new(),
                    error: Some(err),
                },
            },
            None => RpcResponse {
                request_id: request.request_id,
                payload: Vec::new(),
                error: Some(format!(
                    "function '{}' not registered on rank {}",
                    request.function_name,
                    self.backend.rank()
                )),
            },
        };

        // Send response with length prefix.
        let serialized = response.serialize();
        let len_bytes = (serialized.len() as u64).to_le_bytes();
        self.backend.send(&len_bytes, src_rank)?;
        self.backend.send(&serialized, src_rank)?;

        Ok(())
    }

    /// The rank of this agent.
    pub fn rank(&self) -> usize {
        self.backend.rank()
    }

    /// The world size.
    pub fn world_size(&self) -> usize {
        self.backend.world_size()
    }
}

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

    #[test]
    fn test_rpc_request_roundtrip() {
        let req = RpcRequest {
            request_id: 42,
            function_name: "add".to_string(),
            payload: vec![1, 2, 3],
        };
        let bytes = req.serialize();
        let req2 = RpcRequest::deserialize(&bytes).unwrap();
        assert_eq!(req2.request_id, 42);
        assert_eq!(req2.function_name, "add");
        assert_eq!(req2.payload, vec![1, 2, 3]);
    }

    #[test]
    fn test_rpc_response_roundtrip_ok() {
        let resp = RpcResponse {
            request_id: 7,
            payload: vec![4, 5, 6],
            error: None,
        };
        let bytes = resp.serialize();
        let resp2 = RpcResponse::deserialize(&bytes).unwrap();
        assert_eq!(resp2.request_id, 7);
        assert_eq!(resp2.payload, vec![4, 5, 6]);
        assert!(resp2.error.is_none());
    }

    #[test]
    fn test_rpc_response_roundtrip_error() {
        let resp = RpcResponse {
            request_id: 99,
            payload: Vec::new(),
            error: Some("something went wrong".into()),
        };
        let bytes = resp.serialize();
        let resp2 = RpcResponse::deserialize(&bytes).unwrap();
        assert_eq!(resp2.request_id, 99);
        assert_eq!(resp2.error.unwrap(), "something went wrong");
    }

    #[test]
    fn test_max_message_size_constant() {
        assert_eq!(MAX_RPC_MSG_SIZE, 1 << 30);
    }

    #[test]
    fn test_rpc_agent_register_lookup() {
        use crate::backend::SimulatedBackend;

        let group = SimulatedBackend::create_group(1).unwrap();
        let b: Arc<dyn Backend> = Arc::new(group.into_iter().next().unwrap());
        let agent = RpcAgent::new(b);

        agent.register("echo", |args| Ok(args.to_vec()));

        let handler = agent.lookup("echo");
        assert!(handler.is_some());

        let result = handler.unwrap()(b"hello");
        assert_eq!(result.unwrap(), b"hello");
    }

    #[test]
    fn test_rpc_agent_lookup_missing() {
        use crate::backend::SimulatedBackend;

        let group = SimulatedBackend::create_group(1).unwrap();
        let b: Arc<dyn Backend> = Arc::new(group.into_iter().next().unwrap());
        let agent = RpcAgent::new(b);

        assert!(agent.lookup("nonexistent").is_none());
    }
}