import torch


def compute_per_channel_dice(inputs: torch.Tensor, target: torch.Tensor, epsilon: float) -> torch.Tensor:
    """Compute the dice metric for 3D grids
        Parameters
        ----------
        inputs: torch.Tensor,
            3D grid predicted by the model
        target: int,
            Target 3D grid with labels
        epsilon: float,
            smoothing factor
    """
    assert inputs.size() == target.size()

    inputs = torch.flatten(inputs)
    target = torch.flatten(target)
    target = target.float()
    intersect = (inputs * target).sum(-1)

    denominator = (inputs * inputs).sum(-1) + (target * target).sum(-1)
    return 2 * (intersect / denominator.clamp(min=epsilon))