def load_model_from_task(task: str, model_name_or_path: str, **kwargs) -> "PreTrainedModel":
    """Load huggingface model from task and model_name_or_path."""
    from transformers.pipelines import check_task

    task_results = check_task(task.replace("-with-past", ""))
    assert isinstance(task_results, tuple)
    if len(task_results) == 2:
        targeted_task = task_results[0]
    elif len(task_results) == 3:
        targeted_task = task_results[1]
    else:
        raise ValueError("unsupported transformers version")

    model_config = get_model_config(model_name_or_path, **kwargs)
    if getattr(model_config, "quantization_config", None):
        if not isinstance(model_config.quantization_config, dict):
            model_config.quantization_config = model_config.quantization_config.to_dict()

        if model_config.quantization_config.get("quant_method") == "gptq":
            config_use_exllama = model_config.quantization_config.get("use_exllama")
            # Force use_exllama to False for GPTQ quantized models unless it's provided as load kwargs
            # we mostly use quantized model for export and finetuning. Both don't support exllama
            kwargs_use_exllama = kwargs.pop("use_exllama", None) or False
            if config_use_exllama != kwargs_use_exllama:
                model_config.quantization_config["use_exllama"] = kwargs_use_exllama
                # provide config to the model load kwargs to override the saved config
                kwargs["config"] = model_config

    class_tuple = targeted_task["pt"] or (AutoModel,)
    model = None
    for i, model_class in enumerate(class_tuple):
        try:
            model = from_pretrained(model_class, model_name_or_path, "model", **kwargs)
            logger.debug("Loaded model %s with name_or_path %s", model_class, model_name_or_path)
            break
        except (OSError, ValueError) as e:
            if i == len(class_tuple) - 1:
                # len(class_tuple) == 1 covers most common tasks like text-generation, text-classification, etc
                # error could be device OOM, device_map: "auto" not supported, etc

                # len(class_tuple) > 1: not common - image-segmentation, conversational, etc
                # there is no easy way to get tracebacks for earlier failures, so just raise from last
                raise
            # the ValueError need to be caught since there will be multiple model_class for single task.
            # if the model_class is not the one for the task, it will raise ValueError and
            # next model_class will be tried.
            logger.info(
                "Failed to load model %s with name_or_path %s.\n kwargs: %s.\n Exception raised: %s",
                model_class,
                model_name_or_path,
                kwargs,
                e,
            )

    # this won't be None since class_tuple is never empty and we only reach here if model loaded successfully
    # satisfies linter too
    return model
Load huggingface model from task and model_name_or_path.
olive/common/hf/utils.py