From b9965649e7d7000c4b0c05d3e26195e97e8fbb55 Mon Sep 17 00:00:00 2001 From: liuruilong Date: Sun, 2 Sep 2018 18:25:23 +0800 Subject: [PATCH] run mobilenet --- .../paddle-mobile-demo/ViewController.swift | 2 +- .../paddle-mobile/Executor.swift | 14 ++++--- .../Operators/ConvAddBatchNormReluOp.swift | 2 +- .../paddle-mobile/Operators/ConvAddOp.swift | 42 +++++++------------ .../Kernels/ConvAddBatchNormReluKernel.swift | 7 +--- .../Operators/Kernels/SoftmaxKernel.swift | 24 ++++++----- .../paddle-mobile/Operators/SoftmaxOp.swift | 4 +- 7 files changed, 45 insertions(+), 50 deletions(-) diff --git a/metal/paddle-mobile-demo/paddle-mobile-demo/ViewController.swift b/metal/paddle-mobile-demo/paddle-mobile-demo/ViewController.swift index 8b3f1efc9d..e4697135e9 100644 --- a/metal/paddle-mobile-demo/paddle-mobile-demo/ViewController.swift +++ b/metal/paddle-mobile-demo/paddle-mobile-demo/ViewController.swift @@ -109,7 +109,7 @@ class ViewController: UIViewController { threadPickerView.delegate = self threadPickerView.dataSource = self - selectImage = UIImage.init(named: "hand.jpg") + selectImage = UIImage.init(named: "banana.jpeg") selectImageView.image = selectImage net.getTexture(image: selectImage!.cgImage!) {[weak self] (texture) in self?.toPredictTexture = texture diff --git a/metal/paddle-mobile/paddle-mobile/Executor.swift b/metal/paddle-mobile/paddle-mobile/Executor.swift index d28525b187..1d7e5a452a 100644 --- a/metal/paddle-mobile/paddle-mobile/Executor.swift +++ b/metal/paddle-mobile/paddle-mobile/Executor.swift @@ -119,7 +119,7 @@ public class Executor { // }) // print(inputArr.strideArray()) // -// writeToLibrary(fileName: "genet_input_hand", array: inputArr) +// writeToLibrary(fileName: "banana", array: inputArr) // print("write to library done") // return // print(inputArr) @@ -128,11 +128,13 @@ public class Executor { // print(stridableInput) // let _: Flo? = input.logDesc(header: "input: ", stridable: true) -// for i in 0..: OpParam { scale = try ConvAddBatchNormReluParam.inputScale(inputs: opDesc.paraInputs, from: inScope) mean = try ConvAddBatchNormReluParam.inputMean(inputs: opDesc.paraInputs, from: inScope) - y = try ConvAddBatchNormReluParam.inputY(inputs: opDesc.inputs, from: inScope) + y = try ConvAddBatchNormReluParam.inputY(inputs: opDesc.paraInputs, from: inScope) } catch let error { throw error } diff --git a/metal/paddle-mobile/paddle-mobile/Operators/ConvAddOp.swift b/metal/paddle-mobile/paddle-mobile/Operators/ConvAddOp.swift index d15ec685a5..5e184844d8 100644 --- a/metal/paddle-mobile/paddle-mobile/Operators/ConvAddOp.swift +++ b/metal/paddle-mobile/paddle-mobile/Operators/ConvAddOp.swift @@ -93,34 +93,24 @@ class ConvAddOp: Operator, ConvAddParam

>, } func delogOutput() { +// print("op \(type): ") +// print(" padding: ") +// print(para.paddings) +// print("stride: ") +// print(para.stride) +// print("dilations: ") +// print(para.dilations) +// print(" para input dim: ") +// print(para.input.dim) +// print(" para filter dim: ") +// print(para.filter.dim) +// print(" para output dim: ") +// print(para.output.dim) +// print(" biase: ") +// let biase: [Float32] = para.y.buffer.array() +// print(biase) - - print("op \(type): ") print(" \(type) output: ") - print(" padding: ") - print(para.paddings) - print("stride: ") - print(para.stride) - print("dilations: ") - print(para.dilations) - print(" para input dim: ") - print(para.input.dim) - print(" para filter dim: ") - print(para.filter.dim) - print(" para output dim: ") - print(para.output.dim) - print(" biase: ") - let biase: [Float32] = para.y.buffer.array() - print(biase) - - print(" - filter - ") - let array: [Float32] = para.filter.buffer.array() - print(array) - - print(" - y - ") - let yArray: [Float32] = para.y.buffer.array() - print(yArray) - print(para.output.metalTexture.toTensor(dim: (n: para.output.tensorDim[0], c: para.output.tensorDim[1], h: para.output.tensorDim[2], w: para.output.tensorDim[3])).strideArray()) } } diff --git a/metal/paddle-mobile/paddle-mobile/Operators/Kernels/ConvAddBatchNormReluKernel.swift b/metal/paddle-mobile/paddle-mobile/Operators/Kernels/ConvAddBatchNormReluKernel.swift index 909ff47e81..66324dd470 100644 --- a/metal/paddle-mobile/paddle-mobile/Operators/Kernels/ConvAddBatchNormReluKernel.swift +++ b/metal/paddle-mobile/paddle-mobile/Operators/Kernels/ConvAddBatchNormReluKernel.swift @@ -81,8 +81,6 @@ class ConvAddBatchNormReluKernel: Kernel, Computable, Testable fatalError() } - - let offsetX = param.filter.width/2 - Int(param.paddings[0]) let offsetY = param.filter.height/2 - Int(param.paddings[1]) @@ -121,10 +119,10 @@ class ConvAddBatchNormReluKernel: Kernel, Computable, Testable var newBiaseBuffer: MTLBuffer var newScaleBuffer: MTLBuffer - if computePrecision == .Float16 { + if computePrecision == .Float32 { newBiaseBuffer = device.makeBuffer(bytes: newBiase, length: param.bias.buffer.length)! newScaleBuffer = device.makeBuffer(bytes: newScale, length: param.scale.buffer.length)! - } else if computePrecision == .Float32 { + } else if computePrecision == .Float16 { newBiaseBuffer = device.makeBuffer(length: param.bias.buffer.length / 2)! newScaleBuffer = device.makeBuffer(length: param.bias.buffer.length / 2)! @@ -151,7 +149,6 @@ class ConvAddBatchNormReluKernel: Kernel, Computable, Testable throw PaddleMobileError.predictError(message: " encode is nil") } - encoder.setTexture(param.input.metalTexture, index: 0) encoder.setTexture(param.output.metalTexture, index: 1) encoder.setBytes(&metalParam, length: MemoryLayout.size, index: 0) diff --git a/metal/paddle-mobile/paddle-mobile/Operators/Kernels/SoftmaxKernel.swift b/metal/paddle-mobile/paddle-mobile/Operators/Kernels/SoftmaxKernel.swift index 6f6d0af477..0c166c3563 100644 --- a/metal/paddle-mobile/paddle-mobile/Operators/Kernels/SoftmaxKernel.swift +++ b/metal/paddle-mobile/paddle-mobile/Operators/Kernels/SoftmaxKernel.swift @@ -21,6 +21,17 @@ struct SoftmaxMetalParam { class SoftmaxKernel: Kernel, Computable{ + required init(device: MTLDevice, param: SoftmaxParam

) { + param.output.initTexture(device: device, computePrecision: computePrecision) + if computePrecision == .Float32 { + super.init(device: device, inFunctionName: "softmax") + } else if computePrecision == .Float16 { + super.init(device: device, inFunctionName: "softmax_half") + } else { + fatalError() + } + } + func compute(commandBuffer: MTLCommandBuffer, param: SoftmaxParam

) throws { guard let encoder = commandBuffer.makeComputeCommandEncoder() else { throw PaddleMobileError.predictError(message: " encoder is nil") @@ -32,19 +43,12 @@ class SoftmaxKernel: Kernel, Computable{ N: Int32(param.input.tensorDim[0]), K: Int32(param.input.tensorDim[1]) ) + + print(" soft max param: ") + print(smp) encoder.setBytes(&smp, length: MemoryLayout.size, index: 0) encoder.dispatch(computePipline: pipline, outTexture: param.output.metalTexture) encoder.endEncoding() } - required init(device: MTLDevice, param: SoftmaxParam

) { - param.output.initTexture(device: device, computePrecision: computePrecision) - if computePrecision == .Float32 { - super.init(device: device, inFunctionName: "softmax") - } else if computePrecision == .Float16 { - super.init(device: device, inFunctionName: "softmax_half") - } else { - fatalError() - } - } } diff --git a/metal/paddle-mobile/paddle-mobile/Operators/SoftmaxOp.swift b/metal/paddle-mobile/paddle-mobile/Operators/SoftmaxOp.swift index 71c7918d4e..26a50f1533 100644 --- a/metal/paddle-mobile/paddle-mobile/Operators/SoftmaxOp.swift +++ b/metal/paddle-mobile/paddle-mobile/Operators/SoftmaxOp.swift @@ -52,7 +52,9 @@ class SoftmaxOp: Operator, SoftmaxParam

>, func delogOutput() { print("softmax delog") - + print(para.input) + + print(para.output) let originDim = para.output.originDim let outputArray: [Float32] = para.output.metalTexture.realNHWC(dim: (n: originDim[0], h: originDim[1], w: originDim[2], c: originDim[3])) print(outputArray.strideArray()) -- GitLab