Serrano operators

Protocol ComputableOperator

All operators conform to ComputableOperator(API).

Initialization

For most operator, it just simply:

let addOp = AddOperator()
let relu = ReLUOperator()

For some operators with required initial arguments:

let poolOp = MaxPool2DOperator(kernelSize: [2, 2])
let convOp =  ConvOperator2D(numFilters: 64, kernelSize: [3, 3])

If you are not sure, you can check API reference for more details of each operator.

Attributes

Below are some useful attributes:

  • public var inputTensors: [Tensor]: The list of input tensors of an operator.
  • public var outputTensors: [Tensor]: The list of output tensors of an operator.
  • public var operatorLabel: String: A readable label of this operator.
  • public var computationDelegate: OperatorCalculationDelegate?: A delegate which could monitor the computation activities of an operator.

Methods

Below are some useful methods:

  • func outputShape(shapeArray shapes:[TensorShape]) -> [TensorShape]?: This function returns the output shapes for given input shapes.
  • func inputOutputTensorsCheck() -> (check: Bool, msg: String): Validate an operator's input tensors and output tensors. Return checking result (Bool) and error message (String) if invalid.
  • func compute(_ computationMode: OperatorComputationMode): Compute results and store result in output tensors in sync way.
  • func computeAsync(_ computationMode: OperatorComputationMode): Compute results and store result in output tensors in async way. This function will return immediately and operator will do computation in background. When begin and done computation, the operator's computationDelegate will be notified.
  • func gradCompute(_ computationMode: OperatorComputationMode) -> [String: DataSymbolSupportedDataType]: Return calculated gradient in sync way. The key of returned dictionary is the label of corresponding input tensor or parameter.
  • func gradComputAsync(_ computationMode: OperatorComputationMode): Compute gradient in async way. When begin and done computation, the operator's computationDelegate will be notified.

Delegate

The attribute computationDelegate can be used to monitor the operator's activities. User can just make their class conform to OperatorCalculationDelegate(API) by implementation these 4 methods:

  • func operatorWillBeginComputation(_ op: ComputableOperator): An operator will begin to do computation. op is the sender.
  • func operatorDidEndComputation(_:outputTensors:): An operator has completed computation. op is the sender.
  • func operatorWillBeginGradsComputation(_ op: ComputableOperator): An operator will begin to do gradient computation. op is the sender.
  • func operatorDidEndGradsComputation(_ op: ComputableOperator, grads: [String: DataSymbolSupportedDataType]): An operator has completed gradient computation. op is the sender.

Expecting changes

Expecting changes in APIs since now Serrano is in alpha development. If you find any mismatching in guides and code, please make a PR.