mirror of
https://github.com/hwchase17/langchain.git
synced 2025-09-02 03:26:17 +00:00
huggingface: Add IPEX models support (#29179)
Co-authored-by: Erick Friis <erick@langchain.dev>
This commit is contained in:
@@ -9,6 +9,14 @@ from langchain_core.language_models.llms import BaseLLM
|
||||
from langchain_core.outputs import Generation, GenerationChunk, LLMResult
|
||||
from pydantic import ConfigDict, model_validator
|
||||
|
||||
from ..utils.import_utils import (
|
||||
IMPORT_ERROR,
|
||||
is_ipex_available,
|
||||
is_openvino_available,
|
||||
is_optimum_intel_available,
|
||||
is_optimum_intel_version,
|
||||
)
|
||||
|
||||
DEFAULT_MODEL_ID = "gpt2"
|
||||
DEFAULT_TASK = "text-generation"
|
||||
VALID_TASKS = (
|
||||
@@ -18,6 +26,8 @@ VALID_TASKS = (
|
||||
"translation",
|
||||
)
|
||||
DEFAULT_BATCH_SIZE = 4
|
||||
_MIN_OPTIMUM_VERSION = "1.21"
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -126,70 +136,70 @@ class HuggingFacePipeline(BaseLLM):
|
||||
_model_kwargs["device_map"] = device_map
|
||||
tokenizer = AutoTokenizer.from_pretrained(model_id, **_model_kwargs)
|
||||
|
||||
try:
|
||||
if task == "text-generation":
|
||||
if backend == "openvino":
|
||||
try:
|
||||
from optimum.intel.openvino import ( # type: ignore[import]
|
||||
OVModelForCausalLM,
|
||||
)
|
||||
|
||||
except ImportError:
|
||||
raise ValueError(
|
||||
"Could not import optimum-intel python package. "
|
||||
"Please install it with: "
|
||||
"pip install 'optimum[openvino,nncf]' "
|
||||
)
|
||||
try:
|
||||
# use local model
|
||||
model = OVModelForCausalLM.from_pretrained(
|
||||
model_id, **_model_kwargs
|
||||
)
|
||||
|
||||
except Exception:
|
||||
# use remote model
|
||||
model = OVModelForCausalLM.from_pretrained(
|
||||
model_id, export=True, **_model_kwargs
|
||||
)
|
||||
else:
|
||||
model = AutoModelForCausalLM.from_pretrained(
|
||||
model_id, **_model_kwargs
|
||||
)
|
||||
elif task in ("text2text-generation", "summarization", "translation"):
|
||||
if backend == "openvino":
|
||||
try:
|
||||
from optimum.intel.openvino import OVModelForSeq2SeqLM
|
||||
|
||||
except ImportError:
|
||||
raise ValueError(
|
||||
"Could not import optimum-intel python package. "
|
||||
"Please install it with: "
|
||||
"pip install 'optimum[openvino,nncf]' "
|
||||
)
|
||||
try:
|
||||
# use local model
|
||||
model = OVModelForSeq2SeqLM.from_pretrained(
|
||||
model_id, **_model_kwargs
|
||||
)
|
||||
|
||||
except Exception:
|
||||
# use remote model
|
||||
model = OVModelForSeq2SeqLM.from_pretrained(
|
||||
model_id, export=True, **_model_kwargs
|
||||
)
|
||||
else:
|
||||
model = AutoModelForSeq2SeqLM.from_pretrained(
|
||||
model_id, **_model_kwargs
|
||||
)
|
||||
else:
|
||||
if backend in {"openvino", "ipex"}:
|
||||
if task not in VALID_TASKS:
|
||||
raise ValueError(
|
||||
f"Got invalid task {task}, "
|
||||
f"currently only {VALID_TASKS} are supported"
|
||||
)
|
||||
except ImportError as e:
|
||||
raise ValueError(
|
||||
f"Could not load the {task} model due to missing dependencies."
|
||||
) from e
|
||||
|
||||
err_msg = f'Backend: {backend} {IMPORT_ERROR.format(f"optimum[{backend}]")}'
|
||||
if not is_optimum_intel_available():
|
||||
raise ImportError(err_msg)
|
||||
|
||||
# TODO: upgrade _MIN_OPTIMUM_VERSION to 1.22 after release
|
||||
min_optimum_version = (
|
||||
"1.22"
|
||||
if backend == "ipex" and task != "text-generation"
|
||||
else _MIN_OPTIMUM_VERSION
|
||||
)
|
||||
if is_optimum_intel_version("<", min_optimum_version):
|
||||
raise ImportError(
|
||||
f"Backend: {backend} requires optimum-intel>="
|
||||
f"{min_optimum_version}. You can install it with pip: "
|
||||
"`pip install --upgrade --upgrade-strategy eager "
|
||||
f"`optimum[{backend}]`."
|
||||
)
|
||||
|
||||
if backend == "openvino":
|
||||
if not is_openvino_available():
|
||||
raise ImportError(err_msg)
|
||||
|
||||
from optimum.intel import ( # type: ignore[import]
|
||||
OVModelForCausalLM,
|
||||
OVModelForSeq2SeqLM,
|
||||
)
|
||||
|
||||
model_cls = (
|
||||
OVModelForCausalLM
|
||||
if task == "text-generation"
|
||||
else OVModelForSeq2SeqLM
|
||||
)
|
||||
else:
|
||||
if not is_ipex_available():
|
||||
raise ImportError(err_msg)
|
||||
|
||||
if task == "text-generation":
|
||||
from optimum.intel import (
|
||||
IPEXModelForCausalLM, # type: ignore[import]
|
||||
)
|
||||
|
||||
model_cls = IPEXModelForCausalLM
|
||||
else:
|
||||
from optimum.intel import (
|
||||
IPEXModelForSeq2SeqLM, # type: ignore[import]
|
||||
)
|
||||
|
||||
model_cls = IPEXModelForSeq2SeqLM
|
||||
|
||||
else:
|
||||
model_cls = (
|
||||
AutoModelForCausalLM
|
||||
if task == "text-generation"
|
||||
else AutoModelForSeq2SeqLM
|
||||
)
|
||||
|
||||
model = model_cls.from_pretrained(model_id, **_model_kwargs)
|
||||
|
||||
if tokenizer.pad_token is None:
|
||||
tokenizer.pad_token_id = model.config.eos_token_id
|
||||
|
Reference in New Issue
Block a user