def _Dirichlet_backward(x, concentration, grad_output): total = concentration.sum(-1, True).expand_as(concentration) grad = torch._dirichlet_grad(x, concentration, total) return grad * (grad_output - (x * grad_output).sum(-1, True))