community[minor]: migrate bigdl-llm to ipex-llm (#19518)

- **Description**: `bigdl-llm` library has been renamed to
[`ipex-llm`](https://github.com/intel-analytics/ipex-llm). This PR
migrates the `bigdl-llm` integration to `ipex-llm` .
- **Issue**: N/A. The original PR of `bigdl-llm` is
https://github.com/langchain-ai/langchain/pull/17953
- **Dependencies**: `ipex-llm` library
- **Contribution maintainer**: @shane-huang

Updated doc:   docs/docs/integrations/llms/ipex_llm.ipynb
Updated test:
libs/community/tests/integration_tests/llms/test_ipex_llm.py
This commit is contained in:
Shengsheng Huang
2024-03-28 11:12:59 +08:00
committed by GitHub
parent a31f692f4e
commit ac1dd8ad94
8 changed files with 232 additions and 30 deletions

View File

@@ -4,11 +4,11 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"# BigDL-LLM\n", "# IPEX-LLM\n",
"\n", "\n",
"> [BigDL-LLM](https://github.com/intel-analytics/BigDL/) is a low-bit LLM optimization library on Intel XPU (Xeon/Core/Flex/Arc/Max). It can make LLMs run extremely fast and consume much less memory on Intel platforms. It is open sourced under Apache 2.0 License.\n", "> [IPEX-LLM](https://github.com/intel-analytics/ipex-llm/) is a low-bit LLM optimization library on Intel XPU (Xeon/Core/Flex/Arc/Max). It can make LLMs run extremely fast and consume much less memory on Intel platforms. It is open sourced under Apache 2.0 License.\n",
"\n", "\n",
"This example goes over how to use LangChain to interact with BigDL-LLM for text generation. \n" "This example goes over how to use LangChain to interact with IPEX-LLM for text generation. \n"
] ]
}, },
{ {
@@ -33,7 +33,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"Install BigDL-LLM for running LLMs locally on Intel CPU." "Install IEPX-LLM for running LLMs locally on Intel CPU."
] ]
}, },
{ {
@@ -42,8 +42,7 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"# Install BigDL\n", "%pip install --pre --upgrade ipex-llm[all]"
"%pip install --pre --upgrade bigdl-llm[all]"
] ]
}, },
{ {
@@ -60,7 +59,7 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"from langchain.chains import LLMChain\n", "from langchain.chains import LLMChain\n",
"from langchain_community.llms.bigdl import BigdlLLM\n", "from langchain_community.llms import IpexLLM\n",
"from langchain_core.prompts import PromptTemplate" "from langchain_core.prompts import PromptTemplate"
] ]
}, },
@@ -89,7 +88,7 @@
{ {
"data": { "data": {
"application/vnd.jupyter.widget-view+json": { "application/vnd.jupyter.widget-view+json": {
"model_id": "69e018750ffb4de1af22ce49cd6957f4", "model_id": "27c08180714a44c7ab766624d5054163",
"version_major": 2, "version_major": 2,
"version_minor": 0 "version_minor": 0
}, },
@@ -104,13 +103,12 @@
"name": "stderr", "name": "stderr",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"2024-02-23 18:10:22,896 - INFO - Converting the current model to sym_int4 format......\n", "2024-03-27 00:58:43,670 - INFO - Converting the current model to sym_int4 format......\n"
"2024-02-23 18:10:25,415 - INFO - BIGDL_OPT_IPEX: False\n"
] ]
} }
], ],
"source": [ "source": [
"llm = BigdlLLM.from_model_id(\n", "llm = IpexLLM.from_model_id(\n",
" model_id=\"lmsys/vicuna-7b-v1.5\",\n", " model_id=\"lmsys/vicuna-7b-v1.5\",\n",
" model_kwargs={\"temperature\": 0, \"max_length\": 64, \"trust_remote_code\": True},\n", " model_kwargs={\"temperature\": 0, \"max_length\": 64, \"trust_remote_code\": True},\n",
")" ")"
@@ -135,6 +133,10 @@
"/opt/anaconda3/envs/shane-langchain2/lib/python3.9/site-packages/langchain_core/_api/deprecation.py:117: LangChainDeprecationWarning: The function `run` was deprecated in LangChain 0.1.0 and will be removed in 0.2.0. Use invoke instead.\n", "/opt/anaconda3/envs/shane-langchain2/lib/python3.9/site-packages/langchain_core/_api/deprecation.py:117: LangChainDeprecationWarning: The function `run` was deprecated in LangChain 0.1.0 and will be removed in 0.2.0. Use invoke instead.\n",
" warn_deprecated(\n", " warn_deprecated(\n",
"/opt/anaconda3/envs/shane-langchain2/lib/python3.9/site-packages/transformers/generation/utils.py:1369: UserWarning: Using `max_length`'s default (4096) to control the generation length. This behaviour is deprecated and will be removed from the config in v5 of Transformers -- we recommend using `max_new_tokens` to control the maximum length of the generation.\n", "/opt/anaconda3/envs/shane-langchain2/lib/python3.9/site-packages/transformers/generation/utils.py:1369: UserWarning: Using `max_length`'s default (4096) to control the generation length. This behaviour is deprecated and will be removed from the config in v5 of Transformers -- we recommend using `max_new_tokens` to control the maximum length of the generation.\n",
" warnings.warn(\n",
"/opt/anaconda3/envs/shane-langchain2/lib/python3.9/site-packages/ipex_llm/transformers/models/llama.py:218: UserWarning: Passing `padding_mask` is deprecated and will be removed in v4.37.Please make sure use `attention_mask` instead.`\n",
" warnings.warn(\n",
"/opt/anaconda3/envs/shane-langchain2/lib/python3.9/site-packages/ipex_llm/transformers/models/llama.py:218: UserWarning: Passing `padding_mask` is deprecated and will be removed in v4.37.Please make sure use `attention_mask` instead.`\n",
" warnings.warn(\n" " warnings.warn(\n"
] ]
}, },
@@ -156,6 +158,13 @@
"question = \"What is AI?\"\n", "question = \"What is AI?\"\n",
"output = llm_chain.run(question)" "output = llm_chain.run(question)"
] ]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
} }
], ],
"metadata": { "metadata": {

View File

@@ -92,6 +92,10 @@
"source": "/docs/integrations/llms/huggingface_hub", "source": "/docs/integrations/llms/huggingface_hub",
"destination": "/docs/integrations/llms/huggingface_endpoint" "destination": "/docs/integrations/llms/huggingface_endpoint"
}, },
{
"source": "/docs/integrations/llms/bigdl",
"destination": "/docs/integrations/llms/ipex_llm"
},
{ {
"source": "/docs/integrations/llms/watsonxllm", "source": "/docs/integrations/llms/watsonxllm",
"destination": "/docs/integrations/llms/ibm_watsonx" "destination": "/docs/integrations/llms/ibm_watsonx"

View File

@@ -114,6 +114,12 @@ def _import_bedrock() -> Type[BaseLLM]:
return Bedrock return Bedrock
def _import_bigdlllm() -> Type[BaseLLM]:
from langchain_community.llms.bigdl_llm import BigdlLLM
return BigdlLLM
def _import_bittensor() -> Type[BaseLLM]: def _import_bittensor() -> Type[BaseLLM]:
from langchain_community.llms.bittensor import NIBittensorLLM from langchain_community.llms.bittensor import NIBittensorLLM
@@ -278,6 +284,12 @@ def _import_human() -> Type[BaseLLM]:
return HumanInputLLM return HumanInputLLM
def _import_ipex_llm() -> Type[BaseLLM]:
from langchain_community.llms.ipex_llm import IpexLLM
return IpexLLM
def _import_javelin_ai_gateway() -> Type[BaseLLM]: def _import_javelin_ai_gateway() -> Type[BaseLLM]:
from langchain_community.llms.javelin_ai_gateway import JavelinAIGateway from langchain_community.llms.javelin_ai_gateway import JavelinAIGateway
@@ -645,6 +657,8 @@ def __getattr__(name: str) -> Any:
return _import_beam() return _import_beam()
elif name == "Bedrock": elif name == "Bedrock":
return _import_bedrock() return _import_bedrock()
elif name == "BigdlLLM":
return _import_bigdlllm()
elif name == "NIBittensorLLM": elif name == "NIBittensorLLM":
return _import_bittensor() return _import_bittensor()
elif name == "CerebriumAI": elif name == "CerebriumAI":
@@ -695,6 +709,8 @@ def __getattr__(name: str) -> Any:
return _import_huggingface_text_gen_inference() return _import_huggingface_text_gen_inference()
elif name == "HumanInputLLM": elif name == "HumanInputLLM":
return _import_human() return _import_human()
elif name == "IpexLLM":
return _import_ipex_llm()
elif name == "JavelinAIGateway": elif name == "JavelinAIGateway":
return _import_javelin_ai_gateway() return _import_javelin_ai_gateway()
elif name == "KoboldApiLLM": elif name == "KoboldApiLLM":
@@ -851,6 +867,7 @@ __all__ = [
"HuggingFacePipeline", "HuggingFacePipeline",
"HuggingFaceTextGenInference", "HuggingFaceTextGenInference",
"HumanInputLLM", "HumanInputLLM",
"IpexLLM",
"JavelinAIGateway", "JavelinAIGateway",
"KoboldApiLLM", "KoboldApiLLM",
"Konko", "Konko",

View File

@@ -0,0 +1,145 @@
import logging
from typing import Any, Optional
from langchain_core.language_models.llms import LLM
from langchain_community.llms.ipex_llm import IpexLLM
logger = logging.getLogger(__name__)
class BigdlLLM(IpexLLM):
"""Wrapper around the BigdlLLM model
Example:
.. code-block:: python
from langchain_community.llms import BigdlLLM
llm = BigdlLLM.from_model_id(model_id="THUDM/chatglm-6b")
"""
@classmethod
def from_model_id(
cls,
model_id: str,
model_kwargs: Optional[dict] = None,
**kwargs: Any,
) -> LLM:
"""
Construct object from model_id
Args:
model_id: Path for the huggingface repo id to be downloaded or
the huggingface checkpoint folder.
model_kwargs: Keyword arguments to pass to the model and tokenizer.
kwargs: Extra arguments to pass to the model and tokenizer.
Returns:
An object of BigdlLLM.
"""
logger.warning("BigdlLLM was deprecated. Please use IpexLLM instead.")
try:
from bigdl.llm.transformers import (
AutoModel,
AutoModelForCausalLM,
)
from transformers import AutoTokenizer, LlamaTokenizer
except ImportError:
raise ValueError(
"Could not import bigdl-llm or transformers. "
"Please install it with `pip install --pre --upgrade bigdl-llm[all]`."
)
_model_kwargs = model_kwargs or {}
try:
tokenizer = AutoTokenizer.from_pretrained(model_id, **_model_kwargs)
except Exception:
tokenizer = LlamaTokenizer.from_pretrained(model_id, **_model_kwargs)
try:
model = AutoModelForCausalLM.from_pretrained(
model_id, load_in_4bit=True, **_model_kwargs
)
except Exception:
model = AutoModel.from_pretrained(
model_id, load_in_4bit=True, **_model_kwargs
)
if "trust_remote_code" in _model_kwargs:
_model_kwargs = {
k: v for k, v in _model_kwargs.items() if k != "trust_remote_code"
}
return cls(
model_id=model_id,
model=model,
tokenizer=tokenizer,
model_kwargs=_model_kwargs,
**kwargs,
)
@classmethod
def from_model_id_low_bit(
cls,
model_id: str,
model_kwargs: Optional[dict] = None,
**kwargs: Any,
) -> LLM:
"""
Construct low_bit object from model_id
Args:
model_id: Path for the bigdl-llm transformers low-bit model folder.
model_kwargs: Keyword arguments to pass to the model and tokenizer.
kwargs: Extra arguments to pass to the model and tokenizer.
Returns:
An object of BigdlLLM.
"""
logger.warning("BigdlLLM was deprecated. Please use IpexLLM instead.")
try:
from bigdl.llm.transformers import (
AutoModel,
AutoModelForCausalLM,
)
from transformers import AutoTokenizer, LlamaTokenizer
except ImportError:
raise ValueError(
"Could not import bigdl-llm or transformers. "
"Please install it with `pip install --pre --upgrade bigdl-llm[all]`."
)
_model_kwargs = model_kwargs or {}
try:
tokenizer = AutoTokenizer.from_pretrained(model_id, **_model_kwargs)
except Exception:
tokenizer = LlamaTokenizer.from_pretrained(model_id, **_model_kwargs)
try:
model = AutoModelForCausalLM.load_low_bit(model_id, **_model_kwargs)
except Exception:
model = AutoModel.load_low_bit(model_id, **_model_kwargs)
if "trust_remote_code" in _model_kwargs:
_model_kwargs = {
k: v for k, v in _model_kwargs.items() if k != "trust_remote_code"
}
return cls(
model_id=model_id,
model=model,
tokenizer=tokenizer,
model_kwargs=_model_kwargs,
**kwargs,
)
@property
def _llm_type(self) -> str:
return "bigdl-llm"

View File

@@ -7,17 +7,18 @@ from langchain_core.pydantic_v1 import Extra
DEFAULT_MODEL_ID = "gpt2" DEFAULT_MODEL_ID = "gpt2"
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
class BigdlLLM(LLM): class IpexLLM(LLM):
"""Wrapper around the BigDL-LLM Transformer-INT4 model """Wrapper around the IpexLLM model
Example: Example:
.. code-block:: python .. code-block:: python
from langchain.llms import TransformersLLM from langchain_community.llms import IpexLLM
llm = TransformersLLM.from_model_id(model_id="THUDM/chatglm-6b") llm = IpexLLM.from_model_id(model_id="THUDM/chatglm-6b")
""" """
model_id: str = DEFAULT_MODEL_ID model_id: str = DEFAULT_MODEL_ID
@@ -25,7 +26,7 @@ class BigdlLLM(LLM):
model_kwargs: Optional[dict] = None model_kwargs: Optional[dict] = None
"""Keyword arguments passed to the model.""" """Keyword arguments passed to the model."""
model: Any #: :meta private: model: Any #: :meta private:
"""BigDL-LLM Transformers-INT4 model.""" """IpexLLM model."""
tokenizer: Any #: :meta private: tokenizer: Any #: :meta private:
"""Huggingface tokenizer model.""" """Huggingface tokenizer model."""
streaming: bool = True streaming: bool = True
@@ -53,10 +54,10 @@ class BigdlLLM(LLM):
kwargs: Extra arguments to pass to the model and tokenizer. kwargs: Extra arguments to pass to the model and tokenizer.
Returns: Returns:
An object of TransformersLLM. An object of IpexLLM.
""" """
try: try:
from bigdl.llm.transformers import ( from ipex_llm.transformers import (
AutoModel, AutoModel,
AutoModelForCausalLM, AutoModelForCausalLM,
) )
@@ -64,8 +65,8 @@ class BigdlLLM(LLM):
except ImportError: except ImportError:
raise ValueError( raise ValueError(
"Could not import bigdl-llm or transformers. " "Could not import ipex-llm or transformers. "
"Please install it with `pip install --pre --upgrade bigdl-llm[all]`." "Please install it with `pip install --pre --upgrade ipex-llm[all]`."
) )
_model_kwargs = model_kwargs or {} _model_kwargs = model_kwargs or {}
@@ -109,15 +110,15 @@ class BigdlLLM(LLM):
Args: Args:
model_id: Path for the bigdl transformers low-bit model checkpoint folder. model_id: Path for the ipex-llm transformers low-bit model folder.
model_kwargs: Keyword arguments to pass to the model and tokenizer. model_kwargs: Keyword arguments to pass to the model and tokenizer.
kwargs: Extra arguments to pass to the model and tokenizer. kwargs: Extra arguments to pass to the model and tokenizer.
Returns: Returns:
An object of TransformersLLM. An object of IpexLLM.
""" """
try: try:
from bigdl.llm.transformers import ( from ipex_llm.transformers import (
AutoModel, AutoModel,
AutoModelForCausalLM, AutoModelForCausalLM,
) )
@@ -125,8 +126,8 @@ class BigdlLLM(LLM):
except ImportError: except ImportError:
raise ValueError( raise ValueError(
"Could not import bigdl-llm or transformers. " "Could not import ipex-llm or transformers. "
"Please install it with `pip install --pre --upgrade bigdl-llm[all]`" "Please install it with `pip install --pre --upgrade ipex-llm[all]`."
) )
_model_kwargs = model_kwargs or {} _model_kwargs = model_kwargs or {}
@@ -163,7 +164,7 @@ class BigdlLLM(LLM):
@property @property
def _llm_type(self) -> str: def _llm_type(self) -> str:
return "BigDL-llm" return "ipex-llm"
def _call( def _call(
self, self,

View File

@@ -1,11 +1,11 @@
"""Test BigDL LLM""" """Test BigdlLLM"""
from langchain_core.outputs import LLMResult from langchain_core.outputs import LLMResult
from langchain_community.llms.bigdl import BigdlLLM from langchain_community.llms.bigdl_llm import BigdlLLM
def test_call() -> None: def test_call() -> None:
"""Test valid call to baichuan.""" """Test valid call to bigdl-llm."""
llm = BigdlLLM.from_model_id( llm = BigdlLLM.from_model_id(
model_id="lmsys/vicuna-7b-v1.5", model_id="lmsys/vicuna-7b-v1.5",
model_kwargs={"temperature": 0, "max_length": 16, "trust_remote_code": True}, model_kwargs={"temperature": 0, "max_length": 16, "trust_remote_code": True},
@@ -15,7 +15,7 @@ def test_call() -> None:
def test_generate() -> None: def test_generate() -> None:
"""Test valid call to baichuan.""" """Test valid call to bigdl-llm."""
llm = BigdlLLM.from_model_id( llm = BigdlLLM.from_model_id(
model_id="lmsys/vicuna-7b-v1.5", model_id="lmsys/vicuna-7b-v1.5",
model_kwargs={"temperature": 0, "max_length": 16, "trust_remote_code": True}, model_kwargs={"temperature": 0, "max_length": 16, "trust_remote_code": True},

View File

@@ -0,0 +1,25 @@
"""Test IPEX LLM"""
from langchain_core.outputs import LLMResult
from langchain_community.llms.ipex_llm import IpexLLM
def test_call() -> None:
"""Test valid call to ipex-llm."""
llm = IpexLLM.from_model_id(
model_id="lmsys/vicuna-7b-v1.5",
model_kwargs={"temperature": 0, "max_length": 16, "trust_remote_code": True},
)
output = llm("Hello!")
assert isinstance(output, str)
def test_generate() -> None:
"""Test valid call to ipex-llm."""
llm = IpexLLM.from_model_id(
model_id="lmsys/vicuna-7b-v1.5",
model_kwargs={"temperature": 0, "max_length": 16, "trust_remote_code": True},
)
output = llm.generate(["Hello!"])
assert isinstance(output, LLMResult)
assert isinstance(output.generations, list)

View File

@@ -42,6 +42,7 @@ EXPECT_ALL = [
"HuggingFacePipeline", "HuggingFacePipeline",
"HuggingFaceTextGenInference", "HuggingFaceTextGenInference",
"HumanInputLLM", "HumanInputLLM",
"IpexLLM",
"KoboldApiLLM", "KoboldApiLLM",
"Konko", "Konko",
"LlamaCpp", "LlamaCpp",