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
499
500
501
// Copyright 2019 Jared Samet
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Implementations of the base-case singleton and pair contractors for different types of contractions.
//!
//! This module defines the `SingletonViewer`, `SingletonContractor`, and `PairContractor` traits as well
//! as the generic "container" objects `EinsumPath`, `SingletonContraction`, and `PairContraction` that
//! hold `Box`ed trait objects of the specific cases determined at runtime to be most appropriate
//! for the requested contraction.
//!
//! The specific singleton and pair contractors defined in the `singleton_contractors` and
//! `pair_contractors` submodules implement the relevant traits defined here. The six specific singleton
//! contractors defined in `singleton_contractors` perform some combination of permutation of the input
//! axes (e.g. `ijk->jki`), diagonalization across repeated but un-summed axes (e.g. `ii->i`), and
//! summation across axes not present in the output index list (e.g. `ijk->j`). Not all of the nine
//! pair contractors defined in `pair_contractors` are currently used as some appear to be slower than others.
//!
//! Each struct implementing one of the `*Contractor` traits performs all the "setup work"
//! required to perform the actual contraction. For example, `HadamardProductGeneral` permutes
//! the input and output tensors and then computes the element-wise product of the two tensors.
//! Given a `SizedContraction` (but no actual tensors), `HadamardProductGeneral::new()` figures out
//! the permutation orders that will be needed so that `contract_pair` can simply execute the two
//! permutations and then produce the element-wise product. This can be thought of as a way of
//! compiling the `einsum` string into a set of instructions and the `EinsumPath` object
//! can be thought of as an AST that is ready to compute a contraction when supplied with an
//! actual set of operands to contract.

use crate::optimizers::{
    generate_optimized_order, ContractionOrder, OperandNumber, OptimizationMethod,
};
use crate::{ArrayLike, SizedContraction};
use ndarray::prelude::*;
use ndarray::LinalgScalar;
use std::collections::HashSet;
use std::fmt::Debug;

mod singleton_contractors;
use singleton_contractors::{
    Diagonalization, DiagonalizationAndSummation, Identity, Permutation, PermutationAndSummation,
    Summation,
};

mod pair_contractors;
pub use pair_contractors::TensordotGeneral;
use pair_contractors::{
    BroadcastProductGeneral, HadamardProduct, HadamardProductGeneral, MatrixScalarProduct,
    MatrixScalarProductGeneral, ScalarMatrixProduct, ScalarMatrixProductGeneral,
    StackedTensordotGeneral, TensordotFixedPosition,
};

mod strategies;
use strategies::{PairMethod, PairSummary, SingletonMethod, SingletonSummary};

#[cfg(feature = "serde")]
use serde::Serialize;

/// `let new_view = obj.view_singleton(tensor_view);`
///
/// This trait represents contractions that can be performed by returning a view of the original
/// tensor. The structs that currently implement this view are the ones that don't perform
/// any summation over indices and hence return only a subset of the elements of the original tensor:
/// `Identity`, `Permutation`, and `Diagonalization`. Note that whether `Diagonalization`
/// can actually return a view is dependent on the memory layout of the input tensor; if the input
/// tensor is not contiguous, `diag.view_singleton()` will `panic`.
pub trait SingletonViewer<A>: Debug {
    fn view_singleton<'a, 'b>(&self, tensor: &'b ArrayViewD<'a, A>) -> ArrayViewD<'b, A>
    where
        'a: 'b,
        A: Clone + LinalgScalar;
}

/// `let new_array = obj.contract_singleton(tensor_view);`
///
/// All singleton contractions should implement this trait. It returns a new owned `ArrayD`.
pub trait SingletonContractor<A>: Debug {
    fn contract_singleton<'a, 'b>(&self, tensor: &'b ArrayViewD<'a, A>) -> ArrayD<A>
    where
        'a: 'b,
        A: Clone + LinalgScalar;
}

/// `let new_array = obj.contract_pair(lhs_view, rhs_view);`
///
/// All pair contractions should implement this trait. It returns a new owned `ArrayD`. The trait
/// also has a method with a default implementation, `obj.contract_and_assign_pair(lhs_view: &ArrayViewD,
/// rhs_view: &ArrayViewD, out: &mut ArrayViewD) -> ()`.
pub trait PairContractor<A>: Debug {
    fn contract_pair<'a, 'b, 'c, 'd>(
        &self,
        lhs: &'b ArrayViewD<'a, A>,
        rhs: &'d ArrayViewD<'c, A>,
    ) -> ArrayD<A>
    where
        'a: 'b,
        'c: 'd,
        A: Clone + LinalgScalar;

    fn contract_and_assign_pair<'a, 'b, 'c, 'd, 'e, 'f>(
        &self,
        lhs: &'b ArrayViewD<'a, A>,
        rhs: &'d ArrayViewD<'c, A>,
        out: &'f mut ArrayViewMutD<'e, A>,
    ) where
        'a: 'b,
        'c: 'd,
        'e: 'f,
        A: Clone + LinalgScalar,
    {
        let result = self.contract_pair(lhs, rhs);
        out.assign(&result);
    }
}

/// Holds a `Box`ed `SingletonContractor` trait object.
///
/// Constructed at runtime based on the number of diagonalized, summed, and permuted axes
/// in the input. Reimplements the `SingletonContractor` trait by delegating to the inner
/// object.
///
/// For example, the contraction `iij->i` will be performed by assigning a `Box`ed
/// `DiagonalizationAndSummation` to `op`. The contraction `ijk->kij` will be performed
/// by assigning a `Box`ed `Permutation` to `op`.
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct SingletonContraction<A> {
    method: SingletonMethod,
    #[cfg_attr(feature = "serde", serde(skip))]
    op: Box<dyn SingletonContractor<A>>,
}

impl<A> SingletonContraction<A> {
    pub fn new(sc: &SizedContraction) -> Self {
        let singleton_summary = SingletonSummary::new(&sc);
        let method = singleton_summary.get_strategy();

        SingletonContraction {
            method,
            op: match method {
                SingletonMethod::Identity => Box::new(Identity::new(sc)),
                SingletonMethod::Permutation => Box::new(Permutation::new(sc)),
                SingletonMethod::Summation => Box::new(Summation::new(sc)),
                SingletonMethod::Diagonalization => Box::new(Diagonalization::new(sc)),
                SingletonMethod::PermutationAndSummation => {
                    Box::new(PermutationAndSummation::new(sc))
                }
                SingletonMethod::DiagonalizationAndSummation => {
                    Box::new(DiagonalizationAndSummation::new(sc))
                }
            },
        }
    }
}

impl<A> SingletonContractor<A> for SingletonContraction<A> {
    fn contract_singleton<'a, 'b>(&self, tensor: &'b ArrayViewD<'a, A>) -> ArrayD<A>
    where
        'a: 'b,
        A: Clone + LinalgScalar,
    {
        self.op.contract_singleton(tensor)
    }
}

impl<A> Debug for SingletonContraction<A> {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(
            f,
            "SingletonContraction {{ method: {:?}, op: {:?} }}",
            self.method, self.op
        )
    }
}

/// Holds an `Box<dyn SingletonContractor<A>>` and the resulting simplified indices.
#[cfg_attr(feature = "serde", derive(Serialize))]
struct SimplificationMethodAndOutput<A> {
    method: SingletonMethod,
    #[cfg_attr(feature = "serde", serde(skip))]
    op: Box<dyn SingletonContractor<A>>,
    new_indices: Vec<char>,
    einsum_string: String,
}

impl<A> SimplificationMethodAndOutput<A> {
    /// Based on the number of diagonalized, permuted, and summed axes, chooses a struct implementing
    /// `SingletonContractor` to simplify the tensor (or `None` if the tensor doesn't need simplification)
    /// and computes the indices of the simplified tensor.
    fn from_indices_and_sizes(
        this_input_indices: &[char],
        other_input_indices: &[char],
        output_indices: &[char],
        orig_contraction: &SizedContraction,
    ) -> Option<Self> {
        let this_input_uniques: HashSet<char> = this_input_indices.iter().cloned().collect();
        let other_input_uniques: HashSet<char> = other_input_indices.iter().cloned().collect();
        let output_uniques: HashSet<char> = output_indices.iter().cloned().collect();

        let other_and_output: HashSet<char> = other_input_uniques
            .union(&output_uniques)
            .cloned()
            .collect();
        let desired_uniques: HashSet<char> = this_input_uniques
            .intersection(&other_and_output)
            .cloned()
            .collect();
        let new_indices: Vec<char> = desired_uniques.iter().cloned().collect();

        let simplification_sc = orig_contraction
            .subset(&[this_input_indices.to_vec()], &new_indices)
            .unwrap();

        let SingletonContraction { method, op } = SingletonContraction::new(&simplification_sc);

        match method {
            SingletonMethod::Identity | SingletonMethod::Permutation => None,
            _ => Some(SimplificationMethodAndOutput {
                method,
                op,
                new_indices,
                einsum_string: simplification_sc.as_einsum_string(),
            }),
        }
    }
}

impl<A> Debug for SimplificationMethodAndOutput<A> {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(
            f,
            "SingletonContraction {{ method: {:?}, op: {:?}, new_indices: {:?}, einsum_string: {:?} }}",
            self.method, self.op, self.new_indices, self.einsum_string
        )
    }
}

/// Holds a `Box`ed `PairContractor` trait object and two `Option<Box>`ed simplifications for the LHS and RHS tensors.
///
/// For example, the contraction `ijk,kj->jk` will currently be performed as follows:
///
/// 1. Simplify the LHS with the contraction `ijk->jk`
/// 2. Don't simplify the RHS
/// 3. Use HadamardProductGeneral to compute `jk,kj->jk`
///
/// A second example is the contraction `iij,jkk->ik`:
///
/// 1. Simplify the LHS with the contraction `iij->ij`
/// 2. Simplify the RHS with the contraction `jkk->jk`
/// 3. Use TensordotGeneral to compute `ij,jk->ik`
///
/// Since the axis lengths aren't known until runtime, and the actual einsum string may not
/// be either, it is generally not possible to know at compile time which specific PairContractor
/// will be used to perform a given contraction, or even which contractions will be performed;
/// the optimizer could choose a different order.
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct PairContraction<A> {
    lhs_simplification: Option<SimplificationMethodAndOutput<A>>,
    rhs_simplification: Option<SimplificationMethodAndOutput<A>>,
    method: PairMethod,
    #[cfg_attr(feature = "serde", serde(skip))]
    op: Box<dyn PairContractor<A>>,
    simplified_einsum_string: String,
}

impl<A> PairContraction<A> {
    pub fn new(sc: &SizedContraction) -> Self {
        assert_eq!(sc.contraction.operand_indices.len(), 2);
        let lhs_indices = &sc.contraction.operand_indices[0];
        let rhs_indices = &sc.contraction.operand_indices[1];
        let output_indices = &sc.contraction.output_indices;

        let lhs_simplification = SimplificationMethodAndOutput::from_indices_and_sizes(
            &lhs_indices,
            &rhs_indices,
            &output_indices,
            sc,
        );
        let rhs_simplification = SimplificationMethodAndOutput::from_indices_and_sizes(
            &rhs_indices,
            &lhs_indices,
            &output_indices,
            sc,
        );
        let new_lhs_indices = match &lhs_simplification {
            Some(ref s) => s.new_indices.clone(),
            None => lhs_indices.clone(),
        };
        let new_rhs_indices = match &rhs_simplification {
            Some(ref s) => s.new_indices.clone(),
            None => rhs_indices.clone(),
        };

        let reduced_sc = sc
            .subset(&[new_lhs_indices, new_rhs_indices], &output_indices)
            .unwrap();

        let pair_summary = PairSummary::new(&reduced_sc);
        let method = pair_summary.get_strategy();

        let op: Box<dyn PairContractor<A>> = match method {
            PairMethod::HadamardProduct => {
                // Never gets returned in current implementation
                Box::new(HadamardProduct::new(&reduced_sc))
            }
            PairMethod::HadamardProductGeneral => {
                Box::new(HadamardProductGeneral::new(&reduced_sc))
            }
            PairMethod::ScalarMatrixProduct => {
                // Never gets returned in current implementation
                Box::new(ScalarMatrixProduct::new(&reduced_sc))
            }
            PairMethod::ScalarMatrixProductGeneral => {
                Box::new(ScalarMatrixProductGeneral::new(&reduced_sc))
            }
            PairMethod::MatrixScalarProduct => {
                // Never gets returned in current implementation
                Box::new(MatrixScalarProduct::new(&reduced_sc))
            }
            PairMethod::MatrixScalarProductGeneral => {
                Box::new(MatrixScalarProductGeneral::new(&reduced_sc))
            }
            PairMethod::TensordotFixedPosition => {
                // Never gets returned in current implementation
                Box::new(TensordotFixedPosition::new(&reduced_sc))
            }
            PairMethod::TensordotGeneral => Box::new(TensordotGeneral::new(&reduced_sc)),
            PairMethod::StackedTensordotGeneral => {
                Box::new(StackedTensordotGeneral::new(&reduced_sc))
            }
            PairMethod::BroadcastProductGeneral => {
                // Never gets returned in current implementation
                Box::new(BroadcastProductGeneral::new(&reduced_sc))
            }
        };
        PairContraction {
            lhs_simplification,
            rhs_simplification,
            method,
            op,
            simplified_einsum_string: reduced_sc.as_einsum_string(),
        }
    }
}

impl<A> PairContractor<A> for PairContraction<A> {
    fn contract_pair<'a, 'b, 'c, 'd>(
        &self,
        lhs: &'b ArrayViewD<'a, A>,
        rhs: &'d ArrayViewD<'c, A>,
    ) -> ArrayD<A>
    where
        'a: 'b,
        'c: 'd,
        A: Clone + LinalgScalar,
    {
        match (&self.lhs_simplification, &self.rhs_simplification) {
            (None, None) => self.op.contract_pair(lhs, rhs),
            (Some(lhs_contraction), None) => self
                .op
                .contract_pair(&lhs_contraction.op.contract_singleton(lhs).view(), rhs),
            (None, Some(rhs_contraction)) => self
                .op
                .contract_pair(lhs, &rhs_contraction.op.contract_singleton(rhs).view()),
            (Some(lhs_contraction), Some(rhs_contraction)) => self.op.contract_pair(
                &lhs_contraction.op.contract_singleton(lhs).view(),
                &rhs_contraction.op.contract_singleton(rhs).view(),
            ),
        }
    }
}

impl<A> Debug for PairContraction<A> {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(
            f,
            "PairContraction {{ \
             lhs_simplification: {:?}, \
             rhs_simplification: {:?}, \
             method: {:?}, \
             op: {:?}, \
             simplified_einsum_string: {:?}",
            self.lhs_simplification,
            self.rhs_simplification,
            self.method,
            self.op,
            self.simplified_einsum_string
        )
    }
}

/// Either a singleton contraction, in the case of a single input operand, or a list of pair contractions,
/// given two or more input operands
#[cfg_attr(feature = "serde", derive(Serialize))]
#[derive(Debug)]
pub enum EinsumPathSteps<A> {
    /// A `SingletonContraction` consists of some combination of permutation of the input axes,
    /// diagonalization of repeated indices, and summation across axes not present in the output
    SingletonContraction(SingletonContraction<A>),

    /// Each `PairContraction` consists of a possible simplification of each of the two input tensors followed
    /// by a contraction of the two simplified tensors. The two simplified tensors can be combined in a
    /// number of fashions.
    PairContractions(Vec<PairContraction<A>>),
}

/// An `EinsumPath`, returned by [`einsum_path`](fn.einsum_path.html), represents a fully-prepared plan to perform a tensor contraction.
///
/// It contains the order in which the input tensors should be contracted with one another or with one of the previous intermediate results,
/// and for each step in the path, how to perform the pairwise contraction. For example, two tensors might be contracted
/// with one another by computing the Hadamard (element-wise) product of the tensors, while a different pair might be contracted
/// by performing a matrix multiplication. The contractions that will be performed are fully specified within the `EinsumPath`.
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct EinsumPath<A> {
    /// The order in which tensors should be paired off and contracted with one another
    pub contraction_order: ContractionOrder,

    /// The details of the contractions to be performed
    pub steps: EinsumPathSteps<A>,
}

impl<A> EinsumPath<A> {
    pub fn new(sc: &SizedContraction) -> Self {
        let contraction_order = generate_optimized_order(&sc, OptimizationMethod::Naive);

        EinsumPath::from_path(&contraction_order)
    }

    pub fn from_path(contraction_order: &ContractionOrder) -> Self {
        match contraction_order {
            ContractionOrder::Singleton(sized_contraction) => EinsumPath {
                contraction_order: contraction_order.clone(),
                steps: EinsumPathSteps::SingletonContraction(SingletonContraction::new(
                    sized_contraction,
                )),
            },
            ContractionOrder::Pairs(order_steps) => {
                let mut steps = Vec::new();

                for step in order_steps.iter() {
                    steps.push(PairContraction::new(&step.sized_contraction));
                }

                EinsumPath {
                    contraction_order: contraction_order.clone(),
                    steps: EinsumPathSteps::PairContractions(steps),
                }
            }
        }
    }
}

impl<A> EinsumPath<A> {
    pub fn contract_operands(&self, operands: &[&dyn ArrayLike<A>]) -> ArrayD<A>
    where
        A: Clone + LinalgScalar,
    {
        // Uncomment for help debugging
        // println!("{:?}", self);
        match (&self.steps, &self.contraction_order) {
            (EinsumPathSteps::SingletonContraction(c), ContractionOrder::Singleton(_)) => {
                c.contract_singleton(&operands[0].into_dyn_view())
            }
            (EinsumPathSteps::PairContractions(steps), ContractionOrder::Pairs(order_steps)) => {
                let mut intermediate_results: Vec<ArrayD<A>> = Vec::new();
                for (step, order_step) in steps.iter().zip(order_steps.iter()) {
                    let lhs = match order_step.operand_nums.lhs {
                        OperandNumber::Input(pos) => operands[pos].into_dyn_view(),
                        OperandNumber::IntermediateResult(pos) => intermediate_results[pos].view(),
                    };
                    let rhs = match order_step.operand_nums.rhs {
                        OperandNumber::Input(pos) => operands[pos].into_dyn_view(),
                        OperandNumber::IntermediateResult(pos) => intermediate_results[pos].view(),
                    };
                    let intermediate_result = step.contract_pair(&lhs, &rhs);
                    // let lhs = match order_step.
                    intermediate_results.push(intermediate_result);
                }
                intermediate_results.pop().unwrap()
            }
            _ => panic!(), // steps and contraction_order don't match
        }
    }
}

impl<A> Debug for EinsumPath<A> {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match &self.steps {
            EinsumPathSteps::SingletonContraction(step) => write!(f, "only_step: {:?}", step),
            EinsumPathSteps::PairContractions(steps) => write!(f, "steps: {:?}", steps),
        }
    }
}