Repository: Project-MONAI/MONAI
File: monai/inferers/utils.py (Python)

Code before:
            to be buffered on ``sw_device`` before writing to ``device``.
            (Typically, ``sw_device`` is ``cuda`` and ``device`` is ``cpu``.)
            default is None, no buffering. For the buffer dim, when spatial size is divisible by buffer_steps*roi_size,
            (i.e. no overlapping among the buffers) non_blocking copy may be automatically enabled for efficiency.
        buffer_dim: the spatial dimension along which the buffers are created.
            0 indicates the first spatial dimension. Default is -1, the last spatial dimension.
        with_coord: whether to pass the window coordinates to ``predictor``. Default is False.
            If True, the signature of ``predictor`` should be ``predictor(patch_data, patch_coord, ...)``.
        args: optional args to be passed to ``predictor``.
        kwargs: optional keyword args to be passed to ``predictor``.

    Note:
        - Inputs must be channel-first and have a batch dim (NCHW / NCDHW).
        - If your data is NHWC/NDHWC, please apply `EnsureChannelFirst` / `EnsureChannelFirstd` upstream.

    """
    num_spatial_dims = len(inputs.shape) - 2

    # Only perform strict shape validation if roi_size is a sequence (explicit dimensions).
    # If roi_size is an integer, it is broadcast to all dimensions, so we cannot
    # infer the expected dimensionality to enforce a strict check here.
    if isinstance(roi_size, Sequence):
        roi_dims = len(roi_size)
        if num_spatial_dims != roi_dims:
            raise ValueError(
                f"inputs must have {roi_dims + 2} dimensions for {roi_dims}D roi_size "
                f"(Batch, Channel, {', '.join(['Spatial'] * roi_dims)}), "
                f"but got inputs shape {inputs.shape}.\n"
                "If you have channel-last data (e.g. B, D, H, W, C), please use "
                "monai.transforms.EnsureChannelFirst or EnsureChannelFirstd upstream."
            )
    # -----------------------------------------------------------------
    buffered = buffer_steps is not None and buffer_steps > 0
    if buffered:
        if buffer_dim < -num_spatial_dims or buffer_dim > num_spatial_dims:
            raise ValueError(f"buffer_dim must be in [{-num_spatial_dims}, {num_spatial_dims}], got {buffer_dim}.")
        if buffer_dim < 0:
            buffer_dim += num_spatial_dims
    overlap = ensure_tuple_rep(overlap, num_spatial_dims)
    for o in overlap:
        if o < 0 or o >= 1:
            raise ValueError(f"overlap must be >= 0 and < 1, got {overlap}.")
    compute_dtype = inputs.dtype

    # determine image spatial size and batch size
    # Note: all input images must have the same image size and batch size
    batch_size, _, *image_size_ = inputs.shape
    device = device or inputs.device
    sw_device = sw_device or inputs.device

    condition = kwargs.pop("condition", None)

Review comment (suggestion):
```suggestion
                f"Inputs must have {roi_dims + 2} dimensions for {roi_dims}D roi_size "
```

Code after:
        buffer_steps: the number of sliding window iterations along the ``buffer_dim``
            to be buffered on ``sw_device`` before writing to ``device``.
            (Typically, ``sw_device`` is ``cuda`` and ``device`` is ``cpu``.)
            default is None, no buffering. For the buffer dim, when spatial size is divisible by buffer_steps*roi_size,
            (i.e. no overlapping among the buffers) non_blocking copy may be automatically enabled for efficiency.
        buffer_dim: the spatial dimension along which the buffers are created.
            0 indicates the first spatial dimension. Default is -1, the last spatial dimension.
        with_coord: whether to pass the window coordinates to ``predictor``. Default is False.
            If True, the signature of ``predictor`` should be ``predictor(patch_data, patch_coord, ...)``.
        args: optional args to be passed to ``predictor``.
        kwargs: optional keyword args to be passed to ``predictor``.

    Note:
        - Inputs must be channel-first and have a batch dim (NCHW / NCDHW).
        - If your data is NHWC/NDHWC, please apply `EnsureChannelFirst` / `EnsureChannelFirstd` upstream.

    Raises:
        ValueError: When the input dimensions do not match the expected dimensions based on ``roi_size``.

    """
    num_spatial_dims = len(inputs.shape) - 2

    # Only perform strict shape validation if roi_size is a sequence (explicit dimensions).
    # If roi_size is an integer, it is broadcast to all dimensions, so we cannot
    # infer the expected dimensionality to enforce a strict check here.
    if isinstance(roi_size, Sequence):
        roi_dims = len(roi_size)
        if num_spatial_dims != roi_dims:
            raise ValueError(
                f"Inputs must have {roi_dims + 2} dimensions for {roi_dims}D roi_size "
                f"(Batch, Channel, {', '.join(['Spatial'] * roi_dims)}), "
                f"but got inputs shape {inputs.shape}.\n"
                "If you have channel-last data (e.g. B, D, H, W, C), please use "
                "monai.transforms.EnsureChannelFirst or EnsureChannelFirstd upstream."
            )
    # -----------------------------------------------------------------
    buffered = buffer_steps is not None and buffer_steps > 0
    if buffered:
        if buffer_dim < -num_spatial_dims or buffer_dim > num_spatial_dims:
            raise ValueError(f"buffer_dim must be in [{-num_spatial_dims}, {num_spatial_dims}], got {buffer_dim}.")
        if buffer_dim < 0:
            buffer_dim += num_spatial_dims
    overlap = ensure_tuple_rep(overlap, num_spatial_dims)
    for o in overlap:
        if o < 0 or o >= 1:
            raise ValueError(f"overlap must be >= 0 and < 1, got {overlap}.")
    compute_dtype = inputs.dtype

    # determine image spatial size and batch size
    # Note: all input images must have the same image size and batch size
    batch_size, _, *image_size_ = inputs.shape