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

Dynamic shape handling #192

Open
zezhang opened this issue Sep 9, 2024 · 1 comment
Open

Dynamic shape handling #192

zezhang opened this issue Sep 9, 2024 · 1 comment

Comments

@zezhang
Copy link

zezhang commented Sep 9, 2024

Is there any guidelines to follow for handling the dynamic shape (e.g. reshaping a dynamic tensor)?

In addition to using the shapeOp + sliceOp, is there any other way to get a specific dim size?

Is there a good way to construct 1-D tensor for the ShuffleLayer's dynamic shape input? Currently, I'm using shapeOp + sliceOp + concatOp to construct 1D tensor for mixing dynamic batch with static dims, but it's quite complicated. It would be great if there is an easier way to construct 1D tensor, and a direct way get the size of a particular dim from a tensor.

@pranavm-nvidia
Copy link
Collaborator

You should be able to do dynamic reshaping by manipulating the tensor.shape as though it were a list.
For example, you can flatten a tensor by taking the product of its shape:

>>> import math
>>> import tripy as tp
>>>
>>> def flatten_dynamic(x):
...     return tp.reshape(x, [math.prod(x.shape)])
...
>>> x = tp.ones((1, 2), dtype=tp.float32)
>>> flatten_dynamic(x)
tensor([1.0000, 1.0000], dtype=float32, loc=gpu:0, shape=[2])

Dynamic shapes are really only relevant in compiled mode since eager mode always uses dynamic shapes.
You can compile the above function for a range of input shapes and it should still work as expected:

>>> compiler = tp.Compiler(flatten_dynamic)
>>> compiled_flatten = compiler.compile(tp.InputInfo(shape=[(1, 2, 3), 2], dtype=tp.float32))
>>>
>>> x = tp.ones((1, 2), dtype=tp.float32)
>>> compiled_flatten(x)
tensor([1.0000, 1.0000], dtype=float32, loc=gpu:0, shape=[2])
>>>
>>> x = tp.ones((3, 2), dtype=tp.float32)
>>> compiled_flatten(x)
tensor([1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000], dtype=float32, loc=gpu:0, shape=[6])

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

No branches or pull requests

2 participants