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

  • 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. The outputTensors 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 use CPU 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 override cpuElementComputationBlock. 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 override metalKernelFuncLabel 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