keras/src/callbacks/swap_ema_weights.py/SwapEMAWeights/_finalize_ema_values
class SwapEMAWeights:    def _finalize_ema_values(self):
        if hasattr(self.model.optimizer, "inner_optimizer"):
            # LossScaleOptimizer
            optimizer = self.model.optimizer.inner_optimizer
        else:
            optimizer = self.model.optimizer
        if not hasattr(optimizer, "_model_variables_moving_average"):
            raise ValueError(
                "SwapEMAWeights must be used when "
                "`use_ema=True` is set on the optimizer. "
                f"Received: use_ema={optimizer.use_ema}"
            )
        if backend.backend() == "tensorflow":
            self._tf_finalize_ema_values(optimizer)
        else:
            self._backend_finalize_ema_values(optimizer)