Skip to content

CHORE: Factored out _broadcast_shape implementation #1047

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

Merged
merged 1 commit into from
Jan 27, 2023
Merged
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
21 changes: 13 additions & 8 deletions dpctl/tensor/_manipulation_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,14 @@ def _broadcast_strides(X_shape, X_strides, res_ndim):
return tuple(out_strides)


def _broadcast_shapes(*args):
"""
Broadcast the input shapes into a single shape;
returns tuple broadcasted shape.
"""
shapes = [array.shape for array in args]
def _broadcast_shape_impl(shapes):
if len(set(shapes)) == 1:
return shapes[0]
mutable_shapes = False
nds = [len(s) for s in shapes]
biggest = max(nds)
for i in range(len(args)):
sh_len = len(shapes)
for i in range(sh_len):
diff = biggest - nds[i]
if diff > 0:
ty = type(shapes[i])
Expand All @@ -77,7 +73,7 @@ def _broadcast_shapes(*args):
unique.remove(1)
new_length = unique.pop()
common_shape.append(new_length)
for i in range(len(args)):
for i in range(sh_len):
if shapes[i][axis] == 1:
if not mutable_shapes:
shapes = [list(s) for s in shapes]
Expand All @@ -89,6 +85,15 @@ def _broadcast_shapes(*args):
return tuple(common_shape)


def _broadcast_shapes(*args):
"""
Broadcast the input shapes into a single shape;
returns tuple broadcasted shape.
"""
array_shapes = [array.shape for array in args]
return _broadcast_shape_impl(array_shapes)


def permute_dims(X, axes):
"""
permute_dims(X: usm_ndarray, axes: tuple or list) -> usm_ndarray
Expand Down