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

Implementation of histogram with sycl kernel #2027

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

AlexanderKalistratov
Copy link
Collaborator

@AlexanderKalistratov AlexanderKalistratov commented Sep 11, 2024

Implemention of histogram with sycl_kernel.
This PR adds generic histogram kernel which can be used in the future to implement other versions of histogram such as bincount, histogram2d and histogramdd or specialize kernel for special cases like uniform bins.

sycl kernel covers only specific datatype and usm memory types. Unsupported cases are covered by additional copy.

"supported types"
)

a_casted = a.astype(a_bin_dtype, order="C", copy=False)
Copy link
Contributor

Choose a reason for hiding this comment

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

Because a may be usm_ndarray (according to docstring), it is not safe to use a.astype, since usm_ndarray does not implement such a method. Instead dpctl.tensor.astype should be used, or support for such input types should not be advertised.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done, replaced with dpnp.astype(a, ...)

@oleksandr-pavlyk
Copy link
Contributor

Quick validation via independent implementation:

def histogram1d_impl_tensor(data : dpt.usm_ndarray, bins : dpt.usm_ndarray) -> dpt.usm_ndarray:
    assert data.ndim == 1 
    assert bins.ndim == 1
    assert bins.shape[0] > 1
    bin_idx = dpt.searchsorted(bins, data)
    _, c = dpt.unique_counts(dpt.sort(bin_idx))
    return c

In [22]: x = dpnp.random.randn(10**7).get_array()

In [23]: bins = dpnp.asarray([-10, -4, -3, -2, -1, -0.5, -0.25, 0, 0.25, 0.5, 1, 2, 3, 4, 6], dtype=x.dtype).get_array()

In [24]: %time c, _ = dpnp.histogram(dpnp.asarray(x), bins=dpnp.asarray(bins)); print(c)
[    284   13222  214243 1359725 1497540  927356  987069  987352  927886
 1498601 1358597  214704   13114     307]
CPU times: user 10.6 ms, sys: 10.4 ms, total: 21 ms
Wall time: 15.2 ms

In [25]: %time c = histogram1d_impl_tensor(x, bins=bins); print(c)
[    284   13222  214243 1359725 1497540  927356  987069  987352  927886
 1498601 1358597  214704   13114     307]
CPU times: user 711 ms, sys: 581 ms, total: 1.29 s
Wall time: 396 ms

#include <vector>

// dpctl tensor headers
#include "utils/type_dispatch.hpp"
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
#include "utils/type_dispatch.hpp"
#include "dpctl4pybind11.hpp"
#include "utils/type_dispatch.hpp"

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done

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.

2 participants