Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 3 additions & 1 deletion .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ max-line-length = 119
exclude =
.venv/**
build/**
benchmarks/**
benchmarks/**
docs/*
tests/test_documentation/test_documentation.py
128 changes: 107 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,107 @@
# arrayfire-py

Arrayfire python wrapper

## Coverage

- [x] Computer Vision
- [] Events
- [x] Functions to Create and Modify Arrays
- [x] Functions to Work with Internal Array Layout
- [x] Image Processing
- [x] Features
- [x] Input and Output Functions
- [x] Interface Functions
- [x] Linear Algebra
- [x] Machine Learning
- [x] Mathematical Functions
- [x] Signal Processing
- [x] Statistics
- [x] Unified API Functions
- [x] Vector Algorithms
# arrayfire-python (WIP)
<p align="center"><a href="http://arrayfire.com/"><img src="http://arrayfire.com/logos/arrayfire_logo_whitebkgnd.png" width="800"></a></p>

[ArrayFire](https://github.com/arrayfire/arrayfire) is a high performance library for parallel computing with an easy-to-use API. It enables users to write scientific computing code that is portable across CUDA, OpenCL and CPU devices.

This project is a **work in progress**. It is meant to provide a numpy-like Python interface for the ArrayFire C library, i.e, it provides array functionality, math operations, printing, etc. This is the front-end python library for using ArrayFire. It is currently supported on Python 3.10+.

Here is an example of the library at work:
```py
# Set backend and device (optional: 'cuda', 'opencl', 'oneapi', 'cpu')
af.setBackend(af.BackendType.cuda)
af.setDevice(0)

# Create two 5x5 arrays on the GPU
a = af.randu((5, 5))
b = af.randu((5, 5))

# Perform element-wise addition and matrix multiplication
c = a + b
d = af.matmul(a, b)

# Print the result
print(c, "Element-wise Sum")
print(d, "Matrix Product")
```

# Installing

**Requirement Details**
This project is separated into 3 different parts:
```
arrayfire-py -> arrayfire-binary-python-wrapper -> ArrayFire C Libraries
```
This means that arrayfire with python each of these parts is needed:
- [`arrayfire-py`](https://github.com/arrayfire/arrayfire-python) is the intended User Interface that provides a numpy-like layer to execute math and array operations with ArrayFire. *** This is the preferred Interface ***
- [`arrayfire-binary-python-wrapper`](https://github.com/arrayfire/arrayfire-binary-python-wrapper) is the wrapper that provides Python direct access to the ArrayFire functions in the C library. This package must have access to ArrayFire binaries.
- [`ArrayFire C Libraries`](https://github.com/arrayfire/arrayfire) are the binaries obtained from compiling the [ArrayFire C/C++ Project](https://github.com/arrayfire/arrayfire). You obtain these easily through [installers in the ArrayFire download page](https://arrayfire.com/download/).

**Install the last stable version of python wrapper:**
```sh
pip install arrayfire_binary_python_wrapper-0.8.0+af3.10.0-py3-none-linux_x86_64.whl # install required binary wrapper with the 3.10 ArrayFire binaries included
pip install arrayfire-py # install arrayfire python interface library
```

**Install a pre-built wheel:**
```
pip install arrayfire-py -f https://repo.arrayfire.com/python/wheels/arrayfire-python/0.1.0
```

# Building
Building this interface is straight forward using [scikit-build-core](https://github.com/scikit-build/scikit-build-core):
```
python -m pip install -r dev-requirements.txt
python -m build --wheel
```

**Note: Building this project does not require the arrayfire-binary-python-wrapper package; however, the binary wrapper is needed to run any projects with it**

# Running Tests

Tests are located in folder [tests](tests).

To run the tests, use:
```bash
python -m pytest tests/
```

# Contributing

If you are interested in using ArrayFire through python, we would appreciate any feedback and contributions.

The community of ArrayFire developers invites you to build with us if you are
interested and able to write top-performing tensor functions. Together we can
fulfill [The ArrayFire
Mission](https://github.com/arrayfire/arrayfire/wiki/The-ArrayFire-Mission-Statement)
for fast scientific computing for all.

Contributions of any kind are welcome! Please refer to [the
wiki](https://github.com/arrayfire/arrayfire/wiki) and our [Code of
Conduct](33) to learn more about how you can get involved with the ArrayFire
Community through
[Sponsorship](https://github.com/arrayfire/arrayfire/wiki/Sponsorship),
[Developer
Commits](https://github.com/arrayfire/arrayfire/wiki/Contributing-Code-to-ArrayFire),
or [Governance](https://github.com/arrayfire/arrayfire/wiki/Governance).

# Citations and Acknowledgements

If you redistribute ArrayFire, please follow the terms established in [the
license](LICENSE).

ArrayFire development is funded by AccelerEyes LLC and several third parties,
please see the list of [acknowledgements](ACKNOWLEDGEMENTS.md) for an
expression of our gratitude.

# Support and Contact Info

* [Slack Chat](https://join.slack.com/t/arrayfire-org/shared_invite/MjI4MjIzMDMzMTczLTE1MDI5ODg4NzYtN2QwNGE3ODA5OQ)
* [Google Groups](https://groups.google.com/forum/#!forum/arrayfire-users)
* ArrayFire Services: [Consulting](http://arrayfire.com/consulting) | [Support](http://arrayfire.com/download) | [Training](http://arrayfire.com/training)

# Trademark Policy

The literal mark "ArrayFire" and ArrayFire logos are trademarks of AccelerEyes
LLC (dba ArrayFire). If you wish to use either of these marks in your own
project, please consult [ArrayFire's Trademark
Policy](http://arrayfire.com/trademark-policy/)
6 changes: 3 additions & 3 deletions arrayfire/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,6 @@

from arrayfire.library.array_functions import (
constant,
zeros,
ones,
copy_array,
diag,
eval,
Expand All @@ -119,17 +117,19 @@
lookup,
lower,
moddims,
reshape,
ones,
pad,
range,
reorder,
replace,
reshape,
select,
set_manual_eval_flag,
shift,
tile,
transpose,
upper,
zeros,
)

__all__ += ["gloh", "orb", "sift", "dog", "fast", "harris", "susan", "hamming_matcher", "nearest_neighbour"]
Expand Down
22 changes: 13 additions & 9 deletions arrayfire/array_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -758,10 +758,10 @@ def __getitem__(self, key: IndexKey, /) -> Array:
ndims = self.ndim

indexing = key
if isinstance(key, int | float | slice): # when indexing with one dimension, treat it as indexing a flat array

if isinstance(key, int | float | slice): # when indexing with one dimension, treat it as indexing a flat array
ndims = 1
elif isinstance(key, Array): # when indexing with one array, treat it as indexing a flat array
elif isinstance(key, Array): # when indexing with one array, treat it as indexing a flat array
ndims = 1
if key.is_bool:
indexing = wrapper.where(key.arr)
Expand Down Expand Up @@ -836,9 +836,9 @@ def __setitem__(self, key: IndexKey, value: int | float | bool | Array, /) -> No
del_other = False

indexing = key
if isinstance(key, int | float | slice): # when indexing with one dimension, treat it as indexing a flat array
if isinstance(key, int | float | slice): # when indexing with one dimension, treat it as indexing a flat array
ndims = 1
elif isinstance(key, Array): # when indexing with one array, treat it as indexing a flat array
elif isinstance(key, Array): # when indexing with one array, treat it as indexing a flat array
ndims = 1
if key.is_bool:
indexing = wrapper.where(key.arr)
Expand Down Expand Up @@ -930,7 +930,8 @@ def T(self) -> Array:

Note
----
- The array instance must be two-dimensional. If the array instance is not two-dimensional, an error should be raised.
- The array instance must be two-dimensional. If the array instance is not two-dimensional,
| an error should be raised.

"""
if self.ndim < 2:
Expand All @@ -949,11 +950,13 @@ def H(self) -> Array:
-------
Array
Two-dimensional array whose first and last dimensions (axes) are permuted in reverse order relative to
original array with its elements complex conjugated. The returned array must have the same data type as the original array.
| original array with its elements complex conjugated.
| The returned array must have the same data type as the original array.

Note
----
- The array instance must be two-dimensional. If the array instance is not two-dimensional, an error should be raised.
- The array instance must be two-dimensional. If the array instance is not two-dimensional,
| an error should be raised.

"""
if self.ndim < 2:
Expand Down Expand Up @@ -1191,7 +1194,8 @@ def reshape(self, shape) -> Array:
-------
out : af.Array
- An array containing the same data as `array` with the specified shape.
- The total number of elements in `array` must match the product of the dimensions specified in the `shape` tuple.
- The total number of elements in `array` must match the product of the dimensions
| specified in the `shape` tuple.

Raises
------
Expand Down
11 changes: 9 additions & 2 deletions arrayfire/library/array_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def constant(scalar: int | float | complex, shape: tuple[int, ...] = (1,), dtype
"""
return cast(Array, wrapper.create_constant_array(scalar, shape, dtype))


def zeros(shape: tuple[int, ...], dtype: Dtype = float32) -> Array:
"""
Create a multi-dimensional array filled with zeros
Expand Down Expand Up @@ -100,6 +101,7 @@ def zeros(shape: tuple[int, ...], dtype: Dtype = float32) -> Array:
"""
return constant(0, shape, dtype)


def ones(shape: tuple[int, ...], dtype: Dtype = float32) -> Array:
"""
Create a multi-dimensional array filled with ones
Expand Down Expand Up @@ -127,6 +129,7 @@ def ones(shape: tuple[int, ...], dtype: Dtype = float32) -> Array:
"""
return constant(1, shape, dtype)


@afarray_as_array
def diag(array: Array, /, *, diag_index: int = 0, extract: bool = True) -> Array:
"""
Expand Down Expand Up @@ -311,7 +314,8 @@ def lower(array: Array, /, *, is_unit_diag: bool = False) -> Array:
Notes
-----
- The function does not alter the elements above the main diagonal; it simply does not include them in the output.
- This function can be useful for mathematical operations that require lower triangular matrices, such as certain types of matrix factorizations.
- This function can be useful for mathematical operations that require lower triangular matrices,
| such as certain types of matrix factorizations.

Examples
--------
Expand Down Expand Up @@ -367,7 +371,8 @@ def upper(array: Array, /, *, is_unit_diag: bool = False) -> Array:
Notes
-----
- The function does not alter the elements below the main diagonal; it simply does not include them in the output.
- This function can be useful for mathematical operations that require upper triangular matrices, such as certain types of matrix factorizations.
- This function can be useful for mathematical operations that require upper triangular matrices,
| such as certain types of matrix factorizations.

Examples
--------
Expand Down Expand Up @@ -872,6 +877,7 @@ def moddims(array: Array, shape: tuple[int, ...], /) -> Array:
# TODO add examples to doc
return cast(Array, wrapper.moddims(array.arr, shape))


def reshape(array: Array, shape: tuple[int, ...], /) -> Array:
"""
Modify the shape of the array without changing the data layout.
Expand Down Expand Up @@ -907,6 +913,7 @@ def reshape(array: Array, shape: tuple[int, ...], /) -> Array:
"""
return moddims(array, shape)


@afarray_as_array
def reorder(array: Array, /, *, shape: tuple[int, ...] = (1, 0, 2, 3)) -> Array:
"""
Expand Down
3 changes: 2 additions & 1 deletion arrayfire/library/computer_vision.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ def sift(
tuple[Features, Array]
A tuple containing:
- An ArrayFire Features object encapsulating the detected keypoints.
- An ArrayFire Array containing the corresponding descriptors for each keypoint. The descriptors are 128-dimensional vectors describing the local appearance around each keypoint.
- An ArrayFire Array containing the corresponding descriptors for each keypoint.
| The descriptors are 128-dimensional vectors describing the local appearance around each keypoint.

Note
----
Expand Down
2 changes: 1 addition & 1 deletion arrayfire/library/linear_algebra.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def gemm(
rhs_opts: MatProp = MatProp.NONE,
alpha: int | float = 1.0,
beta: int | float = 0.0,
accum: Array = None
accum: Array = None,
) -> Array:
"""
Performs BLAS general matrix multiplication (GEMM) on two Array instances.
Expand Down
8 changes: 8 additions & 0 deletions benchmarks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Benchmarks Results
Here are some graphs comparing ArrayFire Python against other packages for some common operations:

<p align="center"><img src="img/comparison_afcuda_t4.png" width="800"></a></p>
<p align="center"><img src="img/comparison_afopencl_t4.png" width="800"></a></p>
<p align="center"><img src="img/comparison_afoneapi_b580.png" width="800"></a></p>

These graphs were generated with this benchmark code using the ArrayFire C Libraries v3.10
Binary file added benchmarks/img/comparison_afcuda_t4.png
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 benchmarks/img/comparison_afoneapi_b580.png
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 benchmarks/img/comparison_afopencl_b580.png
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 benchmarks/img/comparison_afopencl_t4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 6 additions & 2 deletions docs/afjit.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
# removed, then the execution of this code would be equivalent to the
# following function.

import arrayfire as af
import time

import arrayfire as af

samples = int(9e8)
x = af.randu((samples))
y = af.randu((samples))


def pi_no_jit(x, y, samples):
temp = x * x
af.eval(temp)
Expand All @@ -23,11 +25,13 @@ def pi_no_jit(x, y, samples):
af.eval(temp)
return 4.0 * af.sum(temp) / samples


def pi_jit(x, y, samples):
temp = af.sqrt(x * x + y * y) < 1
af.eval(temp)
return 4.0 * af.sum(temp) / samples


# Print device info
af.info()

Expand All @@ -47,4 +51,4 @@ def pi_jit(x, y, samples):
end = time.perf_counter()
print("no jit:", end - start, res)

# [jit-endsnippet]
# [jit-endsnippet]
Loading
Loading