def _generate_cosine_weights(self, tile_width, tile_height, nbatches, device, dtype):
        """
        Generates cosine weights as a PyTorch tensor for blending tiles.

        Args:
            tile_width (int): Width of the tile in pixels.
            tile_height (int): Height of the tile in pixels.
            nbatches (int): Number of batches.
            device (torch.device): Device where the tensor will be allocated (e.g., 'cuda' or 'cpu').
            dtype (torch.dtype): Data type of the tensor (e.g., torch.float32).

        Returns:
            torch.Tensor: A tensor containing cosine weights for blending tiles, expanded to match batch and channel dimensions.
        """
        # Convert tile dimensions to latent space
        latent_width = tile_width // 8
        latent_height = tile_height // 8

        # Generate x and y coordinates in latent space
        x = np.arange(0, latent_width)
        y = np.arange(0, latent_height)

        # Calculate midpoints
        midpoint_x = (latent_width - 1) / 2
        midpoint_y = (latent_height - 1) / 2

        # Compute cosine probabilities for x and y
        x_probs = np.cos(np.pi * (x - midpoint_x) / latent_width)
        y_probs = np.cos(np.pi * (y - midpoint_y) / latent_height)

        # Create a 2D weight matrix using the outer product
        weights_np = np.outer(y_probs, x_probs)

        # Convert to a PyTorch tensor with the correct device and dtype
        weights_torch = torch.tensor(weights_np, device=device, dtype=dtype)

        # Expand for batch and channel dimensions
        tile_weights_expanded = torch.tile(weights_torch, (nbatches, self.unet.config.in_channels, 1, 1))

        return tile_weights_expanded

        Generates cosine weights as a PyTorch tensor for blending tiles.

        Args:
            tile_width (int): Width of the tile in pixels.
            tile_height (int): Height of the tile in pixels.
            nbatches (int): Number of batches.
            device (torch.device): Device where the tensor will be allocated (e.g., 'cuda' or 'cpu').
            dtype (torch.dtype): Data type of the tensor (e.g., torch.float32).

        Returns:
            torch.Tensor: A tensor containing cosine weights for blending tiles, expanded to match batch and channel dimensions.
        
examples/community/mod_controlnet_tile_sr_sdxl.py