diff --git a/metal/paddle-mobile/paddle-mobile/Src/Operators/Kernels/ConvAddReluKernel.swift b/metal/paddle-mobile/paddle-mobile/Src/Operators/Kernels/ConvAddReluKernel.swift index 1e385b1f2cb4ace63646ff3bb5270dbe4a4e8f15..bfc481a8775238dff05cd8c619780ea83a43d61f 100644 --- a/metal/paddle-mobile/paddle-mobile/Src/Operators/Kernels/ConvAddReluKernel.swift +++ b/metal/paddle-mobile/paddle-mobile/Src/Operators/Kernels/ConvAddReluKernel.swift @@ -147,19 +147,28 @@ class ConvAddReluKernel: Kernel, Computable { } } + var inputImage: AnyObject? + var outputImage: AnyObject? + func compute(commandBuffer: MTLCommandBuffer, param: ConvAddReluParam

) throws { if #available(iOS 10.0, *) { if let conv = mpsConvOp as? MPSCNNConvolution { - let inputImage = MPSImage.init(texture: param.input.metalTexture, featureChannels: param.input.tensorDim[1]) - let outputImage = MPSImage.init(texture: param.output.metalTexture, featureChannels: param.output.tensorDim[1]) - conv.encode(commandBuffer: commandBuffer, sourceImage: inputImage, destinationImage: outputImage) - if #available(iOS 11.3, *) { - if let add = mpsAddOp as? MPSCNNAdd, let y = param.y { - let biasImage = MPSImage.init(texture: y.metalTexture, featureChannels: y.tensorDim[1]) - add.encode(commandBuffer: commandBuffer, primaryImage: outputImage, secondaryImage: biasImage, destinationImage: outputImage) - } - if let relu = mpsReluOp as? MPSCNNNeuronReLU { - relu.encode(commandBuffer: commandBuffer, sourceImage: outputImage, destinationImage: outputImage) + if inputImage == nil { + inputImage = MPSImage.init(texture: param.input.metalTexture, featureChannels: param.input.tensorDim[1]) + } + if outputImage == nil { + outputImage = MPSImage.init(texture: param.output.metalTexture, featureChannels: param.output.tensorDim[1]) + } + if let inputImage = inputImage as? MPSImage, let outputImage = outputImage as? MPSImage { + conv.encode(commandBuffer: commandBuffer, sourceImage: inputImage, destinationImage: outputImage) + if #available(iOS 11.3, *) { + if let add = mpsAddOp as? MPSCNNAdd, let y = param.y { + let biasImage = MPSImage.init(texture: y.metalTexture, featureChannels: y.tensorDim[1]) + add.encode(commandBuffer: commandBuffer, primaryImage: outputImage, secondaryImage: biasImage, destinationImage: outputImage) + } + if let relu = mpsReluOp as? MPSCNNNeuronReLU { + relu.encode(commandBuffer: commandBuffer, sourceImage: outputImage, destinationImage: outputImage) + } } } return diff --git a/metal/paddle-mobile/paddle-mobile/Src/Operators/Kernels/ScaleOpKernel.swift b/metal/paddle-mobile/paddle-mobile/Src/Operators/Kernels/ScaleOpKernel.swift index 5f2f20308d25f83eee29441eee04ef23169b4f96..fc63c08ebc8b91c83022356faa2b388c187b6d7b 100644 --- a/metal/paddle-mobile/paddle-mobile/Src/Operators/Kernels/ScaleOpKernel.swift +++ b/metal/paddle-mobile/paddle-mobile/Src/Operators/Kernels/ScaleOpKernel.swift @@ -23,6 +23,8 @@ struct ScaleMetalParam { class ScaleOpKernel: Kernel, Computable{ var metalParam: ScaleMetalParam var mpsScaleOp: AnyObject? + var inputImage: AnyObject? + var outputImage: AnyObject? required init(device: MTLDevice, param: ScaleParam

, initContext: InitContext) throws { do { @@ -67,9 +69,15 @@ class ScaleOpKernel: Kernel, Computable{ func compute(commandBuffer: MTLCommandBuffer, param: ScaleParam

) throws { if #available(iOS 10.0, *), let mpsScaleOp = mpsScaleOp as? MPSCNNNeuronLinear { - let inputImage = MPSImage.init(texture: param.input.metalTexture, featureChannels: param.input.tensorDim[1]) - let outputImage = MPSImage.init(texture: param.output.metalTexture, featureChannels: param.output.tensorDim[1]) - mpsScaleOp.encode(commandBuffer: commandBuffer, sourceImage: inputImage, destinationImage: outputImage) + if inputImage == nil { + inputImage = MPSImage.init(texture: param.input.metalTexture, featureChannels: param.input.tensorDim[1]) + } + if outputImage == nil { + outputImage = MPSImage.init(texture: param.output.metalTexture, featureChannels: param.output.tensorDim[1]) + } + if let inputImage = inputImage as? MPSImage, let outputImage = outputImage as? MPSImage { + mpsScaleOp.encode(commandBuffer: commandBuffer, sourceImage: inputImage, destinationImage: outputImage) + } return } guard let encoder = commandBuffer.makeComputeCommandEncoder() else {