def _compute_loss(
        max_attention_per_index_fg: List[torch.Tensor],
        max_attention_per_index_bg: List[torch.Tensor],
        dist_x: List[torch.Tensor],
        dist_y: List[torch.Tensor],
        return_losses: bool = False,
    ) -> torch.Tensor:
        """Computes the attend-and-excite loss using the maximum attention value for each token."""
        losses_fg = [max(0, 1.0 - curr_max) for curr_max in max_attention_per_index_fg]
        losses_bg = [max(0, curr_max) for curr_max in max_attention_per_index_bg]
        loss = sum(losses_fg) + sum(losses_bg) + sum(dist_x) + sum(dist_y)
        if return_losses:
            return max(losses_fg), losses_fg
        else:
            return max(losses_fg), loss
Computes the attend-and-excite loss using the maximum attention value for each token.
examples/community/pipeline_stable_diffusion_boxdiff.py