diff --git a/metal/paddle-mobile-demo/paddle-mobile-demo/ViewController.swift b/metal/paddle-mobile-demo/paddle-mobile-demo/ViewController.swift index e249c3aea909869aec73f5ac8fa3d8ca63382c0f..d8101d60880bdbbdf113d064a4631968d523f81f 100644 --- a/metal/paddle-mobile-demo/paddle-mobile-demo/ViewController.swift +++ b/metal/paddle-mobile-demo/paddle-mobile-demo/ViewController.swift @@ -102,9 +102,10 @@ class ViewController: UIViewController { } } } + print("sleep before ") + usleep(33000) + print("sleep after ") } - - case .CPU: guard let inInputPointer = inputPointer else { fatalError( " need input pointer " ) diff --git a/metal/paddle-mobile/paddle-mobile.xcodeproj/project.pbxproj b/metal/paddle-mobile/paddle-mobile.xcodeproj/project.pbxproj index fe260157cd907f22d0c851f1ea6e6ca02db109ba..c3d46e333be55a60f08110d640271cb54415a17e 100644 --- a/metal/paddle-mobile/paddle-mobile.xcodeproj/project.pbxproj +++ b/metal/paddle-mobile/paddle-mobile.xcodeproj/project.pbxproj @@ -57,6 +57,7 @@ FC292C82214255BD00CF622F /* MobileNetSSD.swift in Sources */ = {isa = PBXBuildFile; fileRef = FC292C7E214255BC00CF622F /* MobileNetSSD.swift */; }; FC292C85214257CB00CF622F /* CPUCompute.h in Headers */ = {isa = PBXBuildFile; fileRef = FC292C7D214255BC00CF622F /* CPUCompute.h */; settings = {ATTRIBUTES = (Public, ); }; }; FC292C872142624800CF622F /* Genet.swift in Sources */ = {isa = PBXBuildFile; fileRef = FC292C862142624800CF622F /* Genet.swift */; }; + FC33B0F02147659000714A93 /* MobileNet.swift in Sources */ = {isa = PBXBuildFile; fileRef = FC33B0EF2147659000714A93 /* MobileNet.swift */; }; FC3602CC2108819F00FACB58 /* PaddleMobileUnitTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = FC3602CB2108819F00FACB58 /* PaddleMobileUnitTest.swift */; }; FC4CB74920F0B954007C0C6D /* ConvKernel.metal in Sources */ = {isa = PBXBuildFile; fileRef = FC4CB74820F0B954007C0C6D /* ConvKernel.metal */; }; FC4CB74B20F12C30007C0C6D /* ProgramOptimize.swift in Sources */ = {isa = PBXBuildFile; fileRef = FC4CB74A20F12C30007C0C6D /* ProgramOptimize.swift */; }; @@ -169,6 +170,7 @@ FC292C7D214255BC00CF622F /* CPUCompute.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CPUCompute.h; sourceTree = ""; }; FC292C7E214255BC00CF622F /* MobileNetSSD.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MobileNetSSD.swift; sourceTree = ""; }; FC292C862142624800CF622F /* Genet.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Genet.swift; sourceTree = ""; }; + FC33B0EF2147659000714A93 /* MobileNet.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MobileNet.swift; sourceTree = ""; }; FC3602CB2108819F00FACB58 /* PaddleMobileUnitTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PaddleMobileUnitTest.swift; sourceTree = ""; }; FC4CB74820F0B954007C0C6D /* ConvKernel.metal */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.metal; path = ConvKernel.metal; sourceTree = ""; }; FC4CB74A20F12C30007C0C6D /* ProgramOptimize.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProgramOptimize.swift; sourceTree = ""; }; @@ -278,6 +280,7 @@ FC039B6C20E11C3C0081E9F8 /* paddle-mobile */ = { isa = PBXGroup; children = ( + FC33B0EF2147659000714A93 /* MobileNet.swift */, FC292C862142624800CF622F /* Genet.swift */, FC292C7E214255BC00CF622F /* MobileNetSSD.swift */, FC292C7C214255BC00CF622F /* CPUCompute.mm */, @@ -589,6 +592,7 @@ FCEBC0F420F1FDD90099DBAF /* ConvAddBatchNormReluOp.swift in Sources */, FC0E2DC020EE461F009C1FAC /* ElementwiseAddKernel.swift in Sources */, 4AF928772133F1DB005B6C3A /* BoxCoder.metal in Sources */, + FC33B0F02147659000714A93 /* MobileNet.swift in Sources */, FCEB684C212F093800D2448E /* PreluOp.swift in Sources */, FCA67CD92138287B00BD58AA /* ConvBNReluKernel.metal in Sources */, FC60DB8920E9AAA500FF203F /* MetalExtension.swift in Sources */, diff --git a/metal/paddle-mobile/paddle-mobile/MobileNet.swift b/metal/paddle-mobile/paddle-mobile/MobileNet.swift index 6c2487b0b17f68b7b60bf13b9e9f7d446ea14c43..f54c19a9d20f49a6ff9adc581fab1aef7aef55a0 100644 --- a/metal/paddle-mobile/paddle-mobile/MobileNet.swift +++ b/metal/paddle-mobile/paddle-mobile/MobileNet.swift @@ -17,12 +17,6 @@ import paddle_mobile class MobileNet: Net{ - var means: [Float] = [123.68, 116.78, 103.94] - - var scale: Float = 0.017 - - let except: Int = 0 - class MobilenetPreProccess: CusomKernel { init(device: MTLDevice) { let s = CusomKernel.Shape.init(inWidth: 224, inHeight: 224, inChannel: 3) @@ -49,7 +43,7 @@ class MobileNet: Net{ let labels = PreWords.init(fileName: "synset") - func resultStr(res: [Float]) -> String { + override public func resultStr(res: [Float]) -> String { var s: [String] = [] res.top(r: 5).enumerated().forEach{ s.append(String(format: "%d: %@ (%3.2f%%)", $0 + 1, labels[$1.0], $1.1 * 100)) @@ -57,17 +51,18 @@ class MobileNet: Net{ return s.joined(separator: "\n") } - var preprocessKernel: CusomKernel - let dim = (n: 1, h: 224, w: 224, c: 3) - let modelPath: String - let paramPath: String - let modelDir: String + - init(device: MTLDevice) { + override init(device: MTLDevice) { + super.init(device: device) + means = [123.68, 116.78, 103.94] + scale = 0.017 + except = 0 modelPath = Bundle.main.path(forResource: "model", ofType: nil) ?! "model null" paramPath = Bundle.main.path(forResource: "params", ofType: nil) ?! "para null" modelDir = "" preprocessKernel = MobilenetPreProccess.init(device: device) + dim = (n: 1, h: 224, w: 224, c: 3) } } diff --git a/metal/paddle-mobile/paddle-mobile/PaddleMobile.swift b/metal/paddle-mobile/paddle-mobile/PaddleMobile.swift index be768f665f1d3ea315cb129e75ffae48038b3f93..b43ea3742bc23d18b596d7f90935da840342dbfd 100644 --- a/metal/paddle-mobile/paddle-mobile/PaddleMobile.swift +++ b/metal/paddle-mobile/paddle-mobile/PaddleMobile.swift @@ -37,7 +37,7 @@ public class Net: NSObject { fatalError() } func fetchResult(paddleMobileRes: ResultHolder) -> [Float32] { - fatalError() + return paddleMobileRes.resultArr } @objc public init(device: MTLDevice) { super.init() diff --git a/metal/paddle-mobile/paddle-mobile/framework/Executor.swift b/metal/paddle-mobile/paddle-mobile/framework/Executor.swift index 48f31341a0730f23eaa99b26997e64514e597de7..01d8282b90c563117289cd3082c3d0a170509ba6 100644 --- a/metal/paddle-mobile/paddle-mobile/framework/Executor.swift +++ b/metal/paddle-mobile/paddle-mobile/framework/Executor.swift @@ -59,8 +59,10 @@ public class Executor { var ops: [Runable & InferShaperable] = [] let program: Program let device: MTLDevice + let inflightSemaphore: DispatchSemaphore let queue: MTLCommandQueue public init(inDevice:MTLDevice, inQueue: MTLCommandQueue, inProgram: Program) throws { + self.inflightSemaphore = DispatchSemaphore(value: 3) program = inProgram device = inDevice queue = inQueue @@ -82,6 +84,9 @@ public class Executor { guard let buffer = queue.makeCommandBuffer() else { throw PaddleMobileError.predictError(message: "CommandBuffer is nil") } + inflightSemaphore.wait() + + let resInput: MTLTexture if let inPre = preProcessKernle { do { @@ -112,8 +117,8 @@ public class Executor { outputTextures = ops[ops.count - except].inputVariant() } - buffer.addCompletedHandler { (commandbuffer) in - + buffer.addCompletedHandler { [weak self] (commandbuffer) in +// return // let inputArr = resInput.toTensor(dim: (n: dim[0], c: dim[3], h: dim[1], w: dim[2])) //// print(inputArr.strideArray()) // @@ -139,18 +144,23 @@ public class Executor { // return + guard let SSelf = self else { + fatalError() + } + let afterDate = Date.init() var resultHolder: ResultHolder if except > 0 { resultHolder = ResultHolder.init(inDim: [], inResult: [], inElapsedTime: afterDate.timeIntervalSince(beforeDate), inIntermediateResults: outputTextures) } else { - let outputVar: Variant = self.program.scope.output()! + let outputVar: Variant = SSelf.program.scope.output()! let output: Texture

= outputVar as! Texture

resultHolder = ResultHolder.init(inDim: output.dim.dims, inResult: output.toTensor(), inElapsedTime: afterDate.timeIntervalSince(beforeDate)) } completionHandle(resultHolder) + SSelf.inflightSemaphore.signal() } buffer.commit() }