From 3fff4d3406db5a5038b6aabee2a71abecbc3dd19 Mon Sep 17 00:00:00 2001 From: liuruilong Date: Mon, 27 Aug 2018 15:58:07 +0800 Subject: [PATCH] update --- .../paddle-mobile/Common/MetalExtension.swift | 38 +++++++++++++++++++ .../paddle-mobile/Executor.swift | 2 +- .../paddle-mobile/Operators/ConvAddOp.swift | 2 +- .../Kernels/ConvAddBatchNormReluKernel.swift | 2 +- .../Operators/Kernels/ConvAddKernel.swift | 2 +- .../paddle-mobile/Operators/TransposeOp.swift | 6 ++- 6 files changed, 47 insertions(+), 5 deletions(-) diff --git a/metal/paddle-mobile/paddle-mobile/Common/MetalExtension.swift b/metal/paddle-mobile/paddle-mobile/Common/MetalExtension.swift index 01e8f38099..6e01c4fb27 100644 --- a/metal/paddle-mobile/paddle-mobile/Common/MetalExtension.swift +++ b/metal/paddle-mobile/paddle-mobile/Common/MetalExtension.swift @@ -342,6 +342,10 @@ public extension MTLTexture { // n c h w - dim func toTensor(dim: (n: Int, c: Int, h: Int, w: Int)) -> [Float32] { + print("origin dim: \(dim)") + print("texture: ") + print(self) + let textureArray = floatArray { (i : Float32) -> Float32 in return i } @@ -358,8 +362,42 @@ public extension MTLTexture { } } } + print(" tensor count -- \(output.count)") return output } + + func realNHWC(dim: (n: Int, h: Int, w: Int, c: Int)) -> [Float32] { + print("origin dim: \(dim)") + print("texture: ") + print(self) + + let textureArray = floatArray { (i : Float32) -> Float32 in + return i + } + var output: [Float32] = [] + + let numOfASlice = dim.h * dim.w * 4 + for h in 0.. dim.c { + for i in 0..<((sliceIndex * 4 + 4) - dim.c) { + let value = textureArray[sliceIndex * numOfASlice + h * dim.w * 4 + w * 4 + i] + output.append(value) + } + } else { + for i in 0..<4 { + let value = textureArray[sliceIndex * numOfASlice + h * dim.w * 4 + w * 4 + i] + output.append(value) + } + } + } + } + } + print(" tensor count -- \(output.count)") + return output + } + } diff --git a/metal/paddle-mobile/paddle-mobile/Executor.swift b/metal/paddle-mobile/paddle-mobile/Executor.swift index 869d83729d..d9e2ae9fb1 100644 --- a/metal/paddle-mobile/paddle-mobile/Executor.swift +++ b/metal/paddle-mobile/paddle-mobile/Executor.swift @@ -14,7 +14,7 @@ import Foundation -let testTo = 41 +let testTo = 48 public class ResultHolder { public let dim: [Int] diff --git a/metal/paddle-mobile/paddle-mobile/Operators/ConvAddOp.swift b/metal/paddle-mobile/paddle-mobile/Operators/ConvAddOp.swift index 6f8030b8a5..545b884039 100644 --- a/metal/paddle-mobile/paddle-mobile/Operators/ConvAddOp.swift +++ b/metal/paddle-mobile/paddle-mobile/Operators/ConvAddOp.swift @@ -47,9 +47,9 @@ class ConvAddOp: Operator, ConvAddParam

>, func delogOutput() { 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()) - // print(" conv add: ") + // print(para.input.metalTexture) // print(" filter array: ") diff --git a/metal/paddle-mobile/paddle-mobile/Operators/Kernels/ConvAddBatchNormReluKernel.swift b/metal/paddle-mobile/paddle-mobile/Operators/Kernels/ConvAddBatchNormReluKernel.swift index 704d9aecc2..f2bc495158 100644 --- a/metal/paddle-mobile/paddle-mobile/Operators/Kernels/ConvAddBatchNormReluKernel.swift +++ b/metal/paddle-mobile/paddle-mobile/Operators/Kernels/ConvAddBatchNormReluKernel.swift @@ -50,7 +50,7 @@ class ConvAddBatchNormReluKernel: Kernel, Computable, Testable required init(device: MTLDevice, param: ConvAddBatchNormReluParam

) { - param.output.initTexture(device: device, inTranspose: [0, 3, 1, 2]) + param.output.initTexture(device: device, inTranspose: [0, 2, 3, 1]) if param.filter.width == 1 && param.filter.height == 1 { super.init(device: device, inFunctionName: "conv_add_batch_norm_relu_1x1") diff --git a/metal/paddle-mobile/paddle-mobile/Operators/Kernels/ConvAddKernel.swift b/metal/paddle-mobile/paddle-mobile/Operators/Kernels/ConvAddKernel.swift index 1b2795d621..3c999052fc 100644 --- a/metal/paddle-mobile/paddle-mobile/Operators/Kernels/ConvAddKernel.swift +++ b/metal/paddle-mobile/paddle-mobile/Operators/Kernels/ConvAddKernel.swift @@ -25,7 +25,7 @@ class ConvAddKernel: Kernel, Computable { super.init(device: device, inFunctionName: "conv_add_3x3") } - param.output.initTexture(device: device, inTranspose: [0, 3, 1, 2]) + param.output.initTexture(device: device, inTranspose: [0, 2, 3, 1]) let offsetX = param.filter.width/2 - Int(param.paddings[0]) let offsetY = param.filter.height/2 - Int(param.paddings[1]) diff --git a/metal/paddle-mobile/paddle-mobile/Operators/TransposeOp.swift b/metal/paddle-mobile/paddle-mobile/Operators/TransposeOp.swift index 33c3ef20ff..3306aaa4e9 100644 --- a/metal/paddle-mobile/paddle-mobile/Operators/TransposeOp.swift +++ b/metal/paddle-mobile/paddle-mobile/Operators/TransposeOp.swift @@ -49,8 +49,12 @@ class TransposeOp: Operator, TransposeParam } } func delogOutput() { + print(para.input.metalTexture.toTensor(dim: (n: para.input.tensorDim[0], c: para.input.tensorDim[1], h: para.input.tensorDim[2], w: para.input.tensorDim[3])).strideArray()) - print(para.output.metalTexture.toTensor(dim: (n: 1, c: 21, h: 19, w: 19)).strideArray()) + + let originDim = para.output.tensorDim + let outputArray = para.output.metalTexture.realNHWC(dim: (n: originDim[0], h: originDim[1], w: originDim[2], c: originDim[3])) + print(outputArray.strideArray()) // let inputArray: [Float32] = para.input.metalTexture.floatArray { (ele: Float32) -> Float32 in -- GitLab