//  [python]
def prepare_mask_latents(self, mask, masked_image, batch_size,
                             num_channels_latents, height, width, dtype,
                             device, generator, do_classifier_free_guidance):
        """prepare latents for diffusion to run in latent space.

        Args:
            mask (torch.Tensor): The mask to apply to the image, i.e. regions
                to inpaint.
            image (torch.Tensor): The image to be masked.
            batch_size (int): batch size.
            num_channels_latents (int): latent channel nums.
            height (int): image height.
            width (int): image width.
            dtype (torch.dtype): float type.
            device (torch.device): torch device.
            generator (torch.Generator):
                generator for random functions, defaults to None.
            latents (torch.Tensor):
                Pre-generated noisy latents, defaults to None.
            do_classifier_free_guidance (bool): Whether to apply
                classifier-free guidance.

        Return:
            latents (torch.Tensor): prepared latents.
        """
        shape = (batch_size, num_channels_latents,
                 height // self.vae_scale_factor,
                 width // self.vae_scale_factor)
        mask = t.nn.functional.interpolate(
            mask, size=shape[2:]).to(
                device=device, dtype=dtype)
        masked_image = masked_image.to(device=device, dtype=dtype)

        masked_image_latents = self.vae.encode(
            masked_image).latent_dist.sample(generator)
        masked_image_latents = self.vae.config.scaling_factor * \
            masked_image_latents

        # duplicate mask and masked_image_latents for each generation per
        # prompt, using mps friendly method
        if mask.shape[0] < batch_size:
            if not batch_size % mask.shape[0] == 0:
                raise ValueError(
                    "The passed mask and the required batch size don't match."
                    'Masks are supposed to be duplicated to a total batch'
                    f' size of {batch_size}, but {mask.shape[0]} masks were '
                    'passed. Make sure the number of masks that you pass'
                    ' is divisible by the total requested batch size.')
            mask = mask.repeat(batch_size // mask.shape[0], 1, 1, 1)
        if masked_image_latents.shape[0] < batch_size:
            if not batch_size % masked_image_latents.shape[0] == 0:
                raise ValueError(
                    "The passed images and the required batch size don't "
                    'match. Images are supposed to be duplicated to a total'
                    f' batch size of {batch_size}, but '
                    f'{masked_image_latents.shape[0]} images were passed.'
                    ' Make sure the number of images that you pass is'
                    'divisible by the total requested batch size.')
            masked_image_latents = masked_image_latents.repeat(
                batch_size // masked_image_latents.shape[0], 1, 1, 1)

        mask = torch.cat([mask] * 2) if do_classifier_free_guidance else mask
        masked_image_latents = (
            torch.cat([masked_image_latents] * 2)
            if do_classifier_free_guidance else masked_image_latents)

        # aligning device to prevent device errors when concatenating it with
        # the latent model input
        masked_image_latents = masked_image_latents.to(
            device=device, dtype=dtype)
        return mask, masked_image_latents