Skip to content

Commit 2add36b

Browse files
hylkedonkerhcdonker-codecarolinefrasca
authored
Add bridge (#35)
* Add bridge * Pin max version * Pin to max 24.6 --------- Co-authored-by: Hylke Donker <[email protected]> Co-authored-by: Caroline Frasca <[email protected]>
1 parent 3bf1208 commit 2add36b

File tree

5 files changed

+103
-0
lines changed

5 files changed

+103
-0
lines changed

recipes/bridge/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Bridge
2+
[![SAST](https://gitlab.com/hylkedonker/bridge/badges/main/pipeline.svg?job=sast)](https://gitlab.com/hylkedonker/bridge/-/security/dashboard)
3+
<p align="center">
4+
<img src="misc/image.jpeg" width="30%" alt="Bridge">
5+
</p>
6+
7+
This package acts as a bridge between Mojo native types and those from the Python world.
8+
9+
Example usage, to convert a NumPy array to a Mojo tensor:
10+
```mojo
11+
from python import Python
12+
from bridge.numpy import ndarray_to_tensor
13+
14+
var np = Python.import_module("numpy")
15+
np_array = np.array([[1, 2], [3, 4]], dtype=float)
16+
mojo_tensor = ndarray_to_tensor[DType.float64](np_array)
17+
```
18+
19+
# Installation
20+
Add `https://repo.prefix.dev/modular-community` to the channels section of your of your
21+
`mojoproject.toml` file.
22+
Then run:
23+
```bash
24+
magic add bridge
25+
```
26+
27+
# Dependencies
28+
Requires numpy and mojo.
29+
30+
# Contributing
31+
Please refer to the [contribution guidelines](https://gitlab.com/hylkedonker/bridge/-/blob/main/CONTRIBUTING.md) before contributing.
32+
33+
# License
34+
This code is licensed under the terms of the [MIT License](LICENSE.txt).

recipes/bridge/avatar.jpeg

382 KB
Loading

recipes/bridge/image.jpeg

138 KB
Loading

recipes/bridge/recipe.yaml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
context:
2+
version: "0.0.1"
3+
4+
package:
5+
name: "bridge"
6+
version: ${{ version }}
7+
8+
source:
9+
- git: https://gitlab.com/hylkedonker/bridge.git
10+
rev: bcbd2e6fd51c6187351d2e2f69c136c3c50253a6
11+
12+
build:
13+
number: 0
14+
script:
15+
- mojo package src/bridge -o ${{ PREFIX }}/lib/mojo/bridge.mojopkg
16+
requirements:
17+
host:
18+
- max=24.6
19+
run:
20+
- ${{ pin_compatible('max') }}
21+
22+
tests:
23+
- script:
24+
- if: unix
25+
then:
26+
- mojo test test_numpy.🔥
27+
files:
28+
recipe:
29+
- test_numpy.🔥
30+
about:
31+
homepage: https://gitlab.com/hylkedonker/bridge
32+
# Remember to specify the license variants for BSD, Apache, GPL, and LGPL.
33+
# Use the SPDX identifier, e.g: GPL-2.0-only instead of GNU General Public License version 2.0
34+
# See https://spdx.org/licenses/
35+
license: MIT
36+
# It is strongly encouraged to include a license file in the package,
37+
# (even if the license doesn't require it) using the license_file entry.
38+
# See https://docs.conda.io/projects/conda-build/en/latest/resources/define-metadata.html#license-file
39+
license_file: LICENSE.txt
40+
summary: Convert (bridge) Python objects to Mojo and vice versa.
41+
repository: https://gitlab.com/hylkedonker/bridge
42+
43+
extra:
44+
maintainers:
45+
- hylkedonker
46+
project_name: Bridge

recipes/bridge/test_numpy.🔥

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from python import Python
2+
from tensor import Tensor, rand
3+
from testing import assert_true, assert_equal
4+
5+
from bridge.numpy import ndarray_to_tensor, tensor_to_ndarray
6+
7+
8+
def test_tensor_identity_transformation():
9+
"""Test that `ndarray_to_tensor` is inverse of `tensor_to_ndarray`."""
10+
var values = List[Float64](1.0, 2.0, 3.0, 4.0, 5.0, 6.0)
11+
var in_matrix = Tensor[DType.float64](shape=(3, 2), list=values)
12+
var np_array = tensor_to_ndarray(in_matrix)
13+
var out_matrix = ndarray_to_tensor[DType.float64](np_array)
14+
assert_equal(in_matrix, out_matrix)
15+
16+
17+
def test_numpy_identity_transformation():
18+
"""Test that `tensor_to_ndarray` is inverse of `ndarray_to_tensor`."""
19+
var np = Python.import_module("numpy")
20+
var in_array = np.arange(6).reshape(3, 2)
21+
var tensor = ndarray_to_tensor[DType.float64](in_array)
22+
var out_array = tensor_to_ndarray(tensor)
23+
assert_equal(in_array, out_array)

0 commit comments

Comments
 (0)