Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial support #1

Open
wants to merge 57 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
9516482
initial commit
dc-dc-dc Jan 19, 2024
1c344d1
small cleanup
dc-dc-dc Jan 19, 2024
bf2a0b5
squashed bug
dc-dc-dc Jan 19, 2024
93b3cc2
layer norm
dc-dc-dc Jan 19, 2024
359b977
slice op
dc-dc-dc Jan 19, 2024
c81c052
fix string decode
dc-dc-dc Jan 19, 2024
09e8a0a
add pre-commit
dc-dc-dc Jan 19, 2024
bafdec2
conv 1d/2d support
dc-dc-dc Jan 19, 2024
1e732a3
added mod and optionals, removed failing tests
dc-dc-dc Jan 19, 2024
4a92e5b
isinf / isnan
dc-dc-dc Jan 19, 2024
2b50152
exclude more ops, added some easy ones
dc-dc-dc Jan 19, 2024
89d788a
split things up so that its more readable
dc-dc-dc Jan 20, 2024
c325d1c
no more errors in tests
dc-dc-dc Jan 20, 2024
adace5b
rm from git, not ready
dc-dc-dc Jan 20, 2024
a494e3f
small cleanup
dc-dc-dc Jan 20, 2024
28aae68
resolve optional tests
dc-dc-dc Jan 20, 2024
620f89b
Sequence ops added
dc-dc-dc Jan 20, 2024
2caf364
max pool
dc-dc-dc Jan 22, 2024
a2cb86c
remove indicies test
dc-dc-dc Jan 22, 2024
177239c
autopad in maxpool
dc-dc-dc Jan 22, 2024
db13d91
add averagepool
dc-dc-dc Jan 22, 2024
f037c2f
properly handle count_include_pad in average pool
dc-dc-dc Jan 22, 2024
97c9892
add padding to conv op
dc-dc-dc Jan 22, 2024
fdb65a4
update readme and added mnist example
dc-dc-dc Jan 22, 2024
2a6d5a2
updated readme with examples
dc-dc-dc Jan 22, 2024
37c0d76
instance norm
dc-dc-dc Jan 22, 2024
c9b6cf3
resolve padding issuesin onnx tests
dc-dc-dc Jan 22, 2024
7873b39
space some code apart
dc-dc-dc Jan 22, 2024
d0bc76e
add topk
dc-dc-dc Jan 22, 2024
f50083d
group norm
dc-dc-dc Jan 23, 2024
01b4107
batchnorm
dc-dc-dc Jan 23, 2024
6a6edcc
resnet example
dc-dc-dc Jan 23, 2024
f640c57
windows
dc-dc-dc Jan 23, 2024
8683969
added DepthToSpace / SpaceToDepth
dc-dc-dc Jan 23, 2024
d609c0c
matmul integer
dc-dc-dc Jan 23, 2024
a967fbe
remove include only
dc-dc-dc Jan 23, 2024
baaf31e
onehot
dc-dc-dc Jan 23, 2024
c849dd4
remove include on one hot
dc-dc-dc Jan 23, 2024
a1bf37d
resolve more tests
dc-dc-dc Jan 23, 2024
349acc9
get model tests running
dc-dc-dc Jan 23, 2024
8f22095
pad constant added
dc-dc-dc Jan 23, 2024
bf57772
compress
dc-dc-dc Jan 23, 2024
95d04e2
resolve prelu bug
dc-dc-dc Jan 23, 2024
24ed2a5
ImageDecoder op
dc-dc-dc Jan 23, 2024
bd50952
moved wrapper to core onnx lib
dc-dc-dc Jan 23, 2024
6695fac
added dtype helper func
dc-dc-dc Jan 23, 2024
cbbb252
fix clip errors
dc-dc-dc Jan 23, 2024
0187593
revert onehot change
dc-dc-dc Jan 23, 2024
26ca8d0
properly handle multiple args
dc-dc-dc Jan 29, 2024
c7c9921
tests are different for some reason
dc-dc-dc Jan 29, 2024
da396de
clear up cast tests
dc-dc-dc Jan 29, 2024
3c1e4af
add support for registering ops if not in core
dc-dc-dc Jan 30, 2024
33bea1c
switch image and use mlx-data in example
dc-dc-dc Jan 31, 2024
b400894
shape tuple update
dc-dc-dc Feb 8, 2024
3811bca
mod is fixed
dc-dc-dc Feb 10, 2024
b5ece0e
assert not fmod
dc-dc-dc Feb 10, 2024
8c50e51
some nits
awni Feb 21, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
repos:
- repo: https://github.com/psf/black-pre-commit-mirror
rev: 24.2.0
hooks:
- id: black
- repo: https://github.com/pycqa/isort
rev: 5.13.2
hooks:
- id: isort
args:
- --profile=black
27 changes: 25 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,25 @@
# mlx-onnx
MLX support for the Open Neural Network Exchange (ONNX)
# MLX ONNX

MLX support for the Open Neural Network Exchange ([ONNX](https://onnx.ai/))

## Install

```shell
pip install mlx-onnx
```

## Usage

```python
from mlx.onnx import MlxBackend
from onnx import hub

model = hub.load("mnist")
backend = MlxBackend(model)
result = backend.run(...) # pass inputs to model
```

## Examples

- [ResNet](./examples/resnet/example.py)
- [Mnist](./examples/mnist/example.py)
19 changes: 19 additions & 0 deletions examples/mnist/example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright © 2024 Apple Inc.

import mlx.core as mx
import numpy as np
from onnx import hub
from PIL import Image

from mlx.onnx import MlxBackend

if __name__ == "__main__":
x = (
mx.array(np.asarray(Image.open("./nine.jpeg")))
.reshape((1, 1, 28, 28))
.astype(mx.float32)
)
model = hub.load("mnist")
backend = MlxBackend(model)
res = backend.run(x)
print(f"It was a {mx.argmax(res[0]).item()}")
Binary file added examples/mnist/five.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/mnist/four.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/mnist/nine.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/resnet/car.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions examples/resnet/example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright © 2024 Apple Inc.

import mlx.core as mx
import mlx.data as dx
import onnx

from mlx.onnx import MlxBackend


def run(image: str):
dataset = (
dx.buffer_from_vector([{"file_name": image.encode()}])
.load_image("file_name", output_key="image")
.image_resize_smallest_side("image", 256)
.image_center_crop("image", 224, 224)
.key_transform("image", lambda x: (x - 127.0) / 128.0)
)
with open("./imagenet_labels.txt") as f:
labels = [l.strip() for l in f.readlines()]

model = onnx.hub.load("resnet50")
backend = MlxBackend(model)
res = []
for data in dataset:
img = mx.array(data["image"]).transpose(2, 0, 1)[None]
x = backend.run(img)[0]
res.append((labels[mx.argmax(x).item()], mx.max(x).item()))
return res


if __name__ == "__main__":
for label, score in run("./car.jpg"):
print(f"Image containes a {label} with score {score:.3f}.")
Loading