ferrum-interfaces 0.8.4

Core trait contracts for the Ferrum LLM inference engine
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
use super::{
    expected_lease_transition, invalid_resource, AdmissionFitPolicy, AdmissionPressureAction, Arc,
    BTreeSet, BufferDescriptor, CoreOwnedAllocation, DeviceRuntime, DynamicResourceShape,
    ResourceAllocation, ResourceId, ResourceLeaseAction, ResourceLeaseEntry, ResourceLeaseState,
    ResourceOwnedBuffer, ResourceReservation, ResourceReservationBatch,
    ResourceTransactionIdentity, ResourceWorkShape, StaticProvisioningBinding, VNextError,
};

pub(super) struct OwnedLeaseSlot<B> {
    pub(super) entry: ResourceLeaseEntry,
    pub(super) actual_resource_id: Option<ResourceId>,
    pub(super) actual_generation: Option<u64>,
    pub(super) descriptor: Option<BufferDescriptor>,
    pub(super) buffer: Option<B>,
}

impl<B> OwnedLeaseSlot<B> {
    pub(super) fn new(reservation: &ResourceReservation) -> Self {
        Self {
            entry: ResourceLeaseEntry::from_reservation(reservation, ResourceLeaseState::Active),
            actual_resource_id: None,
            actual_generation: None,
            descriptor: None,
            buffer: None,
        }
    }

    pub(super) fn install(&mut self, allocation: CoreOwnedAllocation<B>) {
        self.actual_resource_id = Some(allocation.resource_id);
        self.actual_generation = Some(allocation.generation);
        self.descriptor = Some(allocation.descriptor);
        self.buffer = Some(allocation.buffer);
    }

    pub(super) fn clear(&mut self) {
        drop(self.buffer.take());
        self.descriptor.take();
        self.actual_resource_id.take();
        self.actual_generation.take();
    }

    pub(super) fn take_allocation(&mut self) -> Option<CoreOwnedAllocation<B>> {
        Some(CoreOwnedAllocation {
            resource_id: self.actual_resource_id.take()?,
            generation: self.actual_generation.take()?,
            descriptor: self.descriptor.take()?,
            buffer: self.buffer.take()?,
        })
    }

    pub(super) fn restore_allocation(&mut self, allocation: CoreOwnedAllocation<B>) {
        debug_assert!(self.buffer.is_none());
        self.install(allocation);
    }
}

/// Borrowed access to a live, active, generation-bound committed buffer.
pub struct LeasedBufferView<'a, B> {
    pub(super) identity: &'a ResourceTransactionIdentity,
    pub(super) admission: &'a StaticProvisioningBinding,
    pub(super) resource_id: &'a ResourceId,
    pub(super) generation: u64,
    pub(super) descriptor: &'a BufferDescriptor,
    pub(super) buffer: &'a B,
}

impl<'a, B> LeasedBufferView<'a, B> {
    pub fn identity(&self) -> &ResourceTransactionIdentity {
        self.identity
    }

    pub fn admission(&self) -> &StaticProvisioningBinding {
        self.admission
    }

    pub fn resource_id(&self) -> &ResourceId {
        self.resource_id
    }

    pub const fn generation(&self) -> u64 {
        self.generation
    }

    pub fn committed_descriptor(&self) -> &BufferDescriptor {
        self.descriptor
    }

    pub fn buffer(&self) -> &B {
        self.buffer
    }
}

#[must_use = "a resource lease is the batch owner of committed buffers"]
pub struct StaticProvisioningLease<R>
where
    R: DeviceRuntime,
{
    pub(super) slots: Vec<OwnedLeaseSlot<R::Buffer>>,
    pub(super) identity: ResourceTransactionIdentity,
    pub(super) admission: StaticProvisioningBinding,
    // Backend context drops after all static and dynamic buffers.
    pub(super) runtime: Arc<R>,
}

impl<R> StaticProvisioningLease<R>
where
    R: DeviceRuntime,
{
    pub(crate) fn runtime(&self) -> &Arc<R> {
        &self.runtime
    }

    pub(super) fn new(
        runtime: Arc<R>,
        identity: &ResourceTransactionIdentity,
        admission: &StaticProvisioningBinding,
        reservations: &ResourceReservationBatch,
    ) -> Self {
        Self {
            slots: reservations
                .reservations()
                .iter()
                .map(OwnedLeaseSlot::new)
                .collect(),
            identity: identity.clone(),
            admission: admission.clone(),
            runtime,
        }
    }

    pub fn identity(&self) -> &ResourceTransactionIdentity {
        &self.identity
    }

    pub fn admission(&self) -> &StaticProvisioningBinding {
        &self.admission
    }

    pub fn state(&self) -> ResourceLeaseState {
        let mut states = self
            .slots
            .iter()
            .filter(|slot| slot.buffer.is_some())
            .map(|slot| slot.entry.state);
        let Some(first) = states.next() else {
            return ResourceLeaseState::Cancelled;
        };
        if states.all(|state| state == first) {
            first
        } else {
            ResourceLeaseState::Mixed
        }
    }

    pub fn entries(&self) -> impl Iterator<Item = &ResourceLeaseEntry> {
        self.slots
            .iter()
            .filter(|slot| slot.buffer.is_some())
            .map(|slot| &slot.entry)
    }

    pub fn plan_static_entries(&self) -> impl Iterator<Item = &ResourceLeaseEntry> {
        self.slots
            .iter()
            .filter(|slot| slot.buffer.is_some())
            .map(|slot| &slot.entry)
    }

    pub(crate) fn view(
        &self,
        resource_id: &ResourceId,
        generation: u64,
    ) -> Result<LeasedBufferView<'_, R::Buffer>, VNextError> {
        let slot = self
            .slots
            .iter()
            .find(|slot| {
                slot.entry.resource_id == *resource_id && slot.entry.generation == generation
            })
            .ok_or_else(|| invalid_resource("lease does not contain that resource generation"))?;
        if slot.entry.state != ResourceLeaseState::Active {
            return Err(VNextError::InvalidLeaseTransition {
                lease_id: self.identity.transaction_id.to_string(),
                from: slot.entry.state.as_str(),
                action: "borrow_live_buffer",
            });
        }
        let descriptor = slot
            .descriptor
            .as_ref()
            .ok_or_else(|| invalid_resource("lease resource is not committed"))?;
        let buffer = slot
            .buffer
            .as_ref()
            .ok_or_else(|| invalid_resource("lease resource buffer is not live"))?;
        Ok(LeasedBufferView {
            identity: &self.identity,
            admission: &self.admission,
            resource_id: &slot.entry.resource_id,
            generation: slot.entry.generation,
            descriptor,
            buffer,
        })
    }

    /// Borrows the plan-static slot selected by the immutable memory plan.
    /// The slot index is prepared once from canonical allocation order; live
    /// generation, descriptor, and buffer ownership are still checked here.
    pub(crate) fn plan_static_view(
        &self,
        slot_index: usize,
        allocation: &ResourceAllocation,
    ) -> Result<LeasedBufferView<'_, R::Buffer>, VNextError> {
        let slot = self
            .slots
            .get(slot_index)
            .ok_or_else(|| invalid_resource("plan-static slot index is out of range"))?;
        let descriptor = slot
            .descriptor
            .as_ref()
            .ok_or_else(|| invalid_resource("plan-static resource is not committed"))?;
        let buffer = slot
            .buffer
            .as_ref()
            .ok_or_else(|| invalid_resource("plan-static resource buffer is not live"))?;
        if slot.entry.resource_id() != allocation.resource_id()
            || slot.entry.size_bytes() != allocation.size_bytes()
            || slot.entry.alignment_bytes() != allocation.alignment_bytes()
            || slot.entry.usage() != allocation.usage()
            || slot.entry.element_type() != allocation.element_type()
            || slot.entry.generation() == 0
            || slot.entry.state() != ResourceLeaseState::Active
            || slot.actual_resource_id.as_ref() != Some(allocation.resource_id())
            || slot.actual_generation != Some(slot.entry.generation())
            || descriptor.resource_id != *allocation.resource_id()
            || descriptor.size_bytes != allocation.size_bytes()
            || descriptor.alignment_bytes != allocation.alignment_bytes()
            || descriptor.usage != allocation.usage()
            || descriptor.element_type != allocation.element_type()
        {
            return Err(invalid_resource(
                "plan-static slot differs from its immutable allocation",
            ));
        }
        Ok(LeasedBufferView {
            identity: &self.identity,
            admission: &self.admission,
            resource_id: &slot.entry.resource_id,
            generation: slot.entry.generation,
            descriptor,
            buffer,
        })
    }

    pub(super) fn buffer(&self, order: usize) -> Option<&R::Buffer> {
        self.slots.get(order).and_then(|slot| slot.buffer.as_ref())
    }

    pub(super) fn install(&mut self, order: usize, allocation: CoreOwnedAllocation<R::Buffer>) {
        self.slots[order].install(allocation);
    }

    pub(super) fn clear(&mut self, order: usize) {
        self.slots[order].clear();
    }

    pub(super) fn transition_subset(
        &mut self,
        orders: &[usize],
        action: ResourceLeaseAction,
    ) -> Result<
        (
            ResourceLeaseState,
            ResourceLeaseState,
            Vec<ResourceLeaseEntry>,
        ),
        VNextError,
    > {
        if orders.is_empty() {
            return Err(invalid_resource(
                "lease transition subset must not be empty",
            ));
        }
        let mut unique = BTreeSet::new();
        let mut common_before = None;
        for &order in orders {
            if !unique.insert(order) {
                return Err(invalid_resource("lease transition subset is duplicated"));
            }
            let slot = self
                .slots
                .get(order)
                .ok_or_else(|| invalid_resource("lease transition order is out of bounds"))?;
            if slot.buffer.is_none() {
                return Err(invalid_resource(
                    "lease transition targets a non-live buffer",
                ));
            }
            let before = slot.entry.state;
            if common_before.is_some_and(|common| common != before) {
                return Err(invalid_resource(
                    "one lease receipt cannot hide heterogeneous before states",
                ));
            }
            if expected_lease_transition(action, before).is_none() {
                return Err(VNextError::InvalidLeaseTransition {
                    lease_id: self.identity.transaction_id.to_string(),
                    from: before.as_str(),
                    action: action.as_str(),
                });
            }
            common_before = Some(before);
        }
        let before = common_before.expect("non-empty subset has a before state");
        let after = expected_lease_transition(action, before)
            .expect("lease subset was preflight validated");
        for &order in orders {
            self.slots[order].entry.state = after;
        }
        Ok((
            before,
            after,
            orders
                .iter()
                .map(|&order| self.slots[order].entry.clone())
                .collect(),
        ))
    }

    pub(super) fn take_owned_buffers(
        &mut self,
        reservations: &ResourceReservationBatch,
    ) -> Vec<ResourceOwnedBuffer<R::Buffer>> {
        self.slots
            .iter_mut()
            .zip(reservations.reservations())
            .enumerate()
            .filter_map(|(order, (slot, reservation))| {
                let allocation = slot.take_allocation()?;
                Some(ResourceOwnedBuffer {
                    order,
                    expected_resource_id: reservation.resource_id.clone(),
                    actual_resource_id: allocation.resource_id,
                    expected_generation: reservation.generation,
                    actual_generation: allocation.generation,
                    expected_descriptor: BufferDescriptor {
                        resource_id: reservation.resource_id.clone(),
                        size_bytes: reservation.size_bytes,
                        alignment_bytes: reservation.alignment_bytes,
                        usage: reservation.usage,
                        element_type: reservation.element_type,
                    },
                    actual_descriptor: allocation.descriptor,
                    buffer: allocation.buffer,
                })
            })
            .collect()
    }

    pub(super) fn restore_owned_buffers(&mut self, buffers: Vec<ResourceOwnedBuffer<R::Buffer>>) {
        for buffer in buffers {
            let (order, allocation) = buffer.into_allocation();
            self.slots[order].restore_allocation(allocation);
        }
    }
}

macro_rules! scoped_resource_admission_request {
    ($name:ident, $single_sequence:literal) => {
        #[derive(Debug, Clone, PartialEq, Eq)]
        pub struct $name {
            pub(super) work_shape: ResourceWorkShape,
            pub(super) fit_policy: AdmissionFitPolicy,
            pub(super) pressure_action: AdmissionPressureAction,
        }

        impl $name {
            pub fn new(
                work_shape: ResourceWorkShape,
                fit_policy: AdmissionFitPolicy,
                pressure_action: AdmissionPressureAction,
            ) -> Result<Self, VNextError> {
                if $single_sequence
                    && (work_shape.immediate_sequences() != 1 || work_shape.fit_sequences() != 1)
                {
                    return Err(invalid_resource(
                        "sequence resource admission requires a single-sequence shape",
                    ));
                }
                Ok(Self {
                    work_shape,
                    fit_policy,
                    pressure_action,
                })
            }

            pub fn work_shape(&self) -> &ResourceWorkShape {
                &self.work_shape
            }

            pub(crate) const fn immediate_shape(&self) -> DynamicResourceShape {
                self.work_shape.immediate_shape()
            }

            pub(crate) const fn fit_shape(&self) -> DynamicResourceShape {
                match self.fit_policy {
                    AdmissionFitPolicy::ImmediateOnly => self.work_shape.immediate_shape(),
                    AdmissionFitPolicy::FullInputMustFit => self.work_shape.fit_shape(),
                }
            }

            pub const fn fit_policy(&self) -> AdmissionFitPolicy {
                self.fit_policy
            }

            pub const fn pressure_action(&self) -> AdmissionPressureAction {
                self.pressure_action
            }
        }
    };
}

scoped_resource_admission_request!(RequestResourceAdmissionRequest, false);
scoped_resource_admission_request!(SequenceResourceAdmissionRequest, true);