From 9fbdede6d985d8a78f6214d546a6f44e9479e524 Mon Sep 17 00:00:00 2001 From: Yanzhan Yang Date: Tue, 28 May 2019 19:05:14 +0800 Subject: [PATCH] 1. lazy load for mpsimage. (#1666) --- .../Operators/Kernels/ConvAddReluKernel.swift | 29 ++++++++++++------- .../Src/Operators/Kernels/ScaleOpKernel.swift | 14 +++++++-- 2 files changed, 30 insertions(+), 13 deletions(-) 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 1e385b1f2c..bfc481a877 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 5f2f20308d..fc63c08ebc 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 { -- GitLab