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
|
public let options: Options
|
||||||
|
|
||||||
// Conformance to the `Delegate` protocol.
|
// 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:
|
/// - Parameters:
|
||||||
/// - options: Configurations for the delegate. The default is a new instance of
|
/// - options: Configurations for the delegate. The default is a new instance of
|
||||||
@ -33,12 +35,10 @@ public final class CoreMLDelegate: Delegate {
|
|||||||
self.options = options
|
self.options = options
|
||||||
var delegateOptions = TfLiteCoreMlDelegateOptions()
|
var delegateOptions = TfLiteCoreMlDelegateOptions()
|
||||||
delegateOptions.enabled_devices = options.enabledDevices.cEnabledDevices
|
delegateOptions.enabled_devices = options.enabledDevices.cEnabledDevices
|
||||||
delegateOptions.max_delegated_partitions = options.maxDelegatedPartitions
|
delegateOptions.max_delegated_partitions = Int32(options.maxDelegatedPartitions)
|
||||||
delegateOptions.min_nodes_per_partition = options.minNodesPerPartition
|
delegateOptions.min_nodes_per_partition = Int32(options.minNodesPerPartition)
|
||||||
cDelegate = TfLiteCoreMlDelegateCreate(&delegateOptions)
|
guard let delegate = TfLiteCoreMlDelegateCreate(&delegateOptions) else { return nil }
|
||||||
if cDelegate == nil {
|
cDelegate = delegate
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
deinit {
|
deinit {
|
||||||
@ -46,42 +46,41 @@ public final class CoreMLDelegate: Delegate {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
extension CoreMLDelegate {
|
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`.
|
/// Options for configuring the `CoreMLDelegate`.
|
||||||
// TODO(b/143931022): Add preferred device support.
|
// TODO(b/143931022): Add preferred device support.
|
||||||
public struct Options: Equatable, Hashable {
|
public struct Options: Equatable, Hashable {
|
||||||
/// A type determines Core ML delegate initialization on devices without Neural Engine. The
|
/// A type indicating which devices the Core ML delegate should be enabled for. The default
|
||||||
/// default is .devicesWithNeuralEngine, where the delegate will not be created for
|
/// value is `.neuralEngine` indicating that the delegate is enabled for Neural Engine devices
|
||||||
/// devices that does not have Neural Engine.
|
/// only.
|
||||||
public var enabledDevices: CoreMLDelegateEnabledDevices = .devicesWithNeuralEngine
|
public var enabledDevices: EnabledDevices = .neuralEngine
|
||||||
/// Maximum number of Core ML delegates created. Each graph corresponds to one delegated node
|
/// The maximum number of Core ML delegate partitions created. Each graph corresponds to one
|
||||||
/// subset in the TFLite model. Set this to 0 to delegate all possible partitions.
|
/// delegated node subset in the TFLite model. The default value is `0` indicating that all
|
||||||
public var maxDelegatedPartitions: Int32 = 0;
|
/// possible partitions are delegated.
|
||||||
|
public var maxDelegatedPartitions = 0
|
||||||
// Minimum number of nodes per partition delegated with
|
/// The minimum number of nodes per partition to be delegated by the Core ML delegate. The
|
||||||
// Core ML delegate. Defaults to 2.
|
/// default value is `2`.
|
||||||
public var minNodesPerPartition: Int32 = 2;
|
public var minNodesPerPartition = 2
|
||||||
|
|
||||||
/// Creates a new instance with the default values.
|
/// Creates a new instance with the default values.
|
||||||
public init() {}
|
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>
|
typealias CDelegate = UnsafeMutablePointer<TfLiteDelegate>
|
||||||
|
|
||||||
/// The delegate that performs model computations.
|
/// 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
|
public let options: Options
|
||||||
|
|
||||||
// Conformance to the `Delegate` protocol.
|
// 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`.
|
||||||
///
|
///
|
||||||
|
Loading…
Reference in New Issue
Block a user