Skip to content

Commit 29ad374

Browse files
authored
8.1 Release (#2394)
1 parent 68b4f5a commit 29ad374

File tree

2,775 files changed

+657165
-432963
lines changed

Some content is hidden

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

2,775 files changed

+657165
-432963
lines changed

.gitlab-ci.yml

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ test_py310_coremltools_test:
190190
PYTHON: "3.10"
191191
REQUIREMENTS: reqs/test.pip
192192

193-
test_py310_pytorch:
193+
test_py310_pytorch_script:
194194
<<: *test_macos_pkg
195195
tags:
196196
- macOS_M1
@@ -201,6 +201,33 @@ test_py310_pytorch:
201201
TEST_PACKAGE: coremltools.converters.mil.frontend.torch
202202
WHEEL_PATH: build/dist/*cp310*11*
203203
REQUIREMENTS: reqs/test.pip
204+
TORCH_FRONTENDS: TORCHSCRIPT
205+
206+
test_py310_pytorch_export:
207+
<<: *test_macos_pkg
208+
tags:
209+
- macOS_M1
210+
dependencies:
211+
- build_wheel_macos_py310
212+
variables:
213+
PYTHON: "3.10"
214+
TEST_PACKAGE: coremltools.converters.mil.frontend.torch
215+
WHEEL_PATH: build/dist/*cp310*11*
216+
REQUIREMENTS: reqs/test.pip
217+
TORCH_FRONTENDS: TORCHEXPORT
218+
219+
test_py310_pytorch_executorch:
220+
<<: *test_macos_pkg
221+
tags:
222+
- macOS_M1
223+
dependencies:
224+
- build_wheel_macos_py310
225+
variables:
226+
PYTHON: "3.10"
227+
TEST_PACKAGE: coremltools.converters.mil.frontend.torch
228+
WHEEL_PATH: build/dist/*cp310*11*
229+
REQUIREMENTS: reqs/test.pip
230+
TORCH_FRONTENDS: EXECUTORCH
204231

205232
test_py310_tf2-1:
206233
<<: *test_macos_pkg

BUILDING.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Follow these steps:
1919
1. Fork and clone the GitHub [coremltools repository](https://github.com/apple/coremltools).
2020

2121
2. Run the [build.sh](scripts/build.sh) script to build `coremltools`.
22-
* By default this script uses Python 3.7, but you can include `--python=3.8` (or `3.9`, `3.10`, `3.11`) as a argument to change the Python version.
22+
* By default this script uses Python 3.7, but you can include `--python=3.8` (or `3.9`, `3.10`, `3.11`, `3.12`) as a argument to change the Python version.
2323
* The script creates a new `build` folder with the coremltools distribution, and a `dist` folder with Python wheel files.
2424

2525
3. Run the [test.sh](scripts/test.sh) script to test the build.
@@ -45,7 +45,7 @@ The following build targets help you configure the development environment. If y
4545
* `test_slow` | Run all non-fast tests.
4646
* `wheel` | Build wheels in release mode.
4747

48-
The script uses Python 3.7, but you can include `--python=3.8` (or `3.9`, `3.10`, `3.11`) as a argument to change the Python version.
48+
The script uses Python 3.7, but you can include `--python=3.8` (or `3.9`, `3.10`, `3.11`, `3.12`) as a argument to change the Python version.
4949

5050
## Resources
5151

coremlpython/CoreMLPython.h

Lines changed: 181 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
#pragma clang diagnostic pop
1515

1616
#import <CoreML/CoreML.h>
17+
#import <Availability.h>
1718

19+
#import <vector>
1820

1921
#ifndef BUILT_WITH_MACOS15_SDK
2022
#define BUILT_WITH_MACOS15_SDK \
@@ -28,25 +30,181 @@
2830
#pragma message ("Building without macOS 15 SDK")
2931
#endif
3032

33+
#if !defined(ML_COMPUTE_PLAN_IS_AVAILABLE) && __has_include(<CoreML/MLComputePlan.h>)
34+
#define ML_COMPUTE_PLAN_IS_AVAILABLE 1
35+
#endif
36+
37+
#if !defined(ML_MODEL_STRUCTURE_IS_AVAILABLE) && __has_include(<CoreML/MLModelStructure.h>)
38+
#define ML_MODEL_STRUCTURE_IS_AVAILABLE 1
39+
#endif
40+
41+
#if !defined(ML_COMPUTE_DEVICE_IS_AVAILABLE) && __has_include(<CoreML/MLComputeDeviceProtocol.h>)
42+
#define ML_COMPUTE_DEVICE_IS_AVAILABLE 1
43+
#endif
44+
45+
#if !defined(ML_MODEL_ASSET_IS_AVAILABLE) && __has_include(<CoreML/MLModelAsset.h>)
46+
#define ML_MODEL_ASSET_IS_AVAILABLE 1
47+
#endif
48+
49+
#if !defined(ML_STATE_IS_AVAILABLE) && __has_include(<CoreML/MLState.h>)
50+
#define ML_STATE_IS_AVAILABLE 1
51+
#endif
52+
3153

3254
namespace py = pybind11;
3355

3456
namespace CoreML {
3557
namespace Python {
36-
37-
3858
struct State {
39-
#if BUILT_WITH_MACOS15_SDK
4059
// MLState must be wrapped in a C++ class for PyBind.
41-
MLState* m_state = nil;
60+
inline State(id impl):
61+
m_impl(impl) {}
62+
63+
#if ML_STATE_IS_AVAILABLE
64+
API_AVAILABLE(macos(15.0))
65+
inline MLState *getImpl() const {
66+
return (MLState *)m_impl;
67+
}
4268
#endif
69+
70+
private:
71+
// Type erase `m_impl` otherwise it will result in a compiler warning.
72+
id m_impl = nil;
73+
};
74+
75+
struct CPUComputeDevice {
76+
// MLCPUComputeDevice must be wrapped in a C++ class for PyBind.
77+
inline CPUComputeDevice(id impl):
78+
m_impl(impl) {}
79+
80+
#if ML_COMPUTE_DEVICE_IS_AVAILABLE
81+
API_AVAILABLE(macos(14.0))
82+
inline MLCPUComputeDevice *getImpl() const {
83+
return (MLCPUComputeDevice *)m_impl;
84+
}
85+
#endif
86+
87+
private:
88+
// Type erase `m_impl` otherwise it will result in a compiler warning.
89+
id m_impl = nil;
90+
};
91+
92+
struct GPUComputeDevice {
93+
// MLGPUComputeDevice must be wrapped in a C++ class for PyBind.
94+
inline GPUComputeDevice(id impl):
95+
m_impl(impl) {}
96+
97+
#if ML_COMPUTE_DEVICE_IS_AVAILABLE
98+
API_AVAILABLE(macos(14.0))
99+
inline MLGPUComputeDevice *getImpl() const {
100+
return (MLGPUComputeDevice *)m_impl;
101+
}
102+
#endif
103+
104+
private:
105+
// Type erase `m_impl` otherwise it will result in a compiler warning.
106+
id m_impl = nil;
107+
};
108+
109+
struct NeuralEngineComputeDevice {
110+
// MLNeuralEngineComputeDevice must be wrapped in a C++ class for PyBind.
111+
inline NeuralEngineComputeDevice(id impl):
112+
m_impl(impl) {}
113+
114+
#if ML_COMPUTE_DEVICE_IS_AVAILABLE
115+
API_AVAILABLE(macos(14.0))
116+
inline MLNeuralEngineComputeDevice *getImpl() const {
117+
return (MLNeuralEngineComputeDevice *)m_impl;
118+
}
119+
#endif
120+
121+
int getTotalCoreCount() const;
122+
123+
private:
124+
// Type erase `m_impl` otherwise it will result in a compiler warning.
125+
id m_impl = nil;
126+
};
127+
128+
struct ModelStructureProgramOperation {
129+
// MLModelStructureProgramOperation must be wrapped in a C++ class for PyBind.
130+
inline ModelStructureProgramOperation(id impl):
131+
m_impl(impl) {}
132+
133+
#if ML_MODEL_STRUCTURE_IS_AVAILABLE
134+
API_AVAILABLE(macos(14.4))
135+
inline MLModelStructureProgramOperation *getImpl() const {
136+
return (MLModelStructureProgramOperation *)m_impl;
137+
}
138+
#endif
139+
140+
private:
141+
// Type erase `m_impl` otherwise it will result in a compiler warning.
142+
__weak id m_impl = nil;
143+
};
144+
145+
struct ModelStructureNeuralNetworkLayer {
146+
// ModelStructureNeuralNetworkLayer must be wrapped in a C++ class for PyBind.
147+
inline ModelStructureNeuralNetworkLayer(id impl):
148+
m_impl(impl) {}
149+
150+
#if ML_MODEL_STRUCTURE_IS_AVAILABLE
151+
API_AVAILABLE(macos(14.4))
152+
inline MLModelStructureNeuralNetworkLayer *getImpl() const {
153+
return (MLModelStructureNeuralNetworkLayer *)m_impl;
154+
}
155+
#endif
156+
157+
private:
158+
// Type erase `m_impl` otherwise it will result in a compiler warning.
159+
__weak id m_impl = nil;
160+
};
161+
162+
struct ComputePlan {
163+
// MLComputePlan must be wrapped in a C++ class for PyBind.
164+
inline ComputePlan(id impl, py::object modelStructure):
165+
m_impl(impl),
166+
m_modelStructure(modelStructure) {}
167+
168+
inline py::object getModelStructure() const {
169+
return m_modelStructure;
170+
}
171+
172+
#if ML_COMPUTE_PLAN_IS_AVAILABLE
173+
API_AVAILABLE(macos(14.4))
174+
inline MLComputePlan *getImpl() const {
175+
return (MLComputePlan *)m_impl;
176+
}
177+
178+
py::object getComputeDeviceUsageForMLProgramOperation(py::object operation);
179+
py::object getComputeDeviceUsageForNeuralNetworkLayer(py::object layer);
180+
py::object getEstimatedCostForMLProgramOperation(py::object operation);
181+
#endif
182+
183+
private:
184+
id m_impl = nil;
185+
py::object m_modelStructure;
186+
};
187+
188+
struct ModelAsset {
189+
// MLModelAsset must be wrapped in a C++ class for PyBind.
190+
inline ModelAsset(id impl, std::vector<py::bytes> datas):
191+
m_impl(impl),
192+
m_datas(std::move(datas)) {}
193+
194+
API_AVAILABLE(macos(13.0))
195+
inline MLModelAsset *getImpl() const {
196+
return (MLModelAsset *)m_impl;
197+
}
198+
199+
id m_impl = nil;
200+
std::vector<py::bytes> m_datas;
43201
};
44202

45203
class Model {
46204
private:
47205
MLModel *m_model = nil;
48206
NSURL *compiledUrl = nil;
49-
bool m_deleteCompiledModelOnExit;
207+
bool m_deleteCompiledModelOnExit = false;
50208

51209
public:
52210
static py::bytes autoSetSpecificationVersion(const py::bytes& modelBytes);
@@ -57,7 +215,18 @@ namespace CoreML {
57215
Model(const Model&) = delete;
58216
Model& operator=(const Model&) = delete;
59217
~Model();
60-
explicit Model(const std::string& urlStr, const std::string& computeUnits, const std::string& functionName, const py::dict& optimizationHints);
218+
explicit Model(const std::string& urlStr,
219+
const std::string& computeUnits,
220+
const std::string& functionName,
221+
const py::dict& optimizationHints,
222+
const py::object& asset);
223+
224+
explicit Model(const std::string& urlStr,
225+
const std::string& computeUnits,
226+
const std::string& functionName,
227+
const py::dict& optimizationHints);
228+
229+
61230
explicit Model(MLModel* m_model, NSURL* compiledUrl, bool deleteCompiledModelOnExit);
62231

63232
py::list batchPredict(const py::list& batch) const;
@@ -71,6 +240,12 @@ namespace CoreML {
71240
State newState() const;
72241
#endif
73242

243+
static py::object createModelAssetFromPath(const std::string& path);
244+
static py::object createModelAssetFromMemory(const py::bytes& specData, const py::dict& blobMapping);
245+
static py::object getModelStructure(const std::string& modelPath);
246+
static py::list getAvailableComputeDevices();
247+
static py::list getAllComputeDevices();
248+
static py::object getComputePlan(const std::string& modelPath, const std::string& computeUnits);
74249
};
75250
}
76251
}

0 commit comments

Comments
 (0)