diff --git a/recipes/bridge/README.md b/recipes/bridge/README.md deleted file mode 100644 index 07a57222..00000000 --- a/recipes/bridge/README.md +++ /dev/null @@ -1,54 +0,0 @@ -# Bridge -[![SAST](https://gitlab.com/hylkedonker/bridge/badges/main/pipeline.svg?job=sast)](https://gitlab.com/hylkedonker/bridge/-/security/dashboard) -

- Bridge -

- -This package acts as a bridge between Mojo native types and those from the Python world. - -Example usage, to convert a NumPy array to a Mojo `LayoutTensor` or `Tensor`: -```mojo -from python import Python -from bridge.numpy import ndarray_to_layouttensor, ndarray_to_tensor - -var np = Python.import_module("numpy") -np_array = np.arange(6.0).reshape(2,3) - -# Convert to new-style `LayoutTensor`. -mojo_tensor = ndarray_to_layouttensor[order=2](np_array) - -# Convert to old-style `Tensor`. -mojo_oldtensor = ndarray_to_tensor[DType.float64](np_array) -``` -Or to achieve the reverse: -```mojo -from tensor import Tensor - -from bridge.numpy import layouttensor_to_ndarray, tensor_to_ndarray - -# Convert new-style `LayoutTensor` to numpy array. -np_array = layouttensor_to_ndarray(mojo_tensor) - -# Convert old-style `Tensor` to numpy array. -values = List[Float64](0.0, 1.0, 2.0, 3.0, 4.0, 5.0) -mojo_oldtensor = Tensor[DType.float64](shape=(2, 3), list=values) -np_array = tensor_to_ndarray(mojo_oldtensor) -``` - -# Installation -Add the `modular-community` channel to your `mojoproject.toml` file and `bridge` to -your dependencies, by running: -```bash -magic project channel add "https://repo.prefix.dev/modular-community" -magic add bridge -``` -That's it, success! 🎉 - -# Dependencies -Requires numpy and mojo. - -# Contributing -Please refer to the [contribution guidelines](https://gitlab.com/hylkedonker/bridge/-/blob/main/CONTRIBUTING.md) before contributing. - -# License -This code is licensed under the terms of the [MIT License](LICENSE.txt). \ No newline at end of file diff --git a/recipes/bridge/avatar.jpeg b/recipes/bridge/avatar.jpeg deleted file mode 100644 index 9b95bdea..00000000 Binary files a/recipes/bridge/avatar.jpeg and /dev/null differ diff --git a/recipes/bridge/image.jpeg b/recipes/bridge/image.jpeg deleted file mode 100644 index f80db956..00000000 Binary files a/recipes/bridge/image.jpeg and /dev/null differ diff --git a/recipes/bridge/recipe.yaml b/recipes/bridge/recipe.yaml deleted file mode 100644 index 1d970802..00000000 --- a/recipes/bridge/recipe.yaml +++ /dev/null @@ -1,46 +0,0 @@ -context: - version: "0.2.0" - -package: - name: "bridge" - version: ${{ version }} - -source: - - git: https://gitlab.com/hylkedonker/bridge.git - rev: 87f10144b004ff4ef8e73d6b8b52e3f81effd40f - -build: - number: 0 - script: - - mojo package src/bridge -o ${{ PREFIX }}/lib/mojo/bridge.mojopkg -requirements: - host: - - max=25.2 - run: - - ${{ pin_compatible('max') }} - -tests: - - script: - - if: unix - then: - - mojo test test_numpy.🔥 - files: - recipe: - - test_numpy.🔥 -about: - homepage: https://gitlab.com/hylkedonker/bridge - # Remember to specify the license variants for BSD, Apache, GPL, and LGPL. - # Use the SPDX identifier, e.g: GPL-2.0-only instead of GNU General Public License version 2.0 - # See https://spdx.org/licenses/ - license: MIT - # It is strongly encouraged to include a license file in the package, - # (even if the license doesn't require it) using the license_file entry. - # See https://docs.conda.io/projects/conda-build/en/latest/resources/define-metadata.html#license-file - license_file: LICENSE.txt - summary: Convert (bridge) Python objects to Mojo and vice versa. - repository: https://gitlab.com/hylkedonker/bridge - -extra: - maintainers: - - hylkedonker - project_name: Bridge diff --git "a/recipes/bridge/test_numpy.\360\237\224\245" "b/recipes/bridge/test_numpy.\360\237\224\245" deleted file mode 100644 index 5b690d06..00000000 --- "a/recipes/bridge/test_numpy.\360\237\224\245" +++ /dev/null @@ -1,123 +0,0 @@ -from python import Python -from layout import Layout, LayoutTensor -from tensor import Tensor -from testing import assert_equal, assert_raises, assert_true - -from bridge.numpy import ( - layouttensor_to_ndarray, - ndarray_to_layouttensor, - ndarray_to_tensor, - tensor_to_ndarray, -) - - -def test_tensor_identity_transformation(): - """Test that `ndarray_to_tensor` is inverse of `tensor_to_ndarray`.""" - var values = List[Float64](1.0, 2.0, 3.0, 4.0, 5.0, 6.0) - var in_matrix = Tensor[DType.float64](shape=(3, 2), list=values) - var np_array = tensor_to_ndarray(in_matrix) - var out_matrix = ndarray_to_tensor[DType.float64](np_array) - assert_equal(in_matrix, out_matrix) - - -def test_numpy_tensor_identity_transformation(): - """Test that `tensor_to_ndarray` is inverse of `ndarray_to_tensor`.""" - var np = Python.import_module("numpy") - var in_array = np.arange(6).reshape(3, 2) - var tensor = ndarray_to_tensor[DType.float64](in_array) - var out_array = tensor_to_ndarray(tensor) - assert_equal(in_array, out_array) - - -def test_ndarray_to_layouttensor(): - """Test numpy array conversion to layouttensor for various tensor shapes.""" - var np = Python.import_module("numpy") - - # 1) Test vectors - var in_vector = np.arange(4.0) - var out_vector = ndarray_to_layouttensor[order=1](in_vector) - assert_equal(out_vector[1], 1.0) - assert_equal(out_vector[3], 3.0) - - # 2) Test matrices - var in_matrix = np.arange(4.0 * 3.0).reshape(3, 4) - var out_matrix = ndarray_to_layouttensor[order=2](in_matrix) - assert_equal(out_matrix[0, 0], 0.0) - assert_equal(out_matrix[1, 1], 5.0) - assert_equal(out_matrix[1, 3], 7.0) - assert_equal(out_matrix[2, 1], 9.0) - - # Check that non-contiguous arrays raise exceptions. - with assert_raises(): - var in_matrix_col_major = np.asfortranarray(in_matrix) - _ = ndarray_to_layouttensor[order=2](in_matrix_col_major) - - # 3) Test three-index tensors - var in_tensor = np.arange(4.0 * 3.0).reshape(3, 1, 4) - var out_tensor = ndarray_to_layouttensor[order=3](in_tensor) - assert_equal(out_tensor[0, 0, 0], 0.0) - assert_equal(out_tensor[1, 0, 1], 5.0) - assert_equal(out_tensor[1, 0, 3], 7.0) - assert_equal(out_tensor[2, 0, 1], 9.0) - - # 3) Test four-index tensors - var in_4tensor = np.arange(4.0 * 3.0).reshape(2, 3, 1, 2) - var out_4tensor = ndarray_to_layouttensor[order=4](in_4tensor) - assert_equal(out_4tensor[0, 0, 0, 0], 0.0) - assert_equal(out_4tensor[0, 1, 0, 1], 3.0) - assert_equal(out_4tensor[1, 0, 0, 1], 7.0) - assert_equal(out_4tensor[0, 2, 0, 0], 4.0) - - -def test_memory_leaks(): - """Test that we can safely remove the reference to the numpy array.""" - var np = Python.import_module("numpy") - var np_array = np.arange(6.0).reshape(3, 2) - var tensor = ndarray_to_layouttensor[order=2](np_array) - np_array.__del__() - assert_equal(tensor[1, 0], 2.0) - assert_equal(tensor[1, 1], 3.0) - assert_equal(tensor[2, 1], 5.0) - - -# def test_layouttensor_numpy_identity_transformation(): -# """Test that `layouttensor_to_ndarray` is inverse of `ndarray_to_layouttensor`. -# """ -# values = List[Float64](0.0, 1.0, 2.0, 3.0, 4.0, 5.0) -# tensor = Tensor[DType.float64](shape=(2, 3), list=values) -# var ptr = tensor.unsafe_ptr() -# layouttensor = LayoutTensor[ -# mut=False, -# DType.float64, -# Layout.row_major(2, 3), -# __origin_of(ptr[]), -# ](ptr=ptr) -# np_array = layouttensor_to_ndarray(layouttensor) -# out_layouttensor = ndarray_to_layouttensor[order=2](in_array) - - -def test_numpy_layouttensor_identity_transformation(): - """Test that `ndarray_to_layouttensor` is the inverse of `layouttensor_to_ndarray`. - """ - var np = Python.import_module("numpy") - - # 1) Test vectors - # TODO: Add support for vectors! - - # 2) Test matrices - var in_matrix = np.arange(4.0 * 3.0).reshape(3, 4) - var layout_matrix = ndarray_to_layouttensor[order=2](in_matrix) - var out_matrix = layouttensor_to_ndarray(layout_matrix) - np.testing.assert_array_equal(in_matrix, out_matrix) - - # 3) Test three-index tensors - var in_tensor = np.arange(4.0 * 3.0).reshape(3, 1, 4) - var layout_tensor = ndarray_to_layouttensor[order=3](in_tensor) - var out_tensor = layouttensor_to_ndarray(layout_tensor) - np.testing.assert_array_equal(in_tensor, out_tensor) - - # 3) Test four-index tensors - var in_4tensor = np.arange(4.0 * 3.0).reshape(2, 3, 1, 2) - var layout_4tensor = ndarray_to_layouttensor[order=4](in_4tensor) - var out_4tensor = layouttensor_to_ndarray(layout_4tensor) - np.testing.assert_array_equal(in_4tensor, out_4tensor)