From c1d09d25d22423bf02007a1d8a974d2359556b7d Mon Sep 17 00:00:00 2001 From: Taehee Jeong Date: Mon, 27 Apr 2020 18:22:43 -0700 Subject: [PATCH] Revise Swift API style PiperOrigin-RevId: 308739030 Change-Id: I63afcf15bc7f1d7f0a1071eef6ffe082f6433591 --- .../swift/Sources/CoreMLDelegate.swift | 75 +++++++++---------- .../experimental/swift/Sources/Delegate.swift | 2 +- .../swift/Sources/MetalDelegate.swift | 2 +- 3 files changed, 39 insertions(+), 40 deletions(-) diff --git a/tensorflow/lite/experimental/swift/Sources/CoreMLDelegate.swift b/tensorflow/lite/experimental/swift/Sources/CoreMLDelegate.swift index 04976d6ce0a..9862de31e2c 100644 --- a/tensorflow/lite/experimental/swift/Sources/CoreMLDelegate.swift +++ b/tensorflow/lite/experimental/swift/Sources/CoreMLDelegate.swift @@ -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 - } - } -} diff --git a/tensorflow/lite/experimental/swift/Sources/Delegate.swift b/tensorflow/lite/experimental/swift/Sources/Delegate.swift index d4d0a3b32d4..7b73d65bf80 100644 --- a/tensorflow/lite/experimental/swift/Sources/Delegate.swift +++ b/tensorflow/lite/experimental/swift/Sources/Delegate.swift @@ -20,5 +20,5 @@ public protocol Delegate: class { typealias CDelegate = UnsafeMutablePointer /// The delegate that performs model computations. - var cDelegate: CDelegate? { get } + var cDelegate: CDelegate { get } } diff --git a/tensorflow/lite/experimental/swift/Sources/MetalDelegate.swift b/tensorflow/lite/experimental/swift/Sources/MetalDelegate.swift index 3bfa4bc048c..8fd15f303da 100644 --- a/tensorflow/lite/experimental/swift/Sources/MetalDelegate.swift +++ b/tensorflow/lite/experimental/swift/Sources/MetalDelegate.swift @@ -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`. ///