pycoreml 0.1.0__tar.gz
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.
- pycoreml-0.1.0/LICENSE +28 -0
- pycoreml-0.1.0/PKG-INFO +67 -0
- pycoreml-0.1.0/README.md +51 -0
- pycoreml-0.1.0/pycoreml/__init__.py +54 -0
- pycoreml-0.1.0/pycoreml/examples/demo.py +74 -0
- pycoreml-0.1.0/pycoreml.egg-info/PKG-INFO +67 -0
- pycoreml-0.1.0/pycoreml.egg-info/SOURCES.txt +10 -0
- pycoreml-0.1.0/pycoreml.egg-info/dependency_links.txt +1 -0
- pycoreml-0.1.0/pycoreml.egg-info/top_level.txt +1 -0
- pycoreml-0.1.0/pyproject.toml +24 -0
- pycoreml-0.1.0/setup.cfg +4 -0
- pycoreml-0.1.0/setup.py +46 -0
pycoreml-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
BSD 3-Clause License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024, Jagtesh Chadha
|
|
4
|
+
|
|
5
|
+
Redistribution and use in source and binary forms, with or without
|
|
6
|
+
modification, are permitted provided that the following conditions are met:
|
|
7
|
+
|
|
8
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
|
9
|
+
list of conditions and the following disclaimer.
|
|
10
|
+
|
|
11
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
12
|
+
this list of conditions and the following disclaimer in the documentation
|
|
13
|
+
and/or other materials provided with the distribution.
|
|
14
|
+
|
|
15
|
+
3. Neither the name of the copyright holder nor the names of its
|
|
16
|
+
contributors may be used to endorse or promote products derived from
|
|
17
|
+
this software without specific prior written permission.
|
|
18
|
+
|
|
19
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
20
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
21
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
22
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
23
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
24
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
25
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
26
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
27
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
28
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
pycoreml-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pycoreml
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: CoreML inference from Python — powered by Swift & ApplePy
|
|
5
|
+
Author: Jagtesh Chadha
|
|
6
|
+
License: BSD-3-Clause
|
|
7
|
+
Project-URL: Repository, https://github.com/jagtesh/pycoreml
|
|
8
|
+
Classifier: Development Status :: 3 - Alpha
|
|
9
|
+
Classifier: Operating System :: MacOS
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
12
|
+
Requires-Python: >=3.10
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
License-File: LICENSE
|
|
15
|
+
Dynamic: license-file
|
|
16
|
+
|
|
17
|
+
# PyCoreML
|
|
18
|
+
|
|
19
|
+
CoreML inference from Python — powered by Swift & [ApplePy](../ApplePy).
|
|
20
|
+
|
|
21
|
+
Direct access to Apple's CoreML framework with explicit compute unit selection (CPU, GPU, Neural Engine).
|
|
22
|
+
|
|
23
|
+
> **macOS only** — requires Swift 6.0+ toolchain
|
|
24
|
+
|
|
25
|
+
## Install
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
pip install -e .
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Usage
|
|
32
|
+
|
|
33
|
+
```python
|
|
34
|
+
import pycoreml
|
|
35
|
+
|
|
36
|
+
# Load and describe a model
|
|
37
|
+
info = pycoreml.load_model("/path/to/model.mlmodelc")
|
|
38
|
+
desc = pycoreml.model_description("/path/to/model.mlmodelc")
|
|
39
|
+
|
|
40
|
+
# Run prediction
|
|
41
|
+
result = pycoreml.predict("/path/to/model.mlmodelc", {"x": 42.0})
|
|
42
|
+
|
|
43
|
+
# Predict with explicit compute unit (the killer feature)
|
|
44
|
+
result = pycoreml.predict_with_options(
|
|
45
|
+
"/path/to/model.mlmodelc",
|
|
46
|
+
{"x": 42.0},
|
|
47
|
+
"cpuAndNeuralEngine" # or "cpuOnly", "cpuAndGPU", "all"
|
|
48
|
+
)
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## API
|
|
52
|
+
|
|
53
|
+
| Function | Returns | Description |
|
|
54
|
+
|----------|---------|-------------|
|
|
55
|
+
| `load_model(path)` | `str` | Load model and return description |
|
|
56
|
+
| `model_description(path)` | `dict[str, str]` | Model metadata |
|
|
57
|
+
| `predict(path, inputs)` | `dict[str, float]` | Run inference |
|
|
58
|
+
| `list_compute_units()` | `list[str]` | Available compute units |
|
|
59
|
+
| `predict_with_options(path, inputs, unit)` | `dict[str, float]` | Predict with compute unit control |
|
|
60
|
+
|
|
61
|
+
## Examples
|
|
62
|
+
|
|
63
|
+
See [`pycoreml/examples/demo.py`](pycoreml/examples/demo.py) for a full demo.
|
|
64
|
+
|
|
65
|
+
## License
|
|
66
|
+
|
|
67
|
+
BSD-3-Clause © Jagtesh Chadha — see [LICENSE](LICENSE).
|
pycoreml-0.1.0/README.md
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# PyCoreML
|
|
2
|
+
|
|
3
|
+
CoreML inference from Python — powered by Swift & [ApplePy](../ApplePy).
|
|
4
|
+
|
|
5
|
+
Direct access to Apple's CoreML framework with explicit compute unit selection (CPU, GPU, Neural Engine).
|
|
6
|
+
|
|
7
|
+
> **macOS only** — requires Swift 6.0+ toolchain
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
pip install -e .
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
```python
|
|
18
|
+
import pycoreml
|
|
19
|
+
|
|
20
|
+
# Load and describe a model
|
|
21
|
+
info = pycoreml.load_model("/path/to/model.mlmodelc")
|
|
22
|
+
desc = pycoreml.model_description("/path/to/model.mlmodelc")
|
|
23
|
+
|
|
24
|
+
# Run prediction
|
|
25
|
+
result = pycoreml.predict("/path/to/model.mlmodelc", {"x": 42.0})
|
|
26
|
+
|
|
27
|
+
# Predict with explicit compute unit (the killer feature)
|
|
28
|
+
result = pycoreml.predict_with_options(
|
|
29
|
+
"/path/to/model.mlmodelc",
|
|
30
|
+
{"x": 42.0},
|
|
31
|
+
"cpuAndNeuralEngine" # or "cpuOnly", "cpuAndGPU", "all"
|
|
32
|
+
)
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## API
|
|
36
|
+
|
|
37
|
+
| Function | Returns | Description |
|
|
38
|
+
|----------|---------|-------------|
|
|
39
|
+
| `load_model(path)` | `str` | Load model and return description |
|
|
40
|
+
| `model_description(path)` | `dict[str, str]` | Model metadata |
|
|
41
|
+
| `predict(path, inputs)` | `dict[str, float]` | Run inference |
|
|
42
|
+
| `list_compute_units()` | `list[str]` | Available compute units |
|
|
43
|
+
| `predict_with_options(path, inputs, unit)` | `dict[str, float]` | Predict with compute unit control |
|
|
44
|
+
|
|
45
|
+
## Examples
|
|
46
|
+
|
|
47
|
+
See [`pycoreml/examples/demo.py`](pycoreml/examples/demo.py) for a full demo.
|
|
48
|
+
|
|
49
|
+
## License
|
|
50
|
+
|
|
51
|
+
BSD-3-Clause © Jagtesh Chadha — see [LICENSE](LICENSE).
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"""pycoreml — CoreML inference from Python
|
|
2
|
+
|
|
3
|
+
Powered by Swift & ApplePy. Direct access to Apple's CoreML framework
|
|
4
|
+
with explicit compute unit selection (CPU, GPU, Neural Engine).
|
|
5
|
+
|
|
6
|
+
Usage:
|
|
7
|
+
import pycoreml
|
|
8
|
+
desc = pycoreml.load_model("/path/to/model.mlmodelc")
|
|
9
|
+
result = pycoreml.predict("/path/to/model.mlmodelc", {"x": 42.0})
|
|
10
|
+
"""
|
|
11
|
+
import importlib
|
|
12
|
+
import os
|
|
13
|
+
import sys
|
|
14
|
+
|
|
15
|
+
if sys.platform != "darwin":
|
|
16
|
+
raise ImportError("pycoreml only supports macOS")
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def _load_native():
|
|
20
|
+
"""Load the compiled Swift extension module."""
|
|
21
|
+
pkg_dir = os.path.dirname(os.path.abspath(__file__))
|
|
22
|
+
so_path = os.path.join(pkg_dir, "pycoreml.so")
|
|
23
|
+
|
|
24
|
+
if not os.path.exists(so_path):
|
|
25
|
+
raise ImportError(
|
|
26
|
+
"Native extension not found. Build it first:\n"
|
|
27
|
+
" pip install -e .\n"
|
|
28
|
+
" # or: python setup.py build_ext --inplace"
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
spec = importlib.util.spec_from_file_location("pycoreml", so_path)
|
|
32
|
+
mod = importlib.util.module_from_spec(spec)
|
|
33
|
+
spec.loader.exec_module(mod)
|
|
34
|
+
return mod
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
_native = _load_native()
|
|
38
|
+
|
|
39
|
+
# Re-export all public functions
|
|
40
|
+
load_model = _native.load_model
|
|
41
|
+
model_description = _native.model_description
|
|
42
|
+
predict = _native.predict
|
|
43
|
+
list_compute_units = _native.list_compute_units
|
|
44
|
+
predict_with_options = _native.predict_with_options
|
|
45
|
+
|
|
46
|
+
__all__ = [
|
|
47
|
+
"load_model",
|
|
48
|
+
"model_description",
|
|
49
|
+
"predict",
|
|
50
|
+
"list_compute_units",
|
|
51
|
+
"predict_with_options",
|
|
52
|
+
]
|
|
53
|
+
|
|
54
|
+
__version__ = "0.1.0"
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""PyCoreML — Example Usage
|
|
3
|
+
|
|
4
|
+
Demonstrates CoreML inference from Python via ApplePy.
|
|
5
|
+
Requires a compiled CoreML model (.mlmodelc) to run predictions.
|
|
6
|
+
"""
|
|
7
|
+
import pycoreml
|
|
8
|
+
|
|
9
|
+
# ── Available Compute Units ─────────────────────────────────
|
|
10
|
+
|
|
11
|
+
print("=== Available Compute Units ===\n")
|
|
12
|
+
|
|
13
|
+
units = pycoreml.list_compute_units()
|
|
14
|
+
for unit in units:
|
|
15
|
+
print(f" • {unit}")
|
|
16
|
+
|
|
17
|
+
# ── Loading a Model ─────────────────────────────────────────
|
|
18
|
+
|
|
19
|
+
print("\n=== Model Loading ===\n")
|
|
20
|
+
|
|
21
|
+
# To run this demo, you need a compiled CoreML model.
|
|
22
|
+
# You can create one with coremltools:
|
|
23
|
+
#
|
|
24
|
+
# import coremltools as ct
|
|
25
|
+
# import torch
|
|
26
|
+
#
|
|
27
|
+
# class SimpleModel(torch.nn.Module):
|
|
28
|
+
# def forward(self, x):
|
|
29
|
+
# return x * 2 + 1
|
|
30
|
+
#
|
|
31
|
+
# model = ct.convert(torch.jit.trace(SimpleModel(), torch.tensor([1.0])))
|
|
32
|
+
# model.save("simple.mlpackage")
|
|
33
|
+
#
|
|
34
|
+
# Then compile it:
|
|
35
|
+
# xcrun coremlcompiler compile simple.mlpackage .
|
|
36
|
+
|
|
37
|
+
MODEL_PATH = "simple.mlmodelc" # Change to your model path
|
|
38
|
+
|
|
39
|
+
try:
|
|
40
|
+
# Load and describe model
|
|
41
|
+
info = pycoreml.load_model(MODEL_PATH)
|
|
42
|
+
print(info)
|
|
43
|
+
|
|
44
|
+
# Get metadata
|
|
45
|
+
desc = pycoreml.model_description(MODEL_PATH)
|
|
46
|
+
print(f"\nMetadata: {desc}")
|
|
47
|
+
|
|
48
|
+
# ── Running Predictions ─────────────────────────────────
|
|
49
|
+
|
|
50
|
+
print("\n=== Predictions ===\n")
|
|
51
|
+
|
|
52
|
+
# Basic prediction
|
|
53
|
+
result = pycoreml.predict(MODEL_PATH, {"x": 42.0})
|
|
54
|
+
print(f"predict(x=42.0) → {result}")
|
|
55
|
+
|
|
56
|
+
# Prediction with explicit compute unit
|
|
57
|
+
result = pycoreml.predict_with_options(
|
|
58
|
+
MODEL_PATH,
|
|
59
|
+
{"x": 42.0},
|
|
60
|
+
"cpuAndNeuralEngine"
|
|
61
|
+
)
|
|
62
|
+
print(f"predict(x=42.0, compute_unit='cpuAndNeuralEngine') → {result}")
|
|
63
|
+
|
|
64
|
+
# Compare compute units
|
|
65
|
+
print("\n=== Compute Unit Comparison ===\n")
|
|
66
|
+
for unit in ["cpuOnly", "cpuAndGPU", "cpuAndNeuralEngine", "all"]:
|
|
67
|
+
result = pycoreml.predict_with_options(MODEL_PATH, {"x": 42.0}, unit)
|
|
68
|
+
print(f" {unit:25s} → {result}")
|
|
69
|
+
|
|
70
|
+
except Exception as e:
|
|
71
|
+
print(f"⚠ Skipping model demo: {e}")
|
|
72
|
+
print(" (Provide a valid .mlmodelc path to run predictions)")
|
|
73
|
+
|
|
74
|
+
print("\n🎉 Done!")
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pycoreml
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: CoreML inference from Python — powered by Swift & ApplePy
|
|
5
|
+
Author: Jagtesh Chadha
|
|
6
|
+
License: BSD-3-Clause
|
|
7
|
+
Project-URL: Repository, https://github.com/jagtesh/pycoreml
|
|
8
|
+
Classifier: Development Status :: 3 - Alpha
|
|
9
|
+
Classifier: Operating System :: MacOS
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
12
|
+
Requires-Python: >=3.10
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
License-File: LICENSE
|
|
15
|
+
Dynamic: license-file
|
|
16
|
+
|
|
17
|
+
# PyCoreML
|
|
18
|
+
|
|
19
|
+
CoreML inference from Python — powered by Swift & [ApplePy](../ApplePy).
|
|
20
|
+
|
|
21
|
+
Direct access to Apple's CoreML framework with explicit compute unit selection (CPU, GPU, Neural Engine).
|
|
22
|
+
|
|
23
|
+
> **macOS only** — requires Swift 6.0+ toolchain
|
|
24
|
+
|
|
25
|
+
## Install
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
pip install -e .
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Usage
|
|
32
|
+
|
|
33
|
+
```python
|
|
34
|
+
import pycoreml
|
|
35
|
+
|
|
36
|
+
# Load and describe a model
|
|
37
|
+
info = pycoreml.load_model("/path/to/model.mlmodelc")
|
|
38
|
+
desc = pycoreml.model_description("/path/to/model.mlmodelc")
|
|
39
|
+
|
|
40
|
+
# Run prediction
|
|
41
|
+
result = pycoreml.predict("/path/to/model.mlmodelc", {"x": 42.0})
|
|
42
|
+
|
|
43
|
+
# Predict with explicit compute unit (the killer feature)
|
|
44
|
+
result = pycoreml.predict_with_options(
|
|
45
|
+
"/path/to/model.mlmodelc",
|
|
46
|
+
{"x": 42.0},
|
|
47
|
+
"cpuAndNeuralEngine" # or "cpuOnly", "cpuAndGPU", "all"
|
|
48
|
+
)
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## API
|
|
52
|
+
|
|
53
|
+
| Function | Returns | Description |
|
|
54
|
+
|----------|---------|-------------|
|
|
55
|
+
| `load_model(path)` | `str` | Load model and return description |
|
|
56
|
+
| `model_description(path)` | `dict[str, str]` | Model metadata |
|
|
57
|
+
| `predict(path, inputs)` | `dict[str, float]` | Run inference |
|
|
58
|
+
| `list_compute_units()` | `list[str]` | Available compute units |
|
|
59
|
+
| `predict_with_options(path, inputs, unit)` | `dict[str, float]` | Predict with compute unit control |
|
|
60
|
+
|
|
61
|
+
## Examples
|
|
62
|
+
|
|
63
|
+
See [`pycoreml/examples/demo.py`](pycoreml/examples/demo.py) for a full demo.
|
|
64
|
+
|
|
65
|
+
## License
|
|
66
|
+
|
|
67
|
+
BSD-3-Clause © Jagtesh Chadha — see [LICENSE](LICENSE).
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
pycoreml
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68.0"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "pycoreml"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "CoreML inference from Python — powered by Swift & ApplePy"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = {text = "BSD-3-Clause"}
|
|
11
|
+
authors = [{name = "Jagtesh Chadha"}]
|
|
12
|
+
requires-python = ">=3.10"
|
|
13
|
+
classifiers = [
|
|
14
|
+
"Development Status :: 3 - Alpha",
|
|
15
|
+
"Operating System :: MacOS",
|
|
16
|
+
"Programming Language :: Python :: 3",
|
|
17
|
+
"Topic :: Scientific/Engineering :: Artificial Intelligence",
|
|
18
|
+
]
|
|
19
|
+
|
|
20
|
+
[project.urls]
|
|
21
|
+
Repository = "https://github.com/jagtesh/pycoreml"
|
|
22
|
+
|
|
23
|
+
[tool.setuptools.packages.find]
|
|
24
|
+
include = ["pycoreml*"]
|
pycoreml-0.1.0/setup.cfg
ADDED
pycoreml-0.1.0/setup.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"""PyCoreML build — compiles Swift source into a Python-loadable .so"""
|
|
2
|
+
import os
|
|
3
|
+
import subprocess
|
|
4
|
+
import sys
|
|
5
|
+
import sysconfig
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
from setuptools import setup
|
|
9
|
+
from setuptools.command.build_ext import build_ext
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class SwiftBuildExt(build_ext):
|
|
13
|
+
"""Custom build_ext that calls `swift build` to compile the Swift extension."""
|
|
14
|
+
|
|
15
|
+
def run(self):
|
|
16
|
+
if sys.platform != "darwin":
|
|
17
|
+
raise RuntimeError("pycoreml only supports macOS")
|
|
18
|
+
|
|
19
|
+
swift_dir = Path(__file__).parent / "swift"
|
|
20
|
+
pkg_config_path = sysconfig.get_config_var("LIBPC") or ""
|
|
21
|
+
|
|
22
|
+
env = os.environ.copy()
|
|
23
|
+
env["PKG_CONFIG_PATH"] = pkg_config_path
|
|
24
|
+
|
|
25
|
+
print("🔨 Building Swift extension...")
|
|
26
|
+
subprocess.check_call(
|
|
27
|
+
["swift", "build"],
|
|
28
|
+
cwd=swift_dir,
|
|
29
|
+
env=env,
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
build_dir = swift_dir / ".build" / "debug"
|
|
33
|
+
dylib = build_dir / "libPyCoreML.dylib"
|
|
34
|
+
if not dylib.exists():
|
|
35
|
+
raise RuntimeError(f"Build succeeded but {dylib} not found")
|
|
36
|
+
|
|
37
|
+
dest = Path(__file__).parent / "pycoreml" / "pycoreml.so"
|
|
38
|
+
print(f"📦 Installing {dylib.name} → {dest}")
|
|
39
|
+
import shutil
|
|
40
|
+
shutil.copy2(dylib, dest)
|
|
41
|
+
|
|
42
|
+
def get_ext_filename(self, ext_name):
|
|
43
|
+
return ext_name + ".so"
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
setup(cmdclass={"build_ext": SwiftBuildExt})
|