For me BartolomeD’s solution did not work with dataparallel. Following piece did work later. Posting here as it may help others
class GradReverse(Function):
    @staticmethod
    def forward(ctx, x,lambd):
        ctx.save_for_backward(lambd)
        return x.view_as(x)

    @staticmethod
    def backward(ctx, grad_output):
        lambd=ctx.saved_tensors[0]
        return grad_output.neg()*lambd, None

def grad_reverse(x,lambd):
    return GradReverse.apply(x,lambd)