tenferro-einsum 0.2.0

Subscripts, contraction planning, concrete/traced/eager einsum APIs, extension runtime, and AD rule 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
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
use std::sync::Arc;

use computegraph::types::ValueRef;
use tenferro_ops::dim_expr::DimExpr;
use tenferro_ops::ext_op::ExtensionOp;
use tenferro_runtime::error::{Error, Result};
use tenferro_runtime::extension::{self, ExtensionCacheKey, ExtensionCacheStore};
use tenferro_runtime::{GraphCompiler, SymDim, TracedTensor};

use crate::binary_dot::{try_build_exact_output_binary_dot_plan, BinaryDotOperandOrder};
use crate::builder::build_einsum_graph_dim_expr;
use crate::cache::{
    einsum_subscripts_retained_bytes, saturating_sum, vec_retained_bytes, ParsedEinsum,
    EINSUM_EXTENSION_FAMILY_ID, EINSUM_PARSE_CACHE, EINSUM_STATIC_PLANS_CACHE,
};
use crate::extension::EinsumExtensionOp;
use crate::optimize::{
    hash_einsum_plan_spec, plan_spec_from_optimize, resolve_einsum_strategy_with_spec,
    resolve_plan_spec, EinsumPlanSpec,
};
use crate::{
    parse_einsum_subscripts, ContractionTree, EinsumOptimize, EinsumSubscripts,
    Error as EinsumError, Result as EinsumResult, Subscripts, TensorDotAxes,
};

/// Traced einsum extension methods for [`GraphCompiler`].
pub trait GraphCompilerEinsumExt {
    fn einsum(&mut self, inputs: &[&TracedTensor], subscripts: &str) -> Result<TracedTensor>;
    fn einsum_subscripts(
        &mut self,
        inputs: &[&TracedTensor],
        subscripts: &EinsumSubscripts,
    ) -> Result<TracedTensor>;
    fn einsum_with(
        &mut self,
        inputs: &[&TracedTensor],
        subscripts: &str,
        optimize: EinsumOptimize,
    ) -> Result<TracedTensor>;
    fn einsum_subscripts_with(
        &mut self,
        inputs: &[&TracedTensor],
        subscripts: &EinsumSubscripts,
        optimize: EinsumOptimize,
    ) -> Result<TracedTensor>;
}

impl GraphCompilerEinsumExt for GraphCompiler {
    fn einsum(&mut self, inputs: &[&TracedTensor], subscripts: &str) -> Result<TracedTensor> {
        einsum(self, inputs, subscripts)
    }

    fn einsum_subscripts(
        &mut self,
        inputs: &[&TracedTensor],
        subscripts: &EinsumSubscripts,
    ) -> Result<TracedTensor> {
        einsum_subscripts(self, inputs, subscripts)
    }

    fn einsum_with(
        &mut self,
        inputs: &[&TracedTensor],
        subscripts: &str,
        optimize: EinsumOptimize,
    ) -> Result<TracedTensor> {
        einsum_with(self, inputs, subscripts, optimize)
    }

    fn einsum_subscripts_with(
        &mut self,
        inputs: &[&TracedTensor],
        subscripts: &EinsumSubscripts,
        optimize: EinsumOptimize,
    ) -> Result<TracedTensor> {
        einsum_subscripts_with(self, inputs, subscripts, optimize)
    }
}

/// Traced tensor contraction-sugar methods.
pub trait TracedTensorEinsumExt {
    fn tensordot(&self, rhs: &TracedTensor, axes: TensorDotAxes<'_>) -> Result<TracedTensor>;
}

impl TracedTensorEinsumExt for TracedTensor {
    fn tensordot(&self, rhs: &TracedTensor, axes: TensorDotAxes<'_>) -> Result<TracedTensor> {
        tensordot(self, rhs, axes)
    }
}

/// N-ary einsum with default time-optimized automatic planning.
///
/// The default optimizer is resolved into a shape-independent plan
/// specification stored in the extension payload. That payload identity
/// participates in traced extension-op equality and in compile/runtime einsum
/// plan caches.
pub fn einsum(
    compiler: &mut GraphCompiler,
    inputs: &[&TracedTensor],
    subscripts: &str,
) -> Result<TracedTensor> {
    einsum_with(compiler, inputs, subscripts, EinsumOptimize::default())
}

/// N-ary einsum using integer labels and the default contraction strategy.
///
/// The default optimizer is resolved into a shape-independent plan
/// specification stored in the extension payload. That payload identity
/// participates in traced extension-op equality and in compile/runtime einsum
/// plan caches.
pub fn einsum_subscripts(
    compiler: &mut GraphCompiler,
    inputs: &[&TracedTensor],
    subscripts: &EinsumSubscripts,
) -> Result<TracedTensor> {
    einsum_subscripts_with(compiler, inputs, subscripts, EinsumOptimize::default())
}

/// N-ary einsum with explicit contraction strategy.
///
/// `optimize` is converted to a shape-independent plan specification carried
/// by the extension payload. `EinsumOptimize::Path` uses JAX-style positions
/// over the current shrinking operand list, so it works with symbolic traced
/// inputs. `EinsumOptimize::Tree` requires concrete shapes for N-ary extension
/// execution; binary trees that lower exactly to `dot_general` bypass the
/// extension path and may use symbolic traced inputs.
///
/// Planner options, explicit paths, and fixed plan identities affect traced
/// extension payload identity and the einsum compile/runtime plan caches.
/// Different options or paths are therefore not treated as identical extension
/// ops.
pub fn einsum_with(
    compiler: &mut GraphCompiler,
    inputs: &[&TracedTensor],
    subscripts: &str,
    optimize: EinsumOptimize,
) -> Result<TracedTensor> {
    let parsed = cached_subscripts(compiler.extension_caches_mut(), subscripts)?;
    einsum_subscripts_with(compiler, inputs, &parsed.subscripts, optimize)
}

/// N-ary einsum with integer labels and explicit contraction strategy.
///
/// `optimize` is converted to a shape-independent plan specification carried
/// by the extension payload. `EinsumOptimize::Path` uses JAX-style positions
/// over the current shrinking operand list, so it works with symbolic traced
/// inputs. `EinsumOptimize::Tree` requires concrete shapes for N-ary extension
/// execution; binary trees that lower exactly to `dot_general` bypass the
/// extension path and may use symbolic traced inputs.
///
/// Planner options, explicit paths, and fixed plan identities affect traced
/// extension payload identity and the einsum compile/runtime plan caches.
/// Different options or paths are therefore not treated as identical extension
/// ops.
pub fn einsum_subscripts_with(
    compiler: &mut GraphCompiler,
    inputs: &[&TracedTensor],
    subscripts: &EinsumSubscripts,
    optimize: EinsumOptimize,
) -> Result<TracedTensor> {
    if inputs.is_empty() {
        return Err(Error::ContractionError(
            "einsum requires at least one input tensor".into(),
        ));
    }
    if subscripts.inputs.len() != inputs.len() {
        return Err(Error::ContractionError(format!(
            "einsum subscripts expect {} inputs, got {}",
            subscripts.inputs.len(),
            inputs.len()
        )));
    }

    let output_shape_hint = infer_symbolic_output_shape(subscripts, inputs)?;
    if let Some(result) = try_direct_binary_dot_general(inputs, subscripts, &optimize)? {
        return Ok(result);
    }

    let subs = Subscripts::from(subscripts);

    let (plan_spec, static_tree) = if let Some(shapes) = concrete_shapes(inputs) {
        let shape_refs: Vec<&[usize]> = shapes.iter().map(Vec::as_slice).collect();
        let (plan_spec, tree) = match optimize {
            EinsumOptimize::Tree(tree) => {
                let (plan_spec, tree) = resolve_einsum_strategy_with_spec(
                    EinsumOptimize::Tree(tree),
                    &subs,
                    &shape_refs,
                )
                .map_err(to_tenferro_error)?;
                let tree = cached_static_tree(
                    compiler.extension_caches_mut(),
                    subscripts,
                    &plan_spec,
                    &shapes,
                    || Ok(tree),
                )?;
                (plan_spec, tree)
            }
            optimize => {
                let plan_spec =
                    plan_spec_from_optimize(optimize, &subs).map_err(to_tenferro_error)?;
                let tree = cached_static_tree(
                    compiler.extension_caches_mut(),
                    subscripts,
                    &plan_spec,
                    &shapes,
                    || resolve_plan_spec(&plan_spec, &subs, &shape_refs),
                )?;
                (plan_spec, tree)
            }
        };
        (plan_spec, Some(tree))
    } else {
        let plan_spec = plan_spec_from_optimize(optimize, &subs).map_err(to_tenferro_error)?;
        let tree = symbolic_fixed_path_tree(&plan_spec, &subs, inputs)?;
        (plan_spec, tree.map(Arc::new))
    };

    if let Some(tree) = static_tree {
        return expand_traced_einsum_graph(inputs, subscripts, tree.as_ref(), output_shape_hint);
    }

    let op =
        EinsumExtensionOp::with_output_shape_hint(subscripts.clone(), output_shape_hint, plan_spec);
    let outputs = extension::apply(Arc::new(op), inputs)?;
    outputs
        .into_iter()
        .next()
        .ok_or_else(|| Error::Internal("einsum extension produced no output".into()))
}

fn tensordot(
    lhs: &TracedTensor,
    rhs: &TracedTensor,
    axes: TensorDotAxes<'_>,
) -> Result<TracedTensor> {
    let config = crate::tensordot::dot_general_config(axes, lhs.rank, rhs.rank)?;
    crate::tensordot::validate_traced_contract_dims(lhs, rhs, &config)?;
    lhs.dot_general(rhs, config)
}

fn expand_traced_einsum_graph(
    inputs: &[&TracedTensor],
    subscripts: &EinsumSubscripts,
    tree: &ContractionTree,
    output_shape_hint: Vec<SymDim>,
) -> Result<TracedTensor> {
    let op = EinsumExtensionOp::with_output_shape_hint(
        subscripts.clone(),
        output_shape_hint,
        EinsumPlanSpec::LeftToRight,
    );
    let input_dtypes: Vec<_> = inputs.iter().map(|tensor| tensor.dtype).collect();
    let input_sym_shapes: Vec<Vec<SymDim>> = inputs
        .iter()
        .map(|tensor| match tensor.sym_shape() {
            Some(shape) => Ok(shape.to_vec()),
            None => (0..tensor.rank)
                .map(|axis| tensor.axis_sym_dim(axis))
                .collect(),
        })
        .collect::<Result<_>>()?;
    let input_sym_shape_refs: Vec<_> = input_sym_shapes.iter().map(Vec::as_slice).collect();
    let output_metas = op.infer_output_meta(&input_dtypes, &input_sym_shape_refs)?;
    let input_dim_shapes = traced_dim_expr_shapes(inputs);

    let outputs = extension::apply_expanded_graph(inputs, output_metas, |builder, input_refs| {
        let result = build_einsum_graph_dim_expr(builder, tree, input_refs, &input_dim_shapes)
            .map_err(|err| Error::ContractionError(err.to_string()))?;
        let ValueRef::Local(local) = result else {
            return Err(Error::Internal(
                "expanded einsum returned an external value".into(),
            ));
        };
        Ok(vec![local])
    })?;

    outputs
        .into_iter()
        .next()
        .ok_or_else(|| Error::Internal("expanded einsum produced no output".into()))
}

fn traced_dim_expr_shapes(inputs: &[&TracedTensor]) -> Vec<Vec<DimExpr>> {
    inputs
        .iter()
        .map(|tensor| DimExpr::input_shape(0, tensor.rank))
        .collect()
}

fn symbolic_fixed_path_tree(
    plan_spec: &EinsumPlanSpec,
    subs: &Subscripts,
    inputs: &[&TracedTensor],
) -> Result<Option<ContractionTree>> {
    if matches!(plan_spec, EinsumPlanSpec::Auto(_)) {
        return Ok(None);
    }
    let dummy_shapes = symbolic_dummy_shapes(inputs);
    let shape_refs: Vec<&[usize]> = dummy_shapes.iter().map(Vec::as_slice).collect();
    resolve_plan_spec(plan_spec, subs, &shape_refs)
        .map(Some)
        .map_err(to_tenferro_error)
}

fn symbolic_dummy_shapes(inputs: &[&TracedTensor]) -> Vec<Vec<usize>> {
    inputs.iter().map(|tensor| vec![1; tensor.rank]).collect()
}

fn try_direct_binary_dot_general(
    inputs: &[&TracedTensor],
    subscripts: &EinsumSubscripts,
    optimize: &EinsumOptimize,
) -> Result<Option<TracedTensor>> {
    if inputs.len() != 2 || subscripts.inputs.len() != 2 {
        return Ok(None);
    }
    if !optimize_allows_direct_binary_dot(optimize)? {
        return Ok(None);
    }

    let lhs_labels = &subscripts.inputs[0];
    let rhs_labels = &subscripts.inputs[1];
    if lhs_labels.len() != inputs[0].rank || rhs_labels.len() != inputs[1].rank {
        return Ok(None);
    }
    validate_direct_binary_dot_label_dims(inputs, subscripts)?;

    let Some(plan) =
        try_build_exact_output_binary_dot_plan(lhs_labels, rhs_labels, &subscripts.output)
    else {
        return Ok(None);
    };

    let result = match plan.operand_order {
        BinaryDotOperandOrder::Original => inputs[0].dot_general(inputs[1], plan.config)?,
        BinaryDotOperandOrder::Swapped => inputs[1].dot_general(inputs[0], plan.config)?,
    };
    Ok(Some(result))
}

fn validate_direct_binary_dot_label_dims(
    inputs: &[&TracedTensor],
    subscripts: &EinsumSubscripts,
) -> Result<()> {
    let mut label_dims = std::collections::HashMap::new();
    for (labels, tensor) in subscripts.inputs.iter().zip(inputs.iter()) {
        let Some(shape) = tensor.sym_shape() else {
            continue;
        };
        for (&label, dim) in labels.iter().zip(shape.iter()) {
            let Some(dim) = dim.constant_value() else {
                continue;
            };
            if let Some(existing) = label_dims.insert(label, dim) {
                if existing != dim {
                    return Err(Error::ContractionError(format!(
                        "einsum label {label} has inconsistent dimensions {existing} and {dim}"
                    )));
                }
            }
        }
    }
    Ok(())
}

fn optimize_allows_direct_binary_dot(optimize: &EinsumOptimize) -> Result<bool> {
    match optimize {
        EinsumOptimize::Auto(options) => {
            options.validate().map_err(to_tenferro_error)?;
            Ok(true)
        }
        EinsumOptimize::False => Ok(true),
        EinsumOptimize::Tree(tree) => {
            Ok(tree.step_count() == 1 && matches!(tree.step_pair(0), Some((0, 1)) | Some((1, 0))))
        }
        EinsumOptimize::Nested(_) | EinsumOptimize::Path(_) => Ok(false),
    }
}

fn cached_subscripts(
    caches: &mut ExtensionCacheStore,
    notation: &str,
) -> Result<Arc<ParsedEinsum>> {
    let key = ExtensionCacheKey::new(
        EINSUM_EXTENSION_FAMILY_ID,
        EINSUM_PARSE_CACHE,
        hash_value(&notation),
    );
    if let Some(cached) = caches.get::<Arc<ParsedEinsum>>(&key) {
        return Ok(Arc::clone(cached));
    }

    let parsed = Arc::new(ParsedEinsum {
        subscripts: parse_einsum_subscripts(notation).map_err(to_tenferro_error)?,
    });
    let retained_bytes = saturating_sum([
        notation.len(),
        einsum_subscripts_retained_bytes(&parsed.subscripts),
    ]);
    caches.put(key, Arc::clone(&parsed), retained_bytes);
    Ok(parsed)
}

fn cached_static_tree(
    caches: &mut ExtensionCacheStore,
    subscripts: &EinsumSubscripts,
    plan_spec: &EinsumPlanSpec,
    shapes: &[Vec<usize>],
    build: impl FnOnce() -> EinsumResult<ContractionTree>,
) -> Result<Arc<ContractionTree>> {
    let mut plan_hasher = DefaultHasher::new();
    hash_einsum_plan_spec(plan_spec, &mut plan_hasher);
    let key_data = (subscripts.clone(), shapes.to_vec(), plan_hasher.finish());
    let key = ExtensionCacheKey::new(
        EINSUM_EXTENSION_FAMILY_ID,
        EINSUM_STATIC_PLANS_CACHE,
        hash_value(&key_data),
    );
    if let Some(cached) = caches.get::<Arc<ContractionTree>>(&key) {
        return Ok(Arc::clone(cached));
    }

    let tree = Arc::new(build().map_err(to_tenferro_error)?);
    let retained_bytes = saturating_sum([
        einsum_subscripts_retained_bytes(subscripts),
        saturating_sum(shapes.iter().map(vec_retained_bytes)),
        std::mem::size_of::<u64>(),
        tree.retained_bytes_for_cache_stats(),
    ]);
    caches.put(key, Arc::clone(&tree), retained_bytes);
    Ok(tree)
}

fn concrete_shapes(inputs: &[&TracedTensor]) -> Option<Vec<Vec<usize>>> {
    inputs
        .iter()
        .map(|tensor| {
            tensor
                .sym_shape()?
                .iter()
                .map(|dim| dim.constant_value())
                .collect::<Option<Vec<_>>>()
        })
        .collect()
}

fn infer_symbolic_output_shape(
    subscripts: &EinsumSubscripts,
    inputs: &[&TracedTensor],
) -> Result<Vec<SymDim>> {
    let mut label_dims = std::collections::HashMap::new();
    for (labels, tensor) in subscripts.inputs.iter().zip(inputs.iter()) {
        let shape: Vec<_> = match tensor.sym_shape() {
            Some(shape) => shape.to_vec(),
            None => (0..tensor.rank)
                .map(|axis| tensor.axis_sym_dim(axis))
                .collect::<Result<_>>()?,
        };
        if labels.len() != shape.len() {
            return Err(Error::ContractionError(format!(
                "einsum input rank mismatch: labels={}, shape={}",
                labels.len(),
                shape.len()
            )));
        }
        for (&label, dim) in labels.iter().zip(shape) {
            label_dims.entry(label).or_insert(dim);
        }
    }
    subscripts
        .output
        .iter()
        .map(|label| {
            label_dims.get(label).cloned().ok_or_else(|| {
                Error::ContractionError(format!(
                    "einsum output label {label} is missing from inputs"
                ))
            })
        })
        .collect()
}

fn to_tenferro_error(error: EinsumError) -> Error {
    Error::ContractionError(error.to_string())
}

fn hash_value<T: Hash + ?Sized>(value: &T) -> u64 {
    let mut hasher = DefaultHasher::new();
    value.hash(&mut hasher);
    hasher.finish()
}

#[cfg(test)]
mod tests;