Change obsolete C++ API to C API in Core ML delegate doc

PiperOrigin-RevId: 332186511
Change-Id: I285c9dbd3ebb9fe75fd3d2d5b6f6b10bac0e72be
This commit is contained in:
Taehee Jeong 2020-09-17 01:53:29 -07:00 committed by TensorFlower Gardener
parent eccfed47aa
commit 81953ff3f0

View File

@ -69,35 +69,42 @@ The Core ML delegate uses C API for Objective-C codes.
#### Step 1. Include `coreml_delegate.h`.
```objectivec
```c
#include "tensorflow/lite/experimental/delegates/coreml/coreml_delegate.h"
```
#### Step 2. Create a delegate and initialize a TensorFlow Lite Interpreter
After initializing the interpreter, call `interpreter->ModifyGraphWithDelegate`
with initialized Core ML delegate to apply the delegate.
After initializing the interpreter options, call
`TfLiteInterpreterOptionsAddDelegate` with initialized Core ML delegate to apply
the delegate. Then initialize the interpreter with the created option.
```objectivec
// initializer interpreter with model.
tflite::InterpreterBuilder(*model, resolver)(&interpreter);
```c
// Initialize interpreter with model
TfLiteModel* model = TfLiteModelCreateFromFile(model_path);
// Add following section to use the Core ML delegate.
TfLiteCoreMlDelegateOptions options = {};
delegate = TfLiteCoreMlDelegateCreate(&options);
if (delegate != nullptr) {
interpreter->ModifyGraphWithDelegate(delegate);
}
// ...
// Initialize interpreter with Core ML delegate
TfLiteInterpreterOptions* options = TfLiteInterpreterOptionsCreate();
TfLiteDelegate* delegate = TfLiteCoreMlDelegateCreate(NULL); // default config
TfLiteInterpreterOptionsAddDelegate(options, delegate);
TfLiteInterpreterOptionsDelete(options);
TfLiteInterpreter* interpreter = TfLiteInterpreterCreate(model, options);
TfLiteInterpreterAllocateTensors(interpreter);
// Run inference ...
```
#### Step 3. Dispose the delegate when it is no longer used.
#### Step 3. Dispose resources when it is no longer used.
Add this code to the section where you dispose of the delegate (e.g. `dealloc`
of class).
```objectivec
```c
TfLiteInterpreterDelete(interpreter);
TfLiteCoreMlDelegateDelete(delegate);
TfLiteModelDelete(model);
```
## Best practices
@ -116,17 +123,17 @@ pass `TfLiteCoreMlDelegateAllDevices`. Following example shows how to do this:
var options = CoreMLDelegate.Options()
options.enabledDevices = .all
let coreMLDelegate = CoreMLDelegate(options: options)!
let interpreter: try Interpreter(modelPath: modelPath,
delegates: [coreMLDelegate])
let interpreter = try Interpreter(modelPath: modelPath,
delegates: [coreMLDelegate])
```
#### Objective-C
```objectivec
```c
TfLiteCoreMlDelegateOptions options;
options.enabled_devices = TfLiteCoreMlDelegateAllDevices;
delegate = TfLiteCoreMlDelegateCreate(&options);
interpreter->ModifyGraphWithDelegate(delegate);
TfLiteDelegate* delegate = TfLiteCoreMlDelegateCreate(&options);
// Initialize interpreter with delegate
```
### Using Metal(GPU) delegate as a fallback.
@ -149,14 +156,14 @@ let interpreter = try Interpreter(modelPath: modelPath,
#### Objective-C
```objectivec
```c
TfLiteCoreMlDelegateOptions options = {};
delegate = TfLiteCoreMlDelegateCreate(&options);
if (delegate == nullptr) {
if (delegate == NULL) {
// Add Metal delegate options if necessary
delegate = TFLGpuDelegateCreate(nullptr);
delegate = TFLGpuDelegateCreate(NULL);
}
interpreter->ModifyGraphWithDelegate(delegate);
// Initialize interpreter with delegate
```
The delegate creation logic reads device's machine id (e.g. iPhone11,1) to