def _quantization_config_from_dict()

in optimum/intel/openvino/configuration.py [0:0]


def _quantization_config_from_dict(config_dict: Dict[str, Any]) -> OVQuantizationConfigBase:
    """
    Helper function to create a quantization config from a dictionary.
    """

    # Check for OVMixedQuantizationConfig
    if "weight_quantization_config" in config_dict and "full_quantization_config" in config_dict:
        return OVMixedQuantizationConfig.from_dict(config_dict)

    # Check for OVPipelineQuantizationConfig
    if "quantization_configs" in config_dict:
        return OVPipelineQuantizationConfig.from_dict(config_dict)

    # Either OVWeightQuantizationConfig or OVQuantizationConfig
    # Try to detect the type of config based on the keys present in the dictionary
    wq_args = set(inspect.getfullargspec(OVWeightQuantizationConfig.__init__).args)
    fq_args = set(inspect.getfullargspec(OVQuantizationConfig.__init__).args)
    common_args = wq_args & fq_args
    wq_only_args = wq_args - common_args
    fq_only_args = fq_args - common_args
    if any(arg in wq_only_args for arg in config_dict):
        return OVWeightQuantizationConfig.from_dict(config_dict)
    if any(arg in fq_only_args for arg in config_dict):
        return OVQuantizationConfig.from_dict(config_dict)

    # Try to create instances of both configs and check which one is valid
    try:
        wq_config = OVWeightQuantizationConfig.from_dict(config_dict)
    except ValueError as wc_exception:  # noqa: F841
        wq_config = None
    try:
        fq_config = OVQuantizationConfig.from_dict(config_dict)
    except ValueError as fq_exception:  # noqa: F841
        fq_config = None
    if (wq_config is None) != (fq_config is None):
        return wq_config or fq_config
    if wq_config is None and fq_config is None:
        raise ValueError(
            "The provided configuration dictionary does not match the expected structure for either "
            "OVWeightQuantizationConfig or OVQuantizationConfig. "
            f"Exceptions encountered: {wc_exception} \n {fq_exception}"  # noqa: F821
        )

    # Try to determine the type of config based on the `weight_only` parameter
    weight_only = config_dict.get("weight_only", None)
    if weight_only:
        return wq_config
    if weight_only is False:
        return fq_config

    # If everything else fails, default to OVWeightQuantizationConfig
    logger.warning(
        "Can't determine type of OV quantization config. Please specify explicitly whether you intend to "
        "run weight-only quantization or not with `weight_only` parameter. Creating an instance of "
        "OVWeightQuantizationConfig."
    )
    return wq_config