incin-core 0.1.0

Core tensor operations and static shape checking for incin
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
//! The single production path from a typed operation to native execution.
//!
//! FND-004 froze one exact identity, one typed descriptor and one capability
//! row per operation, but nothing outside the test suite consumed them: the
//! stable tensor surface still called the operation-family traits directly.
//! This module is the missing production consumer, and the properties FND-005
//! requires of it are all structural rather than conventional:
//!
//! - **Support is explicit.** `B: Execute<O>` is a compile-time
//!   fact, and the exact capability row is queried before launch. A backend
//!   that has not implemented an operation cannot be asked to run it, and a
//!   backend that implements it for one dtype cannot be asked for another.
//! - **Validation precedes execution.** The descriptor is validated against
//!   the real storage metadata, not against what a caller claims.
//! - **Nothing silently returns unsupported.** There is no default method to
//!   fall through; a refusal is a typed [`UnsupportedReason`].
//! - **Outputs are derived, not accepted.** The caller never states output
//!   metadata, so it cannot fabricate any.
//! - **Capture keeps the same descriptor.** The value handed to the backend is
//!   the value a compiler would record.

use alloc::vec::Vec;
use core::fmt;

use crate::err::BackendError;
use crate::exec::capability::{
    Capabilities, CapabilityQuery, OperationIdentity, SupportLevel, UnsupportedReason,
};
use crate::exec::catalog::{DescriptorError, LogicalTensorMeta, Operation};
use crate::exec::context::ExecutionContext;
use crate::exec::meta::TensorMeta;
use crate::exec::policy::FallbackPolicy;

use crate::exec::request::TensorHandle;
use crate::tensor::backend::{Execute, ExecutionRequest};

/// Why a canonical invocation did not produce a value.
///
/// The variants are kept apart on purpose. A [`DescriptorError`] means the
/// request was never legal, [`PolicyViolation`] means its requested support is
/// disallowed before launch, and a [`BackendError`] means a legal request
/// failed at or after launch. Collapsing them would lose the distinction that
/// decides whether the caller, policy, or device is at fault.
#[derive(Debug, Clone, PartialEq)]
pub enum CanonicalError {
    /// The invocation failed contract validation before any backend ran.
    Descriptor(DescriptorError),
    /// A valid invocation required support the execution policy does not allow.
    Policy(PolicyViolation),
    /// The backend refused or failed a validated invocation.
    Backend(BackendError),
}

impl fmt::Display for CanonicalError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Descriptor(error) => write!(f, "{error}"),
            Self::Policy(error) => write!(f, "{error}"),
            Self::Backend(error) => write!(f, "{error}"),
        }
    }
}

/// A capability level that a valid invocation reported but its policy denies.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PolicyViolation {
    /// The exact built-in or custom operation whose reported support was denied.
    pub operation: OperationIdentity,
    /// The support level reported by the capability query.
    pub support: SupportLevel,
    /// The fallback policy effective for this invocation.
    pub fallback: FallbackPolicy,
}

impl fmt::Display for PolicyViolation {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "operation {} reports {:?} support, denied by fallback policy {}",
            self.operation.display_name(),
            self.support,
            self.fallback.as_str()
        )
    }
}

#[cfg(feature = "std")]
impl std::error::Error for CanonicalError {}

impl From<DescriptorError> for CanonicalError {
    fn from(error: DescriptorError) -> Self {
        Self::Descriptor(error)
    }
}

impl From<BackendError> for CanonicalError {
    fn from(error: BackendError) -> Self {
        Self::Backend(error)
    }
}

impl CanonicalError {
    /// A refusal attributed to the backend that made it.
    ///
    /// Deliberately not a `From<UnsupportedReason>` impl: see
    /// [`BackendError::unsupported`]. `?` on a bare reason must not compile,
    /// because the reason alone does not say which backend produced it.
    #[must_use]
    pub const fn unsupported(backend: &'static str, reason: UnsupportedReason) -> Self {
        Self::Backend(BackendError::unsupported(backend, reason))
    }
}

/// Read logical metadata off a checked handle.
///
/// Every field is taken from the allocation the backend will actually read, so
/// validation cannot be satisfied by metadata that describes some other tensor.
#[must_use]
pub fn logical_meta(metadata: &TensorMeta) -> LogicalTensorMeta {
    LogicalTensorMeta {
        shape: Some(metadata.shape.clone()),
        dtype: Some(metadata.dtype),
        device: Some(metadata.device),
    }
}

/// Ask the backend's exact capability registry whether `operation` may run over
/// one concrete operand.
fn admit<B: Capabilities>(
    backend: &B,
    operation: &OperationIdentity,
    metadata: &TensorMeta,
    context_training: bool,
    math_mode: crate::exec::policy::MathMode,
) -> Result<SupportLevel, UnsupportedReason> {
    let query = CapabilityQuery {
        operation: operation.clone(),
        dtype: metadata.dtype,
        layout: metadata.layout,
        rank: metadata.shape.dims().len(),
        training: context_training,
        math_mode,
    };
    match backend.support(&query) {
        SupportLevel::Unsupported(reason) => Err(reason),
        level => Ok(level),
    }
}

/// Convert a capability answer into canonical admission, including the
/// context's fallback policy. Keeping this in one place ensures no dispatch
/// route can reach `Execute` with a composed or transfer fallback it did not
/// explicitly permit.
fn admit_support(
    backend: &'static str,
    operation: &OperationIdentity,
    support: SupportLevel,
    fallback: FallbackPolicy,
) -> Result<(), CanonicalError> {
    match support {
        SupportLevel::Native => Ok(()),
        SupportLevel::Composed if fallback.allows_composition() => Ok(()),
        SupportLevel::Fallback if fallback.allows_transfer() => Ok(()),
        SupportLevel::Unsupported(reason) => Err(CanonicalError::unsupported(backend, reason)),
        support @ (SupportLevel::Composed | SupportLevel::Fallback) => {
            Err(CanonicalError::Policy(PolicyViolation {
                operation: operation.clone(),
                support,
                fallback,
            }))
        }
    }
}

fn admit_operation<O, B>(
    backend: &B,
    operation: &OperationIdentity,
    metadata: &TensorMeta,
    context_training: bool,
    math_mode: crate::exec::policy::MathMode,
    fallback: FallbackPolicy,
) -> Result<(), CanonicalError>
where
    O: Operation,
    B: Capabilities + Execute<O>,
{
    let query = CapabilityQuery {
        operation: operation.clone(),
        dtype: metadata.dtype,
        layout: metadata.layout,
        rank: metadata.shape.dims().len(),
        training: context_training,
        math_mode,
    };
    let support = match operation {
        OperationIdentity::Builtin(_) => Capabilities::support(backend, &query),
        OperationIdentity::Custom(_) => Execute::<O>::supports_custom(backend, &query),
    };
    admit_support(B::BACKEND_NAME, operation, support, fallback)
}

fn admit_metadata_free_operation<O, B>(
    backend: &B,
    operation: &OperationIdentity,
    training: bool,
    math_mode: crate::exec::policy::MathMode,
    fallback: FallbackPolicy,
) -> Result<(), CanonicalError>
where
    O: Operation,
    B: Execute<O>,
{
    let OperationIdentity::Custom(_) = operation else {
        return Ok(());
    };
    admit_support(
        B::BACKEND_NAME,
        operation,
        Execute::<O>::supports_custom_operation(backend, operation, training, math_mode),
        fallback,
    )
}

fn admit_invocation<O, B>(
    context: &ExecutionContext<B>,
    invocation: &crate::exec::catalog::ValidatedInvocation<O>,
    inputs: &[TensorHandle<'_>],
) -> Result<(), CanonicalError>
where
    O: Operation,
    B: Capabilities + Execute<O>,
{
    let training = context.training();
    let fallback = context.fallback();
    let identity = invocation.descriptor().identity();
    for handle in inputs {
        admit_operation::<O, B>(
            context.backend(),
            identity,
            handle.metadata(),
            training,
            context.math_mode(),
            fallback,
        )?;
    }
    if !inputs.is_empty() {
        return Ok(());
    }

    let mut queried = false;
    for output in invocation.descriptor().outputs() {
        let (Some(dtype), Some(shape)) = (output.dtype, output.shape.as_deref()) else {
            continue;
        };
        queried = true;
        let query = CapabilityQuery {
            operation: identity.clone(),
            dtype,
            layout: crate::exec::meta::LayoutClass::Contiguous,
            rank: shape.len(),
            training,
            math_mode: context.math_mode(),
        };
        let support = match identity {
            OperationIdentity::Builtin(_) => Capabilities::support(context.backend(), &query),
            OperationIdentity::Custom(_) => {
                Execute::<O>::supports_custom(context.backend(), &query)
            }
        };
        admit_support(B::BACKEND_NAME, identity, support, fallback)?;
    }
    if !queried {
        admit_metadata_free_operation::<O, B>(
            context.backend(),
            identity,
            training,
            context.math_mode(),
            fallback,
        )?;
    }
    Ok(())
}

/// Validate and run one operation on `context`'s backend.
///
/// `O` names the exact operation identity, so the descriptor, capability query
/// and `Execute` implementation are selected by the same token.
///
/// # Errors
///
/// Returns [`CanonicalError::Descriptor`] when the invocation fails contract
/// validation, [`CanonicalError::Policy`] when its reported support is denied
/// by the effective fallback policy, and [`CanonicalError::Backend`] when the
/// backend's capability registry refuses it or execution itself fails.
pub fn execute<O, B>(
    context: &ExecutionContext<B>,
    attributes: O::Attributes,
    inputs: &[TensorHandle<'_>],
) -> Result<<B as Execute<O>>::Output, CanonicalError>
where
    O: Operation,
    B: Execute<O> + Capabilities,
{
    execute_with_payload(context, attributes, inputs, None)
}

/// Execute an operation with an optional borrowed payload kept outside its descriptor.
pub fn execute_with_payload<O, B>(
    context: &ExecutionContext<B>,
    attributes: O::Attributes,
    inputs: &[TensorHandle<'_>],
    payload: Option<&[u8]>,
) -> Result<<B as Execute<O>>::Output, CanonicalError>
where
    O: Operation,
    B: Execute<O> + Capabilities,
{
    let logical: Vec<LogicalTensorMeta> = inputs
        .iter()
        .map(|handle| logical_meta(handle.metadata()))
        .collect();

    let invocation = O::infer_invocation(attributes, logical)?;

    validate_execution_payload::<O>(&invocation, payload)?;

    admit_invocation::<O, B>(context, &invocation, inputs)?;

    context
        .backend()
        .execute(ExecutionRequest {
            operation: invocation.validated(),
            inputs,
            context,
            payload,
        })
        .map_err(CanonicalError::Backend)
}

/// [`execute`], told the shape value the typed frontend was holding.
pub fn execute_shaped<O, B, S>(
    context: &ExecutionContext<B>,
    attributes: O::Attributes,
    inputs: &[TensorHandle<'_>],
    expected: &crate::shapes::ShapeValue<S>,
) -> Result<<B as Execute<O>>::Output, CanonicalError>
where
    O: Operation,
    B: Execute<O> + Capabilities,
    S: crate::shapes::Shape,
{
    execute_shaped_with_payload(context, attributes, inputs, expected, None)
}

/// [`execute_shaped`], with an optional borrowed execution payload.
pub fn execute_shaped_with_payload<O, B, S>(
    context: &ExecutionContext<B>,
    attributes: O::Attributes,
    inputs: &[TensorHandle<'_>],
    expected: &crate::shapes::ShapeValue<S>,
    payload: Option<&[u8]>,
) -> Result<<B as Execute<O>>::Output, CanonicalError>
where
    O: Operation,
    B: Execute<O> + Capabilities,
    S: crate::shapes::Shape,
{
    let logical: Vec<LogicalTensorMeta> = inputs
        .iter()
        .map(|handle| logical_meta(handle.metadata()))
        .collect();

    let invocation = O::infer_invocation_typed(attributes, logical, expected)?;

    validate_execution_payload::<O>(&invocation, payload)?;

    admit_invocation::<O, B>(context, &invocation, inputs)?;

    context
        .backend()
        .execute(ExecutionRequest {
            operation: invocation.validated(),
            inputs,
            context,
            payload,
        })
        .map_err(CanonicalError::Backend)
}

fn validate_execution_payload<O>(
    invocation: &crate::exec::catalog::ValidatedInvocation<O>,
    payload: Option<&[u8]>,
) -> Result<(), CanonicalError>
where
    O: Operation,
{
    let attributes = invocation.descriptor().attributes();
    let data =
        (attributes as &dyn core::any::Any).downcast_ref::<crate::exec::catalog::DataAttributes>();
    let operation = match invocation.descriptor().identity() {
        OperationIdentity::Builtin(operation) => operation,
        OperationIdentity::Custom(_) => return Ok(()),
    };
    match data {
        Some(data) => match payload {
            Some(bytes) if bytes.len() == data.payload.byte_len() => Ok(()),
            Some(bytes) => Err(CanonicalError::Descriptor(
                crate::exec::catalog::DescriptorError::PayloadByteLength {
                    operation: *operation,
                    expected: data.payload.byte_len(),
                    actual: bytes.len(),
                },
            )),
            None => Err(CanonicalError::Descriptor(
                crate::exec::catalog::DescriptorError::PayloadMissing {
                    operation: *operation,
                },
            )),
        },
        None if payload.is_some() => Err(CanonicalError::Descriptor(
            crate::exec::catalog::DescriptorError::UnexpectedPayload {
                operation: *operation,
            },
        )),
        None => Ok(()),
    }
}

/// The support level `operation` would resolve to for one operand, without
/// running it.
///
/// A caller that wants to choose between backends needs the same answer the
/// execution path uses, from the same registry, or the choice is made against a
/// second source of truth.
///
/// # Errors
///
/// Returns the typed reason the exact capability row refuses the operand.
pub fn support_for<O, B>(
    context: &ExecutionContext<B>,
    metadata: &TensorMeta,
) -> Result<SupportLevel, UnsupportedReason>
where
    O: crate::exec::catalog::CanonicalOperation,
    B: Capabilities + crate::tensor::backend::StorageBackend,
{
    admit(
        context.backend(),
        &OperationIdentity::Builtin(O::ID),
        metadata,
        context.training(),
        context.math_mode(),
    )
}