/* 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 struct ConcatTestParam: TestParam { var input: [MTLTexture] var output: MTLTexture var dims: [[Int]] var axis: Int var odim: [Int] } struct ConcatMetalParam { var odim: (Int32, Int32, Int32, Int32) = (1, 1, 1, 1) var axis: Int32 = 0 var offset: Int32 = 0 var trans: (Int32, Int32, Int32, Int32) = (0, 1, 2, 3) var vdim: (Int32, Int32, Int32, Int32, Int32, Int32) = (0, 0, 0, 0, 0, 0) } class ConcatKernel: Kernel, Computable{ func encodeTest(_ cmdBuffer: MTLCommandBuffer, _ param: ConcatTestParam, _ istart: Int, _ iend: Int) { let encoder = cmdBuffer.makeComputeCommandEncoder()! var p = ConcatMetalParam.init() var odim: [Int32] = [1, 1, 1, 1] for i in 0...size, index: 0) encoder.dispatch(computePipline: pipline, outTexture: param.output) encoder.endEncoding() } func encode(_ cmdBuffer: MTLCommandBuffer, _ param: ConcatParam

, _ istart: Int, _ iend: Int) throws { guard let encoder = cmdBuffer.makeComputeCommandEncoder() else { throw PaddleMobileError.predictError(message: " encode is nil") } var p = ConcatMetalParam.init() let odim = (0..<4).map { Int32(param.output.dim[$0]) } p.odim = (odim[0], odim[1], odim[2], odim[3]) p.axis = Int32(4 - param.output.tensorDim.cout() + param.axis) for i in 0..<4 { if Int32(param.transpose[i]) == p.axis { p.axis = Int32(i) break } } for i in 0...size, index: 0) encoder.dispatch(computePipline: pipline, outTexture: param.output.metalTexture) encoder.endEncoding() } func compute(commandBuffer: MTLCommandBuffer, param: ConcatParam

) throws { let group = param.input.count / 6 let remain = param.input.count % 6 for i in 0.. 0 { try self.encode(commandBuffer, param, 6 * group, param.input.count) } } func test(cmdBuffer: MTLCommandBuffer, param: ConcatTestParam) { let group = param.input.count / 6 let remain = param.input.count % 6 for i in 0.. 0 { self.encodeTest(cmdBuffer, param, 6 * group, param.input.count) } } required init(device: MTLDevice, param: ConcatParam

) { param.output.initTexture(device: device, inTranspose: param.transpose, computePrecision: computePrecision) if computePrecision == .Float32 { super.init(device: device, inFunctionName: "concat") } else if computePrecision == .Float16 { super.init(device: device, inFunctionName: "concat_half") } else { fatalError() } } required init(device: MTLDevice, testParam: ConcatTestParam) { super.init(device: device, inFunctionName: "concat") } }