modelexpress-server 0.8.0

High-performance gRPC server for model serving and management
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
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

//! Backend-neutral gRPC service for the NCCL M2N collective control plane.

#![allow(clippy::result_large_err)] // tonic service helpers return tonic::Status.

use std::sync::Arc;

use modelexpress_common::grpc::refit_collective::{
    CollectiveGroup, CollectiveGroupMembership, CollectiveGroupSpec, CollectiveRole,
    CollectiveTransfer, CreateCollectiveTransferRequest, DeleteCollectiveTransferRequest,
    GetCollectiveGroupRequest, GetCollectiveTransferRequest, JoinCollectiveGroupRequest,
    PublishGroupBootstrapRequest, ReportCollectiveTransferRequest,
    refit_collective_service_server::RefitCollectiveService,
};
use tonic::{Request, Response, Status};

use super::backend::{CollectiveBackend, CollectiveBackendError};

/// Size of an `ncclUniqueId`. Checked here rather than at
/// `Communicator.init`, because a truncated identifier surfaces there as every
/// rank of the lane blocking rather than as an error.
const NCCL_UNIQUE_ID_BYTES: usize = 128;

fn required(value: &str, field: &str) -> Result<(), Status> {
    if value.trim().is_empty() {
        Err(Status::invalid_argument(format!("{field} is required")))
    } else {
        Ok(())
    }
}

/// Characters a slot id may not contain: the Redis records and lane encodings
/// split on them. Shared by the expected-slot and incoming-slot checks.
const SLOT_DELIMITERS: &[char] = &['\0', '\n', '\r', '|', ','];

fn delimiter_free(value: &str, field: &str, delimiters: &[char]) -> Result<(), Status> {
    if let Some(delimiter) = delimiters
        .iter()
        .find(|delimiter| value.contains(**delimiter))
    {
        return Err(Status::invalid_argument(format!(
            "{field} must not contain the reserved delimiter {:?}",
            delimiter
        )));
    }
    Ok(())
}

fn backend_status(error: CollectiveBackendError) -> Status {
    match error {
        CollectiveBackendError::InvalidArgument(message) => Status::invalid_argument(message),
        CollectiveBackendError::NotFound(message) => Status::not_found(message),
        CollectiveBackendError::FailedPrecondition(message) => Status::failed_precondition(message),
        CollectiveBackendError::AlreadyExists(message) => Status::already_exists(message),
        CollectiveBackendError::Internal(message) => Status::internal(message),
        CollectiveBackendError::Unavailable(message) => {
            Status::unavailable(format!("Collective metadata backend error: {message}"))
        }
    }
}

/// Validate the membership declaration every participant of one operation must
/// send identically.
fn validate_spec(spec: Option<&CollectiveGroupSpec>) -> Result<&CollectiveGroupSpec, Status> {
    let spec = spec.ok_or_else(|| Status::invalid_argument("spec is required"))?;
    required(&spec.model_name, "spec.model_name")?;
    delimiter_free(&spec.model_name, "spec.model_name", &['\0'])?;
    if spec.expected_trainer_slots.is_empty() {
        return Err(Status::invalid_argument(
            "spec.expected_trainer_slots must not be empty",
        ));
    }
    if spec.expected_generator_slots.is_empty() {
        return Err(Status::invalid_argument(
            "spec.expected_generator_slots must not be empty",
        ));
    }
    if spec.lanes.is_empty() {
        return Err(Status::invalid_argument(
            "spec.lanes must declare at least one lane",
        ));
    }
    // Duplicate slots would make the expected count disagree with the number
    // of distinct participants, so the group could never reach READY and the
    // failure would present as a timeout rather than as the typo it is.
    if has_duplicates(&spec.expected_trainer_slots) {
        return Err(Status::invalid_argument(
            "spec.expected_trainer_slots must not contain duplicates",
        ));
    }
    if has_duplicates(&spec.expected_generator_slots) {
        return Err(Status::invalid_argument(
            "spec.expected_generator_slots must not contain duplicates",
        ));
    }
    for (field, slots) in [
        ("spec.expected_trainer_slots", &spec.expected_trainer_slots),
        (
            "spec.expected_generator_slots",
            &spec.expected_generator_slots,
        ),
    ] {
        for slot in slots {
            required(slot, field)?;
            // `,` is the slot separator inside the stored lane set
            // (`encode_lanes`); a slot carrying one would decode as two.
            delimiter_free(slot, field, SLOT_DELIMITERS)?;
        }
    }
    if spec
        .expected_trainer_slots
        .iter()
        .any(|slot| spec.expected_generator_slots.contains(slot))
    {
        return Err(Status::invalid_argument(
            "trainer and generator slot namespaces must not overlap",
        ));
    }
    Ok(spec)
}

fn has_duplicates(slots: &[String]) -> bool {
    let mut sorted: Vec<&String> = slots.iter().collect();
    sorted.sort();
    sorted.windows(2).any(|pair| pair[0] == pair[1])
}

#[derive(Clone)]
pub struct RefitCollectiveServiceImpl {
    backend: Arc<dyn CollectiveBackend>,
}

impl RefitCollectiveServiceImpl {
    pub fn new(backend: Arc<dyn CollectiveBackend>) -> Self {
        Self { backend }
    }
}

#[tonic::async_trait]
impl RefitCollectiveService for RefitCollectiveServiceImpl {
    async fn create_collective_transfer(
        &self,
        request: Request<CreateCollectiveTransferRequest>,
    ) -> Result<Response<CollectiveTransfer>, Status> {
        let request = request.into_inner();
        validate_spec(request.spec.as_ref())?;
        required(&request.version_id, "version_id")?;
        required(&request.idempotency_key, "idempotency_key")?;
        if request.expected_epoch == 0 {
            return Err(Status::invalid_argument(
                "expected_epoch is required: name the READY group epoch to transfer at",
            ));
        }

        self.backend
            .create_transfer(&request)
            .await
            .map(Response::new)
            .map_err(backend_status)
    }

    async fn get_collective_transfer(
        &self,
        request: Request<GetCollectiveTransferRequest>,
    ) -> Result<Response<CollectiveTransfer>, Status> {
        let request = request.into_inner();
        required(&request.operation_id, "operation_id")?;
        self.backend
            .get_transfer(&request.operation_id)
            .await
            .map(Response::new)
            .map_err(backend_status)
    }

    async fn delete_collective_transfer(
        &self,
        request: Request<DeleteCollectiveTransferRequest>,
    ) -> Result<Response<CollectiveTransfer>, Status> {
        let request = request.into_inner();
        required(&request.operation_id, "operation_id")?;
        self.backend
            .delete_transfer(&request.operation_id)
            .await
            .map(Response::new)
            .map_err(backend_status)
    }

    async fn join_collective_group(
        &self,
        request: Request<JoinCollectiveGroupRequest>,
    ) -> Result<Response<CollectiveGroupMembership>, Status> {
        let request = request.into_inner();
        validate_spec(request.spec.as_ref())?;
        required(&request.slot_id, "slot_id")?;
        required(&request.worker_id, "worker_id")?;
        required(&request.plan_digest, "plan_digest")?;
        delimiter_free(&request.slot_id, "slot_id", SLOT_DELIMITERS)?;
        delimiter_free(&request.worker_id, "worker_id", &['\0', '|'])?;

        let role = CollectiveRole::try_from(request.role).unwrap_or(CollectiveRole::Unspecified);
        if role == CollectiveRole::Unspecified {
            return Err(Status::invalid_argument("role must be specified"));
        }
        if let Some(source) = request.plan_source.as_ref() {
            required(&source.worker_id, "plan_source.worker_id")?;
            required(&source.endpoint, "plan_source.endpoint")?;
            required(&source.digest, "plan_source.digest")?;
            // The digest MX advertises is what generators verify the fetched
            // plan against, so a source that advertises a different one would
            // make every generator fail closed at fetch time.
            if source.digest != request.plan_digest {
                return Err(Status::invalid_argument(
                    "plan_source.digest must match plan_digest",
                ));
            }
            if source.worker_id != request.worker_id {
                return Err(Status::invalid_argument(
                    "plan_source.worker_id must match worker_id",
                ));
            }
            if role != CollectiveRole::Trainer || request.index_in_role != 0 {
                return Err(Status::invalid_argument(
                    "only trainer index_in_role 0 may advertise plan_source",
                ));
            }
        }

        self.backend
            .join_group(&request)
            .await
            .map(Response::new)
            .map_err(backend_status)
    }

    async fn get_collective_group(
        &self,
        request: Request<GetCollectiveGroupRequest>,
    ) -> Result<Response<CollectiveGroup>, Status> {
        let request = request.into_inner();
        required(&request.group_id, "group_id")?;
        self.backend
            .get_group(&request.group_id)
            .await
            .map(Response::new)
            .map_err(backend_status)
    }

    async fn publish_group_bootstrap(
        &self,
        request: Request<PublishGroupBootstrapRequest>,
    ) -> Result<Response<CollectiveGroup>, Status> {
        let request = request.into_inner();
        required(&request.group_id, "group_id")?;
        required(&request.worker_id, "worker_id")?;
        if request.epoch == 0 {
            return Err(Status::invalid_argument(
                "epoch must be the group's current epoch",
            ));
        }
        if request.nccl_unique_id.len() != NCCL_UNIQUE_ID_BYTES {
            return Err(Status::invalid_argument(format!(
                "nccl_unique_id must be {NCCL_UNIQUE_ID_BYTES} bytes, got {}",
                request.nccl_unique_id.len()
            )));
        }

        self.backend
            .publish_bootstrap(&request)
            .await
            .map(Response::new)
            .map_err(backend_status)
    }

    async fn report_collective_transfer(
        &self,
        request: Request<ReportCollectiveTransferRequest>,
    ) -> Result<Response<CollectiveTransfer>, Status> {
        let request = request.into_inner();
        required(&request.operation_id, "operation_id")?;
        required(&request.group_id, "group_id")?;
        required(&request.worker_id, "worker_id")?;
        if request.epoch == 0 {
            return Err(Status::invalid_argument(
                "epoch must be the epoch the operation was admitted against",
            ));
        }
        if !request.succeeded && request.message.trim().is_empty() {
            return Err(Status::invalid_argument(
                "message is required for a failed report",
            ));
        }

        self.backend
            .report_transfer(&request)
            .await
            .map(Response::new)
            .map_err(backend_status)
    }
}

#[cfg(test)]
#[allow(clippy::expect_used)]
mod tests {
    use super::*;
    use modelexpress_common::grpc::refit_collective::{LaneKind, LaneSpec};

    fn spec() -> CollectiveGroupSpec {
        CollectiveGroupSpec {
            model_name: "m".to_string(),
            expected_trainer_slots: vec!["t0".to_string(), "t1".to_string()],
            expected_generator_slots: vec!["g0".to_string()],
            lanes: vec![LaneSpec {
                lane_id: 0,
                kind: LaneKind::Broadcast.into(),
                trainer_slots: vec!["t0".to_string(), "t1".to_string()],
                generator_slots: vec!["g0".to_string()],
            }],
        }
    }

    #[test]
    fn a_valid_spec_passes() {
        assert!(validate_spec(Some(&spec())).is_ok());
    }

    #[test]
    fn a_slot_id_carrying_the_lane_list_separator_is_rejected() {
        let mut s = spec();
        s.expected_trainer_slots[1] = "t,1".to_string();
        assert_eq!(
            validate_spec(Some(&s)).expect_err("comma in slot").code(),
            tonic::Code::InvalidArgument
        );
    }

    #[test]
    fn an_absent_spec_is_rejected() {
        assert_eq!(
            validate_spec(None).expect_err("absent spec").code(),
            tonic::Code::InvalidArgument
        );
    }

    #[test]
    fn empty_membership_is_rejected() {
        let mut s = spec();
        s.expected_trainer_slots.clear();
        assert!(validate_spec(Some(&s)).is_err());

        let mut s = spec();
        s.expected_generator_slots.clear();
        assert!(validate_spec(Some(&s)).is_err());

        let mut s = spec();
        s.lanes.clear();
        assert!(validate_spec(Some(&s)).is_err());

        let mut s = spec();
        s.model_name = "  ".to_string();
        assert!(validate_spec(Some(&s)).is_err());
    }

    #[test]
    fn duplicate_slots_are_rejected_rather_than_timing_out() {
        // A duplicated slot makes the expected count exceed the number of
        // distinct participants, so the group would sit in FORMING until the
        // client deadline instead of reporting the typo.
        let mut s = spec();
        s.expected_trainer_slots = vec!["t0".to_string(), "t0".to_string()];
        assert!(validate_spec(Some(&s)).is_err());

        let mut s = spec();
        s.expected_generator_slots = vec!["g0".to_string(), "g0".to_string()];
        assert!(validate_spec(Some(&s)).is_err());
    }

    #[test]
    fn duplicate_detection_does_not_reject_distinct_slots() {
        assert!(!has_duplicates(&[
            "a".to_string(),
            "b".to_string(),
            "c".to_string()
        ]));
        assert!(has_duplicates(&[
            "a".to_string(),
            "c".to_string(),
            "a".to_string()
        ]));
        assert!(!has_duplicates(&[]));
    }

    #[test]
    fn slots_must_not_repeat_across_roles() {
        // Redis admission is keyed by slot_id, so sharing a name across roles
        // would overwrite one participant and leave the group permanently short.
        let mut s = spec();
        s.expected_trainer_slots = vec!["r0".to_string()];
        s.expected_generator_slots = vec!["r0".to_string()];
        assert!(validate_spec(Some(&s)).is_err());
    }

    #[test]
    fn slots_reject_the_redis_record_delimiters() {
        let mut s = spec();
        s.expected_trainer_slots = vec!["trainer\n0".to_string()];
        assert!(validate_spec(Some(&s)).is_err());

        let mut s = spec();
        s.expected_generator_slots = vec!["generator|0".to_string()];
        assert!(validate_spec(Some(&s)).is_err());
    }
}