buildanything 1.7.0 → 1.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude-plugin/marketplace.json +1 -1
- package/.claude-plugin/plugin.json +1 -1
- package/CHANGELOG.md +55 -0
- package/README.md +71 -61
- package/agents/ios-app-review-guardian.md +49 -0
- package/agents/ios-foundation-models-specialist.md +46 -0
- package/agents/ios-storekit-specialist.md +52 -0
- package/agents/ios-swift-architect.md +102 -0
- package/agents/ios-swift-search.md +130 -0
- package/agents/ios-swift-ui-design.md +104 -0
- package/commands/build.md +80 -176
- package/commands/fix.md +65 -0
- package/commands/setup.md +73 -0
- package/commands/ux-review.md +63 -0
- package/commands/verify.md +72 -0
- package/hooks/session-start +18 -1
- package/package.json +5 -2
- package/protocols/brainstorm.md +99 -0
- package/protocols/build-fix.md +52 -0
- package/protocols/cleanup.md +54 -0
- package/protocols/design.md +269 -0
- package/protocols/eval-harness.md +61 -0
- package/protocols/fake-data-detector.md +64 -0
- package/protocols/ios-context.md +235 -0
- package/protocols/ios-frameworks-map.md +323 -0
- package/protocols/ios-phase-branches.md +162 -0
- package/protocols/ios-preflight.md +27 -0
- package/protocols/metric-loop.md +93 -0
- package/protocols/planning.md +87 -0
- package/protocols/smoke-test.md +110 -0
- package/protocols/verify.md +67 -0
- package/protocols/web-phase-branches.md +201 -0
- package/skills/ios/_VENDORED.md +60 -0
- package/skills/ios/activitykit/LICENSE +131 -0
- package/skills/ios/activitykit/SKILL.md +505 -0
- package/skills/ios/activitykit/references/activitykit-patterns.md +868 -0
- package/skills/ios/app-intents/LICENSE +131 -0
- package/skills/ios/app-intents/SKILL.md +494 -0
- package/skills/ios/app-intents/references/appintents-advanced.md +1076 -0
- package/skills/ios/apple-on-device-ai/LICENSE +131 -0
- package/skills/ios/apple-on-device-ai/SKILL.md +505 -0
- package/skills/ios/apple-on-device-ai/references/coreml-conversion.md +425 -0
- package/skills/ios/apple-on-device-ai/references/coreml-optimization.md +344 -0
- package/skills/ios/apple-on-device-ai/references/foundation-models.md +508 -0
- package/skills/ios/apple-on-device-ai/references/mlx-swift.md +285 -0
- package/skills/ios/ios-26-platform/SKILL.md +53 -0
- package/skills/ios/ios-26-platform/references/automatic-adoption.md +161 -0
- package/skills/ios/ios-26-platform/references/backward-compat.md +238 -0
- package/skills/ios/ios-26-platform/references/liquid-glass.md +255 -0
- package/skills/ios/ios-26-platform/references/swiftui-apis.md +277 -0
- package/skills/ios/ios-26-platform/references/toolbar-navigation.md +250 -0
- package/skills/ios/ios-bootstrap/SKILL.md +98 -0
- package/skills/ios/ios-bootstrap/references/apple-docs-mcp-config.md +28 -0
- package/skills/ios/ios-bootstrap/references/new-project-dialog.md +41 -0
- package/skills/ios/ios-bootstrap/references/xcode-mcp-config.md +29 -0
- package/skills/ios/ios-debugger-agent/LICENSE +21 -0
- package/skills/ios/ios-debugger-agent/SKILL.md +58 -0
- package/skills/ios/ios-debugger-agent/agents/openai.yaml +4 -0
- package/skills/ios/ios-entitlements-generator/SKILL.md +47 -0
- package/skills/ios/ios-hig/SKILL.md +41 -0
- package/skills/ios/ios-hig/references/accessibility.md +81 -0
- package/skills/ios/ios-hig/references/content.md +142 -0
- package/skills/ios/ios-hig/references/feedback.md +123 -0
- package/skills/ios/ios-hig/references/interaction.md +199 -0
- package/skills/ios/ios-hig/references/performance-platform.md +129 -0
- package/skills/ios/ios-hig/references/privacy-permissions.md +181 -0
- package/skills/ios/ios-hig/references/visual-design.md +84 -0
- package/skills/ios/ios-info-plist-hardening/SKILL.md +130 -0
- package/skills/ios/ios-maestro-flow-author/SKILL.md +68 -0
- package/skills/ios/ios-maestro-flow-author/references/input-and-scroll.yaml +17 -0
- package/skills/ios/ios-maestro-flow-author/references/modal-and-dismiss.yaml +14 -0
- package/skills/ios/ios-maestro-flow-author/references/onboarding-flow.yaml +16 -0
- package/skills/ios/ios-maestro-flow-author/references/tab-navigation.yaml +13 -0
- package/skills/ios/ios-maestro-flow-author/references/tap-and-assert.yaml +9 -0
- package/skills/ios/swift-accessibility/LICENSE +21 -0
- package/skills/ios/swift-accessibility/SKILL.md +371 -0
- package/skills/ios/swift-accessibility/examples/before-after-appkit.md +446 -0
- package/skills/ios/swift-accessibility/examples/before-after-swiftui.md +441 -0
- package/skills/ios/swift-accessibility/examples/before-after-uikit.md +464 -0
- package/skills/ios/swift-accessibility/references/assistive-access.md +441 -0
- package/skills/ios/swift-accessibility/references/display-settings.md +491 -0
- package/skills/ios/swift-accessibility/references/dynamic-type.md +420 -0
- package/skills/ios/swift-accessibility/references/media-accessibility.md +421 -0
- package/skills/ios/swift-accessibility/references/motor-input.md +393 -0
- package/skills/ios/swift-accessibility/references/nutrition-labels.md +362 -0
- package/skills/ios/swift-accessibility/references/platform-specifics.md +515 -0
- package/skills/ios/swift-accessibility/references/semantic-structure.md +585 -0
- package/skills/ios/swift-accessibility/references/testing-auditing.md +507 -0
- package/skills/ios/swift-accessibility/references/voice-control.md +317 -0
- package/skills/ios/swift-accessibility/references/voiceover-swiftui.md +584 -0
- package/skills/ios/swift-accessibility/references/voiceover-uikit.md +519 -0
- package/skills/ios/swift-accessibility/references/wcag-mapping.md +167 -0
- package/skills/ios/swift-accessibility/resources/audit-template.swift +128 -0
- package/skills/ios/swift-accessibility/resources/qa-checklist.md +258 -0
- package/skills/ios/swift-concurrency/LICENSE +21 -0
- package/skills/ios/swift-concurrency/SKILL.md +171 -0
- package/skills/ios/swift-concurrency/references/_index.md +50 -0
- package/skills/ios/swift-concurrency/references/actors.md +660 -0
- package/skills/ios/swift-concurrency/references/async-algorithms.md +847 -0
- package/skills/ios/swift-concurrency/references/async-await-basics.md +266 -0
- package/skills/ios/swift-concurrency/references/async-sequences.md +710 -0
- package/skills/ios/swift-concurrency/references/core-data.md +560 -0
- package/skills/ios/swift-concurrency/references/glossary.md +135 -0
- package/skills/ios/swift-concurrency/references/linting.md +155 -0
- package/skills/ios/swift-concurrency/references/memory-management.md +569 -0
- package/skills/ios/swift-concurrency/references/migration.md +1104 -0
- package/skills/ios/swift-concurrency/references/performance.md +593 -0
- package/skills/ios/swift-concurrency/references/sendable.md +598 -0
- package/skills/ios/swift-concurrency/references/tasks.md +636 -0
- package/skills/ios/swift-concurrency/references/testing.md +592 -0
- package/skills/ios/swift-concurrency/references/threading.md +495 -0
- package/skills/ios/swift-security-expert/LICENSE +21 -0
- package/skills/ios/swift-security-expert/SKILL.md +470 -0
- package/skills/ios/swift-security-expert/references/biometric-authentication.md +565 -0
- package/skills/ios/swift-security-expert/references/certificate-trust.md +592 -0
- package/skills/ios/swift-security-expert/references/common-anti-patterns.md +690 -0
- package/skills/ios/swift-security-expert/references/compliance-owasp-mapping.md +537 -0
- package/skills/ios/swift-security-expert/references/credential-storage-patterns.md +721 -0
- package/skills/ios/swift-security-expert/references/cryptokit-public-key.md +505 -0
- package/skills/ios/swift-security-expert/references/cryptokit-symmetric.md +497 -0
- package/skills/ios/swift-security-expert/references/keychain-access-control.md +508 -0
- package/skills/ios/swift-security-expert/references/keychain-fundamentals.md +596 -0
- package/skills/ios/swift-security-expert/references/keychain-item-classes.md +476 -0
- package/skills/ios/swift-security-expert/references/keychain-sharing.md +458 -0
- package/skills/ios/swift-security-expert/references/migration-legacy-stores.md +727 -0
- package/skills/ios/swift-security-expert/references/secure-enclave.md +539 -0
- package/skills/ios/swift-security-expert/references/testing-security-code.md +781 -0
- package/skills/ios/swift-testing-expert/LICENSE +21 -0
- package/skills/ios/swift-testing-expert/SKILL.md +79 -0
- package/skills/ios/swift-testing-expert/references/_index.md +12 -0
- package/skills/ios/swift-testing-expert/references/async-testing-and-waiting.md +127 -0
- package/skills/ios/swift-testing-expert/references/expectations.md +145 -0
- package/skills/ios/swift-testing-expert/references/fundamentals.md +141 -0
- package/skills/ios/swift-testing-expert/references/migration-from-xctest.md +127 -0
- package/skills/ios/swift-testing-expert/references/parallelization-and-isolation.md +95 -0
- package/skills/ios/swift-testing-expert/references/parameterized-testing.md +284 -0
- package/skills/ios/swift-testing-expert/references/performance-and-best-practices.md +187 -0
- package/skills/ios/swift-testing-expert/references/traits-and-tags.md +114 -0
- package/skills/ios/swift-testing-expert/references/xcode-workflows.md +70 -0
- package/skills/ios/swiftdata-pro/LICENSE +21 -0
- package/skills/ios/swiftdata-pro/SKILL.md +102 -0
- package/skills/ios/swiftdata-pro/agents/openai.yaml +10 -0
- package/skills/ios/swiftdata-pro/assets/swiftdata-pro-icon.png +0 -0
- package/skills/ios/swiftdata-pro/assets/swiftdata-pro-icon.svg +29 -0
- package/skills/ios/swiftdata-pro/references/class-inheritance.md +104 -0
- package/skills/ios/swiftdata-pro/references/cloudkit.md +10 -0
- package/skills/ios/swiftdata-pro/references/core-rules.md +20 -0
- package/skills/ios/swiftdata-pro/references/indexing.md +27 -0
- package/skills/ios/swiftdata-pro/references/predicates.md +73 -0
- package/skills/ios/swiftui-design-principles/AGENTS.md +21 -0
- package/skills/ios/swiftui-design-principles/LICENSE +21 -0
- package/skills/ios/swiftui-design-principles/README.md +41 -0
- package/skills/ios/swiftui-design-principles/SKILL.md +605 -0
- package/skills/ios/swiftui-design-principles/metadata.json +10 -0
- package/skills/ios/swiftui-liquid-glass/LICENSE +21 -0
- package/skills/ios/swiftui-liquid-glass/SKILL.md +95 -0
- package/skills/ios/swiftui-liquid-glass/agents/openai.yaml +4 -0
- package/skills/ios/swiftui-liquid-glass/references/liquid-glass.md +280 -0
- package/skills/ios/swiftui-performance-audit/LICENSE +21 -0
- package/skills/ios/swiftui-performance-audit/SKILL.md +111 -0
- package/skills/ios/swiftui-performance-audit/agents/openai.yaml +4 -0
- package/skills/ios/swiftui-performance-audit/references/code-smells.md +150 -0
- package/skills/ios/swiftui-performance-audit/references/demystify-swiftui-performance-wwdc23.md +46 -0
- package/skills/ios/swiftui-performance-audit/references/optimizing-swiftui-performance-instruments.md +29 -0
- package/skills/ios/swiftui-performance-audit/references/profiling-intake.md +44 -0
- package/skills/ios/swiftui-performance-audit/references/report-template.md +47 -0
- package/skills/ios/swiftui-performance-audit/references/understanding-hangs-in-your-app.md +33 -0
- package/skills/ios/swiftui-performance-audit/references/understanding-improving-swiftui-performance.md +52 -0
- package/skills/ios/swiftui-pro/LICENSE +21 -0
- package/skills/ios/swiftui-pro/SKILL.md +108 -0
- package/skills/ios/swiftui-pro/agents/openai.yaml +10 -0
- package/skills/ios/swiftui-pro/assets/swiftui-pro-icon.png +0 -0
- package/skills/ios/swiftui-pro/assets/swiftui-pro-icon.svg +29 -0
- package/skills/ios/swiftui-pro/references/accessibility.md +13 -0
- package/skills/ios/swiftui-pro/references/api.md +39 -0
- package/skills/ios/swiftui-pro/references/data.md +43 -0
- package/skills/ios/swiftui-pro/references/design.md +31 -0
- package/skills/ios/swiftui-pro/references/hygiene.md +9 -0
- package/skills/ios/swiftui-pro/references/navigation.md +14 -0
- package/skills/ios/swiftui-pro/references/performance.md +46 -0
- package/skills/ios/swiftui-pro/references/swift.md +56 -0
- package/skills/ios/swiftui-pro/references/views.md +35 -0
- package/skills/ios/swiftui-ui-patterns/LICENSE +21 -0
- package/skills/ios/swiftui-ui-patterns/SKILL.md +100 -0
- package/skills/ios/swiftui-ui-patterns/agents/openai.yaml +4 -0
- package/skills/ios/swiftui-ui-patterns/references/app-wiring.md +201 -0
- package/skills/ios/swiftui-ui-patterns/references/async-state.md +96 -0
- package/skills/ios/swiftui-ui-patterns/references/components-index.md +50 -0
- package/skills/ios/swiftui-ui-patterns/references/controls.md +57 -0
- package/skills/ios/swiftui-ui-patterns/references/deeplinks.md +66 -0
- package/skills/ios/swiftui-ui-patterns/references/focus.md +90 -0
- package/skills/ios/swiftui-ui-patterns/references/form.md +97 -0
- package/skills/ios/swiftui-ui-patterns/references/grids.md +71 -0
- package/skills/ios/swiftui-ui-patterns/references/haptics.md +71 -0
- package/skills/ios/swiftui-ui-patterns/references/input-toolbar.md +51 -0
- package/skills/ios/swiftui-ui-patterns/references/lightweight-clients.md +93 -0
- package/skills/ios/swiftui-ui-patterns/references/list.md +86 -0
- package/skills/ios/swiftui-ui-patterns/references/loading-placeholders.md +38 -0
- package/skills/ios/swiftui-ui-patterns/references/macos-settings.md +71 -0
- package/skills/ios/swiftui-ui-patterns/references/matched-transitions.md +59 -0
- package/skills/ios/swiftui-ui-patterns/references/media.md +73 -0
- package/skills/ios/swiftui-ui-patterns/references/menu-bar.md +101 -0
- package/skills/ios/swiftui-ui-patterns/references/navigationstack.md +159 -0
- package/skills/ios/swiftui-ui-patterns/references/overlay.md +45 -0
- package/skills/ios/swiftui-ui-patterns/references/performance.md +62 -0
- package/skills/ios/swiftui-ui-patterns/references/previews.md +48 -0
- package/skills/ios/swiftui-ui-patterns/references/scroll-reveal.md +133 -0
- package/skills/ios/swiftui-ui-patterns/references/scrollview.md +87 -0
- package/skills/ios/swiftui-ui-patterns/references/searchable.md +71 -0
- package/skills/ios/swiftui-ui-patterns/references/sheets.md +155 -0
- package/skills/ios/swiftui-ui-patterns/references/split-views.md +72 -0
- package/skills/ios/swiftui-ui-patterns/references/tabview.md +114 -0
- package/skills/ios/swiftui-ui-patterns/references/theming.md +71 -0
- package/skills/ios/swiftui-ui-patterns/references/title-menus.md +93 -0
- package/skills/ios/swiftui-ui-patterns/references/top-bar.md +49 -0
- package/skills/ios/swiftui-view-refactor/LICENSE +21 -0
- package/skills/ios/swiftui-view-refactor/SKILL.md +207 -0
- package/skills/ios/swiftui-view-refactor/agents/openai.yaml +4 -0
- package/skills/ios/swiftui-view-refactor/references/mv-patterns.md +161 -0
- package/skills/ios/widgetkit/LICENSE +131 -0
- package/skills/ios/widgetkit/SKILL.md +502 -0
- package/skills/ios/widgetkit/references/widgetkit-advanced.md +871 -0
|
@@ -0,0 +1,344 @@
|
|
|
1
|
+
# Core ML Optimization Reference
|
|
2
|
+
|
|
3
|
+
Complete reference for optimizing Core ML models: quantization, palettization,
|
|
4
|
+
pruning, performance tuning, and profiling.
|
|
5
|
+
|
|
6
|
+
## Contents
|
|
7
|
+
|
|
8
|
+
- [Optimization Technique Selection](#optimization-technique-selection)
|
|
9
|
+
- [Post-Training Weight Quantization (Data-Free)](#post-training-weight-quantization-data-free)
|
|
10
|
+
- [Palettization (Weight Clustering)](#palettization-weight-clustering)
|
|
11
|
+
- [Pruning (Weight Sparsification)](#pruning-weight-sparsification)
|
|
12
|
+
- [Joint Compression (Stacking Techniques)](#joint-compression-stacking-techniques)
|
|
13
|
+
- [Per-Op Configuration](#per-op-configuration)
|
|
14
|
+
- [Quantization-Aware Training (QAT)](#quantization-aware-training-qat)
|
|
15
|
+
- [Swift Integration](#swift-integration)
|
|
16
|
+
- [MLTensor (iOS 18+)](#mltensor-ios-18)
|
|
17
|
+
- [Neural Engine Best Practices](#neural-engine-best-practices)
|
|
18
|
+
- [Model Loading Optimization](#model-loading-optimization)
|
|
19
|
+
- [Profiling](#profiling)
|
|
20
|
+
- [Common Optimization Mistakes](#common-optimization-mistakes)
|
|
21
|
+
|
|
22
|
+
## Optimization Technique Selection
|
|
23
|
+
|
|
24
|
+
| Technique | Size Reduction | Accuracy Impact | Best Compute Unit | Min OS |
|
|
25
|
+
|---|---|---|---|---|
|
|
26
|
+
| INT8 per-channel | ~4x | Low | CPU/GPU | iOS 16 |
|
|
27
|
+
| INT4 per-block | ~8x | Medium | GPU | iOS 18 |
|
|
28
|
+
| Palettization 4-bit | ~8x | Low-Medium | Neural Engine | iOS 16 |
|
|
29
|
+
| Palettization 2-bit | ~16x | Medium-High | Neural Engine | iOS 16 |
|
|
30
|
+
| W8A8 (weights+activations) | ~4x | Low | ANE (A17 Pro/M4+) | iOS 17 |
|
|
31
|
+
| Pruning 50% | ~2x | Low | CPU/ANE | iOS 16 |
|
|
32
|
+
| Pruning 75% | ~4x | Medium | CPU/ANE | iOS 16 |
|
|
33
|
+
|
|
34
|
+
## Post-Training Weight Quantization (Data-Free)
|
|
35
|
+
|
|
36
|
+
### INT8 Per-Channel Symmetric
|
|
37
|
+
|
|
38
|
+
```python
|
|
39
|
+
import coremltools as ct
|
|
40
|
+
import coremltools.optimize as cto
|
|
41
|
+
|
|
42
|
+
model = ct.models.MLModel("model.mlpackage")
|
|
43
|
+
|
|
44
|
+
op_config = cto.coreml.OpLinearQuantizerConfig(
|
|
45
|
+
mode="linear_symmetric", # or "linear" (asymmetric with zero-point)
|
|
46
|
+
weight_threshold=512, # only quantize tensors with > N elements
|
|
47
|
+
)
|
|
48
|
+
config = cto.coreml.OptimizationConfig(global_config=op_config)
|
|
49
|
+
compressed = cto.coreml.linear_quantize_weights(model, config=config)
|
|
50
|
+
compressed.save("model_int8.mlpackage")
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### INT4 Per-Block (PyTorch, Data-Free)
|
|
54
|
+
|
|
55
|
+
```python
|
|
56
|
+
import coremltools.optimize as cto
|
|
57
|
+
|
|
58
|
+
config = cto.torch.quantization.PostTrainingQuantizerConfig.from_dict({
|
|
59
|
+
"global_config": {
|
|
60
|
+
"weight_dtype": "int4",
|
|
61
|
+
"granularity": "per_block",
|
|
62
|
+
"block_size": 128,
|
|
63
|
+
}
|
|
64
|
+
})
|
|
65
|
+
quantizer = cto.torch.quantization.PostTrainingQuantizer(model, config)
|
|
66
|
+
quantized_model = quantizer.compress()
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### GPTQ Calibration-Based Quantization
|
|
70
|
+
|
|
71
|
+
```python
|
|
72
|
+
config = cto.torch.layerwise_compression.LayerwiseCompressorConfig.from_dict({
|
|
73
|
+
"global_config": {
|
|
74
|
+
"algorithm": "gptq",
|
|
75
|
+
"weight_dtype": 4,
|
|
76
|
+
"granularity": "per_block",
|
|
77
|
+
"block_size": 128,
|
|
78
|
+
},
|
|
79
|
+
"calibration_nsamples": 16,
|
|
80
|
+
})
|
|
81
|
+
compressor = cto.torch.layerwise_compression.LayerwiseCompressor(model, config)
|
|
82
|
+
compressed_model = compressor.compress(calibration_dataloader)
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## Palettization (Weight Clustering)
|
|
86
|
+
|
|
87
|
+
Especially effective on the Neural Engine. 4-bit palettization typically
|
|
88
|
+
preserves accuracy better than 4-bit linear quantization.
|
|
89
|
+
|
|
90
|
+
### Post-Conversion Palettization
|
|
91
|
+
|
|
92
|
+
```python
|
|
93
|
+
op_config = cto.coreml.OpPalettizerConfig(
|
|
94
|
+
mode="kmeans", # "kmeans" or "uniform"
|
|
95
|
+
nbits=4, # {1, 2, 3, 4, 6, 8}
|
|
96
|
+
granularity="per_grouped_channel", # iOS 18+ for grouped
|
|
97
|
+
group_size=16,
|
|
98
|
+
)
|
|
99
|
+
config = cto.coreml.OptimizationConfig(global_config=op_config)
|
|
100
|
+
palettized = cto.coreml.palettize_weights(model, config=config)
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### Available Bit Widths
|
|
104
|
+
|
|
105
|
+
| Bits | Unique Values | Size Reduction | Typical Quality |
|
|
106
|
+
|---|---|---|---|
|
|
107
|
+
| 8 | 256 | ~2x | Excellent |
|
|
108
|
+
| 6 | 64 | ~2.7x | Very good |
|
|
109
|
+
| 4 | 16 | ~8x | Good |
|
|
110
|
+
| 3 | 8 | ~10.7x | Moderate |
|
|
111
|
+
| 2 | 4 | ~16x | Fair |
|
|
112
|
+
| 1 | 2 | ~32x | Poor (binary) |
|
|
113
|
+
|
|
114
|
+
## Pruning (Weight Sparsification)
|
|
115
|
+
|
|
116
|
+
### Magnitude Pruning
|
|
117
|
+
|
|
118
|
+
```python
|
|
119
|
+
config = cto.coreml.OptimizationConfig(
|
|
120
|
+
global_config=cto.coreml.OpMagnitudePrunerConfig(
|
|
121
|
+
target_sparsity=0.75,
|
|
122
|
+
weight_threshold=2048,
|
|
123
|
+
)
|
|
124
|
+
)
|
|
125
|
+
pruned = cto.coreml.prune_weights(model, config=config)
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
### Threshold Pruning
|
|
129
|
+
|
|
130
|
+
```python
|
|
131
|
+
config = cto.coreml.OptimizationConfig(
|
|
132
|
+
global_config=cto.coreml.OpThresholdPrunerConfig(
|
|
133
|
+
threshold=1e-12,
|
|
134
|
+
minimum_sparsity_percentile=0.5,
|
|
135
|
+
)
|
|
136
|
+
)
|
|
137
|
+
pruned = cto.coreml.prune_weights(model, config=config)
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
## Joint Compression (Stacking Techniques)
|
|
141
|
+
|
|
142
|
+
Apply multiple compression techniques in sequence:
|
|
143
|
+
|
|
144
|
+
```python
|
|
145
|
+
# Palettize first, then prune on top
|
|
146
|
+
palettized = cto.coreml.palettize_weights(model, pal_config)
|
|
147
|
+
final = cto.coreml.prune_weights(
|
|
148
|
+
palettized, prune_config, joint_compression=True
|
|
149
|
+
)
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
## Per-Op Configuration
|
|
153
|
+
|
|
154
|
+
Fine-grained control over which operations get compressed:
|
|
155
|
+
|
|
156
|
+
```python
|
|
157
|
+
config = cto.coreml.OptimizationConfig(
|
|
158
|
+
global_config=global_op_config,
|
|
159
|
+
op_type_configs={
|
|
160
|
+
"linear": linear_config,
|
|
161
|
+
"conv": conv_config,
|
|
162
|
+
},
|
|
163
|
+
op_name_configs={
|
|
164
|
+
"embedding_layer": None, # None = skip compression
|
|
165
|
+
},
|
|
166
|
+
)
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
## Quantization-Aware Training (QAT)
|
|
170
|
+
|
|
171
|
+
Train with quantization in the loop for best accuracy:
|
|
172
|
+
|
|
173
|
+
```python
|
|
174
|
+
from coremltools.optimize.torch.quantization import (
|
|
175
|
+
LinearQuantizer, LinearQuantizerConfig, ModuleLinearQuantizerConfig
|
|
176
|
+
)
|
|
177
|
+
|
|
178
|
+
config = LinearQuantizerConfig(
|
|
179
|
+
global_config=ModuleLinearQuantizerConfig(
|
|
180
|
+
quantization_scheme="symmetric",
|
|
181
|
+
milestones=[0, 1000, 1000, 0],
|
|
182
|
+
)
|
|
183
|
+
)
|
|
184
|
+
quantizer = LinearQuantizer(model, config)
|
|
185
|
+
quantizer.prepare(example_inputs=[1, 3, 224, 224], inplace=True)
|
|
186
|
+
|
|
187
|
+
# Training loop
|
|
188
|
+
for inputs, labels in data:
|
|
189
|
+
output = model(inputs)
|
|
190
|
+
loss = loss_fn(output, labels)
|
|
191
|
+
loss.backward()
|
|
192
|
+
optimizer.step()
|
|
193
|
+
quantizer.step()
|
|
194
|
+
|
|
195
|
+
model = quantizer.finalize(inplace=True)
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
## Swift Integration
|
|
199
|
+
|
|
200
|
+
### Loading Models
|
|
201
|
+
|
|
202
|
+
```swift
|
|
203
|
+
// From Xcode-compiled model (auto-generated class)
|
|
204
|
+
let model = try MyImageClassifier(configuration: MLModelConfiguration())
|
|
205
|
+
|
|
206
|
+
// From URL at runtime
|
|
207
|
+
let config = MLModelConfiguration()
|
|
208
|
+
config.computeUnits = .all
|
|
209
|
+
let model = try MLModel(contentsOf: modelURL, configuration: config)
|
|
210
|
+
|
|
211
|
+
// From pre-compiled model (.mlmodelc) for faster loading
|
|
212
|
+
let compiledURL = try MLModel.compileModel(at: sourceModelURL)
|
|
213
|
+
let model = try MLModel(contentsOf: compiledURL)
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
### MLModelConfiguration
|
|
217
|
+
|
|
218
|
+
```swift
|
|
219
|
+
let config = MLModelConfiguration()
|
|
220
|
+
config.computeUnits = .all
|
|
221
|
+
config.allowLowPrecisionAccumulationOnGPU = true
|
|
222
|
+
// config.functionName = "adapter_1" // For multifunction models (iOS 18+)
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
### Synchronous Prediction
|
|
226
|
+
|
|
227
|
+
```swift
|
|
228
|
+
let input = MyModelInput(image: pixelBuffer)
|
|
229
|
+
let output = try model.prediction(input: input)
|
|
230
|
+
let label = output.classLabel
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
### Async Prediction (iOS 17+)
|
|
234
|
+
|
|
235
|
+
```swift
|
|
236
|
+
let output = try await model.prediction(input: input)
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
Thread-safe, supports Task cancellation, integrates with Swift concurrency.
|
|
240
|
+
~60% faster than synchronous for batch workloads.
|
|
241
|
+
|
|
242
|
+
### Batch Prediction
|
|
243
|
+
|
|
244
|
+
```swift
|
|
245
|
+
let batchInputs: [MyModelInput] = images.map { MyModelInput(image: $0) }
|
|
246
|
+
let batchOutputs = try model.predictions(inputs: batchInputs)
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
### MLFeatureProvider
|
|
250
|
+
|
|
251
|
+
```swift
|
|
252
|
+
let features = try MLDictionaryFeatureProvider(dictionary: [
|
|
253
|
+
"input": MLFeatureValue(pixelBuffer: pixelBuffer),
|
|
254
|
+
"threshold": MLFeatureValue(double: 0.5),
|
|
255
|
+
])
|
|
256
|
+
let output = try model.prediction(from: features)
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
### Vision Framework Integration
|
|
260
|
+
|
|
261
|
+
```swift
|
|
262
|
+
import Vision
|
|
263
|
+
import CoreML
|
|
264
|
+
|
|
265
|
+
let vnModel = try VNCoreMLModel(for: MyDetector().model)
|
|
266
|
+
let request = VNCoreMLRequest(model: vnModel) { request, error in
|
|
267
|
+
guard let results = request.results as? [VNClassificationObservation] else { return }
|
|
268
|
+
let topResult = results.first
|
|
269
|
+
print("\(topResult?.identifier ?? ""): \(topResult?.confidence ?? 0)")
|
|
270
|
+
}
|
|
271
|
+
let handler = VNImageRequestHandler(cgImage: image)
|
|
272
|
+
try handler.perform([request])
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
### Natural Language Integration
|
|
276
|
+
|
|
277
|
+
```swift
|
|
278
|
+
import NaturalLanguage
|
|
279
|
+
|
|
280
|
+
let nlModel = try NLModel(mlModel: SentimentClassifier().model)
|
|
281
|
+
let sentiment = nlModel.predictedLabel(for: "Great product!")
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
## MLTensor (iOS 18+)
|
|
285
|
+
|
|
286
|
+
Swift type for multidimensional array operations:
|
|
287
|
+
|
|
288
|
+
```swift
|
|
289
|
+
import CoreML
|
|
290
|
+
|
|
291
|
+
let tensor = MLTensor([1.0, 2.0, 3.0, 4.0])
|
|
292
|
+
let reshaped = tensor.reshaped(to: [2, 2])
|
|
293
|
+
let result = tensor.softmax()
|
|
294
|
+
let matmulResult = tensorA.matmul(tensorB)
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
## Neural Engine Best Practices
|
|
298
|
+
|
|
299
|
+
1. Use EnumeratedShapes instead of RangeDim for ANE optimization
|
|
300
|
+
2. Avoid unsupported ANE ops -- they cause fallback to CPU/GPU with transfer
|
|
301
|
+
overhead
|
|
302
|
+
3. Use palettization (4-bit or 6-bit) for best ANE memory/latency gains
|
|
303
|
+
4. W8A8 quantization on A17 Pro / M4+ enables optimized INT8 compute on ANE
|
|
304
|
+
|
|
305
|
+
## Model Loading Optimization
|
|
306
|
+
|
|
307
|
+
1. Pre-compile models -- use `.mlmodelc` for instant loading after first
|
|
308
|
+
compilation
|
|
309
|
+
2. Cache compiled models to a fixed location after `MLModel.compileModel(at:)`
|
|
310
|
+
3. Use `bisect_model()` for very large models that are slow to load
|
|
311
|
+
4. Use `MLComputePlan` (iOS 17+) for programmatic profiling
|
|
312
|
+
|
|
313
|
+
## Profiling
|
|
314
|
+
|
|
315
|
+
1. **Xcode Performance tab** -- open .mlpackage in Xcode to see load time,
|
|
316
|
+
prediction time, per-op compute unit assignment
|
|
317
|
+
2. **Core ML Instrument** in Instruments app -- runtime profiling
|
|
318
|
+
3. **MLComputePlan API** -- programmatic access to profiling data
|
|
319
|
+
4. **coremltools debugging** -- MLModelValidator, MLModelComparator,
|
|
320
|
+
MLModelInspector, MLModelBenchmarker
|
|
321
|
+
|
|
322
|
+
### Reshape Frequency Hint
|
|
323
|
+
|
|
324
|
+
```python
|
|
325
|
+
model = ct.models.MLModel("model.mlpackage",
|
|
326
|
+
optimization_hints={
|
|
327
|
+
"reshapeFrequency": ct.ReshapeFrequency.Infrequent
|
|
328
|
+
})
|
|
329
|
+
```
|
|
330
|
+
|
|
331
|
+
## Common Optimization Mistakes
|
|
332
|
+
|
|
333
|
+
1. **Applying quantization without checking accuracy.** Always validate after
|
|
334
|
+
compression. Use MLModelComparator to compare outputs.
|
|
335
|
+
2. **Ignoring weight_threshold.** Small tensors (< 512 elements) should not be
|
|
336
|
+
quantized -- overhead outweighs the benefit.
|
|
337
|
+
3. **Using synchronous predictions in async contexts.** Use async prediction
|
|
338
|
+
(iOS 17+) in Swift concurrency code.
|
|
339
|
+
4. **Not pre-compiling models.** First load triggers device-specific
|
|
340
|
+
compilation, which can be slow.
|
|
341
|
+
5. **Ignoring compute_units configuration.** Default `.all` is correct for
|
|
342
|
+
production. `.cpuOnly` is for debugging only.
|
|
343
|
+
6. **Not testing on physical devices.** Simulator does not support Metal GPU or
|
|
344
|
+
Neural Engine.
|