Skip to content
Merged
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
26 changes: 21 additions & 5 deletions python/tvm/relay/op/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,23 +204,39 @@ def squeeze(data, axis=None):

Parameters
----------
data : tvm.relay.Expr
data : relay.Expr
The input data to the operator.

axis : None or List[int] or Expr
axis : Union[None, int, Tuple[int], List[int]] or Expr
The set of axes to remove.
If axis = None, remove all axis of dimensions 1.
If axis = None, remove all axes of dimension 1.
If any specified axis has dimension that does not equal 1, it is an error.

Returns
-------
result : tvm.relay.Expr
result : relay.Expr
The squeezed result.
"""
if isinstance(axis, Constant):
axis = list(axis.data.numpy())
if axis.data.shape:
axis = list(axis.data.numpy())
else:
axis = [axis.data.numpy().item()]
if isinstance(axis, Expr):
return _dyn_make.squeeze(data, axis)
if isinstance(axis, int):
axis = [axis]
if isinstance(axis, (tuple, list)):
tempaxis = []
for tmpax in axis:
if isinstance(tmpax, _expr.IntImm):
tempaxis.append(tmpax.value)
else:
try:
tempaxis.append(int(tmpax))
except ValueError as err:
raise RuntimeError("Unrecognized axis type: %s" % err)
axis = tempaxis
return _make.squeeze(data, axis)


Expand Down
7 changes: 6 additions & 1 deletion tests/python/relay/test_op_level3.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,13 +210,18 @@ class TestSqueeze:
((1, 3, 2, 5), "float32", None),
((1, 3, 1), "float32", [0]),
((1, 2, 1, 2, 1), "float32", [0, 2]),
((1, 3, 1), "float32", 2),
((1, 3, 1), "float32", []),
)

def test_squeeze(self, shape, dtype, axis):
x = relay.var("x", relay.TensorType(shape, dtype))
squeeze = relay.squeeze(x, axis=axis)

np_axis = tuple(axis) if axis is not None else None
if isinstance(axis, int):
np_axis = (axis,)
else:
np_axis = tuple(axis) if axis is not None else None

data = np.random.random_sample(shape).astype(dtype)
op_res = create_executor().evaluate(squeeze, {x: relay.const(data)})
Expand Down