---
title: Operations and their derivatives
summary: Maps between spaces described once and on no device, lowered to kernels that say how far they differentiate, and composed from a few primitives.
order: 2
depends_on: [concepts/spaces, automatic-differentiation]
---

# Operations and their derivatives

Every value a model computes is the output of an **operation**: a map between [spaces](/concepts/spaces.md), such as a sum, an exponential or a matrix product.
An operation is described once, on no device and at no precision, and is turned into something that runs only when a query is compiled for a target.
This page describes operations, the kernels they are lowered to, and how the core builds everything from a small set of primitives.

## An operation

From `crates/skoro-core/src/operation.rs`, lines 10–21:

```rust
/// A map between spaces, described once and on no device, and lowered to a kernel when a query
/// is compiled.
pub trait Operation: Debug + Send + Sync + 'static {
    /// The registered identifier, such as `"skoro.add"`.
    fn name(&self) -> &'static str;
    /// The plain parameters which, with the name, let a registry make the operation again.
    fn params(&self) -> Vec<Param>;
    /// Checks the inputs' spaces and gives the output's. Called when the node is added.
    fn output(&self, inputs: &[Space]) -> Result<Space, Error>;
    /// What computes this operation on `lowering`'s target.
    fn lower(&self, inputs: &[Space], lowering: &mut Lowering<'_>) -> Result<Lowered, Error>;
}
```

An operation is known by its name and its parameters, plain values fixed when it is made and never inputs.
The two are enough for a registry to make the operation again, which is how a model can one day be written out and read back.
When a node is added to a model, `output` checks the inputs' spaces and gives the output's; a mismatch fails the node there, at the line that built it, rather than when the model is run.

`lower` is asked only when a query is compiled, for one target, and answers in one of two ways:

From `crates/skoro-core/src/operation.rs`, lines 64–70:

```rust
/// What computes an operation on one target.
#[derive(Clone)]
pub enum Lowered {
    Kernel(Kernel),
    /// Expand this composite in place of the operation.
    Composite(Arc<Composite>),
}
```

## Kernels, and how far they differentiate

A kernel computes an operation on one target, and its variant is its **order**: how far it differentiates.

From `crates/skoro-core/src/operation.rs`, lines 133–147:

```rust
/// An operation on one target. The variant is its order.
#[derive(Clone)]
pub enum Kernel {
    Zero(Arc<dyn Forward>),
    One(Arc<dyn FirstOrder>),
    Two(Arc<dyn SecondOrder>),
}

/// How far a kernel differentiates.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub enum Order {
    Zero,
    One,
    Two,
}
```

A kernel of order zero computes its value alone.
One of order one also pushes tangent frames forward and records a pull-back, which carries cotangents back to its inputs.
One of order two also carries the second-order terms that Hessian-vector products need, at velocities given at its inputs:

From `crates/skoro-core/src/operation.rs`, lines 149–179:

```rust
pub trait Forward: Send + Sync + 'static {
    fn forward(&self, inputs: &[Value]) -> Value;
}

pub trait FirstOrder: Forward {
    /// The value and the tangent frame of the output. A missing frame is zero.
    fn tangent(&self, inputs: &[Value], tangents: &[Option<Frame>]) -> (Value, Option<Frame>);
    /// The value and the pull-back at these inputs, into the inputs marked differentiable.
    fn record(&self, inputs: &[Value], differentiable: &[bool]) -> (Value, Box<dyn Pullback>);
}

pub trait SecondOrder: FirstOrder {
    /// As `record`, with the inputs' velocities, and returning the output's.
    fn record_second(
        &self,
        inputs: &[Value],
        differentiable: &[bool],
        velocities: &[Option<Frame>],
    ) -> (Value, Option<Frame>, Box<dyn SecondPullback>);
}

pub trait Pullback: Send {
    /// One frame per input: `None` for an input that is not differentiable.
    fn cotangent(&self, alpha: &Frame) -> Vec<Option<Frame>>;
}

pub trait SecondPullback: Pullback {
    /// `hvp_j(alpha)` per input, at the velocities recorded: `alpha` has one column, each result
    /// as many columns as the velocities.
    fn second(&self, alpha: &Frame) -> Vec<Option<Frame>>;
}
```

These are the maps of [automatic differentiation](/math/automatic-differentiation.md) in forward, reverse and forward-over-reverse mode, one operation at a time.
A program composes them, and a pass over a program needs every step on it to have the order the pass requires: a gradient needs order one of every step it differentiates through, and a Hessian pass order two.
A program checks this before it runs anything and refuses a pass it cannot carry, naming the first step that falls short, as [queries and programs](/concepts/queries.md) describe.
The integer operations, for instance, have order zero, so they may feed a value but no derivative may be taken through them.

## The primitives

A few primitive operations carry hand-written kernels, the same code at both precisions:

From `crates/skoro-core/src/primitive/mod.rs`, lines 9–17:

```rust
pub use crate::embedded::{Include, Project, Retract};
pub use discrete::{
    IntegerAdd, IntegerEq, IntegerLe, IntegerLt, IntegerMul, IntegerSub, IsNonnegative, ToReal,
};
pub use euclidean::{
    Add, Atan2, Concatenate, Cos, Exp, Log, MatMul, Mul, Neg, Scale, Sin, Slice, Sub, Sum,
    Transpose,
};
pub use positive::{PositiveAsReal, PositiveExp, PositiveLog, PositiveMul, PositiveRecip, Select};
```

The Euclidean and positive primitives differentiate to order two; the discrete ones (integer arithmetic and comparison, the embedding `to_real` and the test `is_nonnegative`) to order zero.
`scope::select` chooses one of several values by a categorical index, and routes the cotangent to the one it chose.

Code rarely names a primitive.
A handle carries them as operators and methods (`x + y`, `x * y`, `-x`, `x.exp()`, `x.sum()`, `a.matmul(b)`, `p.as_real()`, `p.recip()`), typed by the markers of their operands, so that adding a vector to a matrix does not compile.
A literal operand, as in `0.5 * x`, becomes a constant node of the model.

## Composites

Everything that is not a primitive, distributions and retractions included, is a **composite**: a named subgraph with designated inputs and one output, built by the same code a model would use.

From `crates/skoro-core/tests/passes.rs`, lines 25–37:

```rust

fn mix() -> Arc<Composite> {
    let v = Space::Vector(2);
    Composite::define(
        "test.mix",
        Vec::new(),
        &[v.clone(), v, Space::Real],
        |inputs| {
            let (x, y, s) = (inputs[0], inputs[1], inputs[2]);
            ((x * y).exp() + s * x.sin() + x * x).sum()
        },
    )
    .unwrap()
```

`Composite::define` runs the body once, on handles standing for the inputs, and keeps the nodes it builds.
`call` adds one node to the model, the composite applied to its inputs.
When a query is compiled, the composite is lowered by expanding its body in place, so it differentiates as far as the operations inside it do, with no derivative written for it.
The retractions of the previous page are composites of this kind.

> [!DECISION] Derivatives come from composition
> Only primitives carry hand-written derivatives; everything else is composed from them, and reports the order its parts support.
> See [position 5 of the charter](/archive/charter/writeup.md#5-derivatives-come-from-composition-and-their-order-is-reported).
