未验证 提交 9fbdede6 编写于 作者: Y Yanzhan Yang 提交者: GitHub

1. lazy load for mpsimage. (#1666)

上级 bfca5725
......@@ -147,19 +147,28 @@ class ConvAddReluKernel<P: PrecisionProtocol>: Kernel, Computable {
}
}
var inputImage: AnyObject?
var outputImage: AnyObject?
func compute(commandBuffer: MTLCommandBuffer, param: ConvAddReluParam<P>) throws {
if #available(iOS 10.0, *) {
if let conv = mpsConvOp as? MPSCNNConvolution {
let inputImage = MPSImage.init(texture: param.input.metalTexture, featureChannels: param.input.tensorDim[1])
let outputImage = MPSImage.init(texture: param.output.metalTexture, featureChannels: param.output.tensorDim[1])
conv.encode(commandBuffer: commandBuffer, sourceImage: inputImage, destinationImage: outputImage)
if #available(iOS 11.3, *) {
if let add = mpsAddOp as? MPSCNNAdd, let y = param.y {
let biasImage = MPSImage.init(texture: y.metalTexture, featureChannels: y.tensorDim[1])
add.encode(commandBuffer: commandBuffer, primaryImage: outputImage, secondaryImage: biasImage, destinationImage: outputImage)
}
if let relu = mpsReluOp as? MPSCNNNeuronReLU {
relu.encode(commandBuffer: commandBuffer, sourceImage: outputImage, destinationImage: outputImage)
if inputImage == nil {
inputImage = MPSImage.init(texture: param.input.metalTexture, featureChannels: param.input.tensorDim[1])
}
if outputImage == nil {
outputImage = MPSImage.init(texture: param.output.metalTexture, featureChannels: param.output.tensorDim[1])
}
if let inputImage = inputImage as? MPSImage, let outputImage = outputImage as? MPSImage {
conv.encode(commandBuffer: commandBuffer, sourceImage: inputImage, destinationImage: outputImage)
if #available(iOS 11.3, *) {
if let add = mpsAddOp as? MPSCNNAdd, let y = param.y {
let biasImage = MPSImage.init(texture: y.metalTexture, featureChannels: y.tensorDim[1])
add.encode(commandBuffer: commandBuffer, primaryImage: outputImage, secondaryImage: biasImage, destinationImage: outputImage)
}
if let relu = mpsReluOp as? MPSCNNNeuronReLU {
relu.encode(commandBuffer: commandBuffer, sourceImage: outputImage, destinationImage: outputImage)
}
}
}
return
......
......@@ -23,6 +23,8 @@ struct ScaleMetalParam {
class ScaleOpKernel<P: PrecisionProtocol>: Kernel, Computable{
var metalParam: ScaleMetalParam
var mpsScaleOp: AnyObject?
var inputImage: AnyObject?
var outputImage: AnyObject?
required init(device: MTLDevice, param: ScaleParam<P>, initContext: InitContext) throws {
do {
......@@ -67,9 +69,15 @@ class ScaleOpKernel<P: PrecisionProtocol>: Kernel, Computable{
func compute(commandBuffer: MTLCommandBuffer, param: ScaleParam<P>) throws {
if #available(iOS 10.0, *), let mpsScaleOp = mpsScaleOp as? MPSCNNNeuronLinear {
let inputImage = MPSImage.init(texture: param.input.metalTexture, featureChannels: param.input.tensorDim[1])
let outputImage = MPSImage.init(texture: param.output.metalTexture, featureChannels: param.output.tensorDim[1])
mpsScaleOp.encode(commandBuffer: commandBuffer, sourceImage: inputImage, destinationImage: outputImage)
if inputImage == nil {
inputImage = MPSImage.init(texture: param.input.metalTexture, featureChannels: param.input.tensorDim[1])
}
if outputImage == nil {
outputImage = MPSImage.init(texture: param.output.metalTexture, featureChannels: param.output.tensorDim[1])
}
if let inputImage = inputImage as? MPSImage, let outputImage = outputImage as? MPSImage {
mpsScaleOp.encode(commandBuffer: commandBuffer, sourceImage: inputImage, destinationImage: outputImage)
}
return
}
guard let encoder = commandBuffer.makeComputeCommandEncoder() else {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册