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 unstack, moveaxis, swapaxes #1137

Merged
merged 2 commits into from
Mar 27, 2023

Conversation

vtavana
Copy link
Collaborator

@vtavana vtavana commented Mar 24, 2023

In this task, unstack, moveaxis, and swapaxes functions are added.
This PR closes #1121

  • 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?
  • If this PR is a work in progress, are you filing the PR as a draft?

@github-actions
Copy link

default value is axis=0.

Returns:
out (usm_narray): A tuple of arrays.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
out (usm_narray): A tuple of arrays.
Tuple[usm_narray,...]: A tuple of arrays.

Comment on lines +839 to +842
ind = list(range(0, X.ndim))
ind[axis1] = axis2
ind[axis2] = axis1
return dpt.permute_dims(X, tuple(ind))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Instead of creating list to be able to mutate it only to then copy it into tuple, use generator to create the right tuple directly. Here is an example:

In [1]: def permuted_range(n, a1, a2):
   ...:     for i in range(n):
   ...:         if i == a1:
   ...:             yield a2
   ...:         elif i == a2:
   ...:             yield a1
   ...:         else:
   ...:             yield i
   ...:

In [2]: tuple(permuted_range(5, 1, 3))
Out[2]: (0, 3, 2, 1, 4)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Actually, the way it is currently implement is faster:


In [13]: def way1(n, a1, a2):
    ...:     ind = list(range(n))
    ...:     ind[a1] = a2
    ...:     ind[a2] = a1
    ...:     return tuple(ind)
    ...:

In [14]: def permuted_range(n, a1, a2):
    ...:     for i in range(n):
    ...:         if i == a1:
    ...:             yield a2
    ...:         elif i == a2:
    ...:             yield a1
    ...:         else:
    ...:             yield i
    ...:

In [15]: def way2(n, a1, a2):
    ...:     return tuple(permuted_range(n, a1, a2))
    ...:

In [16]: way1(5, 1, 3) == way2(5, 1, 3)
Out[16]: True

In [17]: %timeit way1(5, 1, 3)
434 ns ± 23 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)

In [18]: %timeit way2(5, 1, 3)
865 ns ± 98 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)

in the half-open interval `[-N, N)`.

Returns:
out (usm_narray): Array with moved axes.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
out (usm_narray): Array with moved axes.
usm_narray: Array with moved axes.

@github-actions
Copy link

Array API standard conformance tests for dpctl=0.14.3dev0=py310h76be34b_23 ran successfully.
Passed: 46
Failed: 788
Skipped: 280

a valid `axis` must reside in the half-open interval `[-N, N)`.

Returns:
out (usm_narray): Swapped array.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
out (usm_narray): Swapped array.
usm_narray: Array with swapped axes.

@coveralls
Copy link
Collaborator

coveralls commented Mar 24, 2023

Coverage Status

Coverage: 82.84% (+0.01%) from 82.826% when pulling 18e680b on impl_unstack_moveaxis_swapaxes into 3a22dd9 on master.

@github-actions
Copy link

Array API standard conformance tests for dpctl=0.14.3dev0=py310h76be34b_24 ran successfully.
Passed: 46
Failed: 788
Skipped: 280

@oleksandr-pavlyk oleksandr-pavlyk merged commit 26b2eaa into master Mar 27, 2023
@oleksandr-pavlyk oleksandr-pavlyk deleted the impl_unstack_moveaxis_swapaxes branch March 27, 2023 14:14
@github-actions
Copy link

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

@github-actions
Copy link

Array API standard conformance tests for dpctl=0.14.3dev0=py310h76be34b_24 ran successfully.
Passed: 46
Failed: 788
Skipped: 280

@vtavana vtavana self-assigned this Apr 25, 2023
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.

Implemented dpctl.tensor.unstack
3 participants