You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CoreML无法支持并发(多线程)?求技术解决方案

CoreML MLModel 并行预测被强制串行运行的问题

我尝试让CoreML的MLModel实现并行预测,但底层似乎被Apple强制以串行方式运行,已创建公开仓库复现该问题的PoC。

已尝试方案

  • 每次重新创建MLModel实例,而非使用全局实例
  • 仅配置.cpuAndGPU计算单元

需求目标

希望利用多线程同时处理大量视频帧(假设CPU、RAM资源足够),实现比串行处理更快的速度。

复现代码

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let parallelTaskCount = 3
        
        for i in 0...parallelTaskCount - 1 {
            DispatchQueue.global(qos: .userInteractive).async {
                let image = UIImage(named: "image.jpg")!
                self.runPrediction(index: i, image: image)
            }
        }
    }

    
    func runPrediction(index: Int, image: UIImage) {
        let conf = MLModelConfiguration()
        conf.computeUnits = .cpuAndGPU
        conf.allowLowPrecisionAccumulationOnGPU = true
        
        let myModel = try! MyModel(configuration: conf)
        let myModelInput = try! MyModelInput(LR_inputWith: image.cgImage!)
        // Prediction
        let predicition = try! myModel.prediction(input: myModelInput)
        print("finished proccessing \(index)")
    }
    
}

恳请各位提供技术帮助。


内容的提问来源于stack exchange,提问作者Roi Mulia

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 18:31:07