def clip_master_grads(self, max_norm):
        """
        Clips gradient norm and updates dynamic loss scaler.
        """
        self._sync_fp16_grads_to_fp32()
        grad_norm = clip_grad_norm(
            self.fp32_params, max_norm, sync=self._aggregate_gnorms
        )

        # detect overflow and adjust loss scale
        if self.scaler is not None:
            overflow = has_overflow(grad_norm)
            prev_scale = self.scaler.loss_scale
            self.scaler.update_scale(overflow)
            if overflow:
                self.zero_grad()
                if self.scaler.loss_scale <= self.min_loss_scale:
                    # Use FloatingPointError as an uncommon error that parent
                    # functions can safely catch to stop training.
                    self.scaler.loss_scale = prev_scale
                    raise FloatingPointError(
                        (
                            'Minimum loss scale reached ({}). Your loss is probably exploding. '
                            'Try lowering the learning rate, using gradient clipping or '
                            'increasing the batch size.'
                        ).format(self.min_loss_scale)
                    )
                    logging.info(
                        f'Overflow: setting loss scale to {self.scaler.loss_scale}'
                    )

        return grad_norm

        Clips gradient norm and updates dynamic loss scaler.
        
EVA-CLIP-18B/shinji/training/fp16.py