Torch.unique derivative not implemented

Recently, I need to use torch.unique to choose the unique elements in a 2D tensor as part of my graph computation. But the runtime error popped up derivative for _unique2 is not implemented. I used a similar function called tf.raw_ops.uniquev2 in TensorFlow, which fulfills same functioning as torch.unique and it is differentiable in my case, I think. Anyone would know how to make this torch.unique differentiable? Or is there any suggestion someone would make? I will really appreciate it.

I also attached part of code for simulating my case:

import torch
a = torch.tensor([1.0,1.0,2.1,3.1,3.5,2.0,2.0])
w1 = torch.tensor(5.0, requires_grad=True)
d = w1 * a
u = torch.unique(d)
w2 = torch.tensor(.5, requires_grad=True)
g = w2 * u
g.sum().backward()

I met the same issue. So sad :pleading_face:

According to tf.raw_ops  |  TensorFlow v2.14.0, that function isn’t differentiable. Are you sure you backprop through that method, @yong1990?

You could try using torch.autograd.Function and manually define a derivative, but how exactly do you differentiate a unique function with respect to its inputs? There could be a relation to dirac-deltas but who knows.

If you can get the same behavior as torch.unique with standard torch ops then you might be able to get a derivative that way.