pytaco.from_array(array, copy=True)

Convert a NumPy array to a tensor.

Initializes a taco tensor from a NumPy array and copies the array by default. This always creates a dense tensor.

array: numpy.array

A NumPy array to convert to a taco tensor

copy: boolean, optional

If true, taco copies the data from NumPy and stores its own copy. If false, taco points to the same underlying data as the NumPy array.

t: tensor

A taco tensor pointing to the same underlying data as the NumPy array if copy was set to False. Otherwise, returns a taco tensor containing data copied from the NumPy array.


Taco’s changes to tensors may NOT be visible to NumPy since taco places inserts in buffers may copy tensor data after inserting. See notes for details.


The copy flag is ignored if the input array is not C contiguous or F contiguous (so for most transposed views). If taco detects an array that is not contiguous, it will always copy the NumPy array into a C contiguous format. Additionally, if the GPU backend is enabled, taco will always copy the NumPy array to CUDA unified memory. These restriction will be lifted in future versions of taco.

Taco is mainly intended to operate on sparse tensors. As a result, it buffers inserts since inserting into sparse structures is very costly. This means that when the full tensor structure is needed, taco will copy the tensor to another location and insert the new values as needed. This saves a lot of time when dealing with sparse structures but is not needed for dense tensors (like NumPy arrays). Currently, taco does this copy for dense and sparse tensors. As a result, after inserting into a taco tensor, NumPy will not see the changes since taco will not be writing to the same memory location that NumPy is referencing.


If we choose not to copy, modifying the tensor also modifies the NumPy array and vice-versa. An example of this is shown:

>>> import numpy as np
>>> import pytaco as pt
>>> arr = np.array([0, 1, 2, 3]) # Note that this is contiguous so copy possible
>>> t = pt.from_array(arr, copy=False)
>>> arr[0] = 23
>>> t[0]