BinaryOperator
public class BinaryOperator: ComputableOperator
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 element-wise way and return one result tensor, i.e.
x + y ->>> z
This BinaryOperator
does not support broadcasting
-
Operator label. Conforms to
ComputableOperator
Declaration
Swift
public var operatorLabel: String = ""
-
This operator does not operator on GPU. Conforms to
ComputableOperator
Declaration
Swift
public var metalKernelFuncLabel:String
-
Conforms to
ComputableOperator
Declaration
Swift
public var computationDelegate: OperatorCalculationDelegate?
-
Conforms to
ComputableOperator
Declaration
Swift
public var inputTensors: [Tensor]?
-
Conforms to
ComputableOperator
Declaration
Swift
public var outputTensors: [Tensor]?
-
The element compuation block in CPU mode. In most cases, subclass should just override this part in
init
method instead overriding the wholecpu(inputTensors:[Tensor], resultTensor: Tensor)
method. The fisr tensor is input tensor A; the seconds tensor is input tensor B; the third tensor is output tensor C. This block should do some computation and assign value back to result tensor’s reader -
The grad compuation block. parameter: inputA, inputB, mode returns: An array of tensor. Should just have 2 object corresponding to two inputs
Declaration
Swift
public var gradComputationBlock: (Tensor, Tensor, OperatorComputationMode) -> [Tensor]
-
If
true
, operator will not check theupGrads
‘s shape. This is used inside framework to speed up in situation we know it will not be wrong. Cases like auto generated differentiation graph.Declaration
Swift
public var disableUpGradShapeCheck: Bool = false
-
If
true
, operator will not callinputOutputTensorsCheck()
before doing calculation. This is used inside framework to speed up in situation we know it will not be wrong.Declaration
Swift
public var disableInputOutputCheck: Bool = false
-
Indicate if this operator would do paramter update.
Note
AllUnaryOperators
are not trainable.Declaration
Swift
public var trainable: Bool = false
-
The mapping type of this operator.
Constant
for this operator.Declaration
Swift
public var mapType: OperatorMappingType
-
Binary operator can do in-place calculation
Declaration
Swift
public var inPlaceble: Bool = true
-
Designated init function
Declaration
Swift
public init(operatorLabel label: String, cpuComputeBlock block: @escaping (Tensor, Tensor, Tensor) -> Void , gradComputationBlock gradBlock: @escaping (Tensor, Tensor, OperatorComputationMode) -> [Tensor], metalKernelFuncLabel kernelLabel: String, computationDelegate: OperatorCalculationDelegate?)
Parameters
label
label description
delegate
delegate description
-
Convenience initializer Subclass required to override this function to assign
cpuComputeBlock
andmetalKernelFuncLabel
Declaration
Swift
required public convenience init(computationDelegate: OperatorCalculationDelegate? = nil)
-
Convenience initializer
Declaration
Swift
public convenience init(computationDelegate: OperatorCalculationDelegate? = nil, inputTensors: [Tensor], outputTensors: [Tensor])
Parameters
computationDelegate
computationDelegate description
inputTensors
inputTensors description
outputTensors
outputTensors description
-
This operator should just receive two tensors with same dimensions (dataType could be different). Return shape is exactly the same shape as input.
Declaration
Swift
public func outputShape(shapeArray shapes: [TensorShape]) -> [TensorShape]?
Parameters
shapes
input shapes
Return Value
return shapes
-
The
inputTensors
should have exactly two tensors and same dimensions. TheoutputTensors
should have exactly one tensora and same dimension with input tensors.Declaration
Swift
public func inputOutputTensorsCheck() -> (check: Bool, msg: String)
-
Compute asynclly
Declaration
Swift
public func computeAsync(_ computationMode: OperatorComputationMode = SerranoEngine.configuredEngine.defaultComputationMode)
Parameters
tensors
input tensors
computationMode
computation mode
-
Compute synclly.
Declaration
Swift
public func compute(_ computationMode: OperatorComputationMode = SerranoEngine.configuredEngine.defaultComputationMode)
Parameters
tensors
input tensors
computationMode
cmputation mode. If choose
GPU
but haven’t configued a GPU SerranoEngine, operator will useCPU
to compute.Return Value
result tensors
-
Calulate grads sync. All unary operator return grads tensor with same number and shape as attribute
inputTensors
.Declaration
Swift
public func gradCompute(_ computationMode: OperatorComputationMode) -> [String: DataSymbolSupportedDataType]
Parameters
computationMode
computationMode
upGrds
upGrds
Return Value
return grads tensor
-
Cal grads async
Declaration
Swift
public func gradComputAsync(_ computationMode: OperatorComputationMode)
Parameters
computationMode
computationMode
upGrds
upGrds
-
Update params if possible. No update parameters for binary operators.
Declaration
Swift
public func updateParams(grads: [Tensor], LR: Float)
Parameters
grads
grads tensor list
LR
learning rate
-
Binary operator has no parameters. Do nothing
Declaration
Swift
public func bindParamSymbols(_ symbols: [GraphSymbol])
-
This operator has no parameters.
Declaration
Swift
public func paramSymbols() -> [GraphSymbol]
Return Value
An empty array
-
Use cpu do the inplace computation. This function always do inPlace computation for inputTensorA. It’s caller function to decide the tensor’s assignment. Default,
UnaryOperator
defines a workflow. Subclass just needs to overridecpuElementComputationBlock
. If subclass needs custom flow, it could just override this function.Note
This function should not be called from outside.
Declaration
Swift
internal func cpu()
Parameters
tensors
the operation tensors
-
Let GPU call the Metal kernel to do the inplace computation.This function always do inPlace computation for inputTensorA. It’s caller function to decide the tensor’s assignment. Default,
UnaryOperator
defines a workflow. Subclass just needs to overridemetalKernelFuncLabel
attribute. If subclass needs custom flow, it could just override this function.Note
This function should not be called from outside.
Declaration
Swift
internal func gpu()
Parameters
tensors
the operation tensors