Tensor

Tensors are multi-dimension, dense arrays with more than 2 dimensions. Currently 3 - 5 dimension Tensors are supported.

Like Array and Matrix, Tensors can only be used in host code. In accelerator code, use DRAM (for off-chip) or SRAM (on-chip) memories for multi-dimensional array support.


Constructors

Spatial includes syntax for constructing Tensor instances from indexed functions.

The following returns a 16 x 32 x 8 Tensor3, with elements defined by func(i,j,k):

(0::16, 0::32, 0::8){(i,j,k) => func(i,j,k) }

A Tensor4 can be constructed in a similar way:

(0::4, 0::16, 0::8, 0::32){(a,b,c,d) => func(a,b,c,d) }

As can a Tensor5:

(0::2, 0::4, 0::5, 0::3, 0::32){(a,b,c,d,e) => func(a,b,c,d,e) }

More general Range forms can also be used, including strided (e.g. 0::2::8) and offset (e.g. 32::64). Iterators (e.g. i, j, k in the above examples) will iterate over all values in their respective ranges.


Static methods

object Tensor3
def tabulate[T:Type](dim0: Index, dim1: Index, dim2: Index)(func: (Index, Index, Index) => T): Tensor3[T]
Returns an immutable Tensor3 with the given dimensions and elements defined by func.
def fill[T:Type](dim0: Index, dim1: Index, dim2: Index)(func: => T): Tensor3[T]
Returns an immutable Tensor3 with the given dimensions and elements defined by func.
Note that while func does not depend on the index, it is still executed multiple times.
object Tensor4
def tabulate[T:Type](dim0: Index, dim1: Index, dim2: Index, dim3: Index)(func: (Index, Index, Index, Index) => T): Tensor4[T]
Returns an immutable Tensor4 with the given dimensions and elements defined by func.
def fill[T:Type](dim0: Index, dim1: Index, dim2: Index, dim3: Index)(func: => T): Tensor4[T]
Returns an immutable Tensor4 with the given dimensions and elements defined by func.
Note that while func does not depend on the index, it is still executed multiple times.
object Tensor5
def tabulate[T:Type](dim0: Index, dim1: Index, dim2: Index, dim3: Index, dim4: Index)(func: (Index, Index, Index, Index, Index) => T): Tensor5[T]
Returns an immutable Tensor5 with the given dimensions and elements defined by func.
def fill[T:Type](dim0: Index, dim1: Index, dim2: Index, dim3: Index, dim4: Index)(func: => T): Tensor5[T]
Returns an immutable Tensor5 with the given dimensions and elements defined by func.
Note that while func does not depend on the index, it is still executed multiple times.

Infix methods

class Tensor3[T]
def dim0: Index
Returns the first dimension of this Tensor3.
def dim1: Index
Returns the second dimension of this Tensor3.
def dim2: Index
Returns the third dimension of this Tensor3.
def apply(i: Index, j: Index, k: Index): T
Returns the element in this Tensor3 at the given 3-dimensional address.
def update(i: Index, j: Index, k: Index, elem: T): Unit
Updates the element at the given 3-dimensional address to elem.
def flatten: Array[T]
Returns a flattened, immutable Array view of this Tensor3’s data.
def foreach(func: T => Unit): Unit
Applies the function func on each element in this Tensor3.
def map[R:Type](func: T => R): Tensor3[R]
Returns a new Tensor3 created using the mapping func over each element in this Tensor3.
def zip[S,R:Type](that: Tensor3[S])(func: (T,S) => R): Tensor3[R]
Returns a new Tensor3 created using the pairwise mapping func over each element in this Tensor3
and the corresponding element in that.
def reduce(rfunc: (T,T) => T): T
Reduces the elements in this Tensor3 into a single element using associative function rfunc.
class Tensor4[T]
def dim0: Index
Returns the first dimension of this Tensor4.
def dim1: Index
Returns the second dimension of this Tensor4.
def dim2: Index
Returns the third dimension of this Tensor4.
def dim3: Index
Returns the fourth dimension of this Tensor4.
def apply(i: Index, j: Index, k: Index, l: Index): T
Returns the element in this Tensor4 at the given 4-dimensional address.
def update(i: Index, j: Index, k: Index, l: Index, elem: T): Unit
Updates the element at the given 4-dimensional address to elem.
def flatten: Array[T]
Returns a flattened, immutable Array view of this Tensor4’s data.
def foreach(func: T => Unit): Unit
Applies the function func on each element in this Tensor4.
def map[R:Type](func: T => R): Tensor4[R]
Returns a new Tensor4 created using the mapping func over each element in this Tensor4.
def zip[S,R:Type](b: Tensor4[S])(func: (T,S) => R): Tensor4[R]
Returns a new Tensor4 created using the pairwise mapping func over each element in this Tensor4
and the corresponding element in that.
def reduce(rfunc: (T,T) => T): T
Reduces the elements in this Tensor4 into a single element using associative function rfunc.
class Tensor5[T]
def dim0: Index
Returns the first dimension of this Tensor5.
def dim1: Index
Returns the second dimension of this Tensor5.
def dim2: Index
Returns the third dimension of this Tensor5.
def dim3: Index
Returns the fourth dimension of this Tensor5.
def dim4: Index
Returns the fifth dimension of this Tensor5.
def apply(i: Index, j: Index, k: Index, l: Index, m: Index): T
Returns the element in this Tensor5 at the given 5-dimensional addreess.
def update(i: Index, j: Index, k: Index, l: Index, m: Index, elem: T): Unit
Updates the element at the given 5-dimensional address to elem.
def flatten: Array[T]
Returns a flattened, immutable Array view of this Tensor5’s data.
def foreach(func: T => Unit): Unit
Applies the function func on each element in this Tensor5.
def map[R:Type](func: T => R): Tensor5[R]
Returns a new Tensor5 created using the mapping func over each element in this Tensor5.
def zip[S,R:Type](b: Tensor5[S])(func: (T,S) => R): Tensor5[R]
Returns a new Tensor5 created using the pairwise mapping func over each element in this Tensor5
and the corresponding element in that.
def reduce(rfunc: (T,T) => T): T
Reduces the elements in this Tensor5 into a single element using associative function rfunc.