The Vision request does not work in simulator with Error "Could not create inference context"

When I use VNGenerateForegroundInstanceMaskRequest to generate the mask in the simulator by SwiftUI, there is an error "Could not create inference context".

Then I add the code to make the vision by CPU:

let request = VNGenerateForegroundInstanceMaskRequest()
let handler = VNImageRequestHandler(ciImage: inputImage)
#if targetEnvironment(simulator)
    if #available(iOS 18.0, *) {
      let allDevices = MLComputeDevice.allComputeDevices

      for device in allDevices {
        if(device.description.contains("MLCPUComputeDevice")){
          request.setComputeDevice(.some(device), for: .main)
          break
        }
      }

    } else {
      // Fallback on earlier versions
      request.usesCPUOnly = true
    }
#endif

do {
            try handler.perform([request])
            
            if let result = request.results?.first {
                let mask = try result.generateScaledMaskForImage(forInstances: result.allInstances, from: handler)
                return CIImage(cvPixelBuffer: mask)
            }
        } catch {
            print(error)
        }

Even I force the simulator to run the code by CPU, but it still have the error: "Could not create inference context"

Unfortunately VNGenerateForegroundInstanceMaskRequest is not supported on CPU and will therefore not run in the simulator.

usesCPUOnly was deprecated in iOS 17, so lowering the availability check to iOS 17 will ensure the CPU is forced correctly. This will reveal the true error message: "unsupported compute device <MLCPUComputeDevice>".

if #available(iOS 17.0, *) {
  let allDevices = MLComputeDevice.allComputeDevices
  for device in allDevices {
      if case .cpu = device {
          request.setComputeDevice(device, for: .main)
          break
      }
  }
} else {
  // Fallback on earlier versions
  request.usesCPUOnly = true
}

so you mean I can not run the code in iOS18 simulator, right?

The Vision request does not work in simulator with Error "Could not create inference context"
 
 
Q