Skip to content

Commit 878c937

Browse files
vineet-gVineet Garg
andauthored
[Docs] Updates to docs-guides (#2239)
* Consistent usage of coremltools.optimize as cto across docs-guides * Code formatting --------- Co-authored-by: Vineet Garg <vineetgarg@apple.com>
1 parent 5313fc7 commit 878c937

File tree

4 files changed

+29
-27
lines changed

4 files changed

+29
-27
lines changed

docs-guides/source/mlmodel-utilities.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ optimization of the model via the `ct.optimize.coreml` API.
120120

121121
### Using the Metadata
122122

123-
The [`get_weights_metadata()`](https://apple.github.io/coremltools/source/coremltools.optimize.coreml.post_training_quantization.html#coremltools.optimize.coreml.get_weights_metadata) utility returns the weights metadata as an ordered dictionary that maps to strings in [CoreMLWeightMetaData](https://apple.github.io/coremltools/source/coremltools.optimize.coreml.post_training_quantization.html#coremltools.optimize.coreml.CoreMLWeightMetaData) and preserves the sequential order of the weights. The results are useful when constructing [`cto.OptimizationConfig`](https://apple.github.io/coremltools/docs-guides/source/optimizecoreml-api-overview.html#customizing-ops-to-compress).
123+
The [`get_weights_metadata()`](https://apple.github.io/coremltools/source/coremltools.optimize.coreml.post_training_quantization.html#coremltools.optimize.coreml.get_weights_metadata) utility returns the weights metadata as an ordered dictionary that maps to strings in [CoreMLWeightMetaData](https://apple.github.io/coremltools/source/coremltools.optimize.coreml.post_training_quantization.html#coremltools.optimize.coreml.CoreMLWeightMetaData) and preserves the sequential order of the weights. The results are useful when constructing [`cto.coreml.OptimizationConfig`](https://apple.github.io/coremltools/docs-guides/source/optimizecoreml-api-overview.html#customizing-ops-to-compress).
124124

125125
For example, with the [OptimizationConfig](https://apple.github.io/coremltools/source/coremltools.optimize.coreml.config.html#coremltools.optimize.coreml.OptimizationConfig) class you have fine-grain control over applying different optimization configurations to different weights by directly setting `op_type_configs` and `op_name_configs` or using [`set_op_name`](https://apple.github.io/coremltools/source/coremltools.optimize.coreml.config.html#coremltools.optimize.coreml.OptimizationConfig.set_op_name) and [`set_op_type`](https://apple.github.io/coremltools/source/coremltools.optimize.coreml.config.html#coremltools.optimize.coreml.OptimizationConfig.set_op_type). When using [`set_op_name`](https://apple.github.io/coremltools/source/coremltools.optimize.coreml.config.html#coremltools.optimize.coreml.OptimizationConfig.set_op_name), you need to know the name for the `const` op that produces the weight. The `get_weights_metadata()` utility provides the weight name and the corresponding weight numpy data, along with metadata information.
126126

@@ -132,7 +132,7 @@ The following code loads the `SegmentationModel_with_metadata.mlpackage` saved i
132132
The example also shows how to get the name of the last weight in the model. The code palettizes all ops except the last weight, which is a common practical scenario when the last layer is more sensitive and should be skipped from quantization:
133133

134134
```python
135-
import coremltools.optimize.coreml as cto
135+
import coremltools.optimize as cto
136136

137137
from coremltools.models import MLModel
138138
from coremltools.optimize.coreml import get_weights_metadata
@@ -164,11 +164,11 @@ for weight_name, weight_metadata in weight_metadata_dict.items():
164164

165165
# Palettize all weights except for the last weight
166166
last_weight_name = list(weight_metadata_dict.keys())[-1]
167-
global_config = cto.OpPalettizerConfig(nbits=6, mode="kmeans")
168-
config = cto.OptimizationConfig(
167+
global_config = cto.coreml.OpPalettizerConfig(nbits=6, mode="kmeans")
168+
config = cto.coreml.OptimizationConfig(
169169
global_config=global_config,
170170
op_name_configs={last_weight_name: None},
171171
)
172-
compressed_mlmodel = cto.palettize_weights(mlmodel, config)
172+
compressed_mlmodel = cto.coreml.palettize_weights(mlmodel, config)
173173

174174
```

docs-guides/source/opt-palettization-api.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,19 @@ The following example shows `6-bit` palettization applied to all the ops which h
2222
This is controlled by setting the `weight_threshold` parameter to 512.
2323
```python
2424
import coremltools as ct
25-
import coremltools.optimize.coreml as cto
25+
import coremltools.optimize as cto
2626

2727
# load model
2828
mlmodel = ct.models.MLModel(uncompressed_model_path)
2929

3030
# define op config
31-
op_config = cto.OpPalettizerConfig(nbits=6, weight_threshold=512)
31+
op_config = cto.coreml.OpPalettizerConfig(nbits=6, weight_threshold=512)
3232

3333
# define optimization config by applying the op config globally to all ops
34-
config = cto.OptimizationConfig(global_config=op_config)
34+
config = cto.coreml.OptimizationConfig(global_config=op_config)
3535

3636
# palettize weights
37-
compressed_mlmodel = cto.palettize_weights(mlmodel, config)
37+
compressed_mlmodel = cto.coreml.palettize_weights(mlmodel, config)
3838
```
3939
Some key parameters that the config accepts are:
4040
- `n_bits` : This controls the number of clusters, which are `2^n_bits` .
@@ -54,18 +54,18 @@ to `8-bits`, and two of the conv ops (named `conv1` and `conv3`) are omitted fro
5454

5555
```python
5656
import coremltools as ct
57-
import coremltools.optimize.coreml as cto
57+
import coremltools.optimize as cto
5858

5959
mlmodel = ct.models.MLModel(uncompressed_model_path)
6060

61-
global_config = cto.OpPalettizerConfig(nbits=6)
62-
linear_config = cto.OpPalettizerConfig(nbits=8)
63-
config = cto.OptimizationConfig(
61+
global_config = cto.coreml.OpPalettizerConfig(nbits=6)
62+
linear_config = cto.coreml.OpPalettizerConfig(nbits=8)
63+
config = cto.coreml.OptimizationConfig(
6464
global_config=global_config,
6565
op_type_configs={"linear": linear_config},
6666
op_name_configs={"conv1": None, "conv3": None},
6767
)
68-
compressed_mlmodel = cto.palettize_weights(mlmodel, config)
68+
compressed_mlmodel = cto.coreml.palettize_weights(mlmodel, config)
6969
```
7070

7171
For more details, please follow the detailed API page for [coremltools.optimize.coreml.palettize_weights](https://apple.github.io/coremltools/source/coremltools.optimize.coreml.post_training_quantization.html#coremltools.optimize.coreml.palettize_weights)

docs-guides/source/opt-quantization-api.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ You can linearly quantize the weights of your Core ML model by using the
99
[``linear_quantize_weights``](https://apple.github.io/coremltools/source/coremltools.optimize.coreml.post_training_quantization.html#coremltools.optimize.coreml.linear_quantize_weights) method as follows:
1010

1111
```python
12-
import coremltools.optimize.coreml as cto
12+
import coremltools.optimize as cto
1313

14-
op_config = cto.OpLinearQuantizerConfig(mode="linear_symmetric", weight_threshold=512)
15-
config = cto.OptimizationConfig(global_config=op_config)
14+
op_config = cto.coreml.OpLinearQuantizerConfig(
15+
mode="linear_symmetric", weight_threshold=512
16+
)
17+
config = cto.coreml.OptimizationConfig(global_config=op_config)
1618

17-
compressed_8_bit_model = cto.linear_quantize_weights(model, config=config)
19+
compressed_8_bit_model = cto.coreml.linear_quantize_weights(model, config=config)
1820
```
1921

2022
The method defaults to ``linear_symmetric``, which uses only per-channel scales and no zero-points.

docs-guides/source/opt-workflow.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,15 @@ followed by data free palettization etc.
134134
Sample pseudocode of applying palettization to an `mlpackage` model:
135135
```python
136136
import coremltools as ct
137-
import coremltools.optimize.coreml as cto
137+
import coremltools.optimize as cto
138138

139139
mlmodel = ct.models.MLModel(uncompressed_model_path)
140-
op_config = cto.OpPalettizerConfig(mode="kmeans",
140+
op_config = cto.coreml.OpPalettizerConfig(mode="kmeans",
141141
nbits=4,
142142
granularity="per_grouped_channel",
143143
group_size=16)
144-
model_config = cto.OptimizationConfig(global_config=op_config)
145-
compressed_mlmodel = cto.palettize_weights(mlmodel, model_config)
144+
model_config = cto.coreml.OptimizationConfig(global_config=op_config)
145+
compressed_mlmodel = cto.coreml.palettize_weights(mlmodel, model_config)
146146
```
147147

148148
Sample pseudocode of applying palettization to a torch model:
@@ -191,7 +191,7 @@ Quantizing activations can be applied either to the torch model, or
191191
directly to an `mlpackage` model as well. Sample pseudocode snippet to do so:
192192
```python
193193
import coremltools as ct
194-
import coremltools.optimize.coreml as cto
194+
import coremltools.optimize as cto
195195
# The following API is for coremltools==8.0b1
196196
# It will be moved out of "experimental" in later versions of coremltools
197197
from coremltools.optimize.coreml.experimental import OpActivationLinearQuantizerConfig, \
@@ -201,16 +201,16 @@ mlmodel = ct.models.MLModel(uncompressed_model_path)
201201

202202
# quantize activations to 8 bits (this will give an A8W16 model)
203203
act_quant_op_config = OpActivationLinearQuantizerConfig(mode="linear_symmetric")
204-
act_quant_model_config = cto.OptimizationConfig(global_config=act_quant_op_config)
204+
act_quant_model_config = cto.coreml.OptimizationConfig(global_config=act_quant_op_config)
205205
mlmodel_compressed_activations = linear_quantize_activations(mlmodel,
206206
act_quant_model_config,
207207
sample_data=...)
208208

209209
# quantize weights to 8 bits (this will give an A8W8 model)
210-
weight_quant_op_config = cto.OpLinearQuantizerConfig(mode="linear_symmetric",
210+
weight_quant_op_config = cto.coreml.OpLinearQuantizerConfig(mode="linear_symmetric",
211211
dtype="int8")
212-
weight_quant_model_config = cto.OptimizationConfig(weight_quant_op_config)
213-
mlmodel_compressed = cto.linear_quantize_weights(mlmodel_compressed_activations,
212+
weight_quant_model_config = cto.coreml.OptimizationConfig(weight_quant_op_config)
213+
mlmodel_compressed = cto.coreml.linear_quantize_weights(mlmodel_compressed_activations,
214214
weight_quant_model_config)
215215
```
216216

0 commit comments

Comments
 (0)