Skip to content

Added gemm accumulation matrix into interface and tests #26

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
10 changes: 9 additions & 1 deletion arrayfire/library/linear_algebra.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ def gemm(
rhs_opts: MatProp = MatProp.NONE,
alpha: int | float = 1.0,
beta: int | float = 0.0,
accum: Array = None
) -> Array:
"""
Performs BLAS general matrix multiplication (GEMM) on two Array instances.
Expand Down Expand Up @@ -125,6 +126,10 @@ def gemm(
beta : int | float, optional
Scalar multiplier for the existing matrix C in the accumulation. Default is 0.0.

accum: Array, optional
A 2-dimensional, real or complex array representing the matrix C in the accumulation.
Default is None (no accumulation).

Returns
-------
Array
Expand All @@ -135,7 +140,10 @@ def gemm(
- The data types of `lhs` and `rhs` must be compatible.
- Batch operations are not supported in this version.
"""
return cast(Array, wrapper.gemm(lhs.arr, rhs.arr, lhs_opts, rhs_opts, alpha, beta))
accumulator = None
if isinstance(accum, Array):
accumulator = accum.arr
return cast(Array, wrapper.gemm(lhs.arr, rhs.arr, lhs_opts, rhs_opts, alpha, beta, accumulator))


@afarray_as_array
Expand Down
4 changes: 2 additions & 2 deletions tests/test_library/test_linear_algebra.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ def test_gemm_basic(matrix_a: af.Array, matrix_b: af.Array) -> None:
def test_gemm_alpha_beta(matrix_a: af.Array, matrix_b: af.Array) -> None:
alpha = 0.5
beta = 2.0
result = af.gemm(matrix_a, matrix_b, alpha=alpha, beta=beta)
expected = create_from_2d_nested(10.5, 12.0, 22.5, 26.0)
result = af.gemm(matrix_a, matrix_b, alpha=alpha, beta=beta, accum=matrix_a)
expected = create_from_2d_nested(11.5, 15.0, 27.5, 33.0)
assert result == expected, f"Expected {expected}, got {result}"


Expand Down