Skip to content

Conversation

titaiwangms
Copy link
Contributor

@titaiwangms titaiwangms commented Jun 6, 2023

op.SequenceMap comes to the rescue for element iteration cases. (Maybe it's just me that didn't know this...)
This PR helps complete #762

@titaiwangms titaiwangms added the module: torchlib Related to the torch/aten function lib in development label Jun 6, 2023
def reshape_to_2d(tensor):
shape = op.Shape(tensor)
rank = op.Size(shape)
if rank == 0:
Copy link
Collaborator

@gramalingam gramalingam Jun 6, 2023

Choose a reason for hiding this comment

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

nit: you can simplify this as below:

if rank <= 1:
   tensor = op.Reshape(tensor, [1, -1])

In reshape, -1 is treated as "whatever size matches input tensor size".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Copy link
Contributor Author

@titaiwangms titaiwangms Jun 6, 2023

Choose a reason for hiding this comment

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

Also applied to 3d

Copy link
Collaborator

@justinchuby justinchuby left a comment

Choose a reason for hiding this comment

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

Nice! I didn’t know about this either

@titaiwangms titaiwangms merged commit 7e131c5 into microsoft:main Jun 6, 2023
),
skip(
"atleast_1d_single_tensor",
matcher=lambda sample: isinstance(sample.input, (list, tuple)),
Copy link
Collaborator

Choose a reason for hiding this comment

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

So we don’t take a Sequence as input? Where is this op used? How are the inputs produced?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It accepts both tensor and list.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh I see there are two variants

shape = op.Shape(tensor)
rank = op.Size(shape)
if rank <= 1:
tensor = op.Reshape(tensor, op.Constant(value_ints=[1, -1, 1]))
Copy link
Collaborator

Choose a reason for hiding this comment

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

You may want to double-check the downstream usage to make sure whether you want to reshape a tensor of size N to 1x1xN or Nx1x1 ... the above reshapes it to 1xNx1 .... which may be okay, but just wondering.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

module: torchlib Related to the torch/aten function lib in development

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants