gpt4 book ai didi

swift - 应用程序在前台使用 CoreML,GPU 在前台,当应用程序转到后台时,无法切换到 CPU

转载 作者:行者123 更新时间:2023-11-30 10:39:32 49 4
gpt4 key购买 nike

我正在使用 Swift 在 iOS 应用程序中实现 CoreML。在前台时,我希望 CoreML 使用 GPU 来增强性能,但是当切换到后台时,CoreML 应该使用 CPU,因为在 iOS 中,GPU 无法在后台运行。

但是,在后台期间,GPU 会抛出权限错误,表示没有在后台使用 GPU 的权限。但在代码中,我已经注册了 CoreML,仅当应用程序处于后台时才使用 CPU,但 CoreML 类似乎没有读取参数。

尽管将 useCpuOnly 设置为 true,但 CoreML 在后台运行时仍收到错误消息

2019-07-23 12:04:21.747498+0800 Demo[351:9947] Execution of the command buffer was aborted due to an error during execution. Insufficient Permission (to submit GPU work from background) (IOAF code 6)
Error: command buffer exited with error status.
The Metal Performance Shaders operations encoded on it may not have completed.
Error:
(null)
Insufficient Permission (to submit GPU work from background) (IOAF code 6)
<AGXA9FamilyCommandBuffer: 0x106b70720>
label = <none>
device = <AGXA9Device: 0x10bdc4000>
name = Apple A9 GPU
commandQueue = <AGXA9FamilyCommandQueue: 0x106a7f670>
label = <none>
device = <AGXA9Device: 0x10bdc4000>
name = Apple A9 GPU
retainedReferences = 1
2019-07-23 12:04:21.748800+0800 Demo[351:9982] [espresso] [Espresso::handle_ex_plan] exception=Espresso exception: "Generic error": Insufficient Permission (to submit GPU work from background) (IOAF code 6); code=7 status=-1
2019-07-23 12:04:21.749744+0800 Demo[351:9982] [StarMobile] [ERROR] CoreMLClassifier:112 processClassifications(for:error:) error: Unable to classify image. : Optional(Error Domain=com.apple.vis Code=9 "Could not run network (-1: ESPRESSO_STATUS_ERROR_GENERIC)" UserInfo={NSLocalizedDescription=Could not run network (-1: ESPRESSO_STATUS_ERROR_GENERIC)})
2019-07-23 12:04:21.750350+0800 Demo[351:9982] [StarMobile] [ERROR] CoreMLClassifier:105 updateClassifications(for:as:) Failed to perform classification.
Could not run network (-1: ESPRESSO_STATUS_ERROR_GENERIC)

请帮忙!

// AppDelegate.swift
func applicationWillResignActive(_ application: UIApplication) {
Constants.APP_IS_IN_FOREGROUND = false
}
func applicationDidBecomeActive(_ application: UIApplication) {
Constants.APP_IS_IN_FOREGROUND = true
}

// Main.swift
class Main() {
func run_coreml(imageData: Data) {
do {
self.usesCpuOnly = Constants.APP_IS_IN_FOREGROUND
let configuration = MLModelConfiguration()
if self.usesCpuOnly {
configuration.computeUnits = .cpuOnly
} else {
configuration.computeUnits = .all
}
let model = try VNCoreMLModel(for:
CoreMLImageClassifier(configuration: configuration).model)

let request = VNCoreMLRequest(model: model,
completionHandler:
{ [weak self] request, error in
self?.process(for: request, error: error)
})
request.usesCPUOnly = self.usesCpuOnly

let handler = VNImageRequestHandler(data: imageData)
try handler.perform([request])
} catch {}
}

// omit implementation
func process() {}
}

最佳答案

当应用进入后台时,您需要创建一个新的 VNCoreMLModel 实例,并将配置设置为 .cpuOnly,并创建一个新的 VNCoreMLRequest 。 Core ML 不会自动从 GPU 切换到 CPU。

关于swift - 应用程序在前台使用 CoreML,GPU 在前台,当应用程序转到后台时,无法切换到 CPU,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57156468/

49 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com