Skip to content

Implements dpctl.tensor.where - #1147

Merged
ndgrigorian merged 6 commits into
masterfrom
where-impl-gh-1120
Apr 7, 2023
Merged

ndgrigorian merged 6 commits into
masterfrom
where-impl-gh-1120

Conversation

@ndgrigorian

@ndgrigorian ndgrigorian commented Mar 30, 2023 •

Copy link
Copy Markdown
Collaborator

Closes #1120

This PR implements dpt.where function, which takes a condition array and two arrays of data, x1 and x2. The output is an array with elements from x1 where condition evaluates to true and x2 where condition is false. The output is of type determined by type promotion of x1 and x2.

i.e.,

In [4]: condition = dpt.asarray([False, True, True, True])

In [5]: x1 = dpt.asarray([1])

In [6]: x2 = dpt.asarray([2])

In [7]: dpt.where(condition, x1, x2)
Out[7]: usm_ndarray([2, 1, 1, 1])
  • Have you provided a meaningful PR description?
  • Have you added a test, reproducer or referred to an 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 opening the PR as a draft?

@github-actions

Copy link
Copy Markdown

@coveralls

coveralls commented Mar 30, 2023 •

Copy link
Copy Markdown
Collaborator

Coverage Status

Coverage: 83.204% (+0.2%) from 83.018% when pulling db43042 on where-impl-gh-1120 into 7bbfce1 on master.

@github-actions

Copy link
Copy Markdown

Array API standard conformance tests for dpctl=0.14.3dev0=py310h76be34b_59 ran successfully.
Passed: 47
Failed: 787
Skipped: 280

@github-actions

Copy link
Copy Markdown

Array API standard conformance tests for dpctl=0.14.3dev0=py310h76be34b_58 ran successfully.
Passed: 47
Failed: 787
Skipped: 280

@github-actions

Copy link
Copy Markdown

Array API standard conformance tests for dpctl=0.14.3dev0=py310h76be34b_67 ran successfully.
Passed: 47
Failed: 787
Skipped: 280

@ndgrigorian

Copy link
Copy Markdown
Collaborator Author

Significant performance gains from re-implemenation:

In [13]: %timeit where(x, x1, x2)
569 µs ± 30.4 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)

In [14]: %timeit where2(x, x1, x2)
860 µs ± 39.5 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)

In [15]: %timeit where(x, x1, x2)
524 µs ± 32.8 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)

In [16]: %timeit where2(x, x1, x2)
851 µs ± 23.9 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)

Where x is only 60 elements and where2 is the original implementation.

For a larger array (on scale of 10^5 elements)

In [16]: %timeit wf.where2(x, x1, x2)
1.54 ms ± 71.6 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)

In [17]: %timeit wf.where(x, x1, x2)
579 µs ± 59 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)

@oleksandr-pavlyk

Copy link
Copy Markdown
Contributor

An issue in type promotion logic:

import dpctl.tensor as dpt

m = dpt.zeros(100, dtype="?")
m[::2] = True; m[1::3] = True; m[2::5] = False

x1 = dpt.ones(100, dtype="i4")
x2 = dpt.zeros(100, dtype="f4")

dpt.where(m, x1, x2)  # raises ValueError

The error is ValueError: Device Intel(R) Graphics [0x9a49] does not provide native support for double-precision floating point type..

@github-actions

github-actions Bot commented Apr 3, 2023

Copy link
Copy Markdown

Array API standard conformance tests for dpctl=0.14.3dev0=py310h76be34b_68 ran successfully.
Passed: 47
Failed: 787
Skipped: 280

@github-actions

github-actions Bot commented Apr 3, 2023

Copy link
Copy Markdown

Array API standard conformance tests for dpctl=0.14.3dev0=py310h76be34b_69 ran successfully.
Passed: 47
Failed: 787
Skipped: 280

Comment thread dpctl/tensor/_search_functions.py Resolved
@github-actions

github-actions Bot commented Apr 4, 2023

Copy link
Copy Markdown

Array API standard conformance tests for dpctl=0.14.3dev0=py310h76be34b_74 ran successfully.
Passed: 47
Failed: 787
Skipped: 280

@github-actions

github-actions Bot commented Apr 5, 2023

Copy link
Copy Markdown

Array API standard conformance tests for dpctl=0.14.3dev0=py310h76be34b_79 ran successfully.
Passed: 47
Failed: 787
Skipped: 280

@github-actions

github-actions Bot commented Apr 5, 2023

Copy link
Copy Markdown

Array API standard conformance tests for dpctl=0.14.3dev0=py310h76be34b_82 ran successfully.
Passed: 47
Failed: 787
Skipped: 280

@ndgrigorian
ndgrigorian marked this pull request as ready for review April 5, 2023 17:19
@oleksandr-pavlyk

Copy link
Copy Markdown
Contributor

Great to see coverage experience such an upward jump 👍

@ndgrigorian ndgrigorian changed the title Implements dpt.where Implements dpctl.tensor.where Apr 5, 2023
Comment thread dpctl/tensor/_search_functions.py Resolved
Comment thread dpctl/tensor/libtensor/source/where.cpp Resolved
Comment thread dpctl/tensor/libtensor/source/where.cpp Resolved
Comment on lines +135 to +145
// destination must be ample enough to accomodate all elements
{
size_t range =
static_cast<size_t>(dst_offsets.second - dst_offsets.first);
if (range + 1 < static_cast<size_t>(nelems)) {
throw py::value_error(
"Memory addressed by the destination array can not "
"accomodate all the "
"array elements.");
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sometime down the road it would be good to modularize this check into a utility as well. Perhaps dpctl::tensor::utils::can_accomodate(nelems, dst). This is best done in a separate PR.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I was thinking about this as well during refactoring. I'll make note of it.

Comment thread dpctl/tensor/_search_functions.py Resolved
@github-actions

github-actions Bot commented Apr 5, 2023

Copy link
Copy Markdown

Array API standard conformance tests for dpctl=0.14.3dev0=py310h76be34b_83 ran successfully.
Passed: 47
Failed: 787
Skipped: 280

@oleksandr-pavlyk
oleksandr-pavlyk self-requested a review April 5, 2023 20:47

@oleksandr-pavlyk oleksandr-pavlyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks awesome! Thank you @ndgrigorian

@github-actions

github-actions Bot commented Apr 5, 2023

Copy link
Copy Markdown

Array API standard conformance tests for dpctl=0.14.3dev0=py310h76be34b_84 ran successfully.
Passed: 47
Failed: 787
Skipped: 280

@github-actions

github-actions Bot commented Apr 6, 2023

Copy link
Copy Markdown

Array API standard conformance tests for dpctl=0.14.3dev0=py310h76be34b_86 ran successfully.
Passed: 47
Failed: 787
Skipped: 280

Comment thread dpctl/tensor/_search_functions.py Resolved
@github-actions

github-actions Bot commented Apr 6, 2023

Copy link
Copy Markdown

Array API standard conformance tests for dpctl=0.14.3dev0=py310h76be34b_89 ran successfully.
Passed: 47
Failed: 787
Skipped: 280

@github-actions

github-actions Bot commented Apr 6, 2023

Copy link
Copy Markdown

Array API standard conformance tests for dpctl=0.14.3dev0=py310h76be34b_90 ran successfully.
Passed: 47
Failed: 787
Skipped: 280

- Utility functions are for finding an output type for universal and binary functions when the device of allocation lacks fp16 or fp64
- Where now outputs an F-contiguous array when all inputs are F-contiguous
- Where now outputs a empty 0D array if any input is a 0D empty array
- Added tests for these cases

Fixed incorrect logic in where test
@oleksandr-pavlyk
oleksandr-pavlyk self-requested a review April 7, 2023 17:53
@github-actions

github-actions Bot commented Apr 7, 2023

Copy link
Copy Markdown

Array API standard conformance tests for dpctl=0.14.3dev0=py310h76be34b_93 ran successfully.
Passed: 47
Failed: 787
Skipped: 280

@github-actions

github-actions Bot commented Apr 7, 2023

Copy link
Copy Markdown

Array API standard conformance tests for dpctl=0.14.3dev0=py310h76be34b_86 ran successfully.
Passed: 47
Failed: 787
Skipped: 280

- Asymmetric dtype test to improve coverage

@oleksandr-pavlyk oleksandr-pavlyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thank you, @ndgrigorian !

@oleksandr-pavlyk

Copy link
Copy Markdown
Contributor

Please wait till github-action CI has finished and merge. This way artifacts would be ready to get published on dppy/label/dev right away once the PR is merged.

@github-actions

github-actions Bot commented Apr 7, 2023

Copy link
Copy Markdown

Array API standard conformance tests for dpctl=0.14.3dev0=py310h76be34b_87 ran successfully.
Passed: 47
Failed: 787
Skipped: 280

@ndgrigorian
ndgrigorian merged commit a504e96 into master Apr 7, 2023
@ndgrigorian
ndgrigorian deleted the where-impl-gh-1120 branch April 7, 2023 20:38
@github-actions

github-actions Bot commented Apr 7, 2023

Copy link
Copy Markdown

Deleted rendered PR docs from intelpython.github.com/dpctl, latest should be updated shortly. 🤞

@github-actions

github-actions Bot commented Apr 7, 2023

Copy link
Copy Markdown

Array API standard conformance tests for dpctl=0.14.3dev0=py310h76be34b_87 ran successfully.
Passed: 47
Failed: 787
Skipped: 280

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.

Implement dpctl.tensor.where

3 participants