How can I define a mask function based on the values of a list in Pytorch
I want to mask a tensor based on its values. In the following function if I pass a range (second part) it works, but I want to have a list with various values prompt_ids (3, 8, 9, 30). But it doesn't work and throws error.
RuntimeError: Boolean value of Tensor with more than one value is ambiguous

The function:
   def get_prompt_token_fn(self):
        if self.prompt_ids:
            return lambda x: x in self.prompt_ids
        else:
            return lambda x: (x&gt;=self.id_offset)&amp;(x&lt;self.id_offset+self.length)

What's the problem and how can I resolve it?

In pytorch 1.10 there is an isin function that returns a boolean array based on the condition that elements of first array are in the second array. For versions lower than it, you can implement it as follows:
def isin(ar1, ar2):
    return (ar1[..., None] == ar2).any(-1)

