-
Notifications
You must be signed in to change notification settings - Fork 27
How can I use nested tensor with concated tensor? #453
Comments
Hello @maxwellzh, Thank you for posting your question. Could you please detail what the expected output of this operation is you want to perform? Thank you, |
I also post on pytorch forum, which might be more clear. |
Oh I see, so your code snippet is
So with NestedTensor you could achieve this using the constructor via
However, this would incur a copy. You'd probably likely rather want to have an API similar to Do you need autograd for this operation? Please keep in mind that NestedTensor currently is inference only. |
Thank you for your reply! This might be somewhat off the nestedtensor topic, but I want to discuss a bit more: However, ideally, my expectation is making the somehow multiplying Refers |
@maxwellzh - So, effectively you need a segmented multiplication that you can pass lengths to avoid expanding memory. NestedTensor could do that for you, maybe via a function such as this:
You then multiply this by
This will give you
You can then flatten this and get the result as Could this resolve your issue? Note that we'd need to implement the following
Do you want to only run this on CPU or GPU also? Thanks, |
Could I also ask you for your use case for this operation? |
Hi @cpuhrsch , About the As for my real use case, it's a speech recognition system impl. The inputs (e.g. the audio frames) to my model is of variable lengths. A_pad=[
[1, 2, 3, 0],
[2, 0, 0, 0],
[1, 2, 3, 4]] However, paddings lead to extra memory overhead. In most of the speech recognition systems, these waste of memory is acceptable. But for a specific system, called RNN-Transducer, a super big tensor of size Edit: |
Ok great, this makes sense! In terms of timing I don't currently have much time available to work on this specifically, but I'll try to squeeze it in over the coming weeks (no promises!). For your case specifically, it seems that the hardest part is writing an efficient kernel and modifying the RNN-T code. Aside from maybe a nicer input interface as opposed to passing data and lengths separately, what else would you like to see from this? Or do you plan to use this for the entire acoustic model and then feed it into the loss? |
Specifically, RNN-T loss with compact layout computes
Currently, I can only think of this usage. And I recently learn that PyTorch C++ impls api Edit: just take your time! |
@maxwellzh - hm, we do already have operators that take lengths as an argument and also return correct gradients (the user would have to pass in a 1d vector with the data flattened). Take a look at ctc_loss or EmbeddingBag in PyTorch. |
Just correct me if I misunderstand. Aren't the lengths always required in this case? Without lengths, it's impossible to tell the start position of each sequence in the compact layout.
I looked the ctc_loss and EmbeddingBag, but didn't find such operator, can you give more details? |
For example, ctc_loss has input arguments input_lengths and target_lengths where the targets can be a packed 1d input (similar to the data layout NestedTensor would provide). EmbeddingBag has an offsets input, which you can use to segment the 1d input into relevant pieces, so you can also pass a compact or packed input. It's just that in each case you explicitly have to pass in the metadata that encodes the segment lengths or offsets etc. |
Say the original data is
A=[tensor([1., 2., 3.]), tensor([2.]), tensor([1., 2., 3., 4.])]
. For convenience of some other operations, I concat the data intoA_cat = tensor([1., 2., 3., 2., 1., 2., 3., 4.])
.Now my question is, how can I multiply
A_cat
with another tensor likeB=tensor([5., 3., 2.])
, so that each element ofB
multiply the each tensor inA
. (B
has the same length asA
). Is there any efficient way with native pytorch or nestedtensor lib?Thanks.
The text was updated successfully, but these errors were encountered: