Skip to content

Commit

Permalink
Update docs and add examples for Compute Plan and In-memory model usage.
Browse files Browse the repository at this point in the history
  • Loading branch information
cymbalrush committed Dec 18, 2024
1 parent 6ada7fc commit bc54911
Show file tree
Hide file tree
Showing 392 changed files with 4,819 additions and 6,298 deletions.
2 changes: 1 addition & 1 deletion docs-guides/.buildinfo
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Sphinx build info version 1
# This file hashes the configuration used when building these files. When it is not found, a full rebuild will be done.
config: a0cc5a057fb0f76f4409d50e75aca1b6
config: 8b12fed34ce7966d0db3a29cbeb3e84c
tags: 645f666f9bcd5a90fca523b33c5a78b7
86 changes: 84 additions & 2 deletions docs-guides/_sources/source/mlmodel-utilities.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ An example how to update the output data types:

```python
from coremltools.models.model import MLModel
from coremltools.utils import change_array_output_type
from coremltools.utils import change_input_output_tensor_type
from coremltools.proto.FeatureTypes_pb2 import ArrayFeatureType

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

model = MLModel("my_model.mlpackage")
Expand All @@ -257,3 +257,85 @@ Optional arguments:
Special values for `input_names` and `output_names` arguments:
* an empty list means nothing will be modified (default for `input_names`)
* a list containing `"*"` string means all relevant inputs/outputs will be modified (those that will match the `from_type` type)

## Compute Plan

In certain situations, you may want to evaluate the computational needs of a Core ML model before deploying it.
The `MLComputePlan` class is designed for this purpose, allowing you to get insights into the resources and costs
associated with using the model.

Here’s what you can do with `MLComputePlan`:
- Model Structure: Examine the model structure.
- Compute Device Usage: Get insights into the compute devices that would be used for executing an ML Program operation/ NeuralNetwork layer.
- Estimated Cost: Get the estimated cost of executing an ML Program operation.

An example on how to use `MLComputePlan` to get the estimated cost and compute device usages for the operations in an ML Program:

```python
import coremltools as ct
# Path to the compiled ML Program model.
compiled_model_path = "my_model.mlmodelc"
# Load the compute plan of a model.
compute_plan = ct.models.MLComputePlan.compute_plan.load_from_path(
path=compiled_model_path,
compute_units=ct.ComputeUnits.ALL,
)
# Get the model structure.
program = compute_plan.model_structure.program
mainFunction = program.functions["main"]
for operation in mainFunction.block.operations:
# Get the compute device usage for the operation.
compute_device_usage = (
compute_plan.get_compute_device_usage_for_mlprogram_operation(operation)
)
# Get the estimated cost of executing the operation.
estimated_cost = compute_plan.get_estimated_cost_for_mlprogram_operation(operation)
```

## In-memory Model
If you are using an in-memory model in your application, you can easily test the workflow with `MLModelAsset`. The `MLModelAsset` class includes
the `MLModelAsset.from_memory` API, which enables you to load a model directly from the model's in-memory specification data. Once loaded, you
can use the model to make predictions.

An example on how to use `MLModelAsset` to load an `MLCompiledModel` from in-memory specification data:

```python
import coremltools as ct
# Path to the model.
model = MLModel("my_model.model")
model_spec = model.get_spec()
spec_data = model_spec.SerializeToString()
asset = ct.models.model.MLModelAsset.from_memory(spec_data=spec_data)
compiled_model = ct.models.CompiledMLModel.from_asset(asset=asset)
result = compiled_model.predict(
{
"x": np.array([1.0]),
"y": np.array([2.0]),
}
)
```

Another example on how to use `MLModelAsset` to load a MLCompiledModel from in-memory specification data where the specification has external blob file references :


```python
import coremltools as ct
# Path to the model.
mlmodel = MLModel("my_model.mlpackage")
weight_file_path = mlmodel.weights_dir + "/weight.bin"
with open(weight_file_path, "rb") as file:
weights_data = file.read()
model_spec = model.get_spec()
spec_data = model_spec.SerializeToString()
# Provide the weights data as `blob_mapping`.
asset = ct.models.model.MLModelAsset.from_memory(
spec_data=spec_data, blob_mapping={"weights/weight.bin": weights_data}
)
compiled_model = ct.models.CompiledMLModel.from_asset(asset=asset)
result = compiled_model.predict(
{
"x": np.array([1.0]),
"y": np.array([2.0]),
}
)
```
2 changes: 1 addition & 1 deletion docs-guides/_static/documentation_options.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const DOCUMENTATION_OPTIONS = {
VERSION: '7.0',
VERSION: '8.1',
LANGUAGE: 'en',
COLLAPSE_INDEX: false,
BUILDER: 'html',
Expand Down
4 changes: 2 additions & 2 deletions docs-guides/genindex.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
<link rel="preload" as="script" href="_static/scripts/bootstrap.js?digest=26a4bc78f4c0ddb94549" />
<link rel="preload" as="script" href="_static/scripts/pydata-sphinx-theme.js?digest=26a4bc78f4c0ddb94549" />

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

<p class="copyright">

© Copyright 2023, Apple Inc.
© Copyright 2024, Apple Inc.
<br/>

</p>
Expand Down
4 changes: 2 additions & 2 deletions docs-guides/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<link rel="preload" as="script" href="_static/scripts/bootstrap.js?digest=26a4bc78f4c0ddb94549" />
<link rel="preload" as="script" href="_static/scripts/pydata-sphinx-theme.js?digest=26a4bc78f4c0ddb94549" />

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

<p class="copyright">

© Copyright 2023, Apple Inc.
© Copyright 2024, Apple Inc.
<br/>

</p>
Expand Down
4 changes: 2 additions & 2 deletions docs-guides/search.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<link rel="preload" as="script" href="_static/scripts/bootstrap.js?digest=26a4bc78f4c0ddb94549" />
<link rel="preload" as="script" href="_static/scripts/pydata-sphinx-theme.js?digest=26a4bc78f4c0ddb94549" />

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

<p class="copyright">

© Copyright 2023, Apple Inc.
© Copyright 2024, Apple Inc.
<br/>

</p>
Expand Down
2 changes: 1 addition & 1 deletion docs-guides/searchindex.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs-guides/source/classifiers.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<link rel="preload" as="script" href="../_static/scripts/bootstrap.js?digest=26a4bc78f4c0ddb94549" />
<link rel="preload" as="script" href="../_static/scripts/pydata-sphinx-theme.js?digest=26a4bc78f4c0ddb94549" />

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

<p class="copyright">

© Copyright 2023, Apple Inc.
© Copyright 2024, Apple Inc.
<br/>

</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<link rel="preload" as="script" href="../_static/scripts/bootstrap.js?digest=26a4bc78f4c0ddb94549" />
<link rel="preload" as="script" href="../_static/scripts/pydata-sphinx-theme.js?digest=26a4bc78f4c0ddb94549" />

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

<p class="copyright">

© Copyright 2023, Apple Inc.
© Copyright 2024, Apple Inc.
<br/>

</p>
Expand Down
4 changes: 2 additions & 2 deletions docs-guides/source/composite-operators.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<link rel="preload" as="script" href="../_static/scripts/bootstrap.js?digest=26a4bc78f4c0ddb94549" />
<link rel="preload" as="script" href="../_static/scripts/pydata-sphinx-theme.js?digest=26a4bc78f4c0ddb94549" />

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

<p class="copyright">

© Copyright 2023, Apple Inc.
© Copyright 2024, Apple Inc.
<br/>

</p>
Expand Down
4 changes: 2 additions & 2 deletions docs-guides/source/conversion-options.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<link rel="preload" as="script" href="../_static/scripts/bootstrap.js?digest=26a4bc78f4c0ddb94549" />
<link rel="preload" as="script" href="../_static/scripts/pydata-sphinx-theme.js?digest=26a4bc78f4c0ddb94549" />

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

<p class="copyright">

© Copyright 2023, Apple Inc.
© Copyright 2024, Apple Inc.
<br/>

</p>
Expand Down
4 changes: 2 additions & 2 deletions docs-guides/source/convert-a-pytorch-segmentation-model.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<link rel="preload" as="script" href="../_static/scripts/bootstrap.js?digest=26a4bc78f4c0ddb94549" />
<link rel="preload" as="script" href="../_static/scripts/pydata-sphinx-theme.js?digest=26a4bc78f4c0ddb94549" />

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

<p class="copyright">

© Copyright 2023, Apple Inc.
© Copyright 2024, Apple Inc.
<br/>

</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<link rel="preload" as="script" href="../_static/scripts/bootstrap.js?digest=26a4bc78f4c0ddb94549" />
<link rel="preload" as="script" href="../_static/scripts/pydata-sphinx-theme.js?digest=26a4bc78f4c0ddb94549" />

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

<p class="copyright">

© Copyright 2023, Apple Inc.
© Copyright 2024, Apple Inc.
<br/>

</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<link rel="preload" as="script" href="../_static/scripts/bootstrap.js?digest=26a4bc78f4c0ddb94549" />
<link rel="preload" as="script" href="../_static/scripts/pydata-sphinx-theme.js?digest=26a4bc78f4c0ddb94549" />

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

<p class="copyright">

© Copyright 2023, Apple Inc.
© Copyright 2024, Apple Inc.
<br/>

</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<link rel="preload" as="script" href="../_static/scripts/bootstrap.js?digest=26a4bc78f4c0ddb94549" />
<link rel="preload" as="script" href="../_static/scripts/pydata-sphinx-theme.js?digest=26a4bc78f4c0ddb94549" />

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

<p class="copyright">

© Copyright 2023, Apple Inc.
© Copyright 2024, Apple Inc.
<br/>

</p>
Expand Down
4 changes: 2 additions & 2 deletions docs-guides/source/convert-learning-models.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<link rel="preload" as="script" href="../_static/scripts/bootstrap.js?digest=26a4bc78f4c0ddb94549" />
<link rel="preload" as="script" href="../_static/scripts/pydata-sphinx-theme.js?digest=26a4bc78f4c0ddb94549" />

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

<p class="copyright">

© Copyright 2023, Apple Inc.
© Copyright 2024, Apple Inc.
<br/>

</p>
Expand Down
4 changes: 2 additions & 2 deletions docs-guides/source/convert-nlp-model.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<link rel="preload" as="script" href="../_static/scripts/bootstrap.js?digest=26a4bc78f4c0ddb94549" />
<link rel="preload" as="script" href="../_static/scripts/pydata-sphinx-theme.js?digest=26a4bc78f4c0ddb94549" />

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

<p class="copyright">

© Copyright 2023, Apple Inc.
© Copyright 2024, Apple Inc.
<br/>

</p>
Expand Down
4 changes: 2 additions & 2 deletions docs-guides/source/convert-openelm.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<link rel="preload" as="script" href="../_static/scripts/bootstrap.js?digest=26a4bc78f4c0ddb94549" />
<link rel="preload" as="script" href="../_static/scripts/pydata-sphinx-theme.js?digest=26a4bc78f4c0ddb94549" />

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

<p class="copyright">

© Copyright 2023, Apple Inc.
© Copyright 2024, Apple Inc.
<br/>

</p>
Expand Down
4 changes: 2 additions & 2 deletions docs-guides/source/convert-pytorch-workflow.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<link rel="preload" as="script" href="../_static/scripts/bootstrap.js?digest=26a4bc78f4c0ddb94549" />
<link rel="preload" as="script" href="../_static/scripts/pydata-sphinx-theme.js?digest=26a4bc78f4c0ddb94549" />

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

<p class="copyright">

© Copyright 2023, Apple Inc.
© Copyright 2024, Apple Inc.
<br/>

</p>
Expand Down
Loading

0 comments on commit bc54911

Please sign in to comment.