Skip to content

Commit

Permalink
moved model loading based on task and backend to utils.py
Browse files Browse the repository at this point in the history
  • Loading branch information
rbrugaro committed Jul 9, 2024
1 parent dd98415 commit 144d29f
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from langchain_core.outputs import Generation, GenerationChunk, LLMResult
from langchain_core.pydantic_v1 import Extra

from .utils import get_model

DEFAULT_MODEL_ID = "gpt2"
DEFAULT_TASK = "text-generation"
VALID_TASKS = (
Expand Down Expand Up @@ -88,9 +90,6 @@ def from_model_id(
"""
try:
from transformers import ( # type: ignore[import]
AutoConfig,
AutoModelForCausalLM,
AutoModelForSeq2SeqLM,
AutoTokenizer,
)
from transformers import pipeline as hf_pipeline # type: ignore[import]
Expand All @@ -104,104 +103,7 @@ def from_model_id(
_model_kwargs = model_kwargs or {}
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
)
elif backend == "ipex":
try:
import torch
from optimum.intel.ipex import ( # type: ignore[import]
IPEXModelForCausalLM,
)
except ImportError:
raise ValueError(
"Could not import optimum-intel python package. "
"Please install it with: "
"pip install 'optimum[ipex]' "
"or follow installation instructions from: "
" https://github.com/rbrugaro/optimum-intel "
)
try:
# use TorchScript model
config = AutoConfig.from_pretrained(model_id)
export = not getattr(config, "torchscript", False)
except RuntimeError:
logger.warning(
"We will use IPEXModel with export=True to export the model"
)
export = True
model = IPEXModelForCausalLM.from_pretrained(
model_id,
export=export,
**_model_kwargs,
torch_dtype=torch.bfloat16, # keep or remove the dtype????
)

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:
if backend == "ipex":
logger.warning("IPEX backend is not supported for this task.")
else:
model = AutoModelForSeq2SeqLM.from_pretrained(
model_id, **_model_kwargs
)
else:
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
model = get_model(task, backend, model_id, **_model_kwargs)

if tokenizer.pad_token is None:
tokenizer.pad_token_id = model.config.eos_token_id
Expand Down
118 changes: 118 additions & 0 deletions libs/partners/huggingface/langchain_huggingface/llms/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import logging
from typing import Any, Optional

logger = logging.getLogger(__name__)


def get_model(
task: str, backend: str, model_id: str, **_model_kwargs: Optional[dict]
) -> Any:
try:
from transformers import ( # type: ignore[import]
AutoConfig,
AutoModelForCausalLM,
AutoModelForSeq2SeqLM,
)

except ImportError:
raise ValueError(
"Could not import transformers python package. "
"Please install it with `pip install transformers`."
)
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
)
return model
elif backend == "ipex":
try:
import torch
from optimum.intel.ipex import ( # type: ignore[import]
IPEXModelForCausalLM,
)
except ImportError:
raise ValueError(
"Could not import optimum-intel python package. "
"Please install it with: "
"pip install 'optimum[ipex]' "
"or follow installation instructions from: "
" https://github.com/rbrugaro/optimum-intel "
)
try:
# use TorchScript model
config = AutoConfig.from_pretrained(model_id)
export = not getattr(config, "torchscript", False)
except RuntimeError:
logger.warning(
"We will use IPEXModel with export=True to export the model"
)
export = True
model = IPEXModelForCausalLM.from_pretrained(
model_id,
export=export,
**_model_kwargs,
torch_dtype=torch.bfloat16, # keep or remove the dtype????
)
return model
else:
model = AutoModelForCausalLM.from_pretrained(model_id, **_model_kwargs)
return model

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
)
return model
else:
if backend == "ipex":
logger.warning("IPEX backend is not supported for this task.")
else:
model = AutoModelForSeq2SeqLM.from_pretrained(
model_id, **_model_kwargs
)
return model
else:
raise ValueError(f"Got invalid task {task}, " f"currently not supported")
except ImportError as e:
raise ValueError(
f"Could not load the {task} model due to missing dependencies."
) from e

0 comments on commit 144d29f

Please sign in to comment.