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.
Files changed (222) hide show
  1. package/.claude-plugin/marketplace.json +1 -1
  2. package/.claude-plugin/plugin.json +1 -1
  3. package/CHANGELOG.md +55 -0
  4. package/README.md +71 -61
  5. package/agents/ios-app-review-guardian.md +49 -0
  6. package/agents/ios-foundation-models-specialist.md +46 -0
  7. package/agents/ios-storekit-specialist.md +52 -0
  8. package/agents/ios-swift-architect.md +102 -0
  9. package/agents/ios-swift-search.md +130 -0
  10. package/agents/ios-swift-ui-design.md +104 -0
  11. package/commands/build.md +80 -176
  12. package/commands/fix.md +65 -0
  13. package/commands/setup.md +73 -0
  14. package/commands/ux-review.md +63 -0
  15. package/commands/verify.md +72 -0
  16. package/hooks/session-start +18 -1
  17. package/package.json +5 -2
  18. package/protocols/brainstorm.md +99 -0
  19. package/protocols/build-fix.md +52 -0
  20. package/protocols/cleanup.md +54 -0
  21. package/protocols/design.md +269 -0
  22. package/protocols/eval-harness.md +61 -0
  23. package/protocols/fake-data-detector.md +64 -0
  24. package/protocols/ios-context.md +235 -0
  25. package/protocols/ios-frameworks-map.md +323 -0
  26. package/protocols/ios-phase-branches.md +162 -0
  27. package/protocols/ios-preflight.md +27 -0
  28. package/protocols/metric-loop.md +93 -0
  29. package/protocols/planning.md +87 -0
  30. package/protocols/smoke-test.md +110 -0
  31. package/protocols/verify.md +67 -0
  32. package/protocols/web-phase-branches.md +201 -0
  33. package/skills/ios/_VENDORED.md +60 -0
  34. package/skills/ios/activitykit/LICENSE +131 -0
  35. package/skills/ios/activitykit/SKILL.md +505 -0
  36. package/skills/ios/activitykit/references/activitykit-patterns.md +868 -0
  37. package/skills/ios/app-intents/LICENSE +131 -0
  38. package/skills/ios/app-intents/SKILL.md +494 -0
  39. package/skills/ios/app-intents/references/appintents-advanced.md +1076 -0
  40. package/skills/ios/apple-on-device-ai/LICENSE +131 -0
  41. package/skills/ios/apple-on-device-ai/SKILL.md +505 -0
  42. package/skills/ios/apple-on-device-ai/references/coreml-conversion.md +425 -0
  43. package/skills/ios/apple-on-device-ai/references/coreml-optimization.md +344 -0
  44. package/skills/ios/apple-on-device-ai/references/foundation-models.md +508 -0
  45. package/skills/ios/apple-on-device-ai/references/mlx-swift.md +285 -0
  46. package/skills/ios/ios-26-platform/SKILL.md +53 -0
  47. package/skills/ios/ios-26-platform/references/automatic-adoption.md +161 -0
  48. package/skills/ios/ios-26-platform/references/backward-compat.md +238 -0
  49. package/skills/ios/ios-26-platform/references/liquid-glass.md +255 -0
  50. package/skills/ios/ios-26-platform/references/swiftui-apis.md +277 -0
  51. package/skills/ios/ios-26-platform/references/toolbar-navigation.md +250 -0
  52. package/skills/ios/ios-bootstrap/SKILL.md +98 -0
  53. package/skills/ios/ios-bootstrap/references/apple-docs-mcp-config.md +28 -0
  54. package/skills/ios/ios-bootstrap/references/new-project-dialog.md +41 -0
  55. package/skills/ios/ios-bootstrap/references/xcode-mcp-config.md +29 -0
  56. package/skills/ios/ios-debugger-agent/LICENSE +21 -0
  57. package/skills/ios/ios-debugger-agent/SKILL.md +58 -0
  58. package/skills/ios/ios-debugger-agent/agents/openai.yaml +4 -0
  59. package/skills/ios/ios-entitlements-generator/SKILL.md +47 -0
  60. package/skills/ios/ios-hig/SKILL.md +41 -0
  61. package/skills/ios/ios-hig/references/accessibility.md +81 -0
  62. package/skills/ios/ios-hig/references/content.md +142 -0
  63. package/skills/ios/ios-hig/references/feedback.md +123 -0
  64. package/skills/ios/ios-hig/references/interaction.md +199 -0
  65. package/skills/ios/ios-hig/references/performance-platform.md +129 -0
  66. package/skills/ios/ios-hig/references/privacy-permissions.md +181 -0
  67. package/skills/ios/ios-hig/references/visual-design.md +84 -0
  68. package/skills/ios/ios-info-plist-hardening/SKILL.md +130 -0
  69. package/skills/ios/ios-maestro-flow-author/SKILL.md +68 -0
  70. package/skills/ios/ios-maestro-flow-author/references/input-and-scroll.yaml +17 -0
  71. package/skills/ios/ios-maestro-flow-author/references/modal-and-dismiss.yaml +14 -0
  72. package/skills/ios/ios-maestro-flow-author/references/onboarding-flow.yaml +16 -0
  73. package/skills/ios/ios-maestro-flow-author/references/tab-navigation.yaml +13 -0
  74. package/skills/ios/ios-maestro-flow-author/references/tap-and-assert.yaml +9 -0
  75. package/skills/ios/swift-accessibility/LICENSE +21 -0
  76. package/skills/ios/swift-accessibility/SKILL.md +371 -0
  77. package/skills/ios/swift-accessibility/examples/before-after-appkit.md +446 -0
  78. package/skills/ios/swift-accessibility/examples/before-after-swiftui.md +441 -0
  79. package/skills/ios/swift-accessibility/examples/before-after-uikit.md +464 -0
  80. package/skills/ios/swift-accessibility/references/assistive-access.md +441 -0
  81. package/skills/ios/swift-accessibility/references/display-settings.md +491 -0
  82. package/skills/ios/swift-accessibility/references/dynamic-type.md +420 -0
  83. package/skills/ios/swift-accessibility/references/media-accessibility.md +421 -0
  84. package/skills/ios/swift-accessibility/references/motor-input.md +393 -0
  85. package/skills/ios/swift-accessibility/references/nutrition-labels.md +362 -0
  86. package/skills/ios/swift-accessibility/references/platform-specifics.md +515 -0
  87. package/skills/ios/swift-accessibility/references/semantic-structure.md +585 -0
  88. package/skills/ios/swift-accessibility/references/testing-auditing.md +507 -0
  89. package/skills/ios/swift-accessibility/references/voice-control.md +317 -0
  90. package/skills/ios/swift-accessibility/references/voiceover-swiftui.md +584 -0
  91. package/skills/ios/swift-accessibility/references/voiceover-uikit.md +519 -0
  92. package/skills/ios/swift-accessibility/references/wcag-mapping.md +167 -0
  93. package/skills/ios/swift-accessibility/resources/audit-template.swift +128 -0
  94. package/skills/ios/swift-accessibility/resources/qa-checklist.md +258 -0
  95. package/skills/ios/swift-concurrency/LICENSE +21 -0
  96. package/skills/ios/swift-concurrency/SKILL.md +171 -0
  97. package/skills/ios/swift-concurrency/references/_index.md +50 -0
  98. package/skills/ios/swift-concurrency/references/actors.md +660 -0
  99. package/skills/ios/swift-concurrency/references/async-algorithms.md +847 -0
  100. package/skills/ios/swift-concurrency/references/async-await-basics.md +266 -0
  101. package/skills/ios/swift-concurrency/references/async-sequences.md +710 -0
  102. package/skills/ios/swift-concurrency/references/core-data.md +560 -0
  103. package/skills/ios/swift-concurrency/references/glossary.md +135 -0
  104. package/skills/ios/swift-concurrency/references/linting.md +155 -0
  105. package/skills/ios/swift-concurrency/references/memory-management.md +569 -0
  106. package/skills/ios/swift-concurrency/references/migration.md +1104 -0
  107. package/skills/ios/swift-concurrency/references/performance.md +593 -0
  108. package/skills/ios/swift-concurrency/references/sendable.md +598 -0
  109. package/skills/ios/swift-concurrency/references/tasks.md +636 -0
  110. package/skills/ios/swift-concurrency/references/testing.md +592 -0
  111. package/skills/ios/swift-concurrency/references/threading.md +495 -0
  112. package/skills/ios/swift-security-expert/LICENSE +21 -0
  113. package/skills/ios/swift-security-expert/SKILL.md +470 -0
  114. package/skills/ios/swift-security-expert/references/biometric-authentication.md +565 -0
  115. package/skills/ios/swift-security-expert/references/certificate-trust.md +592 -0
  116. package/skills/ios/swift-security-expert/references/common-anti-patterns.md +690 -0
  117. package/skills/ios/swift-security-expert/references/compliance-owasp-mapping.md +537 -0
  118. package/skills/ios/swift-security-expert/references/credential-storage-patterns.md +721 -0
  119. package/skills/ios/swift-security-expert/references/cryptokit-public-key.md +505 -0
  120. package/skills/ios/swift-security-expert/references/cryptokit-symmetric.md +497 -0
  121. package/skills/ios/swift-security-expert/references/keychain-access-control.md +508 -0
  122. package/skills/ios/swift-security-expert/references/keychain-fundamentals.md +596 -0
  123. package/skills/ios/swift-security-expert/references/keychain-item-classes.md +476 -0
  124. package/skills/ios/swift-security-expert/references/keychain-sharing.md +458 -0
  125. package/skills/ios/swift-security-expert/references/migration-legacy-stores.md +727 -0
  126. package/skills/ios/swift-security-expert/references/secure-enclave.md +539 -0
  127. package/skills/ios/swift-security-expert/references/testing-security-code.md +781 -0
  128. package/skills/ios/swift-testing-expert/LICENSE +21 -0
  129. package/skills/ios/swift-testing-expert/SKILL.md +79 -0
  130. package/skills/ios/swift-testing-expert/references/_index.md +12 -0
  131. package/skills/ios/swift-testing-expert/references/async-testing-and-waiting.md +127 -0
  132. package/skills/ios/swift-testing-expert/references/expectations.md +145 -0
  133. package/skills/ios/swift-testing-expert/references/fundamentals.md +141 -0
  134. package/skills/ios/swift-testing-expert/references/migration-from-xctest.md +127 -0
  135. package/skills/ios/swift-testing-expert/references/parallelization-and-isolation.md +95 -0
  136. package/skills/ios/swift-testing-expert/references/parameterized-testing.md +284 -0
  137. package/skills/ios/swift-testing-expert/references/performance-and-best-practices.md +187 -0
  138. package/skills/ios/swift-testing-expert/references/traits-and-tags.md +114 -0
  139. package/skills/ios/swift-testing-expert/references/xcode-workflows.md +70 -0
  140. package/skills/ios/swiftdata-pro/LICENSE +21 -0
  141. package/skills/ios/swiftdata-pro/SKILL.md +102 -0
  142. package/skills/ios/swiftdata-pro/agents/openai.yaml +10 -0
  143. package/skills/ios/swiftdata-pro/assets/swiftdata-pro-icon.png +0 -0
  144. package/skills/ios/swiftdata-pro/assets/swiftdata-pro-icon.svg +29 -0
  145. package/skills/ios/swiftdata-pro/references/class-inheritance.md +104 -0
  146. package/skills/ios/swiftdata-pro/references/cloudkit.md +10 -0
  147. package/skills/ios/swiftdata-pro/references/core-rules.md +20 -0
  148. package/skills/ios/swiftdata-pro/references/indexing.md +27 -0
  149. package/skills/ios/swiftdata-pro/references/predicates.md +73 -0
  150. package/skills/ios/swiftui-design-principles/AGENTS.md +21 -0
  151. package/skills/ios/swiftui-design-principles/LICENSE +21 -0
  152. package/skills/ios/swiftui-design-principles/README.md +41 -0
  153. package/skills/ios/swiftui-design-principles/SKILL.md +605 -0
  154. package/skills/ios/swiftui-design-principles/metadata.json +10 -0
  155. package/skills/ios/swiftui-liquid-glass/LICENSE +21 -0
  156. package/skills/ios/swiftui-liquid-glass/SKILL.md +95 -0
  157. package/skills/ios/swiftui-liquid-glass/agents/openai.yaml +4 -0
  158. package/skills/ios/swiftui-liquid-glass/references/liquid-glass.md +280 -0
  159. package/skills/ios/swiftui-performance-audit/LICENSE +21 -0
  160. package/skills/ios/swiftui-performance-audit/SKILL.md +111 -0
  161. package/skills/ios/swiftui-performance-audit/agents/openai.yaml +4 -0
  162. package/skills/ios/swiftui-performance-audit/references/code-smells.md +150 -0
  163. package/skills/ios/swiftui-performance-audit/references/demystify-swiftui-performance-wwdc23.md +46 -0
  164. package/skills/ios/swiftui-performance-audit/references/optimizing-swiftui-performance-instruments.md +29 -0
  165. package/skills/ios/swiftui-performance-audit/references/profiling-intake.md +44 -0
  166. package/skills/ios/swiftui-performance-audit/references/report-template.md +47 -0
  167. package/skills/ios/swiftui-performance-audit/references/understanding-hangs-in-your-app.md +33 -0
  168. package/skills/ios/swiftui-performance-audit/references/understanding-improving-swiftui-performance.md +52 -0
  169. package/skills/ios/swiftui-pro/LICENSE +21 -0
  170. package/skills/ios/swiftui-pro/SKILL.md +108 -0
  171. package/skills/ios/swiftui-pro/agents/openai.yaml +10 -0
  172. package/skills/ios/swiftui-pro/assets/swiftui-pro-icon.png +0 -0
  173. package/skills/ios/swiftui-pro/assets/swiftui-pro-icon.svg +29 -0
  174. package/skills/ios/swiftui-pro/references/accessibility.md +13 -0
  175. package/skills/ios/swiftui-pro/references/api.md +39 -0
  176. package/skills/ios/swiftui-pro/references/data.md +43 -0
  177. package/skills/ios/swiftui-pro/references/design.md +31 -0
  178. package/skills/ios/swiftui-pro/references/hygiene.md +9 -0
  179. package/skills/ios/swiftui-pro/references/navigation.md +14 -0
  180. package/skills/ios/swiftui-pro/references/performance.md +46 -0
  181. package/skills/ios/swiftui-pro/references/swift.md +56 -0
  182. package/skills/ios/swiftui-pro/references/views.md +35 -0
  183. package/skills/ios/swiftui-ui-patterns/LICENSE +21 -0
  184. package/skills/ios/swiftui-ui-patterns/SKILL.md +100 -0
  185. package/skills/ios/swiftui-ui-patterns/agents/openai.yaml +4 -0
  186. package/skills/ios/swiftui-ui-patterns/references/app-wiring.md +201 -0
  187. package/skills/ios/swiftui-ui-patterns/references/async-state.md +96 -0
  188. package/skills/ios/swiftui-ui-patterns/references/components-index.md +50 -0
  189. package/skills/ios/swiftui-ui-patterns/references/controls.md +57 -0
  190. package/skills/ios/swiftui-ui-patterns/references/deeplinks.md +66 -0
  191. package/skills/ios/swiftui-ui-patterns/references/focus.md +90 -0
  192. package/skills/ios/swiftui-ui-patterns/references/form.md +97 -0
  193. package/skills/ios/swiftui-ui-patterns/references/grids.md +71 -0
  194. package/skills/ios/swiftui-ui-patterns/references/haptics.md +71 -0
  195. package/skills/ios/swiftui-ui-patterns/references/input-toolbar.md +51 -0
  196. package/skills/ios/swiftui-ui-patterns/references/lightweight-clients.md +93 -0
  197. package/skills/ios/swiftui-ui-patterns/references/list.md +86 -0
  198. package/skills/ios/swiftui-ui-patterns/references/loading-placeholders.md +38 -0
  199. package/skills/ios/swiftui-ui-patterns/references/macos-settings.md +71 -0
  200. package/skills/ios/swiftui-ui-patterns/references/matched-transitions.md +59 -0
  201. package/skills/ios/swiftui-ui-patterns/references/media.md +73 -0
  202. package/skills/ios/swiftui-ui-patterns/references/menu-bar.md +101 -0
  203. package/skills/ios/swiftui-ui-patterns/references/navigationstack.md +159 -0
  204. package/skills/ios/swiftui-ui-patterns/references/overlay.md +45 -0
  205. package/skills/ios/swiftui-ui-patterns/references/performance.md +62 -0
  206. package/skills/ios/swiftui-ui-patterns/references/previews.md +48 -0
  207. package/skills/ios/swiftui-ui-patterns/references/scroll-reveal.md +133 -0
  208. package/skills/ios/swiftui-ui-patterns/references/scrollview.md +87 -0
  209. package/skills/ios/swiftui-ui-patterns/references/searchable.md +71 -0
  210. package/skills/ios/swiftui-ui-patterns/references/sheets.md +155 -0
  211. package/skills/ios/swiftui-ui-patterns/references/split-views.md +72 -0
  212. package/skills/ios/swiftui-ui-patterns/references/tabview.md +114 -0
  213. package/skills/ios/swiftui-ui-patterns/references/theming.md +71 -0
  214. package/skills/ios/swiftui-ui-patterns/references/title-menus.md +93 -0
  215. package/skills/ios/swiftui-ui-patterns/references/top-bar.md +49 -0
  216. package/skills/ios/swiftui-view-refactor/LICENSE +21 -0
  217. package/skills/ios/swiftui-view-refactor/SKILL.md +207 -0
  218. package/skills/ios/swiftui-view-refactor/agents/openai.yaml +4 -0
  219. package/skills/ios/swiftui-view-refactor/references/mv-patterns.md +161 -0
  220. package/skills/ios/widgetkit/LICENSE +131 -0
  221. package/skills/ios/widgetkit/SKILL.md +502 -0
  222. 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.