Classes

The following classes are available globally.

  • Intro

    The implementation of Graph. A ComputationGraph just a set of symbols and connections between them. The graph computes the output tensors stage by stage.

    A typical usage:

    
    let graph = ComputationGraph()
    
    let a = graph.tensor("A", shape: TensorShape(dataType: .float, shape: [2,3]))
    let b = graph.tensor("B", shape: TensorShape(dataType: .float, shape: [2,3]))
    let (c, op) = graph.operation("", inputSymbols: [a, b], op: PowOperator()).first
    

    ComputationGraph V.S. Model

    ComputationGraph is low-level abstraction of machine learning models. It has two basic funtions:

    • forward. Compute results from input to output
    • backward. Compute and update data symbols that are differentiable use optimizer User needs to call forward(:) and backward(:) manually to do training and ComputationGraph is not aware of loss function. If you want to use loss function if a graph, you need add it into this graph as an operator symbol.

    Model is a higher level abstraction inherited from ComputationGraph. It has all functions ComputationGraph has and beyond that:

    • Loss function. Model could setup loss function to do the backward training. ‘ComputationGraph’
    • High level training functions. Model could automatically repeat forward(:) and backward(:) util reaches conditions users setup like max number of epoch, early stop etc.
    • High level prediction functions.
    See more

    Declaration

    Swift

    public class ComputationGraph: Graph
  • The regular fully connected operator. Operator do the 1D_dot(inputTensor.flatten, weights) + bias calculation on all input tensors.

    Weight tensor layout

    The weight is a 2D tensor with shape: [n, m] where :

    • n: the flattened dimension of inputTensors, i.e. same value as count of a input tensor.
    • m: the number of hidden nodes, same value as numUnits;

    Thus, each column stores the weights of corresponding hidden unit.

    Bias tensor layout

    The bias is a 1D tensor with shape [m] where:

    • m: the number of hidden nodes, same value as numUnits;

    Thus, each value in the tensor is the bias value of corresponding hidden unit.

    Input tensors auto flatten

    For input tensor with rank >=2, the operator will automatically flatten the tensor and then do calcualtion.

    Multiple input tensors

    If inputTensors has more than 1 tensor object, the operator applies calculation on each input tensor independently and stores the results in corresponding tensor of outputTensors.

    Note

    All input tensor should have same count.

    Bias enable choice

    Bias could be disabled by setting the biasEnabled to false.

    Batch calculation

    This operator itself does not explicitly support batch calculation. But user can use slice tensor to do the same thing. Details can be found in Slice tensor and Batch calculation with operators

    See more

    Declaration

    Swift

    public class FullyconnectedOperator: ComputableOperator
  • Batch normalization operator.

    Normalize on input tensors by each tensor’s mean and variance.

    All input tensors should have same dimentsion.

    output tensors should have same channel order as input

    Currently, only support 2D tensor with channel information.

    See more

    Declaration

    Swift

    public class BatchNormOperator: ComputableOperator
  • 2D Convolution operator.

    Input tensors shapes

    All tensors in inputTensors should have same shapes

    Shape specification

    • inputTensors: each tensor: [channel, height, width] or [height, width, channel] according to channelPosition
    • weight: [num_filter,channel, kernelSize[0], kernelSize[1]],
    • bias: [num_filter],
    • outputTensors: each tensor: [out_height, out_width, num_filter], i.e., TensorChannelOrder.Last

    nil weight

    At declaration, weight could be nil. However, if you add this operator through a graph’s operation(_,inputs:,op:) API (i.e. symbolic constructing graph). You must indicate the inputShape attribute so that the graph could estimate the input and output shape information.

    Batch process

    This operator does not directly support batch data. However, user could use TensorSlice to do the batch processing. Details can be found in Slice tensor and Batch calculation with operators.

    Dilation

    Currently, calculation with dilation > 1 has not been implemented and supported.

    See more

    Declaration

    Swift

    public class ConvOperator2D: ComputableOperator
  • matrix multiplication.

    Transpose input tensors

    Opertor’s attributes transposeA and transposeB indicating if tranposing input tensors before doing calculation. And if any or both of them are set to true, all caulcation and input/output validation will be doing after transposing.

    Metal performance shader support

    By default, operator tries to use MPSMatrixMultiplication in MetalPerformanceShaders. But on some devices which do not support MetalPerformanceShaders, we use self kernel.

    Multiple input

    This operator could takein multiple input. Currently, it support multiple input A and single input B. If inputTensors contains more than 2 elements, operator will view last element as input B and all previous element as input As.

    See more

    Declaration

    Swift

    public class MatrixMultOperator: ComputableOperator
  • 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.

    See more

    Declaration

    Swift

    public class Tensor: Hashable, Equatable, TensorSymbol
  • This util class contains APIs that check the device’s hardware capabilities and limits accroding to Apple’s official docs.

    Note

    The limits values of each type of hardware device should be checked and updated with Apple’s doc frequently. If you find any mismatching with Apple’s official doc, please make a PR at github.
    See more

    Declaration

    Swift

    public class MetalHardwareChecker
  • This class is supposed be initialized and configured at the very beggining of your app. It is responsible for setting up computation envirionment involving with iOS’s GPU device. It will initialize serveral instances which will involve with heavy GPU evaluation and are reconmmended reusing by Apple documentation.

    The configuredEngine is a singleton instance and should be used everywhere you need to access GPUDevice (an instance of MTLDevice),\ serranoCommandQueue (an instance of MTLCommandQueue).

    Initial and configure a GPU engine:

       let configuredEngine = SerranoEngine.configuredEngine
       let (success, message) = configuredEngine.configureEngine(computationMode: .GPU, serranoCommandQueue: nil, serranoMTLLibrary: nil, systemDefaultGPUDevice: nil)
       if !success {
           // Failed to congiure GPU device
           print(message)
       }
    

    Initial and configure a CPU engine:

        let configuredEngine = SerranoEngine.configuredEngine
        configuredEngine.configureEngine(computationMode: .CPU, serranoCommandQueue: nil, serranoMTLLibrary: nil, systemDefaultGPUDevice: nil)
    
    See more

    Declaration

    Swift

    public class SerranoEngine
  • /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    See more

    Declaration

    Swift

    public class OperatorUtils