Classes
The following classes are available globally.

Implementation of
See moreTensorSymbol
Declaration
Swift
public class SerranoTensorSymbol: SerranoGraphSymbol, TensorSymbol

Implementaion of
See moreOperatorSymbol
Declaration
Swift
public class SerranoOperatorSymbol: SerranoGraphSymbol, OperatorSymbol

Implementation of
See moreScalarSymbol
Declaration
Swift
public class SerranoScalarSymbol: SerranoGraphSymbol, ScalarSymbol

Implementation of
See moreGraphSymbol
Declaration
Swift
public class SerranoGraphSymbol: GraphSymbol, Hashable

Intro
The implementation of
Graph
. AComputationGraph
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 lowlevel 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(:)
andbackward(:)
manually to do training andComputationGraph
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 fromComputationGraph
. It has all functionsComputationGraph
has and beyond that: Loss function.
Model
could setup loss function to do the backward training. ‘ComputationGraph’  High level training functions.
Model
could automatically repeatforward(:)
andbackward(:)
util reaches conditions users setup like max number of epoch, early stop etc.  High level prediction functions.
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 ofinputTensors
, i.e. same value ascount
of a input tensor.m
: the number of hidden nodes, same value asnumUnits
;
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 asnumUnits
;
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 ofoutputTensors
.Note
All input tensor should have samecount
.Bias enable choice
Bias could be disabled by setting the
biasEnabled
tofalse
.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 moreDeclaration
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 moreDeclaration
Swift
public class BatchNormOperator: ComputableOperator

This class is an abstract class for 2D pooling operators.
See moreDeclaration
Swift
public class Pooling2DOperator: ComputableOperator

Undocumented
See moreDeclaration
Swift
public class MaxPool2DOperator: Pooling2DOperator

2D Average pooling.
Input shape
Input data should be a tensor with 3D shape
See more[channel, height, width]
or[height, width, channel]
according to thechannelPosition
.Declaration
Swift
public class AvgPool2DOperator: Pooling2DOperator

2D Sum Pooling.
Input shape
Input data should be a tensor with 3D shape
See more[channel, height, width]
or[height, width, channel]
according to thechannelPosition
.Declaration
Swift
public class SumPool2DOperator: Pooling2DOperator

Define APIs reading Flatbuffer
See moreDeclaration
Swift
public class FlatbufferIO

2D Convolution operator.
Input tensors shapes
All tensors in
inputTensors
should have same shapesShape specification
 inputTensors: each tensor:
[channel, height, width]
or[height, width, channel]
according tochannelPosition
 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 benil
. However, if you add this operator through a graph’soperation(_,inputs:,op:)
API (i.e. symbolic constructing graph). You must indicate theinputShape
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
See moredilation > 1
has not been implemented and supported.Declaration
Swift
public class ConvOperator2D: ComputableOperator
 inputTensors: each tensor:

Operator works like img2col in matlab. It converts any 3D tensor (
See more[H, W, C]
or[C, H, W]
) into a 2D tensor ([H*W, C*M*N]
) according to patchSize[M, N]
and stride.Declaration
Swift
public class Img2ColOperator: ComputableOperator

The abstract class for all activation operator. An activation operator is actually a special kind of
See moreUnaryOperator
doing a little bit more complex elementwise computation.Declaration
Swift
public class ActivationOperator: UnaryOperator

Rectified Linear Unit,
See morey=max(x,alpha=0.0)
Declaration
Swift
public class ReLUOperator: ActivationOperator

Sigmoid.
See morey = 1 / (1 + exp(x))
Declaration
Swift
public class SigmoidOperator: ActivationOperator

Softplus.
See morey = log(exp(x) + 1)
Declaration
Swift
public class SoftplusOperator: ActivationOperator

Softsign.
See morey = x / (1 + abs(x))
Declaration
Swift
public class SoftsignOperator: ActivationOperator

Do nothing. Output identify tensor.
See morey = x
Declaration
Swift
public class LinearOperator: ActivationOperator

Exponential Linear Units.
y = x if x > 0, else y = alpha*(exp(x)  1)
Reference
Fast and Accurate Deep Network Learning by Exponential Linear Units (ELUs)
See moreDeclaration
Swift
public class ELUOperator: ActivationOperator

Scaled Exponential Linear Unit.
y = scale * elu(x)
alpha
default value is
1.673263
scale
default value is
1.050701
Reference
SelfNormalizing Neural Networks
See moreDeclaration
Swift
public class SELUOperator: ActivationOperator

Softmax activations.
y = exp(x) / reduce_sum(exp(x), dim)
Default
dim
is the last dimension of input tensors.Note
The operator assumens all input tensors have same rank. If not, it could not do the calculation.Declaration
Swift
public class SoftmaxOperator: ActivationOperator

LeakyReLU activation operator.
y = alpha * x (x < 0) y = x (x >= 0)
scale factor alpha
Default value is
0.3
Reference
Rectifier Nonlinearities Improve Neural Network Acoustic Models
See moreDeclaration
Swift
public class LeakyReLUOperator: ELUOperator

Thresholded Rectified Linear Unit.
f(x) = x if x > alpha, else f(x) = 0
Threshold
alpha
Default value is
1.0
Reference
ZeroBias Autoencoders and the Benefits of CoAdapting Features
See moreDeclaration
Swift
public class ThresholdedReLUOperator: ELUOperator

See moreForwardGraph
is a subclass ofComputationGraph
that can only do forward computation. Thus, it has mooptimization during forward computation and the internal results are not accessable.Declaration
Swift
public class ForwardGraph: ComputationGraph

The abstract class for all reduce operators. Should not be used directly. A reduce operator do some aggregate calculation along given axes. An example given by TensorFlow is here.
See moreDeclaration
Swift
public class ReduceOperator: ComputableOperator

Computes the sum of array elements over given axes.
See moreDeclaration
Swift
public class ReduceSumOperator: ReduceOperator

Computes the product of array elements over given axes.
See moreDeclaration
Swift
public class ReduceProductOperator: ReduceOperator

Computes the max of array elements over given axes.
See moreDeclaration
Swift
public class ReduceMaxOperator: ReduceOperator

Computes the min of array elements over given axes.
See moreDeclaration
Swift
public class ReduceMinOperator: ReduceOperator

Computes the mean of array elements over given axes.
See moreDeclaration
Swift
public class ReduceMeanOperator: ReduceOperator

Do copy operation on
See moreinputTensors
.Declaration
Swift
public class CopyOperator: ComputableOperator

Abstract class define the standard unary operator working flow. This class should not be used directly. Any class inheritance this class is doing elementwise computation for input tensors.
See moreDeclaration
Swift
public class UnaryOperator: ComputableOperator

Undocumented
See moreDeclaration
Swift
public class SinOperator: UnaryOperator

Compute elementwise tangent on input tensors.
Note
This operator may outputNaN
orInfinite
values and operator would not check these situations.Declaration
Swift
public class TanOperator: UnaryOperator

Compute elementwise cosine on input tensors.
Note
This operator may outputNaN
orInfinite
values and operator would not check these situations.Declaration
Swift
public class CosOperator: UnaryOperator

Compute elementwise arc sine on input tensors.
Note
This operator may outputNaN
orInfinite
values and operator would not check these situations.Declaration
Swift
public class ArcsinOperator: UnaryOperator

Compute elementwise arc cosine on input tensors.
Note
This operator may outputNaN
orInfinite
values and operator would not check these situations.Declaration
Swift
public class ArccosOperator: UnaryOperator

Compute elementwise arc tangent on input tensors.
Note
This operator may outputNaN
orInfinite
values and operator would not check these situations.Declaration
Swift
public class ArctanOperator: UnaryOperator

Compute elementwise of input tensors radians to degrees.
See moreDeclaration
Swift
public class DegreeOperator: UnaryOperator

Compute elementwise abs values of input tensors.
See moreDeclaration
Swift
public class AbsOperator: UnaryOperator

Compute elementwise radien values of input tensors fro degrees.
See moreDeclaration
Swift
public class RadianOperator: UnaryOperator

Compute elementwise hyperbolic sine of input tensors.
Note
This operator may outputNaN
orInfinite
values and operator would not check these situations.Declaration
Swift
public class SinhOperator: UnaryOperator

Compute elementwise hyperbolic cosine of input tensors.
Note
This operator may outputNaN
orInfinite
values and operator would not check these situations.Declaration
Swift
public class CoshOperator: UnaryOperator

Compute elementwise hyperbolic tangent of input tensors.
Note
This operator may outputNaN
orInfinite
values and operator would not check these situations.Declaration
Swift
public class TanhOperator: UnaryOperator

Compute elementwise inverse hyperbolic tangent on input tensors.
Note
This operator may outputNaN
orInfinite
values and operator would not check these situations.Declaration
Swift
public class ArctanhOperator: UnaryOperator

Compute elementwise inverse hyperbolic cosine on input tensors.
Note
This operator may outputNaN
orInfinite
values and operator would not check these situations.Declaration
Swift
public class ArccoshOperator: UnaryOperator

Compute elementwise inverse hyperbolic cosine on input tensors.
Note
This operator may outputNaN
orInfinite
values and operator would not check these situations.Declaration
Swift
public class ArcsinhOperator: UnaryOperator

Operator computes elementwise floor of the input tensors and returen result tensors.
See moreDeclaration
Swift
public class FloorOperator: UnaryOperator

Operator computes elementwise floor of the input tensors and returen result tensors.
See moreDeclaration
Swift
public class CeilOperator: UnaryOperator

Operator computes elementwise rounded value to the nearest integer of the input.
See moreDeclaration
Swift
public class RintOperator: UnaryOperator

Operator computes elementwise rounded value to the truncating integers of input values.
See moreDeclaration
Swift
public class RoundOperator: UnaryOperator

Compute elementwise square values of input tensors.
Note
This operator may outputNaN
orInfinite
values and operator would not check these situations.Declaration
Swift
public class SquareOperator: UnaryOperator

Compute elementwise reciprocal values of squareroot of input tensors.
See more1 / sqrt(x)
Declaration
Swift
public class RsqrtOperator: UnaryOperator

Compute elementwise squareroot values of input tensors.
See moreDeclaration
Swift
public class SqrtOperator: UnaryOperator

Compute elementwise
See morelog(1 + x)
values of input tensors. Base ise
.Declaration
Swift
public class Log1pOperator: UnaryOperator

Compute elementwise
See morelog2(x)
values of input tensors.Declaration
Swift
public class Log2Operator: UnaryOperator

Compute elementwise
See morelog10(x)
values of input tensors.Declaration
Swift
public class Log10Operator: UnaryOperator

Compute elementwise
See morelog(x)
values of input tensors. Base ise
.Declaration
Swift
public class LogOperator: UnaryOperator

Compute elementwise
e^x  1
values of input tensors.Note
This operator may outputNaN
orInfinite
values and operator would not check these situations.Declaration
Swift
public class Expm1Operator: UnaryOperator

Compute elementwise
exp(x)
values of input tensors.Note
This operator may outputNaN
orInfinite
values and operator would not check these situations.Declaration
Swift
public class ExpOperator: UnaryOperator

Doing broadcasting on input tensors and store result in output tensors. Serrano follows the broadcasting rule of Scipy.
See moreDeclaration
Swift
public class BroadcastOperator: ComputableOperator

Transpose 2D matrix on all
See moreinputTensors
and put transposed values inoutputTensors
.Declaration
Swift
public class TransposeOperator: ComputableOperator

Convenient function APIs of operators.
See moreDeclaration
Swift
public class OperatorFuncs

The abstract parent class for all broadcast arithmetic oeprators. Any child class of this operator support elementwise calculation between two
See moreTensor
objects with broadcasting support.Declaration
Swift
public class BroadcastArithmeticOperator: ComputableOperator

Broadcasting addition.
Note
TheinputTensors
of this operator should just have exactly 2 tensors and theoutputTensors
should just have 1 tensor.Declaration
Swift
public class BroadcastAddOperator: BroadcastArithmeticOperator

Broadcasting substraction.
Note
TheinputTensors
of this operator should just have exactly 2 tensors and theoutputTensors
should just have 1 tensor.Declaration
Swift
public class BroadcastSubOperator: BroadcastArithmeticOperator

Broadcasting multiplication.
Note
TheinputTensors
of this operator should just have exactly 2 tensors and theoutputTensors
should just have 1 tensor.Declaration
Swift
public class BroadcastMultOperator: BroadcastArithmeticOperator

Broadcasting division.
Note
TheinputTensors
of this operator should just have exactly 2 tensors and theoutputTensors
should just have 1 tensor.Declaration
Swift
public class BroadcastDivOperator: BroadcastArithmeticOperator

Abstract class define the standard binary operator working flow. This class should not be used directly. Any class inheritance this class is doing computation on exactly two input tensors in elementwise way and return one result tensor, i.e.
See morex + y >>> z
ThisBinaryOperator
does not support broadcastingDeclaration
Swift
public class BinaryOperator: ComputableOperator

Do
See morea+b
. Not support broadcasting.Declaration
Swift
public class AddOperator: BinaryOperator

Do
See moreab
. Not support broadcasting.Declaration
Swift
public class SubOperator: BinaryOperator

Do
See morea*b
in elementwise way. Not support broadcasting.Declaration
Swift
public class MultOperator: BinaryOperator

Do
See morea/b
in elementwise way. Not support broadcasting.Declaration
Swift
public class DivOperator: BinaryOperator

Do
See moreb/a
in elementwise way. Not support broadcasting.Declaration
Swift
public class RDivOperator: BinaryOperator

Do
a^b
in elementwise way. Not support broadcasting.Note
This operator may outputNaN
orInfinite
values and operator would not check these situations.Declaration
Swift
public class PowOperator: BinaryOperator

matrix multiplication.
Transpose input tensors
Opertor’s attributes
transposeA
andtransposeB
indicating if tranposing input tensors before doing calculation. And if any or both of them are set totrue
, 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 supportMetalPerformanceShaders
, we use self kernel.Multiple input
This operator could takein multiple input. Currently, it support multiple input
See moreA
and single inputB
. IfinputTensors
contains more than 2 elements, operator will view last element as inputB
and all previous element as inputA
s.Declaration
Swift
public class MatrixMultOperator: ComputableOperator

Undocumented
See moreDeclaration
Swift
public class SerranoLogging

A
Tensor
object is a ndimension pagealigned data container with fixedsize memory space. The attributeshape
specifying the dimension information of aTensor
object.All elements stored as
Float
valuesNo matter what type of array user passing in:
Double
orFloat
orInt
, inside aTensor
object values will be converted and stored asFloat
(32bit signle precision).Serrano
chose this because 32bitFloat
is the maximum precise floating data typeMetal
could support (v1.2).Inside Memory Layout
Inside a
Tensor
object, it maintains a virtual 1d array as a contiguous memory space manually allocated. Elements are stored followingrowmajor
order. Details can be found inTensorShape
docs.TensorTensor arithmetic operators
Besides Operators which contains many math and NN operations,
Tensor
object itself implements/overloads common arithmetic operators. Tensor object support elementwise arithmetic operation with or without broadcasting. Also it supports inplace operation choice.Elementwise operation without broadcasting:
+
Addition without broadcasting
Substraction without broadcasting*
Multiplication without broadcasting/
Division without broadcasting
Elementwise inplace operation:
&+
Addition without broadcasting&
Substraction without broadcasting&*
Multiplication without broadcasting&/
Division without broadcasting
Elementwise operation with broadcasting:
.+
Addition without broadcasting.
Substraction without broadcasting.*
Multiplication without broadcasting./
Division without broadcasting
Elementwise inplace 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 inplace 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 inplace addition with broadcasting let resunt4 = tensorA .&+ tensorD print(result4 == tensorA) // true
Directly used as TensorSymbol
See moreTensor
conforms toTensorSymbol
protocol. So a tensor object could be used as a symbol participating in graph computation.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.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 accessGPUDevice
(an instance ofMTLDevice
),\serranoCommandQueue
(an instance ofMTLCommandQueue
).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:
See morelet configuredEngine = SerranoEngine.configuredEngine configuredEngine.configureEngine(computationMode: .CPU, serranoCommandQueue: nil, serranoMTLLibrary: nil, systemDefaultGPUDevice: nil)
Declaration
Swift
public class SerranoEngine

Undocumented
See moreDeclaration
Swift
public class TensorUtils

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
See moreDeclaration
Swift
public class OperatorUtils

This is a framework level class
See moreDeclaration
Swift
public class SerranoResourceManager