ConcatKernel.swift 4.4 KB
Newer Older
L
liuruilong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* 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

D
concat  
dolphin8 已提交
17
struct ConcatTestParam: TestParam {
D
dolphin8 已提交
18 19 20 21 22
  var input: [MTLTexture]
  var output: MTLTexture
  var dims: [[Int]]
  var axis: Int
  var odim: [Int]
D
concat  
dolphin8 已提交
23 24 25
}

struct ConcatMetalParam {
D
dolphin8 已提交
26 27 28 29 30
  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)
D
concat  
dolphin8 已提交
31 32
}

L
liuruilong 已提交
33
class ConcatKernel<P: PrecisionType>: Kernel, Computable{
D
concat  
dolphin8 已提交
34 35 36 37 38 39
  var v = "normal"
  var pm = ConcatMetalParam.init()
  func compute(commandBuffer: MTLCommandBuffer, param: ConcatParam<P>) throws {
    
    guard let encoder = commandBuffer.makeComputeCommandEncoder() else {
      throw PaddleMobileError.predictError(message: " encode is nil")
L
liuruilong 已提交
40
    }
D
concat  
dolphin8 已提交
41 42 43
    let num = param.input.count
    for i in 0..<num {
      encoder.setTexture(param.input[i].metalTexture, index: i)
D
concat  
dolphin8 已提交
44
    }
D
concat  
dolphin8 已提交
45 46 47
    encoder.setTexture(param.output.metalTexture, index: num)
    if v == "normal" {
      encoder.setTexture(param.output.metalTexture, index: num + 1)
D
concat  
dolphin8 已提交
48
    }
D
concat  
dolphin8 已提交
49 50
    encoder.setBytes(&pm, length: MemoryLayout<ConcatMetalParam>.size, index: 0)
    encoder.dispatch(computePipline: pipline, outTexture: param.output.metalTexture)
D
dolphin8 已提交
51 52
    encoder.endEncoding()
  }
D
concat  
dolphin8 已提交
53 54 55 56 57 58 59

  required init(device: MTLDevice, param: ConcatParam<P>) {
    param.output.initTexture(device: device, inTranspose: param.transpose, computePrecision: computePrecision)
    let orank = param.output.tensorDim.cout()
    let num = param.input.count
    assert(num <= 6)
    var axis = 4 - param.output.tensorDim.cout() + param.axis
D
dolphin8 已提交
60
    for i in 0..<4 {
D
concat  
dolphin8 已提交
61 62
      if param.transpose[i] == axis {
        axis = i
D
dolphin8 已提交
63 64 65
        break
      }
    }
D
concat  
dolphin8 已提交
66 67 68 69 70 71
    pm.axis = Int32(axis)
    pm.odim = (Int32(param.output.dim[0]), Int32(param.output.dim[1]), Int32(param.output.dim[2]), Int32(param.output.dim[3]))
    pm.trans = (Int32(param.output.transpose[0]), Int32(param.output.transpose[1]), Int32(param.output.transpose[2]), Int32(param.output.transpose[3]))
    var vdim: [Int] = [0, 0, 0, 0, 0, 0]
    for i in 0..<num {
      vdim[i] = param.input[i].dim[axis]
D
dolphin8 已提交
72
    }
D
concat  
dolphin8 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
    if orank == 4 {
      if axis == 1 {
        v = "y"
      } else if axis == 2 {
        v = "x"
      } else {
        if (param.output.dim[0] == 1) && axis == 3 {
          var vz = true
          for i in 0..<num {
            if vdim[i] % 4 != 0 {
              vz = false
              break
            }
          }
          if vz {
            v = "z"
            for i in 0..<num {
              vdim[i] = vdim[i] / 4
            }
          }
        }
      }
    } else if orank == 3 {
      if axis == 2 {
        v = "y"
      } else if axis == 3 {
        v = "x"
      } else if axis == 1 {
        var vz = true
        for i in 0..<num {
          if vdim[i] % 4 != 0 {
            vz = false
            break
          }
        }
        if vz {
          v = "z"
          for i in 0..<num {
            vdim[i] = vdim[i] / 4
          }
        }
      }
    } else {
      if axis == 2 {
        v = "y"
      } else if axis == 3 {
        var vx = true
        for i in 0..<num {
          if vdim[i] % 4 != 0 {
            vx = false
            break
          }
        }
        if vx {
          v = "x"
          for i in 0..<num {
            vdim[i] = vdim[i] / 4
          }
        }
      }
L
liuruilong 已提交
133
    }
D
concat  
dolphin8 已提交
134
    pm.vdim = (Int32(vdim[0]), Int32(vdim[1]), Int32(vdim[2]), Int32(vdim[3]), Int32(vdim[4]), Int32(vdim[5]))
L
liuruilong 已提交
135
    if computePrecision == .Float32 {
D
concat  
dolphin8 已提交
136
      super.init(device: device, inFunctionName: "concat_\(orank)_\(num)_\(v)_float")
L
liuruilong 已提交
137
    } else if computePrecision == .Float16 {
D
concat  
dolphin8 已提交
138
      super.init(device: device, inFunctionName: "concat_\(orank)_\(num)_\(v)_half")
L
liuruilong 已提交
139 140 141
    } else {
      fatalError()
    }
D
dolphin8 已提交
142 143 144 145 146
  }
  
  required init(device: MTLDevice, testParam: ConcatTestParam) {
    super.init(device: device, inFunctionName: "concat")
  }
L
liuruilong 已提交
147
}