tenferro-runtime 0.2.0

Concrete tensor helpers, traced tensors, graph execution, and extension runtime support for tenferro.
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
//! Backend-parametric runtime dispatch for extension ops.
//!
//! This module is intentionally generic: extension crates can register an
//! executor for a family and keep runtime cache state outside both the
//! semantic [`ExtensionOp`] payload and the
//! tensor backend implementation.

use std::collections::HashMap;
use std::fmt::{self, Debug};
use std::marker::PhantomData;
use std::sync::Arc;

use tenferro_ops::ext_op::ExtensionOp;
use tenferro_tensor::{CacheStats, Tensor, TensorBackend, TensorRead};

use crate::extension_cache::{ExtensionCacheLimits, ExtensionCacheSelector, ExtensionCacheStore};

/// Errors returned by backend-parametric extension runtime registries.
#[derive(Debug, thiserror::Error)]
pub enum ExtensionRuntimeRegistryError {
    /// The `family_id` does not match the namespaced format
    /// `"<crate-name>.<op-name>.v<major>"`.
    #[error("family_id {family_id:?} does not match the namespaced format")]
    MalformedFamilyId { family_id: &'static str },
    /// A registry lock was poisoned by a panic in another thread.
    #[error("{name} poisoned")]
    PoisonedLock { name: &'static str },
}

/// Backend and cache state passed to one extension execution.
pub struct ExtensionExecutionContext<'a, B: TensorBackend> {
    backend: &'a mut B,
    caches: &'a mut ExtensionCacheStore,
}

impl<B: TensorBackend> fmt::Debug for ExtensionExecutionContext<'_, B> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ExtensionExecutionContext")
            .field("backend_type", &std::any::type_name::<B>())
            .field("caches", &self.caches)
            .finish_non_exhaustive()
    }
}

impl<'a, B: TensorBackend> ExtensionExecutionContext<'a, B> {
    /// Build a context from externally-owned backend and cache state.
    pub fn new(backend: &'a mut B, caches: &'a mut ExtensionCacheStore) -> Self {
        Self { backend, caches }
    }

    /// Borrow the backend for non-mutating inspection.
    pub fn backend(&self) -> &B {
        self.backend
    }

    /// Borrow the backend mutably for extension execution.
    pub fn backend_mut(&mut self) -> &mut B {
        self.backend
    }

    /// Borrow the extension runtime cache store.
    pub fn caches(&self) -> &ExtensionCacheStore {
        self.caches
    }

    /// Borrow the extension runtime cache store mutably.
    pub fn caches_mut(&mut self) -> &mut ExtensionCacheStore {
        self.caches
    }

    /// Execute a core-only execution program one instruction at a time.
    ///
    /// This is for extension runtimes that lower their own operation into a
    /// temporary `ExecProgram` containing only core tensor ops. Nested
    /// `ExecOp::Extension` instructions are rejected so extension dispatch
    /// cannot bypass the owning runtime registry.
    ///
    /// # Examples
    ///
    /// ```
    /// use tenferro_cpu::CpuBackend;
    /// use tenferro_ops::dim_expr::DimExpr;
    /// use tenferro_runtime::extension::{ExecInstruction, ExecOp, ExecProgram};
    /// use tenferro_runtime::{DType, ExtensionCacheStore, ExtensionExecutionContext, Tensor};
    ///
    /// let program = ExecProgram {
    ///     instructions: vec![ExecInstruction {
    ///         op: ExecOp::Add,
    ///         input_slots: vec![0, 1],
    ///         output_slots: vec![2],
    ///         dtype: DType::F64,
    ///         output_shapes: vec![vec![]].into(),
    ///         output_extents: vec![vec![]].into(),
    ///         last_use: vec![true, true],
    ///     }],
    ///     input_slots: vec![0, 1],
    ///     output_slots: vec![2],
    ///     n_slots: 3,
    /// };
    /// let lhs = Tensor::from_vec_col_major(vec![], vec![1.0_f64]).unwrap();
    /// let rhs = Tensor::from_vec_col_major(vec![], vec![2.0_f64]).unwrap();
    ///
    /// let mut backend = CpuBackend::new();
    /// let mut caches = ExtensionCacheStore::new();
    /// let mut ctx = ExtensionExecutionContext::new(&mut backend, &mut caches);
    /// let outputs = ctx
    ///     .execute_core_exec_program_unsegmented(&program, vec![lhs, rhs])
    ///     .unwrap();
    /// assert_eq!(outputs[0].as_slice::<f64>().unwrap(), &[3.0]);
    /// ```
    pub fn execute_core_exec_program_unsegmented(
        &mut self,
        program: &crate::extension::ExecProgram,
        inputs: Vec<Tensor>,
    ) -> crate::error::Result<Vec<Tensor>>
    where
        B: 'static,
    {
        crate::exec::ensure_core_exec_program(
            program,
            "ExtensionExecutionContext::execute_core_exec_program_unsegmented",
        )?;
        crate::exec::eval_exec_ir_unsegmented_with_cache(self.backend, program, inputs)
    }

    /// Borrow backend and extension cache store as disjoint mutable parts.
    pub fn parts_mut(&mut self) -> (&mut B, &mut ExtensionCacheStore) {
        (self.backend, self.caches)
    }
}

/// A backend-specific runtime executor for one extension family.
pub trait ExtensionRuntime<B: TensorBackend + 'static>: Debug + Send + Sync + 'static {
    /// Extension family handled by this executor.
    fn family_id(&self) -> &'static str;

    /// Execute the extension op with backend and cache state supplied by core.
    fn execute(
        &self,
        op: &dyn ExtensionOp,
        inputs: &[&Tensor],
        ctx: &mut ExtensionExecutionContext<'_, B>,
    ) -> tenferro_tensor::Result<Vec<Tensor>>;

    /// Execute the extension op on borrowed tensor reads.
    ///
    /// Implementations that need compact tensors must materialize inputs here
    /// explicitly. Keeping this method required prevents implicit read-path
    /// fallbacks from hiding backend or view handling bugs.
    fn execute_reads(
        &self,
        op: &dyn ExtensionOp,
        inputs: &[TensorRead<'_>],
        ctx: &mut ExtensionExecutionContext<'_, B>,
    ) -> tenferro_tensor::Result<Vec<Tensor>>;
}

fn validate_runtime_output_count(
    op: &dyn ExtensionOp,
    outputs: Vec<Tensor>,
) -> tenferro_tensor::Result<Vec<Tensor>> {
    let expected = op.output_count();
    if outputs.len() != expected {
        return Err(tenferro_tensor::Error::InvalidConfig {
            op: "extension",
            message: format!(
                "family_id {:?}: runtime returned {} outputs but op declared {} outputs",
                op.family_id(),
                outputs.len(),
                expected
            ),
        });
    }
    Ok(outputs)
}

fn validate_runtime_input_count(
    op: &dyn ExtensionOp,
    actual: usize,
) -> tenferro_tensor::Result<()> {
    let expected = op.input_count();
    if actual != expected {
        return Err(tenferro_tensor::Error::InvalidConfig {
            op: "extension",
            message: format!(
                "family_id {:?}: op expects {} inputs, got {}",
                op.family_id(),
                expected,
                actual
            ),
        });
    }
    Ok(())
}

/// Registry of backend-specific extension runtime executors.
pub struct ExtensionRegistry<B: TensorBackend + 'static> {
    executors: HashMap<&'static str, Arc<dyn ExtensionRuntime<B>>>,
}

impl<B: TensorBackend + 'static> fmt::Debug for ExtensionRegistry<B> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut families = self.executors.keys().copied().collect::<Vec<_>>();
        families.sort_unstable();
        f.debug_struct("ExtensionRegistry")
            .field("backend_type", &std::any::type_name::<B>())
            .field("len", &self.executors.len())
            .field("families", &families)
            .finish_non_exhaustive()
    }
}

impl<B: TensorBackend + 'static> ExtensionRegistry<B> {
    /// Create an empty extension runtime registry.
    ///
    /// # Examples
    ///
    /// ```
    /// use tenferro_runtime::ExtensionRegistry;
    /// use tenferro_cpu::CpuBackend;
    ///
    /// let registry = ExtensionRegistry::<CpuBackend>::new();
    /// assert!(!registry.contains("example.identity.v1"));
    /// ```
    pub fn new() -> Self {
        Self {
            executors: HashMap::new(),
        }
    }

    /// Register one runtime executor.
    ///
    /// Registration is idempotent by family id: registering the same extension
    /// family more than once succeeds and keeps the first runtime. This lets
    /// extension crates register their own dependency extensions defensively.
    pub fn register(
        &mut self,
        executor: Arc<dyn ExtensionRuntime<B>>,
    ) -> Result<(), ExtensionRuntimeRegistryError> {
        let family_id = executor.family_id();
        if !is_valid_family_id(family_id) {
            return Err(ExtensionRuntimeRegistryError::MalformedFamilyId { family_id });
        }
        if self.executors.contains_key(family_id) {
            return Ok(());
        }
        self.executors.insert(family_id, executor);
        Ok(())
    }

    /// Look up an executor by extension family id.
    pub fn get(&self, family_id: &str) -> Option<Arc<dyn ExtensionRuntime<B>>> {
        self.executors.get(family_id).cloned()
    }

    /// Return whether an executor is registered for `family_id`.
    pub fn contains(&self, family_id: &str) -> bool {
        self.executors.contains_key(family_id)
    }

    /// Number of registered runtime executors.
    pub fn len(&self) -> usize {
        self.executors.len()
    }

    /// Return whether no runtime executors are registered.
    pub fn is_empty(&self) -> bool {
        self.executors.is_empty()
    }
}

impl<B: TensorBackend + 'static> Default for ExtensionRegistry<B> {
    fn default() -> Self {
        Self::new()
    }
}

/// Runtime owner for backend-specific extension dispatch and caches.
pub struct ExtensionExecutor<B: TensorBackend + 'static> {
    registry: ExtensionRegistry<B>,
    caches: ExtensionCacheStore,
    _backend: PhantomData<fn() -> B>,
}

impl<B: TensorBackend + 'static> fmt::Debug for ExtensionExecutor<B> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ExtensionExecutor")
            .field("backend_type", &std::any::type_name::<B>())
            .field("registry", &self.registry)
            .field("caches", &self.caches)
            .finish_non_exhaustive()
    }
}

impl<B: TensorBackend + 'static> ExtensionExecutor<B> {
    /// Create an executor with an empty registry and default cache limits.
    ///
    /// # Examples
    ///
    /// ```
    /// use tenferro_runtime::ExtensionExecutor;
    /// use tenferro_cpu::CpuBackend;
    ///
    /// let executor = ExtensionExecutor::<CpuBackend>::new();
    /// assert_eq!(executor.cache_stats().entries, 0);
    /// ```
    pub fn new() -> Self {
        Self {
            registry: ExtensionRegistry::new(),
            caches: ExtensionCacheStore::new(),
            _backend: PhantomData,
        }
    }

    /// Create an executor from explicit registry and cache store.
    pub fn with_parts(registry: ExtensionRegistry<B>, caches: ExtensionCacheStore) -> Self {
        Self {
            registry,
            caches,
            _backend: PhantomData,
        }
    }

    /// Borrow the runtime executor registry.
    pub fn registry(&self) -> &ExtensionRegistry<B> {
        &self.registry
    }

    /// Borrow the runtime executor registry mutably.
    pub fn registry_mut(&mut self) -> &mut ExtensionRegistry<B> {
        &mut self.registry
    }

    /// Borrow the extension cache store.
    pub fn caches(&self) -> &ExtensionCacheStore {
        &self.caches
    }

    /// Borrow the extension cache store mutably.
    pub fn caches_mut(&mut self) -> &mut ExtensionCacheStore {
        &mut self.caches
    }

    /// Execute an extension using a registered runtime executor.
    pub fn execute(
        &mut self,
        backend: &mut B,
        op: &dyn ExtensionOp,
        inputs: &[&Tensor],
    ) -> tenferro_tensor::Result<Vec<Tensor>> {
        validate_runtime_input_count(op, inputs.len())?;
        let Some(executor) = self.registry.get(op.family_id()) else {
            return Err(tenferro_tensor::Error::InvalidConfig {
                op: "extension",
                message: format!(
                    "missing runtime for family_id {:?}; register the extension on this runtime owner, for example `executor.register_extension(<extension_crate>::register_runtime)` or `eager_runtime.register_extension(<extension_crate>::register_runtime)`",
                    op.family_id()
                ),
            });
        };
        let mut ctx = ExtensionExecutionContext::new(backend, &mut self.caches);
        validate_runtime_output_count(op, executor.execute(op, inputs, &mut ctx)?)
    }

    /// Execute an extension using borrowed tensor reads.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::any::Any;
    /// use std::hash::Hasher;
    /// use std::sync::Arc;
    ///
    /// use tenferro_cpu::CpuBackend;
    /// use tenferro_ops::{ext_op::ExtensionOp, SymDim};
    /// use tenferro_runtime::{
    ///     DType, ExtensionExecutionContext, ExtensionExecutor, ExtensionRuntime, Tensor,
    /// };
    /// use tenferro_tensor::TensorRead;
    ///
    /// #[derive(Clone, Debug)]
    /// struct IdentityOp;
    ///
    /// impl ExtensionOp for IdentityOp {
    ///     fn family_id(&self) -> &'static str {
    ///         "example.identity.v1"
    ///     }
    ///
    ///     fn payload_hash(&self, _hasher: &mut dyn Hasher) {}
    ///
    ///     fn payload_eq(&self, other: &dyn ExtensionOp) -> bool {
    ///         other.as_any().is::<IdentityOp>()
    ///     }
    ///
    ///     fn clone_arc(&self) -> Arc<dyn ExtensionOp> {
    ///         Arc::new(self.clone())
    ///     }
    ///
    ///     fn as_any(&self) -> &dyn Any {
    ///         self
    ///     }
    ///
    ///     fn input_count(&self) -> usize {
    ///         1
    ///     }
    ///
    ///     fn output_count(&self) -> usize {
    ///         1
    ///     }
    ///
    ///     fn infer_output_meta(
    ///         &self,
    ///         input_dtypes: &[DType],
    ///         input_shapes: &[&[SymDim]],
    ///     ) -> tenferro_tensor::Result<Vec<(DType, Vec<SymDim>)>> {
    ///         Ok(vec![(input_dtypes[0], input_shapes[0].to_vec())])
    ///     }
    ///
    ///     fn eager_execute(&self, inputs: &[&Tensor]) -> tenferro_tensor::Result<Vec<Tensor>> {
    ///         Ok(vec![inputs[0].clone()])
    ///     }
    /// }
    ///
    /// #[derive(Debug)]
    /// struct IdentityRuntime;
    ///
    /// impl ExtensionRuntime<CpuBackend> for IdentityRuntime {
    ///     fn family_id(&self) -> &'static str {
    ///         "example.identity.v1"
    ///     }
    ///
    ///     fn execute(
    ///         &self,
    ///         op: &dyn ExtensionOp,
    ///         inputs: &[&Tensor],
    ///         _ctx: &mut ExtensionExecutionContext<'_, CpuBackend>,
    ///     ) -> tenferro_tensor::Result<Vec<Tensor>> {
    ///         op.eager_execute(inputs)
    ///     }
    ///
    ///     fn execute_reads(
    ///         &self,
    ///         op: &dyn ExtensionOp,
    ///         inputs: &[TensorRead<'_>],
    ///         ctx: &mut ExtensionExecutionContext<'_, CpuBackend>,
    ///     ) -> tenferro_tensor::Result<Vec<Tensor>> {
    ///         let materialized_inputs: Vec<Tensor> = inputs
    ///             .iter()
    ///             .map(TensorRead::to_tensor)
    ///             .collect::<tenferro_tensor::Result<_>>()?;
    ///         let input_refs: Vec<&Tensor> = materialized_inputs.iter().collect();
    ///         self.execute(op, &input_refs, ctx)
    ///     }
    /// }
    ///
    /// let mut executor = ExtensionExecutor::<CpuBackend>::new();
    /// executor.registry_mut().register(Arc::new(IdentityRuntime))?;
    /// let input = Tensor::from_vec_col_major(vec![2], vec![1.0_f64, 2.0]).unwrap();
    /// let read = TensorRead::from_tensor(&input);
    /// let mut backend = CpuBackend::new();
    ///
    /// let outputs = executor.execute_reads(&mut backend, &IdentityOp, &[read])?;
    ///
    /// assert_eq!(outputs[0].as_slice::<f64>().unwrap(), &[1.0, 2.0]);
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn execute_reads(
        &mut self,
        backend: &mut B,
        op: &dyn ExtensionOp,
        inputs: &[TensorRead<'_>],
    ) -> tenferro_tensor::Result<Vec<Tensor>> {
        validate_runtime_input_count(op, inputs.len())?;
        let Some(executor) = self.registry.get(op.family_id()) else {
            return Err(tenferro_tensor::Error::InvalidConfig {
                op: "extension",
                message: format!(
                    "missing runtime for family_id {:?}; register the extension on this runtime owner, for example `executor.register_extension(<extension_crate>::register_runtime)` or `eager_runtime.register_extension(<extension_crate>::register_runtime)`",
                    op.family_id()
                ),
            });
        };
        let mut ctx = ExtensionExecutionContext::new(backend, &mut self.caches);
        validate_runtime_output_count(op, executor.execute_reads(op, inputs, &mut ctx)?)
    }

    /// Clear every runtime extension cache entry.
    pub fn clear_caches(&mut self) {
        self.caches.clear();
    }

    /// Return extension cache stats for all entries.
    pub fn cache_stats(&self) -> CacheStats {
        self.caches.stats(ExtensionCacheSelector::All)
    }

    /// Return the extension cache retention limits.
    pub fn cache_limits(&self) -> ExtensionCacheLimits {
        self.caches.limits()
    }

    /// Replace extension cache retention limits.
    pub fn set_cache_limits(&mut self, limits: ExtensionCacheLimits) {
        self.caches.set_limits(limits);
    }
}

impl<B: TensorBackend + 'static> Default for ExtensionExecutor<B> {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests;

fn is_valid_family_id(family_id: &str) -> bool {
    let mut parts = family_id.rsplitn(2, '.');
    let Some(version_part) = parts.next() else {
        return false;
    };
    let Some(prefix) = parts.next() else {
        return false;
    };
    if !version_part.starts_with('v') {
        return false;
    }
    let digits = &version_part[1..];
    if digits.is_empty() || !digits.chars().all(|c| c.is_ascii_digit()) {
        return false;
    }
    let Some((crate_name, op_name)) = prefix.split_once('.') else {
        return false;
    };
    if crate_name.is_empty() || op_name.is_empty() {
        return false;
    }
    let any_invalid = |s: &str| s.chars().any(|c| c.is_whitespace() || !c.is_ascii());
    !any_invalid(crate_name) && !any_invalid(op_name)
}