From 94e8f70dd424a002feb1b293ab98d1261027118d Mon Sep 17 00:00:00 2001 From: Pedro Cuenca Date: Mon, 20 Feb 2023 16:40:13 +0100 Subject: [PATCH] Add missing file. --- Diffusion-macOS/Capabilities.swift | 35 ++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 Diffusion-macOS/Capabilities.swift diff --git a/Diffusion-macOS/Capabilities.swift b/Diffusion-macOS/Capabilities.swift new file mode 100644 index 0000000..6d075ef --- /dev/null +++ b/Diffusion-macOS/Capabilities.swift @@ -0,0 +1,35 @@ +// +// Capabilities.swift +// Diffusion-macOS +// +// Created by Pedro Cuenca on 20/2/23. +// + +import Foundation + +let runningOnMac = true + +#if canImport(MLCompute) +import MLCompute +let _hasANE = MLCDevice.ane() != nil +#else +let _hasANE = false +#endif + +final class Capabilities { + static let hasANE = _hasANE + + // According to my tests this is a good proxy to estimate whether CPU+GPU + // or CPU+NE works better. Things may become more complicated if we + // choose all compute units. + static var performanceCores: Int = { + var ncores: Int32 = 0 + var bytes = MemoryLayout.size + + // In M1/M2 perflevel0 refers to the performance cores and perflevel1 are the efficiency cores + // In Intel there's only one performance level + let result = sysctlbyname("hw.perflevel0.physicalcpu", &ncores, &bytes, nil, 0) + guard result == 0 else { return 0 } + return Int(ncores) + }() +}