eredu-runtime 0.5.0

Backend-neutral model execution runtime for Eredu
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
//! Requirement-driven synthesis of exact backend mechanism capabilities.

use eredu_checkpoint::{LinearFormat, SourceTensorEncoding, StoredDtype};
use eredu_core::{
    cache::{StateComponentPolicy, StateResidencyClass},
    checkpoint::TensorDtype,
    SessionCapabilities,
};
use eredu_nn::NeuralOperatorCapabilities;

use crate::{
    AddressableStorageCapabilities, BackendMechanismCapabilities, CacheResidencyPolicy,
    GroupedOperationRequirement, ReplicatedTextParameterRole, ReplicatedTextRequirements,
    ReplicatedTextSelectionRequest, StateComponentMechanism, StateComponentPlacement, StateLayout,
    StateMechanismCapabilities, StateStorageDtype, WeightLoweringCapability,
    WeightLoweringDescriptor, WeightLoweringKind, WeightResidencyMechanism,
};

/// Collection-independent facilities of a backend's mutable-state implementation.
#[derive(Debug, Clone, Copy, Default, Eq, PartialEq)]
pub struct StateLifecycleCapabilities {
    checkpoint: bool,
    rollback: bool,
    reset: bool,
    prompt_cache: bool,
    observation_retention: bool,
}

impl StateLifecycleCapabilities {
    /// Creates a fail-closed declaration of state lifecycle facilities.
    pub const fn new() -> Self {
        Self {
            checkpoint: false,
            rollback: false,
            reset: false,
            prompt_cache: false,
            observation_retention: false,
        }
    }

    /// Declares exact checkpoint and rollback support.
    pub const fn with_transactions(mut self, checkpoint: bool, rollback: bool) -> Self {
        self.checkpoint = checkpoint;
        self.rollback = rollback;
        self
    }

    /// Declares complete reset support.
    pub const fn with_reset(mut self, supported: bool) -> Self {
        self.reset = supported;
        self
    }

    /// Declares persistence and restoration support for the requested state policy.
    pub const fn with_prompt_cache(mut self, supported: bool) -> Self {
        self.prompt_cache = supported;
        self
    }

    /// Declares retention of state components through observed submissions.
    pub const fn with_observation_retention(mut self, supported: bool) -> Self {
        self.observation_retention = supported;
        self
    }
}

/// Exact backend facts independent of architecture parameter and state collections.
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct BackendMechanismFacts {
    operators: NeuralOperatorCapabilities,
    weight_residencies: Vec<WeightResidencyMechanism>,
    state: StateLifecycleCapabilities,
    session: SessionCapabilities,
    prompt_cache: bool,
    exact_completion: bool,
    grouped_operations: Vec<GroupedOperationRequirement>,
    indexed_movement: bool,
    addressable_storage: Option<AddressableStorageCapabilities>,
}

impl BackendMechanismFacts {
    /// Declares neural, ordinary residency, and state lifecycle mechanisms.
    /// Optional facilities remain absent until explicitly declared.
    pub fn new(
        operators: NeuralOperatorCapabilities,
        weight_residencies: impl IntoIterator<Item = WeightResidencyMechanism>,
        state: StateLifecycleCapabilities,
    ) -> Self {
        Self {
            operators,
            weight_residencies: weight_residencies.into_iter().collect(),
            state,
            session: SessionCapabilities::default(),
            prompt_cache: false,
            exact_completion: false,
            grouped_operations: Vec::new(),
            indexed_movement: false,
            addressable_storage: None,
        }
    }

    /// Declares the exact capabilities of the resulting session implementation.
    pub const fn with_session(mut self, session: SessionCapabilities) -> Self {
        self.session = session;
        self
    }

    /// Declares prompt-cache persistence mechanisms.
    pub const fn with_prompt_cache(mut self, supported: bool) -> Self {
        self.prompt_cache = supported;
        self
    }

    /// Declares exact completion ownership.
    pub const fn with_exact_completion(mut self, supported: bool) -> Self {
        self.exact_completion = supported;
        self
    }

    /// Declares grouped operation mechanisms.
    pub fn with_grouped_operations(
        mut self,
        operations: impl IntoIterator<Item = GroupedOperationRequirement>,
    ) -> Self {
        self.grouped_operations = operations.into_iter().collect();
        self
    }

    /// Declares indexed discovery, slicing, remapping, and concatenation.
    pub const fn with_indexed_movement(mut self, supported: bool) -> Self {
        self.indexed_movement = supported;
        self
    }

    /// Declares independently addressable storage and exact capacity limits.
    pub const fn with_addressable_storage(
        mut self,
        capabilities: AddressableStorageCapabilities,
    ) -> Self {
        self.addressable_storage = Some(capabilities);
        self
    }
}

/// Side-effect-free native support queries for neutral requirements.
///
/// Implementations report facts about one descriptor or state component. They
/// must not open payloads, allocate tensors, create native resources, or select
/// an architecture. Requirement enumeration and semantic validation belong to
/// [`synthesize_replicated_text_capabilities`].
pub trait ReplicatedTextMechanismSupport {
    /// Reports collection-independent facts for the requested state implementation.
    fn facts(&self, state_policy: &CacheResidencyPolicy) -> BackendMechanismFacts;

    /// Reports native realization of one semantically valid direct descriptor.
    fn supports_direct(&self, descriptor: &WeightLoweringDescriptor) -> bool;

    /// Reports native realization of one semantically valid transform descriptor.
    fn supports_transform(&self, descriptor: &WeightLoweringDescriptor) -> bool;

    /// Resolves native floating-state storage from one architecture-selected source dtype.
    /// Packed embeddings may produce a different native scalar representation.
    /// This metadata-only query must not inspect names or acquire payloads.
    fn floating_state_dtype(&self, source: &TensorDtype) -> Option<StateStorageDtype>;

    /// Reports support for the exact component geometry, native storage dtype, and placement.
    fn supports_state_component(
        &self,
        component: &StateComponentPolicy,
        storage_dtype: StateStorageDtype,
        placement: StateComponentPlacement,
    ) -> bool;
}

/// Enumerates exact lowering and state mechanisms in deterministic requirement order.
///
/// Invalid architecture transforms are left for the selector's structured
/// diagnostic; they never become backend support queries. Absent and tied
/// parameters introduce no descriptor. Equal descriptors retain their first
/// supported occurrence, including across primary and auxiliary requirements.
pub fn synthesize_replicated_text_capabilities(
    requirements: &ReplicatedTextRequirements,
    request: &ReplicatedTextSelectionRequest,
    support: &impl ReplicatedTextMechanismSupport,
) -> BackendMechanismCapabilities {
    let facts = support.facts(request.state());
    let mut weight_lowerings = Vec::new();
    for parameter in requirements
        .parameters()
        .iter()
        .chain(requirements.auxiliary_parameters())
    {
        if !parameter.has_lowering_source() {
            continue;
        }
        let requested = request
            .quantization()
            .and_then(|requested| parameter.transform_target(requested).ok().flatten())
            .map(|target| target.executable());
        for executable in std::iter::once(parameter.native_executable()).chain(requested) {
            let Ok(descriptor) = parameter.lowering_descriptor(executable) else {
                continue;
            };
            if parameter.role() == ReplicatedTextParameterRole::LinearWeight
                && matches!(
                    descriptor.source(),
                    SourceTensorEncoding::Safetensors(StoredDtype::U8)
                )
                && executable == LinearFormat::Dense
            {
                continue;
            }
            let kind = if executable == parameter.native_executable()
                && descriptor.has_valid_direct_geometry()
                && support.supports_direct(&descriptor)
            {
                Some(WeightLoweringKind::Direct)
            } else if descriptor.has_valid_transform_geometry()
                && support.supports_transform(&descriptor)
            {
                Some(WeightLoweringKind::Transform)
            } else {
                None
            };
            if let Some(kind) = kind {
                let capability = WeightLoweringCapability::new(descriptor, kind);
                if !weight_lowerings.contains(&capability) {
                    weight_lowerings.push(capability);
                }
            }
        }
    }

    let floating_dtype = requirements
        .floating_state_source()
        .and_then(|source| support.floating_state_dtype(source))
        .filter(|dtype| dtype.is_floating());
    let mut state =
        synthesize_state_components(requirements.state_layout(), facts.state, |component| {
            let Some(dtype) = StateStorageDtype::resolve(component.dtype(), floating_dtype) else {
                return (None, None);
            };
            let device = support
                .supports_state_component(component, dtype, StateComponentPlacement::Device)
                .then_some(StateComponentPlacement::Device);
            let paged = match component.residency() {
                StateResidencyClass::SealablePaged => support
                    .supports_state_component(component, dtype, StateComponentPlacement::Paged)
                    .then_some(StateComponentPlacement::Paged),
                StateResidencyClass::AlwaysDeviceMutable
                | StateResidencyClass::LayerScopedOffloadable => device,
            };
            (device, paged)
        });

    if let (Some(source), Some(dtype)) = (requirements.floating_state_source(), floating_dtype) {
        state = state.with_floating_state_dtype(source.clone(), dtype);
    }
    let capabilities = BackendMechanismCapabilities::new(
        facts.operators,
        weight_lowerings,
        facts.weight_residencies,
        state,
    )
    .with_session(facts.session)
    .with_prompt_cache(facts.prompt_cache)
    .with_exact_completion(facts.exact_completion)
    .with_grouped_operations(facts.grouped_operations)
    .with_indexed_movement(facts.indexed_movement);
    match facts.addressable_storage {
        Some(storage) => capabilities.with_addressable_storage(storage),
        None => capabilities,
    }
}

/// Shared collection traversal; native providers answer only one component at a time.
pub(crate) fn synthesize_state_components(
    layout: &StateLayout,
    lifecycle: StateLifecycleCapabilities,
    placements: impl Fn(
        &StateComponentPolicy,
    ) -> (
        Option<StateComponentPlacement>,
        Option<StateComponentPlacement>,
    ),
) -> StateMechanismCapabilities {
    StateMechanismCapabilities::new((0..layout.len()).flat_map(|layer| {
        let placements = &placements;
        layout
            .components(layer)
            .expect("validated state layout exposes every layer")
            .iter()
            .filter_map(move |component| {
                let (device, paged) = placements(component);
                (device.is_some() || paged.is_some())
                    .then(|| StateComponentMechanism::new(layer, component.clone(), device, paged))
            })
    }))
    .with_transactions(lifecycle.checkpoint, lifecycle.rollback)
    .with_reset(lifecycle.reset)
    .with_prompt_cache(lifecycle.prompt_cache)
    .with_observation_retention(lifecycle.observation_retention)
}

impl WeightLoweringDescriptor {
    /// Validates source encoding geometry without choosing a native implementation.
    pub fn has_valid_direct_geometry(&self) -> bool {
        if self.executable().validate().is_err() {
            return false;
        }
        if self.executable() != LinearFormat::Dense
            && (self.packed_axis().is_none()
                || self.packed_axis() != self.logical_shape().len().checked_sub(1))
        {
            return false;
        }
        let same_unpacked_dimensions = |packed_axis: usize| {
            self.physical_shape()
                .iter()
                .zip(self.logical_shape())
                .enumerate()
                .all(|(axis, (physical, logical))| axis == packed_axis || physical == logical)
        };
        match self.source() {
            SourceTensorEncoding::Gguf { ggml_type, .. } => ggml_type
                .block_and_bytes()
                .ok()
                .and_then(|(block, _)| usize::try_from(block).ok())
                .is_some_and(|block| match self.packed_axis() {
                    Some(axis) if same_unpacked_dimensions(axis) => {
                        let physical = self.physical_shape()[axis];
                        let logical = self.logical_shape()[axis];
                        physical >= logical
                            && physical.is_multiple_of(block)
                            && physical - logical < block
                    }
                    Some(_) => false,
                    None => self.physical_shape() == self.logical_shape(),
                }),
            SourceTensorEncoding::Safetensors(StoredDtype::U32)
            | SourceTensorEncoding::RecipeOutput(StoredDtype::U32) => {
                let Some(axis) = self.packed_axis() else {
                    return false;
                };
                if !same_unpacked_dimensions(axis) {
                    return false;
                }
                let bits = match self.executable() {
                    LinearFormat::Affine(format) => usize::try_from(format.bits).ok(),
                    LinearFormat::MxFp4 => Some(4),
                    _ => None,
                };
                bits.is_some_and(|bits| {
                    self.physical_shape()[axis]
                        .checked_mul(32)
                        .zip(self.logical_shape()[axis].checked_mul(bits))
                        .is_some_and(|(physical, logical)| physical == logical)
                }) && self.has_valid_packed_geometry()
            }
            SourceTensorEncoding::Safetensors(StoredDtype::U8)
            | SourceTensorEncoding::RecipeOutput(StoredDtype::U8)
                if self.executable() == LinearFormat::MxFp4 =>
            {
                self.physical_shape() == self.logical_shape()
            }
            _ => {
                self.physical_shape() == self.logical_shape()
                    && (self.executable() == LinearFormat::Dense
                        || self.has_valid_packed_geometry())
            }
        }
    }

    /// Validates an unpacked source and the selected transform's packing geometry.
    pub fn has_valid_transform_geometry(&self) -> bool {
        self.physical_shape() == self.logical_shape()
            && self.packed_axis() == self.logical_shape().len().checked_sub(1)
            && self.has_valid_packed_geometry()
    }

    fn has_valid_packed_geometry(&self) -> bool {
        if self.executable() != LinearFormat::Dense
            && (self.packed_axis().is_none()
                || self.packed_axis() != self.logical_shape().len().checked_sub(1))
        {
            return false;
        }
        let Some(extent) = self.packed_extent() else {
            return false;
        };
        match self.executable() {
            LinearFormat::Affine(format) => {
                format.validate().is_ok()
                    && usize::try_from(format.group_size)
                        .ok()
                        .is_some_and(|group| extent.is_multiple_of(group))
            }
            LinearFormat::MxFp4 => extent.is_multiple_of(32),
            LinearFormat::GgufIQuant { ggml_type, .. } => ggml_type
                .block_and_bytes()
                .ok()
                .and_then(|(block, _)| usize::try_from(block).ok())
                .is_some_and(|block| extent.is_multiple_of(block)),
            LinearFormat::E4M3BlockFp8(format) => format.validate().is_ok(),
            LinearFormat::Dense => true,
        }
    }
}

#[cfg(test)]
mod tests;