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

Add mask_tensor util #35

Closed
wants to merge 1 commit into from
Closed

Add mask_tensor util #35

wants to merge 1 commit into from

Conversation

JackTemaki
Copy link
Contributor

Utility to create a boolean tensor mask on the tensors device based on sequence lengths.

Utility to create a boolean tensor mask on the tensors device
based on sequence lengths.
:param seq_len: [B]
:return: Mask of [B,T]
"""
seq_len = seq_len.to(device=tensor.device)
Copy link
Contributor

Choose a reason for hiding this comment

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

I am not sure if this case should be made within this function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The device handling? So completely removing tensor and having just a seq_len to mask function?

Copy link
Contributor

Choose a reason for hiding this comment

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

you still need the tensor for the shape right? But I feel like this casting of the seq_len should be done beforehand (and at least in my setups usually already is done with the model call. So this might be redundant imho.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You could do it without the tensor (using max(seq_len)), I think we can add this as a second function

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If seq_len is already on the device, this should have not more overhead than one integer comparison

Copy link
Contributor

Choose a reason for hiding this comment

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

is this max then exportable?
In general if the overhead is low I don't mind and we can leave it like this.

import torch


def mask_tensor(tensor: torch.Tensor, seq_len: torch.Tensor) -> torch.Tensor:
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure about the name. I find it a bit too generic. And also ambiguous. (Does it mask a tensor? No, actually, it returns a mask tensor. For what? Based on what?)

In TF, the corresponding function is called tf.sequence_mask. Why not just use the same name here? So all people who have used TF at some point in the past (almost everyone) knows what this is about.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

tf.sequence_mask has a different signature, and we can also add such a function. This is just not what I wanted to have here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For the name, yes this can be more verbose if needed.

import torch


def mask_tensor(tensor: torch.Tensor, seq_len: torch.Tensor) -> torch.Tensor:
Copy link
Member

Choose a reason for hiding this comment

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

You should not use tensor here as an argument, as this is not really required. What you actually want is an argument maxlen, where you can just pass tensor.shape[1] or whatever. That argument could also be optional, and then it would just take the max from seq_len.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What you actually want

What I want is to have it with a tensor and make sure the mask is on the same device as the tensor. But maybe the majority disagrees, and I keep this for myself.

@JackTemaki
Copy link
Contributor Author

Closing, as this specific signature seems not to be beneficial for people other than me.

@JackTemaki JackTemaki closed this Sep 15, 2023
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

Successfully merging this pull request may close these issues.

4 participants