diff --git a/metal/paddle-mobile/paddle-mobile/Operators/Kernels/ElementwiseAddKernel.swift b/metal/paddle-mobile/paddle-mobile/Operators/Kernels/ElementwiseAddKernel.swift index d67876d307ce30a6c31f5d1e09eb2f960da1e0b5..16774a85492d2e21ca5575ed661674824319db28 100644 --- a/metal/paddle-mobile/paddle-mobile/Operators/Kernels/ElementwiseAddKernel.swift +++ b/metal/paddle-mobile/paddle-mobile/Operators/Kernels/ElementwiseAddKernel.swift @@ -25,8 +25,31 @@ struct ElementwiseAddMetalParam { } class ElementwiseAddKernel: Kernel, Computable { + var metalParam: ElementwiseAddMetalParam required init(device: MTLDevice, param: ElementwiseAddParam

) { param.output.initTexture(device: device, inTranspose: param.inputX.transpose, computePrecision: computePrecision) + + metalParam = ElementwiseAddMetalParam.init() + + let xdim: [Int32] = (0..<4).map { Int32(param.inputX.dim[$0]) } + let ydim: [Int32] = (0..<4).map { Int32(param.inputY.dim[$0]) } + let xtrans: [Int32] = (0..<4).map { Int32(param.inputX.transpose[$0]) } + let ytrans: [Int32] = (0..<4).map { Int32(param.inputY.transpose[$0]) } + + metalParam.xdim = (xdim[0], xdim[1], xdim[2], xdim[3]) + metalParam.ydim = (ydim[0], ydim[1], ydim[2], ydim[3]) + metalParam.xtrans = (xtrans[0], xtrans[1], xtrans[2], xtrans[3]) + metalParam.ytrans = (ytrans[0], ytrans[1], ytrans[2], ytrans[3]) + if param.axis == -1 { + metalParam.axis = 4 - Int32(param.inputY.tensorDim.cout()) + } else { + metalParam.axis = 4 - Int32(param.inputX.tensorDim.cout()) + Int32(param.axis) + } + metalParam.ylen = Int32(param.inputY.tensorDim.cout()) + if (param.inputX.dim == param.inputY.dim) && (param.inputX.transpose == param.inputY.transpose) { + // print("===> elementwise_add fast!!!") + metalParam.fast = 1 + } if computePrecision == .Float32 { super.init(device: device, inFunctionName: "elementwise_add") } else if computePrecision == .Float16 { @@ -40,32 +63,10 @@ class ElementwiseAddKernel: Kernel, Computable { guard let encoder = commandBuffer.makeComputeCommandEncoder() else { throw PaddleMobileError.predictError(message: " encode is nil") } - var emp = ElementwiseAddMetalParam.init() encoder.setTexture(param.inputX.metalTexture, index: 0) encoder.setTexture(param.inputY.metalTexture, index: 1) encoder.setTexture(param.output.metalTexture, index: 2) - - let xdim: [Int32] = (0..<4).map { Int32(param.inputX.dim[$0]) } - let ydim: [Int32] = (0..<4).map { Int32(param.inputY.dim[$0]) } - let xtrans: [Int32] = (0..<4).map { Int32(param.inputX.transpose[$0]) } - let ytrans: [Int32] = (0..<4).map { Int32(param.inputY.transpose[$0]) } - - emp.xdim = (xdim[0], xdim[1], xdim[2], xdim[3]) - emp.ydim = (ydim[0], ydim[1], ydim[2], ydim[3]) - emp.xtrans = (xtrans[0], xtrans[1], xtrans[2], xtrans[3]) - emp.ytrans = (ytrans[0], ytrans[1], ytrans[2], ytrans[3]) - if param.axis == -1 { - emp.axis = 4 - Int32(param.inputY.tensorDim.cout()) - } else { - emp.axis = 4 - Int32(param.inputX.tensorDim.cout()) + Int32(param.axis) - } - emp.ylen = Int32(param.inputY.tensorDim.cout()) - if (param.inputX.dim == param.inputY.dim) && (param.inputX.transpose == param.inputY.transpose) { -// print("===> elementwise_add fast!!!") - emp.fast = 1 - } - - encoder.setBytes(&emp, length: MemoryLayout.size, index: 0) + encoder.setBytes(&metalParam, length: MemoryLayout.size, index: 0) encoder.dispatch(computePipline: pipline, outTexture: param.output.metalTexture) encoder.endEncoding() } diff --git a/metal/paddle-mobile/paddle-mobile/Operators/Kernels/PoolKernel.swift b/metal/paddle-mobile/paddle-mobile/Operators/Kernels/PoolKernel.swift index 59754e57c112ebf5e300bd0ef98465e08744e4bf..1d66e420e236f2e0a7734838a293215807caa968 100644 --- a/metal/paddle-mobile/paddle-mobile/Operators/Kernels/PoolKernel.swift +++ b/metal/paddle-mobile/paddle-mobile/Operators/Kernels/PoolKernel.swift @@ -25,24 +25,10 @@ struct PoolMetalParam { } class PoolKernel: Kernel, Computable{ - + var metalParam: PoolMetalParam required init(device: MTLDevice, param: PoolParam

) { param.output.initTexture(device: device, inTranspose: param.input.transpose, computePrecision: computePrecision) - if computePrecision == .Float32 { - super.init(device: device, inFunctionName: "pool") - } else if computePrecision == .Float16 { - super.init(device: device, inFunctionName: "pool_half") - } else { - fatalError() - } - } - - func compute(commandBuffer: MTLCommandBuffer, param: PoolParam

) throws { - guard let encoder = commandBuffer.makeComputeCommandEncoder() else { - throw PaddleMobileError.predictError(message: " encoder is nil") - } - encoder.setTexture(param.input.metalTexture, index: 0) - encoder.setTexture(param.output.metalTexture, index: 1) + var poolType: Int32 switch param.poolType { case "max": @@ -50,9 +36,9 @@ class PoolKernel: Kernel, Computable{ case "avg": poolType = 1 default: - throw PaddleMobileError.predictError(message: " unknown pooltype " + param.poolType) + fatalError() } - var pmp = PoolMetalParam.init( + metalParam = PoolMetalParam.init( ksizeX: param.ksize[0], ksizeY: param.ksize[1], strideX: param.stride[0], @@ -61,7 +47,24 @@ class PoolKernel: Kernel, Computable{ paddingY: param.padding[1], poolType: poolType ) - encoder.setBytes(&pmp, length: MemoryLayout.size, index: 0) + + if computePrecision == .Float32 { + super.init(device: device, inFunctionName: "pool") + } else if computePrecision == .Float16 { + super.init(device: device, inFunctionName: "pool_half") + } else { + fatalError() + } + } + + func compute(commandBuffer: MTLCommandBuffer, param: PoolParam

) throws { + guard let encoder = commandBuffer.makeComputeCommandEncoder() else { + throw PaddleMobileError.predictError(message: " encoder is nil") + } + encoder.setTexture(param.input.metalTexture, index: 0) + encoder.setTexture(param.output.metalTexture, index: 1) + + encoder.setBytes(&metalParam, length: MemoryLayout.size, index: 0) encoder.dispatch(computePipline: pipline, outTexture: param.output.metalTexture) encoder.endEncoding() } diff --git a/metal/paddle-mobile/paddle-mobile/Operators/Kernels/ReshapeKernel.swift b/metal/paddle-mobile/paddle-mobile/Operators/Kernels/ReshapeKernel.swift index 6967b5ed72ce18f500305dfe1fdc0e3d3f6ecfd4..91708ff7081e805ddb12777114ff32743c629207 100644 --- a/metal/paddle-mobile/paddle-mobile/Operators/Kernels/ReshapeKernel.swift +++ b/metal/paddle-mobile/paddle-mobile/Operators/Kernels/ReshapeKernel.swift @@ -28,8 +28,27 @@ struct ReshapeTestParam: TestParam { } class ReshapeKernel: Kernel, Computable{ + + var metalParam: ReshapeMetalParam + required init(device: MTLDevice, param: ReshapeParam

) { param.output.initTexture(device: device, computePrecision: computePrecision) + var id: [Int32] = [1, 1, 1, 1] + for i in 0..: Kernel, Computable{ } required init(device: MTLDevice, testParam: ReshapeTestParam) { + metalParam = ReshapeMetalParam.init( + idim: (0, 0, 0, 0), + itrans: (0, 0, 0, 0), + odim: (0, 0, 0, 0), + otrans: (0, 0, 0, 0) + ) super.init(device: device, inFunctionName: "reshape") } @@ -50,23 +75,8 @@ class ReshapeKernel: Kernel, Computable{ encoder.setTexture(param.input.metalTexture, index: 0) encoder.setTexture(param.output.metalTexture, index: 1) - var id: [Int32] = [1, 1, 1, 1] - for i in 0...size, index: 0) + + encoder.setBytes(&metalParam, length: MemoryLayout.size, index: 0) encoder.dispatch(computePipline: pipline, outTexture: param.output.metalTexture) encoder.endEncoding() } diff --git a/metal/paddle-mobile/paddle-mobile/Operators/Kernels/SoftmaxKernel.swift b/metal/paddle-mobile/paddle-mobile/Operators/Kernels/SoftmaxKernel.swift index 0c166c3563149d60dc8cbee451ef23e6a1fb9b93..6fd34702980718c1211947184e7d68e12c16ab14 100644 --- a/metal/paddle-mobile/paddle-mobile/Operators/Kernels/SoftmaxKernel.swift +++ b/metal/paddle-mobile/paddle-mobile/Operators/Kernels/SoftmaxKernel.swift @@ -21,8 +21,13 @@ struct SoftmaxMetalParam { class SoftmaxKernel: Kernel, Computable{ + var metalParam: SoftmaxMetalParam required init(device: MTLDevice, param: SoftmaxParam

) { param.output.initTexture(device: device, computePrecision: computePrecision) + metalParam = SoftmaxMetalParam.init( + N: Int32(param.input.tensorDim[0]), + K: Int32(param.input.tensorDim[1]) + ) if computePrecision == .Float32 { super.init(device: device, inFunctionName: "softmax") } else if computePrecision == .Float16 { @@ -39,14 +44,7 @@ class SoftmaxKernel: Kernel, Computable{ encoder.setTexture(param.input.metalTexture, index: 0) encoder.setTexture(param.output.metalTexture, index: 1) - var smp = SoftmaxMetalParam.init( - N: Int32(param.input.tensorDim[0]), - K: Int32(param.input.tensorDim[1]) - ) - - print(" soft max param: ") - print(smp) - encoder.setBytes(&smp, length: MemoryLayout.size, index: 0) + encoder.setBytes(&metalParam, length: MemoryLayout.size, index: 0) encoder.dispatch(computePipline: pipline, outTexture: param.output.metalTexture) encoder.endEncoding() } diff --git a/metal/paddle-mobile/paddle-mobile/Operators/Kernels/TransposeKernel.swift b/metal/paddle-mobile/paddle-mobile/Operators/Kernels/TransposeKernel.swift index 6594b3474f0abb04364246830f79302f487af499..25f8125534d83f02f919aa8dae3325ff15ca5265 100644 --- a/metal/paddle-mobile/paddle-mobile/Operators/Kernels/TransposeKernel.swift +++ b/metal/paddle-mobile/paddle-mobile/Operators/Kernels/TransposeKernel.swift @@ -57,13 +57,7 @@ class TransposeKernel: Kernel, Computable, Testable { invT[v] = i } var axis: [Int] = [0, 1, 2, 3] - - // var doNothing = false - // if param.axis.count == param.input.transpose.count { - // doNothing = param.axis == param.input.transpose.map { Int32($0) } - // } - - + for i in 0..: Kernel, Computable, Testable { tmp.iC = Int32(param.input.dim[param.input.transpose[3]]) tmp.oC = Int32(param.output.dim[3]) if realAxis == [0, 1, 2, 3] { - print("====> transpose! FAST :)") +// print("====> transpose! FAST :)") } else { - print("====> transpose! SLOW :(") +// print("====> transpose! SLOW :(") } metalParam = tmp }