MatrixMultOperator
public class MatrixMultOperator: ComputableOperator
matrix multiplication.
Transpose input tensors
Opertor’s attributes transposeA
and transposeB
indicating if tranposing input tensors before doing calculation.
And if any or both of them are set to true
, 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 support MetalPerformanceShaders
, we use self kernel.
Multiple input
This operator could takein multiple input. Currently, it support multiple input A
and single input B
.
If inputTensors
contains more than 2 elements, operator will view last element as input B
and all previous element as input A
s.
-
The submatrix size in Metal calcualtion. Should be the same as
SUBMATRIX_SIZE
inmatrix_mult_op.metal
Declaration
Swift
public static let METAL_SUBMATRIX_SIZE = 4
-
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 = "MatrixMult"
-
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]?
-
Whether transpose inputA before calculation
Declaration
Swift
public var transposeA: Bool
-
Whether transpose inputB before calcualtion
Declaration
Swift
public var transposeB: Bool
-
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.
Declaration
Swift
public var trainable: Bool = false
-
Kernel to choose
Declaration
Swift
public var kernel: MatrixMultKernel
-
The mapping type of this operator.
Constant
for this operator.Declaration
Swift
public var mapType: OperatorMappingType
-
Matrix multiplication cannot do in-place calculation
Declaration
Swift
public var inPlaceble: Bool = false
-
If use MPS. Default is
false
Declaration
Swift
internal var disabledMPS: Bool = false
-
beta for mstrix calculation, if want to add into output tensor, set this to 1
Declaration
Swift
internal var matrixBeta: Float = 0.0
-
init(operatorLabel:computationDelegate:transposeA:transposeB:inputTensors:outputTensors:kernel:disableInputOutputCheck:)
Undocumented
Declaration
Swift
public init(operatorLabel: String = "MatrixMultOperator", computationDelegate: OperatorCalculationDelegate? = nil, transposeA: Bool = false, transposeB: Bool = false, inputTensors: [Tensor]? = nil, outputTensors: [Tensor]? = nil, kernel: MatrixMultKernel = MatrixMultKernel.Single, disableInputOutputCheck: Bool = false)
-
Check the input shapes. Following same rule of matrix multiplication.
Note
This function will transpose shape first and then calcualte output shape.
Declaration
Swift
public func outputShape(shapeArray shapes: [TensorShape]) -> [TensorShape]?
Parameters
shapes
input shapes
Return Value
return shapes
-
Check if assigned
inputTensors
andoutputTensors
validNote
This function will first transposes input tensors’ shapes according to attributes
transposeA
andtransposeB
, and then validate the shapes.Declaration
Swift
public func inputOutputTensorsCheck() -> (check: Bool, msg: String)
Return Value
the result and erro message if possible
-
Compute sync
Declaration
Swift
public func compute(_ computationMode: OperatorComputationMode = SerranoEngine.configuredEngine.defaultComputationMode)
Parameters
computationMode
mode
-
Compute async
Declaration
Swift
public func computeAsync(_ computationMode: OperatorComputationMode = SerranoEngine.configuredEngine.defaultComputationMode)
Parameters
computationMode
mode
-
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
-
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
-
Get M, N, K attributes
Declaration
Return Value
M, N, K
-
Use
BLAS
cblas_sgemm
to do calculationDeclaration
Swift
internal func cpu()
-
This method choose proper kernel or MPS to do calculation
Declaration
Swift
internal func gpu()
-
Do calculation of inputA and transpoedB.
transposedInputB
is supposed already transposedDeclaration
Swift
internal func gpu_single(inputABuffer: MTLBufferResource, inputBBufferTransposed: MTLBufferResource, outputCBuffer: MTLBufferResource, dimInfo: inout MatrixDimInfo, kernel: MTLComputePipelineState)
Parameters
inputABuffer
inputA
inputBBufferTransposed
transposedInputB
outputCBuffer
outputCBuffer
dimInfo
dimInfo
kernel
kernel
-
Undocumented
Declaration
Swift
internal func gpu_kernel_single()
-
Do matrix multiplication with submatrix kernel.
Declaration
Swift
internal func gpu_submatrix(inputATransposeBuffer: MTLBufferResource, inputBBuffer: MTLBufferResource, outputCBuffer: MTLBufferResource, dimInfo: inout MatrixDimInfo, kernel: MTLComputePipelineState)
Parameters
inputATransposeBuffer
transposed A buffer
inputBBuffer
inputBBuffer
outputCBuffer
outputCBuffer
dimInfo
dimInfo
kernel
kernel
-
Note
There’s no any transposing processing in this function cause in functiongpu()
it only dispatches suitable inputs to this function.Declaration
Swift
internal func gpu_kernel_submatrix()
-
Undocumented
Declaration
Swift
internal func gpu_kernel_MPS()