UnaryOperator
public class UnaryOperator: ComputableOperator
Abstract class define the standard unary operator working flow. This class should not be used directly. Any class inheritance this class is doing element-wise computation for input tensors.
-
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()
method. The firat pointer is the input tensor, / the second is the output tensor -
The grad compuation block. parameter: inputTensors,
Declaration
Swift
public var gradComputationBlock: ([Tensor], OperatorComputationMode) -> [DataSymbolSupportedDataType]
-
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.
OneToOne
for this operator.Declaration
Swift
public var mapType: OperatorMappingType
-
Unary operator cannot do in-place calculation
Declaration
Swift
public var inPlaceble: Bool = true
-
init(operatorLabel:cpuComputeBlock:gradComputationBlock:metalKernelFuncLabel:computationDelegate:inputTensors:outputTensors:)
Designated init function
Declaration
Swift
init(operatorLabel label: String, cpuComputeBlock block: @escaping (Tensor, Tensor) -> Void , gradComputationBlock gradBlock: @escaping ([Tensor], OperatorComputationMode) -> [DataSymbolSupportedDataType], metalKernelFuncLabel kernelLabel: String, computationDelegate: OperatorCalculationDelegate?, inputTensors: [Tensor]?, outputTensors: [Tensor]?)
Parameters
label
label description
delegate
delegate description
-
Convenience initializer Subclass should override this function to assign
cpuComputeBlock
andmetalKernelFuncLabel
Declaration
Swift
public convenience required init(computationDelegate: OperatorCalculationDelegate? = nil)
Parameters
computationDelegate
computationDelegate
-
Initial by assign input and output tensors
Parameters
inputTensors
inputTensors description
outputTensors
outputTensors description
-
This operator would not do anything about shapes. Basically, it just return input shapes identically.
Note
If the
shapeArray
is empty, function returnsnil
.Declaration
Swift
public func outputShape(shapeArray shapes: [TensorShape]) -> [TensorShape]?
Parameters
shapes
input shapes
Return Value
return shapes
-
The
inputTensors
andoutputTensors
should be have same count of tensors and each tensor should be has same dimension.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
Return Value
return grads tensor
-
Cal grads async
Declaration
Swift
public func gradComputAsync(_ computationMode: OperatorComputationMode)
Parameters
computationMode
computationMode
-
This 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. Default,
UnaryOperator
defines a workflow. Subclass just needs to overridecpuElementComputationBlock
. If subclass needs custom flow, it could just override this function.Declaration
Swift
internal func cpu()
Parameters
tensors
the operation tensors
-
Let GPU call the Metal kernel to do the inplace computation. Default,
UnaryOperator
defines a workflow. Subclass just needs to overridemetalKernelFuncLabel
attribute. If subclass needs custom flow, it could just override this function.Declaration
Swift
internal func gpu()
Parameters
tensors
the operation tensors