    def multiply_grads(self, c):
        """Multiplies grads by a constant *c*."""
        per_device_and_dtype_grads = defaultdict(lambda: defaultdict(list))
        for p in self.params:
            if p.grad is not None:
                if p.grad.is_sparse:
                    p.grad.data.mul_(c.to(p.grad.device) if torch.is_tensor(c) else c)
                else:
                    per_device_and_dtype_grads[p.grad.device][p.grad.dtype].append(
                        p.grad.data
                    )
        for device, per_dtype_grads in per_device_and_dtype_grads.items():
            for grads in per_dtype_grads.values():
                torch._foreach_mul_(grads, c.to(device) if torch.is_tensor(c) else c)
Multiplies grads by a constant *c*.
fairseq/optim/fairseq_optimizer.py