def ensemble_intrinsics(
        targets: torch.Tensor,
        output_uncertainty: bool = False,
        reduction: str = "median",
    ) -> Tuple[torch.Tensor, Optional[torch.Tensor]]:
        """
        Ensembles the intrinsic decomposition represented by the `targets` tensor with expected shape `(B, T, 3, H,
        W)`, where B is the number of ensemble members for a given prediction of size `(H x W)`, and T is the number of
        predicted targets.

        Args:
            targets (`torch.Tensor`):
                Input ensemble of intrinsic image decomposition maps.
            output_uncertainty (`bool`, *optional*, defaults to `False`):
                Whether to output uncertainty map.
            reduction (`str`, *optional*, defaults to `"mean"`):
                Reduction method used to ensemble aligned predictions. The accepted values are: `"median"` and
                `"mean"`.

        Returns:
            A tensor of aligned and ensembled intrinsic decomposition maps with shape `(T, 3, H, W)` and optionally a
            tensor of uncertainties of shape `(T, 3, H, W)`.
        """
        if targets.dim() != 5 or targets.shape[2] != 3:
            raise ValueError(f"Expecting 4D tensor of shape [B,T,3,H,W]; got {targets.shape}.")
        if reduction not in ("median", "mean"):
            raise ValueError(f"Unrecognized reduction method: {reduction}.")

        B, T, _, H, W = targets.shape
        uncertainty = None
        if reduction == "mean":
            prediction = torch.mean(targets, dim=0)  # [T,3,H,W]
            if output_uncertainty:
                uncertainty = torch.std(targets, dim=0)  # [T,3,H,W]
        elif reduction == "median":
            prediction = torch.median(targets, dim=0, keepdim=True).values  # [1,T,3,H,W]
            if output_uncertainty:
                uncertainty = torch.abs(targets - prediction)  # [B,T,3,H,W]
                uncertainty = torch.median(uncertainty, dim=0).values  # [T,3,H,W]
            prediction = prediction.squeeze(0)  # [T,3,H,W]
        else:
            raise ValueError(f"Unrecognized reduction method: {reduction}.")
        return prediction, uncertainty

        Ensembles the intrinsic decomposition represented by the `targets` tensor with expected shape `(B, T, 3, H,
        W)`, where B is the number of ensemble members for a given prediction of size `(H x W)`, and T is the number of
        predicted targets.

        Args:
            targets (`torch.Tensor`):
                Input ensemble of intrinsic image decomposition maps.
            output_uncertainty (`bool`, *optional*, defaults to `False`):
                Whether to output uncertainty map.
            reduction (`str`, *optional*, defaults to `"mean"`):
                Reduction method used to ensemble aligned predictions. The accepted values are: `"median"` and
                `"mean"`.

        Returns:
            A tensor of aligned and ensembled intrinsic decomposition maps with shape `(T, 3, H, W)` and optionally a
            tensor of uncertainties of shape `(T, 3, H, W)`.
        
src/diffusers/pipelines/marigold/pipeline_marigold_intrinsics.py