tenferro-einsum 0.3.0

Subscripts, contraction planning, concrete/traced/eager einsum APIs, extension runtime, and AD rule for tenferro.
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
use std::hash::Hasher;

use omeco::ScoreFunction;

use crate::{
    ContractionOptimizerOptions, ContractionTree, Error, NestedEinsum, Result, Subscripts,
};

/// Controls how the contraction path is determined for N-ary einsum.
///
/// The traced API resolves this enum into a shape-independent plan
/// specification and stores that specification in the einsum extension
/// payload. Concrete traced inputs can resolve a [`ContractionTree`] while the
/// op is built; symbolic traced inputs carry the plan specification until the
/// extension runtime sees concrete execution shapes.
///
/// Planner options and explicit paths are part of the extension payload
/// identity. Two otherwise identical traced einsum ops with different
/// optimizer options, explicit paths, or fixed plan identities are not treated
/// as the same extension op, and their compile/runtime plan cache entries are
/// kept separate.
///
/// # Examples
///
/// ```
/// use tenferro_einsum::{EinsumOptimize, TraceContextEinsumExt};
/// use tenferro_ops::dim_expr::DimExpr;
/// use tenferro_runtime::program::ProgramInputSpec;
/// use tenferro_runtime::{DType, TraceContext};
///
/// let mut trace = TraceContext::new();
/// let lhs = trace.input(ProgramInputSpec::new(
///     DType::F64,
///     DimExpr::from_concrete(&[2, 3]),
/// )).unwrap();
/// let rhs = trace.input(ProgramInputSpec::new(
///     DType::F64,
///     DimExpr::from_concrete(&[3, 2]),
/// )).unwrap();
/// let out = trace.einsum_with(
///     &[lhs, rhs],
///     "ij,jk->ik",
///     EinsumOptimize::False,
/// )
/// .unwrap();
///
/// let graph = trace.finish(&[out]).unwrap();
/// assert_eq!(
///     graph.program().value_metadata(graph.program().outputs()[0]).unwrap().shape().len(),
///     2,
/// );
/// ```
#[derive(Debug)]
pub enum EinsumOptimize {
    /// Automatic optimization via omeco TreeSA.
    Auto(ContractionOptimizerOptions),
    /// No optimization: contract operands left-to-right.
    False,
    /// Parenthesized notation specifying contraction order.
    Nested(NestedEinsum),
    /// JAX-compatible position-based contraction path.
    ///
    /// Each pair references positions in a shrinking operand list. After each
    /// contraction, the two operands are removed and the result is appended.
    /// Because this representation is independent of concrete dimension
    /// values, it can be used with symbolic traced inputs.
    Path(Vec<(usize, usize)>),
    /// Pre-computed contraction tree.
    ///
    /// A tree contains concrete shape-dependent planning results. It is
    /// accepted when shapes are concrete, then converted into fixed contraction
    /// pairs for the extension payload. A binary tree with the single pair
    /// `(0, 1)` or `(1, 0)` may bypass the extension path and lower directly to
    /// `dot_general`, including with symbolic traced inputs. Use
    /// [`EinsumOptimize::Path`] instead when building a symbolic traced graph
    /// for N-ary contraction.
    Tree(ContractionTree),
}

impl Default for EinsumOptimize {
    /// Default: time-optimized automatic planning.
    fn default() -> Self {
        Self::Auto(default_auto_options())
    }
}

#[derive(Clone, Debug)]
pub(crate) enum EinsumPlanSpec {
    Auto(ContractionOptimizerOptions),
    LeftToRight,
    Path(Vec<(usize, usize)>),
    FixedPairs(Vec<(usize, usize)>),
}

/// Return the default automatic optimizer options.
#[must_use]
pub(crate) fn default_auto_options() -> ContractionOptimizerOptions {
    ContractionOptimizerOptions {
        score: ScoreFunction::time_optimized(),
        ..Default::default()
    }
}

pub(crate) fn plan_spec_from_optimize(
    optimize: EinsumOptimize,
    subscripts: &Subscripts,
) -> Result<EinsumPlanSpec> {
    match optimize {
        EinsumOptimize::Auto(options) => {
            options.validate()?;
            Ok(EinsumPlanSpec::Auto(options))
        }
        EinsumOptimize::False => Ok(EinsumPlanSpec::LeftToRight),
        EinsumOptimize::Nested(nested) => {
            let pairs = nested_to_v1_pairs(&nested, subscripts.inputs.len())?;
            validate_fixed_pairs(&pairs, subscripts.inputs.len())?;
            Ok(EinsumPlanSpec::FixedPairs(pairs))
        }
        EinsumOptimize::Path(path) => {
            let _ = jax_path_to_v1_pairs(&path, subscripts.inputs.len())?;
            Ok(EinsumPlanSpec::Path(path))
        }
        EinsumOptimize::Tree(_) => Err(Error::planning(
            "precomputed contraction tree requires concrete input shapes; use Path or parenthesized notation for symbolic traced einsum",
        )),
    }
}

pub(crate) fn resolve_einsum_strategy_with_spec(
    optimize: EinsumOptimize,
    subscripts: &Subscripts,
    shapes: &[&[usize]],
) -> Result<(EinsumPlanSpec, ContractionTree)> {
    match optimize {
        EinsumOptimize::Tree(tree) => {
            let pairs = tree_pairs(&tree);
            let spec = EinsumPlanSpec::FixedPairs(pairs);
            let tree = resolve_plan_spec(&spec, subscripts, shapes)?;
            Ok((spec, tree))
        }
        optimize => {
            let spec = plan_spec_from_optimize(optimize, subscripts)?;
            let tree = resolve_plan_spec(&spec, subscripts, shapes)?;
            Ok((spec, tree))
        }
    }
}

pub(crate) fn resolve_plan_spec(
    spec: &EinsumPlanSpec,
    subscripts: &Subscripts,
    shapes: &[&[usize]],
) -> Result<ContractionTree> {
    match spec {
        EinsumPlanSpec::Auto(options) => {
            ContractionTree::optimize_with_options(subscripts, shapes, options)
        }
        EinsumPlanSpec::LeftToRight => {
            let n = subscripts.inputs.len();
            if n <= 1 {
                ContractionTree::from_pairs(subscripts, shapes, &[])
            } else {
                let path: Vec<(usize, usize)> = (0..n - 1).map(|_| (0, 1)).collect();
                let pairs = jax_path_to_v1_pairs(&path, n)?;
                ContractionTree::from_pairs(subscripts, shapes, &pairs)
            }
        }
        EinsumPlanSpec::Path(path) => {
            let pairs = jax_path_to_v1_pairs(path, subscripts.inputs.len())?;
            ContractionTree::from_pairs(subscripts, shapes, &pairs)
        }
        EinsumPlanSpec::FixedPairs(pairs) => ContractionTree::from_pairs(subscripts, shapes, pairs),
    }
}

pub(crate) fn hash_einsum_plan_spec(spec: &EinsumPlanSpec, state: &mut dyn Hasher) {
    match spec {
        EinsumPlanSpec::Auto(options) => {
            state.write_u8(0);
            hash_optimizer_options(options, state);
        }
        EinsumPlanSpec::LeftToRight => state.write_u8(1),
        EinsumPlanSpec::Path(path) => {
            state.write_u8(2);
            hash_pairs(path, state);
        }
        EinsumPlanSpec::FixedPairs(pairs) => {
            state.write_u8(3);
            hash_pairs(pairs, state);
        }
    }
}

pub(crate) fn plan_specs_equal(lhs: &EinsumPlanSpec, rhs: &EinsumPlanSpec) -> bool {
    match (lhs, rhs) {
        (EinsumPlanSpec::Auto(lhs), EinsumPlanSpec::Auto(rhs)) => {
            optimizer_options_equal_by_bits(lhs, rhs)
        }
        (EinsumPlanSpec::LeftToRight, EinsumPlanSpec::LeftToRight) => true,
        (EinsumPlanSpec::Path(lhs), EinsumPlanSpec::Path(rhs)) => lhs == rhs,
        (EinsumPlanSpec::FixedPairs(lhs), EinsumPlanSpec::FixedPairs(rhs)) => lhs == rhs,
        _ => false,
    }
}

fn tree_pairs(tree: &ContractionTree) -> Vec<(usize, usize)> {
    (0..tree.step_count())
        .filter_map(|step| tree.step_pair(step))
        .collect()
}

fn validate_fixed_pairs(pairs: &[(usize, usize)], input_count: usize) -> Result<()> {
    let required_steps = input_count.saturating_sub(1);
    if pairs.len() != required_steps {
        return Err(Error::planning(format!(
            "explicit contraction path for {input_count} operands must have {required_steps} steps, got {}",
            pairs.len()
        )));
    }

    let mut live = vec![false; input_count + pairs.len()];
    for slot in live.iter_mut().take(input_count) {
        *slot = true;
    }

    for (step_idx, &(left, right)) in pairs.iter().enumerate() {
        let next_idx = input_count + step_idx;
        if left == right {
            return Err(Error::planning(format!(
                "pair ({left}, {right}) must reference two distinct live operands"
            )));
        }
        if left >= next_idx || right >= next_idx {
            return Err(Error::planning(format!(
                "pair ({left}, {right}) references non-existent operand"
            )));
        }
        if !live[left] || !live[right] {
            return Err(Error::planning(format!(
                "pair ({left}, {right}) references an operand or intermediate that is no longer live"
            )));
        }

        live[left] = false;
        live[right] = false;
        live[next_idx] = true;
    }

    let live_count = live.iter().filter(|&&is_live| is_live).count();
    if live_count != 1 {
        return Err(Error::planning(format!(
            "explicit contraction path must leave exactly one live result, got {live_count}"
        )));
    }

    Ok(())
}

fn hash_pairs(pairs: &[(usize, usize)], state: &mut dyn Hasher) {
    state.write_usize(pairs.len());
    for &(left, right) in pairs {
        state.write_usize(left);
        state.write_usize(right);
    }
}

fn hash_optimizer_options(options: &ContractionOptimizerOptions, state: &mut dyn Hasher) {
    state.write_usize(options.ntrials);
    state.write_usize(options.niters);
    state.write_usize(options.betas.len());
    for value in &options.betas {
        state.write_u64(value.to_bits());
    }
    state.write_u64(options.score.tc_weight.to_bits());
    state.write_u64(options.score.sc_weight.to_bits());
    state.write_u64(options.score.rw_weight.to_bits());
    state.write_u64(options.score.sc_target.to_bits());
}

fn optimizer_options_equal_by_bits(
    lhs: &ContractionOptimizerOptions,
    rhs: &ContractionOptimizerOptions,
) -> bool {
    lhs.ntrials == rhs.ntrials
        && lhs.niters == rhs.niters
        && f64_slices_equal_by_bits(&lhs.betas, &rhs.betas)
        && score_functions_equal_by_bits(&lhs.score, &rhs.score)
}

/// Convert JAX-style position-based path to fixed-ID pairs.
///
/// JAX format: each pair `(i, j)` refers to positions in a shrinking list.
/// After contraction, the two operands are removed and the result is appended.
/// Fixed-ID format keeps original operands at `0..input_count` and gives
/// intermediate at step `k` the ID `input_count + k`.
///
/// # Errors
///
/// Returns an error if a path step references the same position twice or a
/// position outside the current shrinking list.
pub(crate) fn jax_path_to_v1_pairs(
    jax_path: &[(usize, usize)],
    input_count: usize,
) -> Result<Vec<(usize, usize)>> {
    let required_steps = input_count.saturating_sub(1);
    if jax_path.len() != required_steps {
        return Err(Error::planning(format!(
            "explicit contraction path for {input_count} operands must have {required_steps} steps, got {}",
            jax_path.len()
        )));
    }

    let mut positions: Vec<usize> = (0..input_count).collect();
    let mut v1_pairs = Vec::with_capacity(jax_path.len());

    for (step, &(pos_a, pos_b)) in jax_path.iter().enumerate() {
        if pos_a == pos_b {
            return Err(Error::planning(format!(
                "path step {step} references the same operand position twice: {pos_a}"
            )));
        }
        let current_len = positions.len();
        if pos_a >= current_len || pos_b >= current_len {
            return Err(Error::planning(format!(
                "path step {step} references operand positions ({pos_a}, {pos_b}) with only {current_len} live operands"
            )));
        }

        let (lo, hi) = if pos_a < pos_b {
            (pos_a, pos_b)
        } else {
            (pos_b, pos_a)
        };
        let id_a = positions[lo];
        let id_b = positions[hi];
        v1_pairs.push((id_a, id_b));

        positions.remove(hi);
        positions.remove(lo);
        positions.push(input_count + step);
    }

    Ok(v1_pairs)
}

/// Convert a [`NestedEinsum`] tree into fixed-ID pairs.
///
/// # Errors
///
/// Returns an error if a leaf references an input outside `0..input_count` or if
/// a node has no children.
pub(crate) fn nested_to_v1_pairs(
    nested: &NestedEinsum,
    input_count: usize,
) -> Result<Vec<(usize, usize)>> {
    let mut pairs = Vec::with_capacity(input_count.saturating_sub(1));
    let mut next_id = input_count;
    let root_id = walk_nested(nested, input_count, &mut pairs, &mut next_id)?;
    if input_count == 0 || root_id >= next_id {
        return Err(Error::planning(
            "nested einsum did not produce a valid root operand",
        ));
    }
    Ok(pairs)
}

fn walk_nested(
    nested: &NestedEinsum,
    input_count: usize,
    pairs: &mut Vec<(usize, usize)>,
    next_id: &mut usize,
) -> Result<usize> {
    match nested {
        NestedEinsum::Leaf(idx) => {
            if *idx >= input_count {
                return Err(Error::planning(format!(
                    "nested einsum leaf {idx} is outside 0..{input_count}"
                )));
            }
            Ok(*idx)
        }
        NestedEinsum::Node { children, .. } => {
            let Some(first) = children.first() else {
                return Err(Error::planning(
                    "nested einsum node must have at least one child",
                ));
            };
            let mut result_id = walk_nested(first, input_count, pairs, next_id)?;
            for child in &children[1..] {
                let child_id = walk_nested(child, input_count, pairs, next_id)?;
                pairs.push((result_id, child_id));
                result_id = *next_id;
                *next_id += 1;
            }
            Ok(result_id)
        }
    }
}

fn f64_slices_equal_by_bits(lhs: &[f64], rhs: &[f64]) -> bool {
    lhs.len() == rhs.len()
        && lhs
            .iter()
            .zip(rhs)
            .all(|(lhs, rhs)| lhs.to_bits() == rhs.to_bits())
}

fn score_functions_equal_by_bits(lhs: &ScoreFunction, rhs: &ScoreFunction) -> bool {
    lhs.tc_weight.to_bits() == rhs.tc_weight.to_bits()
        && lhs.sc_weight.to_bits() == rhs.sc_weight.to_bits()
        && lhs.rw_weight.to_bits() == rhs.rw_weight.to_bits()
        && lhs.sc_target.to_bits() == rhs.sc_target.to_bits()
}

#[cfg(test)]
mod tests;