Tensor Format Object

class pytaco.format(mode_formats=[], mode_ordering=[])

Create a tensor format.

The modes have the given mode storage formats and are stored in the given sequence. Mode i has the mode_format specified by mode_formats[mode_ordering[i]].

If no arguments are given a format for a 0-order tensor (a scalar) is created.

mode_formats: pytaco.mode_format, iterable of pytaco.mode_format, optional

A list representing the mode format used to store each mode (dimension) of the tensor specified by mode_ordering[i]. If a single mode_format is given, then a format for a 1-order tensor (vector) is created. The default value is the empty list meaning a scalar is created.

mode_ordering: int, iterable of ints, optional

Can be specified if len(mode_formats) > 1. Specifies the order in which the dimensions (modes) of the tensor should be stored in memory. That is, the mode stored in the i-th position in memory is specified by mode_ordering[i]. Defaults to mode_ordering[i] = i which corresponds to row-major storage.


PyTaco exports the following common formats:

csr or CSR - Compressed Sparse Row storage format.

csc or CSC - Compressed Sparse Columns storage format.


Here, we will create two common storage formats CSR and CSC in order to better understand formats. First, we look at CSR.

We need a mode formats list to tell taco the first dimension it stores should be dense and the second dimension should be sparse.

>>> import pytaco as pt
>>> mode_formats = [pt.dense, pt.compressed]

We then need to tell taco the order in which to store the dimensions. Since we want CSR, we want to store the rows first then the columns. Once we do this, we can make the format.

>>> mode_ordering = [0, 1] # Taco will default this if no ordering is given.
>>> csr = pt.format(mode_formats, mode_ordering)
>>> csr.order

Now, it is easy to make a CSC format given what we have already. For CSC, we want to store the columns before the rows but also have the columns be dense and the rows be sparse. We do so as follows:

>>> mode_ordering_csc = [1,0]
>>> csc = pt.format(mode_formats, mode_ordering_csc)

This tells taco to store the columns before the rows due to the ordering given and to store the columns as dense since they are now the first storage dimension and the mode_formats[0] is dense.

We can generalize this to make a large number of storage formats.


Returns the number of modes (dimensions) stored in a format.


Returns the storage types of the modes.


Returns a list representing the ordering in which the modes are stored.