.shared.creat(opDesc: op, scope: inProgram.scope) + let op = try OpCreator
.shared.creat(device: inDevice, opDesc: op, scope: inProgram.scope)
op.inferShape()
ops.append(op)
} catch let error {
@@ -65,12 +68,29 @@ public class Executor {
+ let beforeDate = Date.init()
let inputTexture = InputTexture.init(inMTLTexture: input, inExpectDim: Dim.init(inDim: expect))
program.scope.setInput(input: inputTexture)
+ guard let buffer = queue.makeCommandBuffer() else {
+ throw PaddleMobileError.predictError(message: "CommandBuffer is nil")
+ }
+
for op in ops {
- op.run()
+ do {
+ try op.run(device: device, buffer: buffer)
+ } catch let error {
+ throw error
+ }
}
+ buffer.addCompletedHandler { (commandbuffer) in
+ let afterDate = Date.init()
+ print(afterDate.timeIntervalSince(beforeDate))
+ print(" encoder end ! ")
+ }
+
+ buffer.commit()
+
guard let outputVar = program.scope.output() else {
throw PaddleMobileError.netError(message: "output nil")
}
@@ -78,6 +98,8 @@ public class Executor else {
throw PaddleMobileError.netError(message: "output var type error")
}
+
+
return output
}
diff --git a/metal/paddle-mobile/paddle-mobile/Loader.swift b/metal/paddle-mobile/paddle-mobile/Loader.swift
index 8bec6f6ebc76587ff047768d782647d0f9ac51e1..4ef1e1b0720561d45d8bceaa93d4cbe6b09074d0 100644
--- a/metal/paddle-mobile/paddle-mobile/Loader.swift
+++ b/metal/paddle-mobile/paddle-mobile/Loader.swift
@@ -68,11 +68,24 @@ public class Loader .size else {
- throw PaddleMobileError.memoryError(message: "PrecisionType not support")
- }
+// guard (try? tensorDesc.dataType.dataTypeSize()) == MemoryLayout .size else {
+// throw PaddleMobileError.memoryError(message: "PrecisionType not support")
+// }
if (varDesc.persistable
&& varDesc.type != .FeedMiniBatch
@@ -149,7 +162,7 @@ public class Loader .init(device: device, inDim: dim)
}
} else {
if varDesc.name == fetchKey {
diff --git a/metal/paddle-mobile/paddle-mobile/Operators/Base/OpCreator.swift b/metal/paddle-mobile/paddle-mobile/Operators/Base/OpCreator.swift
index dfe5be4240ba48dadbb144dc95015c7ab318051f..579526d7e7e4a2f6570b97553eb0c3d3c6d7530c 100644
--- a/metal/paddle-mobile/paddle-mobile/Operators/Base/OpCreator.swift
+++ b/metal/paddle-mobile/paddle-mobile/Operators/Base/OpCreator.swift
@@ -27,19 +27,19 @@ class OpCreator .creat,
gBatchNormType : BatchNormOp .creat,
gReluType : ReluOp .creat,
diff --git a/metal/paddle-mobile/paddle-mobile/Operators/Base/Operator.swift b/metal/paddle-mobile/paddle-mobile/Operators/Base/Operator.swift
index d74d5ff93ce165313394b452679f1fb72593f898..222c5319c6d7dfd88d8a83140bef98e67dcb2fd5 100644
--- a/metal/paddle-mobile/paddle-mobile/Operators/Base/Operator.swift
+++ b/metal/paddle-mobile/paddle-mobile/Operators/Base/Operator.swift
@@ -12,29 +12,35 @@
See the License for the specific language governing permissions and
limitations under the License. */
+import Metal
import Foundation
protocol Runable {
- func run()
- func runImpl()
+ func run(device: MTLDevice, buffer: MTLCommandBuffer) throws
+ func runImpl(device: MTLDevice,buffer: MTLCommandBuffer) throws
}
extension Runable where Self: OperatorProtocol{
- func run() {
- runImpl()
+ func run(device: MTLDevice, buffer: MTLCommandBuffer) throws {
+ do {
+ try runImpl(device: device, buffer: buffer)
+ } catch let error {
+ throw error
+ }
+
print(type + ": " + para.outputDesc())
}
}
protocol Creator where Self: OperatorProtocol{
associatedtype OpType: OperatorProtocol & Runable & InferShaperable
- static func creat(opDesc: OpDesc, inScope: Scope) throws -> OpType
+ static func creat(device: MTLDevice, opDesc: OpDesc, inScope: Scope) throws -> OpType
}
extension Creator where Self: OperatorProtocol {
- static func creat(opDesc: OpDesc, inScope: Scope) throws -> OpType {
+ static func creat(device: MTLDevice, opDesc: OpDesc, inScope: Scope) throws -> OpType {
do {
- return try OpType.provide(opDesc: opDesc, inScope: inScope)
+ return try OpType.provide(device:device, opDesc: opDesc, inScope: inScope)
} catch let error {
throw error
}
@@ -47,19 +53,21 @@ protocol InferShaperable {
protocol OperatorProtocol {
associatedtype ParamType: OpParam
+ associatedtype KerType: Computable
var type: String { get }
var inputs: [String : [String]] { get }
var paraInputs: [String : [String]] { get }
var outpus: [String : [String]] { get }
var attrs: [String : Attr] { get }
var para: ParamType { get }
- init(opDesc: OpDesc, inScope: Scope) throws
+ var kernel: KerType { get }
+ init(device: MTLDevice, opDesc: OpDesc, inScope: Scope) throws
}
extension OperatorProtocol {
- static func provide(opDesc: OpDesc, inScope: Scope) throws -> Self {
+ static func provide(device: MTLDevice, opDesc: OpDesc, inScope: Scope) throws -> Self {
do {
- return try Self.init(opDesc: opDesc, inScope: inScope)
+ return try Self.init(device: device, opDesc: opDesc, inScope: inScope)
} catch let error {
throw error
}
@@ -67,20 +75,23 @@ extension OperatorProtocol {
}
-class Operator
+ var output: Texture
let inputBias: Tensor >, Runable, Creator, InferShaperable{
func inferShape() {
para.output.dim = para.input.dim
}
typealias OpType = BatchNormOp
- func runImpl() {
+ func runImpl(device: MTLDevice, buffer: MTLCommandBuffer) throws {
print("this is BatchNormOp")
}
}
diff --git a/metal/paddle-mobile/paddle-mobile/Operators/ConvOp.swift b/metal/paddle-mobile/paddle-mobile/Operators/ConvOp.swift
index 3b1dcfc47050c79d6053bc0463272c0a88665c45..b4623bf567cbcb17c99b28e246d3bf0d242bc23b 100644
--- a/metal/paddle-mobile/paddle-mobile/Operators/ConvOp.swift
+++ b/metal/paddle-mobile/paddle-mobile/Operators/ConvOp.swift
@@ -30,8 +30,8 @@ struct ConvParam
+ var output: Texture
let filter: Tensor >, Runable, Creator, InferShaperable {
func inferShape() {
let inDims = para.input.dim
let filterDim = para.filter.dim
@@ -63,7 +63,7 @@ class ConvOp
- func runImpl() {
+ func runImpl(device: MTLDevice, buffer: MTLCommandBuffer) throws {
print("this is conv")
}
}
diff --git a/metal/paddle-mobile/paddle-mobile/Operators/ElementwiseAddOp.swift b/metal/paddle-mobile/paddle-mobile/Operators/ElementwiseAddOp.swift
index f0966c5b4d8ee358443fdbdf32d99d3e741aba1f..12c0b832b76b0975d4b5a138c283866166e06130 100644
--- a/metal/paddle-mobile/paddle-mobile/Operators/ElementwiseAddOp.swift
+++ b/metal/paddle-mobile/paddle-mobile/Operators/ElementwiseAddOp.swift
@@ -26,20 +26,20 @@ struct ElementwiseAddParam
let inputY: Tensor
- var output: Texture
+ var output: Texture
let axis: Int
}
-class ElementwiseAddOp >, Runable, Creator, InferShaperable{
func inferShape() {
para.output.dim = para.input.dim
}
typealias OpType = ElementwiseAddOp
- func runImpl() {
+ func runImpl(device: MTLDevice, buffer: MTLCommandBuffer) throws {
print("this is ElementwiseAddOp")
}
}
diff --git a/metal/paddle-mobile/paddle-mobile/Operators/FeedOp.swift b/metal/paddle-mobile/paddle-mobile/Operators/FeedOp.swift
index 9055fcd7e7b6a97d6e291d5086e5fd10855e2d82..87b71a7a95d316cd226e4e648506fbe011d5e4c8 100644
--- a/metal/paddle-mobile/paddle-mobile/Operators/FeedOp.swift
+++ b/metal/paddle-mobile/paddle-mobile/Operators/FeedOp.swift
@@ -15,7 +15,7 @@
import Foundation
struct FeedParam
var input: InputTexture {
return scope.input() as! InputTexture
}
@@ -33,19 +33,26 @@ struct FeedParam >, Runable, Creator, InferShaperable {
typealias OpType = FeedOp
func inferShape() {
// print("feed input: \(para.input.expectDim)")
print("feed output: \(para.output.dim)")
-
-// para.ou/tput.dim = para.input.expectDim
+ // para.output.dim =
+// para.output.dim = para.input.expectDim
}
- func runImpl() {
- print("feed op")
-// let resizeKernel = ResizeKernel.init(device: <#T##MTLDevice#>)
+ func runImpl(device: MTLDevice, buffer: MTLCommandBuffer) throws {
+ let resizeKernel = ResizeKernel .init(device: device)
+ let resizeParam = ResizeParam.init(input: para.input.mtlTexture, output: para.output.metalTexture, expectDim: para.input.expectDim)
+ do {
+ print("feed op to compute ")
+ try resizeKernel.compute(commandBuffer: buffer, param: resizeParam)
+ print("feed op end compute ")
+ } catch let error {
+ throw error
+ }
}
}
diff --git a/metal/paddle-mobile/paddle-mobile/Operators/FetchOp.swift b/metal/paddle-mobile/paddle-mobile/Operators/FetchOp.swift
index 6e7024909a65475fd1870d7bae01894dbe7f0395..b79538fe7770dd818eb7399547d2621249938c89 100644
--- a/metal/paddle-mobile/paddle-mobile/Operators/FetchOp.swift
+++ b/metal/paddle-mobile/paddle-mobile/Operators/FetchOp.swift
@@ -16,7 +16,7 @@ import Foundation
struct FetchParam = ResultHolder.init(inDim: [], inResult: [])
- let input: Texture
+ let input: Texture
let scope: Scope
init(opDesc: OpDesc, inScope: Scope) throws {
scope = inScope
@@ -30,14 +30,14 @@ struct FetchParam >, Runable, Creator, InferShaperable{
func inferShape() {
print(para.input.dim)
}
typealias OpType = FetchOp
- func runImpl() {
+ func runImpl(device: MTLDevice, buffer: MTLCommandBuffer) throws {
print("fetch op")
}
}
diff --git a/metal/paddle-mobile/paddle-mobile/Operators/Kernels/BatchNormKernel.swift b/metal/paddle-mobile/paddle-mobile/Operators/Kernels/BatchNormKernel.swift
new file mode 100644
index 0000000000000000000000000000000000000000..b525b9bf61a2e66f6e417076eea813d790fe909c
--- /dev/null
+++ b/metal/paddle-mobile/paddle-mobile/Operators/Kernels/BatchNormKernel.swift
@@ -0,0 +1,19 @@
+//
+// BatchNormKernel.swift
+// paddle-mobile
+//
+// Created by liuRuiLong on 2018/7/5.
+// Copyright © 2018年 orange. All rights reserved.
+//
+
+import Foundation
+
+class BatchNormKernel ) throws {
+
+ }
+}
diff --git a/metal/paddle-mobile/paddle-mobile/Operators/Kernels/ConvKernel.swift b/metal/paddle-mobile/paddle-mobile/Operators/Kernels/ConvKernel.swift
new file mode 100644
index 0000000000000000000000000000000000000000..0cdab6f6b967214a0d9e8a11ae8768b252ead38e
--- /dev/null
+++ b/metal/paddle-mobile/paddle-mobile/Operators/Kernels/ConvKernel.swift
@@ -0,0 +1,20 @@
+//
+// ConvKernel.swift
+// paddle-mobile
+//
+// Created by liuRuiLong on 2018/7/5.
+// Copyright © 2018年 orange. All rights reserved.
+//
+
+import Foundation
+
+
+class ConvKernel ) throws {
+
+ }
+ required init(device: MTLDevice) {
+ super.init(device: device, inFunctionName: "conv")
+ }
+
+}
diff --git a/metal/paddle-mobile/paddle-mobile/Operators/Kernels/ElementwiseAddKernel.swift b/metal/paddle-mobile/paddle-mobile/Operators/Kernels/ElementwiseAddKernel.swift
new file mode 100644
index 0000000000000000000000000000000000000000..a93f04842b1b3ce619e2a679c9283853743f1358
--- /dev/null
+++ b/metal/paddle-mobile/paddle-mobile/Operators/Kernels/ElementwiseAddKernel.swift
@@ -0,0 +1,20 @@
+//
+// ElementwiseAddKernel.swift
+// paddle-mobile
+//
+// Created by liuRuiLong on 2018/7/5.
+// Copyright © 2018年 orange. All rights reserved.
+//
+
+import Foundation
+
+
+class ElementwiseAddKernel ) throws {
+
+ }
+}
diff --git a/metal/paddle-mobile/paddle-mobile/Operators/Kernels/Kernel.swift b/metal/paddle-mobile/paddle-mobile/Operators/Kernels/Kernel.swift
index 1fa44c4b975ce543b2a5feafb4a5e19df419a043..b22da765d961837b29b6052413ed4d019f8f1a63 100644
--- a/metal/paddle-mobile/paddle-mobile/Operators/Kernels/Kernel.swift
+++ b/metal/paddle-mobile/paddle-mobile/Operators/Kernels/Kernel.swift
@@ -18,6 +18,12 @@ import Foundation
protocol Computable {
associatedtype ParamType
func compute(commandBuffer: MTLCommandBuffer, param: ParamType) throws
+ init(device: MTLDevice)
+}
+
+protocol KernelProtocol {
+ var pipline: MTLComputePipelineState { get set }
+ var functionName: String { get set }
}
class Kernel {
diff --git a/metal/paddle-mobile/paddle-mobile/Operators/Kernels/Kernels.metal b/metal/paddle-mobile/paddle-mobile/Operators/Kernels/Kernels.metal
index 0b23f5eb4e398ff787d45829db89a4cc3b3b2032..0d872196c9d19497fa37a35dc9b294843624ca56 100644
--- a/metal/paddle-mobile/paddle-mobile/Operators/Kernels/Kernels.metal
+++ b/metal/paddle-mobile/paddle-mobile/Operators/Kernels/Kernels.metal
@@ -1,10 +1,16 @@
-//
-// Kernels.metal
-// paddle-mobile
-//
-// Created by liuRuiLong on 2018/7/4.
-// Copyright © 2018年 orange. All rights reserved.
-//
+/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License. */
#include ) throws {
+ guard let encoder = commandBuffer.makeComputeCommandEncoder() else {
+ throw PaddleMobileError.predictError(message: " encode is nil")
+ }
+ print(" the usage of input of relu \(param.input.metalTexture.usage)")
+ encoder.setTexture(param.input.metalTexture, index: 0)
+ encoder.setTexture(param.output.metalTexture, index: 1)
+ encoder.dispatch(computePipline: pipline, outTexture: param.output.metalTexture)
+ encoder.endEncoding()
+ }
+
+ required init(device: MTLDevice) {
+ super.init(device: device, inFunctionName: "relu")
+ }
+}
diff --git a/metal/paddle-mobile/paddle-mobile/Operators/Kernels/ResizeKernel.swift b/metal/paddle-mobile/paddle-mobile/Operators/Kernels/ResizeKernel.swift
index edd79fb0c005c172a6d687f893be28b1319342ae..9de09ccb98d71eb41ae576ad19677b7f46eea70b 100644
--- a/metal/paddle-mobile/paddle-mobile/Operators/Kernels/ResizeKernel.swift
+++ b/metal/paddle-mobile/paddle-mobile/Operators/Kernels/ResizeKernel.swift
@@ -1,10 +1,16 @@
-//
-// ResizeKernel.swift
-// paddle-mobile
-//
-// Created by liuRuiLong on 2018/7/4.
-// Copyright © 2018年 orange. All rights reserved.
-//
+/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License. */
import Foundation
@@ -22,15 +28,14 @@ struct OutputDim {
let strideY: UInt16
}
-class ResizeKernel: Kernel, Computable{
+class ResizeKernel
+ var output: Texture
}
-class ReluOp >, Runable, Creator, InferShaperable{
func inferShape() {
para.output.dim = para.input.dim
}
typealias OpType = ReluOp
- func runImpl() {
- print("this is ReluOp")
+ func runImpl(device: MTLDevice, buffer: MTLCommandBuffer) throws {
+ do {
+ try kernel.compute(commandBuffer: buffer, param: para)
+ } catch let error {
+ throw error
+ }
}
}
diff --git a/metal/paddle-mobile/paddle-mobile/framework/Texture.swift b/metal/paddle-mobile/paddle-mobile/framework/Texture.swift
index 1e650c99e65c1333617f0213a9425b2ffe0eb817..141dc70df2e966516eaa340cbecbaa6ccb722697 100644
--- a/metal/paddle-mobile/paddle-mobile/framework/Texture.swift
+++ b/metal/paddle-mobile/paddle-mobile/framework/Texture.swift
@@ -38,7 +38,7 @@ extension InputTexture {
}
}
-public class Texture: Tensorial {
+public class Texture .size == 1 {
+ tmpTextureDes.pixelFormat = .r8Sint
+ } else if MemoryLayout .size == 2 {
+ tmpTextureDes.pixelFormat = .r16Float
+ } else if MemoryLayout .size == 4 {
+ tmpTextureDes.pixelFormat = .r32Float
+ }
+
+ tmpTextureDes.usage = .unknown
tmpTextureDes.storageMode = .shared
textureDesc = tmpTextureDes
metalTexture = device.makeTexture(descriptor: tmpTextureDes) ?! " texture nil "