Tensor

public class Tensor: Hashable, Equatable, TensorSymbol

A Tensor object is a n-dimension page-aligned data container with fixed-size memory space. The attribute shape specifying the dimension information of a Tensor object.

All elements stored as Float values

No matter what type of array user passing in: Double or Float or Int, inside a Tensor object values will be converted and stored as Float (32-bit signle precision). Serrano chose this because 32-bit Float is the maximum precise floating data type Metal could support (v1.2).

Inside Memory Layout

Inside a Tensor object, it maintains a virtual 1-d array as a contiguous memory space manually allocated. Elements are stored following row-major order. Details can be found in TensorShape docs.

Tensor-Tensor arithmetic operators

Besides Operators which contains many math and NN operations, Tensor object itself implements/overloads common arithmetic operators. Tensor object support element-wise arithmetic operation with or without broadcasting. Also it supports in-place operation choice.

Element-wise operation without broadcasting:

  • + Addition without broadcasting
  • - Substraction without broadcasting
  • * Multiplication without broadcasting
  • / Division without broadcasting

Element-wise in-place operation:

  • &+ Addition without broadcasting
  • &- Substraction without broadcasting
  • &* Multiplication without broadcasting
  • &/ Division without broadcasting

Element-wise operation with broadcasting:

  • .+ Addition without broadcasting
  • .- Substraction without broadcasting
  • .* Multiplication without broadcasting
  • ./ Division without broadcasting

Element-wise in-place operation with broadcasting:

  • .&+ Addition without broadcasting
  • .&- Substraction without broadcasting
  • .&* Multiplication without broadcasting
  • .&/ Division without broadcasting

Example usage:

/// Element wise addition without broadcasting
let tensorA = Tensor(repeatingValue: 1.0,
                     tensorShape: TensorShape(dataType:.float, shape: [2, 5]))
let tensorB = Tensor(repeatingValue: 2.0,
                     tensorShape: TensorShape(dataType:.float, shape: [2, 5]))
let result1 = tensorA + tensorB

/// Element wise in-place addition without broadcasting
let result2 = tensorA &+ tensorB
print(result2 == tensorA) // true

/// Element wise addition with broadcasting
let tensorD = Tensor(repeatingValue: 2.0,
                     tensorShape: TensorShape(dataType:.float, shape: [1, 5]))
let result3 = tensorA .+ tensorD

/// Element wise in-place addition with broadcasting
let resunt4 = tensorA .&+ tensorD
print(result4 == tensorA) // true

Directly used as TensorSymbol

Tensor conforms to TensorSymbol protocol. So a tensor object could be used as a symbol participating in graph computation.

  • Check if index is valid for fetching a single element

    Declaration

    Swift

    public func indexIsValid(_ index: [Int]) -> Bool

    Parameters

    index

    index description

    Return Value

    return value description

  • Get offset in terms of element counting from valid index

    Declaration

    Swift

    internal func offSetFromIndex(_ index: [Int]) -> Int

    Parameters

    index

    valid index list

    Return Value

    offset

  • Custom subscript to fetch single Float element

    Declaration

    Swift

    public subscript(_ index: Int...) -> Float

    Parameters

    index

    Indices list

  • Custom subscript to fetch single Float element

    Declaration

    Swift

    public subscript(_ index: [Int]) -> Float

    Parameters

    index

    Indices list

  • Internal use, already checked boundary before fetch or set

    Warning

    May cause unexpected result or fatal error if index is not valid.

    Declaration

    Swift

    public subscript(withoutChecking index:[Int]) -> Float

    Parameters

    index

    index array

  • Get element value from index. If input index is invalid, return missingValue.

    Declaration

    Swift

    public func fetchValueOrDefault(_ index: [Int], missingValue: Float = 0.0) -> Float

    Parameters

    index

    index array

    missingValue

    default value for missing elements. Default is 0.0

    Return Value

    value

  • Reload data from a flat array.

    Note

    If array size < tensor’s count

    Declaration

    Swift

    public func reloadData(fromFlatArray array:[SupportedScalarDataType], tensorShape shape: TensorShape)

    Parameters

    array

  • Set tensor’s all selements to a same value.

    Warning

    This operation will erase current values of tensor object.

    Declaration

    Swift

    public func resetValues(_ value:SupportedScalarDataType)

    Parameters

    value

    initial value

  • Set tensor’s all elements to 0.

    Warning

    This operation will erase current values of tensor object.

    Declaration

    Swift

    public func clear()

    Parameters

    value

    initial value

  • Copy values from another tensor.

    Dimension check

    This function just check cp from tensor’s total element count. If two tensors has same number of elements, do the copy. If not, raise fatalError

    Declaration

    Swift

    public func copyValues(_ tensor: Tensor)

    Parameters

    tensor

    cp from tensor

  • Return flat array containing all elements stored in tensor with Float type.

    Declaration

    Swift

    public func flatArrayFloat() -> [Float]

    Return Value

    Type of declared.

  • Return nested array following object’s shape.

    Declaration

    Swift

    public func nestedArrayFloat() -> [Any]

    Return Value

    A nested array with same shape in shape attribute.

  • Recursively construct nested array

    Declaration

    Swift

    internal func constructNestedArrayFrom(location: [Int]) -> [Any]

    Parameters

    shape

    shape needs to construct

    Return Value

    result

  • /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    Declaration

    Swift

    public var hashValue: Int
  • /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    Declaration

    Swift

    public static func  ==(lhs: Tensor, rhs: Tensor) -> Bool
  • Return a new tensor contains the absolute values of this tensor. Same effect as using AbsOperator.

    Declaration

    Swift

    public func abs() -> Tensor

    Return Value

    new tensor object with absed values.

  • Element-wise addition. Not support braodcasting. Same effect as using AddOperator.

    Warning

    If two tensors don’t have same shape, program would be aborted (fatalError() called).

    • left: left tensor
    • right: right tensor

    Declaration

    Swift

    public static func +(left: Tensor, right:Tensor) -> Tensor

    Parameters

    left

    left tensor

    right

    right tensor

    Return Value

    result tensor

  • Element-wise substraction. Not support braodcasting. Same effect as using SubOperator.

    Warning

    If two tensors don’t have same shape, program would be aborted (fatalError() called).

    • left: left tensor
    • right: right tensor

    Declaration

    Swift

    public static func -(left: Tensor, right:Tensor) -> Tensor

    Parameters

    left

    left tensor

    right

    right tensor

    Return Value

    result tensor

  • Element-wise multiplication. Not support braodcasting. Same effect as using AddOperator.

    Warning

    If two tensors don’t have same shape, program would be aborted (fatalError() called).

    • left: left tensor
    • right: right tensor

    Declaration

    Swift

    public static func *(left: Tensor, right:Tensor) -> Tensor

    Parameters

    left

    left tensor

    right

    right tensor

    Return Value

    result tensor

  • Element-wise division. Not support braodcasting. Same effect as using DivOperator.

    Warning

    If two tensors don’t have same shape, program would be aborted (fatalError() called).

    • left: left tensor
    • right: right tensor

    Declaration

    Swift

    public static func /(left: Tensor, right:Tensor) -> Tensor

    Parameters

    left

    left tensor

    right

    right tensor

    Return Value

    result tensor

  • Element-wise in-place addition. Not support braodcasting. Same effect as using AddOperator.

    Note

    This is an in place operation. Result stored in left tensor and return the calcualted left tensor.

    Warning

    If two tensors don’t have same shape, program would be aborted (fatalError() called).

    Declaration

    Swift

    @discardableResult public static func &+(left: Tensor, right:Tensor) -> Tensor

    Parameters

    left

    left tensor

    right

    right tensor

    Return Value

    left tensor after calculated.

  • Element-wise in-place substraction. Not support braodcasting. Same effect as using SubOperator.

    Note

    This is an in place operation. Result stored in left tensor and return the calcualted left tensor.

    Warning

    If two tensors don’t have same shape, program would be aborted (fatalError() called).

    Declaration

    Swift

    @discardableResult public static func &-(left: Tensor, right:Tensor) -> Tensor

    Parameters

    left

    left tensor

    right

    right tensor

    Return Value

    left tensor after calculated.

  • Element-wise in-place multiplication. Not support braodcasting. Same effect as using MultOperator.

    Note

    This is an in place operation. Result stored in left tensor and return the calcualted left tensor.

    Warning

    If two tensors don’t have same shape, program would be aborted (fatalError() called).

    Declaration

    Swift

    @discardableResult public static func &*(left: Tensor, right:Tensor) -> Tensor

    Parameters

    left

    left tensor

    right

    right tensor

    Return Value

    left tensor after calculated.

  • Element-wise in-place division. Not support braodcasting. Same effect as using DivOperator.

    Note

    This is an in place operation. Result stored in left tensor and return the calcualted left tensor.

    Warning

    If two tensors don’t have same shape, program would be aborted (fatalError() called).

    Declaration

    Swift

    @discardableResult public static func &/(left: Tensor, right:Tensor) -> Tensor

    Parameters

    left

    left tensor

    right

    right tensor

    Return Value

    left tensor after calculated.

  • Element-wise addition. support braodcasting. Same effect as using BrodcastAddOperator.

    Warning

    If two tensors don’t have same shape or one of them cannot be broadcasted to anoter one, program would be aborted (fatalError() called).

    • left: left tensor
    • right: right tensor

    Declaration

    Swift

    public static func .+(left: Tensor, right:Tensor) -> Tensor

    Parameters

    left

    left tensor

    right

    right tensor

    Return Value

    result tensor

  • Element-wise substraction. support braodcasting. Same effect as using BroadcastSubOperator.

    Warning

    If two tensors don’t have same shape or one of them cannot be broadcasted to anoter one, program would be aborted (fatalError() called).

    • left: left tensor
    • right: right tensor

    Declaration

    Swift

    public static func .-(left: Tensor, right:Tensor) -> Tensor

    Parameters

    left

    left tensor

    right

    right tensor

    Return Value

    result tensor

  • Element-wise multiplication. support braodcasting. Same effect as using BroadcastMultOperator.

    Warning

    If two tensors don’t have same shape or one of them cannot be broadcasted to anoter one, program would be aborted (fatalError() called).

    • left: left tensor
    • right: right tensor

    Declaration

    Swift

    public static func .*(left: Tensor, right:Tensor) -> Tensor

    Parameters

    left

    left tensor

    right

    right tensor

    Return Value

    result tensor

  • Element-wise division. support braodcasting. Same effect as using BroadcastDivOperator.

    Warning

    If two tensors don’t have same shape or one of them cannot be broadcasted to anoter one, program would be aborted (fatalError() called).

    • left: left tensor
    • right: right tensor

    Declaration

    Swift

    public static func ./(left: Tensor, right:Tensor) -> Tensor

    Parameters

    left

    left tensor

    right

    right tensor

    Return Value

    result tensor, a new created tensor.

  • Element-wise in-place addition. support braodcasting. Same effect as using BrodcastAddOperator.

    Note

    This is an in place operation. Result stored in left tensor and return the calcualted left tensor.

    Warning

    If two tensors don’t have same shape or one of them cannot be broadcasted to anoter one, program would be aborted (fatalError() called).

    Declaration

    Swift

    @discardableResult public static func .&+(left: Tensor, right:Tensor) -> Tensor

    Parameters

    left

    left tensor

    right

    right tensor

    Return Value

    left tensor after calcualted

  • Element-wise in-place substraction. support braodcasting. Same effect as using BroadcastSubOperator.

    Note

    This is an in place operation. Result stored in left tensor and return the calcualted left tensor.

    Warning

    If two tensors don’t have same shape or one of them cannot be broadcasted to anoter one, program would be aborted (fatalError() called).

    Declaration

    Swift

    @discardableResult public static func .&-(left: Tensor, right:Tensor) -> Tensor

    Parameters

    left

    left tensor

    right

    right tensor

    Return Value

    left tensor after calcualted

  • Element-wise in-place multiplication. support braodcasting. Same effect as using BroadcastMultOperator.

    Note

    This is an in place operation. Result stored in left tensor and return the calcualted left tensor.

    Warning

    If two tensors don’t have same shape or one of them cannot be broadcasted to anoter one, program would be aborted (fatalError() called).

    Declaration

    Swift

    @discardableResult public static func .&*(left: Tensor, right:Tensor) -> Tensor

    Parameters

    left

    left tensor

    right

    right tensor

    Return Value

    left tensor after calcualted

  • Element-wise in-place division. support braodcasting. Same effect as using BroadcastDivOperator.

    Note

    This is an in place operation. Result stored in left tensor and return the calcualted left tensor.

    Warning

    If two tensors don’t have same shape or one of them cannot be broadcasted to anoter one, program would be aborted (fatalError() called).

    Declaration

    Swift

    @discardableResult public static func .&/(left: Tensor, right:Tensor) -> Tensor

    Parameters

    left

    left tensor

    right

    right tensor

    Return Value

    left tensor after calcualted

  • A tensor plus a scalar variable. Ex. [2, 3] + 0.5 --> [2.5, 3.5].

    Declaration

    Swift

    public static func + (left: Tensor,  rightScalar: SupportedScalarDataType) -> Tensor

    Parameters

    left

    A tensor object

    rightScalar

    A scalar variable

    Return Value

    Result tensor, new created.

  • A tensor substract a scalar variable. Ex. [2, 3] - 0.5 --> [1.5, 2.5].

    Declaration

    Swift

    public static func - (left: Tensor,  rightScalar: SupportedScalarDataType) -> Tensor

    Parameters

    left

    A tensor object

    rightScalar

    A scalar variable

    Return Value

    Result tensor, new created.

  • A tensor multiply a scalar variable. Ex. [2, 3] * 0.5 --> [1.0, 1.5].

    Declaration

    Swift

    public static func * (left: Tensor,  rightScalar: SupportedScalarDataType) -> Tensor

    Parameters

    left

    A tensor object

    rightScalar

    A scalar variable

    Return Value

    Result tensor, new created.

  • A tensor divide a scalar variable. Ex. [2, 3] / 0.5 --> [4.0, 6.0].

    Declaration

    Swift

    public static func / (left: Tensor,  rightScalar: SupportedScalarDataType) -> Tensor

    Parameters

    left

    A tensor object

    rightScalar

    A scalar variable

    Return Value

    Result tensor, new created.

  • A tensor plus a scalar variable. Ex. [2, 3] + 0.5 --> [2.5, 3.5].

    Note

    This is an in place operation. Result stored in left tensor and return the calcualted left tensor.

    Declaration

    Swift

    @discardableResult public static func &+ (left: Tensor,  rightScalar: SupportedScalarDataType) -> Tensor

    Parameters

    left

    A tensor object

    rightScalar

    A scalar variable

    Return Value

  • A tensor substract a scalar variable. Ex. [2, 3] - 0.5 --> [1.5, 2.5].

    Note

    This is an in place operation. Result stored in left tensor and return the calcualted left tensor.

    Declaration

    Swift

    @discardableResult public static func &- (left: Tensor,  rightScalar: SupportedScalarDataType) -> Tensor

    Parameters

    left

    A tensor object

    rightScalar

    A scalar variable

    Return Value

  • A tensor multiply a scalar variable. Ex. [2, 3] * 0.5 --> [1.0, 1.5].

    Note

    This is an in place operation. Result stored in left tensor and return the calcualted left tensor.

    Declaration

    Swift

    @discardableResult public static func &* (left: Tensor,  rightScalar: SupportedScalarDataType) -> Tensor

    Parameters

    left

    A tensor object

    rightScalar

    A scalar variable

    Return Value

    left tensor after calculated.

  • A tensor divide a scalar variable. Ex. [2, 3] / 0.5 --> [4.0, 6.0].

    Note

    This is an in place operation. Result stored in left tensor and return the calcualted left tensor.

    Declaration

    Swift

    @discardableResult public static func &/ (left: Tensor,  rightScalar: SupportedScalarDataType) -> Tensor

    Parameters

    left

    A tensor object

    rightScalar

    A scalar variable

    Return Value

  • A scalar plus a tensor variable. Ex. 0.5 + [2, 3] --> [2.5, 3.5].

    Declaration

    Swift

    public static func + (leftScalar: SupportedScalarDataType, right: Tensor) -> Tensor

    Parameters

    leftScalar

    A scalar variable

    right

    A tensor object

    Return Value

    Result tensor, new created.

  • A scalar substracts a tensor variable. Ex. 0.5 - [2, 3] --> [-1.5, -2.5].

    Declaration

    Swift

    public static func - (leftScalar: SupportedScalarDataType, right: Tensor) -> Tensor

    Parameters

    leftScalar

    A scalar variable

    right

    A tensor object

    Return Value

    Result tensor, new created.

  • A scalar multiplies a tensor variable. Ex. 0.5 * [2, 3] --> [1.0, 1.5].

    Declaration

    Swift

    public static func * (leftScalar: SupportedScalarDataType, right: Tensor) -> Tensor

    Parameters

    leftScalar

    A scalar variable

    right

    A tensor object

    Return Value

    Result tensor, new created.

  • A scalar divides by a tensor variable. Ex. 0.5 / [2, 3] --> [0.25, 0.16].

    Declaration

    Swift

    public static func / (leftScalar: SupportedScalarDataType, right: Tensor) -> Tensor

    Parameters

    leftScalar

    A scalar variable

    right

    A tensor object

    Return Value

    Result tensor, new created.

  • A scalar plus a tensor variable. Ex. 0.5 + [2, 3] --> [2.5, 3.5].

    Note

    This is an in place operation. Result stored in right tensor and return the calcualted left tensor.

    Declaration

    Swift

    @discardableResult public static func &+ (leftScalar: SupportedScalarDataType, right: Tensor) -> Tensor

    Parameters

    leftScalar

    A scalar variable

    right

    A tensor object

    Return Value

    Result right tensor.

  • A scalar substracts a tensor variable. Ex. 0.5 - [2, 3] --> [-1.5, -2.5].

    Note

    This is an in place operation. Result stored in right tensor and return the calcualted left tensor.

    Declaration

    Swift

    @discardableResult public static func &- (leftScalar: SupportedScalarDataType, right: Tensor) -> Tensor

    Parameters

    leftScalar

    A scalar variable

    right

    A tensor object

    Return Value

    Result right tensor.

  • A scalar multiplies a tensor variable. Ex. 0.5 * [2, 3] --> [1.0, 1.5].

    Note

    This is an in place operation. Result stored in right tensor and return the calcualted left tensor.

    Declaration

    Swift

    @discardableResult public static func &* (leftScalar: SupportedScalarDataType, right: Tensor) -> Tensor

    Parameters

    leftScalar

    A scalar variable

    right

    A tensor object

    Return Value

    Result tensor, new created.

  • A scalar divides by a tensor variable. Ex. 0.5 / [2, 3] --> [0.25, 0.16].

    Note

    This is an in place operation. Result stored in right tensor and return the calcualted left tensor.

    Declaration

    Swift

    @discardableResult public static func &/ (leftScalar: SupportedScalarDataType, right: Tensor) -> Tensor

    Parameters

    leftScalar

    A scalar variable

    right

    A tensor object

    Return Value

    Result tensor, new created.

  • Make a slice tensor from this object.

    Declaration

    Swift

    public func slice(sliceIndex:[Int]) -> Tensor

    Parameters

    sliceIndex

    Slice Inde

    Return Value

    A sliced tensor

  • Check if a slice tensor object belong to this tensor.

    Note

    If the passed in tensor is not a slice tensor, always returns false.

    Declaration

    Swift

    public func containsSlice(_ slice: Tensor) -> Bool

    Parameters

    slice

    slice tensor

    Return Value

    Bool. Result

  • Get the bytes offset for a slice tensor spawned from this tensor.

    Note

    The function will first check if contains this slice, if not it will return nil

    Declaration

    Swift

    public func slicedTensorOffset(_ slice: Tensor) -> Int?

    Parameters

    slice

    slice object

    Return Value

    offset in bytes