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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
use super::{
    align_up, invalid_plan, AllocationLifetime, BTreeMap, BTreeSet, BufferUsage,
    DynamicBackingPoolSpec, DynamicResourceDemand, DynamicResourceDescriptor,
    DynamicStorageProfile, ElementType, InvocationLivenessMode, InvocationResourceLiveness, NodeId,
    PlanNode, ProgramValueId, ProviderId, ProviderResourcePlan, RejectedProvider,
    ResolvedTensorSpec, ResolvedValueStorage, ResourceId, StateId, StateInitialization, VNextError,
};

#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) struct CanonicalValueBinding {
    pub(super) tensor: ResolvedTensorSpec,
    pub(super) usage: BufferUsage,
    pub(super) storage: ResolvedValueStorage,
}

pub(super) struct GlobalValueRange {
    pub(super) value_id: ProgramValueId,
    pub(super) offset_bytes: u64,
    pub(super) end_bytes: u64,
}

#[derive(Default)]
pub(super) struct StateDependencyTracker {
    pub(super) last_writer: BTreeMap<StateId, NodeId>,
    pub(super) readers_since_write: BTreeMap<StateId, BTreeSet<NodeId>>,
}

#[derive(Clone)]
pub(super) struct JointProviderCandidate {
    pub(super) resources: ProviderResourcePlan,
    pub(super) allowed_profiles: BTreeMap<ResourceId, BTreeSet<DynamicStorageProfile>>,
    pub(super) is_preferred: bool,
}

pub(super) struct JointProviderStorageSelection {
    pub(super) node_resources: BTreeMap<NodeId, ProviderResourcePlan>,
    pub(super) resource_profiles: BTreeMap<ResourceId, DynamicStorageProfile>,
    pub(super) storage_rejections: BTreeMap<NodeId, RejectedProvider>,
}

#[derive(Clone, Default)]
pub(super) struct JointPartialSelection {
    pub(super) chosen: Vec<ProviderResourcePlan>,
    pub(super) preferred: Vec<bool>,
}

pub(super) struct JointComponentSolution {
    pub(super) chosen: Vec<ProviderResourcePlan>,
    pub(super) resource_profiles: BTreeMap<ResourceId, DynamicStorageProfile>,
}

pub(super) struct JointSelectionObjective {
    pub(super) preferred: Vec<bool>,
    /// Counts for the least-preferred profile through profile rank 1. Rank 0
    /// has zero penalty, so candidate-specific extra rank-0 resources cannot
    /// bias selection merely by increasing the resource count.
    pub(super) profile_penalty: Vec<usize>,
    pub(super) provider_ids: Vec<ProviderId>,
}

impl JointSelectionObjective {
    pub(super) fn new(
        partial: &JointPartialSelection,
        profile_ranks: impl Iterator<Item = usize>,
        profile_count: usize,
    ) -> Result<Self, VNextError> {
        let mut profile_penalty = vec![0_usize; profile_count.saturating_sub(1)];
        for rank in profile_ranks {
            if rank >= profile_count {
                return Err(invalid_plan(
                    "joint storage objective contains an unknown profile rank",
                ));
            }
            if rank != 0 {
                let penalty_index = profile_count - 1 - rank;
                profile_penalty[penalty_index] = profile_penalty[penalty_index]
                    .checked_add(1)
                    .ok_or_else(|| invalid_plan("joint storage profile penalty overflows usize"))?;
            }
        }
        Ok(Self {
            preferred: partial.preferred.clone(),
            profile_penalty,
            provider_ids: partial
                .chosen
                .iter()
                .map(|resources| resources.provider_id().clone())
                .collect(),
        })
    }

    pub(super) fn precedes(&self, other: &Self) -> bool {
        match compare_preferred(&self.preferred, &other.preferred) {
            std::cmp::Ordering::Less => return true,
            std::cmp::Ordering::Greater => return false,
            std::cmp::Ordering::Equal => {}
        }
        match self.profile_penalty.cmp(&other.profile_penalty) {
            std::cmp::Ordering::Less => true,
            std::cmp::Ordering::Greater => false,
            std::cmp::Ordering::Equal => self.provider_ids < other.provider_ids,
        }
    }
}

pub(super) fn compare_preferred(left: &[bool], right: &[bool]) -> std::cmp::Ordering {
    for (left, right) in left.iter().zip(right) {
        match (*right as u8).cmp(&(*left as u8)) {
            std::cmp::Ordering::Equal => {}
            ordering => return ordering,
        }
    }
    left.len().cmp(&right.len())
}

pub(super) fn joint_partial_precedes(
    left: &JointPartialSelection,
    right: &JointPartialSelection,
) -> bool {
    match compare_preferred(&left.preferred, &right.preferred) {
        std::cmp::Ordering::Less => true,
        std::cmp::Ordering::Greater => false,
        std::cmp::Ordering::Equal => left
            .chosen
            .iter()
            .map(ProviderResourcePlan::provider_id)
            .cmp(right.chosen.iter().map(ProviderResourcePlan::provider_id))
            .is_lt(),
    }
}

pub(super) fn joint_candidate_components(
    candidate_sets: &[Vec<JointProviderCandidate>],
) -> Vec<Vec<usize>> {
    let mut parents = (0..candidate_sets.len()).collect::<Vec<_>>();
    let mut ranks = vec![0_u8; candidate_sets.len()];
    let mut resource_owner = BTreeMap::<ResourceId, usize>::new();
    for (node_index, candidates) in candidate_sets.iter().enumerate() {
        let resource_ids = candidates
            .iter()
            .flat_map(|candidate| candidate.allowed_profiles.keys().cloned())
            .collect::<BTreeSet<_>>();
        for resource_id in resource_ids {
            if let Some(owner) = resource_owner.insert(resource_id, node_index) {
                joint_union(&mut parents, &mut ranks, owner, node_index);
            }
        }
    }
    let mut components = BTreeMap::<usize, Vec<usize>>::new();
    for node_index in 0..candidate_sets.len() {
        let root = joint_find(&mut parents, node_index);
        components.entry(root).or_default().push(node_index);
    }
    let mut components = components.into_values().collect::<Vec<_>>();
    components.sort_by_key(|component| component[0]);
    components
}

pub(super) fn storage_incompatible_resource_ids(
    candidate: &JointProviderCandidate,
    selected_profiles: &BTreeMap<ResourceId, DynamicStorageProfile>,
) -> Vec<ResourceId> {
    candidate
        .allowed_profiles
        .iter()
        .filter(|(resource_id, accepted)| {
            selected_profiles
                .get(*resource_id)
                .is_some_and(|profile| !accepted.contains(profile))
        })
        .map(|(resource_id, _)| resource_id.clone())
        .collect()
}

pub(super) fn joint_find(parents: &mut [usize], value: usize) -> usize {
    let mut root = value;
    while parents[root] != root {
        root = parents[root];
    }
    let mut current = value;
    while parents[current] != current {
        let next = parents[current];
        parents[current] = root;
        current = next;
    }
    root
}

pub(super) fn joint_union(parents: &mut [usize], ranks: &mut [u8], left: usize, right: usize) {
    let left_root = joint_find(parents, left);
    let right_root = joint_find(parents, right);
    if left_root == right_root {
        return;
    }
    match ranks[left_root].cmp(&ranks[right_root]) {
        std::cmp::Ordering::Less => parents[left_root] = right_root,
        std::cmp::Ordering::Greater => parents[right_root] = left_root,
        std::cmp::Ordering::Equal => {
            parents[right_root] = left_root;
            ranks[left_root] = ranks[left_root].saturating_add(1);
        }
    }
}

#[derive(Default)]
pub(super) struct PoolAggregateEvidence {
    pub(super) minimum_request_bytes: u64,
    pub(super) minimum_sequence_bytes: u64,
    pub(super) minimum_step_bytes: u64,
    pub(super) minimum_invocation_peak_bytes: u64,
    pub(super) theoretical_ceiling_bytes: u128,
    pub(super) reusable_workspace_ceiling_bytes: u128,
}

impl PoolAggregateEvidence {
    pub(super) fn add(&mut self, pool: &DynamicBackingPoolSpec) -> Result<(), VNextError> {
        self.minimum_request_bytes = self
            .minimum_request_bytes
            .checked_add(pool.minimum_request_bytes)
            .ok_or_else(|| invalid_plan("aggregate pool request minimum overflows u64"))?;
        self.minimum_sequence_bytes = self
            .minimum_sequence_bytes
            .checked_add(pool.minimum_sequence_bytes)
            .ok_or_else(|| invalid_plan("aggregate pool sequence minimum overflows u64"))?;
        self.minimum_step_bytes = self
            .minimum_step_bytes
            .checked_add(pool.minimum_step_bytes)
            .ok_or_else(|| invalid_plan("aggregate pool step minimum overflows u64"))?;
        self.minimum_invocation_peak_bytes = self
            .minimum_invocation_peak_bytes
            .checked_add(pool.minimum_invocation_peak_bytes)
            .ok_or_else(|| invalid_plan("aggregate pool invocation minimum overflows u64"))?;
        self.theoretical_ceiling_bytes = self
            .theoretical_ceiling_bytes
            .checked_add(pool.theoretical_ceiling_bytes.get())
            .ok_or_else(|| invalid_plan("aggregate pool theoretical ceiling overflows u128"))?;
        self.reusable_workspace_ceiling_bytes = self
            .reusable_workspace_ceiling_bytes
            .checked_add(u128::from(pool.reusable_workspace_ceiling_bytes))
            .ok_or_else(|| invalid_plan("aggregate reusable workspace ceiling overflows u128"))?;
        Ok(())
    }
}

pub(super) fn minimum_for_lifetime(
    descriptors: &[&DynamicResourceDescriptor],
    lifetime: AllocationLifetime,
    overflow_context: &'static str,
) -> Result<u64, VNextError> {
    descriptors
        .iter()
        .filter(|descriptor| descriptor.lifetime == lifetime)
        .try_fold(0_u64, |total, descriptor| {
            total
                .checked_add(descriptor.minimum_request_bytes()?)
                .ok_or_else(|| invalid_plan(format!("{overflow_context} overflows u64")))
        })
}

pub(super) fn node_completion_precedes(
    nodes_by_id: &BTreeMap<NodeId, &PlanNode>,
    predecessor: &NodeId,
    successor: &NodeId,
) -> Result<bool, VNextError> {
    if predecessor == successor {
        return Ok(false);
    }
    let successor = nodes_by_id
        .get(successor)
        .ok_or_else(|| invalid_plan("completion-order successor node is missing"))?;
    let mut pending = successor.dependencies.clone();
    let mut visited = BTreeSet::new();
    while let Some(node_id) = pending.pop() {
        if &node_id == predecessor {
            return Ok(true);
        }
        if !visited.insert(node_id.clone()) {
            continue;
        }
        let node = nodes_by_id
            .get(&node_id)
            .ok_or_else(|| invalid_plan("completion-order dependency node is missing"))?;
        pending.extend(node.dependencies.iter().cloned());
    }
    Ok(false)
}

pub(super) fn validate_pool_liveness_rows(
    mode: InvocationLivenessMode,
    rows: &[InvocationResourceLiveness],
    invocation_ids: &BTreeSet<ResourceId>,
    descriptors: &BTreeMap<ResourceId, &DynamicResourceDescriptor>,
) -> Result<u64, VNextError> {
    if invocation_ids.is_empty() {
        if mode != InvocationLivenessMode::NoInvocationResources || !rows.is_empty() {
            return Err(invalid_plan(
                "pool without invocation resources has liveness evidence",
            ));
        }
        return Ok(0);
    }
    if mode == InvocationLivenessMode::NoInvocationResources || rows.is_empty() {
        return Err(invalid_plan(
            "invocation pool is missing typed liveness evidence",
        ));
    }
    let mut covered = BTreeSet::new();
    let mut maximum_row = 0_u64;
    let mut concurrent_sum = 0_u64;
    for row in rows {
        if row.resource_ids.is_empty() || row.resource_ids.windows(2).any(|pair| pair[0] >= pair[1])
        {
            return Err(invalid_plan(
                "pool invocation liveness row is empty or non-canonical",
            ));
        }
        let row_bytes = row
            .resource_ids
            .iter()
            .try_fold(0_u64, |total, resource_id| {
                if !invocation_ids.contains(resource_id) {
                    return Err(invalid_plan(
                        "pool liveness references a non-member invocation resource",
                    ));
                }
                covered.insert(resource_id.clone());
                total
                    .checked_add(
                        descriptors
                            .get(resource_id)
                            .ok_or_else(|| invalid_plan("pool liveness descriptor is missing"))?
                            .minimum_request_bytes()?,
                    )
                    .ok_or_else(|| invalid_plan("pool invocation row bytes overflow u64"))
            })?;
        maximum_row = maximum_row.max(row_bytes);
        concurrent_sum = concurrent_sum
            .checked_add(row_bytes)
            .ok_or_else(|| invalid_plan("pool concurrent invocation bytes overflow u64"))?;
    }
    if covered != *invocation_ids {
        return Err(invalid_plan(
            "pool liveness does not cover every invocation member",
        ));
    }
    Ok(match mode {
        InvocationLivenessMode::NoInvocationResources => unreachable!(),
        InvocationLivenessMode::TotalOrderReuse => maximum_row,
        InvocationLivenessMode::ConservativeConcurrent => concurrent_sum,
    })
}

pub(super) struct ValueAllocationAccumulator {
    pub(super) end_bytes: u64,
    pub(super) alignment_bytes: u64,
    pub(super) usage: BufferUsage,
    pub(super) element_type: ElementType,
    pub(super) demand: ValueResourceDemand,
    pub(super) initialization: StateInitialization,
    pub(super) logical_layout_fingerprints: BTreeSet<String>,
    pub(super) merge_result: Option<()>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum ValueResourceDemand {
    PlanStatic,
    Fixed {
        lifetime: AllocationLifetime,
    },
    ParticipantFixed {
        lifetime: AllocationLifetime,
        maximum_participants: u32,
    },
    TokenScaled {
        lifetime: AllocationLifetime,
        bytes_per_token: u64,
        maximum_tokens: u64,
    },
}

impl ValueResourceDemand {
    pub(super) fn lifetime(self) -> Option<AllocationLifetime> {
        match self {
            Self::PlanStatic => None,
            Self::Fixed { lifetime }
            | Self::ParticipantFixed { lifetime, .. }
            | Self::TokenScaled { lifetime, .. } => Some(lifetime),
        }
    }

    pub(super) fn dynamic_demand(
        self,
        fixed_bytes: u64,
        alignment_bytes: u64,
    ) -> Result<DynamicResourceDemand, VNextError> {
        match self {
            Self::PlanStatic => Err(invalid_plan(
                "plan-static value cannot produce a dynamic demand",
            )),
            Self::Fixed { .. } => DynamicResourceDemand::fixed(fixed_bytes),
            Self::ParticipantFixed {
                maximum_participants,
                ..
            } => DynamicResourceDemand::actual_sequences(
                align_up(fixed_bytes, alignment_bytes)?,
                maximum_participants,
            ),
            Self::TokenScaled {
                bytes_per_token,
                maximum_tokens,
                ..
            } => DynamicResourceDemand::tokens(bytes_per_token, maximum_tokens),
        }
    }
}

impl ValueAllocationAccumulator {
    pub(super) fn merge(
        &mut self,
        end_bytes: u64,
        alignment_bytes: u64,
        usage: BufferUsage,
        element_type: ElementType,
        demand: ValueResourceDemand,
        initialization: StateInitialization,
        logical_layout_fingerprint: String,
    ) -> Option<()> {
        if self.usage != usage
            || self.element_type != element_type
            || self.demand != demand
            || self.initialization != initialization
        {
            return None;
        }
        self.end_bytes = self.end_bytes.max(end_bytes);
        self.alignment_bytes = self.alignment_bytes.max(alignment_bytes);
        self.logical_layout_fingerprints
            .insert(logical_layout_fingerprint);
        Some(())
    }
}