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

Add new blas extension and update dpnp.matmul func #1616

Merged
merged 25 commits into from
Jan 13, 2024
Merged

Conversation

vlad-perevezentsev
Copy link
Collaborator

@vlad-perevezentsev vlad-perevezentsev commented Nov 3, 2023

This PR updates dpnp.matmul implementation with gemm function call from oneapi::mkl::blas added to dpnp.backend.extensions.blas.
In addition, gemm_batch function is also added which is the batched versions of gemm, performing multiple gemm operations in a single call.
The implementation is written as a pybind11 extension above required BLAS functions.

  • Have you provided a meaningful PR description?
  • Have you added a test, reproducer or referred to issue with a reproducer?
  • Have you tested your changes locally for CPU and GPU devices?
  • Have you made sure that new changes do not introduce compiler warnings?
  • Have you checked performance impact of proposed changes?
  • If this PR is a work in progress, are you filing the PR as a draft?

Copy link
Contributor

github-actions bot commented Nov 3, 2023

View rendered docs @ https://intelpython.github.io/dpnp/index.html

@vtavana vtavana marked this pull request as ready for review November 30, 2023 20:07
@npolina4 npolina4 self-requested a review December 7, 2023 20:03
Copy link
Collaborator

@npolina4 npolina4 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The result does not match with numpy

import numpy, dpnp

def init():
    N = 5
    A = numpy.empty((N, N), dtype=numpy.float32)
    for i in range(N):
        A[i, : i + 1] = numpy.fromfunction(lambda j: (-j % N) / N + 1, (i + 1,))
        A[i, i + 1 :] = 0.0
        A[i, i + 1 :] = 0.0
    return A

def kernel(A):
    for i in range(A.shape[0]):
        for j in range(i):
            A[i, j] -= A[i, :j] @ A[:j, j]

a = init()
b = dpnp.asarray(a)

kernel(a)
kernel(b)
print(a - b.asnumpy())
[[0.  0.  0.  0.  0. ]
 [0.  0.  0.  0.  0. ]
 [0.  0.  0.  0.  0. ]
 [0.  0.  0.  0.  0. ]
 [0.  0.  0.  1.6 0. ]]

dpnp/backend/extensions/blas/blas_py.cpp Outdated Show resolved Hide resolved
dpnp/backend/extensions/blas/blas_py.cpp Outdated Show resolved Hide resolved
dpnp/backend/extensions/blas/blas_py.cpp Outdated Show resolved Hide resolved
dpnp/backend/extensions/blas/gemm.cpp Outdated Show resolved Hide resolved
dpnp/backend/extensions/blas/gemm.cpp Outdated Show resolved Hide resolved
dpnp/backend/extensions/blas/types_matrix.hpp Show resolved Hide resolved
dpnp/dpnp_iface_linearalgebra.py Outdated Show resolved Hide resolved
dpnp/dpnp_iface_linearalgebra.py Show resolved Hide resolved
dpnp/dpnp_iface_linearalgebra.py Outdated Show resolved Hide resolved
tests/third_party/cupy/math_tests/test_matmul.py Outdated Show resolved Hide resolved
dpnp/backend/extensions/blas/blas_py.cpp Outdated Show resolved Hide resolved
dpnp/backend/extensions/blas/gemm_batch.cpp Show resolved Hide resolved
dpnp/backend/extensions/blas/gemm_batch.cpp Outdated Show resolved Hide resolved
dpnp/backend/extensions/blas/gemm_batch.cpp Outdated Show resolved Hide resolved
dpnp/dpnp_iface_linearalgebra.py Outdated Show resolved Hide resolved
dpnp/dpnp_iface_linearalgebra.py Outdated Show resolved Hide resolved
dpnp/dpnp_iface_linearalgebra.py Outdated Show resolved Hide resolved
dpnp/dpnp_iface_linearalgebra.py Outdated Show resolved Hide resolved
dpnp/dpnp_iface_linearalgebra.py Outdated Show resolved Hide resolved
tests/test_mathematical.py Outdated Show resolved Hide resolved
dpnp/dpnp_iface_linearalgebra.py Outdated Show resolved Hide resolved
dpnp/dpnp_iface_linearalgebra.py Outdated Show resolved Hide resolved
dpnp/dpnp_iface_linearalgebra.py Outdated Show resolved Hide resolved
tests/test_mathematical.py Outdated Show resolved Hide resolved
dpnp/dpnp_iface_linearalgebra.py Outdated Show resolved Hide resolved
dpnp/dpnp_iface_linearalgebra.py Outdated Show resolved Hide resolved
dpnp/dpnp_iface_linearalgebra.py Outdated Show resolved Hide resolved
dpnp/dpnp_iface_linearalgebra.py Outdated Show resolved Hide resolved
dpnp/dpnp_iface_linearalgebra.py Outdated Show resolved Hide resolved
dpnp/backend/extensions/blas/gemm.cpp Outdated Show resolved Hide resolved
dpnp/backend/extensions/blas/gemm.cpp Show resolved Hide resolved
dpnp/backend/extensions/blas/gemm.cpp Outdated Show resolved Hide resolved
dpnp/backend/extensions/blas/gemm.cpp Show resolved Hide resolved
dpnp/backend/extensions/blas/gemm_batch.cpp Show resolved Hide resolved
dpnp/dpnp_iface_linearalgebra.py Outdated Show resolved Hide resolved
dpnp/dpnp_iface_linearalgebra.py Outdated Show resolved Hide resolved
dpnp/dpnp_iface_linearalgebra.py Outdated Show resolved Hide resolved
dpnp/dpnp_iface_linearalgebra.py Outdated Show resolved Hide resolved
tests/test_mathematical.py Show resolved Hide resolved
dpnp/dpnp_utils/dpnp_utils_linearalgebra.py Show resolved Hide resolved
dpnp/dpnp_utils/dpnp_utils_linearalgebra.py Outdated Show resolved Hide resolved
dpnp/dpnp_utils/dpnp_utils_linearalgebra.py Outdated Show resolved Hide resolved
dpnp/dpnp_utils/dpnp_utils_linearalgebra.py Outdated Show resolved Hide resolved
dpnp/dpnp_utils/dpnp_utils_linearalgebra.py Show resolved Hide resolved
dpnp/dpnp_utils/dpnp_utils_linearalgebra.py Outdated Show resolved Hide resolved
dpnp/dpnp_utils/dpnp_utils_linearalgebra.py Outdated Show resolved Hide resolved
dpnp/dpnp_utils/dpnp_utils_linearalgebra.py Outdated Show resolved Hide resolved
dpnp/dpnp_utils/dpnp_utils_linearalgebra.py Outdated Show resolved Hide resolved
tests/test_manipulation.py Show resolved Hide resolved
Copy link
Contributor

@antonwolfy antonwolfy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @vtavana !

@vtavana vtavana merged commit f95ceb9 into master Jan 13, 2024
44 checks passed
@vtavana vtavana deleted the update_matmul branch January 13, 2024 01:49
github-actions bot added a commit that referenced this pull request Jan 13, 2024
* Add new blas extension and update matmul impl

* Add support for N-D array

add N-dimension

* support more special cases + add new tests

* fix random behavior on cpu

* correct dtypes + support more keywords

* add strided support

* check input arrays

* address comments - first round

* address comments - second round

* address comments - third round

* fix pre-commit

* improve test coverage

* address comments

* update _gemm_res_dtype func

* fix a test for result_type

* fix minor issues

* skip tests for matmul

---------

Co-authored-by: Vahid Tavanashad <vahid.tavanashad@intel.com>
Co-authored-by: vtavana <120411540+vtavana@users.noreply.github.com>
Co-authored-by: Anton <100830759+antonwolfy@users.noreply.github.com> f95ceb9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants