Skip to content

Commit 6660811

Browse files
committed
Update docs and add examples for Compute Plan and In-memory model usage.
1 parent 6ada7fc commit 6660811

File tree

392 files changed

+4819
-6298
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

392 files changed

+4819
-6298
lines changed

Diff for: docs-guides/.buildinfo

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# Sphinx build info version 1
22
# This file hashes the configuration used when building these files. When it is not found, a full rebuild will be done.
3-
config: a0cc5a057fb0f76f4409d50e75aca1b6
3+
config: 8b12fed34ce7966d0db3a29cbeb3e84c
44
tags: 645f666f9bcd5a90fca523b33c5a78b7

Diff for: docs-guides/_sources/source/mlmodel-utilities.md

+84-2
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ An example how to update the output data types:
219219

220220
```python
221221
from coremltools.models.model import MLModel
222-
from coremltools.utils import change_array_output_type
222+
from coremltools.utils import change_input_output_tensor_type
223223
from coremltools.proto.FeatureTypes_pb2 import ArrayFeatureType
224224

225225
model = MLModel("my_model.mlpackage")
@@ -234,7 +234,7 @@ updated_model.save("my_updated_model.mlpackage")
234234
Another example is showing how to update data types of all the function inputs:
235235
```python
236236
from coremltools.models.model import MLModel
237-
from coremltools.utils import change_array_output_type
237+
from coremltools.utils import change_input_output_tensor_type
238238
from coremltools.proto.FeatureTypes_pb2 import ArrayFeatureType
239239

240240
model = MLModel("my_model.mlpackage")
@@ -257,3 +257,85 @@ Optional arguments:
257257
Special values for `input_names` and `output_names` arguments:
258258
* an empty list means nothing will be modified (default for `input_names`)
259259
* a list containing `"*"` string means all relevant inputs/outputs will be modified (those that will match the `from_type` type)
260+
261+
## Compute Plan
262+
263+
In certain situations, you may want to evaluate the computational needs of a Core ML model before deploying it.
264+
The `MLComputePlan` class is designed for this purpose, allowing you to get insights into the resources and costs
265+
associated with using the model.
266+
267+
Here’s what you can do with `MLComputePlan`:
268+
- Model Structure: Examine the model structure.
269+
- Compute Device Usage: Get insights into the compute devices that would be used for executing an ML Program operation/ NeuralNetwork layer.
270+
- Estimated Cost: Get the estimated cost of executing an ML Program operation.
271+
272+
An example on how to use `MLComputePlan` to get the estimated cost and compute device usages for the operations in an ML Program:
273+
274+
```python
275+
import coremltools as ct
276+
# Path to the compiled ML Program model.
277+
compiled_model_path = "my_model.mlmodelc"
278+
# Load the compute plan of a model.
279+
compute_plan = ct.models.MLComputePlan.compute_plan.load_from_path(
280+
path=compiled_model_path,
281+
compute_units=ct.ComputeUnits.ALL,
282+
)
283+
# Get the model structure.
284+
program = compute_plan.model_structure.program
285+
mainFunction = program.functions["main"]
286+
for operation in mainFunction.block.operations:
287+
# Get the compute device usage for the operation.
288+
compute_device_usage = (
289+
compute_plan.get_compute_device_usage_for_mlprogram_operation(operation)
290+
)
291+
# Get the estimated cost of executing the operation.
292+
estimated_cost = compute_plan.get_estimated_cost_for_mlprogram_operation(operation)
293+
```
294+
295+
## In-memory Model
296+
If you are using an in-memory model in your application, you can easily test the workflow with `MLModelAsset`. The `MLModelAsset` class includes
297+
the `MLModelAsset.from_memory` API, which enables you to load a model directly from the model's in-memory specification data. Once loaded, you
298+
can use the model to make predictions.
299+
300+
An example on how to use `MLModelAsset` to load an `MLCompiledModel` from in-memory specification data:
301+
302+
```python
303+
import coremltools as ct
304+
# Path to the model.
305+
model = MLModel("my_model.model")
306+
model_spec = model.get_spec()
307+
spec_data = model_spec.SerializeToString()
308+
asset = ct.models.model.MLModelAsset.from_memory(spec_data=spec_data)
309+
compiled_model = ct.models.CompiledMLModel.from_asset(asset=asset)
310+
result = compiled_model.predict(
311+
{
312+
"x": np.array([1.0]),
313+
"y": np.array([2.0]),
314+
}
315+
)
316+
```
317+
318+
Another example on how to use `MLModelAsset` to load a MLCompiledModel from in-memory specification data where the specification has external blob file references :
319+
320+
321+
```python
322+
import coremltools as ct
323+
# Path to the model.
324+
mlmodel = MLModel("my_model.mlpackage")
325+
weight_file_path = mlmodel.weights_dir + "/weight.bin"
326+
with open(weight_file_path, "rb") as file:
327+
weights_data = file.read()
328+
model_spec = model.get_spec()
329+
spec_data = model_spec.SerializeToString()
330+
# Provide the weights data as `blob_mapping`.
331+
asset = ct.models.model.MLModelAsset.from_memory(
332+
spec_data=spec_data, blob_mapping={"weights/weight.bin": weights_data}
333+
)
334+
compiled_model = ct.models.CompiledMLModel.from_asset(asset=asset)
335+
result = compiled_model.predict(
336+
{
337+
"x": np.array([1.0]),
338+
"y": np.array([2.0]),
339+
}
340+
)
341+
```

Diff for: docs-guides/_static/documentation_options.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const DOCUMENTATION_OPTIONS = {
2-
VERSION: '7.0',
2+
VERSION: '8.1',
33
LANGUAGE: 'en',
44
COLLAPSE_INDEX: false,
55
BUILDER: 'html',

Diff for: docs-guides/genindex.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
<link rel="preload" as="script" href="_static/scripts/bootstrap.js?digest=26a4bc78f4c0ddb94549" />
4141
<link rel="preload" as="script" href="_static/scripts/pydata-sphinx-theme.js?digest=26a4bc78f4c0ddb94549" />
4242

43-
<script src="_static/documentation_options.js?v=c1ce5b23"></script>
43+
<script src="_static/documentation_options.js?v=6ab2ec1d"></script>
4444
<script src="_static/doctools.js?v=9a2dae69"></script>
4545
<script src="_static/sphinx_highlight.js?v=dc90522c"></script>
4646
<script src="_static/clipboard.min.js?v=a7894cd8"></script>
@@ -908,7 +908,7 @@ <h2 id="X">X</h2>
908908

909909
<p class="copyright">
910910

911-
© Copyright 2023, Apple Inc.
911+
© Copyright 2024, Apple Inc.
912912
<br/>
913913

914914
</p>

Diff for: docs-guides/index.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
<link rel="preload" as="script" href="_static/scripts/bootstrap.js?digest=26a4bc78f4c0ddb94549" />
4242
<link rel="preload" as="script" href="_static/scripts/pydata-sphinx-theme.js?digest=26a4bc78f4c0ddb94549" />
4343

44-
<script src="_static/documentation_options.js?v=c1ce5b23"></script>
44+
<script src="_static/documentation_options.js?v=6ab2ec1d"></script>
4545
<script src="_static/doctools.js?v=9a2dae69"></script>
4646
<script src="_static/sphinx_highlight.js?v=dc90522c"></script>
4747
<script src="_static/clipboard.min.js?v=a7894cd8"></script>
@@ -532,7 +532,7 @@ <h1>Core ML Tools<a class="headerlink" href="#core-ml-tools" title="Link to this
532532

533533
<p class="copyright">
534534

535-
© Copyright 2023, Apple Inc.
535+
© Copyright 2024, Apple Inc.
536536
<br/>
537537

538538
</p>

Diff for: docs-guides/search.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
<link rel="preload" as="script" href="_static/scripts/bootstrap.js?digest=26a4bc78f4c0ddb94549" />
4040
<link rel="preload" as="script" href="_static/scripts/pydata-sphinx-theme.js?digest=26a4bc78f4c0ddb94549" />
4141

42-
<script src="_static/documentation_options.js?v=c1ce5b23"></script>
42+
<script src="_static/documentation_options.js?v=6ab2ec1d"></script>
4343
<script src="_static/doctools.js?v=9a2dae69"></script>
4444
<script src="_static/sphinx_highlight.js?v=dc90522c"></script>
4545
<script src="_static/clipboard.min.js?v=a7894cd8"></script>
@@ -418,7 +418,7 @@ <h1>Search</h1>
418418

419419
<p class="copyright">
420420

421-
© Copyright 2023, Apple Inc.
421+
© Copyright 2024, Apple Inc.
422422
<br/>
423423

424424
</p>

Diff for: docs-guides/searchindex.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: docs-guides/source/classifiers.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
<link rel="preload" as="script" href="../_static/scripts/bootstrap.js?digest=26a4bc78f4c0ddb94549" />
4242
<link rel="preload" as="script" href="../_static/scripts/pydata-sphinx-theme.js?digest=26a4bc78f4c0ddb94549" />
4343

44-
<script src="../_static/documentation_options.js?v=c1ce5b23"></script>
44+
<script src="../_static/documentation_options.js?v=6ab2ec1d"></script>
4545
<script src="../_static/doctools.js?v=9a2dae69"></script>
4646
<script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
4747
<script src="../_static/clipboard.min.js?v=a7894cd8"></script>
@@ -562,7 +562,7 @@ <h2>Produce a Classifier Model<a class="headerlink" href="#produce-a-classifier-
562562

563563
<p class="copyright">
564564

565-
© Copyright 2023, Apple Inc.
565+
© Copyright 2024, Apple Inc.
566566
<br/>
567567

568568
</p>

Diff for: docs-guides/source/comparing-ml-programs-and-neural-networks.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
<link rel="preload" as="script" href="../_static/scripts/bootstrap.js?digest=26a4bc78f4c0ddb94549" />
4242
<link rel="preload" as="script" href="../_static/scripts/pydata-sphinx-theme.js?digest=26a4bc78f4c0ddb94549" />
4343

44-
<script src="../_static/documentation_options.js?v=c1ce5b23"></script>
44+
<script src="../_static/documentation_options.js?v=6ab2ec1d"></script>
4545
<script src="../_static/doctools.js?v=9a2dae69"></script>
4646
<script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
4747
<script src="../_static/clipboard.min.js?v=a7894cd8"></script>
@@ -604,7 +604,7 @@ <h2>Differences in Detail<a class="headerlink" href="#differences-in-detail" tit
604604

605605
<p class="copyright">
606606

607-
© Copyright 2023, Apple Inc.
607+
© Copyright 2024, Apple Inc.
608608
<br/>
609609

610610
</p>

Diff for: docs-guides/source/composite-operators.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
<link rel="preload" as="script" href="../_static/scripts/bootstrap.js?digest=26a4bc78f4c0ddb94549" />
4242
<link rel="preload" as="script" href="../_static/scripts/pydata-sphinx-theme.js?digest=26a4bc78f4c0ddb94549" />
4343

44-
<script src="../_static/documentation_options.js?v=c1ce5b23"></script>
44+
<script src="../_static/documentation_options.js?v=6ab2ec1d"></script>
4545
<script src="../_static/doctools.js?v=9a2dae69"></script>
4646
<script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
4747
<script src="../_static/clipboard.min.js?v=a7894cd8"></script>
@@ -617,7 +617,7 @@ <h2>More Examples<a class="headerlink" href="#more-examples" title="Link to this
617617

618618
<p class="copyright">
619619

620-
© Copyright 2023, Apple Inc.
620+
© Copyright 2024, Apple Inc.
621621
<br/>
622622

623623
</p>

Diff for: docs-guides/source/conversion-options.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
<link rel="preload" as="script" href="../_static/scripts/bootstrap.js?digest=26a4bc78f4c0ddb94549" />
4242
<link rel="preload" as="script" href="../_static/scripts/pydata-sphinx-theme.js?digest=26a4bc78f4c0ddb94549" />
4343

44-
<script src="../_static/documentation_options.js?v=c1ce5b23"></script>
44+
<script src="../_static/documentation_options.js?v=6ab2ec1d"></script>
4545
<script src="../_static/doctools.js?v=9a2dae69"></script>
4646
<script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
4747
<script src="../_static/clipboard.min.js?v=a7894cd8"></script>
@@ -485,7 +485,7 @@ <h1>Conversion Options<a class="headerlink" href="#conversion-options" title="Li
485485

486486
<p class="copyright">
487487

488-
© Copyright 2023, Apple Inc.
488+
© Copyright 2024, Apple Inc.
489489
<br/>
490490

491491
</p>

Diff for: docs-guides/source/convert-a-pytorch-segmentation-model.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
<link rel="preload" as="script" href="../_static/scripts/bootstrap.js?digest=26a4bc78f4c0ddb94549" />
4242
<link rel="preload" as="script" href="../_static/scripts/pydata-sphinx-theme.js?digest=26a4bc78f4c0ddb94549" />
4343

44-
<script src="../_static/documentation_options.js?v=c1ce5b23"></script>
44+
<script src="../_static/documentation_options.js?v=6ab2ec1d"></script>
4545
<script src="../_static/doctools.js?v=9a2dae69"></script>
4646
<script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
4747
<script src="../_static/clipboard.min.js?v=a7894cd8"></script>
@@ -727,7 +727,7 @@ <h2>Convert the Model<a class="headerlink" href="#convert-the-model" title="Link
727727

728728
<p class="copyright">
729729

730-
© Copyright 2023, Apple Inc.
730+
© Copyright 2024, Apple Inc.
731731
<br/>
732732

733733
</p>

Diff for: docs-guides/source/convert-a-tensorflow-1-deepspeech-model.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
<link rel="preload" as="script" href="../_static/scripts/bootstrap.js?digest=26a4bc78f4c0ddb94549" />
4242
<link rel="preload" as="script" href="../_static/scripts/pydata-sphinx-theme.js?digest=26a4bc78f4c0ddb94549" />
4343

44-
<script src="../_static/documentation_options.js?v=c1ce5b23"></script>
44+
<script src="../_static/documentation_options.js?v=6ab2ec1d"></script>
4545
<script src="../_static/doctools.js?v=9a2dae69"></script>
4646
<script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
4747
<script src="../_static/clipboard.min.js?v=a7894cd8"></script>
@@ -675,7 +675,7 @@ <h2>Convert a Dynamic Model to a Static One<a class="headerlink" href="#convert-
675675

676676
<p class="copyright">
677677

678-
© Copyright 2023, Apple Inc.
678+
© Copyright 2024, Apple Inc.
679679
<br/>
680680

681681
</p>

Diff for: docs-guides/source/convert-a-tensorflow-1-image-classifier.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
<link rel="preload" as="script" href="../_static/scripts/bootstrap.js?digest=26a4bc78f4c0ddb94549" />
4242
<link rel="preload" as="script" href="../_static/scripts/pydata-sphinx-theme.js?digest=26a4bc78f4c0ddb94549" />
4343

44-
<script src="../_static/documentation_options.js?v=c1ce5b23"></script>
44+
<script src="../_static/documentation_options.js?v=6ab2ec1d"></script>
4545
<script src="../_static/doctools.js?v=9a2dae69"></script>
4646
<script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
4747
<script src="../_static/clipboard.min.js?v=a7894cd8"></script>
@@ -745,7 +745,7 @@ <h2>Preprocess the Image Before Converting<a class="headerlink" href="#preproces
745745

746746
<p class="copyright">
747747

748-
© Copyright 2023, Apple Inc.
748+
© Copyright 2024, Apple Inc.
749749
<br/>
750750

751751
</p>

Diff for: docs-guides/source/convert-a-torchvision-model-from-pytorch.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
<link rel="preload" as="script" href="../_static/scripts/bootstrap.js?digest=26a4bc78f4c0ddb94549" />
4242
<link rel="preload" as="script" href="../_static/scripts/pydata-sphinx-theme.js?digest=26a4bc78f4c0ddb94549" />
4343

44-
<script src="../_static/documentation_options.js?v=c1ce5b23"></script>
44+
<script src="../_static/documentation_options.js?v=6ab2ec1d"></script>
4545
<script src="../_static/doctools.js?v=9a2dae69"></script>
4646
<script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
4747
<script src="../_static/clipboard.min.js?v=a7894cd8"></script>
@@ -776,7 +776,7 @@ <h3>Make a Prediction with Core ML and Print Outputs<a class="headerlink" href="
776776

777777
<p class="copyright">
778778

779-
© Copyright 2023, Apple Inc.
779+
© Copyright 2024, Apple Inc.
780780
<br/>
781781

782782
</p>

Diff for: docs-guides/source/convert-learning-models.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
<link rel="preload" as="script" href="../_static/scripts/bootstrap.js?digest=26a4bc78f4c0ddb94549" />
4242
<link rel="preload" as="script" href="../_static/scripts/pydata-sphinx-theme.js?digest=26a4bc78f4c0ddb94549" />
4343

44-
<script src="../_static/documentation_options.js?v=c1ce5b23"></script>
44+
<script src="../_static/documentation_options.js?v=6ab2ec1d"></script>
4545
<script src="../_static/doctools.js?v=9a2dae69"></script>
4646
<script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
4747
<script src="../_static/clipboard.min.js?v=a7894cd8"></script>
@@ -482,7 +482,7 @@ <h1>Converting Deep Learning Models<a class="headerlink" href="#converting-deep-
482482

483483
<p class="copyright">
484484

485-
© Copyright 2023, Apple Inc.
485+
© Copyright 2024, Apple Inc.
486486
<br/>
487487

488488
</p>

Diff for: docs-guides/source/convert-nlp-model.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
<link rel="preload" as="script" href="../_static/scripts/bootstrap.js?digest=26a4bc78f4c0ddb94549" />
4242
<link rel="preload" as="script" href="../_static/scripts/pydata-sphinx-theme.js?digest=26a4bc78f4c0ddb94549" />
4343

44-
<script src="../_static/documentation_options.js?v=c1ce5b23"></script>
44+
<script src="../_static/documentation_options.js?v=6ab2ec1d"></script>
4545
<script src="../_static/doctools.js?v=9a2dae69"></script>
4646
<script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
4747
<script src="../_static/clipboard.min.js?v=a7894cd8"></script>
@@ -623,7 +623,7 @@ <h2>Run the Converted Core ML Model<a class="headerlink" href="#run-the-converte
623623

624624
<p class="copyright">
625625

626-
© Copyright 2023, Apple Inc.
626+
© Copyright 2024, Apple Inc.
627627
<br/>
628628

629629
</p>

Diff for: docs-guides/source/convert-openelm.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
<link rel="preload" as="script" href="../_static/scripts/bootstrap.js?digest=26a4bc78f4c0ddb94549" />
4242
<link rel="preload" as="script" href="../_static/scripts/pydata-sphinx-theme.js?digest=26a4bc78f4c0ddb94549" />
4343

44-
<script src="../_static/documentation_options.js?v=c1ce5b23"></script>
44+
<script src="../_static/documentation_options.js?v=6ab2ec1d"></script>
4545
<script src="../_static/doctools.js?v=9a2dae69"></script>
4646
<script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
4747
<script src="../_static/clipboard.min.js?v=a7894cd8"></script>
@@ -643,7 +643,7 @@ <h2>Run the Converted Core ML Model<a class="headerlink" href="#run-the-converte
643643

644644
<p class="copyright">
645645

646-
© Copyright 2023, Apple Inc.
646+
© Copyright 2024, Apple Inc.
647647
<br/>
648648

649649
</p>

Diff for: docs-guides/source/convert-pytorch-workflow.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
<link rel="preload" as="script" href="../_static/scripts/bootstrap.js?digest=26a4bc78f4c0ddb94549" />
4242
<link rel="preload" as="script" href="../_static/scripts/pydata-sphinx-theme.js?digest=26a4bc78f4c0ddb94549" />
4343

44-
<script src="../_static/documentation_options.js?v=c1ce5b23"></script>
44+
<script src="../_static/documentation_options.js?v=6ab2ec1d"></script>
4545
<script src="../_static/doctools.js?v=9a2dae69"></script>
4646
<script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
4747
<script src="../_static/clipboard.min.js?v=a7894cd8"></script>
@@ -601,7 +601,7 @@ <h2>Convert to Core ML<a class="headerlink" href="#convert-to-core-ml" title="Li
601601

602602
<p class="copyright">
603603

604-
© Copyright 2023, Apple Inc.
604+
© Copyright 2024, Apple Inc.
605605
<br/>
606606

607607
</p>

0 commit comments

Comments
 (0)