Support for nesting of nested tensors and view operation #439
Description
🚀 Feature
This is two related features packed in one.
The first one is to support nesting of nested tensors, i.e., this operation should succeed:
list_of_nts = [nested_tensor_1, nested_tensor_2, ...]
nested_nested_tensor = nestedtensor.nested_tensor(list_of_nts)
This currently leads to RuntimeError: Currently only supporting a sequence of Tensors.
.
The second feature is to support a view
operation over the nested structure:
list_of_tensors = [tensor_1, tensor_2, ..., tensor_6]
nested_tensor = nestedtensor.nested_tensor(list_of_tensors)
view_nt = nested_tensor.view(2, 3)
This should produce the same output as
nt1 = nestedtensor.nested_tensor(list_of_tensors[:3])
nt2 = nestedtensor.nested_tensor(list_of_tensors[3:])
equivalent_nt = nestedtensor.nested_tensor([nt1, nt2])
Motivation
I have a set-valued function that returns a different sized output depending on the given input. So, for n x m
-dim input, it returns a k x m
-dim tensor where k < n
depends on the particular input. I need to evaluate this function for batch_1 x batch_2 x n x m
-dim input, which I do in a loop after collapsing the batches together, i.e., input.view(-1, n, m)
. This produces a list of batch_1 * batch_2
different sized outputs. Ideally, I need to return a batch_1 x batch_2 x k x m
-dim tensor, which doesn't work since k
is not fixed. So, I instead want to put the (batch_1 * batch_2)
list of varying k x m
-dim tensors into a nestedtensor
and return a view of it that agrees with the original batch structure, so batch_1 x batch_2 x varying k x 2
. These features would make this possible.
PS: I will also need to use autograd over each k x m
-dim output after joining them with other tensors. Since autograd support seems like an existing concern, I'll skip that here.
Pitch
See above.
Alternatives
I can use a list of tensors and do the operations manually (in a loop) but that's not ideal.
Additional context
N/A