tenferro_fft/backend.rs
1use std::fmt;
2
3use tenferro_runtime::ExtensionCacheStore;
4use tenferro_tensor::{BackendSession, Tensor, TensorRead};
5
6use crate::{FftPlanCache, FftPlanSpec};
7
8#[derive(Clone, Copy, Debug)]
9enum FftCacheOwner {
10 CallerOwned,
11 RuntimeOwned,
12}
13
14/// Execution-cache state supplied to an [`FftBackend`].
15///
16/// Direct repeated calls use a caller-owned [`FftPlanCache`], while traced
17/// execution uses the owning runtime's [`ExtensionCacheStore`]. Both ownership
18/// paths expose the same bounded typed store to a backend, so CPU, Metal, CUDA,
19/// and future implementations can retain private plans or workspaces in their
20/// own cache namespace. Constructors keep the owner representation closed.
21///
22/// # Examples
23///
24/// ```
25/// use tenferro_fft::{FftExecutionCache, FftPlanCache};
26///
27/// let mut plans = FftPlanCache::default();
28/// let cache = FftExecutionCache::caller_owned(&mut plans);
29/// assert!(format!("{cache:?}").contains("CallerOwned"));
30/// ```
31pub struct FftExecutionCache<'a> {
32 owner: FftCacheOwner,
33 store: &'a mut ExtensionCacheStore,
34}
35
36impl fmt::Debug for FftExecutionCache<'_> {
37 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38 f.debug_struct("FftExecutionCache")
39 .field("owner", &self.owner)
40 .field(
41 "stats",
42 &self
43 .store
44 .stats(tenferro_runtime::ExtensionCacheSelector::All),
45 )
46 .finish_non_exhaustive()
47 }
48}
49
50impl<'a> FftExecutionCache<'a> {
51 /// Build a context backed by a caller-owned typed FFT execution cache.
52 pub fn caller_owned(cache: &'a mut FftPlanCache) -> Self {
53 Self {
54 owner: FftCacheOwner::CallerOwned,
55 store: cache.store_mut(),
56 }
57 }
58
59 /// Build a context backed by an extension runtime cache store.
60 pub fn runtime_owned(cache: &'a mut ExtensionCacheStore) -> Self {
61 Self {
62 owner: FftCacheOwner::RuntimeOwned,
63 store: cache,
64 }
65 }
66
67 /// Borrow the bounded typed store owned by the caller or extension runtime.
68 ///
69 /// Backend implementations should use a stable family/cache namespace and
70 /// include every plan-identity field in the key discriminator. The store
71 /// owns LRU bounds, typed retrieval, clear behavior, entry counts, and the
72 /// retained-byte estimates supplied at insertion.
73 pub fn store_mut(&mut self) -> &mut ExtensionCacheStore {
74 self.store
75 }
76}
77
78/// Explicit backend capability required by concrete and traced FFT execution.
79///
80/// Implementations must execute on the input's existing placement. Unsupported
81/// dtypes, layouts, placements, or operations return an error; they must never
82/// transfer the tensor or select a different backend.
83///
84/// # Examples
85/// Backend surface required by the FFT extension runtime and the built-in
86/// concrete FFT dispatch.
87///
88/// This is a backend SPI: third-party backend implementers implement this
89/// trait on their execution sessions to provide FFT capability. The concrete
90/// [`TensorFftExt`]/[`TensorReadFftExt`] methods and [`FftExecutor`] take an
91/// erased `&mut dyn BackendSession` and dispatch **internally** to the
92/// built-in CPU/CUDA/WebGPU execution sessions; a session without this
93/// capability returns a typed `Error::Unsupported` instead of being accepted
94/// as an arbitrary third-party backend (issue #1680 Phase 3).
95///
96/// # Examples
97///
98/// ```
99/// use tenferro_cpu::CpuExecSession;
100/// use tenferro_fft::FftBackend;
101///
102/// fn accepts_fft_backend<B: FftBackend>() {}
103///
104/// accepts_fft_backend::<CpuExecSession<'static>>();
105/// ```
106///
107/// A generic tensor backend does not expose the FFT capability at the SPI
108/// level:
109///
110/// ```compile_fail
111/// use tenferro_tensor::{Tensor, TensorBackend};
112/// use tenferro_fft::FftBackend;
113///
114/// fn requires_fft_backend<B: TensorBackend>(backend: &mut B) {
115/// let _: &mut dyn FftBackend = backend;
116/// }
117/// ```
118pub trait FftBackend: BackendSession {
119 /// Validate a borrowed runtime input before the extension read path
120 /// materializes it into a compact tensor.
121 ///
122 /// Backends that can materialize their own placement may keep the default
123 /// no-op. CPU overrides this to report device inputs as unsupported FFT
124 /// placement errors instead of leaking a generic host-materialization error.
125 ///
126 /// # Errors
127 ///
128 /// Returns [`tenferro_tensor::Error::Unsupported`] when this backend cannot
129 /// consume the borrowed input placement without an explicit transfer.
130 fn validate_fft_read_input(
131 &self,
132 _op: &'static str,
133 _input: &TensorRead<'_>,
134 ) -> tenferro_tensor::Result<()> {
135 Ok(())
136 }
137
138 /// Execute one validated FFT request on `input`'s existing placement.
139 ///
140 /// # Errors
141 ///
142 /// Returns [`tenferro_tensor::Error::Unsupported`] for an unsupported
143 /// operation, dtype, layout, or placement;
144 /// [`tenferro_tensor::Error::Validation`] for inconsistent input/spec
145 /// metadata or checked shape arithmetic; and a typed backend or runtime
146 /// source when plan creation, cache access, or execution fails.
147 fn execute_fft(
148 &mut self,
149 input: &Tensor,
150 spec: &FftPlanSpec,
151 cache: FftExecutionCache<'_>,
152 ) -> tenferro_tensor::Result<Tensor>;
153}
154
155#[cfg(test)]
156mod tests {
157 use super::*;
158
159 #[test]
160 fn execution_cache_debug_identifies_both_owners_and_exposes_the_store() {
161 let mut caller = FftPlanCache::default();
162 let mut caller_cache = FftExecutionCache::caller_owned(&mut caller);
163 assert!(format!("{caller_cache:?}").contains("CallerOwned"));
164 assert_eq!(
165 caller_cache
166 .store_mut()
167 .stats(tenferro_runtime::ExtensionCacheSelector::All)
168 .entries,
169 0
170 );
171
172 let mut runtime = ExtensionCacheStore::default();
173 let mut runtime_cache = FftExecutionCache::runtime_owned(&mut runtime);
174 assert!(format!("{runtime_cache:?}").contains("RuntimeOwned"));
175 assert_eq!(
176 runtime_cache
177 .store_mut()
178 .stats(tenferro_runtime::ExtensionCacheSelector::All)
179 .entries,
180 0
181 );
182 }
183}