diff --git a/metal/paddle-mobile/paddle-mobile/Src/Operators/Kernels/ConvAddKernel.swift b/metal/paddle-mobile/paddle-mobile/Src/Operators/Kernels/ConvAddKernel.swift index 0e893d35e6475d967f3de883b2236b687bacd776..c46640943ba6c2840dc37244d60ca44ab0957d00 100644 --- a/metal/paddle-mobile/paddle-mobile/Src/Operators/Kernels/ConvAddKernel.swift +++ b/metal/paddle-mobile/paddle-mobile/Src/Operators/Kernels/ConvAddKernel.swift @@ -15,16 +15,6 @@ import Foundation import MetalPerformanceShaders -@available(iOS 10.0, *) -var convDic: [String : MPSCNNConvolution] = [:] - -/// 获取唯一字符串 -/// -/// - Returns: 唯一字符串 -func getUniqueKey() -> String { - return UUID.init().uuidString -} - @available(iOS 11.0, *) class ConvDataSource: NSObject, MPSCNNConvolutionDataSource { @@ -99,8 +89,7 @@ class ConvDataSource: NSObject, MPSCNNConvolutionDataSourc class ConvAddKernel: Kernel, Computable { var metalParam: MetalConvParam! - - let identifyingKey: String = getUniqueKey() + var mpsConvOp: Any? required init(device: MTLDevice, param: ConvAddParam

, initContext: InitContext) throws { do { @@ -139,7 +128,7 @@ class ConvAddKernel: Kernel, Computable { func compute(commandBuffer: MTLCommandBuffer, param: ConvAddParam

) throws { if #available(iOS 10.0, *) { - if let conv = convDic[identifyingKey] { + 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) @@ -158,18 +147,10 @@ class ConvAddKernel: Kernel, Computable { encoder.endEncoding() } - deinit { - if #available(iOS 10.0, *) { - convDic.removeValue(forKey: identifyingKey) - } - } - func setupWithMPS(device: MTLDevice, param: ConvAddParam

) { let offsetX = (Int(param.dilations[0]) * (param.filter.tensorDim[3] - 1) + 1) / 2 - Int(param.paddings[0]) let offsetY = (Int(param.dilations[1]) * (param.filter.tensorDim[2] - 1) + 1) / 2 - Int(param.paddings[1]) - let key = identifyingKey - let isDepthWise = param.filter.tensorDim[1] == 1 && param.filter.tensorDim[0] == param.input.tensorDim[1] if #available(iOS 11.0, *) { param.input.useMPS = true @@ -192,7 +173,7 @@ class ConvAddKernel: Kernel, Computable { let conv = MPSCNNConvolution.init(device: device, weights: dataSource) conv.offset = MPSOffset.init(x: offsetX, y: offsetY, z: 0) conv.edgeMode = .zero - convDic[key] = conv + mpsConvOp = conv } }