提交 be3cdaaf 编写于 作者: N NazgulLee 提交者: GitHub

fix semaphore crash (#1591)

上级 491737d0
......@@ -43,7 +43,7 @@ import paddle_mobile
metalLibPath = Bundle.main.path(forResource: "paddle-mobile-metallib", ofType: "metallib")
}
override public func updateProgram(program: Program) {
override public func updateProgram(program: Program) throws {
// n h w c
for block in program.programDesc.blocks {
for varDesc in block.vars {
......@@ -54,14 +54,21 @@ import paddle_mobile
let newDim = Dim.init(inDim: [texture.dim[0], inputDim[1], inputDim[2], texture.tensorDim[1]])
print(" var desc name " + varDesc.name + " new dim" + "\(newDim)")
texture.updateDims(inTensorDim: Dim.init(inDim: [texture.tensorDim[0], texture.tensorDim[1], inputDim[1], inputDim[2]]), inDim: newDim)
texture.initTexture(device: device, inTranspose: [0, 1, 2, 3], computePrecision: GlobalConfig.shared.computePrecision)
do {
try texture.updateDims(inTensorDim: Dim.init(inDim: [texture.tensorDim[0], texture.tensorDim[1], inputDim[1], inputDim[2]]), inDim: newDim)
try texture.initTexture(device: device, inTranspose: [0, 1, 2, 3], computePrecision: GlobalConfig.shared.computePrecision)
} catch let error {
throw error
}
let output: FetchHolder = program.scope.output() as! FetchHolder
output.dim = newDim
output.capacity = newDim.numel()
output.paddedCapacity = newDim.numel() * 4
output.initBuffer(device: device)
if let output: FetchHolder = program.scope.output() as? FetchHolder {
output.dim = newDim
output.capacity = newDim.numel()
output.paddedCapacity = newDim.numel() * 4
output.initBuffer(device: device)
} else {
throw PaddleMobileError.loaderError(message: "scope output nil")
}
}
}
}
......
......@@ -119,13 +119,16 @@ import Foundation
@objc public func predict(texture: MTLTexture, completion: @escaping ( _ success: Bool, _ result: [ResultHolder]?) -> Void) {
do {
try self.executor?.predict(input: texture, dim: self.net.inputDim, completionHandle: { [weak self] (res) in
guard let SSelf = self else {
fatalError( " self nil " )
try self.executor?.predict(input: texture, dim: self.net.inputDim, completionHandle: { [weak self] (success, res) in
if success, let SSelf = self, let res = res {
let result = SSelf.net.fetchResult(paddleMobileRes: res)
if result.count > 0 {
completion(true, result)
return
}
}
let result = SSelf.net.fetchResult(paddleMobileRes: res)
completion(true, result)
}, preProcessKernle: self.net.preprocessKernel, except: self.net.except)
completion(false, nil)
}, preProcessKernle: self.net.preprocessKernel, except: self.net.except)
} catch let error {
print(error)
completion(false, nil)
......
......@@ -42,7 +42,7 @@ var isTest = false
}
protocol Executorable {
func predict(input: MTLTexture, dim: Dim, completionHandle: @escaping ([GPUResultHolder]) -> Void, preProcessKernle: CusomKernel?, except: Int) throws
func predict(input: MTLTexture, dim: Dim, completionHandle: @escaping ( _ success: Bool, _ result: [GPUResultHolder]?) -> Void, preProcessKernle: CusomKernel?, except: Int) throws
func clear()
}
......@@ -53,6 +53,8 @@ public class Executor<P: PrecisionProtocol>: Executorable{
let device: MTLDevice
let inflightSemaphore: DispatchSemaphore
let queue: MTLCommandQueue
private var isValid = true
init(inDevice:MTLDevice, inQueue: MTLCommandQueue, inProgram: Program, initContext: InitContext) throws {
self.inflightSemaphore = DispatchSemaphore(value: 1)
program = inProgram
......@@ -73,9 +75,11 @@ public class Executor<P: PrecisionProtocol>: Executorable{
}
}
public func predict(input: MTLTexture, dim: Dim, completionHandle: @escaping ([GPUResultHolder]) -> Void, preProcessKernle: CusomKernel? = nil, except: Int = 0) throws {
public func predict(input: MTLTexture, dim: Dim, completionHandle: @escaping ( _ success: Bool, _ result: [GPUResultHolder]?) -> Void, preProcessKernle: CusomKernel? = nil, except: Int = 0) throws {
inflightSemaphore.wait()
guard isValid else {
throw PaddleMobileError.predictError(message: "Executor is cleared and invalid")
}
guard let buffer = queue.makeCommandBuffer() else {
throw PaddleMobileError.predictError(message: "CommandBuffer is nil")
}
......@@ -110,9 +114,20 @@ public class Executor<P: PrecisionProtocol>: Executorable{
outputTextures = ops[ops.count - except].inputVariant()
}
let safeComplete = { [weak self] (success: Bool, result: [GPUResultHolder]?) in
completionHandle(success, result)
self?.inflightSemaphore.signal()
}
buffer.addCompletedHandler { [weak self] (commandbuffer) in
guard let SSelf = self else {
fatalError()
safeComplete(false, nil)
return
}
guard SSelf.isValid else {
safeComplete(false, nil)
return
}
//将输入写进文件
......@@ -133,24 +148,31 @@ public class Executor<P: PrecisionProtocol>: Executorable{
}
}
var resultHolder: GPUResultHolder
var resultHolder: GPUResultHolder?
if except > 0 {
resultHolder = GPUResultHolder.init(inDim: [], inPointer: nil, inCapacity: 0, inIntermediateResults: outputTextures)
} else {
let outputVar: Variant = SSelf.program.scope.output()!
let output: FetchHolder = outputVar as! FetchHolder
} else if let output = SSelf.program.scope.output() as? FetchHolder {
resultHolder = GPUResultHolder.init(inDim: output.dim.dims, inPointer: output.result, inCapacity: output.capacity)
}
completionHandle([resultHolder])
SSelf.inflightSemaphore.signal()
if let resultHolder = resultHolder {
safeComplete(true, [resultHolder])
} else {
safeComplete(false, nil)
}
}
buffer.commit()
}
public func clear() {
isValid = false
program.scope.clear()
}
deinit {
while (inflightSemaphore.signal() != 0) {
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册