Skip to content
Open
Show file tree
Hide file tree
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
11 changes: 7 additions & 4 deletions keras/src/backend/common/backend_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import functools
import operator
import re
import warnings

Expand Down Expand Up @@ -262,14 +261,18 @@ def compute_conv_transpose_output_shape(

def canonicalize_axis(axis, num_dims):
"""Canonicalize an axis in [-num_dims, num_dims) to [0, num_dims)."""
axis = operator.index(axis)
if not -num_dims <= axis < num_dims:
# Faster than operator.index() as we avoid function call overhead
try:
axis = axis.__index__()
except AttributeError:
raise TypeError(f"axis must be an integer, got {type(axis)}")
if axis < -num_dims or axis >= num_dims:
raise ValueError(
f"axis {axis} is out of bounds for an array with dimension "
f"{num_dims}."
)
if axis < 0:
axis = axis + num_dims
axis += num_dims
return axis


Expand Down
13 changes: 8 additions & 5 deletions keras/src/ops/operation_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,22 +370,25 @@ def compute_take_along_axis_output_shape(input_shape, indices_shape, axis):

def reduce_shape(shape, axis=None, keepdims=False):
shape = list(shape)
n = len(shape)
if axis is None:
if keepdims:
return tuple([1 for _ in shape])
return (1,) * n
else:
return tuple([])
return ()
elif isinstance(axis, int):
axis = (axis,)

axis = tuple(canonicalize_axis(a, len(shape)) for a in axis)
axis_tuple = tuple(canonicalize_axis(a, n) for a in axis)


if keepdims:
for ax in axis:
for ax in axis_tuple:
shape[ax] = 1
return tuple(shape)
else:
for ax in sorted(axis, reverse=True):
# Pre-sort axis indices once for deletion in reverse order
for ax in sorted(axis_tuple, reverse=True):
del shape[ax]
return tuple(shape)

Expand Down