def dequantize_per_channel(
    qt: torch.Tensor,
    scales: torch.Tensor,
    zps: Optional[torch.Tensor],
    dtype,
    weight_shape=None,
    dequant_nf4_via_int8=False,
):
    r"""
    Dequantize a weight tensor of Linear modules per channel.
    Assume the tensor shape is [output channel, input channel],
    each output channel has its own quantization parameters.

    Args:
        qt: The tensor to be dequantized
        scales: Scales for dequantization
        zps: Zero points for dequantization
        dtype: data type of the quantized tensor, int8, int4 or nf4
        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
    """
    assert qt.ndim == 2
    assert dtype in (WoqWeightDtype.INT8, WoqWeightDtype.INT4, WoqWeightDtype.NF4)
    scales = scales.squeeze()
    sym_quant = zps is None
    if sym_quant:
        zps = torch.zeros_like(scales)
        if dtype == WoqWeightDtype.INT4:
            # shift from [0, 15] to [-8, 7]
            zps += 8
    else:
        zps = zps.squeeze()
    if dtype == WoqWeightDtype.INT8:
        return (qt.to(torch.float) - zps.unsqueeze(-1)) * scales.unsqueeze(-1)
    elif dtype == WoqWeightDtype.INT4:
        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)
        t = (t.to(torch.float) - zps.unsqueeze(-1)) * scales.unsqueeze(-1)
        if weight_shape is not None:
            t = t[: weight_shape[0], : weight_shape[1]].contiguous()
        return t
    else:  # NF4
        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)
        if dequant_nf4_via_int8:
            t = map_nf4_tensor_to_int8(t) / 127.0
        else:
            t = map_nf4_tensor_to_float(t)
        if weight_shape is not None:
            t = t[: weight_shape[0], : weight_shape[1]].contiguous()
        t = t * scales.unsqueeze(-1)
        return t

    Dequantize a weight tensor of Linear modules per channel.
    Assume the tensor shape is [output channel, input channel],
    each output channel has its own quantization parameters.

    Args:
        qt: The tensor to be dequantized
        scales: Scales for dequantization
        zps: Zero points for dequantization
        dtype: data type of the quantized tensor, int8, int4 or nf4
        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