def dequantize_per_block(
    qt: torch.Tensor,
    scales: torch.Tensor,
    zps: Optional[torch.Tensor],
    dtype,
    group_size,
    weight_shape=None,
    dequant_nf4_via_int8=False,
):
    r"""
    Dequantize a weight tensor of Linear modules per block.
    Assume the tensor shape is [output channel, input channel],
    block shape is [1, group_size].

    Args:
        qt: The tensor to be dequantized
        scales: Scales in shape [N, #block_k]
        zps: Zero points in shape [N, #block_k]
        dtype: data type of the quantized tensor, int8, int4 or nf4
        group_size: Size of group along input channel
        weight_shape: True weight shape. INT4 tensor's input channel may
            be padded to even, so we need this to return the correct weight.
        dequant_nf4_via_int8: If true, map nf4 to int8 then dequantize to float

    Returns:
        The dequantized tensor
    """
    N = qt.size(0)
    K = qt.size(1) * 2 if is_4bit(dtype) else qt.size(1)
    if scales.dim() > 2:
        scales = scales.squeeze()
    if zps is None:
        zps = torch.zeros_like(scales)
        if dtype == WoqWeightDtype.INT4:
            # shift from [0, 15] to [-8, 7]
            zps += 8
    if zps.dim() > 2:
        zps = zps.squeeze()
    if is_4bit(dtype):
        t = torch.empty(
            qt.shape[0], qt.shape[1] * 2, dtype=torch.uint8, device=qt.device
        )
        t[:, ::2] = qt.bitwise_and(0xF)
        t[:, 1::2] = qt.bitwise_right_shift(4)
        qt = t
    k_rem = K % group_size
    has_rem = k_rem != 0
    Kc = (K + group_size - 1) // group_size
    qt_com = qt[:, : K - k_rem].view(N, K // group_size, group_size)
    scales_com = scales[:, : Kc - has_rem]
    if dtype == WoqWeightDtype.NF4:
        if dequant_nf4_via_int8:
            t = (
                (map_nf4_tensor_to_int8(qt_com) / 127.0 * scales_com.unsqueeze(-1))
                .view(N, K - k_rem)
                .contiguous()
            )
        else:
            t = (
                (map_nf4_tensor_to_float(qt_com) * scales_com.unsqueeze(-1))
                .view(N, K - k_rem)
                .contiguous()
            )
    else:
        zps_com = zps[:, : Kc - has_rem]
        t = (
            (
                (qt_com.to(torch.float) - zps_com.unsqueeze(-1))
                * scales_com.unsqueeze(-1)
            )
            .view(N, K - k_rem)
            .contiguous()
        )
    if k_rem:
        qt_rem = qt[:, K - k_rem :].view(N, 1, k_rem)
        scales_rem = scales[:, Kc - has_rem :]
        if dtype == WoqWeightDtype.NF4:
            if dequant_nf4_via_int8:
                t_rem = (
                    (map_nf4_tensor_to_int8(qt_rem) / 127.0 * scales_rem.unsqueeze(-1))
                    .view(N, k_rem)
                    .contiguous()
                )
            else:
                t_rem = (
                    (map_nf4_tensor_to_float(qt_rem) * scales_rem.unsqueeze(-1))
                    .view(N, k_rem)
                    .contiguous()
                )
        else:
            zps_rem = zps[:, Kc - has_rem :]
            t_rem = (
                (
                    (qt_rem.to(torch.float) - zps_rem.unsqueeze(-1))
                    * scales_rem.unsqueeze(-1)
                )
                .view(N, k_rem)
                .contiguous()
            )
        t = torch.cat([t, t_rem], dim=1).contiguous()
    if weight_shape is not None:
        t = t[: weight_shape[0], : weight_shape[1]].contiguous()
    return t

    Dequantize a weight tensor of Linear modules per block.
    Assume the tensor shape is [output channel, input channel],
    block shape is [1, group_size].

    Args:
        qt: The tensor to be dequantized
        scales: Scales in shape [N, #block_k]
        zps: Zero points in shape [N, #block_k]
        dtype: data type of the quantized tensor, int8, int4 or nf4
        group_size: Size of group along input channel
        weight_shape: True weight shape. INT4 tensor's input channel may
            be padded to even, so we need this to return the correct weight.
        dequant_nf4_via_int8: If true, map nf4 to int8 then dequantize to float

    Returns:
        The dequantized tensor
    
intel_extension_for_pytorch/quantization/_quantize_utils.py