Revise Swift API style
PiperOrigin-RevId: 308739030 Change-Id: I63afcf15bc7f1d7f0a1071eef6ffe082f6433591
This commit is contained in:
parent
a16d185c85
commit
c1d09d25d2
@ -22,9 +22,11 @@ public final class CoreMLDelegate: Delegate {
|
||||
public let options: Options
|
||||
|
||||
// Conformance to the `Delegate` protocol.
|
||||
public private(set) var cDelegate: CDelegate?
|
||||
public private(set) var cDelegate: CDelegate
|
||||
|
||||
/// Creates a new instance configured with the given `options`.
|
||||
/// Creates a new instance configured with the given `options`. Returns `nil` if the underlying
|
||||
/// Core ML delegate could not be created because `Options.enabledDevices` was set to
|
||||
/// `neuralEngine` but the device does not have the Neural Engine.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - options: Configurations for the delegate. The default is a new instance of
|
||||
@ -33,12 +35,10 @@ public final class CoreMLDelegate: Delegate {
|
||||
self.options = options
|
||||
var delegateOptions = TfLiteCoreMlDelegateOptions()
|
||||
delegateOptions.enabled_devices = options.enabledDevices.cEnabledDevices
|
||||
delegateOptions.max_delegated_partitions = options.maxDelegatedPartitions
|
||||
delegateOptions.min_nodes_per_partition = options.minNodesPerPartition
|
||||
cDelegate = TfLiteCoreMlDelegateCreate(&delegateOptions)
|
||||
if cDelegate == nil {
|
||||
return nil
|
||||
}
|
||||
delegateOptions.max_delegated_partitions = Int32(options.maxDelegatedPartitions)
|
||||
delegateOptions.min_nodes_per_partition = Int32(options.minNodesPerPartition)
|
||||
guard let delegate = TfLiteCoreMlDelegateCreate(&delegateOptions) else { return nil }
|
||||
cDelegate = delegate
|
||||
}
|
||||
|
||||
deinit {
|
||||
@ -46,42 +46,41 @@ public final class CoreMLDelegate: Delegate {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
extension CoreMLDelegate {
|
||||
/// A type indicating which devices the Core ML delegate should be enabled for.
|
||||
public enum EnabledDevices: Equatable, Hashable {
|
||||
/// Enables the delegate for devices with Neural Engine only.
|
||||
case neuralEngine
|
||||
/// Enables the delegate for all devices.
|
||||
case all
|
||||
|
||||
/// The C `TfLiteCoreMlDelegateEnabledDevices` for the current `EnabledDevices`.
|
||||
var cEnabledDevices: TfLiteCoreMlDelegateEnabledDevices {
|
||||
switch self {
|
||||
case .neuralEngine:
|
||||
return TfLiteCoreMlDelegateDevicesWithNeuralEngine
|
||||
case .all:
|
||||
return TfLiteCoreMlDelegateAllDevices
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Options for configuring the `CoreMLDelegate`.
|
||||
// TODO(b/143931022): Add preferred device support.
|
||||
public struct Options: Equatable, Hashable {
|
||||
/// A type determines Core ML delegate initialization on devices without Neural Engine. The
|
||||
/// default is .devicesWithNeuralEngine, where the delegate will not be created for
|
||||
/// devices that does not have Neural Engine.
|
||||
public var enabledDevices: CoreMLDelegateEnabledDevices = .devicesWithNeuralEngine
|
||||
/// Maximum number of Core ML delegates created. Each graph corresponds to one delegated node
|
||||
/// subset in the TFLite model. Set this to 0 to delegate all possible partitions.
|
||||
public var maxDelegatedPartitions: Int32 = 0;
|
||||
|
||||
// Minimum number of nodes per partition delegated with
|
||||
// Core ML delegate. Defaults to 2.
|
||||
public var minNodesPerPartition: Int32 = 2;
|
||||
/// A type indicating which devices the Core ML delegate should be enabled for. The default
|
||||
/// value is `.neuralEngine` indicating that the delegate is enabled for Neural Engine devices
|
||||
/// only.
|
||||
public var enabledDevices: EnabledDevices = .neuralEngine
|
||||
/// The maximum number of Core ML delegate partitions created. Each graph corresponds to one
|
||||
/// delegated node subset in the TFLite model. The default value is `0` indicating that all
|
||||
/// possible partitions are delegated.
|
||||
public var maxDelegatedPartitions = 0
|
||||
/// The minimum number of nodes per partition to be delegated by the Core ML delegate. The
|
||||
/// default value is `2`.
|
||||
public var minNodesPerPartition = 2
|
||||
|
||||
/// Creates a new instance with the default values.
|
||||
public init() {}
|
||||
}
|
||||
}
|
||||
|
||||
/// A type determines Core ML delegate initialization on devices without Neural Engine.
|
||||
public enum CoreMLDelegateEnabledDevices: Equatable, Hashable {
|
||||
/// Creates the delegate only for devices with Neural Engine.
|
||||
case devicesWithNeuralEngine
|
||||
/// Creates the delegate even when Neural Engine is not available.
|
||||
case allDevices
|
||||
|
||||
/// The C `TfLiteCoreMlDelegateEnabledDevices` for the current `CoreMLDelegateEnabledDevices`.
|
||||
var cEnabledDevices: TfLiteCoreMlDelegateEnabledDevices {
|
||||
switch self {
|
||||
case .devicesWithNeuralEngine:
|
||||
return TfLiteCoreMlDelegateDevicesWithNeuralEngine
|
||||
case .allDevices:
|
||||
return TfLiteCoreMlDelegateAllDevices
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -20,5 +20,5 @@ public protocol Delegate: class {
|
||||
typealias CDelegate = UnsafeMutablePointer<TfLiteDelegate>
|
||||
|
||||
/// The delegate that performs model computations.
|
||||
var cDelegate: CDelegate? { get }
|
||||
var cDelegate: CDelegate { get }
|
||||
}
|
||||
|
@ -23,7 +23,7 @@ public final class MetalDelegate: Delegate {
|
||||
public let options: Options
|
||||
|
||||
// Conformance to the `Delegate` protocol.
|
||||
public private(set) var cDelegate: CDelegate?
|
||||
public private(set) var cDelegate: CDelegate
|
||||
|
||||
/// Creates a new instance configured with the given `options`.
|
||||
///
|
||||
|
Loading…
Reference in New Issue
Block a user